# encoding: UTF-8 require 'test_helper' class GovspeakAttachmentTest < Minitest::Test def build_attachment(args = {}) { content_id: "2b4d92f3-f8cd-4284-aaaa-25b3a640d26c", id: 456, url: "http://example.com/attachment.pdf", title: "Attachment Title", }.merge(args) end def compress_html(html) html.gsub(/[\n\r]+[\s]*/, '') end def render_govspeak(govspeak, attachments = [], options = {}) options = options.merge(attachments: attachments) Govspeak::Document.new(govspeak, options).to_html end test "wraps an attachment in a section.attachment.embedded" do rendered = render_govspeak( "[embed:attachments:3ed2]", [build_attachment(content_id: "3ed2")] ) assert_match(/
/, rendered) end test "can convert an attachment with spaces" do rendered = render_govspeak( "[embed:attachments: 3ed2 ]", [build_attachment(content_id: "3ed2")] ) assert_match(/
/, rendered) end test "wraps an external attachment in a section.attachment.hosted-externally" do rendered = render_govspeak( "[embed:attachments:3ed2]", [build_attachment(content_id: "3ed2", external?: true)] ) assert_match(/
/, rendered) end test "outputs a pub-cover.png thumbnail by default" do rendered = render_govspeak( "[embed:attachments:3ed2]", [build_attachment(content_id: "3ed2")] ) assert_match(%r{}, compress_html(rendered)) end test "outputs a title link within a h2" do rendered = render_govspeak( "[embed:attachments:3ed2]", [build_attachment(content_id: "3ed2", id: 1, url: "http://a.b/c.pdf", title: "Attachment Title")] ) assert_match(%r{

\s*Attachment Title

}, compress_html(rendered)) end test "title link has rel='external' for an external link" do rendered = render_govspeak( "[embed:attachments:3ed2]", [build_attachment(content_id: "3ed2", id: 1, url: "http://a.b/c.pdf", external?: true)] ) assert_match(%r{}, rendered) end test "accessible attachment doesn't have the aria-describedby attribute" do rendered = render_govspeak( "[embed:attachments:3ed2]", [build_attachment(content_id: "3ed2", url: "http://a.b/c.pdf", accessible?: true)] ) assert_match(%r{}, rendered) end test "outputs reference if isbn is present" do rendered = render_govspeak( "[embed:attachments:3ed2]", [build_attachment(content_id: "3ed2", isbn: "123")] ) assert_match(%r{Ref: ISBN 123}, rendered) end test "outputs reference if uniuque_reference is present" do rendered = render_govspeak( "[embed:attachments:3ed2]", [build_attachment(content_id: "3ed2", unique_reference: "123")] ) assert_match(%r{Ref: 123}, rendered) end test "outputs reference if command_paper_number is present" do rendered = render_govspeak( "[embed:attachments:3ed2]", [build_attachment(content_id: "3ed2", command_paper_number: "11")] ) assert_match(%r{Ref: 11}, rendered) end test "outputs reference if hoc_paper_number is present" do rendered = render_govspeak( "[embed:attachments:3ed2]", [build_attachment(content_id: "3ed2", hoc_paper_number: "15", parliamentary_session: "1")] ) assert_match(%r{Ref: HC 15 1}, rendered) end test "can have multiple references" do rendered = render_govspeak( "[embed:attachments:3ed2]", [build_attachment(content_id: "3ed2", isbn: "123", unique_reference: "55")] ) assert_match(%r{Ref: ISBN 123, 55}, rendered) end test "can show an unnumbered command paper" do rendered = render_govspeak( "[embed:attachments:3ed2]", [build_attachment(content_id: "3ed2", unnumbered_command_paper?: true)] ) assert_match(%r{\s*Unnumbered command paper\s*}, compress_html(rendered)) end test "can show an unnumbered act paper" do rendered = render_govspeak( "[embed:attachments:3ed2]", [build_attachment(content_id: "3ed2", unnumbered_hoc_paper?: true)] ) assert_match(%r{\s*Unnumbered act paper\s*}, compress_html(rendered)) end test "unnumbered command paper takes precedence to unnumbered act paper" do rendered = render_govspeak( "[embed:attachments:3ed2]", [build_attachment(content_id: "3ed2", unnumbered_command_paper?: true, unnumbered_hoc_paper?: true)] ) assert_match(%r{\s*Unnumbered command paper\s*}, compress_html(rendered)) end test "shows a preview link for a previewable format" do rendered = render_govspeak( "[embed:attachments:3ed2]", [build_attachment(content_id: "3ed2", file_extension: "csv", url: "http://a.b/c.csv")] ) assert_match(%r{\s*View online\s*}, compress_html(rendered)) end test "Shows a download link for a previewable format" do rendered = render_govspeak( "[embed:attachments:3ed2]", [build_attachment(content_id: "3ed2", file_extension: "csv", url: "http://a.b/c.csv")] ) assert_match(%r{\s*Download CSVs*}, compress_html(rendered)) end test "Can show filesize for a download link for a previewable format" do rendered = render_govspeak( "[embed:attachments:3ed2]", [build_attachment(content_id: "3ed2", file_extension: "csv", url: "http://a.b/c.csv", file_size: 2048)] ) assert_match(%r{}, rendered) end test "for a HTML format it outputs HTML" do rendered = render_govspeak( "[embed:attachments:3ed2]", [build_attachment(content_id: "3ed2", file_extension: "html")] ) assert_match(%r{HTML}, rendered) end test "for an external type it outputs the url" do rendered = render_govspeak( "[embed:attachments:3ed2]", [build_attachment(content_id: "3ed2", url: "http://a.b/c.pdf", external?: true)] ) assert_match(%r{http://a.b/c.pdf}, rendered) end test "will show a file extension in a abbr element for non html" do rendered = render_govspeak( "[embed:attachments:1fe8]", [build_attachment(content_id: "1fe8", file_extension: "pdf")] ) assert_match(%r{PDF}, rendered) end test "will show file size in a span" do rendered = render_govspeak( "[embed:attachments:1fe8]", [build_attachment(content_id: "1fe8", file_size: 1024)] ) assert_match(%r{1 KB}, rendered) end test "will show number of pages" do rendered = render_govspeak( "[embed:attachments:1fe8]", [build_attachment(content_id: "1fe8", number_of_pages: 1)] ) assert_match(%r{1 page}, rendered) rendered = render_govspeak( "[embed:attachments:1fe8]", [build_attachment(content_id: "1fe8", number_of_pages: 2)] ) assert_match(%r{2 pages}, rendered) end test "can show multiple attributes separated by a comma" do rendered = render_govspeak( "[embed:attachments:1fe8]", [build_attachment(content_id: "1fe8", file_extension: "pdf", file_size: 1024)] ) pdf = %{PDF} file_size = %{1 KB} assert_match(%r{#{pdf}, #{file_size}}, rendered) end test "can show a link to order a copy of the attachment" do rendered = render_govspeak( "[embed:attachments:1fe8]", [build_attachment(content_id: "1fe8", order_url: "http://a.b/c")] ) assert_match(%r{Order a copy}, rendered) end test "can not show a link to order a copy of the attachment" do rendered = render_govspeak( "[embed:attachments:1fe8]", [build_attachment(content_id: "1fe8", order_url: "nil")] ) refute_match(%r{Order a copy}, rendered) end test "can show a price for ordering a copy of the attachment" do rendered = render_govspeak( "[embed:attachments:1fe8]", [build_attachment(content_id: "1fe8", order_url: "http://a.b/c", price: 10)] ) assert_match(%r{(£10.00)}, rendered) end test "can show opendocument help" do rendered = render_govspeak( "[embed:attachments:1fe8]", [build_attachment(content_id: "1fe8", opendocument?: true)] ) assert_match(%r{

}, rendered) end test "can not show opendocument help" do rendered = render_govspeak( "[embed:attachments:1fe8]", [build_attachment(content_id: "1fe8", opendocument?: false)] ) refute_match(%r{

}, rendered) end test "can show an accessibility warning" do rendered = render_govspeak( "[embed:attachments:1fe8]", [build_attachment(content_id: "1fe8", id: 10, accessible?: false)] ) assert_match(%r{

}, rendered) end test "can not show an accessibility warning" do rendered = render_govspeak( "[embed:attachments:1fe8]", [build_attachment(content_id: "1fe8", id: 10, accessible?: true)] ) refute_match(%r{
}, rendered) end test "shows accessibility mailto on a single line" do rendered = render_govspeak( "[embed:attachments:1fe8]", [build_attachment(content_id: "1fe8", id: 10, accessible?: false)] ) assert_match(%r{govuk-feedback@digital.cabinet-office.gov.uk}, rendered) end Dir.glob("locales/*.yml") do |filename| locale = File.basename(filename, ".yml") test "can render in #{locale}" do rendered = render_govspeak( "[embed:attachments:3ed2]", [build_attachment(content_id: "3ed2")], locale: locale, ) assert_match(/
/, rendered) end end test "a full attachment rendering looks correct" do attachment = { id: 123, content_id: "2b4d92f3-f8cd-4284-aaaa-25b3a640d26c", title: "Attachment Title", url: "http://example.com/test.pdf", opendocument?: true, order_url: "http://example.com/order", price: 12.3, isbn: "isbn-123", unnumbered_command_paper?: true, } rendered = render_govspeak( "[embed:attachments:2b4d92f3-f8cd-4284-aaaa-25b3a640d26c]", [build_attachment(attachment)] ) expected_html_output = %{

Attachment Title

Order a copy(£12.30)

This file is in an OpenDocument format

This file may not be suitable for users of assistive technology. Request an accessible format.

If you use assistive technology (eg a screen reader) and need a version of this document in a more accessible format, please email govuk-feedback@digital.cabinet-office.gov.uk. Please tell us what format you need. It will help us if you say what assistive technology you use.

} assert_equal(compress_html(expected_html_output), compress_html(rendered)) end test "attachment that isn't provided" do govspeak = "[embed:attachments:906ac8b7-850d-45c6-98e0-9525c680f891]" rendered = Govspeak::Document.new(govspeak).to_html assert_equal("\n", rendered) end test "attachment where filename is provided, rather than a content_id" do govspeak = "[embed:attachments:/path/to/file%20name.pdf]" rendered = Govspeak::Document.new(govspeak).to_html assert_equal("\n", rendered) end end