|
| 1 | +require "test_helper" |
| 2 | + |
| 3 | +class GovspeakPreviewHelperTest < ActionView::TestCase |
| 4 | + extend Minitest::Spec::DSL |
| 5 | + |
| 6 | + it "should not alter urls to other sites" do |
| 7 | + html = govspeak_to_html("no [change](http://external.example.com/page.html)") |
| 8 | + assert_select_within_html html, "a[href=?]", "http://external.example.com/page.html", text: "change" |
| 9 | + end |
| 10 | + |
| 11 | + it "should not alter mailto urls" do |
| 12 | + html = govspeak_to_html("no [change](mailto:dave@example.com)") |
| 13 | + assert_select_within_html html, "a[href=?]", "mailto:dave@example.com", text: "change" |
| 14 | + end |
| 15 | + |
| 16 | + it "should not alter invalid urls" do |
| 17 | + html = govspeak_to_html("no [change](not a valid url)") |
| 18 | + assert_select_within_html html, "a[href=?]", "not%20a%20valid%20url", text: "change" |
| 19 | + end |
| 20 | + |
| 21 | + it "should not alter partial urls" do |
| 22 | + html = govspeak_to_html("no [change](http://)") |
| 23 | + assert_select_within_html html, "a[href=?]", "http://", text: "change" |
| 24 | + end |
| 25 | + |
| 26 | + it "should wrap output with a govspeak class" do |
| 27 | + html = govspeak_to_html("govspeak-text") |
| 28 | + assert_select_within_html html, ".govspeak", text: "govspeak-text" |
| 29 | + end |
| 30 | + |
| 31 | + it "should mark the govspeak output as html safe" do |
| 32 | + html = govspeak_to_html("govspeak-text") |
| 33 | + assert html.html_safe? |
| 34 | + end |
| 35 | + |
| 36 | + it "should produce UTF-8 for HTML entities" do |
| 37 | + html = govspeak_to_html("a ['funny'](/url) thing") |
| 38 | + assert_select_within_html html, "a", text: "‘funny’" |
| 39 | + end |
| 40 | + |
| 41 | + it "does not change css class on buttons" do |
| 42 | + html = govspeak_to_html("{button}[Link text](https://www.gov.uk){/button}") |
| 43 | + assert_select_within_html html, "a.govuk-button", "Link text" |
| 44 | + end |
| 45 | + |
| 46 | + it "should only extract level two headers by default" do |
| 47 | + text = "# Heading 1\n\n## Heading 2\n\n### Heading 3" |
| 48 | + headers = govspeak_headers(text) |
| 49 | + assert_equal [Govspeak::Header.new("Heading 2", 2, "heading-2")], headers |
| 50 | + end |
| 51 | + |
| 52 | + it "should extract header hierarchy from level 2+3 headings" do |
| 53 | + text = "# Heading 1\n\n## Heading 2a\n\n### Heading 3a\n\n### Heading 3b\n\n#### Ignored heading\n\n## Heading 2b" |
| 54 | + headers = govspeak_header_hierarchy(text) |
| 55 | + assert_equal [ |
| 56 | + { |
| 57 | + header: Govspeak::Header.new("Heading 2a", 2, "heading-2a"), |
| 58 | + children: [ |
| 59 | + Govspeak::Header.new("Heading 3a", 3, "heading-3a"), |
| 60 | + Govspeak::Header.new("Heading 3b", 3, "heading-3b"), |
| 61 | + ], |
| 62 | + }, |
| 63 | + { |
| 64 | + header: Govspeak::Header.new("Heading 2b", 2, "heading-2b"), |
| 65 | + children: [], |
| 66 | + }, |
| 67 | + ], |
| 68 | + headers |
| 69 | + end |
| 70 | + |
| 71 | + it "should raise exception when extracting header hierarchy with orphaned level 3 headings" do |
| 72 | + e = assert_raise(Govspeak::OrphanedHeadingError) { govspeak_header_hierarchy("### Heading 3") } |
| 73 | + assert_equal "Heading 3", e.heading |
| 74 | + end |
| 75 | + |
| 76 | + it "adds numbers to h2 headings" do |
| 77 | + input = "# main\n\n## first\n\n## second" |
| 78 | + output = '<div class="govspeak"><h1 id="main">main</h1> <h2 id="first"> <span class="number">1. </span>first</h2> <h2 id="second"> <span class="number">2. </span>second</h2></div>' |
| 79 | + assert_equivalent_html output, govspeak_to_html(input, heading_numbering: :auto).gsub(/\s+/, " ") |
| 80 | + end |
| 81 | + |
| 82 | + it "adds sub-numbers to h3 tags" do |
| 83 | + input = "## first\n\n### first point one\n\n### first point two\n\n## second\n\n### second point one" |
| 84 | + expected_output1 = '<h2 id="first"> <span class="number">1. </span>first</h2>' |
| 85 | + expected_output_1a = '<h3 id="first-point-one"> <span class="number">1.1 </span>first point one</h3>' |
| 86 | + expected_output_1b = '<h3 id="first-point-two"> <span class="number">1.2 </span>first point two</h3>' |
| 87 | + expected_output2 = '<h2 id="second"> <span class="number">2. </span>second</h2>' |
| 88 | + expected_output_2a = '<h3 id="second-point-one"> <span class="number">2.1 </span>second point one</h3>' |
| 89 | + actual_output = govspeak_to_html(input, heading_numbering: :auto).gsub(/\s+/, " ") |
| 90 | + assert_match %r{#{expected_output1}}, actual_output |
| 91 | + assert_match %r{#{expected_output_1a}}, actual_output |
| 92 | + assert_match %r{#{expected_output_1b}}, actual_output |
| 93 | + assert_match %r{#{expected_output2}}, actual_output |
| 94 | + assert_match %r{#{expected_output_2a}}, actual_output |
| 95 | + end |
| 96 | + |
| 97 | + it "should not corrupt character encoding of numbered headings" do |
| 98 | + input = "# café" |
| 99 | + actual_output = govspeak_to_html(input, heading_numbering: :auto) |
| 100 | + assert actual_output.include?("café</h1>") |
| 101 | + end |
| 102 | + |
| 103 | + describe "admin flavour of govspeak" do |
| 104 | + it "should wrap admin output with a govspeak class" do |
| 105 | + html = govspeak_to_html("govspeak-text", { preview: true }) |
| 106 | + assert_select_within_html html, ".govspeak", text: "govspeak-text" |
| 107 | + end |
| 108 | + |
| 109 | + it "should mark the admin govspeak output as html safe" do |
| 110 | + html = govspeak_to_html("govspeak-text", { preview: true }) |
| 111 | + assert html.html_safe? |
| 112 | + end |
| 113 | + |
| 114 | + it "should call the embed codes helper" do |
| 115 | + input = "Here is some Govspeak" |
| 116 | + expected = "Expected output" |
| 117 | + ContentBlockManager::FindAndReplaceEmbedCodesService.expects(:call).with(input).returns(expected) |
| 118 | + govspeak_to_html(input, { preview: true }) |
| 119 | + end |
| 120 | + end |
| 121 | + |
| 122 | +private |
| 123 | + |
| 124 | + def collapse_whitespace(string) |
| 125 | + string.gsub(/\s+/, " ").strip |
| 126 | + end |
| 127 | +end |
0 commit comments