<commit_before>require File.expand_path(File.dirname(__FILE__) + '/common')

describe "dashboard" do
  it_should_behave_like "in-process server selenium tests"

  def test_hiding(url)
    factory_with_protected_attributes(Announcement, :context => @course, :title => "hey all read this k", :message => "announcement")
    items = @user.stream_item_instances
    items.size.should == 1
    items.first.hidden.should == false

    get url
    find_all_with_jquery("div.communication_message.announcement").size.should == 1
    # force the element to be visible so we can click it -- webdriver has a
    # hover() event but it only works on Windows so far
    driver.execute_script("$('div.communication_message.announcement .disable_item_link').css('visibility', 'visible')")
    driver.find_element(:css, "div.communication_message.announcement .disable_item_link").click
    keep_trying_until { find_all_with_jquery("div.communication_message.announcement").size.should == 0 }

    # should still be gone on reload
    get url
    find_all_with_jquery("div.communication_message.announcement").size.should == 0

    @user.stream_items.size.should == 0
    items.first.reload.hidden.should == true
  end

  it "should allow hiding a stream item on the dashboard" do
    course_with_student_logged_in(:active_all => true)
    test_hiding("/")
  end

  it "should allow hiding a stream item on the course page" do
    course_with_student_logged_in(:active_all => true)
    test_hiding("/courses/#{@course.to_param}")
  end

  it "should show conversation stream items on the dashboard" do
    course_with_student_logged_in(:active_all => true)
    c = User.create.initiate_conversation([@user.id, User.create.id])
    c.add_message('test')
    c.add_participants([User.create.id])

    items = @user.stream_item_instances
    items.size.should == 1

    get "/"
    find_all_with_jquery("div.communication_message.conversation").size.should == 1
    find_all_with_jquery("div.communication_message.conversation .communication_sub_message:visible").size.should == 3 # two messages, plus add message form
  end

  it "should allow replying to conversation stream items" do
    course_with_student_logged_in(:active_all => true)
    c = User.create.initiate_conversation([@user.id, User.create.id])
    c.add_message('test')

    get "/"
    driver.find_element(:css, ".reply_message .textarea").click
    driver.find_element(:css, "textarea[name='body']").send_keys("<PASSWORD>")
    driver.find_element(:css, ".communication_sub_message .submit_button").click
    wait_for_ajax_requests
    messages = find_all_with_jquery(".communication_message.conversation .communication_sub_message:visible")

    # messages[-1] is the reply form
    messages[-2].text.should =~ /hey there/
  end

  it "should show appointment stream items on the dashboard" do
    Notification.create(:name => 'Appointment Group Published', :category => "Appointment Availability")
    Notification.create(:name => 'Appointment Group Updated', :category => "Appointment Availability")
    Notification.create(:name => 'Appointment Reserved For User', :category => "Appointment Signups")
    course_with_student_logged_in(:active_all => true)
    @me = @user
    student_in_course(:active_all => true, :course => @course)
    @other_student = @user
    @user = @me

    @group = @course.group_categories.create.groups.create(:context => @course)
    @group.users << @other_student << @user
    # appointment group publish notification and signup notification
    appointment_participant_model(:course => @course, :participant => @group, :updating_user => @other_student)
    # appointment group update notification
    @appointment_group.update_attributes(:new_appointments => [[Time.now.utc + 2.hour, Time.now.utc + 3.hour]])

    get "/"
    find_all_with_jquery(".topic_message div.communication_message.dashboard_notification").size.should == 3
    # appointment group publish and update notifications
    find_all_with_jquery("div.communication_message.message_appointment_group_#{@appointment_group.id}").size.should == 2
    # signup notification
    find_all_with_jquery("div.communication_message.message_calendar_event_#{@event.id}").size.should == 1
  end

  it "should display assignment in to do list" do
    course_with_student_logged_in

    due_date = Time.now.utc + 2.days
    @assignment = assignment_model({:due_at => due_date, :course => @course})
    get "/"
    driver.find_element(:css, '.events_list .event a').should include_text(@assignment.title)
    # use jQuery to get the text since selenium can't figure it out when the elements aren't displayed
    driver.execute_script("return $('.event a .tooltip_text').text()").should match(@course.short_name)
  end

  it "should limit the number of visible items in the to do list" do
    course_with_student_logged_in
    due_date = Time.now.utc + 2.days
    20.times do
      assignment_model :due_at => due_date, :course => @course, :submission_types => 'online_text_entry'
    end

    get "/"

    find_all_with_jquery(".to-do-list li:visible").size.should == 5 + 1 # +1 is the see more link
    driver.find_element(:css, ".more_link").click
    wait_for_animations
    find_all_with_jquery(".to-do-list li:visible").size.should == 20
  end

  it "should display assignment to grade in to do list and assignments menu for a teacher" do<commit_msg>spec: added selenium spec to dashboard file refs #7466<commit_after>require File.expand_path(File.dirname(__FILE__) + '/common')

