diff --git a/app/presenters/content_embed_presenter.rb b/app/presenters/content_embed_presenter.rb
index 7be48c9d3..1e20c3543 100644
--- a/app/presenters/content_embed_presenter.rb
+++ b/app/presenters/content_embed_presenter.rb
@@ -52,7 +52,7 @@ def render_embedded_editions(content)
embedded_content_references = EmbeddedContentFinderService.new.find_content_references(content)
return content if embedded_content_references.empty?
- embedded_content_references.each do |content_reference|
+ embedded_content_references.uniq.each do |content_reference|
embed_code = content_reference.embed_code
embedded_edition = embedded_editions[content_reference.content_id]
content = content.gsub(
diff --git a/spec/presenters/content_embed_presenter_spec.rb b/spec/presenters/content_embed_presenter_spec.rb
index 09cf66ce3..3bef84d3c 100644
--- a/spec/presenters/content_embed_presenter_spec.rb
+++ b/spec/presenters/content_embed_presenter_spec.rb
@@ -30,7 +30,13 @@
end
describe "#render_embedded_content" do
- let(:expected_value) { "VALUE" }
+ let(:expected_value) do
+ "VALUE".squish
+ end
let(:stub_block) { double(ContentBlockTools::ContentBlock, render: expected_value) }
before do
@@ -44,12 +50,70 @@
end
context "when body is a string" do
- let(:details) { { body: "some string with a reference: {{embed:contact:#{embedded_content_id}}}" } }
+ context "when there is only one embed" do
+ let(:details) { { body: "some string with a reference: {{embed:contact:#{embedded_content_id}}}" } }
+ it "returns embedded content references with values from their editions" do
+ expect(described_class.new(edition).render_embedded_content(details)).to eq({
+ body: "some string with a reference: #{expected_value}",
+ })
+ end
+ end
- it "returns embedded content references with values from their editions" do
- expect(described_class.new(edition).render_embedded_content(details)).to eq({
- body: "some string with a reference: #{expected_value}",
- })
+ context "when there are multiple embeds" do
+ context "when there are multiple embeds to the same block" do
+ let(:details) { { body: "some string with a reference: #{embed_code} and another: #{embed_code}" } }
+ it "returns embedded content" do
+ expect(described_class.new(edition).render_embedded_content(details)).to eq({
+ body: "some string with a reference: #{expected_value} and another: #{expected_value}",
+ })
+ end
+ end
+
+ context "when there are multiple embeds for different blocks" do
+ let(:embedded_content_id_2) { SecureRandom.uuid }
+ let!(:embedded_edition_2) do
+ embedded_document = create(:document, content_id: embedded_content_id_2)
+ create(
+ :edition,
+ document: embedded_document,
+ state: "published",
+ content_store: "live",
+ document_type: "content_block_pension",
+ title: "VALUE2",
+ )
+ end
+ let(:embed_code_2) { "{{embed:content_block_pension:#{embedded_content_id_2}}}" }
+ let(:links_hash) do
+ {
+ embed: [embedded_content_id, embedded_content_id_2],
+ }
+ end
+ let(:expected_value_2) do
+ "VALUE2".squish
+ end
+ let(:stub_block_2) { double(ContentBlockTools::ContentBlock, render: expected_value_2) }
+
+ before do
+ expect(ContentBlockTools::ContentBlock).to receive(:new).with(
+ document_type: embedded_edition_2.document_type,
+ content_id: embedded_edition_2.document.content_id,
+ title: embedded_edition_2.title,
+ details: embedded_edition_2.details,
+ embed_code: embed_code_2,
+ ).and_return(stub_block_2)
+ end
+ let(:details) { { body: "some string with a reference: #{embed_code} and another: #{embed_code_2}" } }
+
+ it "returns embedded content" do
+ expect(described_class.new(edition).render_embedded_content(details)).to eq({
+ body: "some string with a reference: #{expected_value} and another: #{expected_value_2}",
+ })
+ end
+ end
end
end