<commit_before>require 'spec/spec_helper'

When /^I fill in the basic details of a child$/ do
  fill_in("Birthplace", :with => "Haiti")
end

When /^I attach a photo "([^"]*)"$/ do |photo_path|
    step %Q{I attach the file "#{photo_path}" to "child_photo0"}
end

When /^I attach an audio file "([^"]*)"$/ do |audio_path|
    step %Q{I attach the file "#{audio_path}" to "child[audio]"}
end

When /^I attach the following photos:$/ do |table|
  table.raw.each_with_index do |photo, i|
    step %Q{I attach the file "#{photo}" to "child[photo]#{i}"}
  end
end

Given /^the following form sections exist in the system:$/ do |form_sections_table|
  FormSection.all.each {|u| u.destroy }

  form_sections_table.hashes.each do |form_section_hash|
    form_section_hash.reverse_merge!(
      'unique_id'=> form_section_hash["name"].gsub(/\s/, "_").downcase,
      'fields'=> Array.new
    )

    form_section_hash["order"] = form_section_hash["order"].to_i
    form_section = FormSection.new(form_section_hash)
    form_section.save!
  end
end

Given /^the "([^\"]*)" form section has the field "([^\"]*)" with field type "([^\"]*)"$/ do |form_section, field_name, field_type|
  form_section = FormSection.get_by_unique_id(form_section.downcase.gsub(/\s/, "_"))
  field = Field.new(:name => field_name.dehumanize, :display_name => field_name, :type => field_type)
  FormSection.add_field_to_formsection(form_section, field)
end

Given /^the following fields exists on "([^"]*)":$/ do |form_section_name, table|
  form_section = FormSection.get_by_unique_id(form_section_name)
  form_section.should_not be_nil
  form_section.fields = []
  table.hashes.each do |field_hash|
    field_hash.reverse_merge!(
      'visible' => true,
      'type'=> Field::TEXT_FIELD
    )
    form_section.fields.push Field.new(field_hash)
  end
  form_section.save!
end

Then /^there should be (\d+) child records in the database$/ do |number_of_records|
  Child.all.length.should == number_of_records.to_i
end

When /^the date\/time is "([^\"]*)"$/ do |datetime|
  current_time = Time.parse(datetime)
  current_time.stub!(:getutc).and_return Time.parse(datetime)
  Clock.stub!(:now).and_return current_time
end

When /^the local date\/time is "([^\"]*)" and UTC time is "([^\"]*)"$/ do |datetime, utcdatetime|
  current_time = Time.parse(datetime)
  current_time_in_utc = Time.parse(utcdatetime)
  Clock.stub!(:now).and_return current_time
  current_time.stub!(:getutc).and_return current_time_in_utc
end

Given /^a child record named "([^"]*)" exists with a audio file with the name "([^"]*)"$/ do |name, filename|
  user = User.create!("user_name" => "bob_creator",
               "password" => "<PASSWORD>",
               "password_confirmation" => "<PASSWORD>",
               "full_name" => "<NAME>",
               "organisation" => "UNICEF",
               "email" => "<EMAIL>",
               "disabled" => "false",
               "role_ids" => "ADMIN")
  child = Child.new_with_user_name(user,{:name=>name})
  child.audio = uploadable_audio("features/resources/#{filename}")
  child.create!
end

Given /^I am editing an existing child record$/ do
  child = Child.new
  child[:created_by] = "mary"
  child["birthplace"] = "haiti"
  child.photo = uploadable_photo
  child["unique_identifier"] = "UNIQUE_IDENTIFIER"
  raise "Failed to save a valid child record" unless child.save

  visit children_path+"/#{child.id}/edit"
end

Given /^an existing child with name "([^\"]*)" and a photo from "([^\"]*)"$/ do |name, photo_file_path|
  child = Child.new( :name => name, :birthplace => 'unknown' )
  child.photo = uploadable_photo(photo_file_path)
  child.create
end

When /^I am editing the child with name "([^\"]*)"$/ do |name|
  child = find_child_by_name name
  visit children_path+"/#{child.id}/edit"
end

When /^I wait for (\d+) seconds$/ do |seconds|
  sleep seconds.to_i
end

When 'I wait for the page to load' do
  wait_until { page.evaluate_script('$ && $.active == 0') } if Capybara.current_driver == :selenium<commit_msg>1644 - Akash - fixing broken cuke<commit_after>require 'spec/spec_helper'

When /^I fill in the basic details of a child$/ do
  fill_in("Birthplace", :with => "Haiti")
end

When /^I attach a photo "([^"]*)"$/ do |photo_path|
    step %Q{I attach the file "#{photo_path}" to "child_photo0"}
