Skip to content

Commit

Permalink
Merge pull request #3129 from alphagov/content-modelling/support-mult…
Browse files Browse the repository at this point in the history
…iple-references-to-the-same-content-block

Support multiple refs to the same content block
  • Loading branch information
pezholio authored Feb 6, 2025
2 parents 2f21307 + 857ee26 commit 9b38ea8
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
5 changes: 3 additions & 2 deletions app/services/embedded_content_finder_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ def find_content_references(value)

def check_all_references_exist(content_references, locale)
found_editions = live_editions(content_references, locale)
if found_editions.count != content_references.count
not_found_content_ids = content_references.map(&:content_id) - found_editions.map(&:content_id)
not_found_content_ids = content_references.map(&:content_id) - found_editions.map(&:content_id)

if not_found_content_ids.any?
raise CommandError.new(
code: 422,
message: "Could not find any live editions in locale #{locale} for: #{not_found_content_ids.join(', ')}, ",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
include_context "PutContent call"

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

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)}" })
end

context "when fields are referenced" do
let(:first_embed_code) { "{{embed:contact:#{first_contact.document.content_id}/email}}" }
let(:second_embed_code) { "{{embed:contact:#{first_contact.document.content_id}/phone}}" }

let(:body) do
"
Hello, here is some an email:
#{first_embed_code}
And here is a phone number:
#{second_embed_code}
"
end

let(:expected_body) do
"
Hello, here is some an email:
#{presented_details_for(first_contact, first_embed_code)}
And here is a phone number:
#{presented_details_for(first_contact, second_embed_code)}
"
end

before do
payload.merge!(details: { body: })
end

it "should send transformed content to the content store" do
put "/v2/content/#{content_id}", params: payload.to_json

expect_content_store_to_have_received_details_including({ "body" => expected_body })
end
end
end

context "when embedded content is in a details field other than body" do
Expand Down
10 changes: 9 additions & 1 deletion spec/services/embedded_content_finder_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
state: "published",
document_type:,
content_store: "live",
details: { title: "Some Title" }),
details: { title: "Some Title", another: "thing" }),
create(:edition,
state: "published",
document_type:,
Expand Down Expand Up @@ -40,6 +40,14 @@
expect(links).to eq([editions[0].content_id, editions[0].content_id, editions[1].content_id])
end

it "returns duplicates when there are field references in the field" do
details = { field_name => "{{embed:#{document_type}:#{editions[0].content_id}/title}} {{embed:#{document_type}:#{editions[0].content_id}/another}}" }

links = EmbeddedContentFinderService.new.fetch_linked_content_ids(details, Edition::DEFAULT_LOCALE)

expect(links).to eq([editions[0].content_id, editions[0].content_id])
end

it "finds content references when #{field_name} is an array of hashes" do
details = { field_name => [{ "content" => "{{embed:#{document_type}:#{editions[0].content_id}}} {{embed:#{document_type}:#{editions[1].content_id}}}" }] }

Expand Down

0 comments on commit 9b38ea8

Please sign in to comment.