From 6354ef797973a7c314b95657c9f00f6fd007d4be Mon Sep 17 00:00:00 2001 From: Harriet H-W Date: Fri, 25 Oct 2024 12:27:05 +0100 Subject: [PATCH] search by lead organisation from Content Block Manager --- .../index/filter_options_component.html.erb | 9 +++++++ .../index/filter_options_component.rb | 10 ++++++++ .../content_block/documents_controller.rb | 2 +- .../features/search_for_object.feature | 17 +++++++++---- .../content_block_manager_steps.rb | 7 ++++++ .../index/filter_options_component_test.rb | 24 +++++++++++++++++++ 6 files changed, 64 insertions(+), 5 deletions(-) diff --git a/lib/engines/content_block_manager/app/components/content_block_manager/content_block/document/index/filter_options_component.html.erb b/lib/engines/content_block_manager/app/components/content_block_manager/content_block/document/index/filter_options_component.html.erb index f38070e7c44..9522d6b46be 100644 --- a/lib/engines/content_block_manager/app/components/content_block_manager/content_block/document/index/filter_options_component.html.erb +++ b/lib/engines/content_block_manager/app/components/content_block_manager/content_block/document/index/filter_options_component.html.erb @@ -19,6 +19,15 @@ items: items_for_block_type, } %> + <%= render "components/select_with_search", { + id: "lead_organisation", + name: "lead_organisation", + label: "Lead organisation", + heading_size: "s", + include_blank: true, + options: options_for_lead_organisation, + } %> + <%= render "govuk_publishing_components/components/button", { text: "View results", margin_bottom: 4, diff --git a/lib/engines/content_block_manager/app/components/content_block_manager/content_block/document/index/filter_options_component.rb b/lib/engines/content_block_manager/app/components/content_block_manager/content_block/document/index/filter_options_component.rb index bfde53fd72b..ee44f71154d 100644 --- a/lib/engines/content_block_manager/app/components/content_block_manager/content_block/document/index/filter_options_component.rb +++ b/lib/engines/content_block_manager/app/components/content_block_manager/content_block/document/index/filter_options_component.rb @@ -15,4 +15,14 @@ def items_for_block_type } end end + + def options_for_lead_organisation + helpers.taggable_organisations_container.map do |name, id| + { + text: name, + value: id, + selected: !@filters.nil? && @filters[:lead_organisation] == id.to_s, + } + end + end end diff --git a/lib/engines/content_block_manager/app/controllers/content_block_manager/content_block/documents_controller.rb b/lib/engines/content_block_manager/app/controllers/content_block_manager/content_block/documents_controller.rb index baa961c1194..3a24f7cb273 100644 --- a/lib/engines/content_block_manager/app/controllers/content_block_manager/content_block/documents_controller.rb +++ b/lib/engines/content_block_manager/app/controllers/content_block_manager/content_block/documents_controller.rb @@ -28,7 +28,7 @@ def new_document_options_redirect private def params_filters - params.slice(:keyword, :block_type) + params.slice(:keyword, :block_type, :lead_organisation) .permit! .to_h end diff --git a/lib/engines/content_block_manager/features/search_for_object.feature b/lib/engines/content_block_manager/features/search_for_object.feature index a042d7a42f3..102cde00df6 100644 --- a/lib/engines/content_block_manager/features/search_for_object.feature +++ b/lib/engines/content_block_manager/features/search_for_object.feature @@ -2,17 +2,18 @@ Feature: Search for a content object Background: Given the content block manager feature flag is enabled Given I am a GDS admin - And the organisation "Ministry of Example" exists + And the organisation "Department of Placeholder" exists And a schema "email_address" exists with the following fields: | email_address | And a schema "postal_address" exists with the following fields: | an_address | And an email address content block has been created And an email address content block has been created with the following email address and title: - | title | "example search title" | - | email_address | "ministry@justice.com" | + | title | example search title | + | email_address | ministry@justice.com | And a "postal_address" type of content block has been created with fields: - | an_address | "ABC123" | + | an_address | ABC123 | + | organisation | Department of Placeholder | Scenario: GDS Editor searches for a content object by keyword in title When I visit the Content Block Manager home page @@ -39,3 +40,11 @@ Feature: Search for a content object When I check the block type "Email address" And I click to view results And "2" content blocks are returned + + Scenario: GDS Editor searches for a content object by lead organisation + When I visit the Content Block Manager home page + Then I should see the details for all documents + And "3" content blocks are returned + When I select the lead organisation "Department of Placeholder" + And I click to view results + And "1" content blocks are returned diff --git a/lib/engines/content_block_manager/features/step_definitions/content_block_manager_steps.rb b/lib/engines/content_block_manager/features/step_definitions/content_block_manager_steps.rb index fb24b5603a9..63ea4e4c0b2 100644 --- a/lib/engines/content_block_manager/features/step_definitions/content_block_manager_steps.rb +++ b/lib/engines/content_block_manager/features/step_definitions/content_block_manager_steps.rb @@ -189,9 +189,12 @@ Given("a {string} type of content block has been created with fields:") do |block_type, table| fields = table.rows_hash + organisation_name = fields.delete("organisation") + organisation = Organisation.where(name: organisation_name).first create( :content_block_edition, block_type.to_sym, + organisation:, details: fields, creator: @user, ) @@ -449,6 +452,10 @@ def should_show_edit_form_for_email_address_content_block(document_title, email_ check checkbox_name end +Then("I select the lead organisation {string}") do |organisation| + select organisation, from: "lead_organisation" +end + When("I revisit the edit page") do @content_block = @content_block.document.latest_edition visit_edit_page diff --git a/lib/engines/content_block_manager/test/components/content_block/document/index/filter_options_component_test.rb b/lib/engines/content_block_manager/test/components/content_block/document/index/filter_options_component_test.rb index 964097b75c7..37bb8a0ab06 100644 --- a/lib/engines/content_block_manager/test/components/content_block/document/index/filter_options_component_test.rb +++ b/lib/engines/content_block_manager/test/components/content_block/document/index/filter_options_component_test.rb @@ -1,6 +1,14 @@ require "test_helper" class ContentBlockManager::ContentBlock::Document::Index::FilterOptionsComponentTest < ViewComponent::TestCase + test "adds value of keyword to text input from filter" do + render_inline(ContentBlockManager::ContentBlock::Document::Index::FilterOptionsComponent.new( + filters: { keyword: "ministry defense" }, + )) + + assert_selector "input[name='keyword'][value='ministry defense']" + end + test "renders checkbox items for all valid schemas" do ContentBlockManager::ContentBlock::Schema.expects(:valid_schemas).returns(%w[email_address postal_address]) render_inline(ContentBlockManager::ContentBlock::Document::Index::FilterOptionsComponent.new( @@ -20,4 +28,20 @@ class ContentBlockManager::ContentBlock::Document::Index::FilterOptionsComponent assert_selector "input[type='checkbox'][name='block_type[]'][value='email_address'][checked]" assert_selector "input[type='checkbox'][name='block_type[]'][value='postal_address']" end + + test "selects organisation if selected in filters" do + helper_mock = mock + ContentBlockManager::ContentBlock::Document::Index::FilterOptionsComponent.any_instance.stubs(:helpers).returns(helper_mock) + helper_mock.stubs(:content_block_manager).returns(helper_mock) + helper_mock.stubs(:content_block_manager_content_block_documents_path).returns("path") + helper_mock.stubs(:taggable_organisations_container).returns( + [["Department of Placeholder", 1], ["Ministry of Example", 2]], + ) + render_inline(ContentBlockManager::ContentBlock::Document::Index::FilterOptionsComponent.new( + filters: { lead_organisation: "2" }, + )) + + assert_selector "select[name='lead_organisation']" + assert_selector "option[selected='selected'][value=2]" + end end