<commit_before>require_relative "common"
require_relative "helpers/quizzes_common"

describe "quizzes section hierarchy" do
  include_context "in-process server selenium tests"
  include QuizzesCommon

  before :each do
    course_with_teacher_logged_in
    @new_section = @course.course_sections.create!(name: 'New Section')
    @student = student_in_section(@new_section)
    @course.start_at = Time.zone.now.advance(days: -30)
    @course.conclude_at = Time.zone.now.advance(days: -10)
    @course.restrict_enrollments_to_course_dates = true
    @course.save!
    @new_section.start_at = Time.zone.now.advance(days: -30)
    @new_section.end_at = Time.zone.now.advance(days: 10)
    @new_section.restrict_enrollments_to_section_dates = true
    @new_section.save!

    # create a quiz and assign it to the section with due dae after course end date
    @quiz = quiz_with_multiple_type_questions(false)
    @quiz.reload
    @override = @quiz.assignment_overrides.build
    @override.set = @new_section
    @override.due_at = Time.zone.now.advance(days:3)
    @override.due_at_overridden = true
    @override.save!
  end

  def take_hierarchy_quiz
    get "/courses/#{@course.id}/quizzes/#{@quiz.id}"
    expect_new_page_load{f('#take_quiz_link').click}
    # make sure it does not create a blank submissions
    expect(f(' .quiz_score')).not_to be_present
    expect(f(' .quiz_duration')).not_to be_present
    # take and submit the quiz
    answer_questions_and_submit(@quiz, 3)
  end

  def verify_quiz_accessible
    get "/courses/#{@course.id}/quizzes/#{@quiz.id}"
    expect_new_page_load{f('#take_quiz_link').click}
    # make sure it does not create a blank submissions
    expect(f(' .quiz_score')).not_to be_present
    expect(f(' .quiz_duration')).not_to be_present
    expect_new_page_load(true) { f('#section-tabs .quizzes').click }
  end

  context "section overrides course and term hierarchy" do
    context "course end date in past" do
      it "should allow the student to take the quiz", priority: "1", test_id: 282619 do
        # ensure student is able to take the quiz and it does not create a blank submission
        user_session(@student)
        take_hierarchy_quiz
      end

      it "should allow the teacher to preview the quiz", priority: "1", test_id: 282838 do
        get "/courses/#{@course.id}/quizzes"
        keep_trying_until { fln('Test Quiz').click }
        expect_new_page_load{f('#preview_quiz_button').click}
        keep_trying_until do
         expect(f(' .quiz-header').text).to include('This is a preview of the published version of the quiz')
        end
      end

    it "should work with lock and unlock dates set up", priority: "1", test_id: 323086 do
      @override.unlock_at = Time.zone.now.advance(days:-1)
      @override.lock_at = Time.zone.now.advance(days:4)
      user_session(@student)
      take_hierarchy_quiz
    end
    end

    context "term end date in past" do
      before :each do
        term = EnrollmentTerm.find(@course.enrollment_term_id)
        term.start_at = Time.zone.now.advance(days: -60)
        term.end_at = Time.zone.now.advance(days: -15)
        term.save!
      end

      it "should still be accessible for student in the section after term end date", priority: "1", test_id: 323087 do
        user_session(@student)
        take_hierarchy_quiz
      end

      it "should be accessible for teachers enrolled in section after term end date", priority: "1", test_id: 323089 do
        teacher_in_section(@new_section, user: @teacher)
        take_hierarchy_quiz
      end

      it "should work with lock and unlock dates set up", priority: "1", test_id: 323090 do
        @override.unlock_at = Time.zone.now.advance(days:-1)
        @override.lock_at = Time.zone.now.advance(days:4)
        user_session(@student)
        take_hierarchy_quiz
      end
    end
  end

  context "course overrides hierarchy when restrict to section dates is not checked" do
    before :each do
      @new_section.restrict_enrollments_to_section_dates = false
      @new_section.save!
    end

    context "course ends in past" do
      it "should disallow student to view quiz", priority: "1", test_id: 323323 do
        user_session(@student)
        get "/courses/#{@course.id}/quizzes/#{@quiz.id}"
        expect(f('#quiz_show .quiz-header .lock_explanation').text).
                                        to include('This quiz is no longer available as the course has been concluded')
      end

      it "should still allow teachers to take the quiz", priority: "1", test_id: 323324 do
        verify_quiz_accessible
      end
    end

    context "course ends in future" do
      before :each do
        @course.conclude_at = Time.zone.now.advance(days: 10)
        @course.save!
      end

      it "should allow student in section to take quiz", priority: "1", test_id: 323321 do
        user_session(@student)
        verify_quiz_accessible
      end

      it "should allow teachers to take the quiz", priority: "1", test_id: 323322 do
        verify_quiz_accessible
      end
    end
  end

  context "term overrides hierarchy when restrict to course and section dates are not checked" do
    context "course ends in past" do
      before :each do
        @course.restrict_enrollments_to_course_dates = false
        @course.save!
        @new_section.restrict_enrollments_to_section_dates = false
        @new_section.save!
      end

      it "should allow student to take quiz", priority: "1", test_id: 323326 do
        user_session(@student)
        verify_quiz_accessible
      end

      it "should still allow teachers to take the quiz", priority: "1", test_id: 323328 do
        verify_quiz_accessible
      end
    end
  end
