<commit_before>#encoding: UTF-8
require 'test_helper'

class Admin::EditionsControllerTest < ActionController::TestCase
  include Admin::EditionRoutesHelper

  setup do
    login_as :writer
  end

  should_be_an_admin_controller

  test 'should pass filter parameters to an edition filter' do
    stub_filter = stub_edition_filter
    Admin::EditionFilter.expects(:new).with(anything, anything, has_entries(state: "draft", type: "publication")).returns(stub_filter)

    get :index, state: :draft, type: :publication
  end

  test "should not pass blank parameters to the edition filter" do
    stub_filter = stub_edition_filter
    Admin::EditionFilter.expects(:new).with(anything, anything, Not(has_key(:author))).returns(stub_filter)

    get :index, state: :draft, author: ""
  end

  test 'should add state param set to "active" if none is supplied' do
    stub_filter = stub_edition_filter
    Admin::EditionFilter.expects(:new).with(anything, anything, has_entry(state: "active")).returns(stub_filter)

    get :index, type: :publication
  end

  view_test 'should distinguish between edition types when viewing the list of editions' do
    guide = create(:draft_detailed_guide)
    publication = create(:draft_publication)
    stub_filter = stub_edition_filter(editions: [guide, publication])
    stub_filter.stubs(:show_stats)
    Admin::EditionFilter.stubs(:new).returns(stub_filter)

    get :index, state: :draft

    assert_select_object(guide) { assert_select ".type", text: "Detailed guide" }
    assert_select_object(publication) { assert_select ".type", text: "Publication: Policy paper" }
  end

  view_test '#index should respond to xhr requests with only the filter results html' do
    xhr :get, :index, state: :active
    response_html = Nokogiri::HTML::DocumentFragment.parse(response.body)

    assert_equal "h1", response_html.children[0].node_name()
    assert_match "Everyone’s documents", response_html.children[0].text()
  end

  view_test '#index should show unpublishing information' do
    edition = create(:unpublished_edition)
    xhr :get, :index, state: :active

    assert_select 'td.title', text: /edition.title/
    assert_select 'td.title', text: /unpublished less than a minute ago/
  end

  test "diffing against a previous version" do
    publication = create(:draft_publication)
    editor = create(:departmental_editor)
    Edition::AuditTrail.whodunnit = editor
    publication.first_published_at = Time.zone.now
    publication.major_change_published_at = Time.zone.now
    force_publish(publication)
    draft_publication = Timecop.freeze 1.hour.from_now do
      publication.reload.create_draft(editor)
    end

    get :diff, id: draft_publication, audit_trail_entry_id: draft_publication.document_version_trail.first.version.item_id

    assert_response :success
    assert_template :diff
    assert_equal draft_publication, assigns(:edition)
    assert_equal publication, assigns(:audit_trail_entry)
  end

  test "revising the published edition should create a new draft edition" do
    published_edition = create(:published_publication)
    Edition.stubs(:find).returns(published_edition)
    draft_edition = create(:draft_publication)
    published_edition.expects(:create_draft).with(current_user).returns(draft_edition)

    post :revise, id: published_edition
  end

  test "revising a published edition redirects to edit for the new draft" do
    published_edition = create(:published_publication)

    post :revise, id: published_edition

    draft_edition = Edition.last
    assert_redirected_to edit_admin_publication_path(draft_edition.reload)
  end

  test "failing to revise an edition should redirect to the existing draft" do
    published_edition = create(:published_publication)
    existing_draft = create(:draft_publication, document: published_edition.document)

    post :revise, id: published_edition

    assert_redirected_to edit_admin_publication_path(existing_draft)
    assert_equal "There is already an active draft edition for this document", flash[:alert]
  end

  test "failing to revise an edition should redirect to the existing submitted edition" do
    published_edition = create(:published_publication)
    existing_submitted = create(:submitted_publication, document: published_edition.document)

    post :revise, id: published_edition

    assert_redirected_to edit_admin_publication_path(existing_submitted)
    assert_equal "There is already an active submitted edition for this document", flash[:alert]
  end

  test "failing to revise an edition should redirect to the existing rejected edition" do
    published_edition = create(:published_publication)
    existing_rejected = create(:rejected_publication, document: published_edition.document)

    post :revise, id: published_edition

    assert_redirected_to edit_admin_publication_path(existing_rejected)
    assert_equal "There is already an active rejected edition for this document", flash[:alert]
  end

  test "should remember standard filter options" do
    get :index, state: :draft, type: 'consultation'
    assert_equal 'consultation', session[:document_filters]['type']
  end

  test "should remember author filter options" do
    get :index, state: :draft, author: current_user
    assert_equal current_user.to_param, session[:document_filters]['author']
  end

  test "should remember organisation filter options" do
    organisation = create(:organisation)
    get :index, state: :draft, organisation: organisation
    assert_equal organisation.to_param, session[:document_filters]['organisation']
  end

  test "should remember state filter options" do
    get :index, state: :draft
    assert_equal 'draft', session[:document_filters]['state']
  end

  test "should remember title filter options" do
    get :index, title: "test"
    assert_equal "test", session[:document_filters]['title']
  end

  test "index should redirect to remembered filtered options if available" do
    organisation = create(:organisation)
    get :index, state: :draft, organisation: organisation, state: :submitted

    get :index
    assert_redirected_to admin_editions_path(state: :submitted, organisation: organisation)
  end

  test "index should redirect to remembered filtered options if selected filter is invalid" do
    organisation = create(:organisation)
    session[:document_filters] = { 'state' => 'submitted', 'author' => current_user.to_param, 'organisation' => organisation.to_param }
    get :index, author: 'invalid'
    assert_redirected_to admin_editions_path(state: :submitted, author: current_user, organisation: organisation)
  end

  test "index should redirect to department if logged in with no remembered filters" do
    organisation = create(:organisation)
    editor = login_as create(:departmental_editor, organisation: organisation)
    get :index
    assert_redirected_to admin_editions_path(organisation: organisation.id, state: :active)
  end

  view_test "should not show published editions as force published" do
    publication = create(:published_publication)
    get :index, state: :published, type: :publication

    assert_select_object(publication)
    refute_select "tr.force_published"
  end

  view_test "should show force published editions as force published" do
    publication = create(:published_publication, force_published: true)
    get :index, state: :published, type: :publication
