<commit_before>Given /^the following empty investigations exist:$/ do |table|
  table.hashes.each do |hash|
    user = User.find_by_login hash['user']
    Factory.create(:investigation, hash.merge('user' => user))
  end
end

Given /^the following linked investigations exist:$/ do |table|
  table.hashes.each do |hash|
    user = User.find_by_login hash['user']
    inv = Factory.create(:investigation, hash.merge('user' => user))
      inv.activities << (Factory :activity, { :user => user })
      inv.activities[0].sections << (Factory :section, {:user => user})
      inv.activities[0].sections[0].pages << (Factory :page, {:user => user})
      open_response = (Factory :open_response, {:user => user})
      open_response.pages << inv.activities[0].sections[0].pages[0]
      draw_tool = (Factory :drawing_tool, {:user => user, :background_image_url => "https://lh4.googleusercontent.com/-xcAHK6vd6Pc/Tw24Oful6sI/AAAAAAAAB3Y/iJBgijBzi10/s800/4757765621_6f5be93743_b.jpg"})
      draw_tool.pages << inv.activities[0].sections[0].pages[0]
      snapshot_button = (Factory :lab_book_snapshot, {:user => user, :target_element => draw_tool})
      snapshot_button.pages << inv.activities[0].sections[0].pages[0]
      prediction_graph = (Factory :data_collector, {:user => user})
      prediction_graph.pages << inv.activities[0].sections[0].pages[0]
      displaying_graph = (Factory :data_collector, {:user => user, :prediction_graph_source => prediction_graph})
      displaying_graph.pages << inv.activities[0].sections[0].pages[0]
      inv.reload
  end
end

Given /^the following simple investigations exist:$/ do |investigation_table|
  investigation_table.hashes.each do |hash|
    user = User.first(:conditions => { :login => hash.delete('user') })
    hash[:user_id] = user.id
    investigation = Investigation.create(hash)
    activity = Activity.create(hash)
    section = Section.create(hash)
    page = Page.create(hash)
    section.pages << page
    activity.sections << section
    investigation.activities << activity
    investigation.save
  end
end

Given /^the author "([^"]*)" created an investigation named "([^"]*)" with text and a open response question$/ do |author, name|
  user = User.first(:conditions => { :login => author })
  hash = {:user_id => user.id, :name => name}
  investigation = Investigation.create(hash)
  activity = Activity.create(hash)
  section = Section.create(hash)
  page = Page.create(hash)
  section.pages << page
  activity.sections << section
  investigation.activities << activity
  page.add_embeddable(Embeddable::Xhtml.create(hash))
  page.add_embeddable(Embeddable::OpenResponse.create(hash))
  page.save
  investigation.save
end

#Table: | investigation | activity | activity_teacher_only | section   | page   | multiple_choices |
Given /^the following investigations with multiple choices exist:$/ do |investigation_table|
  investigation_table.hashes.each do |hash|
    investigation = Investigation.find_or_create_by_name(hash['investigation'])
    investigation.user = Factory(:user)
    investigation.save
    # ITSISU requires descriptions on activities
    activity = Activity.find_or_create_by_name(hash['activity'], :description => hash['activity'])
    
    if hash['activity_teacher_only']
      # Create a teacher only activity if specified
      activity.teacher_only = (hash['activity_teacher_only'] == 'true')
      activity.save
    end
    
    section = Section.find_or_create_by_name(hash['section'])
    page = Page.find_or_create_by_name(hash['page'])
    mcs = hash['multiple_choices'].split(",").map{ |q| Embeddable::MultipleChoice.find_by_prompt(q.strip) }
    mcs.each do |q|
      q.pages << page
    end
    imgqs = hash['image_questions'].split(",").map{ |q| Embeddable::ImageQuestion.find_by_prompt(q.strip) }
    imgqs.each do |q|
      q.pages << page
    end
    page.save
    section.pages << page
    activity.sections << section
    investigation.activities << activity
  end
end

Given /^the following semesters exist:$/ do|semesters_table|
   semesters_table.hashes.each do |hash|
     Factory.create(:portal_semester, hash)
   end
end


Given /^the following classes exist:$/ do |table|
  table.hashes.each do |hash|
    if hash['teacher']
      user = User.find_by_login hash['teacher']
      teacher = user.portal_teacher
    else
      teacher = Factory(:teacher)
    end
    hash.merge!('teacher' => teacher)
    
    if !(hash['semester'].nil?) then
      semester = Portal::Semester.find_by_name(hash['semester']);
      if (semester.nil?) then
        semester = Factory.create(:portal_semester, :name => hash['semester']);
      end
      hash.merge!('semester' => semester);
    end
    
    Factory.create(:portal_clazz, hash)
  end
end

Given /^the investigation "([^"]*)" is published$/ do |investigation_name|
  investigation = Investigation.find_by_name investigation_name
  investigation.publish
  investigation.save