end

When /^I attach an audio file "([^"]*)"$/ do |audio_path|
    step %Q{I attach the file "#{audio_path}" to "child[audio]"}
end

When /^I attach the following photos:$/ do |table|
  table.raw.each_with_index do |photo, i|
    step %Q{I attach the file "#{photo}" to "child[photo]#{i}"}
  end
end

Given /^the following form sections exist in the system:$/ do |form_sections_table|
  FormSection.all.each {|u| u.destroy }

  form_sections_table.hashes.each do |form_section_hash|
    form_section_hash.reverse_merge!(
      'unique_id'=> form_section_hash["name"].gsub(/\s/, "_").downcase,
      'fields'=> Array.new
    )

    form_section_hash["order"] = form_section_hash["order"].to_i
    form_section = FormSection.new(form_section_hash)
    form_section.save!
  end
end

Given /^the "([^\"]*)" form section has the field "([^\"]*)" with field type "([^\"]*)"$/ do |form_section, field_name, field_type|
  form_section = FormSection.get_by_unique_id(form_section.downcase.gsub(/\s/, "_"))
  field = Field.new(:name => field_name.dehumanize, :display_name => field_name, :type => field_type)
  FormSection.add_field_to_formsection(form_section, field)
end

Given /^the following fields exists on "([^"]*)":$/ do |form_section_name, table|
  form_section = FormSection.get_by_unique_id(form_section_name)
  form_section.should_not be_nil
  form_section.fields = []
  table.hashes.each do |field_hash|
    field_hash.reverse_merge!(
      'visible' => true,
      'type'=> Field::TEXT_FIELD
    )
    form_section.fields.push Field.new(field_hash)
  end
  form_section.save!
end

Then /^there should be (\d+) child records in the database$/ do |number_of_records|
  Child.all.length.should == number_of_records.to_i
end

When /^the date\/time is "([^\"]*)"$/ do |datetime|
  current_time = Time.parse(datetime)
  current_time.stub!(:getutc).and_return Time.parse(datetime)
  Clock.stub!(:now).and_return current_time
end

When /^the local date\/time is "([^\"]*)" and UTC time is "([^\"]*)"$/ do |datetime, utcdatetime|
  current_time = Time.parse(datetime)
  current_time_in_utc = Time.parse(utcdatetime)
  Clock.stub!(:now).and_return current_time
  current_time.stub!(:getutc).and_return current_time_in_utc
end

Given /^a child record named "([^"]*)" exists with a audio file with the name "([^"]*)"$/ do |name, filename|
  user = User.create!("user_name" => "bob_creator",
               "password" => "<PASSWORD>",
               "password_confirmation" => "<PASSWORD>",
               "full_name" => "<NAME>",
               "organisation" => "UNICEF",
               "email" => "<EMAIL>",
               "disabled" => "false",
               "role_ids" => "ADMIN")
  child = Child.new_with_user_name(user,{:name=>name})
  child.audio = uploadable_audio("features/resources/#{filename}")
  child.create!
end

Given /^I am editing an existing child record$/ do
  child = Child.new
  child[:created_by] = "mary"
  child["birthplace"] = "haiti"
  child.photo = uploadable_photo
  child["unique_identifier"] = "UNIQUE_IDENTIFIER"
  raise "Failed to save a valid child record" unless child.save

  visit children_path+"/#{child.id}/edit"
end

Given /^an existing child with name "([^\"]*)" and a photo from "([^\"]*)"$/ do |name, photo_file_path|
  child = Child.new( :name => name, :birthplace => 'unknown', :created_by => "mary")
  child.photo = uploadable_photo(photo_file_path)
  child.create
end

When /^I am editing the child with name "([^\"]*)"$/ do |name|
  child = find_child_by_name name
  visit children_path+"/#{child.id}/edit"
end

When /^I wait for (\d+) seconds$/ do |seconds|
  sleep seconds.to_i
end

When 'I wait for the page to load' do
  wait_until { page.evaluate_script('$ && $.active == 0') } if Capybara.current_driver == :selenium
{'fraction_non_alphanumeric': 0.1609623015873016, 'fraction_numerical': 0.000992063492063492, 'mean_word_length': 4.037476577139288, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '669112', 'n_tokens_mistral': 2829, 'n_tokens_neox': 2760, 'n_words': 687}