From 6cb3d72f49b519c101c61e7d64cab8e3286085f7 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Thu, 11 Jul 2024 19:38:16 +0000 Subject: [PATCH] Bug 37333: Group search terms in a saved filter This patch surrounds the expanded query of a search filter in parnetheses to correctly group the terms when searching To test: 1 - Enable system preference SavedSearchFilters 2 - In staff client, perform an advanced search for: subject:internet OR subject:programming 3 - Note results 4 - Save search as filter, enabled in staff client and opac 5 - Search for 'a' 6 - Apply filter from top of facets on left hand side 7 - Note results didn't change 8 - Apply patch, restart all 9 - Reload search, it now limits correctly 10 - Test on OPAC 11 - Test on both Zebra and Elastic 12 - Sign off :-) Signed-off-by: Jake Deery Signed-off-by: Martin Renvoize Signed-off-by: Katrin Fischer --- Koha/SearchFilter.pm | 3 +- t/db_dependent/Koha/SearchFilters.t | 77 +++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100755 t/db_dependent/Koha/SearchFilters.t diff --git a/Koha/SearchFilter.pm b/Koha/SearchFilter.pm index 3683a89b28..9694c1e31c 100644 --- a/Koha/SearchFilter.pm +++ b/Koha/SearchFilter.pm @@ -51,7 +51,7 @@ sub expand_filter { my $operands = $query->{operands}; my $indexes = $query->{indexes}; - my $query_limit; + my $query_limit = "("; for( my $i = 0; $i < scalar @$operands; $i++ ){ next unless @$operands[$i]; my $index = @$indexes[$i] ? @$indexes[$i] . "=" : ""; @@ -61,6 +61,7 @@ sub expand_filter { my $limit = $operator . $index . $query; $query_limit .= $limit; } + $query_limit .= ")"; return ($limits, $query_limit); } diff --git a/t/db_dependent/Koha/SearchFilters.t b/t/db_dependent/Koha/SearchFilters.t new file mode 100755 index 0000000000..81a304ba27 --- /dev/null +++ b/t/db_dependent/Koha/SearchFilters.t @@ -0,0 +1,77 @@ +#!/usr/bin/perl + +# Copyright 2024 ByWater Solutions +# +# This file is part of Koha +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Test::More tests => 4; + +use Koha::Database; +use Koha::SearchFilters; + +use t::lib::Mocks; +use t::lib::TestBuilder; + +my $schema = Koha::Database->new->schema; +$schema->storage->txn_begin; + +my $original_count = Koha::SearchFilters->count(); + +my $search_filter = Koha::SearchFilter->new( + { + name => "Test", + query => q|{"operands":["programming","internet"],"operators":["OR"],"indexes":["su","su"]}|, + limits => q|{"limits":[]}|, + opac => 1, + staff_client => 1 + } +)->store; + +is( Koha::SearchFilters->search()->count(), $original_count + 1, "New filter is added" ); +is( + Koha::SearchFilters->search( { opac => 1 } )->count(), $original_count + 1, + "Searching by opac returns the filter if set" +); +$search_filter->opac(0)->store(); +is( + Koha::SearchFilters->search( { opac => 1 } )->count(), $original_count, + "Searching by opac doesn't return the filter if not set" +); + +subtest 'expand_filter tests' => sub { + + plan tests => 2; + + my $search_filter = Koha::SearchFilter->new( + { + name => "Test", + query => q|{"operands":["programming","internet"],"operators":["OR"],"indexes":["su","su"]}|, + limits => q|{"limits":["mc-itype,phr:BK","fic:0"]}|, + opac => 1, + staff_client => 1 + } + )->store; + + my ( $limits, $query_limit ) = $search_filter->expand_filter(); + + is_deeply( $limits, [ 'mc-itype,phr:BK', 'fic:0' ], "Limit from filter is correctly expanded" ); + is( $query_limit, '(su=(programming) OR su=(internet))', "Query from filter is correctly expanded and grouped" ); + +}; + +$schema->storage->txn_rollback; -- 2.39.5