end

When /^I sort investigations by "([^"]*)"$/ do |sort_str|
  visit "/investigations?sort_order=#{sort_str}"
end

When /sort order .*should be "([^"]*)"/ do |sort_str|
  page.should have_selector('select[name="[sort_order]"]')
  page.should have_selector("option[value='#{sort_str}'][selected='selected']")
end

When /^I drag the investigation "([^"]*)" to "([^"]*)"$/ do |investigation_name, to|
  investigation = Investigation.find_by_name investigation_name
  selector_id = "#investigation_#{investigation.id}"
  selector = find(selector_id)
  drop = find(to)
  # NP 2011-09 see support/drag_and_drop.rb
  # TODO: When Selenium issue ( http://bit.ly/q9LHR4 ) closes 
  # use the actual dragging code which we replaced
  #
  # selector.drag_to(drop)
  fake_drop(selector_id,to)
end


Then /^I should not see the "([^"]*)" checkbox in the list filter$/ do |arg1|
  page.should_not have_selector("input[name='#{arg1}'][type='checkbox']")
end

When /^I show offerings count on the investigations page$/ do 
  visit "/investigations?include_usage_count=true"
end

When /^I remove the investigation "([^"]*)" from the class "([^"]*)"$/ do |investigation_name, class_name|
  clazz = Portal::Clazz.find_by_name(class_name)
  investigation = Investigation.find_by_name(investigation_name)
  offering = Portal::Offering.first(:conditions => {
    :runnable_type => investigation.class.name,
    :runnable_id => investigation.id,
    :clazz_id => clazz.id
  })
  visit "/portal/classes/#{clazz.id}/remove_offering?offering_id=#{offering.id}"
end


When /^I follow "(.*)" on the (.*) "(.*)" from the class "(.*)"$/ do |button_name, model_name, obj_name, class_name|
  the_class = model_name.gsub(/\s/, '_').singularize.classify.constantize
  clazz = Portal::Clazz.find_by_name(class_name)
  obj = the_class.find_by_name(obj_name)
  offering = Portal::Offering.first(:conditions => {
    :runnable_type => obj.class.name,
    :runnable_id => obj.id,
    :clazz_id => clazz.id
  })
  
  selector = "#portal__offering_#{offering.id}"
  with_scope(selector) do
    click_link(button_name)
  end
end

When /^a student has performed work on the investigation "([^"]*)" for the class "([^"]*)"$/ do |investigation_name, class_name|
  clazz = Portal::Clazz.find_by_name(class_name)
  investigation = Investigation.find_by_name(investigation_name)
  offering = Portal::Offering.first(:conditions => {
    :runnable_type => investigation.class.name,
    :runnable_id => investigation.id,
    :clazz_id => clazz.id
  })
  Factory.create(:full_portal_learner, :offering => offering)
end

When /^I open the accordion for the offering for investigation "([^"]*)" for the class "([^"]*)"$/ do |investigation_name, class_name|
  clazz = Portal::Clazz.find_by_name(class_name)
  investigation = Investigation.find_by_name(investigation_name)
  offering = Portal::Offering.first(:conditions => {
    :runnable_type => investigation.class.name,
    :runnable_id => investigation.id,
    :clazz_id => clazz.id
  })
  selector = "#_toggle_portal__offering_#{offering.id}"
  find(selector).click
end


When /^I drag the investigation "([^"]*)" in the class "(.*)" to "([^"]*)"$/ do |investigation_name, class_name, to|
  clazz = Portal::Clazz.find_by_name(class_name)
  investigation = Investigation.find_by_name(investigation_name)
  offering = Portal::Offering.first(:conditions => {
    :runnable_type => investigation.class.name,
    :runnable_id => investigation.id,
    :clazz_id => clazz.id
  })
  selector = "#portal__offering_#{offering.id}"
  # NP 2011-09 see support/drag_and_drop.rb
  # TODO: When Selenium issue ( http://bit.ly/q9LHR4 ) closes 
  # use the actual dragging code which we replaced
  #
  # find(selector).drag_to(find(to))
  
  fake_drop(selector,to)
end


Then /^the investigation "([^"]*)" in the class "(.*)" should be active$/ do |investigation_name, class_name|
  clazz = Portal::Clazz.find_by_name(class_name)
  investigation = Investigation.find_by_name(investigation_name)
  offering = Portal::Offering.first(:conditions => {
    :runnable_type => investigation.class.name,
    :runnable_id => investigation.id,
    :clazz_id => clazz.id
  })
  offering.should be_active
