diff --git a/app/models/workflow.rb b/app/models/workflow.rb index 9616ba4e6..077a4b478 100644 --- a/app/models/workflow.rb +++ b/app/models/workflow.rb @@ -31,7 +31,7 @@ class CannotDeletePublishedPublication < RuntimeError; end edition.reviewer = nil end - after_transition on: :publish do |edition, _transition| + before_transition on: :publish do |edition, _transition| edition.was_published end diff --git a/test/models/workflow_test.rb b/test/models/workflow_test.rb index 449b387fe..49034021b 100644 --- a/test/models/workflow_test.rb +++ b/test/models/workflow_test.rb @@ -1,4 +1,5 @@ require "test_helper" +require "minitest/autorun" class WorkflowTest < ActiveSupport::TestCase def setup @@ -521,4 +522,19 @@ def template_user_and_published_transaction assert_equal "schedule_for_publishing", edition.actions.last.request_type end end + + context "#publish" do + setup do + @user = FactoryBot.create(:user, :govuk_editor) + end + + should "not be in published state if the transition fails" do + edition = FactoryBot.create(:edition, state: "ready") + raise_exception = -> { raise "something went wrong with the publish transition" } + edition.stub :was_published, raise_exception do + assert_raises(RuntimeError) { publish(@user, edition, "this should fail") } + end + assert_equal "ready", edition.state + end + end end