From 8d7ed174aed342f1500fe05fb8fd60609ce569f6 Mon Sep 17 00:00:00 2001 From: syed-ali-tw Date: Fri, 25 Oct 2024 12:24:38 +0100 Subject: [PATCH 1/4] Add skip fact check to admin tab when edition is in fact check --- app/controllers/editions_controller.rb | 28 +++++- app/helpers/tabbed_nav_helper.rb | 74 +++++++++------- .../editions/_secondary_navigation.html.erb | 2 +- .../secondary_nav_tabs/_admin.html.erb | 14 +++ config/routes.rb | 9 ++ test/functional/editions_controller_test.rb | 87 +++++++++++++++++++ test/integration/edition_edit_test.rb | 60 +++++++++++++ .../helpers/admin/tabbed_nav_helper_test.rb | 80 ++++++++++++++++- 8 files changed, 313 insertions(+), 41 deletions(-) create mode 100644 app/views/editions/secondary_nav_tabs/_admin.html.erb diff --git a/app/controllers/editions_controller.rb b/app/controllers/editions_controller.rb index a52958e33..c2522b687 100644 --- a/app/controllers/editions_controller.rb +++ b/app/controllers/editions_controller.rb @@ -11,6 +11,9 @@ class EditionsController < InheritedResources::Base before_action only: %i[unpublish confirm_unpublish process_unpublish] do require_govuk_editor(redirect_path: edition_path(resource)) end + before_action only: %i[progress admin] do + require_editor_permissions + end helper_method :locale_to_language @@ -26,15 +29,12 @@ def show alias_method :metadata, :show alias_method :unpublish, :show + alias_method :admin, :show def history render action: "show" end - def admin - render action: "show" - end - def linking render action: "show" end @@ -65,6 +65,15 @@ def process_unpublish render_confirm_page_with_error end + def progress + if progress_edition(resource, params[:edition][:activity].permit(:comment, :request_type, :publish_at)) + flash[:success] = @command.status_message + else + flash[:danger] = @command.status_message + end + redirect_to edition_path(resource) + end + protected def setup_view_paths @@ -73,6 +82,11 @@ def setup_view_paths private + def progress_edition(resource, activity_params) + @command = EditionProgressor.new(resource, current_user) + @command.progress(squash_multiparameter_datetime_attributes(activity_params.to_h, %w[publish_at])) + end + def unpublish_edition(artefact) params["redirect_url"].strip.empty? ? UnpublishService.call(artefact, current_user) : UnpublishService.call(artefact, current_user, redirect_url) end @@ -118,4 +132,10 @@ def redirect_url def description(resource) resource.format.underscore.humanize end + + def progress_action_param + params[:edition][:activity][:request_type] + rescue StandardError + nil + end end diff --git a/app/helpers/tabbed_nav_helper.rb b/app/helpers/tabbed_nav_helper.rb index a174cf782..e7bf89819 100644 --- a/app/helpers/tabbed_nav_helper.rb +++ b/app/helpers/tabbed_nav_helper.rb @@ -1,8 +1,8 @@ module TabbedNavHelper - def edition_nav_items(edition) + def edition_nav_items(edition, current_user) nav_items = [] - all_tab_names.each do |item| + accessible_tabs(current_user, edition).each do |item| next if !edition.state.eql?("published") && item == "unpublish" nav_items << standard_nav_items(item, edition) @@ -10,43 +10,53 @@ def edition_nav_items(edition) nav_items.flatten end +end - def standard_nav_items(item, edition) - url = item.eql?("edit") ? url_for([:edition, { id: edition.id }]) : url_for([:edition, { action: item, id: edition.id }]) +def standard_nav_items(item, edition) + url = item.eql?("edit") ? url_for([:edition, { id: edition.id }]) : url_for([:edition, { action: item, id: edition.id }]) - label = Edition::Tab[item].title - href = url - current = request.path == url + label = Edition::Tab[item].title + href = url + current = request.path == url - edit_nav_item(label, href, current) - end + edit_nav_item(label, href, current) +end + +def edit_nav_item(label, href, current) + [ + { + label:, + href:, + current:, + }, + ] +end - def edit_nav_item(label, href, current) - [ - { - label:, - href:, - current:, - }, - ] +def current_tab_name + current_tab = (request.path.split("/") & all_tab_names).first + + case current_tab + when "metadata" + "metadata" + when "unpublish" + "unpublish" + when "admin" + "admin" + else + "temp_nav_text" end +end - def current_tab_name - current_tab = (request.path.split("/") & all_tab_names).first + private - case current_tab - when "metadata" - "metadata" - when "unpublish" - "unpublish" - else - "temp_nav_text" - end - end +def all_tab_names + %w[edit tagging metadata history admin related_external_links unpublish] +end -private +def accessible_tabs(current_user, edition) + nav_items_to_remove = [] + nav_items_to_remove << "admin" unless current_user.has_editor_permissions?(edition) + nav_items_to_remove << "unpublish" unless current_user.govuk_editor? - def all_tab_names - %w[edit tagging metadata history admin related_external_links unpublish] - end + all_tab_names.reject { |tab| nav_items_to_remove.include?(tab) } end diff --git a/app/views/editions/_secondary_navigation.html.erb b/app/views/editions/_secondary_navigation.html.erb index 0a456c29a..b256189e9 100644 --- a/app/views/editions/_secondary_navigation.html.erb +++ b/app/views/editions/_secondary_navigation.html.erb @@ -1,4 +1,4 @@ <%= render "govuk_publishing_components/components/secondary_navigation", { aria_label: "Document navigation", - items: edition_nav_items(@edition), + items: edition_nav_items(@edition, current_user), } %> diff --git a/app/views/editions/secondary_nav_tabs/_admin.html.erb b/app/views/editions/secondary_nav_tabs/_admin.html.erb new file mode 100644 index 000000000..fd9772b11 --- /dev/null +++ b/app/views/editions/secondary_nav_tabs/_admin.html.erb @@ -0,0 +1,14 @@ +<% @edition = @resource %> +
+
+ <%= header_for("Admin") %> + + <% if @edition.fact_check? %> + <%= form_for @edition, url: skip_fact_check_for_edition(@edition), method: "post" do %> + <%= render "govuk_publishing_components/components/button", { + text: "Skip fact check", + } %> + <% end %> + <% end %> +
+
diff --git a/config/routes.rb b/config/routes.rb index 04545563b..702ec8bb0 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -27,6 +27,15 @@ get "unpublish" get "unpublish/confirm-unpublish", to: "editions#confirm_unpublish", as: "confirm_unpublish" post "process_unpublish" + post "progress" + post "skip_fact_check", + to: "editions#progress", + edition: { + activity: { + request_type: "skip_fact_check", + comment: "Fact check skipped by request.", + }, + } end end end diff --git a/test/functional/editions_controller_test.rb b/test/functional/editions_controller_test.rb index e9edb2977..18153fc91 100644 --- a/test/functional/editions_controller_test.rb +++ b/test/functional/editions_controller_test.rb @@ -200,4 +200,91 @@ class EditionsControllerTest < ActionController::TestCase end end end + + context "#admin" do + setup do + @guide = FactoryBot.create(:guide_edition) + end + + should "show the admin page for the edition" do + get :admin, params: { id: @guide.id } + + assert_response :success + end + + context "Welsh editors" do + setup do + login_as_welsh_editor + @welsh_guide = FactoryBot.create(:guide_edition, :welsh) + end + + should "be able to see the admin page for Welsh editions" do + get :admin, params: { id: @welsh_guide.id } + + assert_response :success + end + + should "not be able to see the admin page for non-Welsh editions" do + get :admin, params: { id: @guide.id } + + assert_redirected_to edition_path(@guide) + assert_equal "You do not have correct editor permissions for this action.", flash[:danger] + end + end + end + + context "#progress" do + setup do + @guide = FactoryBot.create(:guide_edition, panopticon_id: FactoryBot.create(:artefact).id) + end + + context "Welsh editors" do + setup do + login_as_welsh_editor + @artefact = FactoryBot.create(:artefact) + @edition = FactoryBot.create(:guide_edition, :scheduled_for_publishing, panopticon_id: @artefact.id) + @welsh_edition = FactoryBot.create(:guide_edition, :scheduled_for_publishing, :welsh) + end + + should "be able to skip fact checks for Welsh editions" do + @welsh_edition.update!(state: "fact_check") + + post :progress, + params: { + id: @welsh_edition.id, + edition: { + activity: { + "request_type" => "skip_fact_check", + "comment" => "Fact check skipped by request.", + }, + }, + } + + assert_redirected_to edition_path(@welsh_edition) + @welsh_edition.reload + assert_equal flash[:success], "The fact check has been skipped for this publication." + assert_equal @welsh_edition.state, "ready" + end + + should "not be able to skip fact checks for non-Welsh editions" do + @edition.update!(state: "fact_check") + + post :progress, + params: { + id: @edition.id, + edition: { + activity: { + "request_type" => "skip_fact_check", + "comment" => "Fact check skipped by request.", + }, + }, + } + + assert_redirected_to edition_path(@edition) + @edition.reload + assert_equal @edition.state, "fact_check" + assert_equal flash[:danger], "You do not have correct editor permissions for this action." + end + end + end end diff --git a/test/integration/edition_edit_test.rb b/test/integration/edition_edit_test.rb index 5b25d7fde..281ce04e3 100644 --- a/test/integration/edition_edit_test.rb +++ b/test/integration/edition_edit_test.rb @@ -67,6 +67,18 @@ class EditionEditTest < IntegrationTest assert page.has_field?("artefact[slug]", with: "changed-slug") end end + + context "admin tab" do + setup do + click_link("Admin") + end + should "show 'Admin' header and not show 'Skip fact check' button" do + within :css, ".gem-c-heading" do + assert page.has_text?("Admin") + end + assert page.has_no_button?("Skip fact check") + end + end end context "when edition is published" do @@ -157,5 +169,53 @@ class EditionEditTest < IntegrationTest assert page.has_link?("Cancel") end end + + context "admin tab" do + setup do + click_link("Admin") + end + + should "show 'Admin' header and not show 'Skip fact check' button" do + within :css, ".gem-c-heading" do + assert page.has_text?("Admin") + end + assert page.has_no_button?("Skip fact check") + end + end + end + + context "when edition state is 'fact_check'" do + setup do + @edition = FactoryBot.create(:guide_edition, title: "Edit page title", state: "fact_check") + visit edition_path(@edition) + click_link("Admin") + end + + context "admin tab" do + context "when state is 'fact_check'" do + should "show 'Admin' header and an 'Skip fact check' button" do + within :css, ".gem-c-heading" do + assert page.has_text?("Admin") + end + assert page.has_button?("Skip fact check") + end + + should "show success message when fact check skipped successfully" do + click_button("Skip fact check") + + @edition.reload + assert_equal "ready", @edition.state + assert page.has_text?("The fact check has been skipped for this publication.") + end + + should "show error message when skip fact check gives an error" do + User.any_instance.stubs(:progress).returns(false) + + click_button("Skip fact check") + + assert page.has_text?("Could not skip fact check for this publication.") + end + end + end end end diff --git a/test/unit/helpers/admin/tabbed_nav_helper_test.rb b/test/unit/helpers/admin/tabbed_nav_helper_test.rb index d73449e89..1e58a5b62 100644 --- a/test/unit/helpers/admin/tabbed_nav_helper_test.rb +++ b/test/unit/helpers/admin/tabbed_nav_helper_test.rb @@ -1,7 +1,8 @@ require "test_helper" class TabbedNavHelperTest < ActionView::TestCase - test "#secondary_navigation_tabs_items for draft edition edit page" do + test "#secondary_navigation_tabs_items for draft edition edit page when user has editor access" do + user = FactoryBot.create(:user, :govuk_editor, name: "Stub User") resource = FactoryBot.create(:guide_edition, title: "Edit page title", state: "draft") expected_output = [ @@ -37,10 +38,11 @@ class TabbedNavHelperTest < ActionView::TestCase }, ] - assert_equal expected_output, edition_nav_items(resource) + assert_equal expected_output, edition_nav_items(resource, user) end - test "#secondary_navigation_tabs_items for published edition edit page" do + test "#secondary_navigation_tabs_items for published edition edit page when user has editor access" do + user = FactoryBot.create(:user, :govuk_editor, name: "Stub User") resource = FactoryBot.create(:guide_edition, title: "Edit page title", state: "published") expected_output = [ @@ -81,6 +83,76 @@ class TabbedNavHelperTest < ActionView::TestCase }, ] - assert_equal expected_output, edition_nav_items(resource) + assert_equal expected_output, edition_nav_items(resource, user) + end + + test "#secondary_navigation_tabs_items for draft edition edit page when user has no editor access" do + user = FactoryBot.create(:user, name: "Stub User") + resource = FactoryBot.create(:guide_edition, title: "Edit page title", state: "draft") + + expected_output = [ + { + label: "Edit", + href: "/editions/#{resource.id}", + current: false, + }, + { + label: "Tagging", + href: "/editions/#{resource.id}/tagging", + current: false, + }, + { + label: "Metadata", + href: "/editions/#{resource.id}/metadata", + current: false, + }, + { + label: "History and notes", + href: "/editions/#{resource.id}/history", + current: false, + }, + { + label: "Related external links", + href: "/editions/#{resource.id}/related_external_links", + current: false, + }, + ] + + assert_equal expected_output, edition_nav_items(resource, user) + end + + test "#secondary_navigation_tabs_items for published edition edit page when user has no editor access" do + user = FactoryBot.create(:user, name: "Stub User") + resource = FactoryBot.create(:guide_edition, title: "Edit page title", state: "published") + + expected_output = [ + { + label: "Edit", + href: "/editions/#{resource.id}", + current: false, + }, + { + label: "Tagging", + href: "/editions/#{resource.id}/tagging", + current: false, + }, + { + label: "Metadata", + href: "/editions/#{resource.id}/metadata", + current: false, + }, + { + label: "History and notes", + href: "/editions/#{resource.id}/history", + current: false, + }, + { + label: "Related external links", + href: "/editions/#{resource.id}/related_external_links", + current: false, + }, + ] + + assert_equal expected_output, edition_nav_items(resource, user) end end From efbc1d6d443193218cc15e11a6eb09f92ca10359 Mon Sep 17 00:00:00 2001 From: syed-ali-tw Date: Wed, 30 Oct 2024 07:38:36 +0000 Subject: [PATCH 2/4] Get rid of redundant overridden method --- app/controllers/editions_controller.rb | 5 ----- 1 file changed, 5 deletions(-) diff --git a/app/controllers/editions_controller.rb b/app/controllers/editions_controller.rb index c2522b687..cd12f371e 100644 --- a/app/controllers/editions_controller.rb +++ b/app/controllers/editions_controller.rb @@ -100,11 +100,6 @@ def downstream_error_message "Due to a service problem, the edition couldn't be unpublished" end - def setup_view_paths_for(publication) - prepend_view_path "app/views/editions" - prepend_view_path template_folder_for(publication) - end - def locale_to_language(locale) case locale when "en" From fd309f086b820e7e16c3cd75b1950aa187b319a2 Mon Sep 17 00:00:00 2001 From: syed-ali-tw Date: Wed, 30 Oct 2024 09:13:18 +0000 Subject: [PATCH 3/4] Render tabs based on permission --- test/integration/edition_edit_test.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/test/integration/edition_edit_test.rb b/test/integration/edition_edit_test.rb index 281ce04e3..d4cf88241 100644 --- a/test/integration/edition_edit_test.rb +++ b/test/integration/edition_edit_test.rb @@ -2,12 +2,26 @@ class EditionEditTest < IntegrationTest setup do - setup_users + login_as(FactoryBot.create(:user, :govuk_editor, name: "Stub User")) test_strategy = Flipflop::FeatureSet.current.test! test_strategy.switch!(:design_system_edit, true) stub_linkables end + context "when user does not have required permissions" do + setup do + user = FactoryBot.create(:user, name: "Stub User", organisation_slug: "government-digital-service") + login_as(user) + edition = FactoryBot.create(:guide_edition, title: "Edit page title", state: "draft") + visit edition_path(edition) + end + + should "not show admin and unpublish tab when user does not have editor permission" do + assert page.has_no_text?("Admin") + assert page.has_no_text?("Unpublish") + end + end + context "when edition is draft" do setup do edition = FactoryBot.create(:guide_edition, title: "Edit page title", state: "draft") From 57fd5a973e97ff5d65fee69c76503aa83a46bd62 Mon Sep 17 00:00:00 2001 From: syed-ali-tw Date: Fri, 1 Nov 2024 15:14:27 +0000 Subject: [PATCH 4/4] Refactor i> edition edit and edition controller integration test ii> tabbed nav helper module --- app/helpers/base_helper.rb | 4 - app/helpers/tabbed_nav_helper.rb | 78 +++--- .../secondary_nav_tabs/_admin.html.erb | 2 +- app/views/shared/_admin.html.erb | 2 +- test/functional/editions_controller_test.rb | 133 +++++----- test/integration/edition_edit_test.rb | 248 ++++++++++-------- .../helpers/admin/tabbed_nav_helper_test.rb | 158 ----------- 7 files changed, 236 insertions(+), 389 deletions(-) delete mode 100644 test/unit/helpers/admin/tabbed_nav_helper_test.rb diff --git a/app/helpers/base_helper.rb b/app/helpers/base_helper.rb index 53a3a4238..10de3685a 100644 --- a/app/helpers/base_helper.rb +++ b/app/helpers/base_helper.rb @@ -30,10 +30,6 @@ def resource_fields(resource) "/#{resource.format.underscore.downcase.pluralize}/fields" end - def skip_fact_check_for_edition(edition) - send("skip_fact_check_edition_path", edition) - end - def edition_can_be_deleted?(edition) !edition.published? end diff --git a/app/helpers/tabbed_nav_helper.rb b/app/helpers/tabbed_nav_helper.rb index e7bf89819..07c5c4dac 100644 --- a/app/helpers/tabbed_nav_helper.rb +++ b/app/helpers/tabbed_nav_helper.rb @@ -10,53 +10,53 @@ def edition_nav_items(edition, current_user) nav_items.flatten end -end -def standard_nav_items(item, edition) - url = item.eql?("edit") ? url_for([:edition, { id: edition.id }]) : url_for([:edition, { action: item, id: edition.id }]) + def current_tab_name + current_tab = (request.path.split("/") & all_tab_names).first + + case current_tab + when "metadata" + "metadata" + when "unpublish" + "unpublish" + when "admin" + "admin" + else + "temp_nav_text" + end + end - label = Edition::Tab[item].title - href = url - current = request.path == url +private - edit_nav_item(label, href, current) -end + def all_tab_names + %w[edit tagging metadata history admin related_external_links unpublish] + end -def edit_nav_item(label, href, current) - [ - { - label:, - href:, - current:, - }, - ] -end + def accessible_tabs(current_user, edition) + nav_items_to_remove = [] + nav_items_to_remove << "admin" unless current_user.has_editor_permissions?(edition) + nav_items_to_remove << "unpublish" unless current_user.govuk_editor? -def current_tab_name - current_tab = (request.path.split("/") & all_tab_names).first - - case current_tab - when "metadata" - "metadata" - when "unpublish" - "unpublish" - when "admin" - "admin" - else - "temp_nav_text" + all_tab_names.reject { |tab| nav_items_to_remove.include?(tab) } end -end - private + def standard_nav_items(item, edition) + url = item.eql?("edit") ? url_for([:edition, { id: edition.id }]) : url_for([:edition, { action: item, id: edition.id }]) -def all_tab_names - %w[edit tagging metadata history admin related_external_links unpublish] -end + label = Edition::Tab[item].title + href = url + current = request.path == url -def accessible_tabs(current_user, edition) - nav_items_to_remove = [] - nav_items_to_remove << "admin" unless current_user.has_editor_permissions?(edition) - nav_items_to_remove << "unpublish" unless current_user.govuk_editor? + edit_nav_item(label, href, current) + end - all_tab_names.reject { |tab| nav_items_to_remove.include?(tab) } + def edit_nav_item(label, href, current) + [ + { + label:, + href:, + current:, + }, + ] + end end diff --git a/app/views/editions/secondary_nav_tabs/_admin.html.erb b/app/views/editions/secondary_nav_tabs/_admin.html.erb index fd9772b11..882bfe213 100644 --- a/app/views/editions/secondary_nav_tabs/_admin.html.erb +++ b/app/views/editions/secondary_nav_tabs/_admin.html.erb @@ -4,7 +4,7 @@ <%= header_for("Admin") %> <% if @edition.fact_check? %> - <%= form_for @edition, url: skip_fact_check_for_edition(@edition), method: "post" do %> + <%= form_for @edition, url: skip_fact_check_edition_path(@edition), method: "post" do %> <%= render "govuk_publishing_components/components/button", { text: "Skip fact check", } %> diff --git a/app/views/shared/_admin.html.erb b/app/views/shared/_admin.html.erb index 07ac6bd21..7a983e45b 100644 --- a/app/views/shared/_admin.html.erb +++ b/app/views/shared/_admin.html.erb @@ -9,7 +9,7 @@ <% if @resource.fact_check? %>