end


#Then /^"([^\"]*)" should have ([0-9]+) "([^\"]*)"$/ do |selector1, count, selector2|
  #within(selector1) do |content|
       #content.should have_selector(selector2, :count => count.to_i)
  #end
#end


Then /^There should be (\d+) (?:investigations|assignables) displayed$/ do |count|
  within("#offering_list") do
    page.all(".runnable").size.should == count.to_i
  end
end

Then /^"([^"]*)" should not be displayed in the (?:investigations|assignables) list$/ do |not_expected|
  within("#offering_list") do 
    page.should have_no_content(not_expected)
  end
end

Then /the following should (not )?be displayed in the (?:investigations|assignables) list:$/ do |nomatch, table|
  within('#assignable_list') do
    table.hashes.each do |hash|
      if nomatch == "not "
        page.should have_no_content(hash[:name])
      else
        page.should have_content(hash[:name])
      end
    end
  end
end

When /^I click on the next page of results$/ do
  within('.pagination') do
    click_link('Next')
  end
end

When /^I browse public investigations$/ do
  visit "/investigations"
end

When /^I browse draft investigations$/ do
  visit "/investigations?include_drafts=true"
end

Then /^"([^"]*)" should be displayed in the investigations list$/ do |expected|
  within("#offering_list") do 
    page.should have_content(expected)
  end<commit_msg>add missing carrot, helps with cucumber ide tools<commit_after>Given /^the following empty investigations exist:$/ do |table|
  table.hashes.each do |hash|
    user = User.find_by_login hash['user']
    Factory.create(:investigation, hash.merge('user' => user))
  end
end

Given /^the following linked investigations exist:$/ do |table|
  table.hashes.each do |hash|
    user = User.find_by_login hash['user']
    inv = Factory.create(:investigation, hash.merge('user' => user))
      inv.activities << (Factory :activity, { :user => user })
      inv.activities[0].sections << (Factory :section, {:user => user})
      inv.activities[0].sections[0].pages << (Factory :page, {:user => user})
      open_response = (Factory :open_response, {:user => user})
      open_response.pages << inv.activities[0].sections[0].pages[0]
      draw_tool = (Factory :drawing_tool, {:user => user, :background_image_url => "https://lh4.googleusercontent.com/-xcAHK6vd6Pc/Tw24Oful6sI/AAAAAAAAB3Y/iJBgijBzi10/s800/4757765621_6f5be93743_b.jpg"})
      draw_tool.pages << inv.activities[0].sections[0].pages[0]
      snapshot_button = (Factory :lab_book_snapshot, {:user => user, :target_element => draw_tool})
      snapshot_button.pages << inv.activities[0].sections[0].pages[0]
      prediction_graph = (Factory :data_collector, {:user => user})
      prediction_graph.pages << inv.activities[0].sections[0].pages[0]
      displaying_graph = (Factory :data_collector, {:user => user, :prediction_graph_source => prediction_graph})
      displaying_graph.pages << inv.activities[0].sections[0].pages[0]
      inv.reload
  end
end

Given /^the following simple investigations exist:$/ do |investigation_table|
  investigation_table.hashes.each do |hash|
    user = User.first(:conditions => { :login => hash.delete('user') })
    hash[:user_id] = user.id
    investigation = Investigation.create(hash)
    activity = Activity.create(hash)
    section = Section.create(hash)
    page = Page.create(hash)
    section.pages << page
    activity.sections << section
    investigation.activities << activity
    investigation.save
  end
end

