Skip to content

Commit 9b38ea8

Browse files
authored
Merge pull request #3129 from alphagov/content-modelling/support-multiple-references-to-the-same-content-block
Support multiple refs to the same content block
2 parents 2f21307 + 857ee26 commit 9b38ea8

File tree

3 files changed

+52
-4
lines changed

3 files changed

+52
-4
lines changed

app/services/embedded_content_finder_service.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ def find_content_references(value)
2525

2626
def check_all_references_exist(content_references, locale)
2727
found_editions = live_editions(content_references, locale)
28-
if found_editions.count != content_references.count
29-
not_found_content_ids = content_references.map(&:content_id) - found_editions.map(&:content_id)
28+
not_found_content_ids = content_references.map(&:content_id) - found_editions.map(&:content_id)
29+
30+
if not_found_content_ids.any?
3031
raise CommandError.new(
3132
code: 422,
3233
message: "Could not find any live editions in locale #{locale} for: #{not_found_content_ids.join(', ')}, ",

spec/integration/put_content/content_with_embedded_content_spec.rb

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
include_context "PutContent call"
33

44
context "with embedded content as a string" do
5-
let(:first_contact) { create(:edition, state: "published", content_store: "live", document_type: "contact") }
5+
let(:first_contact) { create(:edition, state: "published", content_store: "live", document_type: "contact", details: { email: "[email protected]", phone: "123456" }) }
66
let(:second_contact) { create(:edition, state: "published", content_store: "live", document_type: "contact") }
77
let(:document) { create(:document, content_id:) }
88
let(:first_embed_code) { "{{embed:contact:#{first_contact.document.content_id}}}" }
@@ -26,6 +26,45 @@
2626

2727
expect_content_store_to_have_received_details_including({ "body" => "#{presented_details_for(first_contact, first_embed_code)} #{presented_details_for(second_contact, second_embed_code)}" })
2828
end
29+
30+
context "when fields are referenced" do
31+
let(:first_embed_code) { "{{embed:contact:#{first_contact.document.content_id}/email}}" }
32+
let(:second_embed_code) { "{{embed:contact:#{first_contact.document.content_id}/phone}}" }
33+
34+
let(:body) do
35+
"
36+
Hello, here is some an email:
37+
38+
#{first_embed_code}
39+
40+
And here is a phone number:
41+
42+
#{second_embed_code}
43+
"
44+
end
45+
46+
let(:expected_body) do
47+
"
48+
Hello, here is some an email:
49+
50+
#{presented_details_for(first_contact, first_embed_code)}
51+
52+
And here is a phone number:
53+
54+
#{presented_details_for(first_contact, second_embed_code)}
55+
"
56+
end
57+
58+
before do
59+
payload.merge!(details: { body: })
60+
end
61+
62+
it "should send transformed content to the content store" do
63+
put "/v2/content/#{content_id}", params: payload.to_json
64+
65+
expect_content_store_to_have_received_details_including({ "body" => expected_body })
66+
end
67+
end
2968
end
3069

3170
context "when embedded content is in a details field other than body" do

spec/services/embedded_content_finder_service_spec.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
state: "published",
77
document_type:,
88
content_store: "live",
9-
details: { title: "Some Title" }),
9+
details: { title: "Some Title", another: "thing" }),
1010
create(:edition,
1111
state: "published",
1212
document_type:,
@@ -40,6 +40,14 @@
4040
expect(links).to eq([editions[0].content_id, editions[0].content_id, editions[1].content_id])
4141
end
4242

43+
it "returns duplicates when there are field references in the field" do
44+
details = { field_name => "{{embed:#{document_type}:#{editions[0].content_id}/title}} {{embed:#{document_type}:#{editions[0].content_id}/another}}" }
45+
46+
links = EmbeddedContentFinderService.new.fetch_linked_content_ids(details, Edition::DEFAULT_LOCALE)
47+
48+
expect(links).to eq([editions[0].content_id, editions[0].content_id])
49+
end
50+
4351
it "finds content references when #{field_name} is an array of hashes" do
4452
details = { field_name => [{ "content" => "{{embed:#{document_type}:#{editions[0].content_id}}} {{embed:#{document_type}:#{editions[1].content_id}}}" }] }
4553

0 commit comments

Comments
 (0)