3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20 use Test::More tests => 2;
22 use t::lib::TestBuilder;
26 use Koha::SearchEngine::Zebra::QueryBuilder;
28 my $schema = Koha::Database->new->schema;
30 subtest 'build_query_compat() SearchLimitLibrary tests' => sub {
34 $schema->storage->txn_begin;
36 my $builder = t::lib::TestBuilder->new;
38 my $branch_1 = $builder->build_object({ class => 'Koha::Libraries' });
39 my $branch_2 = $builder->build_object({ class => 'Koha::Libraries' });
40 my $group = $builder->build_object({ class => 'Koha::Library::Groups', value => {
41 ft_search_groups_opac => 1,
42 ft_search_groups_staff => 1,
47 my $group_1 = $builder->build_object({ class => 'Koha::Library::Groups', value => {
48 parent_id => $group->id,
49 branchcode => $branch_1->id
52 my $group_2 = $builder->build_object({ class => 'Koha::Library::Groups', value => {
53 parent_id => $group->id,
54 branchcode => $branch_2->id
57 my $groupid = $group->id;
58 my @branchcodes = sort { $a cmp $b } ( $branch_1->id, $branch_2->id );
61 my $query_builder = Koha::SearchEngine::Zebra::QueryBuilder->new({index => $Koha::SearchEngine::BIBLIOS_INDEX});
62 t::lib::Mocks::mock_preference('SearchLimitLibrary', 'both');
63 my ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc, undef ) =
64 $query_builder->build_query_compat( undef, undef, undef, [ "branch:CPL" ], undef, undef, undef, undef );
65 is( $limit, "(homebranch= CPL or holdingbranch= CPL)", "Branch limit expanded to home/holding branch");
66 is( $limit_desc, "(homebranch: CPL or holdingbranch: CPL)", "Limit description correctly expanded");
67 is( $limit_cgi, "&limit=branch%3ACPL", "Limit cgi does not get expanded");
68 ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc, undef ) =
69 $query_builder->build_query_compat( undef, undef, undef, [ "multibranchlimit:$groupid" ], undef, undef, undef, undef );
70 is( $limit, "(homebranch= $branchcodes[0] or homebranch= $branchcodes[1] or holdingbranch= $branchcodes[0] or holdingbranch= $branchcodes[1])", "Multibranch limit expanded to home/holding branches");
71 is( $limit_desc, "(homebranch: $branchcodes[0] or homebranch: $branchcodes[1] or holdingbranch: $branchcodes[0] or holdingbranch: $branchcodes[1])", "Multibranch limit description correctly expanded");
72 is( $limit_cgi, "&limit=multibranchlimit%3A$groupid", "Multibranch limit cgi does not get expanded");
74 t::lib::Mocks::mock_preference('SearchLimitLibrary', 'homebranch');
75 ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc, undef ) =
76 $query_builder->build_query_compat( undef, undef, undef, [ "branch:CPL" ], undef, undef, undef, undef );
77 is( $limit, "(homebranch= CPL)", "branch limit expanded to home branch");
78 is( $limit_desc, "(homebranch: CPL)", "limit description correctly expanded");
79 is( $limit_cgi, "&limit=branch%3ACPL", "limit cgi does not get expanded");
80 ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc, undef ) =
81 $query_builder->build_query_compat( undef, undef, undef, [ "multibranchlimit:$groupid" ], undef, undef, undef, undef );
82 is( $limit, "(homebranch= $branchcodes[0] or homebranch= $branchcodes[1])", "branch limit expanded to home branch");
83 is( $limit_desc, "(homebranch: $branchcodes[0] or homebranch: $branchcodes[1])", "limit description correctly expanded");
84 is( $limit_cgi, "&limit=multibranchlimit%3A$groupid", "Limit cgi does not get expanded");
86 t::lib::Mocks::mock_preference('SearchLimitLibrary', 'holdingbranch');
87 ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc, undef ) =
88 $query_builder->build_query_compat( undef, undef, undef, [ "branch:CPL" ], undef, undef, undef, undef );
89 is( $limit, "(holdingbranch= CPL)", "branch limit expanded to holding branch");
90 is( $limit_desc, "(holdingbranch: CPL)", "Limit description correctly expanded");
91 is( $limit_cgi, "&limit=branch%3ACPL", "Limit cgi does not get expanded");
92 ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc, undef ) =
93 $query_builder->build_query_compat( undef, undef, undef, [ "multibranchlimit:$groupid" ], undef, undef, undef, undef );
94 is( $limit, "(holdingbranch= $branchcodes[0] or holdingbranch= $branchcodes[1])", "branch limit expanded to holding branch");
95 is( $limit_desc, "(holdingbranch: $branchcodes[0] or holdingbranch: $branchcodes[1])", "Limit description correctly expanded");
96 is( $limit_cgi, "&limit=multibranchlimit%3A$groupid", "Limit cgi does not get expanded");
100 subtest "Handle search filters" => sub {
106 $qb = Koha::SearchEngine::Zebra::QueryBuilder->new({ 'index' => 'biblios' }),
107 'Creating new query builder object for biblios'
110 my $filter = Koha::SearchFilter->new({
112 query => q|{"operands":["cat","bat","rat"],"indexes":["kw","ti","au"],"operators":["AND","OR"]}|,
113 limits => q|{"limits":["mc-itype,phr:BK","available"]}|,
115 my $filter_id = $filter->id;
117 my ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc ) = $qb->build_query_compat( undef, undef, undef, ["search_filter:$filter_id"] );
119 is( $limit,q{(kw=(cat) AND ti=(bat) OR au=(rat)) and (mc-itype,phr=BK) and (( (allrecords,AlwaysMatches='') and (not-onloan-count,st-numeric >= 1) and (lost,st-numeric=0) ))},"Limit correctly formed");
120 is( $limit_cgi,"&limit=search_filter%3A$filter_id","CGI limit is not expanded");
121 is( $limit_desc,q{(kw=(cat) AND ti=(bat) OR au=(rat)) and (mc-itype,phr=BK) and (( (allrecords,AlwaysMatches='') and (not-onloan-count,st-numeric >= 1) and (lost,st-numeric=0) ))},"Limit description is correctly expanded");