forked from alphagov/whitehall
/
policies_controller_test.rb
345 lines (262 loc) · 13.4 KB
/
policies_controller_test.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
require "test_helper"
class PoliciesControllerTest < ActionController::TestCase
include PolicyViewAssertions
should_be_a_public_facing_controller
should_render_a_list_of :policies
should_show_the_countries_associated_with :policy
test "should show inapplicable nations" do
published_policy = create(:published_policy)
northern_ireland_inapplicability = published_policy.nation_inapplicabilities.create!(nation: Nation.northern_ireland, alternative_url: "http://northern-ireland.com/")
scotland_inapplicability = published_policy.nation_inapplicabilities.create!(nation: Nation.scotland)
get :show, id: published_policy.document_identity
assert_select inapplicable_nations_selector do
assert_select "p", "This policy does not apply to Northern Ireland and Scotland."
assert_select_object northern_ireland_inapplicability do
assert_select "a[href='http://northern-ireland.com/']"
end
refute_select_object scotland_inapplicability
end
end
test "should not explicitly say that policy applies to the whole of the UK" do
published_policy = create(:published_policy)
get :show, id: published_policy.document_identity
refute_select inapplicable_nations_selector
end
test "show displays recently changed documents relating to the policy" do
policy = create(:published_policy)
publication = create(:published_publication, related_policies: [policy])
consultation = create(:published_consultation, related_policies: [policy])
news_article = create(:published_news_article, related_policies: [policy])
speech = create(:published_speech, related_policies: [policy])
get :show, id: policy.document_identity
assert_select "#recently-changed" do
assert_select_object publication
assert_select_object consultation
assert_select_object news_article
assert_select_object speech
end
end
test "show displays metadata about the recently changed documents" do
published_at = Time.zone.now
policy = create(:published_policy)
speech = create(:published_speech, published_at: published_at, related_policies: [policy])
get :show, id: policy.document_identity
assert_select "#recently-changed" do
assert_select_object speech do
assert_select ".metadata .document_type", text: "Speech"
assert_select ".metadata .published_at[title='#{published_at.iso8601}']"
end
end
end
test "show orders recently changed documents relating to the policy most recent first" do
policy = create(:published_policy)
publication = create(:published_publication, published_at: 4.weeks.ago, related_policies: [policy])
consultation = create(:published_consultation, published_at: 1.weeks.ago, related_policies: [policy])
news_article = create(:published_news_article, published_at: 3.weeks.ago, related_policies: [policy])
speech = create(:published_speech, published_at: 2.weeks.ago, related_policies: [policy])
get :show, id: policy.document_identity
assert_equal [consultation, speech, news_article, publication], assigns[:recently_changed_documents]
end
test "show displays related published publications" do
published_policy = create(:published_policy)
related_publication = create(:published_publication, title: "Voting Patterns", related_policies: [published_policy])
get :show, id: published_policy.document_identity
assert_select related_publications_selector do
assert_select_object related_publication
end
end
test "show excludes related unpublished publications" do
published_policy = create(:published_policy)
related_publication = create(:draft_publication, title: "Voting Patterns", related_policies: [published_policy])
get :show, id: published_policy.document_identity
refute_select related_publications_selector
end
test "show displays related published consultations" do
published_policy = create(:published_policy)
related_consultation = create(:published_consultation, title: "Consultation on Voting Patterns",
related_policies: [published_policy])
get :show, id: published_policy.document_identity
assert_select related_consultations_selector do
assert_select_object related_consultation
end
end
test "show excludes related unpublished consultations" do
published_policy = create(:published_policy)
related_consultation = create(:draft_consultation, title: "Consultation on Voting Patterns",
related_policies: [published_policy])
get :show, id: published_policy.document_identity
refute_select related_consultations_selector
end
test "show displays related news articles" do
published_policy = create(:published_policy)
related_news_article = create(:published_news_article, title: "News about Voting Patterns",
related_policies: [published_policy])
get :show, id: published_policy.document_identity
assert_select related_news_articles_selector do
assert_select_object related_news_article
end
end
test "show excludes related unpublished news articles" do
published_policy = create(:published_policy)
related_news_article = create(:draft_news_article, title: "News about Voting Patterns",
related_policies: [published_policy])
get :show, id: published_policy.document_identity
refute_select related_news_articles_selector
end
test "show lists supporting pages when there are some" do
published_document = create(:published_policy)
first_supporting_page = create(:supporting_page, document: published_document)
second_supporting_page = create(:supporting_page, document: published_document)
get :show, id: published_document.document_identity
assert_select ".contextual_info nav.supporting_pages" do
assert_select "a[href='#{policy_supporting_page_path(published_document.document_identity, first_supporting_page)}']", text: first_supporting_page.title
assert_select "a[href='#{policy_supporting_page_path(published_document.document_identity, second_supporting_page)}']", text: second_supporting_page.title
end
end
test "doesn't show supporting pages list when empty" do
published_document = create(:published_policy)
get :show, id: published_document.document_identity
refute_select supporting_pages_selector
end
test "should render the content using govspeak markup" do
published_policy = create(:published_policy, body: "body-in-govspeak")
Govspeak::Document.stubs(:to_html).with("body-in-govspeak").returns("body-in-html")
get :show, id: published_policy.document_identity
assert_select ".body", text: "body-in-html"
end
test "should render 404 if the document doesn't have a published document" do
document_identity = create(:document_identity)
get :show, id: document_identity
assert_response :not_found
end
test "should display the published document" do
published_document = create(:published_policy)
draft = published_document.create_draft(create(:user))
document_identity = draft.document_identity
get :show, id: document_identity
assert_response :success
assert_equal published_document, assigns[:document]
end
test "should link to policy areas from within the metadata navigation" do
first_policy_area = create(:policy_area)
second_policy_area = create(:policy_area)
document = create(:published_policy, policy_areas: [first_policy_area, second_policy_area])
get :show, id: document.document_identity
assert_select "#{metadata_nav_selector} a.policy_area", text: first_policy_area.name
assert_select "#{metadata_nav_selector} a.policy_area", text: second_policy_area.name
end
test "should link to organisations from within the metadata navigation" do
first_org = create(:organisation)
second_org = create(:organisation)
document = create(:published_policy, organisations: [first_org, second_org])
get :show, id: document.document_identity
assert_select "#{metadata_nav_selector} a.organisation", text: first_org.name
assert_select "#{metadata_nav_selector} a.organisation", text: second_org.name
end
test "should link to ministers from within the metadata navigation" do
role = create(:ministerial_role)
appointment = create(:role_appointment, person: create(:person, forename: "minister-name"), role: role)
document = create(:published_policy, ministerial_roles: [appointment.role])
get :show, id: document.document_identity
assert_select "#{metadata_nav_selector} a.minister", text: "minister-name"
end
test "shows link to policy overview" do
policy = create(:published_policy)
get :show, id: policy.document_identity
assert_select "a[href='#{policy_path(policy.document_identity)}#policy_view']", text: policy.title
end
test "shows link to each policy section in the markdown" do
policy = create(:published_policy, body: %{
## First Section
Some content
## Another Bit
More content
## Final Part
That's all
})
get :show, id: policy.document_identity
assert_select_policy_section_link policy, 'First Section', 'first-section'
assert_select_policy_section_link policy, 'Another Bit', 'another-bit'
assert_select_policy_section_link policy, 'Final Part', 'final-part'
end
test "show links to related news articles on policy if any" do
policy = create(:published_policy)
related_news_article = create(:published_news_article, title: "News about Voting Patterns",
related_policies: [policy])
get :show, id: policy.document_identity
assert_select_policy_section_link policy, 'Related news', 'related-news-articles'
end
test "show doesn't link to related news articles on policy if none exist" do
policy = create(:published_policy)
get :show, id: policy.document_identity
refute_select_policy_section_list
end
test "show links to related speeches on policy if any" do
policy = create(:published_policy)
related_speech = create(:published_speech, title: "Speech about Voting Patterns",
related_policies: [policy])
get :show, id: policy.document_identity
assert_select_policy_section_link policy, 'Related speeches', 'related-speeches'
end
test "show doesn't link to related speeches on policy if none exist" do
policy = create(:published_policy)
get :show, id: policy.document_identity
refute_select_policy_section_list
end
test "show links to related consultations on policy if any" do
policy = create(:published_policy)
related_consultation = create(:published_consultation, title: "Consultation about Voting Patterns",
related_policies: [policy])
get :show, id: policy.document_identity
assert_select_policy_section_link policy, 'Related consultations', 'related-consultations'
end
test "show doesn't link to related consultations on policy if none exist" do
policy = create(:published_policy)
get :show, id: policy.document_identity
refute_select_policy_section_list
end
test "show links to related publications on policy if any" do
policy = create(:published_policy)
related_publication = create(:published_publication, title: "Consultation about Voting Patterns",
related_policies: [policy])
get :show, id: policy.document_identity
assert_select_policy_section_link policy, 'Related publications', 'related-publications'
end
test "show doesn't link to related publications on policy if none exist" do
policy = create(:published_policy)
get :show, id: policy.document_identity
refute_select_policy_section_list
end
test "show displays change history in reverse chronological order" do
editions = []
editions << create(:published_policy, change_note: "Third go.", published_at: 1.month.ago)
document_identity = editions.first.document_identity
editions << create(:archived_policy, change_note: "Second attempt.", document_identity: document_identity, published_at: 2.months.ago)
editions << create(:archived_policy, change_note: "First effort.", document_identity: document_identity, published_at: 3.months.ago)
get :show, id: document_identity
assert_select ".policy .change_notes li" do |list_items|
list_items.each_with_index do |list_item, index|
assert_select list_item, ".published_at[title='#{editions[index].published_at.iso8601}']"
assert_select list_item, "p", text: editions[index].change_note
end
end
end
test "show displays default change note for first edition" do
first_edition = create(:published_policy, change_note: nil, published_at: 1.month.ago)
get :show, id: first_edition.document_identity
assert_select ".policy .change_notes li" do
assert_select ".published_at[title='#{first_edition.published_at.iso8601}']"
assert_select "p", text: "First published."
end
end
test "show does not display blank change notes in change history" do
second_edition = create(:published_policy, change_note: nil, published_at: 1.months.ago)
document_identity = second_edition.document_identity
first_edition = create(:archived_policy, change_note: "First effort.", document_identity: document_identity, published_at: 2.months.ago)
get :show, id: document_identity
assert_select ".policy .change_notes li" do
refute_select ".published_at[title='#{second_edition.published_at.iso8601}']"
refute_select "p", text: ""
end
end
end