Skip to content

Commit

Permalink
Merge pull request #2355 from alphagov/allow-unpublishing-when-manual…
Browse files Browse the repository at this point in the history
…-is-withdrawn

Allow unpublsihing when a manual state is withdrawn
  • Loading branch information
lauraghiorghisor-tw authored Apr 5, 2024
2 parents 3d1f427 + 3899adf commit 875f3ef
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 17 deletions.
12 changes: 5 additions & 7 deletions app/lib/withdraw_and_redirect_manual.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,17 @@ def execute

return if dry_run

published_manual = manual.current_versions[:published]

raise ManualNotPublishedError, manual.slug if published_manual.blank?

published_manual.withdraw
published_manual.save!(user)
raise ManualNotPublishedError, manual.slug unless manual.can_withdraw?

Adapters.publishing.unpublish_and_redirect_manual_and_sections(
published_manual,
manual,
redirect:,
include_sections:,
discard_drafts:,
)

manual.withdraw
manual.save!(user)
end

private
Expand Down
4 changes: 4 additions & 0 deletions app/models/manual.rb
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,10 @@ def withdrawn?
state == "withdrawn"
end

def can_withdraw?
current_versions[:published] || withdrawn?
end

def has_ever_been_published?
@ever_been_published
end
Expand Down
33 changes: 23 additions & 10 deletions spec/lib/withdraw_and_redirect_manual_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,30 +33,43 @@
allow(publishing_adapter).to receive(:unpublish_and_redirect_manual_and_sections)
end

it "withdraws the manual" do
it "withdraws the manual and unpublishes" do
subject.execute

reloaded_manual = Manual.find(manual.id, user)
expect(reloaded_manual.withdrawn?).to eq(true)
end

it "calls the publishing adapter to unpublish the manual" do
subject.execute
expect(publishing_adapter).to have_received(:unpublish_and_redirect_manual_and_sections)
.with(instance_of(Manual),
redirect:,
include_sections:,
discard_drafts:)
.with(instance_of(Manual),
redirect:,
include_sections:,
discard_drafts:)
end

context "when there is no published manual" do
context "when the manual is in draft" do
let(:state) { "draft" }

it "raises an error" do
expect { subject.execute }.to raise_error(WithdrawAndRedirectManual::ManualNotPublishedError)
end
end

context "when the manual is withdrawn" do
it "withdraws and unpublishes the manual again" do
manual.withdraw
manual.save!(user)

subject.execute

reloaded_manual = Manual.find(manual.id, user)
expect(reloaded_manual.withdrawn?).to eq(true)
expect(publishing_adapter).to have_received(:unpublish_and_redirect_manual_and_sections)
.with(instance_of(Manual),
redirect:,
include_sections:,
discard_drafts:)
end
end

context "when a dry run is flagged" do
let(:dry_run) { true }

Expand Down

0 comments on commit 875f3ef

Please sign in to comment.