<commit_before>#
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.

require_relative '../common'
require_relative '../helpers/announcements_common'

describe "announcements" do
  include_context "in-process server selenium tests"
  include AnnouncementsCommon

  context "announcements as a teacher" do
    before :once do
      @teacher = user_with_pseudonym(active_user: true)
      course_with_teacher(user: @teacher, active_course: true, active_enrollment: true)
    end

    before :each do
      user_session(@teacher)
    end

    describe "shared bulk topics specs" do
      let(:url) { "/courses/#{@course.id}/announcements/" }
      let(:what_to_create) { Announcement }

      before :once do
        @context = @course
        5.times do |i|
          title = "new #{i.to_s.rjust(3, '0')}"
          what_to_create == DiscussionTopic ? @course.discussion_topics.create!(:title => title, :user => @user) : announcement_model(:title => title, :user => @user)
        end
      end

      before :each do
        get url
        @checkboxes = ff('.toggleSelected')
      end

      it "should bulk delete topics", priority: "1", test_id: 220360 do
        5.times { |i| @checkboxes[i].click }
        f('#delete').click
        driver.switch_to.alert.accept
        wait_for_ajax_requests
        expect(f("#content")).not_to contain_css('.discussion-topic')
        expect(what_to_create.where(:workflow_state => 'active').count).to eq 0
      end

      it "should bulk lock topics", priority: "1", test_id: 220361 do
        5.times { |i| @checkboxes[i].click }
        scroll_page_to_top # if we only scroll until it's in view, its tooltip can interfere with clicks
        move_to_click('label[for=lock]')
        wait_for_ajax_requests
        #TODO: check the UI to make sure the topics have a locked symbol
        expect(what_to_create.where(locked: true).count).to eq 5
      end

      it "should search by title", priority: "1", test_id: 150525 do
        expected_text = 'hey there'
        update_attributes_and_validate(:title, expected_text)
      end

      it "should search by body", priority: "1", test_id: 220358 do
        body_text = 'new topic body'
        update_attributes_and_validate(:message, body_text, 'topic')
      end

      it "should search by author", priority: "1", test_id: 220359 do
        user_name = '<EMAIL>'
        title = 'new one'
        new_teacher = teacher_in_course(:course => @course, :active_all => true, :name => user_name)
        what_to_create == DiscussionTopic ? @course.discussion_topics.create!(:title => title, :user => new_teacher.user) : announcement_model(:title => title, :user => new_teacher.user)
        refresh_and_filter(:string, 'jake', user_name)
      end

      it "should search an entire phrase" do
        replace_content(f('#searchTerm'), 'new 001')
        expect(ff('.discussionTopicIndexList .discussion-topic').count).to eq 1
      end

      it "should return multiple items in the search", priority: "1", test_id: 220362 do
        new_title = 'updated'
        what_to_create.first.update_attributes(:title => "#{new_title} first")
        what_to_create.last.update_attributes(:title => "#{new_title} last")
        refresh_and_filter(:string, new_title, new_title, 2)
      end

      it "should filter by unread", priority: "1", test_id: 220363 do
        what_to_create.last.change_read_state('unread', @user)
        refresh_and_filter(:css, '#discussionsFilter', 'new 004')
      end
    end

    describe "shared main page topics specs" do
      let(:url) { "/courses/#{@course.id}/announcements" }
      let(:new_url) { "/courses/#{@course.id}/discussion_topics/new?is_announcement=true" }
      let(:what_to_create) { Announcement }

      before :once do
        @topic_title = 'new discussion'
        @context = @course
      end

      it "should have a lock that appears and disappears when the cog menu is used to lock/unlock the announcement for comments", priority: "1", test_id: 220365 do
        title = "My announcement"
        announcement_model(:title => title, :user => @user)
        get url

        expect(f("#content")).not_to contain_css('.discussion-info-icons .icon-lock')
        f('.discussion-actions .al-trigger').click
        wait_for_ajaximations
        f('.al-options li a.icon-lock').click
        wait_for_ajaximations
        expect(f('.discussion-info-icons .icon-lock')).not_to be_nil
        f('.discussion-actions .al-trigger').click
        wait_for_ajaximations
        f('.al-options li a.icon-lock').click
        wait_for_ajaximations
        expect(f("#content")).not_to contain_css('.discussion-info-icons .icon-lock')
      end

      it "should remove an announcement when it is deleted from the delete option in the cog menu", priority: "1", test_id: 220364 do
        title = "My announcement"
        announcement_model(:title => title, :user => @user)
        get url

        expect(f('.discussion-topic')).not_to be_nil
        f('.discussion-actions .al-trigger').click
        f('.al-options li a.icon-trash').click
        alert_present?
        alert = driver.switch_to.alert
        expect(alert.text).to match "Are you sure you want to delete this announcement?"
        alert.accept
        expect(f("#content")).not_to contain_css('.discussion-topic')
      end

      it "should start a new topic", priority: "1", test_id: 150528 do
        get url

        expect_new_page_load { f('.btn-primary').click }
        edit(@topic_title, 'new topic')
      end

      it "should add an attachment to a new topic", priority: "1", test_id: 150529 do
        topic_title = 'new topic with file'
        get new_url

        replace_content(f('input[name=title]'), topic_title)
        add_attachment_and_validate
        expect(what_to_create.where(title: topic_title).first.attachment_id).to be_present
      end

      it "should perform front-end validation for message", priority: "1", test_id: 220366 do
        topic_title = 'new topic with file'
        get new_url

        wait_for_tiny(f('#discussion-edit-view textarea[name=message]'))
        replace_content(f('input[name=title]'), topic_title)
        submit_form('.form-actions')
        wait_for_ajaximations

        expect(ff('.error_box').any?{|box| box.text.include?("A message is required")}).to be_truthy
      end

      it "should add an attachment to a graded topic", priority: "1", test_id: 220367 do
        what_to_create == DiscussionTopic ? @course.discussion_topics.create!(:title => 'graded attachment topic', :user => @user) : announcement_model(:title => 'graded attachment topic', :user => @user)
        if what_to_create == DiscussionTopic
          what_to_create.last.update_attributes(:assignment => @course.assignments.create!(:name => 'graded topic assignment'))
        end<commit_msg>fix another flaky announcement spec<commit_after>#
