Skip to content

Commit

Permalink
Merge pull request #2840 from alphagov/content-modelling/support-emai…
Browse files Browse the repository at this point in the history
…l-address-content-blocks

Support email address content blocks
  • Loading branch information
pezholio authored Aug 12, 2024
2 parents eb1fbf4 + 06ee8a1 commit 0244779
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 46 deletions.
12 changes: 11 additions & 1 deletion app/presenters/content_embed_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,21 @@ def render_embedded_editions(content)
embed_code = embedded_content_references_by_content_id[embedded_edition.content_id].embed_code
content = content.gsub(
embed_code,
embedded_edition.title,
get_content_for_edition(embedded_edition),
)
end

content
end

# This is a temporary solution to get email address content blocks working
# while we agree on a long-term approach that works for everything.
def get_content_for_edition(edition)
if edition.document_type == "content_block_email_address"
edition.details[:email_address]
else
edition.title
end
end
end
end
2 changes: 1 addition & 1 deletion app/services/embedded_content_finder_service.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class EmbeddedContentFinderService
ContentReference = Data.define(:document_type, :content_id, :embed_code)

SUPPORTED_DOCUMENT_TYPES = %w[contact].freeze
SUPPORTED_DOCUMENT_TYPES = %w[contact content_block_email_address].freeze
UUID_REGEX = /([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12})/
EMBED_REGEX = /({{embed:(#{SUPPORTED_DOCUMENT_TYPES.join('|')}):#{UUID_REGEX}}})/

Expand Down
39 changes: 36 additions & 3 deletions spec/presenters/content_embed_presenter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,14 @@
:edition,
document:,
details: details.deep_stringify_keys,
links_hash: {
embed: [embedded_content_id],
},
links_hash:,
)
end
let(:links_hash) do
{
embed: [embedded_content_id],
}
end
let(:details) { {} }

before do
Expand Down Expand Up @@ -97,5 +100,35 @@
end
end
end

context "when the document is an email address" do
let(:embedded_document) { create(:document) }
let(:links_hash) do
{
embed: [embedded_document.content_id],
}
end

before do
create(
:edition,
document: embedded_document,
state: "published",
content_store: "live",
document_type: "content_block_email_address",
details: {
email_address: "[email protected]",
},
)
end

let(:details) { { body: "some string with a reference: {{embed:content_block_email_address:#{embedded_document.content_id}}}" } }

it "returns an email address" do
expect(described_class.new(edition).render_embedded_content(details)).to eq({
body: "some string with a reference: [email protected]",
})
end
end
end
end
91 changes: 50 additions & 41 deletions spec/services/embedded_content_finder_service_spec.rb
Original file line number Diff line number Diff line change
@@ -1,67 +1,76 @@
RSpec.describe EmbeddedContentFinderService do
let(:contacts) do
[
create(:edition,
state: "published",
document_type: "contact",
content_store: "live",
details: { title: "Some Title" }),
RSpec.shared_examples "finds references" do |document_type|
describe "when content is a #{document_type}" do
let(:editions) do
[
create(:edition,
state: "published",
document_type:,
content_store: "live",
details: { title: "Some Title" }),
create(:edition,
state: "published",
document_type:,
content_store: "live",
details: { title: "Some other Title" }),
]
end

let(:draft_edition) do
create(:edition,
state: "published",
document_type: "contact",
state: "draft",
document_type:,
content_store: "live",
details: { title: "Some other Title" }),
]
end
let(:draft_contact) do
create(:edition,
state: "draft",
document_type: "contact",
content_store: "live",
details: { title: "Some Title" })
end
details: { title: "Some Title" })
end

describe ".fetch_linked_content_ids" do
it "returns an empty hash where there are no embeds" do
body = "Hello world!"
it "finds content references" do
body = "{{embed:#{document_type}:#{editions[0].content_id}}} {{embed:#{document_type}:#{editions[1].content_id}}}"

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

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

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

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

expect(links).to eq([contacts[0].content_id, contacts[1].content_id])
expect(links).to eq([editions[0].content_id, editions[1].content_id])
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}}}" }]

links = EmbeddedContentFinderService.new.fetch_linked_content_ids(body, Edition::DEFAULT_LOCALE)
it "errors when given a content ID that is still draft" do
body = "{{embed:#{document_type}:#{draft_edition.content_id}}}"

expect(links).to eq([contacts[0].content_id, contacts[1].content_id])
expect { EmbeddedContentFinderService.new.fetch_linked_content_ids(body, Edition::DEFAULT_LOCALE) }.to raise_error(CommandError)
end

it "errors when given a content ID that has no live editions" do
body = "{{embed:contact:00000000-0000-0000-0000-000000000000}}"
it "errors when given a live content ID that is not available in the current locale" do
body = "{{embed:#{document_type}:#{editions[0].content_id}}}"

expect { EmbeddedContentFinderService.new.fetch_linked_content_ids(body, Edition::DEFAULT_LOCALE) }.to raise_error(CommandError)
expect { EmbeddedContentFinderService.new.fetch_linked_content_ids(body, "foo") }.to raise_error(CommandError)
end
end
end

it "errors when given a content ID that is still draft" do
body = "{{embed:contact:#{draft_contact.content_id}}}"
RSpec.describe EmbeddedContentFinderService do
describe ".fetch_linked_content_ids" do
EmbeddedContentFinderService::SUPPORTED_DOCUMENT_TYPES.each do |document_type|
include_examples "finds references", document_type
end

expect { EmbeddedContentFinderService.new.fetch_linked_content_ids(body, Edition::DEFAULT_LOCALE) }.to raise_error(CommandError)
it "returns an empty hash where there are no embeds" do
body = "Hello world!"

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

expect(links).to eq([])
end

it "errors when given a live content ID that is not available in the current locale" do
body = "{{embed:contact:#{contacts[0].content_id}}}"
it "errors when given a content ID that has no live editions" do
body = "{{embed:contact:00000000-0000-0000-0000-000000000000}}"

expect { EmbeddedContentFinderService.new.fetch_linked_content_ids(body, "foo") }.to raise_error(CommandError)
expect { EmbeddedContentFinderService.new.fetch_linked_content_ids(body, Edition::DEFAULT_LOCALE) }.to raise_error(CommandError)
end
end
end

0 comments on commit 0244779

Please sign in to comment.