Skip to content

Commit

Permalink
wip replace existing content block with new one
Browse files Browse the repository at this point in the history
  • Loading branch information
Harriethw committed Nov 22, 2024
1 parent f79d0a8 commit 41c0ea2
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
Expand All @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,44 @@ 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
"<!DOCTYPE html><body><p>test</p></body>"
"<body><p>test</p><span class=\"content-embed content-embed__content_block_email_address\" data-content-block=\"\" data-document-type=\"content_block_email_address\" data-content-id=\"#{preview_content_id}\">example@example.com</span></body>"
end
let(:expected_html) do
"<body><p>test</p><span class=\"content-embed content-embed__content_block_email_address\" data-content-block=\"\" data-document-type=\"content_block_email_address\" data-content-id=\"#{preview_content_id}\">new@new.com</span></body>"
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
setup do
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

0 comments on commit 41c0ea2

Please sign in to comment.