From 88325b57706942bf0ce70a812f121276d13de1c2 Mon Sep 17 00:00:00 2001 From: pezholio Date: Fri, 10 Jan 2025 11:29:05 +0000 Subject: [PATCH] Use `ContentBlockManager::PreviewContent` We also remove the `GetPreviewContent` service --- .../editions/host_content_controller.rb | 2 +- .../get_preview_content.rb | 121 -------------- .../app/services/get_preview_content_test.rb | 149 ------------------ 3 files changed, 1 insertion(+), 271 deletions(-) delete mode 100644 lib/engines/content_block_manager/app/services/content_block_manager/get_preview_content.rb delete mode 100644 lib/engines/content_block_manager/test/unit/app/services/get_preview_content_test.rb diff --git a/lib/engines/content_block_manager/app/controllers/content_block_manager/content_block/editions/host_content_controller.rb b/lib/engines/content_block_manager/app/controllers/content_block_manager/content_block/editions/host_content_controller.rb index ad046c5bb77..11d64638ef8 100644 --- a/lib/engines/content_block_manager/app/controllers/content_block_manager/content_block/editions/host_content_controller.rb +++ b/lib/engines/content_block_manager/app/controllers/content_block_manager/content_block/editions/host_content_controller.rb @@ -2,7 +2,7 @@ class ContentBlockManager::ContentBlock::Editions::HostContentController < Conte def preview host_content_id = params[:host_content_id] @content_block_edition = ContentBlockManager::ContentBlock::Edition.find(params[:id]) - @preview_content = ContentBlockManager::GetPreviewContent.for_content_id( + @preview_content = ContentBlockManager::PreviewContent.for_content_id( content_id: host_content_id, content_block_edition: @content_block_edition, base_path: params[:base_path], 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 deleted file mode 100644 index 427cc524963..00000000000 --- a/lib/engines/content_block_manager/app/services/content_block_manager/get_preview_content.rb +++ /dev/null @@ -1,121 +0,0 @@ -require "net/http" -require "json" -require "uri" - -module ContentBlockManager - class GetPreviewContent - include ContentBlockManager::Engine.routes.url_helpers - - def self.for_content_id(content_id:, content_block_edition:, base_path: nil) - new(content_id:, content_block_edition:, base_path:).for_content_id - end - - def for_content_id - ContentBlockManager::PreviewContent.new(title: content_item["title"], html:, instances_count:) - end - - private - - def initialize(content_id:, content_block_edition:, base_path: nil) - @content_id = content_id - @content_block_edition = content_block_edition - @base_path = base_path - end - - def html - @html ||= preview_html - end - - def content_item - @content_item ||= begin - response = Services.publishing_api.get_content(@content_id) - response.parsed_content - end - end - - def frontend_base_path - Rails.env.development? ? Plek.external_url_for("government-frontend") : Plek.website_root - end - - def base_path - @base_path || content_item["base_path"] - end - - def frontend_path - frontend_base_path + base_path - end - - def preview_html - uri = URI(frontend_path) - nokogiri_html = html_snapshot_from_frontend(uri) - update_local_link_paths(nokogiri_html) - add_draft_style(nokogiri_html) - replace_existing_content_blocks(nokogiri_html) - end - - def update_local_link_paths(nokogiri_html) - url = content_block_manager_content_block_host_content_preview_path(id: @content_block_edition.id, host_content_id: @content_id) - nokogiri_html.css("a").each do |link| - next if link[:href].start_with?("//") || link[:href].start_with?("http") - - link[:href] = "#{url}?base_path=#{link[:href]}" - link[:target] = "_parent" - end - - nokogiri_html - end - - def replace_existing_content_blocks(nokogiri_html) - replace_blocks(nokogiri_html) - style_blocks(nokogiri_html) - nokogiri_html - end - - def replace_blocks(nokogiri_html) - @preview_content_block_render ||= @content_block_edition.render - content_block_spans(nokogiri_html).each do |span| - span.replace @preview_content_block_render - end - end - - BLOCK_STYLE = "background-color: yellow;".freeze - - def style_blocks(nokogiri_html) - content_block_spans(nokogiri_html).each do |span| - span["style"] = BLOCK_STYLE - end - end - - 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_host_content_item_for_content_id(@content_block_edition.document.content_id, @content_id) - response.parsed_content - end - - def instances_count - metadata["instances"] - end - - ERROR_HTML = "

Preview not found

".freeze - - def html_snapshot_from_frontend(uri) - begin - raw_html = Net::HTTP.get(uri) - rescue StandardError - raw_html = ERROR_HTML - end - Nokogiri::HTML.parse(raw_html) - end - - def add_draft_style(nokogiri_html) - nokogiri_html.css("body").each do |body| - body["class"] ||= "" - body["class"] += " draft" - end - nokogiri_html - 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 deleted file mode 100644 index edd2cb8a50b..00000000000 --- a/lib/engines/content_block_manager/test/unit/app/services/get_preview_content_test.rb +++ /dev/null @@ -1,149 +0,0 @@ -require "test_helper" - -class ContentBlockManager::GetPreviewContentTest < ActiveSupport::TestCase - extend Minitest::Spec::DSL - include ContentBlockManager::Engine.routes.url_helpers - include TextAssertions - - let(:described_class) { ContentBlockManager::GetPreviewContent } - 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

example@example.com" - end - let(:block_render) do - "new@new.com" - end - let(:block_render_with_style) do - "new@new.com" - end - let(:expected_html) do - "

test

#{block_render_with_style}" - end - let(:error_html) do - "

Preview not found

" - 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" }, id: 1) - end - let(:metadata_response) do - stub(:response, parsed_content: { "instances" => 2 }) - end - - before do - Services.publishing_api.expects(:get_host_content_item_for_content_id) - .with(block_to_preview.document.content_id, host_content_id) - .returns(metadata_response) - end - - describe "#for_content_id" do - setup do - stub_publishing_api_has_item(content_id: host_content_id, title: host_title, base_path: host_base_path) - block_to_preview.expects(:render).returns(block_render) - end - - it "returns the title of host document" do - Net::HTTP.expects(:get).with(URI(Plek.website_root + host_base_path)).returns(fake_frontend_response) - - expected_content = { - title: host_title, - } - - actual_content = ContentBlockManager::GetPreviewContent.for_content_id( - content_id: host_content_id, - content_block_edition: block_to_preview, - ) - - assert_equal expected_content[:title], actual_content.title - end - - it "returns the count of instances" do - Net::HTTP.expects(:get).with(URI(Plek.website_root + host_base_path)).returns(fake_frontend_response) - - actual_content = ContentBlockManager::GetPreviewContent.for_content_id( - content_id: host_content_id, - content_block_edition: block_to_preview, - ) - - assert_equal 2, actual_content.instances_count - end - - it "returns the preview html" do - Net::HTTP.expects(:get).with(URI(Plek.website_root + host_base_path)).returns(fake_frontend_response) - - expected_content = { - html: Nokogiri::HTML.parse(expected_html), - } - - actual_content = ContentBlockManager::GetPreviewContent.for_content_id( - content_id: host_content_id, - content_block_edition: block_to_preview, - ) - - assert_equal expected_content[:html].to_s, actual_content.html.to_s - end - - it "shows an error template when the request to the frontend throws an error" do - exception = StandardError.new("Something went wrong") - Net::HTTP.expects(:get).with(URI(Plek.website_root + host_base_path)).raises(exception) - - expected_content = { - title: host_title, - html: Nokogiri::HTML.parse(error_html), - } - - actual_content = ContentBlockManager::GetPreviewContent.for_content_id( - content_id: host_content_id, - content_block_edition: block_to_preview, - ) - - assert_equal expected_content[:title], actual_content.title - assert_equal expected_content[:html].to_s, actual_content.html.to_s - end - - it "updates any link paths" do - fake_frontend_response = " - Internal link - External link - Protocol relative link - " - Net::HTTP.expects(:get).with(URI(Plek.website_root + host_base_path)).returns(fake_frontend_response) - - actual_content = ContentBlockManager::GetPreviewContent.for_content_id( - content_id: host_content_id, - content_block_edition: block_to_preview, - ) - - url = content_block_manager_content_block_host_content_preview_path(id: block_to_preview.id, host_content_id:) - - expected_content = Nokogiri::HTML.parse(" - - - Internal link - External link - Protocol relative link - - - ").to_s - - assert_equal_ignoring_whitespace actual_content.html.to_s, expected_content - end - - it "allows a base_path to be provided" do - base_path = "something/different" - Net::HTTP.expects(:get).with(URI(Plek.website_root + base_path)).returns("") - - ContentBlockManager::GetPreviewContent.for_content_id( - content_id: host_content_id, - content_block_edition: block_to_preview, - base_path:, - ) - end - end -end