diff --git a/app/workers/publishing_api_document_republishing_worker.rb b/app/workers/publishing_api_document_republishing_worker.rb index 58e4d735522..9e822dd4a7b 100644 --- a/app/workers/publishing_api_document_republishing_worker.rb +++ b/app/workers/publishing_api_document_republishing_worker.rb @@ -57,7 +57,7 @@ def the_document_has_non_superseded_editions_to_republish? end def the_document_has_been_unpublished? - @latest_unpublished_edition.present? + latest_unpublished_edition.present? end def the_document_has_been_withdrawn? @@ -113,12 +113,6 @@ def send_unpublish(edition) handle_attachments_for(edition) end - def locales_for(edition) - Whitehall::PublishingApi.locales_for(edition).each do |locale| - yield locale.to_s - end - end - def handle_attachments_for(edition) ServiceListeners::PublishingApiAssociatedDocuments.process( edition, diff --git a/test/unit/app/workers/publishing_api_document_republishing_worker_test.rb b/test/unit/app/workers/publishing_api_document_republishing_worker_test.rb index b3af4e0b54f..31434503c6d 100644 --- a/test/unit/app/workers/publishing_api_document_republishing_worker_test.rb +++ b/test/unit/app/workers/publishing_api_document_republishing_worker_test.rb @@ -2,16 +2,108 @@ require "gds_api/test_helpers/publishing_api" class PublishingApiDocumentRepublishingWorkerTest < ActiveSupport::TestCase - test "should ignore old superseded editions when doing bulk republishing" do - document = create(:document, editions: [build(:superseded_edition)]) + extend Minitest::Spec::DSL - Whitehall::PublishingApi.expects(:publish).never - Whitehall::PublishingApi.expects(:save_draft).never - Whitehall::PublishingApi.expects(:locales_for).never - Whitehall::PublishingApi.expects(:patch_links).never - PublishingApiUnpublishingWorker.any_instance.expects(:perform).never - ServiceListeners::PublishingApiAssociatedDocuments.expects(:process).never + context "#perform" do + test "does nothing when the document only has superseded editions" do + document = create(:document, editions: [build(:superseded_edition)]) - PublishingApiDocumentRepublishingWorker.new.perform(document.id) + Whitehall::PublishingApi.expects(:publish).never + Whitehall::PublishingApi.expects(:save_draft).never + Whitehall::PublishingApi.expects(:patch_links).never + PublishingApiUnpublishingWorker.any_instance.expects(:perform).never + ServiceListeners::PublishingApiAssociatedDocuments.expects(:process).never + + PublishingApiDocumentRepublishingWorker.new.perform(document.id) + end + + context "when there are non-superseded editions" do + test "unpublishes the latest unpublished edition when the document has been unpublished" do + first_unpublished_edition = build(:unpublished_edition) + last_unpublished_edition = build(:unpublished_edition) + document = create(:document, editions: [first_unpublished_edition, last_unpublished_edition]) + + PublishingApiUnpublishingWorker.any_instance.expects(:perform).with(last_unpublished_edition.unpublishing.id, last_unpublished_edition.draft?).once + ServiceListeners::PublishingApiAssociatedDocuments.expects(:process).with(last_unpublished_edition, "republish").once + Whitehall::PublishingApi.expects(:save_draft).never + Whitehall::PublishingApi.expects(:publish).never + Whitehall::PublishingApi.expects(:patch_links).never + + PublishingApiDocumentRepublishingWorker.new.perform(document.id) + end + + test "unpublishes the latest unpublished edition and draft edition when the document has been unpublished and there's also a draft edition" do + first_unpublished_edition = build(:unpublished_edition) + last_unpublished_edition = build(:unpublished_edition) + draft_edition = build(:draft_edition) + document = create(:document, editions: [first_unpublished_edition, last_unpublished_edition, draft_edition]) + + PublishingApiUnpublishingWorker.any_instance.expects(:perform).with(last_unpublished_edition.unpublishing.id, last_unpublished_edition.draft?).once + ServiceListeners::PublishingApiAssociatedDocuments.expects(:process).with(last_unpublished_edition, "republish").once + Whitehall::PublishingApi.expects(:save_draft).with(draft_edition, "republish", bulk_publishing: false).once + ServiceListeners::PublishingApiAssociatedDocuments.expects(:process).with(draft_edition, "republish").once + Whitehall::PublishingApi.expects(:publish).never + Whitehall::PublishingApi.expects(:patch_links).never + + PublishingApiDocumentRepublishingWorker.new.perform(document.id) + end + + test "republishes then unpublishes the live edition when the document has not been unpublished but has been withdrawn" do + live_withdrawn_edition = build(:withdrawn_edition) + document = create(:document, live_edition: live_withdrawn_edition, editions: [live_withdrawn_edition]) + + Whitehall::PublishingApi.expects(:publish).with(live_withdrawn_edition, "republish", bulk_publishing: false).once + ServiceListeners::PublishingApiAssociatedDocuments.expects(:process).with(live_withdrawn_edition, "republish").once + PublishingApiUnpublishingWorker.any_instance.expects(:perform).with(live_withdrawn_edition.unpublishing.id, live_withdrawn_edition.draft?).once + ServiceListeners::PublishingApiAssociatedDocuments.expects(:process).with(live_withdrawn_edition, "republish").once + Whitehall::PublishingApi.expects(:save_draft).never + Whitehall::PublishingApi.expects(:patch_links).never + + PublishingApiDocumentRepublishingWorker.new.perform(document.id) + end + + context "when the document has not been unpublished or withdrawn" do + test "patches links then republishes the draft edition when there's only a draft edition" do + draft_edition = build(:draft_edition) + document = create(:document, editions: [draft_edition]) + + Whitehall::PublishingApi.expects(:patch_links).with(draft_edition, bulk_publishing: false).once + Whitehall::PublishingApi.expects(:save_draft).with(draft_edition, "republish", bulk_publishing: false).once + ServiceListeners::PublishingApiAssociatedDocuments.expects(:process).with(draft_edition, "republish").once + Whitehall::PublishingApi.expects(:publish).never + PublishingApiUnpublishingWorker.any_instance.expects(:perform).never + + PublishingApiDocumentRepublishingWorker.new.perform(document.id) + end + + test "patches links then republishes the live edition when there's only a live edition" do + live_edition = build(:published_edition) + document = create(:document, live_edition:, editions: [live_edition]) + + Whitehall::PublishingApi.expects(:patch_links).with(live_edition, bulk_publishing: false).once + Whitehall::PublishingApi.expects(:publish).with(live_edition, "republish", bulk_publishing: false).once + ServiceListeners::PublishingApiAssociatedDocuments.expects(:process).with(live_edition, "republish").once + Whitehall::PublishingApi.expects(:save_draft).never + PublishingApiUnpublishingWorker.any_instance.expects(:perform).never + + PublishingApiDocumentRepublishingWorker.new.perform(document.id) + end + + test "patches links with the live edition then republishes the live edition then republishes the draft edition when there's both a live and draft edition" do + live_edition = build(:published_edition) + draft_edition = build(:draft_edition) + document = create(:document, live_edition:, editions: [live_edition, draft_edition]) + + Whitehall::PublishingApi.expects(:patch_links).with(live_edition, bulk_publishing: false).once + Whitehall::PublishingApi.expects(:publish).with(live_edition, "republish", bulk_publishing: false).once + ServiceListeners::PublishingApiAssociatedDocuments.expects(:process).with(live_edition, "republish").once + Whitehall::PublishingApi.expects(:save_draft).with(draft_edition, "republish", bulk_publishing: false).once + ServiceListeners::PublishingApiAssociatedDocuments.expects(:process).with(draft_edition, "republish").once + PublishingApiUnpublishingWorker.any_instance.expects(:perform).never + + PublishingApiDocumentRepublishingWorker.new.perform(document.id) + end + end + end end end