diff --git a/app/services/embedded_content_finder_service.rb b/app/services/embedded_content_finder_service.rb index 6249c89dd..57f397597 100644 --- a/app/services/embedded_content_finder_service.rb +++ b/app/services/embedded_content_finder_service.rb @@ -7,7 +7,7 @@ class EmbeddedContentFinderService def fetch_linked_content_ids(body, locale) content_references = if body.is_a?(Array) - body.map { |hash| find_content_references(hash[:content]) }.flatten + body.map { |hash| find_content_references(hash["content"]) }.flatten else find_content_references(body) end 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 39ee3cfef..04c96c8a8 100644 --- a/spec/integration/put_content/content_with_embedded_content_spec.rb +++ b/spec/integration/put_content/content_with_embedded_content_spec.rb @@ -1,7 +1,7 @@ RSpec.describe "PUT /v2/content when embedded content is provided" do include_context "PutContent call" - context "with embedded content" do + context "with embedded content as a string" do let(:first_contact) { create(:edition, state: "published", content_store: "live", document_type: "contact") } let(:second_contact) { create(:edition, state: "published", content_store: "live", document_type: "contact") } let(:document) { create(:document, content_id:) } @@ -20,6 +20,25 @@ end end + context "with embedded content as an array" do + let(:first_contact) { create(:edition, state: "published", content_store: "live", document_type: "contact") } + let(:second_contact) { create(:edition, state: "published", content_store: "live", document_type: "contact") } + let(:document) { create(:document, content_id:) } + + before do + payload.merge!(document_type: "person", schema_name: "person", details: { body: [{ content_type: "text/govspeak", content: "{{embed:contact:#{first_contact.document.content_id}}} {{embed:contact:#{second_contact.document.content_id}}}" }] }) + end + + it "should create links" do + expect { + put "/v2/content/#{content_id}", params: payload.to_json + }.to change(Link, :count).by(2) + + expect(Link.find_by(target_content_id: first_contact.content_id)).not_to be_nil + expect(Link.find_by(target_content_id: second_contact.content_id)).not_to be_nil + end + end + context "without embedded content and embed links already existing on a draft edition" do let(:contact) { create(:edition, state: "published", content_store: "live", document_type: "contact") } let(:document) { create(:document, content_id:) } diff --git a/spec/services/embedded_content_finder_service_spec.rb b/spec/services/embedded_content_finder_service_spec.rb index 5c7dad095..f4ea27ffc 100644 --- a/spec/services/embedded_content_finder_service_spec.rb +++ b/spec/services/embedded_content_finder_service_spec.rb @@ -39,7 +39,7 @@ end it "finds contact references when body is an array of hashes" do - body = [{ content: "{{embed:contact:#{contacts[0].content_id}}} {{embed:contact:#{contacts[1].content_id}}}" }] + body = [{ "content" => "{{embed:contact:#{contacts[0].content_id}}} {{embed:contact:#{contacts[1].content_id}}}" }] links = EmbeddedContentFinderService.new.fetch_linked_content_ids(body, Edition::DEFAULT_LOCALE)