describe "dashboard" do
  it_should_behave_like "in-process server selenium tests"

  def test_hiding(url)
    factory_with_protected_attributes(Announcement, :context => @course, :title => "hey all read this k", :message => "announcement")
    items = @user.stream_item_instances
    items.size.should == 1
    items.first.hidden.should == false

    get url
    find_all_with_jquery("div.communication_message.announcement").size.should == 1
    # force the element to be visible so we can click it -- webdriver has a
    # hover() event but it only works on Windows so far
    driver.execute_script("$('div.communication_message.announcement .disable_item_link').css('visibility', 'visible')")
    driver.find_element(:css, "div.communication_message.announcement .disable_item_link").click
    keep_trying_until { find_all_with_jquery("div.communication_message.announcement").size.should == 0 }

    # should still be gone on reload
    get url
    find_all_with_jquery("div.communication_message.announcement").size.should == 0

    @user.stream_items.size.should == 0
    items.first.reload.hidden.should == true
  end

  it "should allow hiding a stream item on the dashboard" do
    course_with_student_logged_in(:active_all => true)
    test_hiding("/")
  end

  it "should allow hiding a stream item on the course page" do
    course_with_student_logged_in(:active_all => true)
    test_hiding("/courses/#{@course.to_param}")
  end

  it "should show conversation stream items on the dashboard" do
    course_with_student_logged_in(:active_all => true)
    c = User.create.initiate_conversation([@user.id, User.create.id])
    c.add_message('test')
    c.add_participants([User.create.id])

    items = @user.stream_item_instances
    items.size.should == 1

    get "/"
    find_all_with_jquery("div.communication_message.conversation").size.should == 1
    find_all_with_jquery("div.communication_message.conversation .communication_sub_message:visible").size.should == 3 # two messages, plus add message form
  end

  it "should allow replying to conversation stream items" do
    course_with_student_logged_in(:active_all => true)
    c = User.create.initiate_conversation([@user.id, User.create.id])
    c.add_message('test')

    get "/"
    driver.find_element(:css, ".reply_message .textarea").click
    driver.find_element(:css, "textarea[name='body']").send_keys("hey there")
    driver.find_element(:css, ".communication_sub_message .submit_button").click
    wait_for_ajax_requests
    messages = find_all_with_jquery(".communication_message.conversation .communication_sub_message:visible")

    # messages[-1] is the reply form
    messages[-2].text.should =~ /hey there/
  end

  it "should show appointment stream items on the dashboard" do
    Notification.create(:name => 'Appointment Group Published', :category => "Appointment Availability")
    Notification.create(:name => 'Appointment Group Updated', :category => "Appointment Availability")
    Notification.create(:name => 'Appointment Reserved For User', :category => "Appointment Signups")
    course_with_student_logged_in(:active_all => true)
    @me = @user
    student_in_course(:active_all => true, :course => @course)
    @other_student = @user
    @user = @me

    @group = @course.group_categories.create.groups.create(:context => @course)
    @group.users << @other_student << @user
    # appointment group publish notification and signup notification
    appointment_participant_model(:course => @course, :participant => @group, :updating_user => @other_student)
    # appointment group update notification
    @appointment_group.update_attributes(:new_appointments => [[Time.now.utc + 2.hour, Time.now.utc + 3.hour]])

    get "/"
    find_all_with_jquery(".topic_message div.communication_message.dashboard_notification").size.should == 3
    # appointment group publish and update notifications
    find_all_with_jquery("div.communication_message.message_appointment_group_#{@appointment_group.id}").size.should == 2
    # signup notification
    find_all_with_jquery("div.communication_message.message_calendar_event_#{@event.id}").size.should == 1
  end

  it "should display assignment in to do list" do
    course_with_student_logged_in

    due_date = Time.now.utc + 2.days
    @assignment = assignment_model({:due_at => due_date, :course => @course})
    get "/"
    driver.find_element(:css, '.events_list .event a').should include_text(@assignment.title)
    # use jQuery to get the text since selenium can't figure it out when the elements aren't displayed
    driver.execute_script("return $('.event a .tooltip_text').text()").should match(@course.short_name)
  end

  it "should show submitted essay quizzes in the todo list" do
    pending("bug 7466 - Essay's in quizzes that have been taken are not showing up in the To Do list for a Teacher") do
      quiz_title = 'new quiz'
      course_with_teacher_logged_in
      student_in_course
      q = @course.quizzes.create!(:title => quiz_title)
      q.quiz_questions.create!(:question_data => {:name => "Quiz Essay Question 1", :question_type=>'essay_question', :question_text=>'qq1', :points_possible=>10})
      q.generate_quiz_data
      q.workflow_state = 'available'
      q.save
      q.reload
      qs = q.generate_submission(@user)
      qs.workflow_state = 'complete'
      qs.save
      qs.reload
      get "/"

      todo_list = f('.right-side-list .to-do-list')
      todo_list.should_not be_nil
      todo_list.should include_text(quiz_title)
    end
  end

  it "should limit the number of visible items in the to do list" do
    course_with_student_logged_in
    due_date = Time.now.utc + 2.days
    20.times do
      assignment_model :due_at => due_date, :course => @course, :submission_types => 'online_text_entry'
    end

    get "/"

    find_all_with_jquery(".to-do-list li:visible").size.should == 5 + 1 # +1 is the see more link
    driver.find_element(:css, ".more_link").click
    wait_for_animations
    find_all_with_jquery(".to-do-list li:visible").size.should == 20
  end

  it "should display assignment to grade in to do list and assignments menu for a teacher" do
{'fraction_non_alphanumeric': 0.11267123287671232, 'fraction_numerical': 0.005136986301369863, 'mean_word_length': 4.295104261106074, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 62, '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': '1984437', 'n_tokens_mistral': 3657, 'n_tokens_neox': 3493, 'n_words': 845}