From b63f6d95f7eadba6aa7cbf44b130755c5b2ea7af Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 28 Apr 2022 14:42:04 +0200 Subject: [PATCH] Bug 30639: Split search terms for patron search If several terms are passed we should split them. Test plan: Search for "edna acosta" (without quotes) in the filters from the left side of the main patron search Do some regression tests Signed-off-by: Nick Clemens Signed-off-by: Marcel de Rooy Signed-off-by: Fridolin Somers --- .../prog/en/includes/patron-search.inc | 29 ++++++++++++++----- t/db_dependent/selenium/patrons_search.t | 28 +++++++++++++++++- 2 files changed, 49 insertions(+), 8 deletions(-) diff --git a/koha-tmpl/intranet-tmpl/prog/en/includes/patron-search.inc b/koha-tmpl/intranet-tmpl/prog/en/includes/patron-search.inc index 34f8b8597d..7dddb52339 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/includes/patron-search.inc +++ b/koha-tmpl/intranet-tmpl/prog/en/includes/patron-search.inc @@ -309,8 +309,9 @@ return { "like": start_with + "%" } }, "-and": function(){ - let filter = $("#search_patron_filter").val(); - if (!filter) return ""; + let pattern = $("#search_patron_filter").val(); + if (!pattern) return ""; + let patterns = pattern.split(' ').filter(function(s){ return s.length }); let filters = []; let search_type = $("#searchtype_filter").val() || "contain"; @@ -318,14 +319,28 @@ if ( !search_fields ) { search_fields = "[% Koha.Preference('DefaultPatronSearchFields') || 'firstname,surname,othernames,cardnumber,userid' | html %]"; } - search_fields.split(',').forEach(function(e,i){ - filters.push({["me."+e]:{"like":(search_type == "contain" ? "%" : "" ) + filter + "%"}}); + + let subquery_and = []; + patterns.forEach(function(pattern,i){ + let sub_or = []; + search_fields.split(',').forEach(function(attr,ii){ + sub_or.push({["me."+attr]:{"like":(search_type == "contain" ? "%" : "" ) + pattern + "%"}}); + }); + subquery_and.push(sub_or); }); + filters.push({"-and": subquery_and}); + [% IF Koha.Preference('ExtendedPatronAttributes') && extended_attribute_types %] - filters.push({ - "extended_attributes.value": { "like": "%" + filter + (search_type == "contain" ? "%" : "" )}, - "extended_attributes.code": extended_attribute_types + subquery_and = []; + patterns.forEach(function(pattern,i){ + let sub_or = []; + sub_or.push({ + "extended_attributes.value": { "like": "%" + pattern + (search_type == "contain" ? "%" : "" )}, + "extended_attributes.code": extended_attribute_types + }); + subquery_and.push(sub_or); }); + filters.push({"-and": subquery_and}); [% END %] return filters; } diff --git a/t/db_dependent/selenium/patrons_search.t b/t/db_dependent/selenium/patrons_search.t index eb2014fd97..cd63599192 100755 --- a/t/db_dependent/selenium/patrons_search.t +++ b/t/db_dependent/selenium/patrons_search.t @@ -51,7 +51,7 @@ my $base_url = $s->base_url; my $builder = t::lib::TestBuilder->new; subtest 'Search patrons' => sub { - plan tests => 23; + plan tests => 24; if ( Koha::Patrons->search({surname => {-like => "test_patron_%"}})->count ) { BAIL_OUT("Cannot run this test, data we need to create already exist in the DB"); @@ -91,6 +91,22 @@ subtest 'Search patrons' => sub { } ); } + + push @patrons, $builder->build_object( + { + class => 'Koha::Patrons', + value => { + surname => "test", + firstname => "not_p_a_t_r_o_n", # won't match 'patron' + categorycode => $patron_category->categorycode, + branchcode => $library->branchcode, + borrowernotes => $borrowernotes, + address => $address, + email => $email, + } + } + ); + my $library_2 = $builder->build_object( { class => 'Koha::Libraries', value => { branchname => 'X' . $branchname } } ); @@ -221,6 +237,16 @@ subtest 'Search patrons' => sub { # And make sure all the patrons are present is( $driver->find_element('//div[@id="'.$table_id.'_info"]')->get_text, sprintf('Showing 1 to %s of %s entries', $PatronsPerPage, $total_number_of_patrons), 'Resetting filters works as expected' ); + # Pattern terms must be split + $s->fill_form( { search_patron_filter => 'test patron' } ); + $s->submit_form; + + $s->wait_for_ajax; + is( $driver->find_element('//div[@id="'.$table_id.'_info"]')->get_text, sprintf('Showing 1 to %s of %s entries (filtered from %s total entries)', $PatronsPerPage, 26, $total_number_of_patrons) ); + $driver->find_element('//form[@id="patron_search_form"]//*[@id="clear_search"]')->click(); + $s->submit_form; + $s->wait_for_ajax; + # Search on non-searchable attribute, we expect no result! $s->fill_form( { search_patron_filter => 'test_attr_1' } ); $s->submit_form; -- 2.39.5