diff --git a/app/services/embedded_content_finder_service.rb b/app/services/embedded_content_finder_service.rb index e9b3b6a96..c9b35414c 100644 --- a/app/services/embedded_content_finder_service.rb +++ b/app/services/embedded_content_finder_service.rb @@ -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(', ')}, ", diff --git a/spec/integration/put_content/content_with_embedded_content_spec.rb b/spec/integration/put_content/content_with_embedded_content_spec.rb index 92aab607a..bafd81d3c 100644 --- a/spec/integration/put_content/content_with_embedded_content_spec.rb +++ b/spec/integration/put_content/content_with_embedded_content_spec.rb @@ -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: "foo@example.com", 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}}}" } @@ -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 diff --git a/spec/services/embedded_content_finder_service_spec.rb b/spec/services/embedded_content_finder_service_spec.rb index 6bc66f2f1..440249689 100644 --- a/spec/services/embedded_content_finder_service_spec.rb +++ b/spec/services/embedded_content_finder_service_spec.rb @@ -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:, @@ -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}}}" }] }