diff --git a/app/controllers/manuals_controller.rb b/app/controllers/manuals_controller.rb index e791c1ce5..b5ecefec4 100644 --- a/app/controllers/manuals_controller.rb +++ b/app/controllers/manuals_controller.rb @@ -161,6 +161,29 @@ def publish ) end + def confirm_discard + service = Manual::ShowService.new( + manual_id:, + user: current_user, + ) + manual = service.call + + if !manual.has_ever_been_published? + render( + :confirm_discard, + layout: "design_system", + locals: { + manual:, + }, + ) + else + redirect_to( + manual_path(manual_id), + flash: { error: "#{manual.title} cannot be discarded as it has already been published" }, + ) + end + end + def discard_draft service = Manual::DiscardDraftService.new( user: current_user, @@ -171,12 +194,12 @@ def discard_draft if result.successful? redirect_to( manuals_path, - flash: { notice: "Discarded draft of #{result.manual_title}" }, + flash: { success: "Discarded draft of #{result.manual_title}" }, ) else redirect_to( manual_path(manual_id), - flash: { notice: "Unable to discard draft of #{result.manual_title}" }, + flash: { error: "Unable to discard draft of #{result.manual_title}" }, ) end end diff --git a/app/views/manuals/confirm_discard.html.erb b/app/views/manuals/confirm_discard.html.erb new file mode 100644 index 000000000..f17975e03 --- /dev/null +++ b/app/views/manuals/confirm_discard.html.erb @@ -0,0 +1,39 @@ +<% content_for :title, "Discard #{manual.title}" %> + +<% content_for :title_margin_bottom, 6 %> + +<%= render "govuk_publishing_components/components/breadcrumbs", { + collapse_on_mobile: true, + breadcrumbs: [ + { + title: "Your manuals", + url: manuals_path + }, + { + title: manual.title, + url: manual_path(manual) + }, + { + title: "Discard" + }, + ] +} %> + +<%= render "govuk_publishing_components/components/title", { + title: "Discard #{manual.title}" +} %> + +

You are about to discard "<%= manual.title %>".

+

Are you sure you want to discard this draft manual?

+ +<%= form_tag(discard_draft_manual_path(manual), method: :delete) do %> +
+ <%= render "govuk_publishing_components/components/button", { + text: "Discard manual", + name: "submit", + destructive: true + } %> + + <%= link_to("Cancel", manual_path(manual), class: "govuk-link govuk-link--no-visited-state") %> +
+<% end %> diff --git a/app/views/manuals/show.html.erb b/app/views/manuals/show.html.erb index 21524f73a..385c374a1 100644 --- a/app/views/manuals/show.html.erb +++ b/app/views/manuals/show.html.erb @@ -105,9 +105,7 @@

Discard draft manual

- <%= form_tag(discard_draft_manual_path(manual), method: :delete) do %> - - <% end -%> + <%= link_to 'Discard draft', confirm_discard_manual_path(manual), class: 'btn btn-danger' %>
<% end %> diff --git a/config/routes.rb b/config/routes.rb index be8d4e983..f1e133100 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -28,6 +28,7 @@ delete :discard_draft, on: :member get :confirm_publish, on: :member + get :confirm_discard, on: :member get :original_publication_date, on: :member, action: :edit_original_publication_date put :original_publication_date, on: :member, action: :update_original_publication_date diff --git a/features/deleting-a-manual.feature b/features/deleting-a-manual.feature index c95c1d9e2..12e08036f 100644 --- a/features/deleting-a-manual.feature +++ b/features/deleting-a-manual.feature @@ -16,6 +16,7 @@ Feature: Rake task to delete a manual Given a draft manual exists without any sections And a draft section exists for the manual When I discard the draft manual + And I confirm draft deletion Then the manual and its sections are deleted Scenario: Deleting a published manual diff --git a/features/step_definitions/deleting_manuals_steps.rb b/features/step_definitions/deleting_manuals_steps.rb index 71291e71d..7c0078e84 100644 --- a/features/step_definitions/deleting_manuals_steps.rb +++ b/features/step_definitions/deleting_manuals_steps.rb @@ -29,3 +29,8 @@ When(/^I discard the draft manual$/) do discard_draft_manual(@manual.title) end + +When(/^I confirm draft deletion$/) do + expect(page).to have_button("Discard manual") + click_on "Discard manual" +end diff --git a/spec/controllers/manuals_controller_spec.rb b/spec/controllers/manuals_controller_spec.rb index 664cd1cf3..6492c470c 100644 --- a/spec/controllers/manuals_controller_spec.rb +++ b/spec/controllers/manuals_controller_spec.rb @@ -34,6 +34,48 @@ end end + describe "#confirm_discard" do + let(:manual_id) { "manual-1" } + let(:service) { double(Manual::ShowService, call: manual) } + let(:manual) { instance_double(Manual, title: manual_title) } + let(:manual_title) { "My manual title" } + + before do + login_as_stub_user + allow(Manual::ShowService).to receive(:new).and_return(service) + end + + context "when the manual has been previously published" do + before do + allow(manual).to receive(:has_ever_been_published?).and_return(true) + get :confirm_discard, params: { id: manual_id } + end + + it "sets a flash message indicating failure" do + expect(flash[:error]).to include("#{manual_title} cannot be discarded as it has already been published") + end + + it "redirects to the show page for the manual" do + expect(response).to redirect_to manual_path(manual_id) + end + end + + context "when the manual has not been previously published" do + before do + allow(manual).to receive(:has_ever_been_published?).and_return(false) + get :confirm_discard, params: { id: manual_id } + end + + it "renders the discard confirmation page" do + expect(response).to render_template(:confirm_discard) + end + + it "renders with the design system layout" do + expect(response).to render_template("design_system") + end + end + end + describe "#discard_draft" do let(:manual_id) { "manual-1" } let(:service) { double(Manual::DiscardDraftService, call: result) } @@ -49,7 +91,7 @@ let(:discard_success) { true } it "sets a flash message indicating success" do - expect(flash[:notice]).to include("Discarded draft of My manual") + expect(flash[:success]).to include("Discarded draft of My manual") end it "redirects to the manuals index" do @@ -61,7 +103,7 @@ let(:discard_success) { false } it "sets a flash message indicating failure" do - expect(flash[:notice]).to include("Unable to discard draft of My manual") + expect(flash[:error]).to include("Unable to discard draft of My manual") end it "redirects to the show page for the manual" do diff --git a/spec/views/manuals/show.html.erb_spec.rb b/spec/views/manuals/show.html.erb_spec.rb new file mode 100644 index 000000000..c35a8c597 --- /dev/null +++ b/spec/views/manuals/show.html.erb_spec.rb @@ -0,0 +1,26 @@ +require "spec_helper" + +describe "manuals/show", type: :view do + before do + allow(view).to receive(:current_user_is_gds_editor?).and_return(true) + allow(view).to receive(:current_user_can_publish?).and_return(true) + end + + it "does not render the discard button for a published manual" do + manual = FactoryBot.build_stubbed(:manual, ever_been_published: true) + manual.publish_tasks = [] + + render template: "manuals/show", locals: { manual:, slug_unique: true, clashing_sections: [] } + + expect(rendered).not_to match(/Discard draft/) + end + + it "renders the discard button for an unpublished manual" do + manual = FactoryBot.build_stubbed(:manual, ever_been_published: false) + manual.publish_tasks = [] + + render template: "manuals/show", locals: { manual:, slug_unique: true, clashing_sections: [] } + + expect(rendered).to match(/Discard draft/) + end +end