# Copyright (C) 2011 - present Instructure, Inc.
#
# This file is part of Canvas.
#
# Canvas is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, version 3 of the License.
#
# Canvas is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
# A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <http://www.gnu.org/licenses/>.

require_relative '../common'
require_relative '../helpers/announcements_common'

describe "announcements" do
  include_context "in-process server selenium tests"
  include AnnouncementsCommon

  context "announcements as a teacher" do
    before :once do
      @teacher = user_with_pseudonym(active_user: true)
      course_with_teacher(user: @teacher, active_course: true, active_enrollment: true)
    end

    before :each do
      user_session(@teacher)
    end

    describe "shared bulk topics specs" do
      let(:url) { "/courses/#{@course.id}/announcements/" }
      let(:what_to_create) { Announcement }

      before :once do
        @context = @course
        5.times do |i|
          title = "new #{i.to_s.rjust(3, '0')}"
          what_to_create == DiscussionTopic ? @course.discussion_topics.create!(:title => title, :user => @user) : announcement_model(:title => title, :user => @user)
        end
      end

      before :each do
        get url
        @checkboxes = ff('.toggleSelected')
      end

      it "should bulk delete topics", priority: "1", test_id: 220360 do
        5.times { |i| @checkboxes[i].click }
        f('#delete').click
        driver.switch_to.alert.accept
        wait_for_ajax_requests
        expect(f("#content")).not_to contain_css('.discussion-topic')
        expect(what_to_create.where(:workflow_state => 'active').count).to eq 0
      end

      it "should bulk lock topics", priority: "1", test_id: 220361 do
        5.times { |i| @checkboxes[i].click }
        scroll_page_to_top # if we only scroll until it's in view, its tooltip can interfere with clicks
        move_to_click('label[for=lock]')
        wait_for_ajax_requests
        #TODO: check the UI to make sure the topics have a locked symbol
        expect(what_to_create.where(locked: true).count).to eq 5
      end

      it "should search by title", priority: "1", test_id: 150525 do
        expected_text = 'hey there'
        update_attributes_and_validate(:title, expected_text)
      end

      it "should search by body", priority: "1", test_id: 220358 do
        body_text = 'new topic body'
        update_attributes_and_validate(:message, body_text, 'topic')
      end

      it "should search by author", priority: "1", test_id: 220359 do
        user_name = '<EMAIL>'
        title = 'new one'
        new_teacher = teacher_in_course(:course => @course, :active_all => true, :name => user_name)
        what_to_create == DiscussionTopic ? @course.discussion_topics.create!(:title => title, :user => new_teacher.user) : announcement_model(:title => title, :user => new_teacher.user)
        refresh_and_filter(:string, 'jake', user_name)
      end

      it "should search an entire phrase" do
        replace_content(f('#searchTerm'), 'new 001')
        expect(ff('.discussionTopicIndexList .discussion-topic').count).to eq 1
      end

      it "should return multiple items in the search", priority: "1", test_id: 220362 do
        new_title = 'updated'
        what_to_create.first.update_attributes(:title => "#{new_title} first")
        what_to_create.last.update_attributes(:title => "#{new_title} last")
        refresh_and_filter(:string, new_title, new_title, 2)
      end

      it "should filter by unread", priority: "1", test_id: 220363 do
        what_to_create.last.change_read_state('unread', @user)
        refresh_and_filter(:css, '#discussionsFilter', 'new 004')
      end
    end

    describe "shared main page topics specs" do
      let(:url) { "/courses/#{@course.id}/announcements" }
      let(:new_url) { "/courses/#{@course.id}/discussion_topics/new?is_announcement=true" }
      let(:what_to_create) { Announcement }

      before :once do
        @topic_title = 'new discussion'
        @context = @course
      end

      it "should have a lock that appears and disappears when the cog menu is used to lock/unlock the announcement for comments", priority: "1", test_id: 220365 do
        title = "My announcement"
        announcement_model(:title => title, :user => @user)
        get url

        expect(f("#content")).not_to contain_css('.discussion-info-icons .icon-lock')
        f('.discussion-actions .al-trigger').click
        wait_for_ajaximations
        f('.al-options li a.icon-lock').click
        wait_for_ajaximations
        expect(f('.discussion-info-icons .icon-lock')).not_to be_nil
        f('.discussion-actions .al-trigger').click
        wait_for_ajaximations
        f('.al-options li a.icon-lock').click
        wait_for_ajaximations
        expect(f("#content")).not_to contain_css('.discussion-info-icons .icon-lock')
      end

      it "should remove an announcement when it is deleted from the delete option in the cog menu", priority: "1", test_id: 220364 do
        title = "My announcement"
        announcement_model(:title => title, :user => @user)
        get url

        expect(f('.discussion-topic')).not_to be_nil
        f('.discussion-actions .al-trigger').click
        f('.al-options li a.icon-trash').click
        alert_present?
        alert = driver.switch_to.alert
        expect(alert.text).to match "Are you sure you want to delete this announcement?"
        alert.accept
        expect(f("#content")).not_to contain_css('.discussion-topic')
      end

      it "should start a new topic", priority: "1", test_id: 150528 do
        get url

        expect_new_page_load { f('.btn-primary').click }
        edit(@topic_title, 'new topic')
      end

      it "should add an attachment to a new topic", priority: "1", test_id: 150529 do
        topic_title = 'new topic with file'
        get new_url
        wait_for_tiny(f('#discussion-edit-view textarea[name=message]'))

        replace_content(f('input[name=title]'), topic_title)
        add_attachment_and_validate
        expect(what_to_create.where(title: topic_title).first.attachment_id).to be_present
      end

      it "should perform front-end validation for message", priority: "1", test_id: 220366 do
        topic_title = 'new topic with file'
        get new_url

        wait_for_tiny(f('#discussion-edit-view textarea[name=message]'))
        replace_content(f('input[name=title]'), topic_title)
        submit_form('.form-actions')
        wait_for_ajaximations

        expect(ff('.error_box').any?{|box| box.text.include?("A message is required")}).to be_truthy
      end

      it "should add an attachment to a graded topic", priority: "1", test_id: 220367 do
        what_to_create == DiscussionTopic ? @course.discussion_topics.create!(:title => 'graded attachment topic', :user => @user) : announcement_model(:title => 'graded attachment topic', :user => @user)
        if what_to_create == DiscussionTopic
          what_to_create.last.update_attributes(:assignment => @course.assignments.create!(:name => 'graded topic assignment'))
        end
{'fraction_non_alphanumeric': 0.11231618870184311, 'fraction_numerical': 0.014771441879033868, 'mean_word_length': 3.043583535108959, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 55, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '1886783', 'n_tokens_mistral': 4608, 'n_tokens_neox': 4342, 'n_words': 1281}