Given /^the author "([^"]*)" created an investigation named "([^"]*)" with text and a open response question$/ do |author, name|
  user = User.first(:conditions => { :login => author })
  hash = {:user_id => user.id, :name => name}
  investigation = Investigation.create(hash)
  activity = Activity.create(hash)
  section = Section.create(hash)
  page = Page.create(hash)
  section.pages << page
  activity.sections << section
  investigation.activities << activity
  page.add_embeddable(Embeddable::Xhtml.create(hash))
  page.add_embeddable(Embeddable::OpenResponse.create(hash))
  page.save
  investigation.save
end

#Table: | investigation | activity | activity_teacher_only | section   | page   | multiple_choices |
Given /^the following investigations with multiple choices exist:$/ do |investigation_table|
  investigation_table.hashes.each do |hash|
    investigation = Investigation.find_or_create_by_name(hash['investigation'])
    investigation.user = Factory(:user)
    investigation.save
    # ITSISU requires descriptions on activities
    activity = Activity.find_or_create_by_name(hash['activity'], :description => hash['activity'])
    
    if hash['activity_teacher_only']
      # Create a teacher only activity if specified
      activity.teacher_only = (hash['activity_teacher_only'] == 'true')
      activity.save
    end
    
    section = Section.find_or_create_by_name(hash['section'])
    page = Page.find_or_create_by_name(hash['page'])
    mcs = hash['multiple_choices'].split(",").map{ |q| Embeddable::MultipleChoice.find_by_prompt(q.strip) }
    mcs.each do |q|
      q.pages << page
    end
    imgqs = hash['image_questions'].split(",").map{ |q| Embeddable::ImageQuestion.find_by_prompt(q.strip) }
    imgqs.each do |q|
      q.pages << page
    end
    page.save
    section.pages << page
    activity.sections << section
    investigation.activities << activity
  end
end

Given /^the following semesters exist:$/ do|semesters_table|
   semesters_table.hashes.each do |hash|
     Factory.create(:portal_semester, hash)
   end
end


Given /^the following classes exist:$/ do |table|
  table.hashes.each do |hash|
    if hash['teacher']
      user = User.find_by_login hash['teacher']
      teacher = user.portal_teacher
    else
      teacher = Factory(:teacher)
    end
    hash.merge!('teacher' => teacher)
    
    if !(hash['semester'].nil?) then
      semester = Portal::Semester.find_by_name(hash['semester']);
      if (semester.nil?) then
        semester = Factory.create(:portal_semester, :name => hash['semester']);
      end
      hash.merge!('semester' => semester);
    end
    
    Factory.create(:portal_clazz, hash)
  end
end

Given /^the investigation "([^"]*)" is published$/ do |investigation_name|
  investigation = Investigation.find_by_name investigation_name
  investigation.publish
  investigation.save
end

When /^I sort investigations by "([^"]*)"$/ do |sort_str|
  visit "/investigations?sort_order=#{sort_str}"
end

When /sort order .*should be "([^"]*)"/ do |sort_str|
  page.should have_selector('select[name="[sort_order]"]')
  page.should have_selector("option[value='#{sort_str}'][selected='selected']")
end

When /^I drag the investigation "([^"]*)" to "([^"]*)"$/ do |investigation_name, to|
  investigation = Investigation.find_by_name investigation_name
  selector_id = "#investigation_#{investigation.id}"
  selector = find(selector_id)
  drop = find(to)
  # NP 2011-09 see support/drag_and_drop.rb
  # TODO: When Selenium issue ( http://bit.ly/q9LHR4 ) closes 
  # use the actual dragging code which we replaced
  #
  # selector.drag_to(drop)
  fake_drop(selector_id,to)
end


Then /^I should not see the "([^"]*)" checkbox in the list filter$/ do |arg1|
  page.should_not have_selector("input[name='#{arg1}'][type='checkbox']")
end

When /^I show offerings count on the investigations page$/ do 
  visit "/investigations?include_usage_count=true"
end

