<commit_before>require_relative '../common'
require_relative '../helpers/quizzes_common'

describe 'quizzes question with html answers' do
  include_context "in-process server selenium tests"
  include QuizzesCommon

  before(:each) do
    course_with_teacher_logged_in
    @last_quiz = start_quiz_question
    driver.manage.window.maximize
  end

  def edit_first_html_answer(question_type=nil)
    edit_first_question
    click_option('.question_form:visible .question_type', question_type) if question_type
    driver.execute_script "$('.answer').addClass('hover');"
    fj('.edit_html:visible').click
  end

  def close_first_html_answer
    move_to_click('.btn.edit_html_done')
  end

  def check_for_no_edit_button(option)
    click_option('.question_form:visible .question_type', option)
    driver.execute_script "$('.answer').addClass('hover');"
    expect(f("#content")).not_to contain_jqcss('.edit_html:visible')
  end

  it 'allows HTML answers for multiple choice', priority: "1", test_id: 209356 do
    quiz_with_new_questions
    click_questions_tab
    edit_first_html_answer
    type_in_tiny '.answer:eq(3) textarea', 'HTML'
    close_first_html_answer
    html = driver.execute_script "return $('.answer:eq(3) .answer_html').html()"
    expect(html).to eq '<p>HTML</p>'
    submit_form('.question_form')
    refresh_page
    click_questions_tab
    edit_first_question
    html = driver.execute_script "return $('.answer:eq(3) .answer_html').html()"
    wait_for_ajaximations
    expect(html).to eq '<p>HTML</p>'
  end

  it 'sets focus back to the edit button after editing', priority: "1", test_id: 209357 do
    quiz_with_new_questions
    click_questions_tab
    edit_first_html_answer
    close_first_html_answer
    wait_for_ajaximations
    check_element_has_focus(fj('.edit_html:visible'))
  end

  it 'doesn\'t show the edit html button for question types besides multiple choice and multiple answers', priority: "1", test_id: 209358 do
    quiz_with_new_questions
    click_questions_tab
    edit_first_question

    check_for_no_edit_button 'True/False'
    check_for_no_edit_button 'Fill In the Blank'
    check_for_no_edit_button 'Fill In Multiple Blanks'
    check_for_no_edit_button 'Multiple Dropdowns'
    check_for_no_edit_button 'Matching'
    check_for_no_edit_button 'Numerical Answer'
  end

  it 'restores normal input when html answer is empty', priority: "1", test_id: 209359 do
    skip('CNVS-29570 - fragile because RCE is not being reactive')
    quiz_with_new_questions
    click_questions_tab
    edit_first_html_answer
    type_in_tiny '.answer:eq(3) textarea', 'HTML'

    # clear tiny
    driver.execute_script "tinyMCE.activeEditor.setContent('')"
    close_first_html_answer
    input_length = driver.execute_script "return $('.answer:eq(3) input[name=answer_text]:visible').length"
    expect(input_length).to eq 1
  end

  it 'populates the editor and input elements properly', priority: "1", test_id: 209360 do
    skip('CNVS-29570 - fragile because RCE is not being reactive')
    quiz_with_new_questions
    click_questions_tab

    # add text to regular input
    edit_first_question
    input = fj('input[name=answer_text]:visible')
    input.click
    input.send_keys 'ohai'
    submit_form('.question_form')
    wait_for_ajax_requests

    # open it up in the editor, make sure the text matches the input
    edit_first_html_answer
    content = driver.execute_script "return $('.answer:eq(3) textarea')._justGetCode()"
    expect(content).to eq '<p>ohai</p>'

    # clear it out, make sure the original input is empty also
    driver.execute_script "tinyMCE.activeEditor.setContent('')"
    close_first_html_answer
    value = driver.execute_script "return $('input[name=answer_text]:visible')[0].value"
    expect(value).to eq ''<commit_msg>Spec: unskip specs previously failing on new UI<commit_after>require_relative '../common'
