Skip to content

Commit

Permalink
validate and then reset state for edition under review
Browse files Browse the repository at this point in the history
during the review stage, we want to check the
scheduled values from the previous page are valid,
but not yet put the edition into a scheduled or
published state.

here we validate there are no errors by updating
the edition, but then reset its state to `draft`
to enable us to then delete it if the user presses
`cancel` or change the values if the user were to
go backwards in the form.
  • Loading branch information
Harriethw committed Dec 11, 2024
1 parent fd09930 commit 67797cf
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,23 @@ def review_update_url
)
end

def validate_scheduled_edition
@content_block_edition.update!(scheduled_publication_params)
@content_block_edition.schedule!
end

def review_update
@content_block_edition = ContentBlockManager::ContentBlock::Edition.find(params[:id])

if params[:schedule_publishing].blank?
@content_block_edition.errors.add(:schedule_publishing, "cannot be blank")
raise ActiveRecord::RecordInvalid, @content_block_edition
elsif params[:schedule_publishing] == "schedule"
@content_block_edition.update!(scheduled_publication_params)
@content_block_edition.schedule!
raise ActiveRecord::RecordInvalid, @content_block_edition if @content_block_edition.errors.any?
validate_scheduled_edition
end

@url = review_update_url
@content_block_edition.draft!

render :review
rescue ActiveRecord::RecordInvalid
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ def valid_state?(state)
event :schedule do
transitions from: %i[draft], to: :scheduled
end
event :draft do
transitions from: %i[scheduled draft], to: :draft
end
event :supersede do
transitions from: %i[scheduled], to: :superseded
end
Expand Down
14 changes: 13 additions & 1 deletion lib/engines/content_block_manager/features/edit_object.feature
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Feature: Edit a content object
Then I should be taken back to the document page
And no draft Content Block Edition has been created

Scenario: GDS editor cancels the creation of an object before publishing
Scenario: GDS editor cancels the creation of an object before scheduling
When I visit the Content Block Manager home page
When I click to view the document
When I click the first edit link
Expand All @@ -58,6 +58,18 @@ Feature: Edit a content object
Then I should be taken back to the document page
And no draft Content Block Edition has been created

Scenario: GDS editor cancels the creation of an object before confirming answers
When I visit the Content Block Manager home page
When I click to view the document
When I click the first edit link
When I fill out the form
When I save and continue
When I choose to publish the change now
And I save and continue
And I click cancel
Then I am taken back to Content Block Manager home page
And no draft Content Block Edition has been created

Scenario: GDS editor sees validation errors for missing fields
And a schema "email_address" exists with the following fields:
| field | type | format | required |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,3 +124,11 @@ Feature: Schedule a content object
And I enter a date in the past
And I save and continue
Then I see the errors informing me the date must be in the future

Scenario: GDS Editor cancels after scheduling a block
When I am updating a content block
Then I am asked when I want to publish the change
When I choose to schedule the change
And the block is scheduled and published
And I click cancel
Then I am taken back to Content Block Manager home page
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,34 @@ class ContentBlockManager::WorkflowTest < ActiveSupport::TestCase
assert edition.draft?
end

test "editions can transition to draft from draft state" do
edition = create(:content_block_edition, document: create(:content_block_document, block_type: "email_address"))
edition.draft!
assert edition.draft?
end

test "editions can transition to draft from scheduled state" do
edition = create(
:content_block_edition,
document: create(:content_block_document, block_type: "email_address"),
scheduled_publication: 7.days.since(Time.zone.now).to_date,
state: "scheduled",
)
edition.draft!
assert edition.draft?
end

test "editions cannot transition to draft from published state" do
edition = create(
:content_block_edition,
document: create(:content_block_document, block_type: "email_address"),
state: "published",
)
assert_raises Transitions::InvalidTransition do
edition.draft!
end
end

test "publishing a draft edition transitions it into the published state" do
edition = create(:content_block_edition, document: create(:content_block_document, block_type: "email_address"))
edition.publish!
Expand Down

0 comments on commit 67797cf

Please sign in to comment.