Bug 34972: Add tests for ModReservesCancelAll
[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 Test::More tests => 2;
21 use Test::MockModule;
22 use Test::Warn;
23 use t::lib::Mocks;
24
25 use C4::Search;
26
27 subtest "_build_initial_query tests" => sub {
28
29     plan tests => 20;
30
31     my ($query,$query_cgi,$query_desc,$previous_operand);
32     # all params have values
33     my $params = {
34         query            => "query",
35         query_cgi        => "query_cgi",
36         query_desc       => "query_desc",
37         operator         => "operator",
38         parsed_operand   => "parsed_operand",
39         original_operand => "original_operand",
40         index            => "index",
41         index_plus       => "index_plus",
42         indexes_set      => "indexes_set",
43         previous_operand => "previous_operand"
44     };
45
46     ($query,$query_cgi,$query_desc,$previous_operand) =
47         C4::Search::_build_initial_query($params);
48     is( $query, "query operator parsed_operand",
49         "\$query built correctly");
50     is( $query_cgi, "query_cgi&op=operator&idx=index&q=original_operand",
51         "\$query_cgi built correctly");
52     is( $query_desc, "query_desc operator index_plus original_operand",
53         "\$query_desc build correctly");
54     is( $previous_operand, "previous_operand",
55         "\$query build correctly");
56
57     # no operator
58     $params = {
59         query            => "query",
60         query_cgi        => "query_cgi",
61         query_desc       => "query_desc",
62         operator         => undef,
63         parsed_operand   => "parsed_operand",
64         original_operand => "original_operand",
65         index            => "index",
66         index_plus       => "index_plus",
67         indexes_set      => "indexes_set",
68         previous_operand => "previous_operand"
69     };
70
71     ($query,$query_cgi,$query_desc,$previous_operand) =
72         C4::Search::_build_initial_query($params);
73     is( $query, "query AND parsed_operand",
74         "\$query built correctly (no operator)");
75     is( $query_cgi, "query_cgi&op=AND&idx=index&q=original_operand",
76         "\$query_cgi built correctly (no operator)");
77     is( $query_desc, "query_desc AND index_plus original_operand",
78         "\$query_desc build correctly (no operator)");
79     is( $previous_operand, "previous_operand",
80         "\$query build correctly (no operator)");
81
82     # no previous operand
83     $params = {
84         query            => "query",
85         query_cgi        => "query_cgi",
86         query_desc       => "query_desc",
87         operator         => "operator",
88         parsed_operand   => "parsed_operand",
89         original_operand => "original_operand",
90         index            => "index",
91         index_plus       => "index_plus",
92         indexes_set      => "indexes_set",
93         previous_operand => undef
94     };
95
96     ($query,$query_cgi,$query_desc,$previous_operand) =
97         C4::Search::_build_initial_query($params);
98     is( $query, "query  parsed_operand",
99         "\$query built correctly (no previous operand)");
100     is( $query_cgi, "query_cgi&idx=index&q=original_operand",
101         "\$query_cgi built correctly (no previous operand)");
102     is( $query_desc, "query_desc  index_plus original_operand",
103         "\$query_desc build correctly (no previous operand)");
104     is( $previous_operand, 1,
105         "\$query build correctly (no previous operand)");
106
107     # no index passed
108     $params = {
109         query            => "query",
110         query_cgi        => "query_cgi",
111         query_desc       => "query_desc",
112         operator         => "operator",
113         parsed_operand   => "parsed_operand",
114         original_operand => "original_operand",
115         index            => undef,
116         index_plus       => "index_plus",
117         indexes_set      => "indexes_set",
118         previous_operand => "previous_operand"
119     };
120
121     ($query,$query_cgi,$query_desc,$previous_operand) =
122         C4::Search::_build_initial_query($params);
123     is( $query, "query operator parsed_operand",
124         "\$query built correctly (no index passed)");
125     is( $query_cgi, "query_cgi&op=operator&q=original_operand",
126         "\$query_cgi built correctly (no index passed)");
127     is( $query_desc, "query_desc operator index_plus original_operand",
128         "\$query_desc build correctly (no index passed)");
129     is( $previous_operand, "previous_operand",
130         "\$query build correctly (no index passed)");
131
132     # no index_plus passed
133     $params = {
134         query            => "query",
135         query_cgi        => "query_cgi",
136         query_desc       => "query_desc",
137         operator         => "operator",
138         parsed_operand   => "parsed_operand",
139         original_operand => "original_operand",
140         index            => "index",
141         index_plus       => undef,
142         indexes_set      => "indexes_set",
143         previous_operand => "previous_operand"
144     };
145
146     ($query,$query_cgi,$query_desc,$previous_operand) =
147         C4::Search::_build_initial_query($params);
148     is( $query, "query operator parsed_operand",
149         "\$query built correctly (no index_plus passed)");
150     is( $query_cgi, "query_cgi&op=operator&idx=index&q=original_operand",
151         "\$query_cgi built correctly (no index_plus passed)");
152     is( $query_desc, "query_desc operator  original_operand",
153         "\$query_desc build correctly (no index_plus passed)");
154     is( $previous_operand, "previous_operand",
155         "\$query build correctly (no index_plus passed)");
156
157 };
158
159 subtest "searchResults PassItemMarcToXSLT test" => sub {
160
161     plan tests => 2;
162
163     t::lib::Mocks::mock_preference('OPACXSLTResultsDisplay','default');
164     t::lib::Mocks::mock_preference('marcflavour','MARC21');
165     my $mock_xslt = Test::MockModule->new("C4::Search");
166     $mock_xslt->mock( XSLTParse4Display => sub {
167         my $params = shift;
168         my $record = $params->{record};
169         warn $record->field('952') ? "Item here" : "No item";
170         return;
171     });
172
173     my $xml_record = q{
174 <record
175     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
176     xsi:schemaLocation="http://www.loc.gov/MARC21/slim http://www.loc.gov/standards/marcxml/schema/MARC21slim.xsd"
177     xmlns="http://www.loc.gov/MARC21/slim">
178
179   <leader>00144    a2200073   4500</leader>
180   <datafield tag="245" ind1=" " ind2=" ">
181     <subfield code="a">Some boring read</subfield>
182   </datafield>
183   <datafield tag="100" ind1=" " ind2=" ">
184     <subfield code="a">Some boring author</subfield>
185   </datafield>
186   <datafield tag="942" ind1=" " ind2=" ">
187     <subfield code="c">gOlAIZMF</subfield>
188   </datafield>
189   <datafield tag="952" ind1=" " ind2=" ">
190     <subfield code="0">0</subfield>
191     <subfield code="1">0</subfield>
192     <subfield code="4">0</subfield>
193     <subfield code="7">0</subfield>
194     <subfield code="9">1117</subfield>
195     <subfield code="a">D6C8Pj</subfield>
196     <subfield code="b">D6C8Pj</subfield>
197     <subfield code="d">2023-03-31</subfield>
198     <subfield code="l">0</subfield>
199     <subfield code="p">g57ad1Zn3NOYZ</subfield>
200     <subfield code="r">2023-03-31</subfield>
201     <subfield code="w">2023-03-31</subfield>
202     <subfield code="y">gOlAIZMF</subfield>
203   </datafield>
204   <datafield tag="999" ind1=" " ind2=" ">
205     <subfield code="c">553</subfield>
206     <subfield code="d">553</subfield>
207   </datafield>
208 </record>
209 };
210     t::lib::Mocks::mock_preference('PassItemMarcToXSLT','1');
211
212     # The routine uses a count of items in DB to determine if record should be hidden.
213     # Our item is not in the DB, so we avoid hiding the record which would
214     # mean we don't call XSLTParse4Display.
215     # Also ensure item is not hidden
216     t::lib::Mocks::mock_preference('OpacHiddenItems','');
217     t::lib::Mocks::mock_preference('OpacHiddenItemsHidesRecord','0');
218
219     warnings_like { C4::Search::searchResults({ interface => "opac" },"test",1,1,0,0,[ $xml_record ] ,undef) }
220         [qr/Item here/],
221         "Item field returned from default XSLT if pref set";
222
223     t::lib::Mocks::mock_preference('PassItemMarcToXSLT','0');
224
225     warnings_like { C4::Search::searchResults({ interface => "opac" },"test",1,1,0,0,[ $xml_record ] ,undef) }
226         [qr/No item/],
227         "Item field returned from default XSLT if pref set";
228
229 }