<commit_before>require_relative 'common'
require_relative 'helpers/files_common'
require_relative 'helpers/announcements_common'
require_relative 'helpers/color_common'

describe 'dashcards' do
  include_context 'in-process server selenium tests'
  include AnnouncementsCommon
  include ColorCommon
  include FilesCommon

  context 'as a student' do

    before do
      @course = course(active_all: true)
      course_with_student_logged_in(active_all: true)
    end

    it 'should show the toggle button for dashcard in new UI', priority: "1", test_id: 222506 do
      get '/'
      # verify features of new UI
      expect(f('#application.ic-app')).to be_present
      expect(f('.ic-app-header__main-navigation')).to be_present
      # verify the toggle switch is present
      expect(f('#dashboardToggleButton')).to be_present
    end

    it 'should toggle dashboard based on the toggle switch', priority: "1", test_id: 222507 do
      get '/'
      expect(f('#dashboardToggleButton')).to be_present
      f('#dashboardToggleButton').click
      if fj('#dashboardToggleButtonGridIcon').attribute(:class).include?('--active')
        expect(f('.ic-DashboardCard__link')).to be_displayed
      else
        expect(f('#dashboard-activity').text).to include('Recent Activity')
      end
    end

    it 'should redirect to announcements index', priority: "1", test_id: 222509 do
      # Icon will not display unless there is an announcement.
      create_announcement
      get '/'

      f('a.announcements').click
      expect(driver.current_url).to include("/courses/#{@course.id}/announcements")
    end

    it 'should redirect to assignments index', priority: "1", test_id: 238637 do
      # Icon will not display unless there is an assignment.
      @course.assignments.create!(title: 'assignment 1', name: 'assignment 1')
      get '/'

      f('a.assignments').click
      expect(driver.current_url).to include("/courses/#{@course.id}/assignments")
    end

    it 'should redirect to discussions index', priority: "1", test_id: 238638 do
      # Icon will not display unless there is a discussion.
      @course.discussion_topics.create!(title: 'discussion 1', message: 'This is a message.')
      get '/'

      f('a.discussions').click
      expect(driver.current_url).to include("/courses/#{@course.id}/discussion_topics")
    end

    it 'should redirect to files index', priority: "1", test_id: 238639 do
      # Icon will not display unless there is a file.
      add_file(fixture_file_upload('files/example.pdf', 'application/pdf'), @course, 'example.pdf')
      get '/'

      f('a.files').click
      expect(driver.current_url).to include("/courses/#{@course.id}/files")
    end

    it 'should display color picker', priority: "1", test_id: 249122 do
      get '/'
      f('.ic-DashboardCard__header-button').click
      expect(f('.ColorPicker__Container')).to be_displayed
    end

    it 'should display dashcard icons for course contents', priority: "1", test_id: 222508 do
      # create discussion, announcement, discussion and files as these 4 icons need to be displayed
      @course.discussion_topics.create!(title: 'discussion 1', message: 'This is a message.')
      @course.assignments.create!(title: 'assignment 1', name: 'assignment 1')
      create_announcement
      add_file(fixture_file_upload('files/example.pdf', 'application/pdf'), @course, 'example.pdf')
      get '/'

      expect(f('a.announcements')).to be_present
      expect(f('a.assignments')).to be_present
      expect(f('a.discussions')).to be_present
      expect(f('a.files')).to be_present
    end

    it 'should show announcement created notifications in dashcard', priority: "1", test_id: 238411 do
      create_announcement('New Announcement')
      get '/'
      expect(f('a.announcements .unread_count').text).to include('1')
      # The notifications should go away after visiting the show page of announcements
      expect_new_page_load{f('a.announcements').click}
      expect_new_page_load{fln('New Announcement').click}
      get '/'
      expect(f("#content")).not_to contain_css('a.announcements .unread_count')
    end

    it 'should show discussions created notifications in dashcard', priority: "1", test_id: 240009 do
      @course.discussion_topics.create!(title: 'discussion 1', message: 'This is a message.')
      get '/'
      expect(f('a.discussions .unread_count').text).to include('1')
      # The notifications should go away after visiting the show page of discussions
      expect_new_page_load{f('a.discussions').click}
      expect_new_page_load{fln('discussion 1').click}
      get '/'
      expect(f("#content")).not_to contain_css('a.discussions .unread_count')
    end

    it 'should show assignments created notifications in dashcard', priority: "1", test_id: 238413

    it 'should show files created notifications in dashcard', priority: "1", test_id: 238414

    context "course name and code display" do
      before :each do
        @course1 = course_model
        @course1.offer!
        @course1.save!
        enrollment = student_in_course(course: @course1, user: @student)
        enrollment.accept!
      end

      it 'should display special characters in a course title', priority: "1", test_id: 238192 do
        @course1.name = '(/*-+_@&$#%)"Course 1"æøåñó?äçíì[{c}]<strong>stuff</strong> )'
        @course1.save!
        get '/'
        expect(f('.ic-DashboardCard__header-title').text).to eq(@course1.name)
      end

      it 'should display special characters in course code', priority: "1", test_id: 240008 do
        # code is not displayed if the course name is too long
        @course1.name = 'test'
        @course1.course_code = '(/*-+_@&$#%)"Course 1"[{c}]<strong>stuff</strong> )'
        @course1.save!
        get '/'
        # as course codes are always displayed in upper case
        expect(f('.ic-DashboardCard__header-subtitle').text).to eq(@course1.course_code.upcase)
      end
    end

    context "dashcard custom color calendar" do
      before :each do
        # create another course to ensure the color matches to the right course
        @course1 = course_model
        @course1.name = 'Test Course'
        @course1.offer!
        @course1.save!
        enrollment = student_in_course(course: @course1, user: @student)
        enrollment.accept!
      end

      it 'should customize color by selecting from color palet in the calendar page', priority: "1", test_id: 239994 do
        select_color_pallet_from_calendar_page

        old_color = f('.ColorPicker__CustomInputContainer .ColorPicker__ColorPreview').attribute(:title)

        expect(f('.ColorPicker__Container')).to be_displayed
        f('.ColorPicker__Container .ColorPicker__ColorBlock:nth-of-type(7)').click
        wait_for_ajaximations
        new_color =  f('.ColorPicker__CustomInputContainer .ColorPicker__ColorPreview').attribute(:title)

        # make sure that we choose a new color for background
        if old_color == new_color
          f('.ColorPicker__Container .ColorPicker__ColorBlock:nth-of-type(8)').click<commit_msg>spec: add a spec for testrails C268713<commit_after>require_relative 'common'
