diff --git a/app/controllers/admin/document_collection_group_document_search_controller.rb b/app/controllers/admin/document_collection_group_document_search_controller.rb
index e181d2e0ec43..278f4398ec1d 100644
--- a/app/controllers/admin/document_collection_group_document_search_controller.rb
+++ b/app/controllers/admin/document_collection_group_document_search_controller.rb
@@ -15,12 +15,30 @@ def search
end
def search_title_slug
- flash.now[:alert] = "Please enter a search query" if params[:query] && params[:query].empty?
- @results = Edition.published.with_title_containing(params[:query].strip) if params[:query].present?
+ flash.now[:alert] = "Please enter a search query" if params[:title] && params[:title].empty?
+ @editions = filter.editions if params[:title].present?
end
private
+ def filter
+ Admin::EditionFilter.new(edition_scope, current_user, edition_filter_options)
+ end
+
+ def edition_scope
+ Edition.with_translations(I18n.locale)
+ end
+
+ def edition_filter_options
+ params.slice(:title, :page)
+ .permit!
+ .to_h.reverse_merge("state" => "active")
+ .symbolize_keys
+ .merge(
+ per_page: Admin::EditionFilter::GOVUK_DESIGN_SYSTEM_PER_PAGE,
+ )
+ end
+
def check_new_design_system_permissions
forbidden! unless new_design_system?
end
diff --git a/app/views/admin/document_collection_group_document_search/search_title_slug.html.erb b/app/views/admin/document_collection_group_document_search/search_title_slug.html.erb
index fdacddede67b..c82656ee6d2d 100644
--- a/app/views/admin/document_collection_group_document_search/search_title_slug.html.erb
+++ b/app/views/admin/document_collection_group_document_search/search_title_slug.html.erb
@@ -12,27 +12,27 @@
<%= form_with method: :get do |_form| %>
<%= render "govuk_publishing_components/components/search", {
- name: "query",
- value: params[:query],
+ name: "title",
+ value: params[:title],
label_text: "Search by title",
label_size: "l",
} %>
<% end %>
- <% if @results %>
+ <% unless @editions.nil? %>
-
<%= pluralize(number_with_delimiter(@results.count), "document") %>
+
<%= pluralize(number_with_delimiter(@editions.total_count), "document") %>
- <% if @results.empty? %>
+ <% if @editions.empty? %>
No results found. Search again using the <%= link_to "full URL", "#", class: "govuk-link govuk-link--no-visited-state" %>.
<% else %>
<%= render "govuk_publishing_components/components/table",
{
- rows: @results.map do |edition|
+ rows: @editions.map do |edition|
title_span = tag.span(index_table_title_row(edition), class: "govuk-!-font-weight-bold")
view_link = link_to(sanitize("View #{tag.span(edition.title, class: "govuk-visually-hidden")}"), edition.public_url, class: "govuk-link")
add_button = button_to("Add",
@@ -57,6 +57,7 @@
} %>
<% end %>
+ <%= paginate(@editions, theme: "govuk_paginator") %>
<% end %>
diff --git a/test/functional/admin/document_collection_group_document_search_controller_test.rb b/test/functional/admin/document_collection_group_document_search_controller_test.rb
index 16619fdd63e2..5ac8bbf33aa3 100644
--- a/test/functional/admin/document_collection_group_document_search_controller_test.rb
+++ b/test/functional/admin/document_collection_group_document_search_controller_test.rb
@@ -5,7 +5,11 @@ class Admin::DocumentCollectionGroupDocumentSearchControllerTest < ActionControl
@collection = create(:document_collection, :with_group)
@group = @collection.groups.first
@request_params = { document_collection_id: @collection, group_id: @group }
- login_as_preview_design_system_user :writer
+ @default_filter_params = {
+ state: "active",
+ per_page: 15,
+ }
+ @user = login_as_preview_design_system_user :gds_editor
end
should_be_an_admin_controller
@@ -34,48 +38,98 @@ class Admin::DocumentCollectionGroupDocumentSearchControllerTest < ActionControl
assert_template nil
end
- test "POST #search redirects to #search_title_slug if search option passed is title-or-slug" do
- @request_params[:search_option] = "title-or-slug"
+ test "POST #search redirects to #search_by_title if search option passed is title" do
+ @request_params[:search_option] = "title"
post :search, params: @request_params
- assert_redirected_to admin_document_collection_group_search_title_slug_path(@collection, @group)
+ assert_redirected_to admin_document_collection_group_search_by_title_path(@collection, @group)
end
- test "GET #search_title_slug without query renders search for title & slug page with no results section" do
- get :search_title_slug, params: @request_params
- assert_template "document_collection_group_document_search/search_title_slug"
+ test "GET #search_by_title without query renders search for title page with no results section" do
+ get :search_by_title, params: @request_params
+ assert_template "document_collection_group_document_search/search_by_title"
assert_select ".app-view-document-collection-document-search-results", count: 0
end
- view_test "GET #search_title_slug with a query returns the document with the query in the results" do
- edition = build(:consultation, title: "Something", document: build(:document, slug: "something"))
+ test "GET :search_by_title with search value passes title and default params to filter" do
+ stub_filter = stub_edition_filter({ editions: [], options: { per_page: 15 } })
+ edition_scope = Edition.with_translations(I18n.locale)
+ default_filter_params_with_title = @default_filter_params.merge(title: "Something")
+ @request_params[:title] = "Something"
+ Admin::EditionFilter.expects(:new).with(edition_scope, @user, default_filter_params_with_title).returns(stub_filter)
- mock_live_editions = mock
- mock_live_editions.expects(:with_title_containing).with("Something").once.returns([edition])
+ get :search_by_title, params: @request_params
+ assert_template "document_collection_group_document_search/search_by_title"
+ end
- Edition.expects(:published).once.returns(mock_live_editions)
+ view_test "GET #search_by_title with a query that returns no results renders empty results list" do
+ editions = []
+ stub_filter = stub_edition_filter({ editions:, options: { per_page: 15 } })
+ Admin::EditionFilter.stubs(:new).returns(stub_filter)
+ @request_params[:title] = "Something "
- @request_params[:query] = "Something "
- get :search_title_slug, params: @request_params
+ get :search_by_title, params: @request_params
+ assert_template "document_collection_group_document_search/search_by_title"
+ assert_select ".govuk-body", text: /No results found. Search again using the full URL./
+ end
- assert_template "document_collection_group_document_search/search_title_slug"
- assert_select ".govuk-table__row .govuk-table__cell a[href='#{edition.public_url}']", text: "View #{edition.title}"
+ view_test "GET #search_by_title with an empty query string shows an alert flash" do
+ @request_params[:title] = ""
+ get :search_by_title, params: @request_params
+ assert_template "document_collection_group_document_search/search_by_title"
+ assert_select ".gem-c-error-alert__message", text: /Please enter a search query/
end
- view_test "GET #search_title_slug with a query that returns no results renders empty results list" do
- mock_live_editions = mock
- mock_live_editions.expects(:with_title_containing).with("Something").once.returns([])
- Edition.expects(:published).once.returns(mock_live_editions)
+ view_test "GET :search_by_title with search value renders paginated results" do
+ editions = []
+ edition = build(:news_article, title: "Something", document: build(:document, slug: "something"))
+ 16.times { editions << edition }
+
+ stub_filter = stub_edition_filter({ editions:, options: { per_page: 15 } })
+ Admin::EditionFilter.stubs(:new).returns(stub_filter)
+ @request_params[:title] = "Something "
+
+ get :search_by_title, params: @request_params
+ assert_response :success
+ assert_template "document_collection_group_document_search/search_by_title"
+ assert_select "input[name='title']"
+ assert_select ".govuk-heading-s", "16 documents"
+ assert_select ".govuk-table" do
+ assert_select "tr", count: 15
+ end
+ assert_select "nav.govuk-pagination"
+ end
- @request_params[:query] = "Something "
- get :search_title_slug, params: @request_params
- assert_template "document_collection_group_document_search/search_title_slug"
- assert_select ".govuk-body", text: /No results found. Search again using the full URL./
+ view_test "GET :search_by_title with search value renders results without pagination if length of result is 15" do
+ editions = []
+ edition = build(:news_article, title: "Something", document: build(:document, slug: "something"))
+ 15.times { editions << edition }
+
+ stub_filter = stub_edition_filter({ editions:, options: { per_page: 15 } })
+ Admin::EditionFilter.stubs(:new).returns(stub_filter)
+ @request_params[:title] = "Something "
+
+ get :search_by_title, params: @request_params
+ assert_response :success
+ assert_template "document_collection_group_document_search/search_by_title"
+ assert_select "input[name='title']"
+ assert_select ".govuk-heading-s", "15 documents"
+ assert_select ".govuk-table" do
+ assert_select "tr", count: 15
+ end
+ assert_select "nav.govuk-pagination", count: 0
end
- view_test "GET #search_title_slug with an empty query string shows an alert flash" do
- @request_params[:query] = ""
- get :search_title_slug, params: @request_params
- assert_template "document_collection_group_document_search/search_title_slug"
- assert_select ".gem-c-error-alert__message", text: /Please enter a search query/
+private
+
+ def stub_edition_filter(attributes = {})
+ default_attributes = {
+ editions: Kaminari.paginate_array(attributes[:editions] || [], limit: attributes[:options][:per_page]).page(1),
+ page_title: "",
+ edition_state: "",
+ valid?: true,
+ options: {},
+ hide_type: false,
+ }
+ stub("edition filter", default_attributes.merge(attributes.except(:editions)))
end
end