end<commit_msg>spec: Add specs for quizzes hierarchy<commit_after>require_relative "common"
require_relative "helpers/quizzes_common"

describe "quizzes section hierarchy" do
  include_context "in-process server selenium tests"
  include QuizzesCommon

  before :each do
    course_with_teacher_logged_in
    @new_section = @course.course_sections.create!(name: 'New Section')
    @student = student_in_section(@new_section)
    @course.start_at = Time.zone.now.advance(days: -30)
    @course.conclude_at = Time.zone.now.advance(days: -10)
    @course.restrict_enrollments_to_course_dates = true
    @course.save!
    @new_section.start_at = Time.zone.now.advance(days: -30)
    @new_section.end_at = Time.zone.now.advance(days: 10)
    @new_section.restrict_enrollments_to_section_dates = true
    @new_section.save!

    # create a quiz and assign it to the section with due dae after course end date
    @quiz = quiz_with_multiple_type_questions(false)
    @quiz.reload
    @override = @quiz.assignment_overrides.build
    @override.set = @new_section
    @override.due_at = Time.zone.now.advance(days:3)
    @override.due_at_overridden = true
    @override.save!
  end

  def take_hierarchy_quiz
    get "/courses/#{@course.id}/quizzes/#{@quiz.id}"
    expect_new_page_load{f('#take_quiz_link').click}
    # make sure it does not create a blank submissions
    expect(f(' .quiz_score')).not_to be_present
    expect(f(' .quiz_duration')).not_to be_present
    # take and submit the quiz
    answer_questions_and_submit(@quiz, 3)
  end

  def verify_quiz_accessible
    get "/courses/#{@course.id}/quizzes/#{@quiz.id}"
    expect_new_page_load{f('#take_quiz_link').click}
    # make sure it does not create a blank submissions
    expect(f(' .quiz_score')).not_to be_present
    expect(f(' .quiz_duration')).not_to be_present
    expect_new_page_load(true) { f('#section-tabs .quizzes').click }
  end

  context "section overrides course and term hierarchy" do
    context "course end date in past" do
      it "should allow the student to take the quiz", priority: "1", test_id: 282619 do
        # ensure student is able to take the quiz and it does not create a blank submission
        user_session(@student)
        take_hierarchy_quiz
      end

      it "should allow the teacher to preview the quiz", priority: "1", test_id: 282838 do
        get "/courses/#{@course.id}/quizzes"
        keep_trying_until { fln('Test Quiz').click }
        expect_new_page_load{f('#preview_quiz_button').click}
        keep_trying_until do
         expect(f(' .quiz-header').text).to include('This is a preview of the published version of the quiz')
        end
      end

    it "should work with lock and unlock dates set up", priority: "1", test_id: 323086 do
      @override.unlock_at = Time.zone.now.advance(days:-1)
      @override.lock_at = Time.zone.now.advance(days:4)
      user_session(@student)
      take_hierarchy_quiz
    end
    end

    context "term end date in past" do
      before :each do
        term = EnrollmentTerm.find(@course.enrollment_term_id)
        term.start_at = Time.zone.now.advance(days: -60)
        term.end_at = Time.zone.now.advance(days: -15)
        term.save!
      end

      it "should still be accessible for student in the section after term end date", priority: "1", test_id: 323087 do
        user_session(@student)
        take_hierarchy_quiz
      end

      it "should be accessible for teachers enrolled in section after term end date", priority: "1", test_id: 323089 do
        teacher_in_section(@new_section, user: @teacher)
        take_hierarchy_quiz
      end

      it "should work with lock and unlock dates set up", priority: "1", test_id: 323090 do
        @override.unlock_at = Time.zone.now.advance(days:-1)
        @override.lock_at = Time.zone.now.advance(days:4)
        user_session(@student)
        take_hierarchy_quiz
      end

      it "should not be accessible for student in the main section", priority: "1", test_id: 350077 do
        student1 = user_with_pseudonym(username: '<EMAIL>', active_all: 1)
        student_in_course(course: @course, user: student1)
        user_session(student1)
        get "/courses/#{@course.id}/quizzes/#{@quiz.id}"
        expect(f('#quiz_show .quiz-header .lock_explanation').text).
            to include('This quiz is no longer available as the course has been concluded')
      end
    end
  end

  context "course overrides hierarchy when restrict to section dates is not checked" do
    before :each do
      @new_section.restrict_enrollments_to_section_dates = false
      @new_section.save!
    end

    context "course ends in past" do
      it "should disallow student to view quiz", priority: "1", test_id: 323323 do
        user_session(@student)
        get "/courses/#{@course.id}/quizzes/#{@quiz.id}"
        expect(f('#quiz_show .quiz-header .lock_explanation').text).
                                        to include('This quiz is no longer available as the course has been concluded')
      end

      it "should still allow teachers to take the quiz", priority: "1", test_id: 323324 do
        verify_quiz_accessible
      end
    end

    context "course ends in future" do
      before :each do
        @course.conclude_at = Time.zone.now.advance(days: 10)
        @course.save!
      end

      it "should allow student in section to take quiz", priority: "1", test_id: 323321 do
        user_session(@student)
        verify_quiz_accessible
      end

      it "should allow teachers to take the quiz", priority: "1", test_id: 323322 do
        verify_quiz_accessible
      end
    end
  end

  context "term overrides hierarchy when restrict to course and section dates are not checked" do
    context "course ends in past" do
      before :each do
        @course.restrict_enrollments_to_course_dates = false
        @course.save!
        @new_section.restrict_enrollments_to_section_dates = false
        @new_section.save!
      end

      it "should allow student to take quiz", priority: "1", test_id: 323326 do
        user_session(@student)
        verify_quiz_accessible
      end

      it "should still allow teachers to take the quiz", priority: "1", test_id: 323328 do
        verify_quiz_accessible
      end

      it "should allow students in the main section to take the quiz", priority: "1", test_id: 350075 do
        student1 = user_with_pseudonym(username: '<EMAIL>', active_all: 1)
        enrollment = student_in_course(course: @course, user: student1)
        enrollment.accept!
        user_session(student1)
        verify_quiz_accessible
      end
    end
  end
end
{'fraction_non_alphanumeric': 0.09135822466996031, 'fraction_numerical': 0.01862800680327205, 'mean_word_length': 2.9870842751049405, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3520259', 'n_tokens_mistral': 4052, 'n_tokens_neox': 3827, 'n_words': 1100}