From 921c6c4f87e834b3f1a7b5dbe9f1ed03aaf61026 Mon Sep 17 00:00:00 2001 From: Keith Lawrence Date: Mon, 17 Jun 2024 17:02:09 +0100 Subject: [PATCH] Maintain lead organisations first when publishing HTML attachments - add test for ordering. - update existing test to handle new mock requirements - add delegation for call_for_evidence and consultation_response so that they can retrieve lead and supporting organisations separately to pass to presenter. - add tests for delegated methods --- app/models/call_for_evidence_response.rb | 4 +++ app/models/consultation_response.rb | 4 +++ .../html_attachment_presenter.rb | 10 +++++++- .../models/call_for_evidence_response_test.rb | 10 ++++++++ .../app/models/consultation_response_test.rb | 10 ++++++++ .../html_attachment_presenter_test.rb | 25 ++++++++++++++++++- 6 files changed, 61 insertions(+), 2 deletions(-) diff --git a/app/models/call_for_evidence_response.rb b/app/models/call_for_evidence_response.rb index 87670f5d93f..796ab474d56 100644 --- a/app/models/call_for_evidence_response.rb +++ b/app/models/call_for_evidence_response.rb @@ -20,6 +20,10 @@ def access_limited_object delegate :organisations, to: :parent_attachable + delegate :lead_organisations, to: :parent_attachable + + delegate :supporting_organisations, to: :parent_attachable + delegate :alternative_format_contact_email, to: :call_for_evidence delegate :publicly_visible?, to: :parent_attachable diff --git a/app/models/consultation_response.rb b/app/models/consultation_response.rb index 37ecd229691..1253d604359 100644 --- a/app/models/consultation_response.rb +++ b/app/models/consultation_response.rb @@ -20,6 +20,10 @@ def access_limited_object delegate :organisations, to: :parent_attachable + delegate :lead_organisations, to: :parent_attachable + + delegate :supporting_organisations, to: :parent_attachable + delegate :alternative_format_contact_email, to: :consultation delegate :publicly_visible?, to: :parent_attachable diff --git a/app/presenters/publishing_api/html_attachment_presenter.rb b/app/presenters/publishing_api/html_attachment_presenter.rb index b0f67ed195f..d8f3dde2ec6 100644 --- a/app/presenters/publishing_api/html_attachment_presenter.rb +++ b/app/presenters/publishing_api/html_attachment_presenter.rb @@ -38,7 +38,7 @@ def links def edition_links { parent: parent_content_ids, # please use the breadcrumb component when migrating document_type to government-frontend - organisations: parent.organisations.pluck(:content_id).uniq, + organisations: (lead_org_ids + supporting_org_ids).uniq, primary_publishing_organisation:, government: government_id, } @@ -95,6 +95,14 @@ def lead_org_id parent.try(:lead_organisations).try(:first).try(:content_id) end + def lead_org_ids + (parent.try(:lead_organisations) || []).pluck(:content_id) + end + + def supporting_org_ids + (parent.try(:supporting_organisations) || []).pluck(:content_id) + end + def first_org_id parent.try(:organisations).try(:first).try(:content_id) end diff --git a/test/unit/app/models/call_for_evidence_response_test.rb b/test/unit/app/models/call_for_evidence_response_test.rb index 1de7cf455fe..6638d7d40f1 100644 --- a/test/unit/app/models/call_for_evidence_response_test.rb +++ b/test/unit/app/models/call_for_evidence_response_test.rb @@ -141,4 +141,14 @@ class CallForEvidenceResponseTest < ActiveSupport::TestCase assert_equal [], response.organisations end + + test "delegates lead_organisations and supporting_organisations to the parent" do + lead_organisation = create(:organisation) + supporting_organisation = create(:organisation) + call_for_evidence = create(:call_for_evidence, lead_organisations: [lead_organisation], supporting_organisations: [supporting_organisation]) + response = build(:call_for_evidence_outcome, call_for_evidence:) + + assert_equal [lead_organisation], response.lead_organisations + assert_equal [supporting_organisation], response.supporting_organisations + end end diff --git a/test/unit/app/models/consultation_response_test.rb b/test/unit/app/models/consultation_response_test.rb index f6453c2164d..3082c2f04af 100644 --- a/test/unit/app/models/consultation_response_test.rb +++ b/test/unit/app/models/consultation_response_test.rb @@ -142,6 +142,16 @@ class ConsultationResponseTest < ActiveSupport::TestCase assert_equal [], response.organisations end + test "delegates lead_organisations and supporting_organisations to the parent" do + lead_organisation = create(:organisation) + supporting_organisation = create(:organisation) + consultation = create(:consultation, lead_organisations: [lead_organisation], supporting_organisations: [supporting_organisation]) + response = build(:consultation_outcome, consultation:) + + assert_equal [lead_organisation], response.lead_organisations + assert_equal [supporting_organisation], response.supporting_organisations + end + test "allows HTML attachments" do outcome = build(:consultation_outcome) assert outcome.allows_html_attachments? diff --git a/test/unit/app/presenters/publishing_api/html_attachment_presenter_test.rb b/test/unit/app/presenters/publishing_api/html_attachment_presenter_test.rb index 8cf65f94bfb..24973f3cdac 100644 --- a/test/unit/app/presenters/publishing_api/html_attachment_presenter_test.rb +++ b/test/unit/app/presenters/publishing_api/html_attachment_presenter_test.rb @@ -87,11 +87,34 @@ def present(record) html_attachment = HtmlAttachment.last # if an organisation has multiple translations, pluck returns # duplicate content_ids because it constructs a left outer join - html_attachment.attachable.organisations.expects(:pluck).with(:content_id).returns(%w[abcdef abcdef]) + lead_orgs = mock + lead_orgs.expects(:pluck).with(:content_id).returns(%w[abcdef]) + html_attachment.attachable.expects(:lead_organisations).returns(lead_orgs).twice + + supporting_orgs = mock + supporting_orgs.expects(:pluck).with(:content_id).returns(%w[abcdef]) + html_attachment.attachable.expects(:supporting_organisations).returns(supporting_orgs) assert_equal %w[abcdef], present(html_attachment).links[:organisations] end + test "HtmlAttachment presents lead organisation content_ids before supporting organisation content_ids" do + create(:publication, :with_html_attachment, :published) + + html_attachment = HtmlAttachment.last + # if an organisation has multiple translations, pluck returns + # duplicate content_ids because it constructs a left outer join + lead_orgs = mock + lead_orgs.expects(:pluck).with(:content_id).returns(%w[abcdef]) + html_attachment.attachable.expects(:lead_organisations).returns(lead_orgs).twice + + supporting_orgs = mock + supporting_orgs.expects(:pluck).with(:content_id).returns(%w[bcdefg]) + html_attachment.attachable.expects(:supporting_organisations).returns(supporting_orgs) + + assert_equal %w[abcdef bcdefg], present(html_attachment).links[:organisations] + end + test "HtmlAttachment presents primary_publishing_organisation" do create(:publication, :with_html_attachment, :published)