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

class GovspeakHelperTest < ActionView::TestCase
  include Admin::EditionRoutesHelper
  include PublicDocumentRoutesHelper

  setup do
    @request  = ActionController::TestRequest.new
    ActionController::Base.default_url_options = {}
  end
  attr_reader :request

  test "should wrap admin output with a govspeak class" do
    html = govspeak_to_admin_html("govspeak-text")
    assert_equal %{<div class="govspeak">\n<p>govspeak-text</p>\n</div>}, html.strip
  end

  test "should mark the admin govspeak output as html safe" do
    html = govspeak_to_admin_html("govspeak-text")
    assert html.html_safe?
  end

  test "should not alter urls to other sites in the admin preview" do
    html = govspeak_to_admin_html("no [change](http://external.example.com/page.html)")
    assert_govspeak %{<p>no <a href="http://external.example.com/page.html">change</a></p>}, html
  end

  test "should not alter urls to other sites" do
    html = govspeak_to_html("no [change](http://external.example.com/page.html)")
    assert_govspeak %{<p>no <a href="http://external.example.com/page.html">change</a></p>}, html
  end

  test "should not alter mailto urls in the admin preview" do
    html = govspeak_to_admin_html("no [change](mailto:<EMAIL>)")
    assert_govspeak %{<p>no <a href="mailto:<EMAIL>">change</a></p>}, html
  end

  test "should not alter mailto urls" do
    html = govspeak_to_html("no [change](mailto:<EMAIL>)")
    assert_govspeak %{<p>no <a href="mailto:<EMAIL>">change</a></p>}, html
  end

  test "should not alter invalid urls" do
    html = govspeak_to_html("no [change](not a valid url)")
    assert_govspeak %{<p>no <a href="not%20a%20valid%20url">change</a></p>}, html
  end

  test "should not alter partial urls in the admin preview" do
    html = govspeak_to_admin_html("no [change](http://)")
    assert_govspeak %{<p>no <a href="http://">change</a></p>}, html
  end

  test "should not alter partial urls" do
    html = govspeak_to_html("no [change](http://)")
    assert_govspeak %{<p>no <a href="http://">change</a></p>}, html
  end

  test "should rewrite link to draft edition in admin preview" do
    publication = create(:draft_publication)
    html = govspeak_to_admin_html("this and [that](#{admin_publication_url(publication)})")
    assert_govspeak %{<p>this and <span class="draft_link">that <sup class="explanation">(<a href="#{admin_publication_path(publication)}">draft</a>)</sup></span></p>}, html
  end

  test "should not alter unicode when replacing links" do
    publication = create(:published_publication)
    html = govspeak_to_admin_html("the [☃](#{admin_publication_url(publication)})")
    assert_govspeak %{<p>the <span class="published_link"><a href="#{public_document_url(publication)}">☃</a> <sup class="explanation">(<a href="#{admin_publication_path(publication)}">published</a>)</sup></span></p>}, html
  end

  test "should rewrite link to deleted edition in admin preview" do
    publication = create(:deleted_publication)
    html = govspeak_to_admin_html("this and [that](#{admin_publication_url(publication)})")
    assert_govspeak %{<p>this and <span class="deleted_link"><del>that</del> <sup class="explanation">(deleted)</sup></span></p>}, html
  end

  test "should rewrite link to missing edition in admin preview" do
    html = govspeak_to_admin_html("this and [that](#{admin_publication_url('missing-id')})")
    assert_govspeak %{<p>this and <span class="deleted_link"><del>that</del> <sup class="explanation">(deleted)</sup></span></p>}, html
  end

  test "should rewrite link to destroyed supporting page in admin preview" do
    html = govspeak_to_admin_html("this and [that](#{admin_edition_supporting_page_url("doesnt-exist", "missing-id")})")
    assert_govspeak %{<p>this and <span class="deleted_link"><del>that</del> <sup class="explanation">(deleted)</sup></span></p>}, html
  end

  test "should rewrite link to published edition in admin preview" do
    publication = create(:published_publication)
    html = govspeak_to_admin_html("this and [that](#{admin_publication_url(publication)})")
    assert_govspeak %{<p>this and <span class="published_link"><a href="#{public_document_url(publication)}">that</a> <sup class="explanation">(<a href="#{admin_publication_path(publication)}">published</a>)</sup></span></p>}, html
  end

  test "should rewrite link to published edition with a newer draft in admin preview" do
    publication = create(:published_publication)
    new_draft = publication.create_draft(create(:policy_writer))
    html = govspeak_to_admin_html("this and [that](#{admin_publication_url(publication)})")
    assert_govspeak %{<p>this and <span class="draft_link"><a href="#{public_document_url(publication)}">that</a> <sup class="explanation">(<a href="#{admin_publication_path(new_draft)}">draft</a>)</sup></span></p>}, html
  end

  test "should rewrite link to archived edition with a newer published edition in admin preview" do
    publication = create(:published_publication)
    writer = create(:policy_writer)
    editor = create(:departmental_editor)
    new_edition = publication.create_draft(writer)
    new_edition.change_note = "change-note"
    new_edition.save_as(writer)
    new_edition.submit!
    new_edition.publish_as(editor)
    html = govspeak_to_admin_html("this and [that](#{admin_publication_url(publication)})")
    assert_govspeak %{<p>this and <span class="published_link"><a href="#{public_document_url(publication)}">that</a> <sup class="explanation">(<a href="#{admin_publication_path(new_edition)}">published</a>)</sup></span></p>}, html
  end

  test "should rewrite link to deleted edition with an older published edition in admin preview" do
    publication = create(:published_publication)
    new_draft = publication.create_draft(create(:policy_writer))
    new_draft.delete!
    deleted_edition = new_draft
    html = govspeak_to_admin_html("this and [that](#{admin_publication_url(deleted_edition)})")
    assert_govspeak %{<p>this and <span class="published_link"><a href="#{public_document_url(publication)}">that</a> <sup class="explanation">(<a href="#{admin_publication_path(publication)}">published</a>)</sup></span></p>}, html
  end

  test "should allow attached images to be embedded in admin html" do
    images = [OpenStruct.new(alt_text: "My Alt", url: "http://example.com/image.jpg")]
    html = govspeak_to_admin_html("!!1", images)
    assert_govspeak_by_css_selector '.govspeak figure.image.embedded img', html
  end

  # public govspeak helper tests

  test "should wrap output with a govspeak class" do
    html = govspeak_to_html("govspeak-text")
    assert_equal %{<div class="govspeak">\n<p>govspeak-text</p>\n</div>}, html.strip
  end

  test "should mark the govspeak output as html safe" do
    html = govspeak_to_html("govspeak-text")
    assert html.html_safe?
  end

  test "should produce UTF-8 for HTML entities" do
    html = govspeak_to_html("a ['funny'](/url) thing")
    assert_govspeak %{<p>a <a href="/url">‘funny’</a> thing</p>}, html
  end

  test "should not link to draft editions with no published edition" do
    publication = create(:draft_publication)
    html = govspeak_to_html("this and [that](#{admin_publication_url(publication)})")
    assert_govspeak "<p>this and that</p>", html
  end

  test "should not link to deleted editions with no published edition" do
    publication = create(:deleted_publication)
    html = govspeak_to_html("this and [that](#{admin_publication_url(publication)})")
    assert_govspeak "<p>this and that</p>", html
  end

  [Policy, Publication, NewsArticle, Consultation].each do |edition_class|
    test "should rewrite absolute links to admin previews of published #{edition_class.name} as their public doc identity" do
      edition = create(:"published_#{edition_class.name.underscore}")
      html = govspeak_to_html("this and [that](http://test.host#{admin_document_path(edition)}) yeah?")
      assert_govspeak %{<p>this and <a href="#{public_document_url(edition)}">that</a> yeah?</p>}, html
    end
  end

  test "should rewrite absolute links to admin previews of published Speeches as their public doc identity" do
    speech = create(:published_speech)
    public_path = public_document_url(speech)
    html = govspeak_to_html("this and [that](#{admin_speech_url(speech)}) yeah?")
    assert_govspeak %{<p>this and <a href="#{public_path}">that</a> yeah?</p>}, html
  end

  test "should rewrite absolute links to admin previews of published SupportingPages as their public doc identity" do
    policy = create(:published_policy)
    supporting_page = create(:supporting_page, edition: policy)
    html = govspeak_to_html("this and [that](#{admin_supporting_page_url(supporting_page)}) yeah?")
    assert_govspeak %{<p>this and <a href="#{public_supporting_page_path(policy, supporting_page)}">that</a> yeah?</p>}, html
  end

  test 'should rewrite admin link to an archived edition with a published edition' do
    edition = create(:published_policy)
    writer = create(:policy_writer)
    editor = create(:departmental_editor)
    new_draft = edition.create_draft(writer)
    new_draft.save_as(writer)
    new_draft.submit!
    new_draft.publish_as(editor)

    html = govspeak_to_html("this and [that](http://test.host#{admin_document_path(edition)}) yeah?")
    assert_govspeak %{<p>this and <a href="#{public_document_url(edition)}">that</a> yeah?</p>}, html
  end
