Skip to content

Commit

Permalink
Merge pull request #2833 from alphagov/fix-embedded-bug
Browse files Browse the repository at this point in the history
Fix issue with embedded content on body being an array
  • Loading branch information
brucebolt authored Aug 7, 2024
2 parents 07b3b64 + 542dae7 commit 969aa07
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 3 deletions.
2 changes: 1 addition & 1 deletion app/services/embedded_content_finder_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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:) }
Expand All @@ -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:) }
Expand Down
2 changes: 1 addition & 1 deletion spec/services/embedded_content_finder_service_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit 969aa07

Please sign in to comment.