<commit_msg>Remove duplicate hash key in test<commit_after>#encoding: UTF-8
require 'test_helper'

class Admin::EditionsControllerTest < ActionController::TestCase
  include Admin::EditionRoutesHelper

  setup do
    login_as :writer
  end

  should_be_an_admin_controller

  test 'should pass filter parameters to an edition filter' do
    stub_filter = stub_edition_filter
    Admin::EditionFilter.expects(:new).with(anything, anything, has_entries(state: "draft", type: "publication")).returns(stub_filter)

    get :index, state: :draft, type: :publication
  end

  test "should not pass blank parameters to the edition filter" do
    stub_filter = stub_edition_filter
    Admin::EditionFilter.expects(:new).with(anything, anything, Not(has_key(:author))).returns(stub_filter)

    get :index, state: :draft, author: ""
  end

  test 'should add state param set to "active" if none is supplied' do
    stub_filter = stub_edition_filter
    Admin::EditionFilter.expects(:new).with(anything, anything, has_entry(state: "active")).returns(stub_filter)

    get :index, type: :publication
  end

  view_test 'should distinguish between edition types when viewing the list of editions' do
    guide = create(:draft_detailed_guide)
    publication = create(:draft_publication)
    stub_filter = stub_edition_filter(editions: [guide, publication])
    stub_filter.stubs(:show_stats)
    Admin::EditionFilter.stubs(:new).returns(stub_filter)

    get :index, state: :draft

    assert_select_object(guide) { assert_select ".type", text: "Detailed guide" }
    assert_select_object(publication) { assert_select ".type", text: "Publication: Policy paper" }
  end

  view_test '#index should respond to xhr requests with only the filter results html' do
    xhr :get, :index, state: :active
    response_html = Nokogiri::HTML::DocumentFragment.parse(response.body)

    assert_equal "h1", response_html.children[0].node_name()
    assert_match "Everyone’s documents", response_html.children[0].text()
  end

  view_test '#index should show unpublishing information' do
    edition = create(:unpublished_edition)
    xhr :get, :index, state: :active

    assert_select 'td.title', text: /edition.title/
    assert_select 'td.title', text: /unpublished less than a minute ago/
  end

  test "diffing against a previous version" do
    publication = create(:draft_publication)
    editor = create(:departmental_editor)
    Edition::AuditTrail.whodunnit = editor
    publication.first_published_at = Time.zone.now
    publication.major_change_published_at = Time.zone.now
    force_publish(publication)
    draft_publication = Timecop.freeze 1.hour.from_now do
      publication.reload.create_draft(editor)
    end

    get :diff, id: draft_publication, audit_trail_entry_id: draft_publication.document_version_trail.first.version.item_id

    assert_response :success
    assert_template :diff
    assert_equal draft_publication, assigns(:edition)
    assert_equal publication, assigns(:audit_trail_entry)
  end

  test "revising the published edition should create a new draft edition" do
    published_edition = create(:published_publication)
    Edition.stubs(:find).returns(published_edition)
    draft_edition = create(:draft_publication)
    published_edition.expects(:create_draft).with(current_user).returns(draft_edition)

    post :revise, id: published_edition
  end

  test "revising a published edition redirects to edit for the new draft" do
    published_edition = create(:published_publication)

    post :revise, id: published_edition

    draft_edition = Edition.last
    assert_redirected_to edit_admin_publication_path(draft_edition.reload)
  end

  test "failing to revise an edition should redirect to the existing draft" do
    published_edition = create(:published_publication)
    existing_draft = create(:draft_publication, document: published_edition.document)

    post :revise, id: published_edition

    assert_redirected_to edit_admin_publication_path(existing_draft)
    assert_equal "There is already an active draft edition for this document", flash[:alert]
  end

  test "failing to revise an edition should redirect to the existing submitted edition" do
    published_edition = create(:published_publication)
    existing_submitted = create(:submitted_publication, document: published_edition.document)

    post :revise, id: published_edition

    assert_redirected_to edit_admin_publication_path(existing_submitted)
    assert_equal "There is already an active submitted edition for this document", flash[:alert]
  end

  test "failing to revise an edition should redirect to the existing rejected edition" do
    published_edition = create(:published_publication)
    existing_rejected = create(:rejected_publication, document: published_edition.document)

    post :revise, id: published_edition

    assert_redirected_to edit_admin_publication_path(existing_rejected)
    assert_equal "There is already an active rejected edition for this document", flash[:alert]
  end

  test "should remember standard filter options" do
    get :index, state: :draft, type: 'consultation'
    assert_equal 'consultation', session[:document_filters]['type']
  end

  test "should remember author filter options" do
    get :index, state: :draft, author: current_user
    assert_equal current_user.to_param, session[:document_filters]['author']
  end

  test "should remember organisation filter options" do
    organisation = create(:organisation)
    get :index, state: :draft, organisation: organisation
    assert_equal organisation.to_param, session[:document_filters]['organisation']
  end

  test "should remember state filter options" do
    get :index, state: :draft
    assert_equal 'draft', session[:document_filters]['state']
  end

  test "should remember title filter options" do
    get :index, title: "test"
    assert_equal "test", session[:document_filters]['title']
  end

  test "index should redirect to remembered filtered options if available" do
    organisation = create(:organisation)
    get :index, organisation: organisation, state: :submitted

    get :index
    assert_redirected_to admin_editions_path(state: :submitted, organisation: organisation)
  end

  test "index should redirect to remembered filtered options if selected filter is invalid" do
    organisation = create(:organisation)
    session[:document_filters] = { 'state' => 'submitted', 'author' => current_user.to_param, 'organisation' => organisation.to_param }
    get :index, author: 'invalid'
    assert_redirected_to admin_editions_path(state: :submitted, author: current_user, organisation: organisation)
  end

  test "index should redirect to department if logged in with no remembered filters" do
    organisation = create(:organisation)
    editor = login_as create(:departmental_editor, organisation: organisation)
    get :index
    assert_redirected_to admin_editions_path(organisation: organisation.id, state: :active)
  end

  view_test "should not show published editions as force published" do
    publication = create(:published_publication)
    get :index, state: :published, type: :publication

    assert_select_object(publication)
    refute_select "tr.force_published"
  end

  view_test "should show force published editions as force published" do
    publication = create(:published_publication, force_published: true)
    get :index, state: :published, type: :publication

{'fraction_non_alphanumeric': 0.08160367791224393, 'fraction_numerical': 0.0006760868095463458, 'mean_word_length': 5.042483660130719, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 9, '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': '4257673', 'n_tokens_mistral': 4203, 'n_tokens_neox': 4122, 'n_words': 1215}