<commit_msg>Ensure existing links to supporting pages are re-written correctly.<commit_after># encoding: UTF-8
require 'test_helper'

class GovspeakHelperTest < ActionView::TestCase
  include Admin::EditionRoutesHelper
  include PublicDocumentRoutesHelper

  setup do
    @request  = ActionController::TestRequest.new
    ActionController::Base.default_url_options = {}
  end
  attr_reader :request

  test "should wrap admin output with a govspeak class" do
    html = govspeak_to_admin_html("govspeak-text")
    assert_equal %{<div class="govspeak">\n<p>govspeak-text</p>\n</div>}, html.strip
  end

  test "should mark the admin govspeak output as html safe" do
    html = govspeak_to_admin_html("govspeak-text")
    assert html.html_safe?
  end

  test "should not alter urls to other sites in the admin preview" do
    html = govspeak_to_admin_html("no [change](http://external.example.com/page.html)")
    assert_govspeak %{<p>no <a href="http://external.example.com/page.html">change</a></p>}, html
  end

  test "should not alter urls to other sites" do
    html = govspeak_to_html("no [change](http://external.example.com/page.html)")
    assert_govspeak %{<p>no <a href="http://external.example.com/page.html">change</a></p>}, html
  end

  test "should not alter mailto urls in the admin preview" do
    html = govspeak_to_admin_html("no [change](mailto:<EMAIL>)")
    assert_govspeak %{<p>no <a href="mailto:<EMAIL>">change</a></p>}, html
  end

  test "should not alter mailto urls" do
    html = govspeak_to_html("no [change](mailto:<EMAIL>)")
    assert_govspeak %{<p>no <a href="mailto:<EMAIL>">change</a></p>}, html
  end

  test "should not alter invalid urls" do
    html = govspeak_to_html("no [change](not a valid url)")
    assert_govspeak %{<p>no <a href="not%20a%20valid%20url">change</a></p>}, html
  end

  test "should not alter partial urls in the admin preview" do
    html = govspeak_to_admin_html("no [change](http://)")
    assert_govspeak %{<p>no <a href="http://">change</a></p>}, html
  end

  test "should not alter partial urls" do
    html = govspeak_to_html("no [change](http://)")
    assert_govspeak %{<p>no <a href="http://">change</a></p>}, html
  end

  test "should rewrite link to draft edition in admin preview" do
    publication = create(:draft_publication)
    html = govspeak_to_admin_html("this and [that](#{admin_publication_url(publication)})")
    assert_govspeak %{<p>this and <span class="draft_link">that <sup class="explanation">(<a href="#{admin_publication_path(publication)}">draft</a>)</sup></span></p>}, html
  end

  test "should not alter unicode when replacing links" do
    publication = create(:published_publication)
    html = govspeak_to_admin_html("the [☃](#{admin_publication_url(publication)})")
    assert_govspeak %{<p>the <span class="published_link"><a href="#{public_document_url(publication)}">☃</a> <sup class="explanation">(<a href="#{admin_publication_path(publication)}">published</a>)</sup></span></p>}, html
  end

  test "should rewrite link to deleted edition in admin preview" do
    publication = create(:deleted_publication)
    html = govspeak_to_admin_html("this and [that](#{admin_publication_url(publication)})")
    assert_govspeak %{<p>this and <span class="deleted_link"><del>that</del> <sup class="explanation">(deleted)</sup></span></p>}, html
  end

  test "should rewrite link to missing edition in admin preview" do
    html = govspeak_to_admin_html("this and [that](#{admin_publication_url('missing-id')})")
    assert_govspeak %{<p>this and <span class="deleted_link"><del>that</del> <sup class="explanation">(deleted)</sup></span></p>}, html
  end

  test "should rewrite link to destroyed supporting page in admin preview" do
    html = govspeak_to_admin_html("this and [that](#{admin_edition_supporting_page_url("doesnt-exist", "missing-id")})")
    assert_govspeak %{<p>this and <span class="deleted_link"><del>that</del> <sup class="explanation">(deleted)</sup></span></p>}, html
  end

  test "should rewrite link to published edition in admin preview" do
    publication = create(:published_publication)
    html = govspeak_to_admin_html("this and [that](#{admin_publication_url(publication)})")
    assert_govspeak %{<p>this and <span class="published_link"><a href="#{public_document_url(publication)}">that</a> <sup class="explanation">(<a href="#{admin_publication_path(publication)}">published</a>)</sup></span></p>}, html
  end

  test "should rewrite link to published edition with a newer draft in admin preview" do
    publication = create(:published_publication)
    new_draft = publication.create_draft(create(:policy_writer))
    html = govspeak_to_admin_html("this and [that](#{admin_publication_url(publication)})")
    assert_govspeak %{<p>this and <span class="draft_link"><a href="#{public_document_url(publication)}">that</a> <sup class="explanation">(<a href="#{admin_publication_path(new_draft)}">draft</a>)</sup></span></p>}, html
  end

  test "should rewrite link to archived edition with a newer published edition in admin preview" do
    publication = create(:published_publication)
    writer = create(:policy_writer)
    editor = create(:departmental_editor)
    new_edition = publication.create_draft(writer)
    new_edition.change_note = "change-note"
    new_edition.save_as(writer)
    new_edition.submit!
    new_edition.publish_as(editor)
    html = govspeak_to_admin_html("this and [that](#{admin_publication_url(publication)})")
    assert_govspeak %{<p>this and <span class="published_link"><a href="#{public_document_url(publication)}">that</a> <sup class="explanation">(<a href="#{admin_publication_path(new_edition)}">published</a>)</sup></span></p>}, html
  end

  test "should rewrite link to deleted edition with an older published edition in admin preview" do
    publication = create(:published_publication)
    new_draft = publication.create_draft(create(:policy_writer))
    new_draft.delete!
    deleted_edition = new_draft
    html = govspeak_to_admin_html("this and [that](#{admin_publication_url(deleted_edition)})")
    assert_govspeak %{<p>this and <span class="published_link"><a href="#{public_document_url(publication)}">that</a> <sup class="explanation">(<a href="#{admin_publication_path(publication)}">published</a>)</sup></span></p>}, html
  end

  test "should allow attached images to be embedded in admin html" do
    images = [OpenStruct.new(alt_text: "My Alt", url: "http://example.com/image.jpg")]
    html = govspeak_to_admin_html("!!1", images)
    assert_govspeak_by_css_selector '.govspeak figure.image.embedded img', html
  end

  # public govspeak helper tests

  test "should wrap output with a govspeak class" do
    html = govspeak_to_html("govspeak-text")
    assert_equal %{<div class="govspeak">\n<p>govspeak-text</p>\n</div>}, html.strip
  end

  test "should mark the govspeak output as html safe" do
    html = govspeak_to_html("govspeak-text")
    assert html.html_safe?
  end

  test "should produce UTF-8 for HTML entities" do
    html = govspeak_to_html("a ['funny'](/url) thing")
    assert_govspeak %{<p>a <a href="/url">‘funny’</a> thing</p>}, html
  end

  test "should not link to draft editions with no published edition" do
    publication = create(:draft_publication)
    html = govspeak_to_html("this and [that](#{admin_publication_url(publication)})")
    assert_govspeak "<p>this and that</p>", html
  end

  test "should not link to deleted editions with no published edition" do
    publication = create(:deleted_publication)
    html = govspeak_to_html("this and [that](#{admin_publication_url(publication)})")
    assert_govspeak "<p>this and that</p>", html
  end

  [Policy, Publication, NewsArticle, Consultation].each do |edition_class|
    test "should rewrite absolute links to admin previews of published #{edition_class.name} as their public doc identity" do
      edition = create(:"published_#{edition_class.name.underscore}")
      html = govspeak_to_html("this and [that](http://test.host#{admin_document_path(edition)}) yeah?")
      assert_govspeak %{<p>this and <a href="#{public_document_url(edition)}">that</a> yeah?</p>}, html
    end
  end

  test "should rewrite absolute links to admin previews of published Speeches as their public doc identity" do
    speech = create(:published_speech)
    public_path = public_document_url(speech)
    html = govspeak_to_html("this and [that](#{admin_speech_url(speech)}) yeah?")
    assert_govspeak %{<p>this and <a href="#{public_path}">that</a> yeah?</p>}, html
  end

  test "should rewrite absolute links to admin previews of published SupportingPages as their public doc identity" do
    policy = create(:published_policy)
    supporting_page = create(:supporting_page, edition: policy)
    html = govspeak_to_html("this and [that](#{admin_supporting_page_url(supporting_page)}) yeah?")
    assert_govspeak %{<p>this and <a href="#{public_supporting_page_path(policy, supporting_page)}">that</a> yeah?</p>}, html
  end

  test "should rewrite absolute links to old-style admin previews of published SupportingPages as their doc identity" do
    policy = create(:published_policy)
    supporting_page = create(:supporting_page, edition: policy)
    old_style_supporting_page_url = admin_supporting_page_url(supporting_page).gsub(/editions/, "documents")
    html = govspeak_to_html("this and [that](#{old_style_supporting_page_url}) yeah?")
    assert_govspeak %{<p>this and <a href="#{public_supporting_page_path(policy, supporting_page)}">that</a> yeah?</p>}, html
  end

  test 'should rewrite admin link to an archived edition with a published edition' do
    edition = create(:published_policy)
    writer = create(:policy_writer)
    editor = create(:departmental_editor)
    new_draft = edition.create_draft(writer)
    new_draft.save_as(writer)
    new_draft.submit!
    new_draft.publish_as(editor)

    html = govspeak_to_html("this and [that](http://test.host#{admin_document_path(edition)}) yeah?")
    assert_govspeak %{<p>this and <a href="#{public_document_url(edition)}">that</a> yeah?</p>}, html
  end

{'fraction_non_alphanumeric': 0.14015442041212864, 'fraction_numerical': 0.0009203865623561896, 'mean_word_length': 5.404060248853962, 'pattern_counts': {'":': 0, '<': 301, '<?xml version=': 0, '>': 299, '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': '3942093', 'n_tokens_mistral': 6504, 'n_tokens_neox': 6328, 'n_words': 1512}