Skip to content

Commit

Permalink
Call the new model method
Browse files Browse the repository at this point in the history
This replaces all calls to `ContentBlockManager::GetHostContentItems`
with `ContentBlockManager::HostContentItem` and deletes the now unused
service.
  • Loading branch information
pezholio committed Jan 10, 2025
1 parent dedbac1 commit 49eb6b0
Show file tree
Hide file tree
Showing 15 changed files with 162 additions and 284 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def initialize(caption:, host_content_items:, content_block_edition:, is_preview
@host_content_items = host_content_items
@is_preview = is_preview
@current_page = current_page.presence || 1
@order = order.presence || ContentBlockManager::GetHostContentItems::DEFAULT_ORDER
@order = order.presence || ContentBlockManager::HostContentItem::DEFAULT_ORDER
@content_block_edition = content_block_edition
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ def show
@order = params[:order]
@page = params[:page]

@host_content_items = ContentBlockManager::GetHostContentItems.by_embedded_document(
content_block_document: @content_block_document,
@host_content_items = ContentBlockManager::HostContentItem.for_document(
@content_block_document,
order: @order,
page: @page,
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,8 +109,8 @@ def review_links
@order = params[:order]
@page = params[:page]

@host_content_items = ContentBlockManager::GetHostContentItems.by_embedded_document(
content_block_document: @content_block_document,
@host_content_items = ContentBlockManager::HostContentItem.for_document(
@content_block_document,
order: @order,
page: @page,
)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ def schedule_with_rollback
end

def send_publish_intents_for_host_documents(content_block_edition:)
host_content_items = ContentBlockManager::GetHostContentItems.by_embedded_document(
content_block_document: content_block_edition.document,
)
host_content_items = ContentBlockManager::HostContentItem.for_document(content_block_edition.document)
host_content_items.each do |host_content_item|
ContentBlockManager::PublishIntentWorker.perform_async(
host_content_item.base_path,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,7 +536,7 @@ def should_show_edit_form_for_email_address_content_block(document_title, email_
stub_publishing_api_has_embedded_content_for_any_content_id(
results: @dependent_content,
total: @dependent_content.length,
order: ContentBlockManager::GetHostContentItems::DEFAULT_ORDER,
order: ContentBlockManager::HostContentItem::DEFAULT_ORDER,
rollup: @rollup,
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Before do
stub_publishing_api_has_embedded_content_for_any_content_id(total: 0, results: [], order: ContentBlockManager::GetHostContentItems::DEFAULT_ORDER)
stub_publishing_api_has_embedded_content_for_any_content_id(total: 0, results: [], order: ContentBlockManager::HostContentItem::DEFAULT_ORDER)
end

Before("@disable_sidekiq") do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
FactoryBot.define do
factory :host_content_items, class: "ContentBlockManager::HostContentItems" do
factory :host_content_items, class: "ContentBlockManager::HostContentItem::Items" do
total_pages { 1 }
total { 10 }
items { build_list(:host_content_item, 10) }
Expand All @@ -13,7 +13,7 @@
end
end

factory :rollup, class: "ContentBlockManager::HostContentItems::Rollup" do
factory :rollup, class: "ContentBlockManager::HostContentItem::Items::Rollup" do
views { Random.rand(0..10) }
locations { Random.rand(0..10) }
instances { Random.rand(0..10) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class ContentBlockManager::ContentBlock::DocumentsTest < ActionDispatch::Integra
stub_publishing_api_has_embedded_content_for_any_content_id(
results: [],
total: 0,
order: ContentBlockManager::GetHostContentItems::DEFAULT_ORDER,
order: ContentBlockManager::HostContentItem::DEFAULT_ORDER,
)

visit content_block_manager_content_block_document_path(document)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ContentBlockManager::ContentBlock::WorkflowTest < ActionDispatch::Integrat

stub_request_for_schema("email_address")

stub_publishing_api_has_embedded_content(content_id: @content_id, total: 0, results: [], order: ContentBlockManager::GetHostContentItems::DEFAULT_ORDER)
stub_publishing_api_has_embedded_content(content_id: @content_id, total: 0, results: [], order: ContentBlockManager::HostContentItem::DEFAULT_ORDER)
end

describe "when creating a new content block" do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def self.it_returns_embedded_content(&block)
stub_publishing_api_has_embedded_content_for_any_content_id(
results: host_content_items,
total: host_content_items.length,
order: ContentBlockManager::GetHostContentItems::DEFAULT_ORDER,
order: ContentBlockManager::HostContentItem::DEFAULT_ORDER,
)

ContentBlockManager::HostContentItem::Editor.stubs(:with_uuids).with(host_content_items.map { |i| i["last_edited_by_editor_id"] }).returns(host_content_item_users)
Expand All @@ -51,15 +51,15 @@ def self.it_returns_embedded_content(&block)
results: host_content_items,
total:,
total_pages: 4,
order: ContentBlockManager::GetHostContentItems::DEFAULT_ORDER,
order: ContentBlockManager::HostContentItem::DEFAULT_ORDER,
)

pages.each.with_index(2) do |page, i|
stub_publishing_api_has_embedded_content_for_any_content_id(
results: page,
total:,
total_pages: 4,
order: ContentBlockManager::GetHostContentItems::DEFAULT_ORDER,
order: ContentBlockManager::HostContentItem::DEFAULT_ORDER,
page_number: i.to_s,
)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,151 @@
class ContentBlockManager::HostContentItemTest < ActiveSupport::TestCase
extend Minitest::Spec::DSL

describe ".for_document" do
let(:described_class) { ContentBlockManager::HostContentItem }

let(:target_content_id) { SecureRandom.uuid }

let(:host_content_id) { SecureRandom.uuid }

let(:last_edited_by_editor_id) { SecureRandom.uuid }

let(:rollup) { build(:rollup) }

let(:response_body) do
{
"content_id" => SecureRandom.uuid,
"total" => 111,
"total_pages" => 12,
"rollup" => rollup.to_h,
"results" => [
{
"title" => "foo",
"base_path" => "/foo",
"document_type" => "something",
"publishing_app" => "publisher",
"last_edited_by_editor_id" => last_edited_by_editor_id,
"last_edited_at" => "2023-01-01T08:00:00.000Z",
"unique_pageviews" => 123,
"instances" => 1,
"host_content_id" => host_content_id,
"primary_publishing_organisation" => {
"content_id" => SecureRandom.uuid,
"title" => "bar",
"base_path" => "/bar",
},
},
],
}
end

let(:editor) { build(:host_content_item_editor, uid: last_edited_by_editor_id) }

let(:fake_api_response) do
GdsApi::Response.new(
stub("http_response", code: 200, body: response_body.to_json),
)
end
let(:publishing_api_mock) { stub("GdsApi::PublishingApi") }
let(:document) { mock("content_block_document", content_id: target_content_id) }

before do
Services.expects(:publishing_api).returns(publishing_api_mock)
ContentBlockManager::HostContentItem::Editor.stubs(:with_uuids).returns([editor])
end

it "calls the Publishing API for the content which embeds the target" do
publishing_api_mock.expects(:get_host_content_for_content_id)
.with(target_content_id, { order: described_class::DEFAULT_ORDER })
.returns(fake_api_response)

described_class.for_document(document)
end

it "supports pagination" do
publishing_api_mock.expects(:get_host_content_for_content_id)
.with(target_content_id, { page: 1, order: described_class::DEFAULT_ORDER })
.returns(fake_api_response)

described_class.for_document(document, page: 1)
end

it "supports sorting" do
publishing_api_mock.expects(:get_host_content_for_content_id)
.with(target_content_id, { order: "-abc" })
.returns(fake_api_response)

described_class.for_document(document, order: "-abc")
end

it "calls the editor finder with the correct argument" do
publishing_api_mock.expects(:get_host_content_for_content_id).returns(fake_api_response)
ContentBlockManager::HostContentItem::Editor.expects(:with_uuids).with([last_edited_by_editor_id]).returns([editor])

described_class.for_document(document)
end

it "returns items" do
publishing_api_mock.expects(:get_host_content_for_content_id).returns(fake_api_response)

result = described_class.for_document(document)

expected_publishing_organisation = {
"content_id" => response_body["results"][0]["primary_publishing_organisation"]["content_id"],
"title" => response_body["results"][0]["primary_publishing_organisation"]["title"],
"base_path" => response_body["results"][0]["primary_publishing_organisation"]["base_path"],
}

assert_equal result.total, response_body["total"]
assert_equal result.total_pages, response_body["total_pages"]

assert_equal result.rollup.views, rollup.views
assert_equal result.rollup.locations, rollup.locations
assert_equal result.rollup.instances, rollup.instances
assert_equal result.rollup.organisations, rollup.organisations

assert_equal result[0].title, response_body["results"][0]["title"]
assert_equal result[0].base_path, response_body["results"][0]["base_path"]
assert_equal result[0].document_type, response_body["results"][0]["document_type"]
assert_equal result[0].publishing_app, response_body["results"][0]["publishing_app"]
assert_equal result[0].last_edited_by_editor, editor
assert_equal result[0].last_edited_at, Time.zone.parse(response_body["results"][0]["last_edited_at"])
assert_equal result[0].unique_pageviews, response_body["results"][0]["unique_pageviews"]
assert_equal result[0].instances, response_body["results"][0]["instances"]
assert_equal result[0].host_content_id, response_body["results"][0]["host_content_id"]

assert_equal result[0].publishing_organisation, expected_publishing_organisation
end

describe "when last_edited_by_editor_id is nil" do
let(:last_edited_by_editor_id) { nil }

it "returns nil for last_edited_by_editor" do
publishing_api_mock.expects(:get_host_content_for_content_id).returns(fake_api_response)

ContentBlockManager::HostContentItem::Editor.expects(:with_uuids).never

result = described_class.for_document(document)

assert_equal result[0].last_edited_by_editor, nil
end
end

it "returns an error if the content that embeds the target can't be loaded" do
publishing_api_mock.expects(:get_host_content_for_content_id).raises(
GdsApi::HTTPErrorResponse.new(
500,
"An internal error message",
"error" => { "message" => "Some backend error" },
),
)

assert_raises(GdsApi::HTTPErrorResponse) do
described_class.for_document(document)
end
end
end

describe "#last_edited_at" do
it "translates to a TimeWithZone object" do
last_edited_at = 4.days.ago
Expand Down

This file was deleted.

Loading

0 comments on commit 49eb6b0

Please sign in to comment.