From 1b8cc3eb97aec40f5b81c36785576158e2d424c9 Mon Sep 17 00:00:00 2001 From: Harriet H-W Date: Thu, 24 Oct 2024 15:45:38 +0100 Subject: [PATCH 1/5] add option to filter by content block type this adds hardcoded checkboxes to filter Content Blocks by block type. We will shortly generate these checkboxes from the list of valid schemas. --- .../content_block/documents_controller.rb | 2 +- .../content_block/document/document_filter.rb | 1 + .../documents/_filter_options.html.erb | 28 ++++++++++++++++--- .../features/search_for_object.feature | 18 ++++++++++-- .../content_block_manager_steps.rb | 14 ++++++++++ .../document/document_filter_test.rb | 22 +++++++++++++++ 6 files changed, 77 insertions(+), 8 deletions(-) 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 9e4185d8e76..baa961c1194 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) + params.slice(:keyword, :block_type) .permit! .to_h end diff --git a/lib/engines/content_block_manager/app/models/content_block_manager/content_block/document/document_filter.rb b/lib/engines/content_block_manager/app/models/content_block_manager/content_block/document/document_filter.rb index c2bb05713fd..dcdef7d3af0 100644 --- a/lib/engines/content_block_manager/app/models/content_block_manager/content_block/document/document_filter.rb +++ b/lib/engines/content_block_manager/app/models/content_block_manager/content_block/document/document_filter.rb @@ -8,6 +8,7 @@ def documents documents = ContentBlock::Document documents = documents.live documents = documents.with_keyword(@filters[:keyword]) if @filters[:keyword].present? + documents = documents.where(block_type: @filters[:block_type]) if @filters[:block_type].present? documents end end diff --git a/lib/engines/content_block_manager/app/views/content_block_manager/content_block/documents/_filter_options.html.erb b/lib/engines/content_block_manager/app/views/content_block_manager/content_block/documents/_filter_options.html.erb index ad99cf2b49b..2bc4df33461 100644 --- a/lib/engines/content_block_manager/app/views/content_block_manager/content_block/documents/_filter_options.html.erb +++ b/lib/engines/content_block_manager/app/views/content_block_manager/content_block/documents/_filter_options.html.erb @@ -10,8 +10,28 @@ value: !@filters.nil? && @filters[:keyword], } %> - <%= render "govuk_publishing_components/components/button", { - text: "View results", - margin_bottom: 4, - } %> + <%= render "govuk_publishing_components/components/checkboxes", { + heading: "Content block type", + heading_size: "s", + no_hint_text: true, + id: "block_type", + name: "block_type", + items: [ + { + label: "Email address", + value: "email_address", + checked: !@filters.nil? && @filters[:block_type] == "email_address", + }, + { + label: "Postal address", + value: "postal_address", + checked: !@filters.nil? && @filters[:block_type] == "postal_address", + }, + ], + } %> + + <%= render "govuk_publishing_components/components/button", { + text: "View results", + margin_bottom: 4, + } %> <% 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 e2ef49bf02d..a042d7a42f3 100644 --- a/lib/engines/content_block_manager/features/search_for_object.feature +++ b/lib/engines/content_block_manager/features/search_for_object.feature @@ -5,15 +5,19 @@ Feature: Search for a content object And the organisation "Ministry of Example" 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" | + And a "postal_address" type of content block has been created with fields: + | an_address | "ABC123" | Scenario: GDS Editor searches for a content object by keyword in title When I visit the Content Block Manager home page Then I should see the details for all documents - And "2" content blocks are returned + And "3" content blocks are returned When I enter the keyword "example search" And I click to view results Then I should see the content block with title "example search title" returned @@ -22,8 +26,16 @@ Feature: Search for a content object Scenario: GDS Editor searches for a content object by keyword in details When I visit the Content Block Manager home page Then I should see the details for all documents - And "2" content blocks are returned + And "3" content blocks are returned When I enter the keyword "ministry justice" And I click to view results Then I should see the content block with title "example search title" returned - And "1" content blocks are returned \ No newline at end of file + And "1" content blocks are returned + + Scenario: GDS Editor searches for a content object by block type + 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 check the block type "Email address" + And I click to view results + And "2" 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 6ad8ed550b7..fb24b5603a9 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 @@ -187,6 +187,16 @@ @content_blocks.push(@content_block) end +Given("a {string} type of content block has been created with fields:") do |block_type, table| + fields = table.rows_hash + create( + :content_block_edition, + block_type.to_sym, + details: fields, + creator: @user, + ) +end + Given("an email address content block has been created with the following email address and title:") do |table| fields = table.rows_hash @content_blocks ||= [] @@ -435,6 +445,10 @@ def should_show_edit_form_for_email_address_content_block(document_title, email_ choose "Publish the change now" end +Then("I check the block type {string}") do |checkbox_name| + check checkbox_name +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/unit/app/models/content_block_edition/document/document_filter_test.rb b/lib/engines/content_block_manager/test/unit/app/models/content_block_edition/document/document_filter_test.rb index 5aaa7da7bc4..01261d8602f 100644 --- a/lib/engines/content_block_manager/test/unit/app/models/content_block_edition/document/document_filter_test.rb +++ b/lib/engines/content_block_manager/test/unit/app/models/content_block_edition/document/document_filter_test.rb @@ -9,6 +9,7 @@ class ContentBlockManager::DocumentFilterTest < ActiveSupport::TestCase document_scope_mock = mock ContentBlockManager::ContentBlock::Document.expects(:live).returns(document_scope_mock) document_scope_mock.expects(:with_keyword).never + document_scope_mock.expects(:where).never ContentBlockManager::ContentBlock::Document::DocumentFilter.new({}).documents end @@ -22,5 +23,26 @@ class ContentBlockManager::DocumentFilterTest < ActiveSupport::TestCase ContentBlockManager::ContentBlock::Document::DocumentFilter.new({ keyword: "ministry of example" }).documents end end + + describe "when a block type is given" do + it "returns live documents of the type given" do + document_scope_mock = mock + ContentBlockManager::ContentBlock::Document.expects(:live).returns(document_scope_mock) + document_scope_mock.expects(:where).with(block_type: %w[email_address]).returns([]) + ContentBlockManager::ContentBlock::Document::DocumentFilter.new({ block_type: %w[email_address] }).documents + end + end + + describe "when both block types and keyword is given" do + it "returns live documents of the type and keyword given" do + document_scope_mock = mock + ContentBlockManager::ContentBlock::Document.expects(:live).returns(document_scope_mock) + document_scope_mock.expects(:with_keyword).with("ministry of example").returns(document_scope_mock) + document_scope_mock.expects(:where).with(block_type: %w[email_address]).returns([]) + ContentBlockManager::ContentBlock::Document::DocumentFilter.new( + { block_type: %w[email_address], keyword: "ministry of example" }, + ).documents + end + end end end From a627a9735955b36e964dfc9acef4b667801f0d0e Mon Sep 17 00:00:00 2001 From: Harriet H-W Date: Thu, 24 Oct 2024 16:20:26 +0100 Subject: [PATCH 2/5] create filter options component for content blocks this will enable us to do more complicated functions like generating checkboxes for block types. --- .../index/filter_options_component.html.erb | 26 +++++++++++++ .../index/filter_options_component.rb | 18 +++++++++ .../documents/_filter_options.html.erb | 37 ------------------- .../content_block/documents/index.html.erb | 6 ++- .../index/filter_options_component_test.rb | 23 ++++++++++++ 5 files changed, 72 insertions(+), 38 deletions(-) create mode 100644 lib/engines/content_block_manager/app/components/content_block_manager/content_block/document/index/filter_options_component.html.erb create mode 100644 lib/engines/content_block_manager/app/components/content_block_manager/content_block/document/index/filter_options_component.rb delete mode 100644 lib/engines/content_block_manager/app/views/content_block_manager/content_block/documents/_filter_options.html.erb create mode 100644 lib/engines/content_block_manager/test/components/content_block/document/index/filter_options_component_test.rb 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 new file mode 100644 index 00000000000..f38070e7c44 --- /dev/null +++ b/lib/engines/content_block_manager/app/components/content_block_manager/content_block/document/index/filter_options_component.html.erb @@ -0,0 +1,26 @@ +<%= form_with url: helpers.content_block_manager.content_block_manager_content_block_documents_path, method: :get do %> + <%= render "govuk_publishing_components/components/input", { + label: { + text: "Keyword", + bold: true, + }, + hint: 'For example, "driving standards"', + name: "keyword", + id: "keyword_filter", + value: !@filters.nil? && @filters[:keyword], + } %> + + <%= render "govuk_publishing_components/components/checkboxes", { + heading: "Content block type", + heading_size: "s", + no_hint_text: true, + id: "block_type", + name: "block_type[]", + items: items_for_block_type, + } %> + + <%= render "govuk_publishing_components/components/button", { + text: "View results", + margin_bottom: 4, + } %> +<% end %> 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 new file mode 100644 index 00000000000..bfde53fd72b --- /dev/null +++ b/lib/engines/content_block_manager/app/components/content_block_manager/content_block/document/index/filter_options_component.rb @@ -0,0 +1,18 @@ +class ContentBlockManager::ContentBlock::Document::Index::FilterOptionsComponent < ViewComponent::Base + include ActionView::Helpers::RecordTagHelper + def initialize(filters:) + @filters = filters + end + +private + + def items_for_block_type + ContentBlockManager::ContentBlock::Schema.valid_schemas.map do |schema_name| + { + label: schema_name.humanize, + value: schema_name, + checked: !@filters.nil? && @filters[:block_type]&.include?(schema_name), + } + end + end +end diff --git a/lib/engines/content_block_manager/app/views/content_block_manager/content_block/documents/_filter_options.html.erb b/lib/engines/content_block_manager/app/views/content_block_manager/content_block/documents/_filter_options.html.erb deleted file mode 100644 index 2bc4df33461..00000000000 --- a/lib/engines/content_block_manager/app/views/content_block_manager/content_block/documents/_filter_options.html.erb +++ /dev/null @@ -1,37 +0,0 @@ -<%= form_with url: content_block_manager.content_block_manager_content_block_documents_path, method: :get do %> - <%= render "govuk_publishing_components/components/input", { - label: { - text: "Keyword", - bold: true, - }, - hint: 'For example, "driving standards"', - name: "keyword", - id: "keyword_filter", - value: !@filters.nil? && @filters[:keyword], - } %> - - <%= render "govuk_publishing_components/components/checkboxes", { - heading: "Content block type", - heading_size: "s", - no_hint_text: true, - id: "block_type", - name: "block_type", - items: [ - { - label: "Email address", - value: "email_address", - checked: !@filters.nil? && @filters[:block_type] == "email_address", - }, - { - label: "Postal address", - value: "postal_address", - checked: !@filters.nil? && @filters[:block_type] == "postal_address", - }, - ], - } %> - - <%= render "govuk_publishing_components/components/button", { - text: "View results", - margin_bottom: 4, - } %> -<% end %> diff --git a/lib/engines/content_block_manager/app/views/content_block_manager/content_block/documents/index.html.erb b/lib/engines/content_block_manager/app/views/content_block_manager/content_block/documents/index.html.erb index ba03c68400f..7e8f26b99fb 100644 --- a/lib/engines/content_block_manager/app/views/content_block_manager/content_block/documents/index.html.erb +++ b/lib/engines/content_block_manager/app/views/content_block_manager/content_block/documents/index.html.erb @@ -12,7 +12,11 @@
- <%= render "filter_options" %> + <%= render( + ContentBlockManager::ContentBlock::Document::Index::FilterOptionsComponent.new( + filters: @filters, + ), + ) %>

