Bug 34024: (QA follow-up) Allow pickup location changing for in transit holds
[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=operator&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=AND&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, "query  parsed_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_desc  index_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=operator&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=operator&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 $xml_record = q{
193 <record
194     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
195     xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd"
196     xmlns="http://www.loc.gov/MARC21/slim">
197
198   <leader>00144    a2200073   4500</leader>
199   <datafield tag="245" ind1=" " ind2=" ">
200     <subfield code="a">Some boring read</subfield>
201   </datafield>
202   <datafield tag="100" ind1=" " ind2=" ">
203     <subfield code="a">Some boring author</subfield>
204   </datafield>
205   <datafield tag="942" ind1=" " ind2=" ">
206     <subfield code="c">gOlAIZMF</subfield>
207   </datafield>
208   <datafield tag="952" ind1=" " ind2=" ">
209     <subfield code="0">0</subfield>
210     <subfield code="1">0</subfield>
211     <subfield code="4">0</subfield>
212     <subfield code="7">0</subfield>
213     <subfield code="9">1117</subfield>
214     <subfield code="a">D6C8Pj</subfield>
215     <subfield code="b">D6C8Pj</subfield>
216     <subfield code="d">2023-03-31</subfield>
217     <subfield code="l">0</subfield>
218     <subfield code="p">g57ad1Zn3NOYZ</subfield>
219     <subfield code="r">2023-03-31</subfield>
220     <subfield code="w">2023-03-31</subfield>
221     <subfield code="y">gOlAIZMF</subfield>
222   </datafield>
223   <datafield tag="999" ind1=" " ind2=" ">
224     <subfield code="c">553</subfield>
225     <subfield code="d">553</subfield>
226   </datafield>
227 </record>
228 };
229     t::lib::Mocks::mock_preference('PassItemMarcToXSLT','1');
230
231     # The routine uses a count of items in DB to determine if record should be hidden.
232     # Our item is not in the DB, so we avoid hiding the record which would
233     # mean we don't call XSLTParse4Display.
234     # Also ensure item is not hidden
235     t::lib::Mocks::mock_preference('OpacHiddenItems','');
236     t::lib::Mocks::mock_preference('OpacHiddenItemsHidesRecord','0');
237
238     warnings_like { C4::Search::searchResults({ interface => "opac" },"test",1,1,0,0,[ $xml_record ] ,undef) }
239         [qr/Item here/],
240         "Item field returned from default XSLT if pref set";
241
242     t::lib::Mocks::mock_preference('PassItemMarcToXSLT','0');
243
244     warnings_like { C4::Search::searchResults({ interface => "opac" },"test",1,1,0,0,[ $xml_record ] ,undef) }
245         [qr/No item/],
246         "Item field returned from default XSLT if pref set";
247
248 }