View | Details | Raw Unified | Return to bug 37333
Collapse All | Expand All

(-)a/Koha/SearchFilter.pm (-1 / +2 lines)
Lines 51-57 sub expand_filter { Link Here
51
    my $operands = $query->{operands};
51
    my $operands = $query->{operands};
52
    my $indexes = $query->{indexes};
52
    my $indexes = $query->{indexes};
53
53
54
    my $query_limit;
54
    my $query_limit = "(";
55
    for( my $i = 0; $i < scalar @$operands; $i++ ){
55
    for( my $i = 0; $i < scalar @$operands; $i++ ){
56
        next unless @$operands[$i];
56
        next unless @$operands[$i];
57
        my $index = @$indexes[$i] ? @$indexes[$i] . "=" : "";
57
        my $index = @$indexes[$i] ? @$indexes[$i] . "=" : "";
Lines 61-66 sub expand_filter { Link Here
61
        my $limit = $operator . $index . $query;
61
        my $limit = $operator . $index . $query;
62
        $query_limit .= $limit;
62
        $query_limit .= $limit;
63
    }
63
    }
64
    $query_limit .= ")";
64
65
65
    return ($limits, $query_limit);
66
    return ($limits, $query_limit);
66
}
67
}
(-)a/t/db_dependent/Koha/SearchFilters.t (-1 / +77 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# Copyright 2024 ByWater Solutions
4
#
5
# This file is part of Koha
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use Test::More tests => 4;
23
24
use Koha::Database;
25
use Koha::SearchFilters;
26
27
use t::lib::Mocks;
28
use t::lib::TestBuilder;
29
30
my $schema = Koha::Database->new->schema;
31
$schema->storage->txn_begin;
32
33
my $original_count = Koha::SearchFilters->count();
34
35
my $search_filter = Koha::SearchFilter->new(
36
    {
37
        name         => "Test",
38
        query        => q|{"operands":["programming","internet"],"operators":["OR"],"indexes":["su","su"]}|,
39
        limits       => q|{"limits":[]}|,
40
        opac         => 1,
41
        staff_client => 1
42
    }
43
)->store;
44
45
is( Koha::SearchFilters->search()->count(), $original_count + 1, "New filter is added" );
46
is(
47
    Koha::SearchFilters->search( { opac => 1 } )->count(), $original_count + 1,
48
    "Searching by opac returns the filter if set"
49
);
50
$search_filter->opac(0)->store();
51
is(
52
    Koha::SearchFilters->search( { opac => 1 } )->count(), $original_count,
53
    "Searching by opac doesn't return the filter if not set"
54
);
55
56
subtest 'expand_filter tests' => sub {
57
58
    plan tests => 2;
59
60
    my $search_filter = Koha::SearchFilter->new(
61
        {
62
            name         => "Test",
63
            query        => q|{"operands":["programming","internet"],"operators":["OR"],"indexes":["su","su"]}|,
64
            limits       => q|{"limits":["mc-itype,phr:BK","fic:0"]}|,
65
            opac         => 1,
66
            staff_client => 1
67
        }
68
    )->store;
69
70
    my ( $limits, $query_limit ) = $search_filter->expand_filter();
71
72
    is_deeply( $limits, [ 'mc-itype,phr:BK', 'fic:0' ], "Limit from filter is correctly expanded" );
73
    is( $query_limit, '(su=(programming) OR su=(internet))', "Query from filter is correctly expanded and grouped" );
74
75
};
76
77
$schema->storage->txn_rollback;

Return to bug 37333