govspeak 4.0.0 → 5.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,22 @@
1
+ require 'active_support/core_ext/array'
2
+ require 'ostruct'
3
+
4
+ class ContactPresenter
5
+ attr_reader :contact
6
+ delegate :id, :content_id, :title, :recipient, :street_address, :postal_code,
7
+ :locality, :region, :country_code, :country_name, :email, :contact_form_url,
8
+ :comments, :worldwide_organisation_path, to: :contact
9
+
10
+ def initialize(contact)
11
+ @contact = OpenStruct.new(contact)
12
+ end
13
+
14
+ def contact_numbers
15
+ Array.wrap(contact[:contact_numbers])
16
+ end
17
+
18
+ def has_postal_address?
19
+ recipient.present? || street_address.present? || locality.present? ||
20
+ region.present? || postal_code.present?
21
+ end
22
+ end
@@ -3,12 +3,10 @@
3
3
  <%= attachment.thumbnail_link %>
4
4
  </div>
5
5
  <div class="attachment-details">
6
- <h2 class="title"><%= attachment.attachement_details %></h2>
6
+ <h2 class="title"><%= attachment.attachment_details %></h2>
7
7
  <p class="metadata">
8
8
  <% if attachment.references? %>
9
- <span class="references">
10
- <%= t('attachment.headings.reference') %>: <%= attachment.references %>
11
- </span>
9
+ <span class="references"><%= t('attachment.headings.reference') %>: <%= attachment.reference %></span>
12
10
  <% end %>
13
11
  <% if attachment.unnumbered_paper? %>
14
12
  <span class="unnumbered-paper">
@@ -21,13 +19,9 @@
21
19
  <% end %>
22
20
  <% if attachment.previewable? %>
23
21
  <span class="preview">
24
- <strong>
25
- <%= attachment.link("View online", attachment.preview_url) %>
26
- </strong>
27
- </span>
28
- <span class="download">
29
- attachment.download_link
22
+ <strong><%= attachment.link("View online", attachment.preview_url) %></strong>
30
23
  </span>
24
+ <span class="download"><%= attachment.download_link %></span>
31
25
  <% else %>
32
26
  <%= attachment.attachment_attributes %>
33
27
  <% end %>
@@ -35,10 +29,8 @@
35
29
  <% if attachment.order_url.present? %>
36
30
  <p>
37
31
  <%= attachment.link t('attachment.headings.order_a_copy'), attachment.order_url,
38
- class: "order_url", title: t('attachment.headings.order_a_copy_full') %>
39
- <% if attachment.price %>
40
- (<span class="price"><%= attachment.price %></span>)
41
- <% end %>
32
+ class: "order_url", title: t('attachment.headings.order_a_copy_full')
33
+ %><% if attachment.price %>(<span class="price"><%= attachment.price %></span>)<% end %>
42
34
  </p>
43
35
  <% end %>
44
36
 
@@ -57,7 +49,7 @@
57
49
  <%= t('attachment.accessibility.full_help_html',
58
50
  email: attachment.alternative_format_order_link,
59
51
  title: attachment.title,
60
- references: attachment.references) %>
52
+ references: attachment.references_for_title) %>
61
53
  </p>
62
54
  </div>
63
55
  <% end %>
@@ -25,8 +25,8 @@
25
25
  <% end %>
26
26
  <% contact.contact_numbers.each do |number| %>
27
27
  <p class="tel">
28
- <span class="type"><%= number.label %></span>
29
- <%= number.number %>
28
+ <span class="type"><%= number[:label] %></span>
29
+ <%= number[:number] %>
30
30
  </p>
31
31
  <% end %>
32
32
  </div>
