-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathexpansion_rules_spec.rb
511 lines (450 loc) · 20.9 KB
/
expansion_rules_spec.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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
RSpec.describe ExpansionRules do
subject(:rules) { described_class }
describe ".reverse_link_type" do
specify { expect(rules.reverse_link_type(:parent)).to be(:children) }
specify { expect(rules.reverse_link_type(:children)).to be_nil }
specify { expect(rules.reverse_link_type(:made_up)).to be_nil }
end
describe ".reverse_to_direct_link_type" do
specify { expect(rules.reverse_to_direct_link_type(:children)).to match_array(%i[parent]) }
specify { expect(rules.reverse_to_direct_link_type(:role_appointments)).to match_array(%i[role person]) }
specify { expect(rules.reverse_to_direct_link_type(:parent)).to be_empty }
specify { expect(rules.reverse_to_direct_link_type(:made_up)).to be_empty }
end
describe ".is_reverse_link_type?" do
specify { expect(rules.is_reverse_link_type?(:children)).to be(true) }
specify { expect(rules.is_reverse_link_type?(:parent)).to be(false) }
specify { expect(rules.is_reverse_link_type?(:made_up)).to be(false) }
end
describe ".reverse_to_direct_link_types" do
specify do
expect(rules.reverse_to_direct_link_types(%i[children documents]))
.to match([:parent])
end
specify { expect(rules.reverse_to_direct_link_types([:made_up])).to match([]) }
end
describe ".reverse_link_types_hash" do
let(:content_ids) { %w[a b] }
specify do
expect(rules.reverse_link_types_hash(parent: content_ids))
.to match(children: content_ids)
expect(rules.reverse_link_types_hash(policies: content_ids)).to match({})
end
end
describe ".expansion_fields" do
let(:default_fields) { rules::DEFAULT_FIELDS }
let(:contact_fields) { default_fields + [%i[details description], %i[details title], %i[details contact_form_links], %i[details post_addresses], %i[details email_addresses], %i[details phone_numbers]] }
let(:content_block_email_fields) { default_fields + [%i[details email_address]] }
let(:content_block_postal_fields) { default_fields + [%i[details line_1], %i[details town_or_city], %i[details postcode]] }
let(:content_block_pension_fields) { default_fields + [%i[details description], %i[details rates]] }
let(:organisation_fields) { default_fields - [:public_updated_at] + [%i[details acronym], %i[details logo], %i[details brand], %i[details default_news_image], %i[details organisation_govuk_status]] }
let(:taxon_fields) { default_fields + %i[description details phase] }
let(:default_fields_and_description) { default_fields + %i[description] }
let(:need_fields) { default_fields + [%i[details role], %i[details goal], %i[details benefit], %i[details met_when], %i[details justifications]] }
let(:fatality_notice_fields) { default_fields + [%i[details roll_call_introduction], %i[details casualties]] }
let(:finder_fields) { default_fields + [%i[details facets], %i[details show_metadata_block]] }
let(:historic_appointment_fields) { default_fields + [%i[details political_party], %i[details dates_in_office]] }
let(:ministerial_role_fields) { role_fields + [%i[details seniority], %i[details whip_organisation]] }
let(:person_fields) { default_fields + [%i[details body], %i[details image]] }
let(:person_with_image_fields) { default_fields + [%i[details image], %i[details privy_counsellor]] }
let(:role_fields) { default_fields + [%i[details body], %i[details role_payment_type]] }
let(:role_appointment_fields) { default_fields + [%i[details started_on], %i[details ended_on], %i[details current], %i[details person_appointment_order]] }
let(:service_manual_topic_fields) { default_fields + %i[description] }
let(:step_by_step_fields) { default_fields + [%i[details step_by_step_nav title], %i[details step_by_step_nav steps]] }
let(:step_by_step_auth_bypass_fields) { step_by_step_fields + %i[auth_bypass_ids] }
let(:travel_advice_fields) { default_fields + [%i[details country], %i[details change_description]] }
let(:world_location_fields) { %i[content_id title schema_name locale analytics_identifier] }
let(:worldwide_office_fields) { default_fields + [%i[details access_and_opening_times]] }
let(:worldwide_organisation_fields) { default_fields_and_description + [%i[details logo]] + [%i[details world_location_names]] + [%i[details default_news_image]] }
let(:link_collection_fields) { default_fields + [%i[details link_items]] }
specify { expect(rules.expansion_fields(:redirect)).to eq([]) }
specify { expect(rules.expansion_fields(:gone)).to eq([]) }
specify { expect(rules.expansion_fields(:parent)).to eq(default_fields) }
specify { expect(rules.expansion_fields(:contact)).to eq(contact_fields) }
specify { expect(rules.expansion_fields(:content_block_email_address)).to eq(content_block_email_fields) }
specify { expect(rules.expansion_fields(:content_block_postal_address)).to eq(content_block_postal_fields) }
specify { expect(rules.expansion_fields(:content_block_pension)).to eq(content_block_pension_fields) }
specify { expect(rules.expansion_fields(:historic_appointment)).to eq(historic_appointment_fields) }
specify { expect(rules.expansion_fields(:fatality_notice)).to eq(fatality_notice_fields) }
specify { expect(rules.expansion_fields(:mainstream_browse_page)).to eq(default_fields_and_description) }
specify { expect(rules.expansion_fields(:need)).to eq(need_fields) }
specify { expect(rules.expansion_fields(:organisation)).to eq(organisation_fields) }
specify { expect(rules.expansion_fields(:person, link_type: :people)).to eq(default_fields) }
specify { expect(rules.expansion_fields(:person, link_type: :person)).to eq(person_fields) }
specify { expect(rules.expansion_fields(:role_appointment)).to eq(role_appointment_fields) }
specify { expect(rules.expansion_fields(:service_manual_topic)).to eq(service_manual_topic_fields) }
specify { expect(rules.expansion_fields(:topical_event)).to eq(default_fields) }
specify { expect(rules.expansion_fields(:person, link_type: :current_prime_minister)).to eq(person_with_image_fields) }
specify { expect(rules.expansion_fields(:person, link_type: :ordered_also_attends_cabinet)).to eq(person_with_image_fields) }
specify { expect(rules.expansion_fields(:person, link_type: :ordered_assistant_whips)).to eq(person_with_image_fields) }
specify { expect(rules.expansion_fields(:person, link_type: :ordered_baronesses_and_lords_in_waiting_whips)).to eq(person_with_image_fields) }
specify { expect(rules.expansion_fields(:person, link_type: :ordered_board_members)).to eq(person_with_image_fields) }
specify { expect(rules.expansion_fields(:person, link_type: :ordered_cabinet_ministers)).to eq(person_with_image_fields) }
specify { expect(rules.expansion_fields(:person, link_type: :ordered_chief_professional_officers)).to eq(person_with_image_fields) }
specify { expect(rules.expansion_fields(:person, link_type: :ordered_house_lords_whips)).to eq(person_with_image_fields) }
specify { expect(rules.expansion_fields(:person, link_type: :ordered_house_of_commons_whips)).to eq(person_with_image_fields) }
specify { expect(rules.expansion_fields(:person, link_type: :ordered_junior_lords_of_the_treasury_whips)).to eq(person_with_image_fields) }
specify { expect(rules.expansion_fields(:person, link_type: :ordered_military_personnel)).to eq(person_with_image_fields) }
specify { expect(rules.expansion_fields(:person, link_type: :ordered_ministers)).to eq(person_with_image_fields) }
specify { expect(rules.expansion_fields(:person, link_type: :ordered_special_representatives)).to eq(person_with_image_fields) }
specify { expect(rules.expansion_fields(:person, link_type: :ordered_traffic_commissioners)).to eq(person_with_image_fields) }
specify { expect(rules.expansion_fields(:person, link_type: :primary_role_person)).to eq(person_with_image_fields) }
specify { expect(rules.expansion_fields(:ministerial_role)).to eq(ministerial_role_fields) }
specify { expect(rules.expansion_fields(:ambassador_role)).to eq(role_fields) }
specify { expect(rules.expansion_fields(:board_member_role)).to eq(role_fields) }
specify { expect(rules.expansion_fields(:chief_professional_officer_role)).to eq(role_fields) }
specify { expect(rules.expansion_fields(:chief_scientific_advisor_role)).to eq(role_fields) }
specify { expect(rules.expansion_fields(:chief_scientific_officer_role)).to eq(role_fields) }
specify { expect(rules.expansion_fields(:deputy_head_of_mission_role)).to eq(role_fields) }
specify { expect(rules.expansion_fields(:governor_role)).to eq(role_fields) }
specify { expect(rules.expansion_fields(:high_commissioner_role)).to eq(role_fields) }
specify { expect(rules.expansion_fields(:military_role)).to eq(role_fields) }
specify { expect(rules.expansion_fields(:special_representative_role)).to eq(role_fields) }
specify { expect(rules.expansion_fields(:traffic_commissioner_role)).to eq(role_fields) }
specify { expect(rules.expansion_fields(:worldwide_office_staff_role)).to eq(role_fields) }
specify { expect(rules.expansion_fields(:step_by_step_nav, link_type: :part_of_step_navs)).to eq(step_by_step_auth_bypass_fields) }
specify { expect(rules.expansion_fields(:step_by_step_nav, link_type: :part_of_step_navs, draft: false)).to eq(step_by_step_fields) }
specify { expect(rules.expansion_fields(:step_by_step_nav, link_type: :related_to_step_navs)).to eq(step_by_step_auth_bypass_fields) }
specify { expect(rules.expansion_fields(:step_by_step_nav, link_type: :related_to_step_navs, draft: false)).to eq(step_by_step_fields) }
specify { expect(rules.expansion_fields(:step_by_step_nav, link_type: :unspecified_link)).to eq(step_by_step_fields) }
specify { expect(rules.expansion_fields(:step_by_step_nav)).to eq(step_by_step_auth_bypass_fields) }
specify { expect(rules.expansion_fields(:taxon)).to eq(taxon_fields) }
specify { expect(rules.expansion_fields(:travel_advice)).to eq(travel_advice_fields) }
specify { expect(rules.expansion_fields(:world_location)).to eq(world_location_fields) }
specify { expect(rules.expansion_fields(:worldwide_organisation)).to eq(worldwide_organisation_fields) }
specify { expect(rules.expansion_fields(:worldwide_office)).to eq(worldwide_office_fields) }
specify { expect(rules.expansion_fields(:finder, link_type: :finder)).to eq(finder_fields) }
specify { expect(rules.expansion_fields(:parent, link_type: :finder)).to eq(default_fields) }
specify { expect(rules.expansion_fields(:link_collection)).to eq(link_collection_fields) }
end
describe ".expansion_fields_for_document_type" do
before do
stub_const(
"ExpansionRules::CUSTOM_EXPANSION_FIELDS",
[
{ document_type: :news, fields: %i[news_a] },
{ document_type: :news,
link_type: :breaking_news,
fields: %i[news_a news_b] },
{ document_type: :news,
link_type: :local_news,
fields: %i[news_c news_d] },
],
)
end
it "returns default fields when a document type doesn't have any custom entries" do
expect(rules.expansion_fields_for_document_type(:other_type))
.to match_array(ExpansionRules::DEFAULT_FIELDS)
end
it "can accept a string instead of a symbol" do
from_symbol = rules.expansion_fields_for_document_type(:news)
from_string = rules.expansion_fields_for_document_type("news")
expect(from_symbol).to eq(from_string)
end
it "collates all the fields used by links for the document type" do
expect(rules.expansion_fields_for_document_type(:news))
.to match_array(%i[news_a news_b news_c news_d])
end
context "when a custom fields entry only has examples with links" do
before do
stub_const(
"ExpansionRules::CUSTOM_EXPANSION_FIELDS",
[
{ document_type: :editorial,
link_type: :current_events,
fields: %i[editorial_a] },
{ document_type: :editorial,
link_type: :world_events,
fields: %i[editorial_b] },
],
)
end
it "includes default fields as expansion fields" do
expect(rules.expansion_fields_for_document_type(:editorial))
.to contain_exactly(:editorial_a, :editorial_b, *ExpansionRules::DEFAULT_FIELDS)
end
end
end
describe ".expansion_fields_for_linked_document_type" do
before do
stub_const(
"ExpansionRules::CUSTOM_EXPANSION_FIELDS",
[
{ document_type: :news, fields: %i[news_a] },
{ document_type: :news,
link_type: :breaking_news,
fields: %i[news_a news_b] },
{ document_type: :editorial,
link_type: :current_events,
fields: %i[editorial_a] },
],
)
end
it "can accept strings instead of symbols" do
from_symbols = rules.expansion_fields_for_linked_document_type(:news, :breaking_news)
from_strings = rules.expansion_fields_for_linked_document_type("news", "breaking_news")
expect(from_symbols).to eq(from_strings)
end
it "returns default fields when a document type doesn't have any custom entries" do
expect(rules.expansion_fields_for_linked_document_type(:unknown_type, :unknown_link))
.to match_array(ExpansionRules::DEFAULT_FIELDS)
end
it "returns default fields when a document type is only listed for specific links" do
expect(rules.expansion_fields_for_linked_document_type(:editorial, :unknown_link))
.to match_array(ExpansionRules::DEFAULT_FIELDS)
end
it "returns specified fields when document type and link type match" do
expect(rules.expansion_fields_for_linked_document_type(:news, :breaking_news))
.to match_array(%i[news_a news_b])
end
it "returns specified fields when link type is not matched but "\
"document_type is defined without a link type" do
expect(rules.expansion_fields_for_linked_document_type(:news, :unknown_type))
.to match_array(%i[news_a])
end
end
describe ".next_allowed_direct_link_types" do
let(:reverse_to_direct) { false }
subject do
described_class.next_allowed_direct_link_types(
next_allowed_link_types, reverse_to_direct:
)
end
context "when passed direct links only" do
let(:next_allowed_link_types) do
{
parent: %i[parent parent_taxons],
}
end
it "returns the links unchanged" do
is_expected.to match(next_allowed_link_types)
end
end
context "when passed reverse links only" do
let(:next_allowed_link_types) do
{
parent: [:children],
}
end
it "returns an empty hash" do
is_expected.to be_empty
end
end
context "when passed a mixture of direct and reverse links" do
let(:next_allowed_link_types) do
{
parent: %i[children parent],
}
end
it "returns the direct links" do
is_expected.to match(parent: [:parent])
end
end
context "when passed a reverse link with direct links" do
let(:next_allowed_link_types) do
{
children: [:parent],
}
end
it "returns the direct links" do
is_expected.to match(children: [:parent])
end
end
context "when reverse_to_direct is true and passed a reverse link with direct links" do
let(:reverse_to_direct) { true }
let(:next_allowed_link_types) do
{
children: [:parent],
}
end
it "reverses the link type" do
is_expected.to match(parent: [:parent])
end
end
context "when reverse_to_direct is true and passed a reverse link with multiple direct links" do
let(:reverse_to_direct) { true }
let(:next_allowed_link_types) do
{
role_appointments: [:other],
}
end
it "reverses the link type" do
is_expected.to match(
person: [:other],
role: [:other],
)
end
end
end
describe ".next_allowed_reverse_link_types" do
let(:reverse_to_direct) { false }
subject do
described_class.next_allowed_reverse_link_types(
next_allowed_link_types,
reverse_to_direct:,
)
end
context "when passed direct links only" do
let(:next_allowed_link_types) do
{
children: %i[parent parent_taxons],
}
end
it "returns an empty hash" do
is_expected.to be_empty
end
end
context "when passed reverse links only" do
let(:next_allowed_link_types) do
{
children: [:children],
}
end
it "returns the links unchanged" do
is_expected.to match(next_allowed_link_types)
end
end
context "when passed a mixture of direct and reverse links" do
let(:next_allowed_link_types) do
{
children: %i[children parent],
}
end
it "returns the reverse links" do
is_expected.to match(children: [:children])
end
end
context "when reverse_to_direct is true" do
let(:next_allowed_link_types) do
{
children: [:children],
}
end
let(:reverse_to_direct) { true }
it "changes the link types to be their direct counterpart" do
is_expected.to match(parent: [:parent])
end
end
context "when reverse_to_direct is true and passed a reverse link with multiple direct links" do
let(:reverse_to_direct) { true }
let(:next_allowed_link_types) do
{
other: [:role_appointments],
}
end
it "changes the link types to be their direct counterpart" do
is_expected.to match(other: %i[person role])
end
end
end
describe "REVERSE_LINKS" do
let(:reverse_links) { described_class.reverse_links.map(&:to_s) }
describe "are defined in necessary frontend schemas" do
schemas_of_type("frontend/schema").each do |path, schema|
links = schema["properties"]["links"]
next unless links
context "when the schema is #{path}" do
subject { links["properties"].keys }
it { is_expected.to include(*reverse_links) }
end
end
end
describe "are not defined in publisher schemas" do
schemas_of_type("publisher_v2/links").each do |path, schema|
links = schema["properties"]["links"]
next unless links
context "when the schema is #{path}" do
subject { links["properties"].keys }
it { is_expected.not_to include(*reverse_links) }
end
end
end
end
describe ".expand_fields" do
context "with a format that expands subfields of the details hash" do
let(:edition_hash) do
{
document_type: "travel_advice",
details: {
country: "fr",
other_field: "test",
},
}
end
it "expands into a new details hash" do
expect(described_class.expand_fields(edition_hash)).to eq(
document_type: "travel_advice",
details: {
country: "fr",
change_description: nil,
},
analytics_identifier: nil,
api_path: nil,
base_path: nil,
content_id: nil,
locale: nil,
public_updated_at: nil,
schema_name: nil,
title: nil,
withdrawn: nil,
)
end
end
context "with a format with a deeply nested details hash" do
let(:edition_hash) do
{
document_type: "content_block_pension",
details: {
description: "some description",
something: "else",
rates: {
"rate-1" => {
name: "Rate 1",
amount: "£123",
cadence: "Monthly",
description: "Some description",
},
"rate-2" => {
name: "Rate 2",
amount: "£12",
cadence: "Weekly",
description: "Some other description",
},
},
},
}
end
it "expands into a new details hash" do
expect(described_class.expand_fields(edition_hash)).to eq(
document_type: "content_block_pension",
details: {
description: "some description",
rates: {
"rate-1" => {
name: "Rate 1",
amount: "£123",
cadence: "Monthly",
description: "Some description",
},
"rate-2" => {
name: "Rate 2",
amount: "£12",
cadence: "Weekly",
description: "Some other description",
},
},
},
analytics_identifier: nil,
api_path: nil,
base_path: nil,
content_id: nil,
locale: nil,
public_updated_at: nil,
schema_name: nil,
title: nil,
withdrawn: nil,
)
end
end
end
end