From d9f80def5066314e677bab998274c67381ec31eb Mon Sep 17 00:00:00 2001 From: Joseph Kempster Date: Wed, 17 Apr 2024 13:24:26 +0100 Subject: [PATCH 1/6] Add worldwide organisation page presenter The new worldwide organisation page model is to replace corporate information pages for editionable worldwide organisations. Add a new presenter for it. --- app/models/worldwide_organisation_page.rb | 23 ++++++- .../worldwide_organisation_page_presenter.rb | 49 ++++++++++++++ .../factories/worldwide_organisation_pages.rb | 1 + ...ldwide_organisation_page_presenter_test.rb | 65 +++++++++++++++++++ 4 files changed, 137 insertions(+), 1 deletion(-) create mode 100644 app/presenters/publishing_api/worldwide_organisation_page_presenter.rb create mode 100644 test/unit/app/presenters/publishing_api/worldwide_organisation_page_presenter_test.rb diff --git a/app/models/worldwide_organisation_page.rb b/app/models/worldwide_organisation_page.rb index 7c50feb1889..eb9ee60acf2 100644 --- a/app/models/worldwide_organisation_page.rb +++ b/app/models/worldwide_organisation_page.rb @@ -8,7 +8,7 @@ class WorldwideOrganisationPage < ApplicationRecord exclusion: { in: [CorporateInformationPageType::AboutUs.id], message: "Type cannot be `About us`" } validate :unique_worldwide_organisation_and_page_type, on: :create, if: :edition - delegate :display_type_key, to: :corporate_information_page_type + delegate :slug, :display_type_key, to: :corporate_information_page_type include Attachable @@ -24,6 +24,11 @@ def corporate_information_page_type=(type) self.corporate_information_page_type_id = type && type.id end + def self.by_menu_heading(menu_heading) + type_ids = CorporateInformationPageType.by_menu_heading(menu_heading).map(&:id) + where(corporate_information_page_type_id: type_ids) + end + def publicly_visible? true end @@ -32,6 +37,22 @@ def access_limited? false end + def publishing_api_presenter + PublishingApi::WorldwideOrganisationPagePresenter + end + + def base_path + "#{edition.base_path}/about/#{slug}" + end + + def public_path(options = {}) + append_url_options(base_path, options) + end + + def public_url(options = {}) + Plek.website_root + public_path(options) + end + private def unique_worldwide_organisation_and_page_type diff --git a/app/presenters/publishing_api/worldwide_organisation_page_presenter.rb b/app/presenters/publishing_api/worldwide_organisation_page_presenter.rb new file mode 100644 index 00000000000..1da8b9f83e0 --- /dev/null +++ b/app/presenters/publishing_api/worldwide_organisation_page_presenter.rb @@ -0,0 +1,49 @@ +module PublishingApi + class WorldwideOrganisationPagePresenter + attr_accessor :item, :update_type + + def initialize(item, update_type: nil) + self.item = item + self.update_type = update_type || "major" + end + + delegate :content_id, to: :item + + def content + content = BaseItemPresenter.new( + item, + title: item.title, + update_type:, + ).base_attributes + + content.merge!( + details: { + body: Whitehall::GovspeakRenderer.new.govspeak_with_attachments_to_html(item.body, item.attachments), + }, + description: item.summary, + public_updated_at: item.updated_at, + rendering_app: Whitehall::RenderingApp::GOVERNMENT_FRONTEND, + schema_name: "worldwide_corporate_information_page", + document_type:, + links: edition_links, + ) + + content.merge!(PayloadBuilder::PolymorphicPath.for(item)) + end + + def edition_links + { + parent: [item.edition.content_id], + worldwide_organisation: [item.edition.content_id], + } + end + + def links + {} + end + + def document_type + item.display_type_key + end + end +end diff --git a/test/factories/worldwide_organisation_pages.rb b/test/factories/worldwide_organisation_pages.rb index 3e15602f708..ff7384c13e8 100644 --- a/test/factories/worldwide_organisation_pages.rb +++ b/test/factories/worldwide_organisation_pages.rb @@ -1,5 +1,6 @@ FactoryBot.define do factory :worldwide_organisation_page do + content_id { SecureRandom.uuid } summary { "Some summary" } body { "Some body" } corporate_information_page_type_id { CorporateInformationPageType::PublicationScheme.id } diff --git a/test/unit/app/presenters/publishing_api/worldwide_organisation_page_presenter_test.rb b/test/unit/app/presenters/publishing_api/worldwide_organisation_page_presenter_test.rb new file mode 100644 index 00000000000..dd78e3c5c65 --- /dev/null +++ b/test/unit/app/presenters/publishing_api/worldwide_organisation_page_presenter_test.rb @@ -0,0 +1,65 @@ +require "test_helper" + +module PublishingApi::WorldwideOrganisationPagePresenterTest + class TestCase < ActiveSupport::TestCase + attr_accessor :page, :update_type + + def presented_page + PublishingApi::WorldwideOrganisationPagePresenter.new( + page, + update_type:, + ) + end + + class BasicWorldwideOrganisationPageTest < TestCase + setup do + self.page = create(:worldwide_organisation_page) + end + + test "presents a Worldwide Organisation Page ready for adding to the publishing API" do + public_path = page.public_path + + expected_hash = { + base_path: public_path, + title: page.title, + schema_name: "worldwide_corporate_information_page", + document_type: page.display_type_key, + locale: "en", + publishing_app: Whitehall::PublishingApp::WHITEHALL, + rendering_app: Whitehall::RenderingApp::GOVERNMENT_FRONTEND, + public_updated_at: page.updated_at, + routes: [{ path: public_path, type: "exact" }], + redirects: [], + description: "Some summary", + details: { + body: "

Some body

\n
", + }, + update_type: "major", + links: { + parent: [page.edition.content_id], + worldwide_organisation: [page.edition.content_id], + }, + } + + expected_links = { + parent: [ + page.edition.content_id, + ], + worldwide_organisation: [ + page.edition.content_id, + ], + } + + presented_item = presented_page + + assert_equal expected_hash, presented_item.content + assert_hash_includes presented_item.edition_links, expected_links + assert_equal "major", presented_item.update_type + assert_equal 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.edition_links }, "worldwide_corporate_information_page") + end + end + end +end From 2d44b6e9f5793b67dc3cc126e5ac05238498c24b Mon Sep 17 00:00:00 2001 From: Joseph Kempster Date: Wed, 17 Apr 2024 13:36:13 +0100 Subject: [PATCH 2/6] Add multiple page types to worldwide organisation factory This is required to sufficiently test the editionable worldwide organisation presenter. --- .../editionable_worldwide_organisations.rb | 16 ++++++++++++++-- .../editionable_worldwide_organisation_test.rb | 2 +- 2 files changed, 15 insertions(+), 3 deletions(-) diff --git a/test/factories/editionable_worldwide_organisations.rb b/test/factories/editionable_worldwide_organisations.rb index 319c0d4bc81..e9752d2392e 100644 --- a/test/factories/editionable_worldwide_organisations.rb +++ b/test/factories/editionable_worldwide_organisations.rb @@ -42,9 +42,21 @@ end end + trait(:with_page) do + after :create do |organisation, _evaluator| + organisation.pages = [build(:worldwide_organisation_page)] + end + end + trait(:with_pages) do - after :create do |organisation| - create(:worldwide_organisation_page, edition: organisation) + after :create do |organisation, _evaluator| + organisation.pages = [ + build(:worldwide_organisation_page, corporate_information_page_type: CorporateInformationPageType::ComplaintsProcedure, edition: organisation), + build(:worldwide_organisation_page, corporate_information_page_type: CorporateInformationPageType::PersonalInformationCharter, edition: organisation), + build(:worldwide_organisation_page, corporate_information_page_type: CorporateInformationPageType::PublicationScheme, edition: organisation), + build(:worldwide_organisation_page, corporate_information_page_type: CorporateInformationPageType::Recruitment, edition: organisation), + build(:worldwide_organisation_page, corporate_information_page_type: CorporateInformationPageType::WelshLanguageScheme, edition: organisation), + ] end end end diff --git a/test/unit/app/models/editionable_worldwide_organisation_test.rb b/test/unit/app/models/editionable_worldwide_organisation_test.rb index a97cb9674ef..1195e82c882 100644 --- a/test/unit/app/models/editionable_worldwide_organisation_test.rb +++ b/test/unit/app/models/editionable_worldwide_organisation_test.rb @@ -187,7 +187,7 @@ class EditionableWorldwideOrganisationTest < ActiveSupport::TestCase published_worldwide_organisation = create( :editionable_worldwide_organisation, :published, - :with_pages, + :with_page, ) draft_worldwide_organisation = published_worldwide_organisation.create_draft(create(:writer)) From e89ab2a24f875e88511464983ed231fe40efb7f6 Mon Sep 17 00:00:00 2001 From: Joseph Kempster Date: Wed, 17 Apr 2024 14:06:07 +0100 Subject: [PATCH 3/6] Publish worldwide organisation pages associated with organisation The associated pages of an editionable worldwide organisation must be published. --- app/models/worldwide_organisation_page.rb | 12 +++ ...onable_worldwide_organisation_presenter.rb | 76 +++++++++++++++++++ ...e_worldwide_organisation_presenter_test.rb | 13 ++++ 3 files changed, 101 insertions(+) diff --git a/app/models/worldwide_organisation_page.rb b/app/models/worldwide_organisation_page.rb index eb9ee60acf2..0611e5506ee 100644 --- a/app/models/worldwide_organisation_page.rb +++ b/app/models/worldwide_organisation_page.rb @@ -29,6 +29,18 @@ def self.by_menu_heading(menu_heading) where(corporate_information_page_type_id: type_ids) end + def self.for_slug(slug) + if (type = CorporateInformationPageType.find(slug)) + find_by(corporate_information_page_type_id: type.id) + end + end + + def self.for_slug!(slug) + if (type = CorporateInformationPageType.find(slug)) + find_by!(corporate_information_page_type_id: type.id) + end + end + def publicly_visible? true end diff --git a/app/presenters/publishing_api/editionable_worldwide_organisation_presenter.rb b/app/presenters/publishing_api/editionable_worldwide_organisation_presenter.rb index 18032211872..9656538e9bd 100644 --- a/app/presenters/publishing_api/editionable_worldwide_organisation_presenter.rb +++ b/app/presenters/publishing_api/editionable_worldwide_organisation_presenter.rb @@ -29,6 +29,8 @@ def content crest: "single-identity", formatted_title: worldwide_organisation_logo_name(item), }, + ordered_corporate_information_pages:, + secondary_corporate_information_pages:, office_contact_associations:, people_role_associations:, social_media_links:, @@ -56,6 +58,7 @@ def edition_links secondary_role_person:, sponsoring_organisations: item.lead_organisations.map(&:content_id), world_locations: item.world_locations.map(&:content_id), + corporate_information_pages: pages, } end @@ -155,5 +158,78 @@ def world_location_names } end end + + def pages + return [] unless item.pages.any? + + item.pages.map(&:content_id) + end + + def ordered_corporate_information_pages + return [] unless item.pages + + links = [] + + %i[our_information jobs_and_contracts].each do |page_type| + item.pages.by_menu_heading(page_type).each do |page| + links << { + content_id: page.content_id, + title: page.title, + } + end + end + + links + end + + def secondary_corporate_information_pages + return [] unless item.pages + + sentences = [] + + publication_scheme = item.pages.find_by(corporate_information_page_type_id: CorporateInformationPageType::PublicationScheme.id) + if publication_scheme.present? + sentences << I18n.t( + "worldwide_organisation.corporate_information.publication_scheme_html", + link: t_corporate_information_page_link(item, "publication-scheme"), + ) + end + + welsh_language_scheme = item.pages.find_by(corporate_information_page_type_id: CorporateInformationPageType::WelshLanguageScheme.id) + if welsh_language_scheme.present? + sentences << I18n.t( + "worldwide_organisation.corporate_information.welsh_language_scheme_html", + link: t_corporate_information_page_link(item, "welsh-language-scheme"), + ) + end + + personal_information_charter = item.pages.find_by(corporate_information_page_type_id: CorporateInformationPageType::PersonalInformationCharter.id) + if personal_information_charter.present? + sentences << I18n.t( + "worldwide_organisation.corporate_information.personal_information_charter_html", + link: t_corporate_information_page_link(item, "personal-information-charter"), + ) + end + + sentences.join(" ") + end + + def t_corporate_information_page_link(organisation, slug) + page = organisation.pages.for_slug(slug) + page.extend(UseSlugAsParam) + link_to( + t_corporate_information_page_type_link_text(page), + page.public_path, + class: "govuk-link", + ) + end + + def t_corporate_information_page_type_link_text(page) + if I18n.exists?("corporate_information_page.type.link_text.#{page.display_type_key}") + I18n.t("corporate_information_page.type.link_text.#{page.display_type_key}") + else + I18n.t("corporate_information_page.type.title.#{page.display_type_key}") + end + end end end diff --git a/test/unit/app/presenters/publishing_api/editionable_worldwide_organisation_presenter_test.rb b/test/unit/app/presenters/publishing_api/editionable_worldwide_organisation_presenter_test.rb index b94a84f129a..14407f97df2 100644 --- a/test/unit/app/presenters/publishing_api/editionable_worldwide_organisation_presenter_test.rb +++ b/test/unit/app/presenters/publishing_api/editionable_worldwide_organisation_presenter_test.rb @@ -11,6 +11,7 @@ def present(...) :with_social_media_account, :with_main_office, :with_home_page_offices, + :with_pages, analytics_identifier: "WO123") primary_role = create(:ambassador_role) @@ -47,6 +48,17 @@ def present(...) crest: "single-identity", formatted_title: "Editionable
worldwide
organisation
title", }, + ordered_corporate_information_pages: [ + { + content_id: worldwide_org.pages[0].content_id, + title: "Complaints procedure", + }, + { + content_id: worldwide_org.pages[3].content_id, + title: "Working for Editionable worldwide organisation title", + }, + ], + secondary_corporate_information_pages: "Read about the types of information we routinely publish in our Publication scheme. Find out about our commitment to publishing in Welsh. Our Personal information charter explains how we treat your personal information.", office_contact_associations: [ { office_content_id: worldwide_org.reload.main_office.content_id, @@ -115,6 +127,7 @@ def present(...) ], sponsoring_organisations: worldwide_org.organisations.map(&:content_id), world_locations: worldwide_org.world_locations.map(&:content_id), + corporate_information_pages: worldwide_org.pages.map(&:content_id), }, analytics_identifier: "WO123", update_type: "major", From 217f3e2e86ce2d4e3193f1c8e5b1ab81d3a88fa1 Mon Sep 17 00:00:00 2001 From: Joseph Kempster Date: Thu, 18 Apr 2024 11:51:47 +0100 Subject: [PATCH 4/6] Publish worldwide organisation pages with organisation We want to present pages to Publishing API in sync with their editionable worldwide organisation. This copies the approach taken in #8904 to present offices along with organisations. --- .../editionable_worldwide_organisation.rb | 2 +- app/models/worldwide_organisation_page.rb | 1 + ...ublishing_api_associated_documents_test.rb | 104 +++++++++++++++--- 3 files changed, 93 insertions(+), 14 deletions(-) diff --git a/app/models/editionable_worldwide_organisation.rb b/app/models/editionable_worldwide_organisation.rb index f4ceb71bd56..384e0cc4884 100644 --- a/app/models/editionable_worldwide_organisation.rb +++ b/app/models/editionable_worldwide_organisation.rb @@ -183,6 +183,6 @@ def republish_dependent_documents end def associated_documents - (offices + offices.map(&:contact)).compact.flatten + [offices, offices.map(&:contact), pages].compact.flatten end end diff --git a/app/models/worldwide_organisation_page.rb b/app/models/worldwide_organisation_page.rb index 0611e5506ee..e87aeb9d027 100644 --- a/app/models/worldwide_organisation_page.rb +++ b/app/models/worldwide_organisation_page.rb @@ -10,6 +10,7 @@ class WorldwideOrganisationPage < ApplicationRecord delegate :slug, :display_type_key, to: :corporate_information_page_type + include HasContentId include Attachable def title(_locale = :en) diff --git a/test/unit/app/services/service_listeners/publishing_api_associated_documents_test.rb b/test/unit/app/services/service_listeners/publishing_api_associated_documents_test.rb index 6688cfc022e..29735bf59d0 100644 --- a/test/unit/app/services/service_listeners/publishing_api_associated_documents_test.rb +++ b/test/unit/app/services/service_listeners/publishing_api_associated_documents_test.rb @@ -203,8 +203,8 @@ class Publish < PublishingApiAssociatedDocumentsTest call(new_edition) end - test "with an office on a new editionable worldwide organisation publishes the office and it's contact" do - worldwide_organisation = create(:editionable_worldwide_organisation, :with_main_office) + test "with an office on a new editionable worldwide organisation publishes the office, it's contact and it's page" do + worldwide_organisation = create(:editionable_worldwide_organisation, :with_main_office, :with_page) PublishingApiWorker.any_instance.expects(:perform).with( "WorldwideOffice", @@ -220,6 +220,13 @@ class Publish < PublishingApiAssociatedDocumentsTest "en", ) + PublishingApiWorker.any_instance.expects(:perform).with( + "WorldwideOrganisationPage", + worldwide_organisation.pages.first.id, + "major", + "en", + ) + call(worldwide_organisation) end @@ -248,6 +255,26 @@ class Publish < PublishingApiAssociatedDocumentsTest call(new_edition) end + + test "with a page on the old version of an editionable worldwide organisation redirects the page" do + worldwide_organisation = create(:published_editionable_worldwide_organisation, :with_page) + + new_edition = worldwide_organisation.create_draft(create(:writer)) + new_edition.reload.pages.first.destroy! + new_edition.minor_change = true + new_edition.submit! + new_edition.publish! + + old_page = worldwide_organisation.pages.first + + PublishingApiRedirectWorker.any_instance.expects(:perform).with( + old_page.content_id, + new_edition.search_link, + "en", + ) + + call(new_edition) + end end class UpdateDraft < PublishingApiAssociatedDocumentsTest @@ -364,8 +391,9 @@ class Unpublish < PublishingApiAssociatedDocumentsTest end test "for an editionable worldwide organisation that has been consolidated publishes a redirect to the alternative url" do - worldwide_organisation = create(:unpublished_editionable_worldwide_organisation_consolidated, :with_main_office) + worldwide_organisation = create(:unpublished_editionable_worldwide_organisation_consolidated, :with_main_office, :with_page) office = worldwide_organisation.main_office + page = worldwide_organisation.pages.first PublishingApiRedirectWorker.any_instance.expects(:perform).with( office.content_id, @@ -381,6 +409,13 @@ class Unpublish < PublishingApiAssociatedDocumentsTest false, ) + PublishingApiRedirectWorker.any_instance.expects(:perform).with( + page.content_id, + "/government/another/page", + "en", + false, + ) + call(worldwide_organisation) end @@ -432,8 +467,9 @@ class Unpublish < PublishingApiAssociatedDocumentsTest test "for an editionable worldwide organisation that has been unpublished with an external redirect publishes a redirect to the alternative url" do external_url = "https://test.ukri.org/some-page" - worldwide_organisation = create(:unpublished_editionable_worldwide_organisation_in_error_redirect, :with_main_office, { unpublishing: build(:unpublishing, { redirect: true, alternative_url: external_url }) }) + worldwide_organisation = create(:unpublished_editionable_worldwide_organisation_in_error_redirect, :with_main_office, :with_page, { unpublishing: build(:unpublishing, { redirect: true, alternative_url: external_url }) }) office = worldwide_organisation.main_office + page = worldwide_organisation.pages.first PublishingApiRedirectWorker.any_instance.expects(:perform).with( office.content_id, @@ -449,6 +485,13 @@ class Unpublish < PublishingApiAssociatedDocumentsTest false, ) + PublishingApiRedirectWorker.any_instance.expects(:perform).with( + page.content_id, + external_url, + "en", + false, + ) + call(worldwide_organisation) end @@ -465,8 +508,9 @@ class Unpublish < PublishingApiAssociatedDocumentsTest end test "for an editionable worldwide organisation that has been unpublished without a redirect publishes a redirect to the parent docuemnt" do - worldwide_organisation = create(:unpublished_editionable_worldwide_organisation_in_error_no_redirect, :with_main_office) + worldwide_organisation = create(:unpublished_editionable_worldwide_organisation_in_error_no_redirect, :with_main_office, :with_page) office = worldwide_organisation.main_office + page = worldwide_organisation.pages.first PublishingApiRedirectWorker.any_instance.expects(:perform).with( office.content_id, @@ -482,6 +526,13 @@ class Unpublish < PublishingApiAssociatedDocumentsTest false, ) + PublishingApiRedirectWorker.any_instance.expects(:perform).with( + page.content_id, + worldwide_organisation.search_link, + "en", + false, + ) + call(worldwide_organisation) end @@ -508,8 +559,9 @@ class Withdraw < PublishingApiAssociatedDocumentsTest call(publication) end - test "for an editionable worldwide organisation that has been withdrawn publishes a withdrawal for the office" do - worldwide_organisation = create(:withdrawn_editionable_worldwide_organisation, :with_main_office) + test "for an editionable worldwide organisation that has been withdrawn publishes a withdrawal for the office and page" do + worldwide_organisation = create(:withdrawn_editionable_worldwide_organisation, :with_main_office, :with_page) + page = worldwide_organisation.pages.first PublishingApiWithdrawalWorker.any_instance.expects(:perform).with( worldwide_organisation.main_office.content_id, @@ -527,6 +579,14 @@ class Withdraw < PublishingApiAssociatedDocumentsTest worldwide_organisation.unpublishing.unpublished_at.to_s, ) + PublishingApiWithdrawalWorker.any_instance.expects(:perform).with( + page.content_id, + "content was withdrawn", + "en", + false, + worldwide_organisation.unpublishing.unpublished_at.to_s, + ) + call(worldwide_organisation) end @@ -569,8 +629,8 @@ class Delete < PublishingApiAssociatedDocumentsTest call(publication) end - test "for a draft editionable worldwide organisation with offices discards the draft" do - worldwide_organisation = create(:draft_editionable_worldwide_organisation, :with_main_office) + test "for a draft editionable worldwide organisation with offices and pages discards the draft" do + worldwide_organisation = create(:draft_editionable_worldwide_organisation, :with_main_office, :with_page) PublishingApiDiscardDraftWorker.expects(:perform_async).with( worldwide_organisation.main_office.content_id, @@ -582,6 +642,11 @@ class Delete < PublishingApiAssociatedDocumentsTest "en", ) + PublishingApiDiscardDraftWorker.expects(:perform_async).with( + worldwide_organisation.pages.first.content_id, + "en", + ) + call(worldwide_organisation) end @@ -611,8 +676,8 @@ class Republish < PublishingApiAssociatedDocumentsTest call(publication) end - test "for a draft editionable worldwide organisation with an office publishes the draft office" do - worldwide_organisation = create(:draft_editionable_worldwide_organisation, :with_main_office) + test "for a draft editionable worldwide organisation with an office and page publishes the draft office and page" do + worldwide_organisation = create(:draft_editionable_worldwide_organisation, :with_main_office, :with_page) Whitehall::PublishingApi.expects(:save_draft_translation).with( worldwide_organisation.main_office, @@ -626,6 +691,12 @@ class Republish < PublishingApiAssociatedDocumentsTest "republish", ) + Whitehall::PublishingApi.expects(:save_draft_translation).with( + worldwide_organisation.pages.first, + "en", + "republish", + ) + call(worldwide_organisation) end @@ -641,8 +712,8 @@ class Republish < PublishingApiAssociatedDocumentsTest call(publication) end - test "for a published editionable worldwide organisation with an office publishes the office" do - worldwide_organisation = create(:published_editionable_worldwide_organisation, :with_main_office) + test "for a published editionable worldwide organisation with an office and page publishes the office and page" do + worldwide_organisation = create(:published_editionable_worldwide_organisation, :with_main_office, :with_page) PublishingApiWorker.any_instance.expects(:perform).with( "WorldwideOffice", @@ -658,6 +729,13 @@ class Republish < PublishingApiAssociatedDocumentsTest "en", ) + PublishingApiWorker.any_instance.expects(:perform).with( + "WorldwideOrganisationPage", + worldwide_organisation.pages.first.id, + "republish", + "en", + ) + call(worldwide_organisation) end From 4e82c6c15721899d9cdaf41563196c1c995b83c4 Mon Sep 17 00:00:00 2001 From: Joseph Kempster Date: Wed, 17 Apr 2024 13:53:25 +0100 Subject: [PATCH 5/6] Republish draft when worldwide organisation page changes The pages of an editionabel worldwide organisation are surfaced in the content item in `details`. We must republish the draft worldwide organisation when there are any changes to the pages so that the draft correctly displays the pages. This copies the approach taken in #8904. --- app/models/worldwide_organisation_page.rb | 6 ++++++ .../worldwide_organisation_page_test.rb | 21 +++++++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/app/models/worldwide_organisation_page.rb b/app/models/worldwide_organisation_page.rb index e87aeb9d027..85a2cff107e 100644 --- a/app/models/worldwide_organisation_page.rb +++ b/app/models/worldwide_organisation_page.rb @@ -10,6 +10,8 @@ class WorldwideOrganisationPage < ApplicationRecord delegate :slug, :display_type_key, to: :corporate_information_page_type + after_commit :republish_worldwide_organisation_draft + include HasContentId include Attachable @@ -68,6 +70,10 @@ def public_url(options = {}) private + def republish_worldwide_organisation_draft + Whitehall.edition_services.draft_updater(edition).perform! if edition.present? + end + def unique_worldwide_organisation_and_page_type current_page_types = edition.pages.map(&:corporate_information_page_type_id).flatten duplicate_page = current_page_types.include?(corporate_information_page_type_id) diff --git a/test/unit/app/models/worldwide_organisation_page_test.rb b/test/unit/app/models/worldwide_organisation_page_test.rb index 04e2fd3a71b..083ba238e29 100644 --- a/test/unit/app/models/worldwide_organisation_page_test.rb +++ b/test/unit/app/models/worldwide_organisation_page_test.rb @@ -1,6 +1,27 @@ require "test_helper" class WorldwideOrganisationPageTest < ActiveSupport::TestCase + test "creating a new page republishes the associated worldwide organisation" do + worldwide_organisation = create(:editionable_worldwide_organisation) + Whitehall::PublishingApi.expects(:save_draft).with(worldwide_organisation).once + create(:worldwide_organisation_page, edition: worldwide_organisation) + end + + test "updating an existing page republishes the associated worldwide organisation" do + worldwide_organisation = create(:editionable_worldwide_organisation) + page = create(:worldwide_organisation_page, edition: worldwide_organisation) + page.body = "updated" + Whitehall::PublishingApi.expects(:save_draft).with(worldwide_organisation).once + page.save! + end + + test "deleting a page republishes the associated worldwide organisation" do + worldwide_organisation = create(:editionable_worldwide_organisation) + page = create(:worldwide_organisation_page, edition: worldwide_organisation) + Whitehall::PublishingApi.expects(:save_draft).with(worldwide_organisation).once + page.destroy! + end + %w[body corporate_information_page_type_id].each do |param| test "should not be valid without a #{param}" do assert_not build(:worldwide_organisation_page, param.to_sym => nil).valid? From 2f453caf49475005bab241480ebd07f478fe5a43 Mon Sep 17 00:00:00 2001 From: Joseph Kempster Date: Wed, 17 Apr 2024 14:02:51 +0100 Subject: [PATCH 6/6] Discard worldwide organisation page draft when destroyed We must discard the draft of an editionable worldwide organisation page when its model is destroyed so that the draft app is in sync with the model state. This copies the approach taken in #8904. --- app/models/worldwide_organisation_page.rb | 5 +++++ test/unit/app/models/worldwide_organisation_page_test.rb | 7 +++++++ 2 files changed, 12 insertions(+) diff --git a/app/models/worldwide_organisation_page.rb b/app/models/worldwide_organisation_page.rb index 85a2cff107e..0b9faf23934 100644 --- a/app/models/worldwide_organisation_page.rb +++ b/app/models/worldwide_organisation_page.rb @@ -11,6 +11,7 @@ class WorldwideOrganisationPage < ApplicationRecord delegate :slug, :display_type_key, to: :corporate_information_page_type after_commit :republish_worldwide_organisation_draft + after_destroy :discard_draft include HasContentId include Attachable @@ -74,6 +75,10 @@ def republish_worldwide_organisation_draft Whitehall.edition_services.draft_updater(edition).perform! if edition.present? end + def discard_draft + PublishingApiDiscardDraftWorker.perform_async(content_id, I18n.default_locale.to_s) + end + def unique_worldwide_organisation_and_page_type current_page_types = edition.pages.map(&:corporate_information_page_type_id).flatten duplicate_page = current_page_types.include?(corporate_information_page_type_id) diff --git a/test/unit/app/models/worldwide_organisation_page_test.rb b/test/unit/app/models/worldwide_organisation_page_test.rb index 083ba238e29..56928acd9c7 100644 --- a/test/unit/app/models/worldwide_organisation_page_test.rb +++ b/test/unit/app/models/worldwide_organisation_page_test.rb @@ -22,6 +22,13 @@ class WorldwideOrganisationPageTest < ActiveSupport::TestCase page.destroy! end + test "deleting a page discards the draft page" do + worldwide_organisation = create(:editionable_worldwide_organisation) + page = create(:worldwide_organisation_page, edition: worldwide_organisation) + PublishingApiDiscardDraftWorker.expects(:perform_async).with(page.content_id, "en").once + page.destroy! + end + %w[body corporate_information_page_type_id].each do |param| test "should not be valid without a #{param}" do assert_not build(:worldwide_organisation_page, param.to_sym => nil).valid?