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>.
23 use Module::Load::Conditional qw/check_install/;
26 if ( check_install( module => 'Test::DBIx::Class' ) ) {
29 plan skip_all => "Need Test::DBIx::Class"
33 # Mock the DB connection and C4::Context
34 use Test::DBIx::Class;
39 use_ok("Net::Z3950::ZOOM");
41 #FIXME: would it be better to use our real ccl.properties file?
42 sub _get_ccl_properties {
69 #FIXME: We should add QueryFuzzy and QueryStemming preferences to expand test permutations
71 subtest "test weighted autotruncated" => sub {
74 t::lib::Mocks::mock_preference('QueryWeightFields', '1');
75 t::lib::Mocks::mock_preference('QueryAutoTruncate', '1');
77 my $config = _get_ccl_properties();
79 my $operands = ["test"];
85 my ($error,$query,$simple_query,$query_cgi,$query_desc,$limit,$limit_cgi,$limit_desc,$query_type) =
86 C4::Search::buildQuery($operators,$operands,$indexes,$limits,$sort_by,$scan,$lang);
87 is($error,undef,"Error is correct");
88 is($query,'(rk=(kw,wrdl,rtrn=test )) ','Query is correct with auto truncation');
89 is($simple_query,'test',"Simple query is correct");
90 is($query_cgi,'idx=kw&q=test','Query cgi is correct');
91 is($query_desc,'kw,wrdl: test','Query desc is correct');
92 is($limit,'',"Limit is correct");
93 is($limit_cgi,undef,"Limit cgi is correct");
94 is($limit_desc,'',"Limit desc is correct");
95 is($query_type,undef,"Query type is correct");
96 my $q = Net::Z3950::ZOOM::query_create();
97 my ($ccl_errcode, $ccl_errstr, $ccl_errpos) = (0,"",0);
98 my $res = Net::Z3950::ZOOM::query_ccl2rpn($q, $query, $config,
99 $ccl_errcode, $ccl_errstr, $ccl_errpos
101 is($res,0,"created CCL2RPN query");
105 Net::Z3950::ZOOM::query_destroy($q);
109 subtest "test* weighted autotruncated" => sub {
112 t::lib::Mocks::mock_preference('QueryWeightFields', '1');
113 t::lib::Mocks::mock_preference('QueryAutoTruncate', '1');
115 my $config = _get_ccl_properties();
116 my $operators = [""];
117 my $operands = ["test*"];
123 my ($error,$query,$simple_query,$query_cgi,$query_desc,$limit,$limit_cgi,$limit_desc,$query_type) =
124 C4::Search::buildQuery($operators,$operands,$indexes,$limits,$sort_by,$scan,$lang);
125 is($error,undef,"Error is correct");
126 is($query,'(rk=(kw,wrdl,rtrn=test )) ','Query is correct with manual truncation');
127 is($simple_query,'test*',"Simple query is correct");
128 is($query_cgi,'idx=kw&q=test%2A','Query cgi is correct');
129 is($query_desc,'kw,wrdl: test*','Query desc is correct');
130 is($limit,'',"Limit is correct");
131 is($limit_cgi,undef,"Limit cgi is correct");
132 is($limit_desc,'',"Limit desc is correct");
133 is($query_type,undef,"Query type is correct");
134 my $q = Net::Z3950::ZOOM::query_create();
135 my ($ccl_errcode, $ccl_errstr, $ccl_errpos) = (0,"",0);
136 my $res = Net::Z3950::ZOOM::query_ccl2rpn($q, $query, $config,
137 $ccl_errcode, $ccl_errstr, $ccl_errpos
139 is($res,0,"created CCL2RPN query");
143 Net::Z3950::ZOOM::query_destroy($q);
146 subtest "test weighted not-autotruncated" => sub {
149 t::lib::Mocks::mock_preference('QueryWeightFields', '1');
150 t::lib::Mocks::mock_preference('QueryAutoTruncate', '0');
152 my $config = _get_ccl_properties();
153 my $operators = [""];
154 my $operands = ["test"];
160 my ($error,$query,$simple_query,$query_cgi,$query_desc,$limit,$limit_cgi,$limit_desc,$query_type) =
161 C4::Search::buildQuery($operators,$operands,$indexes,$limits,$sort_by,$scan,$lang);
162 is($error,undef,"Error is correct");
163 is($query,'(rk=(Title-cover,ext,r1="test" or ti,ext,r2="test" or Title-cover,phr,r3="test" or ti,wrdl,r4="test" or wrdl,fuzzy,r8="test" or wrdl,right-Truncation,r9="test" or wrdl,r9="test")) ','Query is correct with weighted fields');
164 is($simple_query,'test',"Simple query is correct");
165 is($query_cgi,'idx=kw&q=test','Query cgi is correct');
166 is($query_desc,'kw,wrdl: test','Query desc is correct');
167 is($limit,'',"Limit is correct");
168 is($limit_cgi,undef,"Limit cgi is correct");
169 is($limit_desc,'',"Limit desc is correct");
170 is($query_type,undef,"Query type is correct");
171 my $q = Net::Z3950::ZOOM::query_create();
172 my ($ccl_errcode, $ccl_errstr, $ccl_errpos) = (0,"",0);
173 my $res = Net::Z3950::ZOOM::query_ccl2rpn($q, $query, $config,
174 $ccl_errcode, $ccl_errstr, $ccl_errpos
176 is($res,0,"created CCL2RPN query");
180 Net::Z3950::ZOOM::query_destroy($q);
184 subtest "test ccode:REF weighted autotruncated" => sub {
187 t::lib::Mocks::mock_preference('QueryWeightFields', '1');
188 t::lib::Mocks::mock_preference('QueryAutoTruncate', '1');
190 my $config = _get_ccl_properties();
191 my $operators = [""];
192 my $operands = ["test"];
194 my $limits = ["ccode:REF"];
198 my ($error,$query,$simple_query,$query_cgi,$query_desc,$limit,$limit_cgi,$limit_desc,$query_type) =
199 C4::Search::buildQuery($operators,$operands,$indexes,$limits,$sort_by,$scan,$lang);
200 is($error,undef,"Error is correct");
201 is($query,'(rk=(kw,wrdl,rtrn=test )) and ccode=REF','Query is correct with auto truncation and limits');
202 is($simple_query,'test',"Simple query is correct");
203 is($query_cgi,'idx=kw&q=test','Query cgi is correct');
204 is($query_desc,'kw,wrdl: test','Query desc is correct');
205 is($limit,'and ccode=REF',"Limit is correct");
206 is($limit_cgi,'&limit=ccode%3AREF',"Limit cgi is correct");
207 is($limit_desc,'ccode:REF',"Limit desc is correct");
208 is($query_type,undef,"Query type is correct");
209 my $q = Net::Z3950::ZOOM::query_create();
210 my ($ccl_errcode, $ccl_errstr, $ccl_errpos) = (0,"",0);
211 my $res = Net::Z3950::ZOOM::query_ccl2rpn($q, $query, $config,
212 $ccl_errcode, $ccl_errstr, $ccl_errpos
214 is($res,0,"created CCL2RPN query");
218 Net::Z3950::ZOOM::query_destroy($q);
221 subtest "test ccode:REF weighted not-autotruncated" => sub {
224 t::lib::Mocks::mock_preference('QueryWeightFields', '1');
225 t::lib::Mocks::mock_preference('QueryAutoTruncate', '0');
227 my $config = _get_ccl_properties();
228 my $operators = [""];
229 my $operands = ["test"];
231 my $limits = ["ccode:REF"];
235 my ($error,$query,$simple_query,$query_cgi,$query_desc,$limit,$limit_cgi,$limit_desc,$query_type) =
236 C4::Search::buildQuery($operators,$operands,$indexes,$limits,$sort_by,$scan,$lang);
237 is($error,undef,"Error is correct");
238 is($query,'(rk=(Title-cover,ext,r1="test" or ti,ext,r2="test" or Title-cover,phr,r3="test" or ti,wrdl,r4="test" or wrdl,fuzzy,r8="test" or wrdl,right-Truncation,r9="test" or wrdl,r9="test")) and ccode=REF','Query is correct with weighted fields and limits');
239 is($simple_query,'test',"Simple query is correct");
240 is($query_cgi,'idx=kw&q=test','Query cgi is correct');
241 is($query_desc,'kw,wrdl: test','Query desc is correct');
242 is($limit,'and ccode=REF',"Limit is correct");
243 is($limit_cgi,'&limit=ccode%3AREF',"Limit cgi is correct");
244 is($limit_desc,'ccode:REF',"Limit desc is correct");
245 is($query_type,undef,"Query type is correct");
246 my $q = Net::Z3950::ZOOM::query_create();
247 my ($ccl_errcode, $ccl_errstr, $ccl_errpos) = (0,"",0);
248 my $res = Net::Z3950::ZOOM::query_ccl2rpn($q, $query, $config,
249 $ccl_errcode, $ccl_errstr, $ccl_errpos
251 is($res,0,"created CCL2RPN query");
255 Net::Z3950::ZOOM::query_destroy($q);
258 subtest "kw:one and title:two ccode:REF weighted autotruncated" => sub {
261 t::lib::Mocks::mock_preference('QueryWeightFields', '1');
262 t::lib::Mocks::mock_preference('QueryAutoTruncate', '1');
264 my $config = _get_ccl_properties();
265 my $operators = ["and"];
266 my $operands = ["one","two"];
267 my $indexes = ["kw","title"];
268 my $limits = ["ccode:REF"];
272 my ($error,$query,$simple_query,$query_cgi,$query_desc,$limit,$limit_cgi,$limit_desc,$query_type) =
273 C4::Search::buildQuery($operators,$operands,$indexes,$limits,$sort_by,$scan,$lang);
274 is($error,undef,"Error is correct");
275 is($query,'(rk=(kw,wrdl,rtrn=one )) and (rk=(title,wrdl,rtrn=two )) and ccode=REF','Query is correct with auto truncation, limits, and using indexes and operators');
276 is($simple_query,'one',"Simple query is correct?");
277 is($query_cgi,'idx=kw&q=one&op=%20and%20&idx=title&q=two','Query cgi is correct');
278 is($query_desc,'kw,wrdl: one and title,wrdl: two','Query desc is correct');
279 is($limit,'and ccode=REF',"Limit is correct");
280 is($limit_cgi,'&limit=ccode%3AREF',"Limit cgi is correct");
281 is($limit_desc,'ccode:REF',"Limit desc is correct");
282 is($query_type,undef,"Query type is correct");
283 my $q = Net::Z3950::ZOOM::query_create();
284 my ($ccl_errcode, $ccl_errstr, $ccl_errpos) = (0,"",0);
285 my $res = Net::Z3950::ZOOM::query_ccl2rpn($q, $query, $config,
286 $ccl_errcode, $ccl_errstr, $ccl_errpos
288 is($res,0,"created CCL2RPN query");
292 Net::Z3950::ZOOM::query_destroy($q);
295 subtest "one and two weighted autotruncated" => sub {
298 t::lib::Mocks::mock_preference('QueryWeightFields', '1');
299 t::lib::Mocks::mock_preference('QueryAutoTruncate', '1');
301 my $config = _get_ccl_properties();
302 my $operators = [""];
303 my $operands = ["one and two"];
309 my ($error,$query,$simple_query,$query_cgi,$query_desc,$limit,$limit_cgi,$limit_desc,$query_type) =
310 C4::Search::buildQuery($operators,$operands,$indexes,$limits,$sort_by,$scan,$lang);
311 is($error,undef,"Error is correct");
312 is($query,'(rk=(kw,wrdl,rtrn=one and two )) ','Query is correct with auto truncation and unstructured query');
313 is($simple_query,'one and two',"Simple query is correct");
314 is($query_cgi,'idx=kw&q=one%20and%20two','Query cgi is correct');
315 is($query_desc,'kw,wrdl: one and two','Query desc is correct');
316 is($limit,'',"Limit is correct");
317 is($limit_cgi,undef,"Limit cgi is correct");
318 is($limit_desc,'',"Limit desc is correct");
319 is($query_type,undef,"Query type is correct");
320 my $q = Net::Z3950::ZOOM::query_create();
321 my ($ccl_errcode, $ccl_errstr, $ccl_errpos) = (0,"",0);
322 my $res = Net::Z3950::ZOOM::query_ccl2rpn($q, $query, $config,
323 $ccl_errcode, $ccl_errstr, $ccl_errpos
325 is($res,0,"created CCL2RPN query");
329 Net::Z3950::ZOOM::query_destroy($q);