# encoding: UTF-8 require 'simplecov' require 'simplecov-rcov' SimpleCov.start SimpleCov.formatter = SimpleCov::Formatter::RcovFormatter require 'test_helper' require 'govspeak_test_helper' require 'ostruct' class GovspeakTest < Test::Unit::TestCase include GovspeakTestHelper test "simple smoke-test" do rendered = Govspeak::Document.new("*this is markdown*").to_html assert_equal "

this is markdown

\n", rendered end test "simple smoke-test for simplified API" do rendered = Govspeak::Document.to_html("*this is markdown*") assert_equal "

this is markdown

\n", rendered end test "simple block extension" do rendered = Govspeak::Document.new("this \n{::reverse}\n*is*\n{:/reverse}\n markdown").to_html assert_equal "

this

\n\n

si

\n\n

markdown

\n", rendered end test "highlight-answer block extension" do rendered = Govspeak::Document.new("this \n{::highlight-answer}Lead in to *BIG TEXT*\n{:/highlight-answer}").to_html assert_equal %Q{

this

\n\n
\n

Lead in to BIG TEXT

\n
\n}, rendered end test "extracts headers with text, level and generated id" do document = Govspeak::Document.new %{ # Big title ### Small subtitle ## Medium title } assert_equal [ Govspeak::Header.new('Big title', 1, 'big-title'), Govspeak::Header.new('Small subtitle', 3, 'small-subtitle'), Govspeak::Header.new('Medium title', 2, 'medium-title') ], document.headers end test "extracts different ids for duplicate headers" do document = Govspeak::Document.new("## Duplicate header\n\n## Duplicate header") assert_equal [ Govspeak::Header.new('Duplicate header', 2, 'duplicate-header'), Govspeak::Header.new('Duplicate header', 2, 'duplicate-header-1') ], document.headers end test "extracts text with no HTML and normalised spacing" do input = "# foo\n\nbar baz " doc = Govspeak::Document.new(input) assert_equal "foo bar baz", doc.to_text end test "trailing space after the address should not prevent parsing" do input = %{$A 123 Test Street Testcase Cliffs Teston 0123 456 7890 $A } doc = Govspeak::Document.new(input) assert_equal %{

\n123 Test Street
Testcase Cliffs
Teston
0123 456 7890 \n

\n}, doc.to_html end test_given_govspeak("^ I am very informational ^") do assert_html_output %{

I am very informational

} assert_text_output "I am very informational" end test "processing an extension does not modify the provided input" do input = "^ I am very informational" Govspeak::Document.new(input).to_html assert_equal "^ I am very informational", input end test_given_govspeak "The following is very informational\n^ I am very informational ^" do assert_html_output %{

The following is very informational

I am very informational

} assert_text_output "The following is very informational I am very informational" end test_given_govspeak "^ I am very informational" do assert_html_output %{

I am very informational

} assert_text_output "I am very informational" end test_given_govspeak "@ I am very important @" do assert_html_output %{

I am very important

} assert_text_output "I am very important" end test_given_govspeak " The following is very important @ I am very important @ " do assert_html_output %{

The following is very important

I am very important

} assert_text_output "The following is very important I am very important" end test_given_govspeak "% I am very helpful %" do assert_html_output %{

I am very helpful

} assert_text_output "I am very helpful" end test_given_govspeak "The following is very helpful\n% I am very helpful %" do assert_html_output %{

The following is very helpful

I am very helpful

} assert_text_output "The following is very helpful I am very helpful" end test_given_govspeak "## Hello ##\n\n% I am very helpful %\r\n### Young Workers ###\n\n" do assert_html_output %{

Hello

I am very helpful

Young Workers

} assert_text_output "Hello I am very helpful Young Workers" end test_given_govspeak "% I am very helpful" do assert_html_output %{

I am very helpful

} assert_text_output "I am very helpful" end test_given_govspeak "This is a [link](http://www.gov.uk) isn't it?" do assert_html_output '

This is a link isn’t it?

' assert_text_output "This is a link isn’t it?" end test_given_govspeak "This is a [link with an at sign in it](http://www.gov.uk/@dg/@this) isn't it?" do assert_html_output '

This is a link with an at sign in it isn’t it?

' assert_text_output "This is a link with an at sign in it isn’t it?" end test_given_govspeak " HTML *[HTML]: Hyper Text Markup Language" do assert_html_output %{

HTML

} assert_text_output "HTML" end test_given_govspeak "x[a link](http://rubyforge.org)x" do assert_html_output '

a link

' assert_text_output "a link" end test_given_govspeak "x[an xx link](http://x.com)x" do assert_html_output '

an xx link

' end test_given_govspeak "[internal link](http://www.gov.uk)" do assert_html_output '

internal link

' end test_given_govspeak "[link with no host is assumed to be internal](/)" do assert_html_output '

link with no host is assumed to be internal

' end test_given_govspeak "[internal link with rel attribute keeps it](http://www.gov.uk){:rel='next'}" do assert_html_output '

' end test_given_govspeak "[external link without x markers](http://www.google.com)" do assert_html_output '

external link without x markers

' end test_given_govspeak "[external link with rel attribute](http://www.google.com){:rel='next'}" do assert_html_output '

' end test_given_govspeak "Text before [an external link](http://www.google.com)" do assert_html_output '

Text before an external link