require_relative '../helpers/quizzes_common'

describe 'quizzes question with html answers' do
  include_context "in-process server selenium tests"
  include QuizzesCommon

  before(:each) do
    course_with_teacher_logged_in
    @last_quiz = start_quiz_question
    driver.manage.window.maximize
  end

  def edit_first_html_answer(question_type=nil)
    edit_first_question
    click_option('.question_form:visible .question_type', question_type) if question_type
    driver.execute_script "$('.answer').addClass('hover');"
    fj('.edit_html:visible').click
  end

  def close_first_html_answer
    move_to_click('.btn.edit_html_done')
  end

  def check_for_no_edit_button(option)
    click_option('.question_form:visible .question_type', option)
    driver.execute_script "$('.answer').addClass('hover');"
    expect(f("#content")).not_to contain_jqcss('.edit_html:visible')
  end

  it 'allows HTML answers for multiple choice', priority: "1", test_id: 209356 do
    quiz_with_new_questions
    click_questions_tab
    edit_first_html_answer
    type_in_tiny '.answer:eq(3) textarea', 'HTML'
    close_first_html_answer
    html = driver.execute_script "return $('.answer:eq(3) .answer_html').html()"
    expect(html).to eq '<p>HTML</p>'
    submit_form('.question_form')
    refresh_page
    click_questions_tab
    edit_first_question
    html = driver.execute_script "return $('.answer:eq(3) .answer_html').html()"
    wait_for_ajaximations
    expect(html).to eq '<p>HTML</p>'
  end

  it 'sets focus back to the edit button after editing', priority: "1", test_id: 209357 do
    quiz_with_new_questions
    click_questions_tab
    edit_first_html_answer
    close_first_html_answer
    wait_for_ajaximations
    check_element_has_focus(fj('.edit_html:visible'))
  end

  it 'doesn\'t show the edit html button for question types besides multiple choice and multiple answers', priority: "1", test_id: 209358 do
    quiz_with_new_questions
    click_questions_tab
    edit_first_question

    check_for_no_edit_button 'True/False'
    check_for_no_edit_button 'Fill In the Blank'
    check_for_no_edit_button 'Fill In Multiple Blanks'
    check_for_no_edit_button 'Multiple Dropdowns'
    check_for_no_edit_button 'Matching'
    check_for_no_edit_button 'Numerical Answer'
  end

  it 'restores normal input when html answer is empty', priority: "1", test_id: 209359 do
    quiz_with_new_questions
    click_questions_tab
    edit_first_html_answer
    type_in_tiny '.answer:eq(3) textarea', 'HTML'

    # clear tiny
    driver.execute_script "tinyMCE.activeEditor.setContent('')"
    close_first_html_answer
    input_length = driver.execute_script "return $('.answer:eq(3) input[name=answer_text]:visible').length"
    expect(input_length).to eq 1
  end

  it 'populates the editor and input elements properly', priority: "1", test_id: 209360 do
    quiz_with_new_questions
    click_questions_tab

    # add text to regular input
    edit_first_question
    input = fj('input[name=answer_text]:visible')
    input.click
    input.send_keys 'ohai'
    submit_form('.question_form')
    wait_for_ajax_requests

    # open it up in the editor, make sure the text matches the input
    edit_first_html_answer
    content = driver.execute_script "return $('.answer:eq(3) textarea')._justGetCode()"
    expect(content).to eq '<p>ohai</p>'

    # clear it out, make sure the original input is empty also
    driver.execute_script "tinyMCE.activeEditor.setContent('')"
    close_first_html_answer
    value = driver.execute_script "return $('input[name=answer_text]:visible')[0].value"
    expect(value).to eq ''
{'fraction_non_alphanumeric': 0.09627163781624501, 'fraction_numerical': 0.012782956058588549, 'mean_word_length': 4.506598240469208, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '6645230', 'n_tokens_mistral': 2499, 'n_tokens_neox': 2353, 'n_words': 553}