From cd243dc1ff1a8cbfcf2a5cd2e161cdff00333e07 Mon Sep 17 00:00:00 2001 From: Mark Taylor <138604938+mtaylorgds@users.noreply.github.com> Date: Fri, 6 Sep 2024 10:11:21 +0100 Subject: [PATCH] Default the state filters on the publications page When visiting the root page, with no filters set, default the state filters so that only editions in the "pre-published" states are shown, as these are the editions that users of Mainstream Publisher mostly care about. Uses the presence of the title filter as a proxy for whether any filters have been set, since that filter is always present when the 'update filter' button is clicked (even if no title text has been supplied, in which case it will have an empty value). --- app/controllers/root_controller.rb | 12 ++++++- test/functional/root_controller_test.rb | 45 ++++++++++++++++++++++++- 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/app/controllers/root_controller.rb b/app/controllers/root_controller.rb index 18afffd96..47294fa56 100644 --- a/app/controllers/root_controller.rb +++ b/app/controllers/root_controller.rb @@ -15,10 +15,16 @@ class RootController < ApplicationController archived ].freeze + DEFAULT_FILTER_STATES = %w[draft amends_needed in_review fact_check fact_check_received ready].freeze + def index filter_params_hash = filter_params.to_h states_filter_params = filter_params_hash[:states_filter] - sanitised_states_filter_params = states_filter_params&.select { |fp| PERMITTED_FILTER_STATES.include?(fp) } + sanitised_states_filter_params = if user_has_submitted_filters? + states_filter_params&.select { |fp| PERMITTED_FILTER_STATES.include?(fp) } + else + DEFAULT_FILTER_STATES + end assignee_filter = filter_params_hash[:assignee_filter] content_type_filter = filter_params_hash[:content_type_filter] title_filter = filter_params_hash[:title_filter] @@ -33,6 +39,10 @@ def index private + def user_has_submitted_filters? + filter_params.to_h[:title_filter] + end + def filter_params params.permit(:page, :assignee_filter, :content_type_filter, :title_filter, states_filter: []) end diff --git a/test/functional/root_controller_test.rb b/test/functional/root_controller_test.rb index 3e5d5e858..ddf3407a2 100644 --- a/test/functional/root_controller_test.rb +++ b/test/functional/root_controller_test.rb @@ -13,6 +13,49 @@ class RootControllerTest < ActionController::TestCase assert_template "root/index" end + should "default the state filter checkboxes when no filters are specified" do + get :index + + # These filters should be 'checked' + assert_select "input.govuk-checkboxes__input[value='draft'][checked]" + assert_select "input.govuk-checkboxes__input[value='amends_needed'][checked]" + assert_select "input.govuk-checkboxes__input[value='in_review'][checked]" + assert_select "input.govuk-checkboxes__input[value='fact_check'][checked]" + assert_select "input.govuk-checkboxes__input[value='fact_check_received'][checked]" + assert_select "input.govuk-checkboxes__input[value='ready'][checked]" + + # These filters should NOT be 'checked' + assert_select "input.govuk-checkboxes__input[value='scheduled_for_publishing']" + assert_select "input.govuk-checkboxes__input[value='scheduled_for_publishing'][checked]", false + assert_select "input.govuk-checkboxes__input[value='published']" + assert_select "input.govuk-checkboxes__input[value='published'][checked]", false + assert_select "input.govuk-checkboxes__input[value='archived']" + assert_select "input.govuk-checkboxes__input[value='archived'][checked]", false + end + + should "default the applied state filters when no filters are specified" do + FactoryBot.create(:edition, state: "draft") + FactoryBot.create(:edition, state: "amends_needed") + FactoryBot.create(:edition, state: "in_review", review_requested_at: 1.hour.ago) + fact_check_edition = FactoryBot.create(:edition, state: "fact_check", title: "Check yo fax") + fact_check_edition.new_action(FactoryBot.create(:user), "send_fact_check") + FactoryBot.create(:edition, state: "fact_check_received") + FactoryBot.create(:edition, state: "ready") + FactoryBot.create(:edition, state: "scheduled_for_publishing", publish_at: Time.zone.now + 1.hour) + FactoryBot.create(:edition, state: "published") + FactoryBot.create(:edition, state: "archived") + + get :index + + assert_select "p.publications-table__heading", "6 document(s)" + assert_select "span.govuk-tag--draft", "Draft" + assert_select "span.govuk-tag--amends_needed", "Amends needed" + assert_select "span.govuk-tag--in_review", "In review" + assert_select "span.govuk-tag--fact_check", "Fact check" + assert_select "span.govuk-tag--fact_check_received", "Fact check received" + assert_select "span.govuk-tag--ready", "Ready" + end + should "filter publications by state" do FactoryBot.create(:guide_edition, state: "draft") FactoryBot.create(:guide_edition, state: "published") @@ -66,7 +109,7 @@ class RootControllerTest < ActionController::TestCase edition_states: [], )) - get :index, params: { states_filter: %w[draft not_a_real_state] } + get :index, params: { title_filter: "", states_filter: %w[draft not_a_real_state] } end should "show the first page of publications when no page is specified" do