<%= pluralize(@content_block_documents.count, "result") %>

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 new file mode 100644 index 00000000000..964097b75c7 --- /dev/null +++ b/lib/engines/content_block_manager/test/components/content_block/document/index/filter_options_component_test.rb @@ -0,0 +1,23 @@ +require "test_helper" + +class ContentBlockManager::ContentBlock::Document::Index::FilterOptionsComponentTest < ViewComponent::TestCase + 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( + filters: {}, + )) + + assert_selector "input[type='checkbox'][name='block_type[]'][value='email_address']" + assert_selector "input[type='checkbox'][name='block_type[]'][value='postal_address']" + end + + test "checks checkbox items if checked in filters" do + ContentBlockManager::ContentBlock::Schema.expects(:valid_schemas).returns(%w[email_address postal_address]) + render_inline(ContentBlockManager::ContentBlock::Document::Index::FilterOptionsComponent.new( + filters: { block_type: %w[email_address] }, + )) + + assert_selector "input[type='checkbox'][name='block_type[]'][value='email_address'][checked]" + assert_selector "input[type='checkbox'][name='block_type[]'][value='postal_address']" + end +end From c7f6878ba51e2892d8e40a9f96c8c53ad2a8b721 Mon Sep 17 00:00:00 2001 From: Harriet H-W Date: Fri, 25 Oct 2024 12:26:44 +0100 Subject: [PATCH 3/5] add SearchByLeadOrganisation to Content Block Documents --- .../content_block/document.rb | 1 + .../scopes/searchable_by_lead_organisation.rb | 12 +++++++++++ .../searchable_by_lead_organisation_test.rb | 20 +++++++++++++++++++ 3 files changed, 33 insertions(+) create mode 100644 lib/engines/content_block_manager/app/models/content_block_manager/content_block/document/scopes/searchable_by_lead_organisation.rb create mode 100644 lib/engines/content_block_manager/test/unit/app/models/content_block_edition/document/scopes/searchable_by_lead_organisation_test.rb diff --git a/lib/engines/content_block_manager/app/models/content_block_manager/content_block/document.rb b/lib/engines/content_block_manager/app/models/content_block_manager/content_block/document.rb index afe2424b5d4..dd877073bb0 100644 --- a/lib/engines/content_block_manager/app/models/content_block_manager/content_block/document.rb +++ b/lib/engines/content_block_manager/app/models/content_block_manager/content_block/document.rb @@ -2,6 +2,7 @@ module ContentBlockManager module ContentBlock class Document < ApplicationRecord include Scopes::SearchableByKeyword + include Scopes::SearchableByLeadOrganisation extend FriendlyId friendly_id :title, use: :slugged, slug_column: :content_id_alias, routes: :default diff --git a/lib/engines/content_block_manager/app/models/content_block_manager/content_block/document/scopes/searchable_by_lead_organisation.rb b/lib/engines/content_block_manager/app/models/content_block_manager/content_block/document/scopes/searchable_by_lead_organisation.rb new file mode 100644 index 00000000000..6bc1cb1897d --- /dev/null +++ b/lib/engines/content_block_manager/app/models/content_block_manager/content_block/document/scopes/searchable_by_lead_organisation.rb @@ -0,0 +1,12 @@ +module ContentBlockManager + module ContentBlock::Document::Scopes::SearchableByLeadOrganisation + extend ActiveSupport::Concern + + included do + scope :with_lead_organisation, + lambda { |id| + joins(latest_edition: :edition_organisation).where("content_block_edition_organisations.organisation_id = :id", id:) + } + end + end +end diff --git a/lib/engines/content_block_manager/test/unit/app/models/content_block_edition/document/scopes/searchable_by_lead_organisation_test.rb b/lib/engines/content_block_manager/test/unit/app/models/content_block_edition/document/scopes/searchable_by_lead_organisation_test.rb new file mode 100644 index 00000000000..3950b086423 --- /dev/null +++ b/lib/engines/content_block_manager/test/unit/app/models/content_block_edition/document/scopes/searchable_by_lead_organisation_test.rb @@ -0,0 +1,20 @@ +require "test_helper" + +class ContentBlockManager::SearchableByLeadOrganisationTest < ActiveSupport::TestCase + extend Minitest::Spec::DSL + + describe ".with_lead_organisation" do + test "finds documents with lead organisation on latest edition" do + matching_organisation = create(:organisation, id: "1234") + document_with_org = create(:content_block_document, :email_address) + _edition_with_org = create(:content_block_edition, + :email_address, + document: document_with_org, + organisation: matching_organisation) + document_without_org = create(:content_block_document, :email_address) + _edition_without_org = create(:content_block_edition, :email_address, document: document_without_org) + _document_without_latest_edition = create(:content_block_document, :email_address) + assert_equal [document_with_org], ContentBlockManager::ContentBlock::Document.with_lead_organisation("1234") + end + end +end From fcf8af85ba54bfcd5cec550b6a589d0f579bbd2d Mon Sep 17 00:00:00 2001 From: Harriet H-W Date: Fri, 25 Oct 2024 12:26:19 +0100 Subject: [PATCH 4/5] add lead_organisation filter to Content Block Document Filter --- .../content_block/document/document_filter.rb | 1 + .../document/document_filter_test.rb | 19 +++++++++++++++---- 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/lib/engines/content_block_manager/app/models/content_block_manager/content_block/document/document_filter.rb b/lib/engines/content_block_manager/app/models/content_block_manager/content_block/document/document_filter.rb index dcdef7d3af0..ba30dd275e9 100644 --- a/lib/engines/content_block_manager/app/models/content_block_manager/content_block/document/document_filter.rb +++ b/lib/engines/content_block_manager/app/models/content_block_manager/content_block/document/document_filter.rb @@ -9,6 +9,7 @@ def documents documents = documents.live documents = documents.with_keyword(@filters[:keyword]) if @filters[:keyword].present? documents = documents.where(block_type: @filters[:block_type]) if @filters[:block_type].present? + documents = documents.with_lead_organisation(@filters[:lead_organisation]) if @filters[:lead_organisation].present? documents end end diff --git a/lib/engines/content_block_manager/test/unit/app/models/content_block_edition/document/document_filter_test.rb b/lib/engines/content_block_manager/test/unit/app/models/content_block_edition/document/document_filter_test.rb index 01261d8602f..b0f02eace43 100644 --- a/lib/engines/content_block_manager/test/unit/app/models/content_block_edition/document/document_filter_test.rb +++ b/lib/engines/content_block_manager/test/unit/app/models/content_block_edition/document/document_filter_test.rb @@ -10,6 +10,7 @@ class ContentBlockManager::DocumentFilterTest < ActiveSupport::TestCase ContentBlockManager::ContentBlock::Document.expects(:live).returns(document_scope_mock) document_scope_mock.expects(:with_keyword).never document_scope_mock.expects(:where).never + document_scope_mock.expects(:with_lead_organisation).never ContentBlockManager::ContentBlock::Document::DocumentFilter.new({}).documents end @@ -33,14 +34,24 @@ class ContentBlockManager::DocumentFilterTest < ActiveSupport::TestCase end end - describe "when both block types and keyword is given" do - it "returns live documents of the type and keyword given" do + describe "when a lead organisation id is given" do + it "returns live documents with lead org given" do + document_scope_mock = mock + ContentBlockManager::ContentBlock::Document.expects(:live).returns(document_scope_mock) + document_scope_mock.expects(:with_lead_organisation).with("123").returns([]) + ContentBlockManager::ContentBlock::Document::DocumentFilter.new({ lead_organisation: "123" }).documents + end + end + + describe "when block types, keyword and organisation is given" do + it "returns live documents with the filters given" do document_scope_mock = mock ContentBlockManager::ContentBlock::Document.expects(:live).returns(document_scope_mock) document_scope_mock.expects(:with_keyword).with("ministry of example").returns(document_scope_mock) - document_scope_mock.expects(:where).with(block_type: %w[email_address]).returns([]) + document_scope_mock.expects(:where).with(block_type: %w[email_address]).returns(document_scope_mock) + document_scope_mock.expects(:with_lead_organisation).with("123").returns([]) ContentBlockManager::ContentBlock::Document::DocumentFilter.new( - { block_type: %w[email_address], keyword: "ministry of example" }, + { block_type: %w[email_address], keyword: "ministry of example", lead_organisation: "123" }, ).documents end end From 66a7a9913d592aefddad096881dcb14bb20920d2 Mon Sep 17 00:00:00 2001 From: Harriet H-W Date: Fri, 25 Oct 2024 12:27:05 +0100 Subject: [PATCH 5/5] 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