diff --git a/app/controllers/downtimes_controller.rb b/app/controllers/downtimes_controller.rb index 99b8b27c0..e828cc646 100644 --- a/app/controllers/downtimes_controller.rb +++ b/app/controllers/downtimes_controller.rb @@ -1,7 +1,7 @@ class DowntimesController < ApplicationController before_action :require_govuk_editor before_action :load_edition - before_action :process_params, only: %i[create] + before_action :process_params, only: %i[create update] def new @downtime = Downtime.new(artefact: @edition.artefact) @@ -18,6 +18,26 @@ def create end end + def edit + @downtime = Downtime.for(@edition.artefact) + end + + def update + @downtime = Downtime.for(@edition.artefact) + + if params["commit"] == "Cancel downtime" + DowntimeRemover.destroy_immediately(@downtime) + flash[:success] = "#{edition_link} downtime message cancelled".html_safe + redirect_to downtimes_path + elsif @downtime.update(downtime_params) + DowntimeScheduler.schedule_publish_and_expiry(@downtime) + flash[:success] = "#{edition_link} downtime message re-scheduled (from #{view_context.downtime_datetime(@downtime)})".html_safe + redirect_to downtimes_path + else + render :edit + end + end + private def downtime_params diff --git a/app/views/downtimes/edit.html.erb b/app/views/downtimes/edit.html.erb new file mode 100644 index 000000000..9d263c669 --- /dev/null +++ b/app/views/downtimes/edit.html.erb @@ -0,0 +1,19 @@ +<% content_for :page_title, 'Re-schedule downtime message' %> + + + +
+

+ <%= @downtime.artefact.name %> + Re-schedule downtime message +

+
+ +<%= form_for @downtime, url: edition_downtime_path(@edition), html: { class: 'form well remove-top-margin', 'data-module': 'downtime-message' } do |f| %> + <%= render 'form', f: f %> + <%= f.submit 'Re-schedule downtime message', class: 'js-submit btn btn-success' %> + <%= f.submit 'Cancel downtime', class: 'add-left-margin btn btn-danger' %> +<% end %> diff --git a/config/features.rb b/config/features.rb index 52fc88a34..6976dc26e 100644 --- a/config/features.rb +++ b/config/features.rb @@ -16,4 +16,8 @@ feature :design_system_downtime_new, default: false, description: "A transition of the add downtime page to use the GOV.UK Design System" + + feature :design_system_downtime_edit, + default: false, + description: "A transition of the edit downtime page to the GOV.UK Design System" end diff --git a/config/routes.rb b/config/routes.rb index e7dd1626f..7f51b36b1 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -45,6 +45,9 @@ constraints FeatureConstraint.new("design_system_downtime_new") do resource :downtime, only: %i[new create] end + constraints FeatureConstraint.new("design_system_downtime_edit") do + resource :downtime, only: %i[edit update] + end resource :downtime, only: %i[new create edit update destroy], controller: "legacy_downtimes" end diff --git a/test/functional/downtimes_controller_test.rb b/test/functional/downtimes_controller_test.rb index 745d5459d..d3c9dc571 100644 --- a/test/functional/downtimes_controller_test.rb +++ b/test/functional/downtimes_controller_test.rb @@ -50,6 +50,51 @@ class DowntimesControllerTest < ActionController::TestCase end end + context "#edit" do + should "render the page ok" do + create_downtime + get :edit, params: { edition_id: edition.id } + assert_response :ok + end + end + + context "#update" do + context "cancelling scheduled downtime" do + should "invoke the DowntimeRemover" do + DowntimeRemover.expects(:destroy_immediately).with(downtime) + put :update, params: { edition_id: edition.id, downtime: downtime_params, commit: "Cancel downtime" } + end + + should "redirect to the downtime index" do + DowntimeRemover.stubs(:destroy_immediately) + put :update, params: { edition_id: edition.id, downtime: downtime_params, commit: "Cancel downtime" } + assert_redirected_to controller: "legacy_downtimes", action: "index" + end + end + + context "rescheduling planned downtime" do + should "schedule the changes for publication and expiration" do + DowntimeScheduler.expects(:schedule_publish_and_expiry).with(downtime) + put :update, params: { edition_id: edition.id, downtime: downtime_params, commit: "Re-schedule downtime message" } + end + + should "redirect to the downtime index" do + create_downtime + DowntimeScheduler.stubs(:schedule_publish_and_expiry) + put :update, params: { edition_id: edition.id, downtime: downtime_params, commit: "Re-schedule downtime message" } + assert_redirected_to controller: "legacy_downtimes", action: "index" + end + end + + context "with invalid form data" do + should "rerender the page" do + create_downtime + put :update, params: { edition_id: edition.id, downtime: invalid_params, commit: "Re-schedule downtime message" } + assert_template :edit + end + end + end + def edition @edition ||= FactoryBot.create(:transaction_edition) end diff --git a/test/integration/routes_test.rb b/test/integration/routes_test.rb index 984213dfe..481f0e1c6 100644 --- a/test/integration/routes_test.rb +++ b/test/integration/routes_test.rb @@ -14,4 +14,13 @@ class RoutesTest < ActionDispatch::IntegrationTest assert_routing("/reports", controller: "legacy_reports", action: "index") end + + should "route to legacy reports controller when 'design_system_downtime_edit' toggle is enabled" do + test_strategy = Flipflop::FeatureSet.current.test! + test_strategy.switch!(:design_system_downtime_edit, true) + edition = FactoryBot.create(:edition) + edition_id = edition.id.to_s + + assert_routing("/editions/#{edition_id}/downtime/edit", controller: "downtimes", action: "edit", edition_id:) + end end