@@ -0,0 +1,6 @@
1
+ <span <% if attachment.id %>id="attachment_<%= attachment.id %>" <% end %>class="attachment-inline">
2
+ <%= attachment.link attachment.title, attachment.url %>
3
+ <% unless attachment.attachment_attributes.empty? %>
4
+ (<%= attachment.attachment_attributes %>)
5
+ <% end %>
6
+ </span>
@@ -0,0 +1,108 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'test_helper'
4
+
5
+ class GovspeakAttachmentsImageTest < Minitest::Test
6
+ def build_attachment(args = {})
7
+ {
8
+ content_id: "2b4d92f3-f8cd-4284-aaaa-25b3a640d26c",
9
+ id: 456,
10
+ url: "http://example.com/attachment.jpg",
11
+ title: "Attachment Title",
12
+ }.merge(args)
13
+ end
14
+
15
+ def render_govspeak(govspeak, attachments = [])
16
+ Govspeak::Document.new(govspeak, attachments: attachments).to_html
17
+ end
18
+
19
+ def compress_html(html)
20
+ html.gsub(/[\n\r]+[\s]*/, '')
21
+ end
22
+
23
+ test "renders an empty string for an image attachment not found" do
24
+ assert_equal("\n", render_govspeak("[embed:attachments:image:1fe8]", [build_attachment]))
25
+ end
26
+
27
+ test "wraps an attachment in a figure with the id if the id is present" do
28
+ rendered = render_govspeak(
29
+ "[embed:attachments:image:1fe8]",
30
+ [build_attachment(id: 10, content_id: "1fe8")]
31
+ )
32
+ assert_match(/<figure id="attachment_10" class="image embedded">/, rendered)
33
+ end
34
+
35
+ test "wraps an attachment in a figure without the id if the id is not present" do
36
+ rendered = render_govspeak(
37
+ "[embed:attachments:image:1fe8]",
38
+ [build_attachment(id: nil, content_id: "1fe8")]
39
+ )
40
+ assert_match(/<figure class="image embedded">/, rendered)
41
+ end
42
+
43
+ test "renders an attachment if there are spaces around the content_id" do
44
+ rendered = render_govspeak(
45
+ "[embed:attachments:image: 1fe8 ]",
46
+ [build_attachment(id: 10, content_id: "1fe8")]
47
+ )
48
+ assert_match(/<figure id="attachment_10" class="image embedded">/, rendered)
49
+ end
50
+
51
+ test "has an image element to the file" do
52
+ rendered = render_govspeak(
53
+ "[embed:attachments:image:1fe8]",
54
+ [build_attachment(id: nil, url: "http://a.b/c.jpg", content_id: "1fe8")]
55
+ )
56
+ assert_match(%r{<img.*src="http://a.b/c.jpg"}, rendered)
57
+ end
58
+
59
+ test "renders the image title as an alt tag" do
60
+ rendered = render_govspeak(
61
+ "[embed:attachments:image:1fe8]",
62
+ [build_attachment(id: nil, title: "My Title", content_id: "1fe8")]
63
+ )
64
+ assert_match(%r{<img.*alt="My Title"}, rendered)
65
+ end
66
+
67
+ test "can render a nil image title" do
68
+ rendered = render_govspeak(
69
+ "[embed:attachments:image:1fe8]",
70
+ [build_attachment(id: nil, title: nil, content_id: "1fe8")]
71
+ )
72
+ assert_match(%r{<img.*alt=""}, rendered)
73
+ end
74
+
75
+ test "a full image attachment rendering looks correct" do
76
+ rendered = render_govspeak(
77
+ "[embed:attachments:image:1fe8]",
78
+ [build_attachment(id: 10, url: "http://a.b/c.jpg", title: "My Title", content_id: "1fe8")]
79
+ )
80
+ expected_html_output = %{
81
+ <figure id="attachment_10" class="image embedded">
82
+ <div class="img"><img src="http://a.b/c.jpg" alt="My Title"></div>
83
+ </figure>
84
+ }
85
+ assert_match(compress_html(expected_html_output), compress_html(rendered))
86
+ end
87
+
88
+ # test inserted because divs can be stripped inside a table
89
+ test "can be rendered inside a table" do
90
+ rendered = render_govspeak(
91
+ "| [embed:attachments:image:1fe8] |",
92
+ [build_attachment(content_id: "1fe8", id: nil)]
93
+ )
94
+
95
+ regex = %r{<td><figure class="image embedded"><div class="img">(.*?)</div></figure></td>}
96
+ assert_match(regex, rendered)
97
+ end
98
+
99
+ test "renders an empty string for no match" do
100
+ rendered = render_govspeak("[embed:attachments:image:1fe8]")
101
+ assert_equal("\n", rendered)
102
+ end
103
+
104
+ test "renders an empty string for a filename instead of a content id" do
105
+ rendered = render_govspeak("[embed:attachments:image: path/to/file.jpg ]")
106
+ assert_equal("\n", rendered)
107
+ end
108
+ end
@@ -0,0 +1,172 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'test_helper'
4
+
5
+ class GovspeakAttachmentsInlineTest < Minitest::Test
6
+ def build_attachment(args = {})
7
+ {
8
+ content_id: "2b4d92f3-f8cd-4284-aaaa-25b3a640d26c",
9
+ id: 456,
10
+ url: "http://example.com/attachment.pdf",
11
+ title: "Attachment Title",
12
+ }.merge(args)
13
+ end
14
+
15
+ def render_govspeak(govspeak, attachments = [])
16
+ Govspeak::Document.new(govspeak, attachments: attachments).to_html
17
+ end
18
+
19
+ test "renders an empty string for an inline attachment not found" do
20
+ assert_equal("\n", render_govspeak("[embed:attachments:inline:1fe8]", [build_attachment]))
21
+ end
22
+
23
+ test "wraps an attachment in a span with the id if the id is present" do
24
+ rendered = render_govspeak(
25
+ "[embed:attachments:inline:1fe8]",
26
+ [build_attachment(id: 10, content_id: "1fe8")]
27
+ )
28
+ assert_match(/<span id="attachment_10" class="attachment-inline">/, rendered)
29
+ end
30
+
31
+ test "wraps an attachment in a span without the id if the id is not present" do
32
+ rendered = render_govspeak(
33
+ "[embed:attachments:inline:1fe8]",
34
+ [build_attachment(id: nil, content_id: "1fe8")]
35
+ )
36
+ assert_match(/<span class="attachment-inline">/, rendered)
37
+ end
38
+
39
+ test "renders an attachment where the content id has spaces" do
40
+ rendered = render_govspeak(
41
+ "[embed:attachments:inline: 1fe8 ]",
42
+ [build_attachment(id: nil, content_id: "1fe8")]
43
+ )
44
+ assert_match(/<span class="attachment-inline">/, rendered)
45
+ end
46
+
47
+ test "links to the attachment file" do
48
+ rendered = render_govspeak(
49
+ "[embed:attachments:inline:1fe8]",
50
+ [build_attachment(content_id: "1fe8", url: "http://a.b/f.pdf", title: "My Pdf")]
51
+ )
52
+ assert_match(%r{<a href="http://a.b/f.pdf">My Pdf</a>}, rendered)
53
+ end
54
+
55
+ test "renders with a nil title" do
56
+ rendered = render_govspeak(
57
+ "[embed:attachments:inline:1fe8]",
58
+ [build_attachment(content_id: "1fe8", url: "http://a.b/f.pdf", title: nil)]
59
+ )
60
+ assert_match(%r{<a href="http://a.b/f.pdf"></a>}, rendered)
61
+ end
62
+
63
+ test "renders on a single line" do
64
+ rendered = render_govspeak(
65
+ "[embed:attachments:inline:2bc1]",
66
+ [build_attachment(content_id: "2bc1", external?: true)]
67
+ )
68
+ assert_match(%r{<span id="attachment[^\n]*</span>}, rendered)
69
+ end
70
+
71
+ test "doesn't have spaces between the span and the link" do
72
+ rendered = render_govspeak(
73
+ "[embed:attachments:inline:2bc1]",
74
+ [build_attachment(content_id: "2bc1", id: nil)]
75
+ )
76
+ assert_match(%r{<span class="attachment-inline"><a href=".*">.*</a></span>}, rendered)
77
+ end
78
+
79
+ test "will show HTML type (in brackets) if file_extension is specified as html" do
80
+ rendered = render_govspeak(
81
+ "[embed:attachments:inline:1fe8]",
82
+ [build_attachment(content_id: "1fe8", file_extension: "html")]
83
+ )
84
+ assert_match(%r{(<span class="type">HTML</span>)}, rendered)
85
+ end
86
+
87
+ test "will show url (in brackets) if specified as external" do
88
+ rendered = render_govspeak(
89
+ "[embed:attachments:inline:1fe8]",
90
+ [build_attachment(content_id: "1fe8", url: "http://a.b/f.pdf", external?: true)]
91
+ )
92
+ assert_match(%r{(<span class="url">http://a.b/f.pdf</span>)}, rendered)
93
+ end
94
+
95
+ test "will not show url if file_extension is specified as html" do
96
+ rendered = render_govspeak(
97
+ "[embed:attachments:inline:1fe8]",
98
+ [build_attachment(content_id: "1fe8", url: "http://a.b/f.pdf", file_extension: "html", external?: true)]
99
+ )
100
+ refute_match(%r{(<span class="url">http://a.b/f.pdf</span>)}, rendered)
101
+ end
102
+
103
+ test "will show a file extension in a abbr element for non html" do
104
+ rendered = render_govspeak(
105
+ "[embed:attachments:inline:1fe8]",
106
+ [build_attachment(content_id: "1fe8", file_extension: "csv")]
107
+ )
108
+ refute_match(%r{<span class="type"><abbr title="Comma-separated values">CSV</abbr></span>}, rendered)
109
+ end
110
+
111
+ test "will show file size in a span" do
112
+ rendered = render_govspeak(
113
+ "[embed:attachments:inline:1fe8]",
114
+ [build_attachment(content_id: "1fe8", file_size: 1024)]
115
+ )
116
+ assert_match(%r{<span class="file-size">1 KB</span>}, rendered)
117
+ end
118
+
119
+ test "will show number of pages" do
120
+ rendered = render_govspeak(
121
+ "[embed:attachments:inline:1fe8]",
122
+ [build_attachment(content_id: "1fe8", number_of_pages: 1)]
123
+ )
124
+ assert_match(%r{<span class="page-length">1 page</span>}, rendered)
125
+ rendered = render_govspeak(
126
+ "[embed:attachments:inline:1fe8]",
127
+ [build_attachment(content_id: "1fe8", number_of_pages: 2)]
128
+ )
129
+ assert_match(%r{<span class="page-length">2 pages</span>}, rendered)
130
+ end
131
+
132
+ test "will show attributes after link" do
133
+ rendered = render_govspeak(
134
+ "[embed:attachments:inline:1fe8]",
135
+ [build_attachment(
136
+ content_id: "1fe8",
137
+ url: "http://a.b/test.txt",
138
+ title: "My Attached Text File",
139
+ file_extension: "txt",
140
+ file_size: 2048,
141
+ number_of_pages: 2,
142
+ )]
143
+ )
144
+ link = %{<a href="#{Regexp.quote('http://a.b/test.txt')}">My Attached Text File</a>}
145
+ type = %{<span class="type">Plain text</span>}
146
+ file_size = %{<span class="file-size">2 KB</span>}
147
+ pages = %{<span class="page-length">2 pages</span>}
148
+ assert_match(/#{link}\s+\(#{type}, #{file_size}, #{pages}\)/, rendered)
149
+ end
150
+
151
+ test "can render two inline attachments on the same line" do
152
+ rendered = render_govspeak(
153
+ "[embed:attachments:inline:1fe8] and [embed:attachments:inline:2abc]",
154
+ [
155
+ build_attachment(content_id: "1fe8", url: "http://a.b/test.txt", title: "Text File"),
156
+ build_attachment(content_id: "2abc", url: "http://a.b/test.pdf", title: "PDF File")
157
+ ]
158
+ )
159
+ assert_match(%r{<a href="http://a.b/test.txt">Text File</a>}, rendered)
160
+ assert_match(%r{<a href="http://a.b/test.pdf">PDF File</a>}, rendered)
161
+ end
162
+
163
+ test "will render a not found attachment as an empty string" do
164
+ rendered = render_govspeak("[embed:attachments:inline:1fe8]")
165
+ assert_equal("\n", rendered)
166
+ end
167
+
168
+ test "will render a file name used as a content id as an empty string" do
169
+ rendered = render_govspeak("[embed:attachments:inline: path/to/file name.jpg ]")
170
+ assert_equal("\n", rendered)
171
+ end
172
+ end
@@ -0,0 +1,411 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'test_helper'
4
+
5
+ class GovspeakAttachmentTest < Minitest::Test
6
+ def build_attachment(args = {})
7
+ {
8
+ content_id: "2b4d92f3-f8cd-4284-aaaa-25b3a640d26c",
9
+ id: 456,
10
+ url: "http://example.com/attachment.pdf",
11
+ title: "Attachment Title",
12
+ }.merge(args)
13
+ end
14
+
15
+ def compress_html(html)
16
+ html.gsub(/[\n\r]+[\s]*/, '')
17
+ end
18
+
19
+ def render_govspeak(govspeak, attachments = [], options = {})
20
+ options = options.merge(attachments: attachments)
21
+ Govspeak::Document.new(govspeak, options).to_html
22
+ end
23
+
24
+ test "wraps an attachment in a section.attachment.embedded" do
25
+ rendered = render_govspeak(
26
+ "[embed:attachments:3ed2]",
27
+ [build_attachment(content_id: "3ed2")]
28
+ )
29
+ assert_match(/<section class="attachment embedded">/, rendered)
30
+ end
31
+
32
+ test "can convert an attachment with spaces" do
33
+ rendered = render_govspeak(
34
+ "[embed:attachments: 3ed2 ]",
35
+ [build_attachment(content_id: "3ed2")]
36
+ )
37
+ assert_match(/<section class="attachment embedded">/, rendered)
38
+ end
39
+
40
+ test "wraps an external attachment in a section.attachment.hosted-externally" do
41
+ rendered = render_govspeak(
42
+ "[embed:attachments:3ed2]",
43
+ [build_attachment(content_id: "3ed2", external?: true)]
44
+ )
45
+ assert_match(/<section class="attachment hosted-externally">/, rendered)
46
+ end
47
+
48
+ test "outputs a pub-cover.png thumbnail by default" do
49
+ rendered = render_govspeak(
50
+ "[embed:attachments:3ed2]",
51
+ [build_attachment(content_id: "3ed2")]
52
+ )
53
+ assert_match(%r{<img src="/images/pub-cover.png"}, rendered)
54
+ end
55
+
56
+ test "outputs a specified thumbnail for a pdf with a thumbnail_url" do
57
+ rendered = render_govspeak(
58
+ "[embed:attachments:3ed2]",
59
+ [build_attachment(content_id: "3ed2", file_extension: "pdf", thumbnail_url: "http://a.b/custom.png")]
60
+ )
61
+ assert_match(%r{<img src="http://a.b/custom.png"}, rendered)
62
+ end
63
+
64
+ test "outputs pub-cover.png for a pdf without thumbnail_url" do
65
+ rendered = render_govspeak(
66
+ "[embed:attachments:3ed2]",
67
+ [build_attachment(content_id: "3ed2", file_extension: "pdf", thumbnail_url: nil)]
68
+ )
69
+ assert_match(%r{<img src="/images/pub-cover.png"}, rendered)
70
+ end
71
+
72
+ test "outputs pub-cover-html.png for a file with html file_extension" do
73
+ rendered = render_govspeak(
74
+ "[embed:attachments:3ed2]",
75
+ [build_attachment(content_id: "3ed2", file_extension: "html")]
76
+ )
77
+ assert_match(%r{<img src="/images/pub-cover-html.png"}, rendered)
78
+ end
79
+
80
+ test "outputs pub-cover-doc.png for a file with docx file_extension" do
81
+ rendered = render_govspeak(
82
+ "[embed:attachments:3ed2]",
83
+ [build_attachment(content_id: "3ed2", file_extension: "docx")]
84
+ )
85
+ assert_match(%r{<img src="/images/pub-cover-doc.png"}, rendered)
86
+ end
87
+
88
+ test "outputs pub-cover-spreadsheet.png for a file with xls file_extension" do
89
+ rendered = render_govspeak(
90
+ "[embed:attachments:3ed2]",
91
+ [build_attachment(content_id: "3ed2", file_extension: "xls")]
92
+ )
93
+ assert_match(%r{<img src="/images/pub-cover-spreadsheet.png"}, rendered)
94
+ end
95
+
96
+ test "outputs no thumbnail for a previewable file" do
97
+ rendered = render_govspeak(
98
+ "[embed:attachments:3ed2]",
99
+ [build_attachment(content_id: "3ed2", file_extension: "csv")]
100
+ )
101
+ assert_match(%r{<div class="attachment-thumb"></div>}, compress_html(rendered))
102
+ end
103
+
104
+ test "outputs a title link within a h2" do
105
+ rendered = render_govspeak(
106
+ "[embed:attachments:3ed2]",
107
+ [build_attachment(content_id: "3ed2", id: 1, url: "http://a.b/c.pdf", title: "Attachment Title")]
108
+ )
109
+ assert_match(%r{<h2 class="title">\s*<a href="http://a.b/c.pdf" aria-describedby="attachment-1-accessibility-help">Attachment Title</a></h2>}, compress_html(rendered))
110
+ end
111
+
112
+ test "title link has rel='external' for an external link" do
113
+ rendered = render_govspeak(
114
+ "[embed:attachments:3ed2]",
115
+ [build_attachment(content_id: "3ed2", id: 1, url: "http://a.b/c.pdf", external?: true)]
116
+ )
117
+ assert_match(%r{<a href="http://a.b/c.pdf" rel="external" aria-describedby="attachment-1-accessibility-help">}, rendered)
118
+ end
119
+
120
+ test "accessible attachment doesn't have the aria-describedby attribute" do
121
+ rendered = render_govspeak(
122
+ "[embed:attachments:3ed2]",
123
+ [build_attachment(content_id: "3ed2", url: "http://a.b/c.pdf", accessible?: true)]
124
+ )
125
+ assert_match(%r{<a href="http://a.b/c.pdf">}, rendered)
126
+ end
127
+
128
+ test "outputs reference if isbn is present" do
129
+ rendered = render_govspeak(
130
+ "[embed:attachments:3ed2]",
131
+ [build_attachment(content_id: "3ed2", isbn: "123")]
132
+ )
133
+ assert_match(%r{<span class="references">Ref: ISBN <span class="isbn">123</span></span>}, rendered)
134
+ end
135
+
136
+ test "outputs reference if uniuque_reference is present" do
137
+ rendered = render_govspeak(
138
+ "[embed:attachments:3ed2]",
139
+ [build_attachment(content_id: "3ed2", unique_reference: "123")]
140
+ )
141
+ assert_match(%r{<span class="references">Ref: <span class="unique_reference">123</span></span>}, rendered)
142
+ end
143
+
144
+ test "outputs reference if command_paper_number is present" do
145
+ rendered = render_govspeak(
146
+ "[embed:attachments:3ed2]",
147
+ [build_attachment(content_id: "3ed2", command_paper_number: "11")]
148
+ )
149
+ assert_match(%r{<span class="references">Ref: <span class="command_paper_number">11</span></span>}, rendered)
150
+ end
151
+
152
+ test "outputs reference if hoc_paper_number is present" do
153
+ rendered = render_govspeak(
154
+ "[embed:attachments:3ed2]",
155
+ [build_attachment(content_id: "3ed2", hoc_paper_number: "15", parliamentary_session: "1")]
156
+ )
157
+ assert_match(%r{<span class="references">Ref: <span class="house_of_commons_paper_number">HC 15</span> <span class="parliamentary_session">1</span></span>}, rendered)
158
+ end
159
+
160
+ test "can have multiple references" do
161
+ rendered = render_govspeak(
162
+ "[embed:attachments:3ed2]",
163
+ [build_attachment(content_id: "3ed2", isbn: "123", unique_reference: "55")]
164
+ )
165
+ assert_match(%r{<span class="references">Ref: ISBN <span class="isbn">123</span>, <span class="unique_reference">55</span></span>}, rendered)
166
+ end
167
+
168
+ test "can show an unnumbered command paper" do
169
+ rendered = render_govspeak(
170
+ "[embed:attachments:3ed2]",
171
+ [build_attachment(content_id: "3ed2", unnumbered_command_paper?: true)]
172
+ )
173
+ assert_match(%r{<span class="unnumbered-paper">\s*Unnumbered command paper\s*</span>}, compress_html(rendered))
174
+ end
175
+
176
+ test "can show an unnumbered act paper" do
177
+ rendered = render_govspeak(
178
+ "[embed:attachments:3ed2]",
179
+ [build_attachment(content_id: "3ed2", unnumbered_hoc_paper?: true)]
180
+ )
181
+ assert_match(%r{<span class="unnumbered-paper">\s*Unnumbered act paper\s*</span>}, compress_html(rendered))
182
+ end
183
+
184
+ test "unnumbered command paper takes precedence to unnumbered act paper" do
185
+ rendered = render_govspeak(
186
+ "[embed:attachments:3ed2]",
187
+ [build_attachment(content_id: "3ed2", unnumbered_command_paper?: true, unnumbered_hoc_paper?: true)]
188
+ )
189
+ assert_match(%r{<span class="unnumbered-paper">\s*Unnumbered command paper\s*</span>}, compress_html(rendered))
190
+ end
191
+
192
+ test "shows a preview link for a previewable format" do
193
+ rendered = render_govspeak(
194
+ "[embed:attachments:3ed2]",
195
+ [build_attachment(content_id: "3ed2", file_extension: "csv", url: "http://a.b/c.csv")]
196
+ )
197
+ assert_match(%r{<span class="preview"><strong>\s*<a href="http://a.b/c.csv/preview">View online</a>\s*</strong></span>}, compress_html(rendered))
198
+ end
199
+
200
+ test "Shows a download link for a previewable format" do
201
+ rendered = render_govspeak(
202
+ "[embed:attachments:3ed2]",
203
+ [build_attachment(content_id: "3ed2", file_extension: "csv", url: "http://a.b/c.csv")]
204
+ )
205
+ assert_match(%r{<span class="download">\s*<a href="http://a.b/c.csv"><strong>Download CSV</strong></a>s*</span>}, compress_html(rendered))
206
+ end
207
+
208
+ test "Can show filesize for a download link for a previewable format" do
209
+ rendered = render_govspeak(
210
+ "[embed:attachments:3ed2]",
211
+ [build_attachment(content_id: "3ed2", file_extension: "csv", url: "http://a.b/c.csv", file_size: 2048)]
212
+ )
213
+ assert_match(%r{<a href="http://a.b/c.csv" title="2 KB">}, rendered)
214
+ end
215
+
216
+ test "for a HTML format it outputs HTML" do
217
+ rendered = render_govspeak(
218
+ "[embed:attachments:3ed2]",
219
+ [build_attachment(content_id: "3ed2", file_extension: "html")]
220
+ )
221
+ assert_match(%r{<span class="type">HTML</span>}, rendered)
222
+ end
223
+
224
+ test "for an external type it outputs the url" do
225
+ rendered = render_govspeak(
226
+ "[embed:attachments:3ed2]",
227
+ [build_attachment(content_id: "3ed2", url: "http://a.b/c.pdf", external?: true)]
228
+ )
229
+ assert_match(%r{<span class="url">http://a.b/c.pdf</span>}, rendered)
230
+ end
231
+
232
+ test "will show a file extension in a abbr element for non html" do
233
+ rendered = render_govspeak(
234
+ "[embed:attachments:1fe8]",
235
+ [build_attachment(content_id: "1fe8", file_extension: "pdf")]
236
+ )
237
+ assert_match(%r{<span class="type"><abbr title="Portable Document Format">PDF</abbr></span>}, rendered)
238
+ end
239
+
240
+ test "will show file size in a span" do
241
+ rendered = render_govspeak(
242
+ "[embed:attachments:1fe8]",
243
+ [build_attachment(content_id: "1fe8", file_size: 1024)]
244
+ )
245
+ assert_match(%r{<span class="file-size">1 KB</span>}, rendered)
246
+ end
247
+
248
+ test "will show number of pages" do
249
+ rendered = render_govspeak(
250
+ "[embed:attachments:1fe8]",
251
+ [build_attachment(content_id: "1fe8", number_of_pages: 1)]
252
+ )
253
+ assert_match(%r{<span class="page-length">1 page</span>}, rendered)
254
+ rendered = render_govspeak(
255
+ "[embed:attachments:1fe8]",
256
+ [build_attachment(content_id: "1fe8", number_of_pages: 2)]
257
+ )
258
+ assert_match(%r{<span class="page-length">2 pages</span>}, rendered)
259
+ end
260
+
261
+ test "can show multiple attributes separated by a comma" do
262
+ rendered = render_govspeak(
263
+ "[embed:attachments:1fe8]",
264
+ [build_attachment(content_id: "1fe8", file_extension: "pdf", file_size: 1024)]
265
+ )
266
+ pdf = %{<span class="type"><abbr title="Portable Document Format">PDF</abbr></span>}
267
+ file_size = %{<span class="file-size">1 KB</span>}
268
+ assert_match(%r{#{pdf}, #{file_size}}, rendered)
269
+ end
270
+
271
+ test "can show a link to order a copy of the attachment" do
272
+ rendered = render_govspeak(
273
+ "[embed:attachments:1fe8]",
274
+ [build_attachment(content_id: "1fe8", order_url: "http://a.b/c")]
275
+ )
276
+ assert_match(%r{<a href="http://a.b/c" class="order_url" title="Order a copy of the publication">Order a copy</a>}, rendered)
277
+ end
278
+
279
+ test "can not show a link to order a copy of the attachment" do
280
+ rendered = render_govspeak(
281
+ "[embed:attachments:1fe8]",
282
+ [build_attachment(content_id: "1fe8", order_url: "nil")]
283
+ )
284
+ refute_match(%r{<a href="http://a.b/c" class="order_url" title="Order a copy of the publication">Order a copy</a>}, rendered)
285
+ end
286
+
287
+ test "can show a price for ordering a copy of the attachment" do
288
+ rendered = render_govspeak(
289
+ "[embed:attachments:1fe8]",
290
+ [build_attachment(content_id: "1fe8", order_url: "http://a.b/c", price: 10)]
291
+ )
292
+ assert_match(%r{(<span class="price">£10.00</span>)}, rendered)
293
+ end
294
+
295
+ test "can show opendocument help" do
296
+ rendered = render_govspeak(
297
+ "[embed:attachments:1fe8]",
298
+ [build_attachment(content_id: "1fe8", opendocument?: true)]
299
+ )
300
+ assert_match(%r{<p class="opendocument-help">}, rendered)
301
+ end
302
+
303
+ test "can not show opendocument help" do
304
+ rendered = render_govspeak(
305
+ "[embed:attachments:1fe8]",
306
+ [build_attachment(content_id: "1fe8", opendocument?: false)]
307
+ )
308
+ refute_match(%r{<p class="opendocument-help">}, rendered)
309
+ end
310
+
311
+ test "can show an accessibility warning" do
312
+ rendered = render_govspeak(
313
+ "[embed:attachments:1fe8]",
314
+ [build_attachment(content_id: "1fe8", id: 10, accessible?: false)]
315
+ )
316
+ assert_match(%r{<div data-module="toggle" class="accessibility-warning" id="attachment-10-accessibility-help">}, rendered)
317
+ end
318
+
319
+ test "can not show an accessibility warning" do
320
+ rendered = render_govspeak(
321
+ "[embed:attachments:1fe8]",
322
+ [build_attachment(content_id: "1fe8", id: 10, accessible?: true)]
323
+ )
324
+ refute_match(%r{<div data-module="toggle" class="accessibility-warning" id="attachment-10-accessibility-help">}, rendered)
325
+ end
326
+
327
+ test "shows accessibility mailto on a single line" do
328
+ rendered = render_govspeak(
329
+ "[embed:attachments:1fe8]",
330
+ [build_attachment(content_id: "1fe8", id: 10, accessible?: false)]
331
+ )
332
+ assert_match(%r{<a href="mailto:govuk-feedback@digital.cabinet-office.gov.uk\?subject=[^\n]*&amp;body=[^\n]*">govuk-feedback@digital.cabinet-office.gov.uk</a>}, rendered)
333
+ end
334
+
335
+ Dir.glob("locales/*.yml") do |filename|
336
+ locale = File.basename(filename, ".yml")
337
+ test "can render in #{locale}" do
338
+ rendered = render_govspeak(
339
+ "[embed:attachments:3ed2]",
340
+ [build_attachment(content_id: "3ed2")],
341
+ locale: locale,
342
+ )
343
+ assert_match(/<section class="attachment embedded">/, rendered)
344
+ end
345
+ end
346
+
347
+ test "a full attachment rendering looks correct" do
348
+ attachment = {
349
+ id: 123,
350
+ content_id: "2b4d92f3-f8cd-4284-aaaa-25b3a640d26c",
351
+ title: "Attachment Title",
352
+ url: "http://example.com/test.pdf",
353
+ opendocument?: true,
354
+ order_url: "http://example.com/order",
355
+ price: 12.3,
356
+ isbn: "isbn-123",
357
+ unnumbered_command_paper?: true,
358
+ }
359
+ rendered = render_govspeak(
360
+ "[embed:attachments:2b4d92f3-f8cd-4284-aaaa-25b3a640d26c]",
361
+ [build_attachment(attachment)]
362
+ )
363
+ expected_html_output = %{
364
+ <section class="attachment embedded">
365
+ <div class="attachment-thumb">
366
+ <a href="http://example.com/test.pdf" aria-hidden="true" class="embedded"><img src="/images/pub-cover.png" alt="Pub cover"></a>
367
+ </div>
368
+ <div class="attachment-details">
369
+ <h2 class="title">
370
+ <a href="http://example.com/test.pdf" aria-describedby="attachment-123-accessibility-help">Attachment Title</a>
371
+ </h2>
372
+ <p class="metadata">
373
+ <span class="references">Ref: ISBN <span class="isbn">isbn-123</span></span>
374
+ <span class="unnumbered-paper">
375
+ Unnumbered command paper
376
+ </span>
377
+ </p>
378
+ <p>
379
+ <a href="http://example.com/order" class="order_url" title="Order a copy of the publication">Order a copy</a>(<span class="price">£12.30</span>)
380
+ </p>
381
+ <p class="opendocument-help">
382
+ This file is in an <a rel="external" href="https://en.wikipedia.org/wiki/OpenDocument_software">OpenDocument</a> format
383
+ </p>
384
+ <div data-module="toggle" class="accessibility-warning" id="attachment-123-accessibility-help">
385
+ <h2>This file may not be suitable for users of assistive technology.
386
+ <a class="toggler" href="#attachment-123-accessibility-request" data-controls="attachment-123-accessibility-request" data-expanded="false">Request an accessible format.</a>
387
+ </h2>
388
+ <p id="attachment-123-accessibility-request" class="js-hidden">
389
+ If you use assistive technology (eg a screen reader) and need a
390
+ version of this document in a more accessible format, please email <a href="mailto:govuk-feedback@digital.cabinet-office.gov.uk?subject=Request%20for%20%27Attachment%20Title%27%20in%20an%20alternative%20format&amp;body=Details%20of%20document%20required%3A%0A%0A%20%20Title%3A%20Attachment%20Title%0A%20%20ISBN%3A%20isbn-123%0A%0APlease%20tell%20us%3A%0A%0A%20%201.%20What%20makes%20this%20format%20unsuitable%20for%20you%3F%0A%20%202.%20What%20format%20you%20would%20prefer%3F%0A">govuk-feedback@digital.cabinet-office.gov.uk</a>.
391
+ Please tell us what format you need. It will help us if you say what assistive technology you use.
392
+ </p>
393
+ </div>
394
+ </div>
395
+ </section>
396
+ }
397
+ assert_equal(compress_html(expected_html_output), compress_html(rendered))
398
+ end
399
+
400
+ test "attachment that isn't provided" do
401
+ govspeak = "[embed:attachments:906ac8b7-850d-45c6-98e0-9525c680f891]"
402
+ rendered = Govspeak::Document.new(govspeak).to_html
403
+ assert_equal("\n", rendered)
404
+ end
405
+
406
+ test "attachment where filename is provided, rather than a content_id" do
407
+ govspeak = "[embed:attachments:/path/to/file%20name.pdf]"
408
+ rendered = Govspeak::Document.new(govspeak).to_html
409
+ assert_equal("\n", rendered)
410
+ end
411
+ end