Skip to content

Commit

Permalink
Merge pull request #8403 from alphagov/fix-attachment-redirects-when-…
Browse files Browse the repository at this point in the history
…draft-exists

Fix unpublished edition attachment redirects when draft edition exists
  • Loading branch information
ryanb-gds authored Oct 24, 2023
2 parents 31a56bf + e4f0b51 commit fef7348
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 3 deletions.
10 changes: 9 additions & 1 deletion app/models/attachment_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def draft?

delegate :access_limited_object, to: :last_attachable

delegate :unpublished?, to: :last_attachable
delegate :unpublished?, to: :unpublished_attachable

def replaced?
replaced_by.present?
Expand Down Expand Up @@ -151,6 +151,10 @@ def last_attachable
last_attachment.attachable || Attachable::Null.new
end

def unpublished_attachable
unpublished_attachment&.attachable || Attachable::Null.new
end

def significant_attachment(**args)
last_publicly_visible_attachment || last_attachment(**args)
end
Expand All @@ -159,6 +163,10 @@ def last_attachment(**args)
filtered_attachments(**args).last || Attachment::Null.new
end

def unpublished_attachment
attachments.reverse.detect { |a| a.attachable&.unpublished? }
end

def last_publicly_visible_attachment
attachments.reverse.detect { |a| (a.attachable || Attachable::Null.new).publicly_visible? }
end
Expand Down
2 changes: 1 addition & 1 deletion app/services/asset_manager/attachment_updater.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def self.get_link_header(attachment_data)
def self.get_redirect_url(attachment_data)
return nil unless attachment_data.unpublished?

attachment_data.last_attachable.unpublishing.document_url
attachment_data.unpublished_attachable.unpublishing.document_url
end

def self.get_replacement_id(replaced_attachment_data, variant)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,17 @@ class AttachmentRedirectDueToUnpublishingIntegrationTest < ActionDispatch::Integ
end
end

context "given a published document with file attachment and a draft" do
let(:edition) { create(:published_news_article) }
let!(:draft) { edition.create_draft(edition.creator) }

it "sets redirect URL for attachment in Asset Manager when document is unpublished" do
visit admin_news_article_path(edition)
unpublish_document_published_in_error
assert_sets_redirect_url_in_asset_manager_to redirect_url
end
end

context "given a published document with HTML attachment" do
let(:edition) { create(:published_publication, :with_html_attachment) }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ class AssetManager::AttachmentUpdaterTest < ActiveSupport::TestCase

before do
attachment_data.stubs(:unpublished?).returns(unpublished)
attachment_data.stubs(:last_attachable).returns(unpublished_edition)
attachment_data.stubs(:unpublished_attachable).returns(unpublished_edition)
end

it "updates redirect URL for all assets" do
Expand Down

0 comments on commit fef7348

Please sign in to comment.