From 4387232522732c8c130b5d35bce48bfd9d77bb87 Mon Sep 17 00:00:00 2001 From: Bruce Bolt Date: Thu, 15 Feb 2024 11:21:23 +0000 Subject: [PATCH] Add support for worldwide offices embedded in worldwide organisations As part of the switch to Worldwide Organisation content items containing the content for their offices, we need to switch the rendering of the office pages to use these parts. This makes that switch in a backward compatible way, so we can continue rendering office pages under the existing content items until the office routes are updated to point to the organisation's content item. In a later PR, we will remove `WorldwideOfficePresenter` and it's associated view. --- app/presenters/content_item_presenter.rb | 4 ++ ...worldwide_organisation_office_presenter.rb | 46 ++++++++++++ app/services/presenter_builder.rb | 5 ++ .../worldwide_organisation_office.html.erb | 35 ++++++++++ ...wide_organisation_office_presenter_test.rb | 70 +++++++++++++++++++ 5 files changed, 160 insertions(+) create mode 100644 app/presenters/worldwide_organisation_office_presenter.rb create mode 100644 app/views/content_items/worldwide_organisation_office.html.erb create mode 100644 test/presenters/worldwide_organisation_office_presenter_test.rb diff --git a/app/presenters/content_item_presenter.rb b/app/presenters/content_item_presenter.rb index 850db6aa7c..0354e9cd3d 100644 --- a/app/presenters/content_item_presenter.rb +++ b/app/presenters/content_item_presenter.rb @@ -110,6 +110,10 @@ def hmrc_manual_updates? view_context.request.path =~ /^\/hmrc-internal-manuals\/.*\/updates$/ && content_item["schema_name"] == "hmrc_manual" end + def worldwide_organisation_office? + view_context.request.path =~ /^\/world\/.*\/office\/.*$/ && content_item["schema_name"] == "worldwide_organisation" + end + def show_default_breadcrumbs? true end diff --git a/app/presenters/worldwide_organisation_office_presenter.rb b/app/presenters/worldwide_organisation_office_presenter.rb new file mode 100644 index 0000000000..fa0feb3709 --- /dev/null +++ b/app/presenters/worldwide_organisation_office_presenter.rb @@ -0,0 +1,46 @@ +class WorldwideOrganisationOfficePresenter < ContentItemPresenter + include ContentItem::ContentsList + include WorldwideOrganisation::Branding + + def formatted_title + worldwide_organisation&.formatted_title + end + + def body + office["access_and_opening_times"] + end + + def contact + associated_contact = content_item.dig("links", "contacts").select { |contact| + contact["content_id"] == office["contact_content_id"] + }.first + + WorldwideOrganisation::LinkedContactPresenter.new(associated_contact) + end + + def show_default_breadcrumbs? + false + end + + def office + all_offices = content_item.dig("details", "main_office_parts") + content_item.dig("details", "home_page_office_parts") + + all_offices.select { |office| + office["slug"] == requested_path.split("/").last + }.first + end + + def worldwide_organisation + WorldwideOrganisationPresenter.new(content_item, requested_path, view_context) + end + + def sponsoring_organisations + worldwide_organisation&.sponsoring_organisations + end + +private + + def show_contents_list? + true + end +end diff --git a/app/services/presenter_builder.rb b/app/services/presenter_builder.rb index 6ea7ef3a49..7bf1307523 100644 --- a/app/services/presenter_builder.rb +++ b/app/services/presenter_builder.rb @@ -37,6 +37,7 @@ def presenter_name return service_sign_in_presenter_name if service_sign_in_format? return "ManualUpdatesPresenter" if manual_updates? return "HmrcManualUpdatesPresenter" if hmrc_manual_updates? + return "WorldwideOrganisationOfficePresenter" if worldwide_organisation_office? "#{content_item['schema_name'].classify}Presenter" end @@ -49,6 +50,10 @@ def hmrc_manual_updates? view_context.request.path =~ /^\/hmrc-internal-manuals\/.*\/updates$/ && content_item["schema_name"] == "hmrc_manual" end + def worldwide_organisation_office? + view_context.request.path =~ /^\/world\/.*\/office\/.*$/ && content_item["schema_name"] == "worldwide_organisation" + end + def service_sign_in_format? content_item["schema_name"] == "service_sign_in" end diff --git a/app/views/content_items/worldwide_organisation_office.html.erb b/app/views/content_items/worldwide_organisation_office.html.erb new file mode 100644 index 0000000000..eac88d649f --- /dev/null +++ b/app/views/content_items/worldwide_organisation_office.html.erb @@ -0,0 +1,35 @@ +<%= render partial: "content_items/worldwide_organisation/header", locals: { + worldwide_organisation: @content_item.worldwide_organisation, +} %> + +
+
+ <%= render "govuk_publishing_components/components/contents_list", + contents: @content_item.contents, + underline_links: true + %> +
+ +
+
+ <%= render "govuk_publishing_components/components/heading", { + text: sanitize("About #{@content_item.contact.title}"), + heading_level: 2, + font_size: "xl", + margin_bottom: 4, + } %> + +
+
+ <%= render partial: "content_items/worldwide_organisation/contact", locals: { + contact: @content_item.contact, + hide_title: true, + } %> +
+ + <%= render "govuk_publishing_components/components/govspeak", {} do + raw(@content_item.body) + end %> +
+
+
diff --git a/test/presenters/worldwide_organisation_office_presenter_test.rb b/test/presenters/worldwide_organisation_office_presenter_test.rb new file mode 100644 index 0000000000..60ecf95b93 --- /dev/null +++ b/test/presenters/worldwide_organisation_office_presenter_test.rb @@ -0,0 +1,70 @@ +require "presenter_test_helper" + +class WorldwideOrganisationOfficePresenterTest < PresenterTestCase + def schema_name + "worldwide_organisation" + end + + def requested_path + "#{schema_item['base_path']}/office/#{schema_item.dig('details', 'main_office_parts', 0, 'slug')}" + end + + def present_example(example) + create_presenter( + "WorldwideOrganisationOfficePresenter".safe_constantize, + content_item: example, + requested_path:, + ) + end + + test "#title returns the title of the schema item" do + assert_equal schema_item["title"], presented_item.title + end + + test "#body returns the access and opening times of the schema item" do + assert_equal schema_item.dig("details", "main_office_parts", 0, "access_and_opening_times"), presented_item.body + end + + test "#contact returns the contact as an instance of #{WorldwideOrganisation::LinkedContactPresenter}" do + assert presented_item.contact.is_a?(WorldwideOrganisation::LinkedContactPresenter) + end + + test "#sponsoring_organisation_logo returns the logo details of the item" do + with_non_default_crest = schema_item + sponsoring_organisation = first_sponsoring_organisation(with_non_default_crest) + sponsoring_organisation["details"]["logo"]["crest"] = "dbt" + + presented = present_example(with_non_default_crest) + + expected = { name: "British Deputy High Commission
Hyderabad", url: "/world/uk-embassy-in-country", crest: "dbt", brand: "foreign-commonwealth-development-office" } + assert_equal expected, presented.organisation_logo + end + + test "#sponsoring_organisation_logo returns default values when the crest and brand of the first sponsoring organisation are blank" do + with_empty_logo = schema_item + sponsoring_organisation = first_sponsoring_organisation(with_empty_logo) + sponsoring_organisation["details"]["logo"]["crest"] = nil + sponsoring_organisation["details"]["brand"] = nil + + presented = present_example(with_empty_logo) + + expected = { name: "British Deputy High Commission
Hyderabad", url: "/world/uk-embassy-in-country", crest: "single-identity", brand: "single-identity" } + assert_equal expected, presented.organisation_logo + end + + test "#sponsoring_organisation_logo returns default values when the sponsoring organisations are nil" do + without_sponsoring_organisations = schema_item + without_sponsoring_organisations["links"].delete("sponsoring_organisations") + + presented = present_example(without_sponsoring_organisations) + + expected = { name: "British Deputy High Commission
Hyderabad", url: "/world/uk-embassy-in-country", crest: "single-identity", brand: "single-identity" } + assert_equal expected, presented.organisation_logo + end + +private + + def first_sponsoring_organisation(item) + item["links"]["sponsoring_organisations"][0] + end +end