<commit_before>require "test_helper"

class PublicationTest < ActiveSupport::TestCase
  should_allow_image_attachments
  should_be_attachable
  should_force_review_of_bulk_uploaded_attachments
  should_not_allow_inline_attachments
  should_allow_referencing_of_statistical_data_sets
  should_protect_against_xss_and_content_attacks_on :title, :body, :summary, :change_note


  test 'has optional html version' do
    publication = build(:publication)
    refute publication.html_version.present?

    publication.html_version_attributes = {
      title: "title",
      body: "body"
    }
    publication.save!

    assert_equal "title", publication.reload.html_version.title
  end

  test 'html version is destroyed if the publication is destroyed' do
    publication = create(:publication, :with_html_version)
    html_version = publication.html_version
    publication.destroy
    refute HtmlVersion.find_by_id(html_version.id)
  end

  test 'html version is not saved if all blank' do
    publication = build(:publication)
    publication.html_version_attributes = {
      title: "",
      body: ""
    }
    publication.save!
    refute publication.html_version
  end

  test 'html version is copied over on republish' do
    publication = create(:published_publication, :with_html_version)
    new_draft = publication.create_draft(create(:author))

    assert publication.html_version.title, new_draft.html_version.title
    assert publication.html_version.body, new_draft.html_version.body

    new_draft.html_version.title = 'new title'
    refute_equal 'new title', publication.reload.html_version.title
  end

  test 'imported publications are valid when the publication_type is \'imported-awaiting-type\'' do
    publication = build(:publication, state: 'imported', publication_type: PublicationType.find_by_slug('imported-awaiting-type'))
    assert publication.valid?
  end

  test 'imported publications are not valid_as_draft? when the publcation_type is \'imported-awaiting-type\'' do
    publication = build(:publication, state: 'imported', publication_type: PublicationType.find_by_slug('imported-awaiting-type'))
    refute publication.valid_as_draft?
  end

  test 'imported publications are valid with a blank publication_date' do
    publication = build(:publication, state: 'imported', publication_date: nil)
    assert publication.valid?
  end

  test 'imported publications with a blank publication_date have no first_public_at' do
    publication = build(:publication, state: 'imported', publication_date: nil)
    assert_nil publication.first_public_at
  end

  [:draft, :scheduled, :published, :archived, :submitted, :rejected].each do |state|
    test "#{state} editions are not valid when the publication type is 'imported-awaiting-type'" do
      edition = build(:publication, state: state, publication_type: PublicationType.find_by_slug('imported-awaiting-type'))<commit_msg>Test slug does not change when the html title changes<commit_after>require "test_helper"

class PublicationTest < ActiveSupport::TestCase
  should_allow_image_attachments
  should_be_attachable
  should_force_review_of_bulk_uploaded_attachments
  should_not_allow_inline_attachments
  should_allow_referencing_of_statistical_data_sets
  should_protect_against_xss_and_content_attacks_on :title, :body, :summary, :change_note


  test 'has optional html version' do
    publication = build(:publication)
    refute publication.html_version.present?

    publication.html_version_attributes = {
      title: "title",
      body: "body"
    }
    publication.save!

    assert_equal "title", publication.reload.html_version.title
  end

  test 'html version is destroyed if the publication is destroyed' do
    publication = create(:publication, :with_html_version)
    html_version = publication.html_version
    publication.destroy
    refute HtmlVersion.find_by_id(html_version.id)
  end

  test 'html version is not saved if all blank' do
    publication = build(:publication)
    publication.html_version_attributes = {
      title: "",
      body: ""
    }
    publication.save!
    refute publication.html_version
  end

  test 'html version is copied over on republish' do
    publication = create(:published_publication, :with_html_version)
    new_draft = publication.create_draft(create(:author))

    assert publication.html_version.title, new_draft.html_version.title
    assert publication.html_version.body, new_draft.html_version.body
    assert publication.html_version.slug, new_draft.html_version.slug

    new_draft.html_version.title = 'new title'
    refute_equal 'new title', publication.reload.html_version.title
  end

  test 'slug does not change on republish' do
    publication = create(:published_publication, :with_html_version)
    new_draft = publication.create_draft(create(:author))
    new_draft.html_version.title = "title-changed"
    new_draft.minor_change = true
    new_draft.publish!
    document = new_draft.document
    latest_edition = document.latest_edition

    assert new_draft.html_version.slug, latest_edition.html_version.slug
  end

  test 'imported publications are valid when the publication_type is \'imported-awaiting-type\'' do
    publication = build(:publication, state: 'imported', publication_type: PublicationType.find_by_slug('imported-awaiting-type'))
    assert publication.valid?
  end

  test 'imported publications are not valid_as_draft? when the publcation_type is \'imported-awaiting-type\'' do
    publication = build(:publication, state: 'imported', publication_type: PublicationType.find_by_slug('imported-awaiting-type'))
    refute publication.valid_as_draft?
  end

  test 'imported publications are valid with a blank publication_date' do
    publication = build(:publication, state: 'imported', publication_date: nil)
    assert publication.valid?
  end

  test 'imported publications with a blank publication_date have no first_public_at' do
    publication = build(:publication, state: 'imported', publication_date: nil)
    assert_nil publication.first_public_at
  end

  [:draft, :scheduled, :published, :archived, :submitted, :rejected].each do |state|
    test "#{state} editions are not valid when the publication type is 'imported-awaiting-type'" do
      edition = build(:publication, state: state, publication_type: PublicationType.find_by_slug('imported-awaiting-type'))
{'fraction_non_alphanumeric': 0.08030803080308031, 'fraction_numerical': 0.0, 'mean_word_length': 4.854645814167434, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, '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': '5191142', 'n_tokens_mistral': 1856, 'n_tokens_neox': 1851, 'n_words': 455}