Bug 17170: Unit tests
[koha.git] / t / db_dependent / Koha / SearchEngine / Zebra / QueryBuilder.t
1 #!/usr/bin/perl
2 #
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 2;
21 use t::lib::Mocks;
22 use t::lib::TestBuilder;
23 use Test::MockModule;
24
25 use Koha::Database;
26 use Koha::SearchEngine::Zebra::QueryBuilder;
27
28 my $schema = Koha::Database->new->schema;
29
30 subtest 'build_query_compat() SearchLimitLibrary tests' => sub {
31
32     plan tests => 18;
33
34     $schema->storage->txn_begin;
35
36     my $builder = t::lib::TestBuilder->new;
37
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,
43             parent_id => undef,
44             branchcode => undef
45         }
46     });
47     my $group_1  = $builder->build_object({ class => 'Koha::Library::Groups', value => {
48             parent_id => $group->id,
49             branchcode => $branch_1->id
50         }
51     });
52     my $group_2  = $builder->build_object({ class => 'Koha::Library::Groups', value => {
53             parent_id => $group->id,
54             branchcode => $branch_2->id
55         }
56     });
57     my $groupid = $group->id;
58     my @branchcodes = sort { $a cmp $b } ( $branch_1->id, $branch_2->id );
59
60
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");
73
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");
85
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");
97
98 };
99
100 subtest "Handle search filters" => sub {
101     plan tests => 4;
102
103     my $qb;
104
105     ok(
106         $qb = Koha::SearchEngine::Zebra::QueryBuilder->new({ 'index' => 'biblios' }),
107         'Creating new query builder object for biblios'
108     );
109
110     my $filter = Koha::SearchFilter->new({
111         name => "test",
112         query => q|{"operands":["cat","bat","rat"],"indexes":["kw","ti","au"],"operators":["AND","OR"]}|,
113         limits => q|{"limits":["mc-itype,phr:BK","available"]}|,
114     })->store;
115     my $filter_id = $filter->id;
116
117     my ( undef, undef, undef, undef, undef, $limit, $limit_cgi, $limit_desc ) = $qb->build_query_compat( undef, undef, undef, ["search_filter:$filter_id"] );
118
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");
122
123 };