Skip to content

Commit

Permalink
Get instances from Publishing API
Browse files Browse the repository at this point in the history
In some circumstances (such as guides), instances can be over multiple
pages, so we can’t reliably get the number of instances from the
preview, so we need to get the metadata directly from the Publishing API
  • Loading branch information
pezholio committed Dec 9, 2024
1 parent 2d074c8 commit d49745c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,13 @@ def content_block_spans(nokogiri_html)
nokogiri_html.css("span[data-content-id=\"#{@content_block_edition.document.content_id}\"]")
end

def metadata
response = Services.publishing_api.get_content_by_embedded_document(@content_block_edition.document.content_id, { host_content_id: @content_id })
response.parsed_content
end

def instances_count
content_block_spans(html).length
metadata["results"][0]["instances"]
end

ERROR_HTML = "<html><body><p>Preview not found</p></body></html>".freeze
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ class ContentBlockManager::GetPreviewContentTest < ActiveSupport::TestCase
let(:block_to_preview) do
build(:content_block_edition, :email_address, document:, details: { "email_address" => "[email protected]" }, id: 1)
end
let(:metadata_response) do
stub(:response, parsed_content: {
"results" => [
{ "instances" => 2 },
],
})
end

before do
Services.publishing_api.expects(:get_content_by_embedded_document)
.with(block_to_preview.document.content_id, { host_content_id: })
.returns(metadata_response)
end

describe "#for_content_id" do
setup do
Expand All @@ -57,16 +70,12 @@ class ContentBlockManager::GetPreviewContentTest < ActiveSupport::TestCase
it "returns the count of instances" do
Net::HTTP.expects(:get).with(URI(Plek.website_root + host_base_path)).returns(fake_frontend_response)

expected_content = {
instances_count: 1,
}

actual_content = ContentBlockManager::GetPreviewContent.for_content_id(
content_id: host_content_id,
content_block_edition: block_to_preview,
)

assert_equal expected_content[:instances_count], actual_content.instances_count
assert_equal 2, actual_content.instances_count
end

it "returns the preview html" do
Expand Down

0 comments on commit d49745c

Please sign in to comment.