diff --git a/lib/engines/content_block_manager/app/controllers/content_block_manager/content_block/editions/preview_controller.rb b/lib/engines/content_block_manager/app/controllers/content_block_manager/content_block/editions/preview_controller.rb index 54a7ae83a6e..678bc6b0506 100644 --- a/lib/engines/content_block_manager/app/controllers/content_block_manager/content_block/editions/preview_controller.rb +++ b/lib/engines/content_block_manager/app/controllers/content_block_manager/content_block/editions/preview_controller.rb @@ -1,6 +1,7 @@ class ContentBlockManager::ContentBlock::Editions::PreviewController < ContentBlockManager::BaseController def show host_content_id = params[:host_content_id] - @preview_content = ContentBlockManager::GetPreviewContent.new(content_id: host_content_id).preview_content + content_block_edition = ContentBlockManager::ContentBlock::Edition.find(params[:id]) + @preview_content = ContentBlockManager::GetPreviewContent.new(content_id: host_content_id, content_block_edition:).preview_content end end diff --git a/lib/engines/content_block_manager/app/services/content_block_manager/get_preview_content.rb b/lib/engines/content_block_manager/app/services/content_block_manager/get_preview_content.rb index a513ad58cd4..572b70486ca 100644 --- a/lib/engines/content_block_manager/app/services/content_block_manager/get_preview_content.rb +++ b/lib/engines/content_block_manager/app/services/content_block_manager/get_preview_content.rb @@ -4,8 +4,9 @@ module ContentBlockManager class GetPreviewContent - def initialize(content_id:) + def initialize(content_id:, content_block_edition:) @content_id = content_id + @content_block_edition = content_block_edition end def preview_content @@ -17,6 +18,10 @@ def preview_content private + def html + @html ||= preview_html + end + def content_item @content_item ||= begin response = Services.publishing_api.get_content(@content_id) @@ -32,9 +37,21 @@ def frontend_path frontend_base_path + content_item["base_path"] end - def html + def preview_html uri = URI(frontend_path) - @html ||= Nokogiri::HTML.parse(Net::HTTP.get(uri)) + nokogiri_html = Nokogiri::HTML.parse(Net::HTTP.get(uri)) + replace_existing_content_blocks(nokogiri_html) + end + + def replace_existing_content_blocks(nokogiri_html) + existing_content_block_spans(nokogiri_html).each do |span| + span.replace @content_block_edition.render + end + nokogiri_html + end + + def existing_content_block_spans(nokogiri_html) + nokogiri_html.css("span[data-content-id=\"#{@content_block_edition.document.content_id}\"]") end end end diff --git a/lib/engines/content_block_manager/test/unit/app/services/get_preview_content_test.rb b/lib/engines/content_block_manager/test/unit/app/services/get_preview_content_test.rb index f7e00ad329e..00194f07a8d 100644 --- a/lib/engines/content_block_manager/test/unit/app/services/get_preview_content_test.rb +++ b/lib/engines/content_block_manager/test/unit/app/services/get_preview_content_test.rb @@ -4,12 +4,22 @@ class ContentBlockManager::GetPreviewContentTest < ActiveSupport::TestCase extend Minitest::Spec::DSL let(:described_class) { ContentBlockManager::GetPreviewContent } - let(:host_content_id) { "64570503-7a7f-4fca-80c1-e6dce7278419" } + let(:host_content_id) { SecureRandom.uuid } + let(:preview_content_id) { SecureRandom.uuid } let(:host_title) { "Test" } let(:host_base_path) { "/test" } let(:uri_mock) { mock } let(:fake_frontend_response) do - "
test
" + "test
" + end + let(:expected_html) do + "test
" + end + let(:document) do + build(:content_block_document, :email_address, content_id: preview_content_id) + end + let(:block_to_preview) do + build(:content_block_edition, :email_address, document:, details: { "email_address" => "new@new.com" }) end describe "#preview_content" do @@ -17,16 +27,21 @@ class ContentBlockManager::GetPreviewContentTest < ActiveSupport::TestCase stub_publishing_api_has_item(content_id: host_content_id, title: host_title, base_path: host_base_path) end - it "returns the title and raw frontend HTML for a document" do + it "returns the title and preview HTML for a document" do Net::HTTP.expects(:get).with(URI(Plek.website_root + host_base_path)).returns(fake_frontend_response) - Nokogiri::HTML4.expects(:parse).with(fake_frontend_response).returns(fake_frontend_response) expected_content = { title: host_title, - html: fake_frontend_response, + html: Nokogiri::HTML.parse(expected_html), } - assert_equal expected_content, ContentBlockManager::GetPreviewContent.new(content_id: host_content_id).preview_content + actual_content = ContentBlockManager::GetPreviewContent.new( + content_id: host_content_id, + content_block_edition: block_to_preview, + ).preview_content + + assert_equal expected_content[:title], actual_content[:title] + assert_equal expected_content[:html].to_s, actual_content[:html].to_s end end end