Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Present worldwide corporate information pages #7789

Merged
merged 3 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions app/models/corporate_information_page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class CorporateInformationPage < Edition
include ::Attachable
include Searchable

after_commit :republish_organisation_to_publishing_api
after_commit :republish_owning_organisation_to_publishing_api
after_commit :republish_about_page_to_publishing_api, unless: :about_page?
after_save :reindex_organisation_in_search_index, if: :about_page?

Expand Down Expand Up @@ -31,8 +31,8 @@ def process_associations_before_save(new_edition)
scope :with_organisation_govuk_status, ->(status) { joins(:organisation).where(organisations: { govuk_status: status }) }
scope :accessible_documents_policy, -> { where(corporate_information_page_type_id: CorporateInformationPageType::AccessibleDocumentsPolicy.id) }

def republish_organisation_to_publishing_api
Whitehall::PublishingApi.republish_async(owning_organisation) if owning_organisation.is_a?(Organisation)
def republish_owning_organisation_to_publishing_api
Whitehall::PublishingApi.republish_async(owning_organisation) if owning_organisation.present?
end

def republish_about_page_to_publishing_api
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,10 @@ def content
.new(corporate_information_page, update_type:)
.base_attributes
.merge(PayloadBuilder::PublicDocumentPath.for(corporate_information_page))
.merge(
description: corporate_information_page.summary,
.merge(PayloadBuilder::CorporateInformationPage.for(corporate_information_page))
.deep_merge(
details:,
document_type:,
public_updated_at:,
rendering_app: corporate_information_page.rendering_app,
schema_name: SCHEMA_NAME,
links: edition_links,
auth_bypass_ids: [corporate_information_page.auth_bypass_id],
Expand Down Expand Up @@ -59,21 +57,8 @@ def document_type

delegate :display_type_key, to: :corporate_information_page

def base_details
{
body:,
}
end

def body
Whitehall::GovspeakRenderer
.new
.govspeak_edition_to_html(corporate_information_page)
end

def details
base_details
.merge(change_history)
change_history
.merge(CorporateInformationGroups.for(corporate_information_page))
.merge(Organisation.for(corporate_information_page))
.merge(PayloadBuilder::TagDetails.for(corporate_information_page))
Expand All @@ -84,13 +69,6 @@ def links_presenter
@links_presenter ||= LinksPresenter.new(corporate_information_page)
end

def public_updated_at
public_updated_at = corporate_information_page.public_timestamp ||
corporate_information_page.updated_at

public_updated_at.rfc3339
end

def change_history
return {} if corporate_information_page.change_history.blank?

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
module PublishingApi
module PayloadBuilder
class CorporateInformationPage
attr_reader :item

def self.for(item)
new(item).call
end

def initialize(item)
@item = item
end

def call
{
description: item.summary,
rendering_app: item.rendering_app,
public_updated_at:,
details: {
body:,
},
}
end

private

def public_updated_at
public_updated_at = item.public_timestamp ||
item.updated_at

public_updated_at.rfc3339
end

def body
Whitehall::GovspeakRenderer
.new
.govspeak_edition_to_html(item)
end
end
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module PublishingApi
class WorldwideCorporateInformationPagePresenter
include UpdateTypeHelper

attr_accessor :item, :update_type

def initialize(item, update_type: nil)
self.item = item
self.update_type =
update_type || default_update_type(item)
end

delegate :content_id, to: :item

def content
content = BaseItemPresenter.new(
item,
update_type:,
).base_attributes

content.merge!(PayloadBuilder::CorporateInformationPage.for(item))
content.merge!(PayloadBuilder::PolymorphicPath.for(item))

content.merge!(
document_type: item.display_type_key,
schema_name: "worldwide_corporate_information_page",
)
end

def links
{
parent: [item.worldwide_organisation.content_id],
worldwide_organisation: [item.worldwide_organisation.content_id],
}
end
end
end
8 changes: 5 additions & 3 deletions app/presenters/publishing_api_presenters.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@ def presenter_class_for_edition(edition)
when Consultation
PublishingApi::ConsultationPresenter
when CorporateInformationPage
if edition.worldwide_organisation.present?
FALLBACK_EDITION_PRESENTER
else
if edition.worldwide_organisation.present? && !edition.about_page?
PublishingApi::WorldwideCorporateInformationPagePresenter
elsif edition.organisation.present?
PublishingApi::CorporateInformationPagePresenter
else
FALLBACK_EDITION_PRESENTER
end
when ::DetailedGuide
PublishingApi::DetailedGuidePresenter
Expand Down
26 changes: 26 additions & 0 deletions test/unit/models/corporate_information_page_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,30 @@ class CorporateInformationPageTest < ActiveSupport::TestCase

assert_equal "/world/organisations/#{worldwide_organisation.name}/about/about", corporate_information_page.base_path
end

test "republishes owning organisation after commit when present" do
organisation = create(:organisation)
corporate_information_page = create(:corporate_information_page, organisation:, worldwide_organisation: nil)

Whitehall::PublishingApi.expects(:republish_async).with(organisation).once

corporate_information_page.update!(body: "new body")
end

test "republishes owning worldwide organisation after commit when present" do
worldwide_organisation = create(:worldwide_organisation)
corporate_information_page = create(:corporate_information_page, organisation: nil, worldwide_organisation:)

Whitehall::PublishingApi.expects(:republish_async).with(worldwide_organisation).once

corporate_information_page.update!(body: "new body")
end

test "does not republish owning organisation when absent" do
corporate_information_page = create(:corporate_information_page, organisation: nil, worldwide_organisation: nil)

Whitehall::PublishingApi.expects(:republish_async).never

corporate_information_page.update!(body: "new body")
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
require "test_helper"

module PublishingApi::WorldwideCorporateInformationPagePresenterTest
class TestCase < ActiveSupport::TestCase
attr_accessor :corporate_information_page, :update_type

def presented_corporate_information_page
PublishingApi::WorldwideCorporateInformationPagePresenter.new(
corporate_information_page,
update_type:,
)
end

class BasicCorporateInformationPageTest < TestCase
setup do
worldwide_organisation = create(:worldwide_organisation)
self.corporate_information_page = create(:corporate_information_page, worldwide_organisation:, organisation: nil)
end

test "presents a Worldwide Corporate Information Page ready for adding to the publishing API" do
public_path = corporate_information_page.public_path

expected_hash = {
base_path: public_path,
title: corporate_information_page.title,
schema_name: "worldwide_corporate_information_page",
document_type: corporate_information_page.display_type_key,
locale: "en",
publishing_app: Whitehall::PublishingApp::WHITEHALL,
rendering_app: Whitehall::RenderingApp::WHITEHALL_FRONTEND,
public_updated_at: corporate_information_page.updated_at,
routes: [{ path: public_path, type: "exact" }],
redirects: [],
description: "edition-summary",
details: {
body: "<div class=\"govspeak\"><p>Some stuff</p>\n</div>",
},
update_type: "major",
}

expected_links = {
parent: [
corporate_information_page.owning_organisation.content_id,
],
worldwide_organisation: [
corporate_information_page.owning_organisation.content_id,
],
}

presented_item = presented_corporate_information_page

assert_equal expected_hash, presented_item.content
assert_hash_includes presented_item.links, expected_links
assert_equal "major", presented_item.update_type
assert_equal corporate_information_page.content_id, presented_item.content_id

assert_valid_against_publisher_schema(presented_item.content, "worldwide_corporate_information_page")
assert_valid_against_links_schema({ links: presented_item.links }, "worldwide_corporate_information_page")
end
end

class CorporateInformationPageWithMinorChange < TestCase
setup do
self.corporate_information_page = create(
:corporate_information_page,
minor_change: true,
)
end

test "update type" do
assert_equal "minor", presented_corporate_information_page.update_type
end
end

class CorporateInformationPageWithoutMinorChange < TestCase
setup do
self.corporate_information_page = create(
:corporate_information_page,
minor_change: false,
)
end

test "update type" do
assert_equal "major", presented_corporate_information_page.update_type
end
end
end
end
22 changes: 20 additions & 2 deletions test/unit/presenters/publishing_api_presenters_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,15 @@ class PublishingApiPresentersTest < ActiveSupport::TestCase
)
end

test ".presenter_for returns a GenericEditionPresenter for a " \
"CorporateInformationPage belonging to an WorldwideOrganisation" do
test ".presenter_for returns a GenericEditionPresenter for an " \
"AboutUs CorporateInformationPage belonging to an WorldwideOrganisation" do
presenter = PublishingApiPresenters
.presenter_for(
build(
:corporate_information_page,
worldwide_organisation: build(:worldwide_organisation),
organisation: nil,
corporate_information_page_type_id: CorporateInformationPageType::AboutUs.id,
),
)

Expand All @@ -159,4 +161,20 @@ class PublishingApiPresentersTest < ActiveSupport::TestCase
presenter.class,
)
end

test ".presenter_for returns a WorldwideCorporateInformationPagePresenter for a " \
"CorporateInformationPage belonging to an WorldwideOrganisation" do
presenter = PublishingApiPresenters
.presenter_for(
build(
:corporate_information_page,
worldwide_organisation: build(:worldwide_organisation),
),
)

assert_equal(
PublishingApi::WorldwideCorporateInformationPagePresenter,
presenter.class,
)
end
end