From e6bbf4e19820df095d056f696a97d3f50395cb07 Mon Sep 17 00:00:00 2001 From: Jonathan Druart <jonathan.druart@bugs.koha-community.org> Date: Mon, 10 Feb 2025 14:10:46 +0100 Subject: [PATCH] Bug 39020: Add tests Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org> --- t/db_dependent/Koha/SearchFilters.t | 30 +++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) diff --git a/t/db_dependent/Koha/SearchFilters.t b/t/db_dependent/Koha/SearchFilters.t index 81a304ba273..b576e8f545a 100755 --- a/t/db_dependent/Koha/SearchFilters.t +++ b/t/db_dependent/Koha/SearchFilters.t @@ -20,6 +20,7 @@ use Modern::Perl; use Test::More tests => 4; +use JSON qw( encode_json ); use Koha::Database; use Koha::SearchFilters; @@ -30,7 +31,8 @@ use t::lib::TestBuilder; my $schema = Koha::Database->new->schema; $schema->storage->txn_begin; -my $original_count = Koha::SearchFilters->count(); +my $original_count = Koha::SearchFilters->search->count(); +my $original_count_opac = Koha::SearchFilters->search( { opac => 1 } )->count(); my $search_filter = Koha::SearchFilter->new( { @@ -44,18 +46,18 @@ my $search_filter = Koha::SearchFilter->new( is( Koha::SearchFilters->search()->count(), $original_count + 1, "New filter is added" ); is( - Koha::SearchFilters->search( { opac => 1 } )->count(), $original_count + 1, + Koha::SearchFilters->search( { opac => 1 } )->count(), $original_count_opac + 1, "Searching by opac returns the filter if set" ); $search_filter->opac(0)->store(); is( - Koha::SearchFilters->search( { opac => 1 } )->count(), $original_count, + Koha::SearchFilters->search( { opac => 1 } )->count(), $original_count_opac, "Searching by opac doesn't return the filter if not set" ); subtest 'expand_filter tests' => sub { - plan tests => 2; + plan tests => 4; my $search_filter = Koha::SearchFilter->new( { @@ -72,6 +74,26 @@ subtest 'expand_filter tests' => sub { 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" ); + my $empty_query = { + indexes => [ ("kw") x 3 ], + operands => [ ("") x 3 ], + operators => [], + }; + $search_filter = Koha::SearchFilter->new( + { + name => "Test", + query => encode_json($empty_query), + limits => q|{"limits":["mc-itype,phr:BK","fic:0"]}|, + opac => 1, + staff_client => 1 + } + )->store; + + ( $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, '', "Empty query does not generate anything" ); + }; $schema->storage->txn_rollback; -- 2.34.1