When /^I remove the investigation "([^"]*)" from the class "([^"]*)"$/ do |investigation_name, class_name|
  clazz = Portal::Clazz.find_by_name(class_name)
  investigation = Investigation.find_by_name(investigation_name)
  offering = Portal::Offering.first(:conditions => {
    :runnable_type => investigation.class.name,
    :runnable_id => investigation.id,
    :clazz_id => clazz.id
  })
  visit "/portal/classes/#{clazz.id}/remove_offering?offering_id=#{offering.id}"
end


When /^I follow "(.*)" on the (.*) "(.*)" from the class "(.*)"$/ do |button_name, model_name, obj_name, class_name|
  the_class = model_name.gsub(/\s/, '_').singularize.classify.constantize
  clazz = Portal::Clazz.find_by_name(class_name)
  obj = the_class.find_by_name(obj_name)
  offering = Portal::Offering.first(:conditions => {
    :runnable_type => obj.class.name,
    :runnable_id => obj.id,
    :clazz_id => clazz.id
  })
  
  selector = "#portal__offering_#{offering.id}"
  with_scope(selector) do
    click_link(button_name)
  end
end

When /^a student has performed work on the investigation "([^"]*)" for the class "([^"]*)"$/ do |investigation_name, class_name|
  clazz = Portal::Clazz.find_by_name(class_name)
  investigation = Investigation.find_by_name(investigation_name)
  offering = Portal::Offering.first(:conditions => {
    :runnable_type => investigation.class.name,
    :runnable_id => investigation.id,
    :clazz_id => clazz.id
  })
  Factory.create(:full_portal_learner, :offering => offering)
end

When /^I open the accordion for the offering for investigation "([^"]*)" for the class "([^"]*)"$/ do |investigation_name, class_name|
  clazz = Portal::Clazz.find_by_name(class_name)
  investigation = Investigation.find_by_name(investigation_name)
  offering = Portal::Offering.first(:conditions => {
    :runnable_type => investigation.class.name,
    :runnable_id => investigation.id,
    :clazz_id => clazz.id
  })
  selector = "#_toggle_portal__offering_#{offering.id}"
  find(selector).click
end


When /^I drag the investigation "([^"]*)" in the class "(.*)" to "([^"]*)"$/ do |investigation_name, class_name, to|
  clazz = Portal::Clazz.find_by_name(class_name)
  investigation = Investigation.find_by_name(investigation_name)
  offering = Portal::Offering.first(:conditions => {
    :runnable_type => investigation.class.name,
    :runnable_id => investigation.id,
    :clazz_id => clazz.id
  })
  selector = "#portal__offering_#{offering.id}"
  # NP 2011-09 see support/drag_and_drop.rb
  # TODO: When Selenium issue ( http://bit.ly/q9LHR4 ) closes 
  # use the actual dragging code which we replaced
  #
  # find(selector).drag_to(find(to))
  
  fake_drop(selector,to)
end


Then /^the investigation "([^"]*)" in the class "(.*)" should be active$/ do |investigation_name, class_name|
  clazz = Portal::Clazz.find_by_name(class_name)
  investigation = Investigation.find_by_name(investigation_name)
  offering = Portal::Offering.first(:conditions => {
    :runnable_type => investigation.class.name,
    :runnable_id => investigation.id,
    :clazz_id => clazz.id
  })
  offering.should be_active
end


#Then /^"([^\"]*)" should have ([0-9]+) "([^\"]*)"$/ do |selector1, count, selector2|
  #within(selector1) do |content|
       #content.should have_selector(selector2, :count => count.to_i)
  #end
#end


Then /^There should be (\d+) (?:investigations|assignables) displayed$/ do |count|
  within("#offering_list") do
    page.all(".runnable").size.should == count.to_i
  end
end

Then /^"([^"]*)" should not be displayed in the (?:investigations|assignables) list$/ do |not_expected|
  within("#offering_list") do 
    page.should have_no_content(not_expected)
  end
end

Then /^the following should (not )?be displayed in the (?:investigations|assignables) list:$/ do |nomatch, table|
  within('#assignable_list') do
    table.hashes.each do |hash|
      if nomatch == "not "
        page.should have_no_content(hash[:name])
      else
        page.should have_content(hash[:name])
      end
    end
  end
end

When /^I click on the next page of results$/ do
  within('.pagination') do
    click_link('Next')
  end
end

When /^I browse public investigations$/ do
  visit "/investigations"
end

When /^I browse draft investigations$/ do
  visit "/investigations?include_drafts=true"
end

Then /^"([^"]*)" should be displayed in the investigations list$/ do |expected|
  within("#offering_list") do 
    page.should have_content(expected)
  end
{'fraction_non_alphanumeric': 0.14448524152400935, 'fraction_numerical': 0.006771255543369415, 'mean_word_length': 4.333672431332655, 'pattern_counts': {'":': 0, '<': 79, '<?xml version=': 0, '>': 101, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '3625112', 'n_tokens_mistral': 7083, 'n_tokens_neox': 6802, 'n_words': 1490}