<commit_before>require 'test_helper'

class Admin::ConsultationsControllerTest < ActionController::TestCase

  setup do
    login_as :policy_writer
  end

  should_be_an_admin_controller

  should_allow_showing_of :consultation
  should_allow_creating_of :consultation
  should_allow_editing_of :consultation
  should_allow_revision_of :consultation

  should_show_document_audit_trail_for :consultation, :show
  should_show_document_audit_trail_for :consultation, :edit

  should_allow_related_policies_for :consultation
  should_allow_organisations_for :consultation
  should_allow_ministerial_roles_for :consultation
  should_allow_attachments_for :consultation
  should_show_inline_attachment_help_for :consultation
  should_allow_attached_images_for :consultation
  should_allow_attachment_references_for :consultation
  should_not_use_lead_image_for :consultation
  should_be_rejectable :consultation
  should_be_publishable :consultation
  should_be_force_publishable :consultation
  should_be_able_to_delete_an_edition :consultation
  should_link_to_public_version_when_published :consultation
  should_link_to_preview_version_when_not_published :consultation
  should_not_link_to_public_version_when_not_published :consultation
  should_prevent_modification_of_unmodifiable :consultation
  should_allow_alternative_format_provider_for :consultation

  test 'new displays consultation fields' do
    get :new

    assert_select "form#edition_new" do
      assert_select "textarea[name='edition[summary]']"
      assert_select "select[name*='edition[opening_on']", count: 3
      assert_select "select[name*='edition[closing_on']", count: 3
      assert_select "input[type='text'][name='edition[consultation_participation_attributes][link_url]']"
      assert_select "input[type='text'][name='edition[consultation_participation_attributes][email]']"
      assert_select "input[type='text'][name='edition[consultation_participation_attributes][consultation_response_form_attributes][title]']"
      assert_select "input[type='file'][name='edition[consultation_participation_attributes][consultation_response_form_attributes][file]']"
    end
  end

  test "create should create a new consultation" do
    attributes = attributes_for(:consultation,
      consultation_participation_attributes: {
        link_url: "http://participation.com",
        email: "<EMAIL>",
        consultation_response_form_attributes: {
          title: "the title of the response form",
          file: fixture_file_upload('two-pages.pdf')
        }
      }
    )

    post :create, edition: attributes

    consultation = Consultation.last
    assert_equal attributes[:summary], consultation.summary
    assert_equal attributes[:opening_on].to_date, consultation.opening_on
    assert_equal attributes[:closing_on].to_date, consultation.closing_on
    assert_equal "http://participation.com", consultation.consultation_participation.link_url
    assert_equal "<EMAIL>", consultation.consultation_participation.email
    assert_equal "the title of the response form", consultation.consultation_participation.consultation_response_form.title
    assert consultation.consultation_participation.consultation_response_form.file.present?
  end

  test "create should create a new consultation without consultation participation if participation fields are all blank" do
    attributes = attributes_for(:consultation,
      consultation_participation_attributes: {
        link_url: nil,
        email: nil,
        consultation_response_form_attributes: {
          title: nil,
          file: nil,
        }
      }
    )

    post :create, edition: attributes

    consultation = Consultation.last
    assert_nil consultation.consultation_participation
  end

  test "creating an consultation with invalid data but valid form file should still display the cached form file" do
    attributes = attributes_for(:consultation,<commit_msg>Fix typo in test name<commit_after>require 'test_helper'

class Admin::ConsultationsControllerTest < ActionController::TestCase

  setup do
    login_as :policy_writer
  end

  should_be_an_admin_controller

  should_allow_showing_of :consultation
  should_allow_creating_of :consultation
  should_allow_editing_of :consultation
  should_allow_revision_of :consultation

  should_show_document_audit_trail_for :consultation, :show
  should_show_document_audit_trail_for :consultation, :edit

  should_allow_related_policies_for :consultation
  should_allow_organisations_for :consultation
  should_allow_ministerial_roles_for :consultation
  should_allow_attachments_for :consultation
  should_show_inline_attachment_help_for :consultation
  should_allow_attached_images_for :consultation
  should_allow_attachment_references_for :consultation
  should_not_use_lead_image_for :consultation
  should_be_rejectable :consultation
  should_be_publishable :consultation
  should_be_force_publishable :consultation
  should_be_able_to_delete_an_edition :consultation
  should_link_to_public_version_when_published :consultation
  should_link_to_preview_version_when_not_published :consultation
  should_not_link_to_public_version_when_not_published :consultation
  should_prevent_modification_of_unmodifiable :consultation
  should_allow_alternative_format_provider_for :consultation

  test 'new displays consultation fields' do
    get :new

    assert_select "form#edition_new" do
      assert_select "textarea[name='edition[summary]']"
      assert_select "select[name*='edition[opening_on']", count: 3
      assert_select "select[name*='edition[closing_on']", count: 3
      assert_select "input[type='text'][name='edition[consultation_participation_attributes][link_url]']"
      assert_select "input[type='text'][name='edition[consultation_participation_attributes][email]']"
      assert_select "input[type='text'][name='edition[consultation_participation_attributes][consultation_response_form_attributes][title]']"
      assert_select "input[type='file'][name='edition[consultation_participation_attributes][consultation_response_form_attributes][file]']"
    end
  end

  test "create should create a new consultation" do
    attributes = attributes_for(:consultation,
      consultation_participation_attributes: {
        link_url: "http://participation.com",
        email: "<EMAIL>",
        consultation_response_form_attributes: {
          title: "the title of the response form",
          file: fixture_file_upload('two-pages.pdf')
        }
      }
    )

    post :create, edition: attributes

    consultation = Consultation.last
    assert_equal attributes[:summary], consultation.summary
    assert_equal attributes[:opening_on].to_date, consultation.opening_on
    assert_equal attributes[:closing_on].to_date, consultation.closing_on
    assert_equal "http://participation.com", consultation.consultation_participation.link_url
    assert_equal "<EMAIL>", consultation.consultation_participation.email
    assert_equal "the title of the response form", consultation.consultation_participation.consultation_response_form.title
    assert consultation.consultation_participation.consultation_response_form.file.present?
  end

  test "create should create a new consultation without consultation participation if participation fields are all blank" do
    attributes = attributes_for(:consultation,
      consultation_participation_attributes: {
        link_url: nil,
        email: nil,
        consultation_response_form_attributes: {
          title: nil,
          file: nil,
        }
      }
    )

    post :create, edition: attributes

    consultation = Consultation.last
    assert_nil consultation.consultation_participation
  end

  test "creating a consultation with invalid data but valid form file should still display the cached form file" do
    attributes = attributes_for(:consultation,
{'fraction_non_alphanumeric': 0.06706617365803902, 'fraction_numerical': 0.0005100089251561902, 'mean_word_length': 5.698548249359522, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 7, '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': '1688705', 'n_tokens_mistral': 2304, 'n_tokens_neox': 2263, 'n_words': 415}