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