From 711ce3c33a76520f6dfda92f36ccb7c14cdd58fe Mon Sep 17 00:00:00 2001 From: Peter Hartshorn Date: Fri, 25 Feb 2022 13:17:18 +0000 Subject: [PATCH] Add attachment arguments These are needed to render the new accessible form link from GOVUK Publishing Components. This is dependant on the component being updated first [1]. This change will apply to publications and consultations, which use the attachments partial. Initially, the new flow of requesting accessible formats will be a pilot and only available for documents, owned by a few organisations. This code will not need to be changed when the change is released to all orgs. [1]: https://github.com/alphagov/govuk_publishing_components/pull/2636 --- app/presenters/content_item/attachments.rb | 8 ++- .../content_item/attachments_test.rb | 49 +++++++++++++++++++ .../attachments.html.erb_test.rb | 15 ++++-- 3 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 test/presenters/content_item/attachments_test.rb diff --git a/app/presenters/content_item/attachments.rb b/app/presenters/content_item/attachments.rb index ca26c41cc..5842ce6da 100644 --- a/app/presenters/content_item/attachments.rb +++ b/app/presenters/content_item/attachments.rb @@ -1,9 +1,15 @@ module ContentItem module Attachments def attachment_details(attachment_id) - content_item.dig("details", "attachments")&.find do |attachment| + found_attachment = content_item.dig("details", "attachments")&.find do |attachment| attachment["id"] == attachment_id end + + return unless found_attachment + + found_attachment["attachment_id"] = found_attachment.delete("id") + found_attachment["owning_document_content_id"] = content_item["content_id"] + found_attachment end end end diff --git a/test/presenters/content_item/attachments_test.rb b/test/presenters/content_item/attachments_test.rb new file mode 100644 index 000000000..68bd5596f --- /dev/null +++ b/test/presenters/content_item/attachments_test.rb @@ -0,0 +1,49 @@ +require "test_helper" + +class ContentItemAttachmentsTest < ActiveSupport::TestCase + class DummyContentItem + include ContentItem::Attachments + attr_reader :content_item, :attachment_id, :content_id + + def initialize + @attachment_id = 1234 + @content_id = SecureRandom.uuid + @content_item = { + "content_id" => content_id, + "details" => { + "attachments" => [ + { + "id" => attachment_id, + "title" => "Some title", + "url" => "some/url", + "alternative_format_contact_email" => "alternative.formats@education.gov.uk", + }, + ], + }, + } + end + end + + test "returns attachment details for found attachment" do + dummy = DummyContentItem.new + + details = dummy.attachment_details(dummy.attachment_id) + + assert_equal details, { + "title" => "Some title", + "url" => "some/url", + "alternative_format_contact_email" => "alternative.formats@education.gov.uk", + "attachment_id" => dummy.attachment_id, + "owning_document_content_id" => dummy.content_id, + } + end + + test "returns nil if no attachment found" do + attachment_id = 4321 + dummy = DummyContentItem.new + + details = dummy.attachment_details(attachment_id) + + assert_equal details, nil + end +end diff --git a/test/views/content_items/attachments.html.erb_test.rb b/test/views/content_items/attachments.html.erb_test.rb index 913d549ba..55fbee79f 100644 --- a/test/views/content_items/attachments.html.erb_test.rb +++ b/test/views/content_items/attachments.html.erb_test.rb @@ -14,9 +14,17 @@ class ContentItemsAttachmentsTest < ActionView::TestCase test "can render attachments using their metadata" do @content_item = PublicationPresenter.new( - { "details" => { "attachments" => [{ "id" => "attachment_id", - "title" => "Some title", - "url" => "some/url" }] } }, + { + "content_id" => "doc_content_id", + "details" => { + "attachments" => [ + { "id" => "attachment_id", + "title" => "Some title", + "url" => "some/url", + "alternative_format_contact_email" => "alternative.formats@education.gov.uk" }, + ], + }, + }, "/publication", ApplicationController.new.view_context, ) @@ -30,6 +38,7 @@ class ContentItemsAttachmentsTest < ActionView::TestCase assert_includes rendered, "gem-c-attachment" assert_includes rendered, "Some title" + assert_includes rendered, "href=\"/contact/govuk/request-accessible-format?content_id=doc_content_id&attachment_id=attachment_id" end test "it prioritises pre-rendered attachments" do