Skip fact check

- <%= button_to "Skip fact check", skip_fact_check_for_edition(@resource), :method => :post, :class => "btn btn-default" %> + <%= button_to "Skip fact check", skip_fact_check_edition_path(@resource), :method => :post, :class => "btn btn-default" %> <% end %> <% if @edition.published? && @edition.can_create_new_edition? %> diff --git a/test/functional/editions_controller_test.rb b/test/functional/editions_controller_test.rb index 18153fc91..7c6b016d2 100644 --- a/test/functional/editions_controller_test.rb +++ b/test/functional/editions_controller_test.rb @@ -3,11 +3,10 @@ class EditionsControllerTest < ActionController::TestCase setup do login_as_stub_user - stub_linkables - stub_holidays_used_by_fact_check - test_strategy = Flipflop::FeatureSet.current.test! test_strategy.switch!(:restrict_access_by_org, false) + @edition = FactoryBot.create(:edition, :fact_check) + @welsh_edition = FactoryBot.create(:edition, :fact_check, :welsh) end context "#template_folder_for" do @@ -28,41 +27,19 @@ class EditionsControllerTest < ActionController::TestCase end context "#show" do - setup do - artefact = FactoryBot.create( - :artefact, - slug: "test2", - kind: "guide", - name: "test", - owning_app: "publisher", - ) - @guide = GuideEdition.create!(title: "test", slug: "test2", panopticon_id: artefact.id) - end - should "return a 404 when requesting a publication that doesn't exist" do get :show, params: { id: "4e663834e2ba80480a0000e6" } assert_response :not_found end should "return a view for the requested guide" do - get :show, params: { id: @guide.id } + get :show, params: { id: @edition.id } assert_response :success assert_not_nil assigns(:resource) end end context "#metadata" do - setup do - artefact = FactoryBot.create( - :artefact, - slug: "test2", - kind: "guide", - name: "test", - owning_app: "publisher", - ) - @guide = GuideEdition.create!(title: "test", slug: "test2", panopticon_id: artefact.id) - end - should "alias to show method" do assert_equal EditionsController.new.method(:metadata).super_method.name, :show end @@ -115,50 +92,39 @@ class EditionsControllerTest < ActionController::TestCase end context "#unpublish" do - setup do - artefact = FactoryBot.create( - :artefact, - slug: "test2", - kind: "guide", - name: "test", - owning_app: "publisher", - ) - @guide = GuideEdition.create!(title: "test", slug: "test2", panopticon_id: artefact.id) - end - should "redirect to edition_path when user does not have govuk-editor permission" do user = FactoryBot.create(:user, :welsh_editor, name: "Stub User") login_as(user) - get :unpublish, params: { id: @guide.id } + get :unpublish, params: { id: @edition.id } - assert_redirected_to edition_path(@guide) + assert_redirected_to edition_path(@edition) end context "#confirm_unpublish" do should "redirect to edition_path when user does not have govuk-editor permission" do user = FactoryBot.create(:user, :welsh_editor, name: "Stub User") login_as(user) - get :confirm_unpublish, params: { id: @guide.id } + get :confirm_unpublish, params: { id: @edition.id } - assert_redirected_to edition_path(@guide) + assert_redirected_to edition_path(@edition) end should "render 'confirm_unpublish' template if redirect url is blank" do - get :confirm_unpublish, params: { id: @guide.id, redirect_url: "" } + get :confirm_unpublish, params: { id: @edition.id, redirect_url: "" } assert_template "secondary_nav_tabs/confirm_unpublish" end should "render 'confirm_unpublish' template if redirect url is a valid url" do - get :confirm_unpublish, params: { id: @guide.id, redirect_url: "https://www.gov.uk/redirect-to-replacement-page" } + get :confirm_unpublish, params: { id: @edition.id, redirect_url: "https://www.gov.uk/redirect-to-replacement-page" } assert_template "secondary_nav_tabs/confirm_unpublish" end should "render show template with error message when redirect url is not valid" do - get :confirm_unpublish, params: { id: @guide.id, redirect_url: "bob" } + get :confirm_unpublish, params: { id: @edition.id, redirect_url: "bob" } - assert_select ".gem-c-error-summary__list-item", "Redirect path is invalid. Guide can not be unpublished." + assert_select ".gem-c-error-summary__list-item", "Redirect path is invalid. Answer can not be unpublished." assert_template "show" end end @@ -167,34 +133,34 @@ class EditionsControllerTest < ActionController::TestCase should "redirect to edition_path when user does not have govuk-editor permission" do user = FactoryBot.create(:user, :welsh_editor, name: "Stub User") login_as(user) - get :confirm_unpublish, params: { id: @guide.id, redirect_url: nil } + get :confirm_unpublish, params: { id: @edition.id, redirect_url: nil } - assert_redirected_to edition_path(@guide) + assert_redirected_to edition_path(@edition) end should "show success message and redirect to root path when unpublished successfully with redirect url" do - get :process_unpublish, params: { id: @guide.id, redirect_url: "https://www.gov.uk/redirect-to-replacement-page" } + get :process_unpublish, params: { id: @edition.id, redirect_url: "https://www.gov.uk/redirect-to-replacement-page" } assert_equal "Content unpublished and redirected", flash[:success] end should "show success message and redirect to root path when unpublished successfully without redirect url" do UnpublishService.stubs(:call).returns(true) - get :process_unpublish, params: { id: @guide.id, redirect_url: nil } + get :process_unpublish, params: { id: @edition.id, redirect_url: nil } assert_equal "Content unpublished", flash[:success] end should "show error message when unpublish is unsuccessful" do UnpublishService.stubs(:call).returns(nil) - get :process_unpublish, params: { id: @guide.id, redirect_url: nil } + get :process_unpublish, params: { id: @edition.id, redirect_url: nil } assert_select ".gem-c-error-summary__list-item", "Due to a service problem, the edition couldn't be unpublished" end should "show error message when unpublish service returns an error" do UnpublishService.stubs(:call).raises(StandardError) - get :process_unpublish, params: { id: @guide.id, redirect_url: nil } + get :process_unpublish, params: { id: @edition.id, redirect_url: nil } assert_select ".gem-c-error-summary__list-item", "Due to a service problem, the edition couldn't be unpublished" end @@ -202,12 +168,8 @@ class EditionsControllerTest < ActionController::TestCase end context "#admin" do - setup do - @guide = FactoryBot.create(:guide_edition) - end - should "show the admin page for the edition" do - get :admin, params: { id: @guide.id } + get :admin, params: { id: @edition.id } assert_response :success end @@ -215,40 +177,30 @@ class EditionsControllerTest < ActionController::TestCase context "Welsh editors" do setup do login_as_welsh_editor - @welsh_guide = FactoryBot.create(:guide_edition, :welsh) end should "be able to see the admin page for Welsh editions" do - get :admin, params: { id: @welsh_guide.id } + get :admin, params: { id: @welsh_edition.id } assert_response :success end should "not be able to see the admin page for non-Welsh editions" do - get :admin, params: { id: @guide.id } + get :admin, params: { id: @edition.id } - assert_redirected_to edition_path(@guide) + assert_redirected_to edition_path(@edition) assert_equal "You do not have correct editor permissions for this action.", flash[:danger] end end end context "#progress" do - setup do - @guide = FactoryBot.create(:guide_edition, panopticon_id: FactoryBot.create(:artefact).id) - end - context "Welsh editors" do setup do login_as_welsh_editor - @artefact = FactoryBot.create(:artefact) - @edition = FactoryBot.create(:guide_edition, :scheduled_for_publishing, panopticon_id: @artefact.id) - @welsh_edition = FactoryBot.create(:guide_edition, :scheduled_for_publishing, :welsh) end should "be able to skip fact checks for Welsh editions" do - @welsh_edition.update!(state: "fact_check") - post :progress, params: { id: @welsh_edition.id, @@ -267,8 +219,6 @@ class EditionsControllerTest < ActionController::TestCase end should "not be able to skip fact checks for non-Welsh editions" do - @edition.update!(state: "fact_check") - post :progress, params: { id: @edition.id, @@ -286,5 +236,46 @@ class EditionsControllerTest < ActionController::TestCase assert_equal flash[:danger], "You do not have correct editor permissions for this action." end end + + context "govuk editors" do + setup do + login_as_govuk_editor + end + + should "be able to skip fact checks" do + post :progress, + params: { + id: @edition.id, + edition: { + activity: { + "request_type" => "skip_fact_check", + "comment" => "Fact check skipped by request.", + }, + }, + } + + assert_redirected_to edition_path(@edition) + @edition.reload + assert_equal flash[:success], "The fact check has been skipped for this publication." + assert_equal @edition.state, "ready" + end + + should "be able to skip fact checks Welsh editions" do + post :progress, + params: { + id: @welsh_edition.id, + edition: { + activity: { + "request_type" => "skip_fact_check", + "comment" => "Fact check skipped by request.", + }, + }, + } + + assert_redirected_to edition_path(@welsh_edition) + @welsh_edition.reload + assert_equal "ready", @welsh_edition.state + end + end end end diff --git a/test/integration/edition_edit_test.rb b/test/integration/edition_edit_test.rb index d4cf88241..8c841a226 100644 --- a/test/integration/edition_edit_test.rb +++ b/test/integration/edition_edit_test.rb @@ -8,24 +8,19 @@ class EditionEditTest < IntegrationTest stub_linkables end - context "when user does not have required permissions" do + context "all tabs" do setup do - user = FactoryBot.create(:user, name: "Stub User", organisation_slug: "government-digital-service") - login_as(user) - edition = FactoryBot.create(:guide_edition, title: "Edit page title", state: "draft") - visit edition_path(edition) + visit_edition_in_published end - should "not show admin and unpublish tab when user does not have editor permission" do - assert page.has_no_text?("Admin") - assert page.has_no_text?("Unpublish") - end - end - - context "when edition is draft" do - setup do - edition = FactoryBot.create(:guide_edition, title: "Edit page title", state: "draft") - visit edition_path(edition) + should "show all the tabs when user has required permission and edition is published" do + assert page.has_text?("Edit") + assert page.has_text?("Tagging") + assert page.has_text?("Metadata") + assert page.has_text?("History and notes") + assert page.has_text?("Admin") + assert page.has_text?("Related external links") + assert page.has_text?("Unpublish") end should "show document summary and title" do @@ -34,27 +29,17 @@ class EditionEditTest < IntegrationTest row = find_all(".govuk-summary-list__row") assert row[0].has_content?("Assigned to") assert row[1].has_text?("Content type") - assert row[1].has_text?("Guide") + assert row[1].has_text?("Answer") assert row[2].has_text?("Edition") assert row[2].has_text?("1") - assert row[2].has_text?("Draft") - end - - should "show all the tabs for the edit" do - assert page.has_text?("Edit") - assert page.has_text?("Tagging") - assert page.has_text?("Metadata") - assert page.has_text?("History and notes") - assert page.has_text?("Admin") - assert page.has_text?("Related external links") - end - - should "not show unpublish tab" do - assert page.has_no_text?("Unpublish") + assert row[2].has_text?("Published") end + end - context "metadata tab" do + context "metadata tab" do + context "when state is draft" do setup do + visit_edition_in_draft click_link("Metadata") end @@ -82,45 +67,9 @@ class EditionEditTest < IntegrationTest end end - context "admin tab" do - setup do - click_link("Admin") - end - should "show 'Admin' header and not show 'Skip fact check' button" do - within :css, ".gem-c-heading" do - assert page.has_text?("Admin") - end - assert page.has_no_button?("Skip fact check") - end - end - end - - context "when edition is published" do - setup do - @edition = FactoryBot.create( - :completed_transaction_edition, - panopticon_id: FactoryBot.create( - :artefact, - slug: "can-i-get-a-driving-licence", - ).id, - state: "published", - slug: "can-i-get-a-driving-licence", - ) - visit edition_path(@edition) - end - - should "show all the tabs for the published edition" do - assert page.has_text?("Edit") - assert page.has_text?("Tagging") - assert page.has_text?("Metadata") - assert page.has_text?("History and notes") - assert page.has_text?("Admin") - assert page.has_text?("Related external links") - assert page.has_text?("Unpublish") - end - - context "metadata tab" do + context "when state state is not draft" do setup do + visit_edition_in_published click_link("Metadata") end @@ -134,79 +83,120 @@ class EditionEditTest < IntegrationTest assert page.has_text?(/English/) end end + end - context "unpublish tab" do + context "unpublish tab" do + context "do not have required permissions" do setup do - click_link("Unpublish") + login_as(FactoryBot.create(:user, name: "Stub User")) + visit_edition_in_draft end - should "show 'Unpublish' header and 'Continue' button" do - within :css, ".gem-c-heading" do - assert page.has_text?("Unpublish") - end - assert page.has_button?("Continue") + should "not show unpublish tab when user is not govuk editor" do + assert page.has_no_text?("Unpublish") end + end - should "show 'cannot be undone' banner" do - assert page.has_text?("If you unpublish a page from GOV.UK it cannot be undone.") + context "has required permissions" do + setup do + login_as(FactoryBot.create(:user, :govuk_editor, name: "Stub User")) + visit_edition_in_draft end - should "show 'Redirect to URL' text, input box and example text" do - assert page.has_text?("Redirect to URL") - assert page.has_text?("For example: https://www.gov.uk/redirect-to-replacement-page") - assert page.has_css?(".govuk-input", count: 1) + context "when state is publish" do + setup do + visit_edition_in_published + click_link("Unpublish") + end + + should "show 'Unpublish' header and 'Continue' button" do + within :css, ".gem-c-heading" do + assert page.has_text?("Unpublish") + end + assert page.has_button?("Continue") + end + + should "show 'cannot be undone' banner" do + assert page.has_text?("If you unpublish a page from GOV.UK it cannot be undone.") + end + + should "show 'Redirect to URL' text, input box and example text" do + assert page.has_text?("Redirect to URL") + assert page.has_text?("For example: https://www.gov.uk/redirect-to-replacement-page") + assert page.has_css?(".govuk-input", count: 1) + end + + should "navigate to 'confirm-unpublish' page when 'Continue' button is clicked" do + click_button("Continue") + assert_equal(page.current_path, "/editions/#{@published_edition.id}/unpublish/confirm-unpublish") + end end - should "navigate to 'confirm-unpublish' page when clicked on 'Continue' button" do - click_button("Continue") - assert_equal(page.current_path, "/editions/#{@edition.id}/unpublish/confirm-unpublish") + context "when state is not publish" do + setup do + edition = FactoryBot.create(:edition, state: "draft") + visit edition_path(edition) + end + + should "not show unpublish tab" do + assert page.has_no_text?("Unpublish") + end end end + end - context "confirm unpublish" do + context "admin tab" do + context "do not have required permissions" do setup do - click_link("Unpublish") - click_button("Continue") + login_as(FactoryBot.create(:user, name: "Stub User")) + visit_edition_in_draft end - should "show 'Unpublish' header and document title" do - assert page.has_text?("Unpublish") - assert page.has_text?(@edition.title.to_s) + should "not show when user is not govuk editor or welsh editor" do + assert page.has_no_text?("Admin") end - should "show 'cannot be undone' banner" do - assert page.has_text?("If you unpublish a page from GOV.UK it cannot be undone.") - end + should "not show when user is welsh editor and edition is not welsh" do + login_as(FactoryBot.create(:user, :welsh_editor, name: "Stub User")) + visit_edition_in_draft - should "show 'Unpublish document' button and 'Cancel' link" do - assert page.has_button?("Unpublish document") - assert page.has_link?("Cancel") + assert page.has_no_text?("Admin") end end - context "admin tab" do + context "has required permissions" do setup do - click_link("Admin") + login_as(FactoryBot.create(:user, :govuk_editor, name: "Stub User")) end - should "show 'Admin' header and not show 'Skip fact check' button" do - within :css, ".gem-c-heading" do - assert page.has_text?("Admin") + context "when state is not 'fact_check'" do + setup do + visit_edition_in_draft + click_link("Admin") end - assert page.has_no_button?("Skip fact check") - end - end - end - context "when edition state is 'fact_check'" do - setup do - @edition = FactoryBot.create(:guide_edition, title: "Edit page title", state: "fact_check") - visit edition_path(@edition) - click_link("Admin") - end + should "show 'Admin' header and not show 'Skip fact check' button" do + within :css, ".gem-c-heading" do + assert page.has_text?("Admin") + end + assert page.has_no_button?("Skip fact check") + end + end - context "admin tab" do context "when state is 'fact_check'" do + setup do + visit_edition_in_fact_check + click_link("Admin") + end + + should "show tab when user is welsh editor and edition is welsh edition" do + login_as(FactoryBot.create(:user, :welsh_editor, name: "Stub User")) + welsh_edition = FactoryBot.create(:edition, :fact_check, :welsh) + visit edition_path(welsh_edition) + + assert page.has_text?("Admin") + end + should "show 'Admin' header and an 'Skip fact check' button" do within :css, ".gem-c-heading" do assert page.has_text?("Admin") @@ -216,9 +206,9 @@ class EditionEditTest < IntegrationTest should "show success message when fact check skipped successfully" do click_button("Skip fact check") + @fact_check_edition.reload - @edition.reload - assert_equal "ready", @edition.state + assert_equal "ready", @fact_check_edition.state assert page.has_text?("The fact check has been skipped for this publication.") end @@ -226,10 +216,38 @@ class EditionEditTest < IntegrationTest User.any_instance.stubs(:progress).returns(false) click_button("Skip fact check") + @fact_check_edition.reload + assert_equal "fact_check", @fact_check_edition.state assert page.has_text?("Could not skip fact check for this publication.") end end end end + +private + + def visit_edition_in_draft + @draft_edition = FactoryBot.create(:edition, title: "Edit page title", state: "draft") + visit edition_path(@draft_edition) + end + + def visit_edition_in_published + @published_edition = FactoryBot.create( + :edition, + title: "Edit page title", + panopticon_id: FactoryBot.create( + :artefact, + slug: "can-i-get-a-driving-licence", + ).id, + state: "published", + slug: "can-i-get-a-driving-licence", + ) + visit edition_path(@published_edition) + end + + def visit_edition_in_fact_check + @fact_check_edition = FactoryBot.create(:edition, title: "Edit page title", state: "fact_check") + visit edition_path(@fact_check_edition) + end end diff --git a/test/unit/helpers/admin/tabbed_nav_helper_test.rb b/test/unit/helpers/admin/tabbed_nav_helper_test.rb deleted file mode 100644 index 1e58a5b62..000000000 --- a/test/unit/helpers/admin/tabbed_nav_helper_test.rb +++ /dev/null @@ -1,158 +0,0 @@ -require "test_helper" - -class TabbedNavHelperTest < ActionView::TestCase - test "#secondary_navigation_tabs_items for draft edition edit page when user has editor access" do - user = FactoryBot.create(:user, :govuk_editor, name: "Stub User") - resource = FactoryBot.create(:guide_edition, title: "Edit page title", state: "draft") - - expected_output = [ - { - label: "Edit", - href: "/editions/#{resource.id}", - current: false, - }, - { - label: "Tagging", - href: "/editions/#{resource.id}/tagging", - current: false, - }, - { - label: "Metadata", - href: "/editions/#{resource.id}/metadata", - current: false, - }, - { - label: "History and notes", - href: "/editions/#{resource.id}/history", - current: false, - }, - { - label: "Admin", - href: "/editions/#{resource.id}/admin", - current: false, - }, - { - label: "Related external links", - href: "/editions/#{resource.id}/related_external_links", - current: false, - }, - ] - - assert_equal expected_output, edition_nav_items(resource, user) - end - - test "#secondary_navigation_tabs_items for published edition edit page when user has editor access" do - user = FactoryBot.create(:user, :govuk_editor, name: "Stub User") - resource = FactoryBot.create(:guide_edition, title: "Edit page title", state: "published") - - expected_output = [ - { - label: "Edit", - href: "/editions/#{resource.id}", - current: false, - }, - { - label: "Tagging", - href: "/editions/#{resource.id}/tagging", - current: false, - }, - { - label: "Metadata", - href: "/editions/#{resource.id}/metadata", - current: false, - }, - { - label: "History and notes", - href: "/editions/#{resource.id}/history", - current: false, - }, - { - label: "Admin", - href: "/editions/#{resource.id}/admin", - current: false, - }, - { - label: "Related external links", - href: "/editions/#{resource.id}/related_external_links", - current: false, - }, - { - label: "Unpublish", - href: "/editions/#{resource.id}/unpublish", - current: false, - }, - ] - - assert_equal expected_output, edition_nav_items(resource, user) - end - - test "#secondary_navigation_tabs_items for draft edition edit page when user has no editor access" do - user = FactoryBot.create(:user, name: "Stub User") - resource = FactoryBot.create(:guide_edition, title: "Edit page title", state: "draft") - - expected_output = [ - { - label: "Edit", - href: "/editions/#{resource.id}", - current: false, - }, - { - label: "Tagging", - href: "/editions/#{resource.id}/tagging", - current: false, - }, - { - label: "Metadata", - href: "/editions/#{resource.id}/metadata", - current: false, - }, - { - label: "History and notes", - href: "/editions/#{resource.id}/history", - current: false, - }, - { - label: "Related external links", - href: "/editions/#{resource.id}/related_external_links", - current: false, - }, - ] - - assert_equal expected_output, edition_nav_items(resource, user) - end - - test "#secondary_navigation_tabs_items for published edition edit page when user has no editor access" do - user = FactoryBot.create(:user, name: "Stub User") - resource = FactoryBot.create(:guide_edition, title: "Edit page title", state: "published") - - expected_output = [ - { - label: "Edit", - href: "/editions/#{resource.id}", - current: false, - }, - { - label: "Tagging", - href: "/editions/#{resource.id}/tagging", - current: false, - }, - { - label: "Metadata", - href: "/editions/#{resource.id}/metadata", - current: false, - }, - { - label: "History and notes", - href: "/editions/#{resource.id}/history", - current: false, - }, - { - label: "Related external links", - href: "/editions/#{resource.id}/related_external_links", - current: false, - }, - ] - - assert_equal expected_output, edition_nav_items(resource, user) - end -end