require_relative 'helpers/files_common'
require_relative 'helpers/announcements_common'
require_relative 'helpers/color_common'

describe 'dashcards' do
  include_context 'in-process server selenium tests'
  include AnnouncementsCommon
  include ColorCommon
  include FilesCommon

  context 'as a student' do

    before do
      @course = course(active_all: true)
      course_with_student_logged_in(active_all: true)
    end

    it 'should show the toggle button for dashcard in new UI', priority: "1", test_id: 222506 do
      get '/'
      # verify features of new UI
      expect(f('#application.ic-app')).to be_present
      expect(f('.ic-app-header__main-navigation')).to be_present
      # verify the toggle switch is present
      expect(f('#dashboardToggleButton')).to be_present
    end

    it 'should toggle dashboard based on the toggle switch', priority: "1", test_id: 222507 do
      get '/'
      expect(f('#dashboardToggleButton')).to be_present
      f('#dashboardToggleButton').click
      if fj('#dashboardToggleButtonGridIcon').attribute(:class).include?('--active')
        expect(f('.ic-DashboardCard__link')).to be_displayed
      else
        expect(f('#dashboard-activity').text).to include('Recent Activity')
      end
    end

    it 'should redirect to announcements index', priority: "1", test_id: 222509 do
      # Icon will not display unless there is an announcement.
      create_announcement
      get '/'

      f('a.announcements').click
      expect(driver.current_url).to include("/courses/#{@course.id}/announcements")
    end

    it 'should redirect to assignments index', priority: "1", test_id: 238637 do
      # Icon will not display unless there is an assignment.
      @course.assignments.create!(title: 'assignment 1', name: 'assignment 1')
      get '/'

      f('a.assignments').click
      expect(driver.current_url).to include("/courses/#{@course.id}/assignments")
    end

    it 'should redirect to discussions index', priority: "1", test_id: 238638 do
      # Icon will not display unless there is a discussion.
      @course.discussion_topics.create!(title: 'discussion 1', message: 'This is a message.')
      get '/'

      f('a.discussions').click
      expect(driver.current_url).to include("/courses/#{@course.id}/discussion_topics")
    end

    it 'should redirect to files index', priority: "1", test_id: 238639 do
      # Icon will not display unless there is a file.
      add_file(fixture_file_upload('files/example.pdf', 'application/pdf'), @course, 'example.pdf')
      get '/'

      f('a.files').click
      expect(driver.current_url).to include("/courses/#{@course.id}/files")
    end

    it 'should display color picker', priority: "1", test_id: 249122 do
      get '/'
      f('.ic-DashboardCard__header-button').click
      expect(f('.ColorPicker__Container')).to be_displayed
    end

    it 'should display dashcard icons for course contents', priority: "1", test_id: 222508 do
      # create discussion, announcement, discussion and files as these 4 icons need to be displayed
      @course.discussion_topics.create!(title: 'discussion 1', message: 'This is a message.')
      @course.assignments.create!(title: 'assignment 1', name: 'assignment 1')
      create_announcement
      add_file(fixture_file_upload('files/example.pdf', 'application/pdf'), @course, 'example.pdf')
      get '/'

      expect(f('a.announcements')).to be_present
      expect(f('a.assignments')).to be_present
      expect(f('a.discussions')).to be_present
      expect(f('a.files')).to be_present
    end

    it 'should show announcement created notifications in dashcard', priority: "1", test_id: 238411 do
      create_announcement('New Announcement')
      get '/'
      expect(f('a.announcements .unread_count').text).to include('1')
      # The notifications should go away after visiting the show page of announcements
      expect_new_page_load{f('a.announcements').click}
      expect_new_page_load{fln('New Announcement').click}
      get '/'
      expect(f("#content")).not_to contain_css('a.announcements .unread_count')
    end

    it 'should show discussions created notifications in dashcard', priority: "1", test_id: 240009 do
      @course.discussion_topics.create!(title: 'discussion 1', message: 'This is a message.')
      get '/'
      expect(f('a.discussions .unread_count').text).to include('1')
      # The notifications should go away after visiting the show page of discussions
      expect_new_page_load{f('a.discussions').click}
      expect_new_page_load{fln('discussion 1').click}
      get '/'
      expect(f("#content")).not_to contain_css('a.discussions .unread_count')
    end

    it 'should show assignments created notifications in dashcard', priority: "1", test_id: 238413

    it 'should show files created notifications in dashcard', priority: "1", test_id: 238414

    context "course name and code display" do
      before :each do
        @course1 = course_model
        @course1.offer!
        @course1.save!
        enrollment = student_in_course(course: @course1, user: @student)
        enrollment.accept!
      end

      it 'should display special characters in a course title', priority: "1", test_id: 238192 do
        @course1.name = '(/*-+_@&$#%)"Course 1"æøåñó?äçíì[{c}]<strong>stuff</strong> )'
        @course1.save!
        get '/'
        expect(f('.ic-DashboardCard__header-title').text).to eq(@course1.name)
      end

      it 'should display special characters in course code', priority: "1", test_id: 240008 do
        # code is not displayed if the course name is too long
        @course1.name = 'test'
        @course1.course_code = '(/*-+_@&$#%)"Course 1"[{c}]<strong>stuff</strong> )'
        @course1.save!
        get '/'
        # as course codes are always displayed in upper case
        expect(f('.ic-DashboardCard__header-subtitle').text).to eq(@course1.course_code.upcase)
      end
    end

    context "dashcard custom color calendar" do
      before :each do
        # create another course to ensure the color matches to the right course
        @course1 = course_model
        @course1.name = '<NAME>'
        @course1.offer!
        @course1.save!
        enrollment = student_in_course(course: @course1, user: @student)
        enrollment.accept!
      end

      it 'should initially match color to the dashcard', priority: "1", test_id: 268713 do
        get '/calendar'
        calendar_color = f(".context-list-toggle-box.group_course_#{@course1.id}").style('background-color')
        get '/'
        hero = f("div[aria-label='#{@course1.name}'] .ic-DashboardCard__header_hero").style('background-color')
        expect(hero).to eq(calendar_color)
      end

      it 'should customize color by selecting from color palet in the calendar page', priority: "1", test_id: 239994 do
        select_color_pallet_from_calendar_page

        old_color = f('.ColorPicker__CustomInputContainer .ColorPicker__ColorPreview').attribute(:title)

        expect(f('.ColorPicker__Container')).to be_displayed
        f('.ColorPicker__Container .ColorPicker__ColorBlock:nth-of-type(7)').click
        wait_for_ajaximations
        new_color =  f('.ColorPicker__CustomInputContainer .ColorPicker__ColorPreview').attribute(:title)

        # make sure that we choose a new color for background
        if old_color == new_color
          f('.ColorPicker__Container .ColorPicker__ColorBlock:nth-of-type(8)').click
{'fraction_non_alphanumeric': 0.11749007800739017, 'fraction_numerical': 0.019638702613931847, 'mean_word_length': 3.33293803735547, 'pattern_counts': {'":': 0, '<': 12, '<?xml version=': 0, '>': 12, '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': '4988377', 'n_tokens_mistral': 4500, 'n_tokens_neox': 4265, 'n_words': 1154}