]> git.koha-community.org Git - koha.git/blob - t/Search.t
Bug 25616: [21.11.x] (follow-up) Fix t/Search.t
[koha.git] / t / Search.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 C4::Biblio;
21 use Test::More;
22 use Test::MockModule;
23 use Test::Warn;
24 use t::lib::Mocks;
25 use t::lib::TestBuilder;
26
27 use Module::Load::Conditional qw/check_install/;
28
29 BEGIN {
30     if ( check_install( module => 'Test::DBIx::Class' ) ) {
31         plan tests => 4;
32     } else {
33         plan skip_all => "Need Test::DBIx::Class"
34     }
35 }
36
37 # Mock the DB connexion and C4::Context
38 use Test::DBIx::Class;
39
40 use_ok('C4::Search');
41 can_ok('C4::Search',
42     qw/_build_initial_query/);
43
44 subtest "_build_initial_query tests" => sub {
45
46     plan tests => 20;
47
48     my ($query,$query_cgi,$query_desc,$previous_operand);
49     # all params have values
50     my $params = {
51         query            => "query",
52         query_cgi        => "query_cgi",
53         query_desc       => "query_desc",
54         operator         => "operator",
55         parsed_operand   => "parsed_operand",
56         original_operand => "original_operand",
57         index            => "index",
58         index_plus       => "index_plus",
59         indexes_set      => "indexes_set",
60         previous_operand => "previous_operand"
61     };
62
63     ($query,$query_cgi,$query_desc,$previous_operand) =
64         C4::Search::_build_initial_query($params);
65     is( $query, "query operator parsed_operand",
66         "\$query built correctly");
67     is( $query_cgi, "query_cgi&op=%20operator%20&idx=index&q=original_operand",
68         "\$query_cgi built correctly");
69     is( $query_desc, "query_desc operator index_plus original_operand",
70         "\$query_desc build correctly");
71     is( $previous_operand, "previous_operand",
72         "\$query build correctly");
73
74     # no operator
75     $params = {
76         query            => "query",
77         query_cgi        => "query_cgi",
78         query_desc       => "query_desc",
79         operator         => undef,
80         parsed_operand   => "parsed_operand",
81         original_operand => "original_operand",
82         index            => "index",
83         index_plus       => "index_plus",
84         indexes_set      => "indexes_set",
85         previous_operand => "previous_operand"
86     };
87
88     ($query,$query_cgi,$query_desc,$previous_operand) =
89         C4::Search::_build_initial_query($params);
90     is( $query, "query AND parsed_operand",
91         "\$query built correctly (no operator)");
92     is( $query_cgi, "query_cgi&op=%20AND%20&idx=index&q=original_operand",
93         "\$query_cgi built correctly (no operator)");
94     is( $query_desc, "query_desc AND index_plus original_operand",
95         "\$query_desc build correctly (no operator)");
96     is( $previous_operand, "previous_operand",
97         "\$query build correctly (no operator)");
98
99     # no previous operand
100     $params = {
101         query            => "query",
102         query_cgi        => "query_cgi",
103         query_desc       => "query_desc",
104         operator         => "operator",
105         parsed_operand   => "parsed_operand",
106         original_operand => "original_operand",
107         index            => "index",
108         index_plus       => "index_plus",
109         indexes_set      => "indexes_set",
110         previous_operand => undef
111     };
112
113     ($query,$query_cgi,$query_desc,$previous_operand) =
114         C4::Search::_build_initial_query($params);
115     is( $query, "queryparsed_operand",
116         "\$query built correctly (no previous operand)");
117     is( $query_cgi, "query_cgi&idx=index&q=original_operand",
118         "\$query_cgi built correctly (no previous operand)");
119     is( $query_desc, "query_descindex_plus original_operand",
120         "\$query_desc build correctly (no previous operand)");
121     is( $previous_operand, 1,
122         "\$query build correctly (no previous operand)");
123
124     # no index passed
125     $params = {
126         query            => "query",
127         query_cgi        => "query_cgi",
128         query_desc       => "query_desc",
129         operator         => "operator",
130         parsed_operand   => "parsed_operand",
131         original_operand => "original_operand",
132         index            => undef,
133         index_plus       => "index_plus",
134         indexes_set      => "indexes_set",
135         previous_operand => "previous_operand"
136     };
137
138     ($query,$query_cgi,$query_desc,$previous_operand) =
139         C4::Search::_build_initial_query($params);
140     is( $query, "query operator parsed_operand",
141         "\$query built correctly (no index passed)");
142     is( $query_cgi, "query_cgi&op=%20operator%20&q=original_operand",
143         "\$query_cgi built correctly (no index passed)");
144     is( $query_desc, "query_desc operator index_plus original_operand",
145         "\$query_desc build correctly (no index passed)");
146     is( $previous_operand, "previous_operand",
147         "\$query build correctly (no index passed)");
148
149     # no index_plus passed
150     $params = {
151         query            => "query",
152         query_cgi        => "query_cgi",
153         query_desc       => "query_desc",
154         operator         => "operator",
155         parsed_operand   => "parsed_operand",
156         original_operand => "original_operand",
157         index            => "index",
158         index_plus       => undef,
159         indexes_set      => "indexes_set",
160         previous_operand => "previous_operand"
161     };
162
163     ($query,$query_cgi,$query_desc,$previous_operand) =
164         C4::Search::_build_initial_query($params);
165     is( $query, "query operator parsed_operand",
166         "\$query built correctly (no index_plus passed)");
167     is( $query_cgi, "query_cgi&op=%20operator%20&idx=index&q=original_operand",
168         "\$query_cgi built correctly (no index_plus passed)");
169     is( $query_desc, "query_desc operator  original_operand",
170         "\$query_desc build correctly (no index_plus passed)");
171     is( $previous_operand, "previous_operand",
172         "\$query build correctly (no index_plus passed)");
173
174 };
175
176 subtest "searchResults PassItemMarcToXSLT test" => sub {
177
178     plan tests => 2;
179
180     t::lib::Mocks::mock_preference('OPACXSLTResultsDisplay','default');
181     t::lib::Mocks::mock_preference('marcflavour','MARC21');
182     my $mock_xslt = Test::MockModule->new("C4::Search");
183     $mock_xslt->mock( XSLTParse4Display => sub {
184         my $params = shift;
185         my $record = $params->{record};
186         warn $record->field('952') ? "Item here" : "No item";
187         return;
188     });
189
190     my $builder = t::lib::TestBuilder->new;
191
192     my $item = $builder->build_sample_item();
193     my $record = $item->biblio->metadata->record;
194     C4::Biblio::EmbedItemsInMarcBiblio({ marc_record => $record, biblionumber => $item->biblionumber });
195
196     t::lib::Mocks::mock_preference('PassItemMarcToXSLT','1');
197
198     warnings_like { C4::Search::searchResults({ interface => "opac" },"test",1,1,0,0,[ $record->as_xml_record ] ,undef) }
199         [qr/Item here/],
200         "Item field returned from default XSLT if pref set";
201
202     t::lib::Mocks::mock_preference('PassItemMarcToXSLT','0');
203
204     warnings_like { C4::Search::searchResults({ interface => "opac" },"test",1,1,0,0,[ $record->as_xml_record ] ,undef) }
205         [qr/No item/],
206         "Item field returned from default XSLT if pref set";
207
208 }