' end test_given_govspeak "[An external link](http://www.google.com) with text afterwards" do assert_html_output '

An external link with text afterwards

' end test_given_govspeak "Text before [an external link](http://www.google.com) and text afterwards" do assert_html_output '

Text before an external link and text afterwards

' end test_given_govspeak "![image with external url](http://www.example.com/image.jpg)" do assert_html_output '

image with external url

' end test "should be able to override default 'document_domains' option" do html = Govspeak::Document.new("[internal link](http://www.not-external.com)", document_domains: %w(www.not-external.com)).to_html refute html.include?('rel="external"'), "should not consider www.not-external.com as an external url" end test "should be able to supply multiple domains for 'document_domains' option" do html = Govspeak::Document.new("[internal link](http://www.not-external-either.com)", document_domains: %w(www.not-external.com www.not-external-either.com)).to_html refute html.include?('rel="external"'), "should not consider www.not-external-either.com as an external url" end test "should be able to override default 'input' option" do html = Govspeak::Document.new("[external link](http://www.external.com)", input: "kramdown").to_html refute html.include?('rel="external"'), "should not automatically add rel external attribute" end test "should be able to override default 'entity output' option" do html = Govspeak::Document.new("&", entity_output: :numeric).to_html assert html.include?("&") end test "should be assume link with invalid uri is internal" do html = Govspeak::Document.new("[link](:invalid-uri)").to_html refute html.include?('rel="external"') end test "should be assume link with invalid uri component is internal" do html = Govspeak::Document.new("[link](mailto://www.example.com)").to_html refute html.include?('rel="external"') end # Regression test - the surrounded_by helper doesn't require the closing x # so 'xaa' was getting picked up by the external link helper above # TODO: review whether we should require closing symbols for these extensions # need to check all existing content. test_given_govspeak "xaa" do assert_html_output '

xaa

' assert_text_output "xaa" end test_given_govspeak " $! rainbow $!" do assert_html_output %{

rainbow

} assert_text_output "rainbow" end test_given_govspeak "$C help, send cake $C" do assert_html_output %{

help, send cake

} assert_text_output "help, send cake" end test_given_govspeak " $A street road $A" do assert_html_output %{

street
road

} assert_text_output "street road" end test_given_govspeak " $P $I help $I $P" do assert_html_output %{
\n\n
\n

help

\n
\n
} assert_text_output "help" end test_given_govspeak " $D can you tell me how to get to... $D" do assert_html_output %{

can you tell me how to get to…

} assert_text_output "can you tell me how to get to…" end test_given_govspeak " $CTA Click here to start the tool $CTA" do assert_html_output %{

Click here to start the tool

} assert_text_output "Click here to start the tool" end test_given_govspeak "Here is some text $CTA Click here to start the tool $CTA " do assert_html_output %{

Here is some text

Click here to start the tool

} end test_given_govspeak " [internal link](http://www.not-external.com) $CTA Click here to start the tool $CTA", [], document_domains: %w(www.not-external.com) do assert_html_output %{

internal link

Click here to start the tool

} end test_given_govspeak " 1. rod 2. jane 3. freddy" do assert_html_output "
    \n
  1. rod
  2. \n
  3. jane
  4. \n
  5. freddy
  6. \n
" assert_text_output "rod jane freddy" end test_given_govspeak " s1. zippy s2. bungle s3. george " do assert_html_output %{
  1. zippy

  2. bungle

  3. george

} assert_text_output "zippy bungle george" end test_given_govspeak ":scotland: I am very devolved\n and very scottish \n:scotland:" do assert_html_output '

This section applies to Scotland

I am very devolved and very scottish

' end test_given_govspeak "@ Message with [a link](http://foo.bar/)@" do assert_html_output %{

Message with a link

} end test "can reference attached images using !!n" do images = [OpenStruct.new(alt_text: 'my alt', url: "http://example.com/image.jpg")] given_govspeak "!!1", images do assert_html_output %Q{
my alt
} end end test "alt text of referenced images is escaped" do images = [OpenStruct.new(alt_text: %Q{my alt '&"<>}, url: "http://example.com/image.jpg")] given_govspeak "!!1", images do assert_html_output %Q{
my alt '&"<>
} end end test "silently ignores an image attachment if the referenced image is missing" do doc = Govspeak::Document.new("!!1") doc.images = [] assert_equal %Q{\n}, doc.to_html end test "adds image caption if given" do images = [OpenStruct.new(alt_text: "my alt", url: "http://example.com/image.jpg", caption: 'My Caption & so on')] given_govspeak "!!1", images do assert_html_output %Q{
my alt
My Caption & so on
} end end test "ignores a blank caption" do images = [OpenStruct.new(alt_text: "my alt", url: "http://example.com/image.jpg", caption: ' ')] given_govspeak "!!1", images do assert_html_output %Q{
my alt
} end end test "can sanitize a document" do document = Govspeak::Document.new("") assert_equal "doBadThings();", document.to_sanitized_html end test "can sanitize a document without image" do document = Govspeak::Document.new("") assert_equal "doBadThings();

", document.to_sanitized_html_without_images end test "identifies a Govspeak document containing malicious HTML as invalid" do document = Govspeak::Document.new("") refute document.valid? end test "identifies a Govspeak document containing acceptable HTML as valid" do document = Govspeak::Document.new("
some content
") assert document.valid? end end