Bug 21720: Use Koha::Account->add_debit in AddIssuingCharge
[koha.git] / t / db_dependent / Search.t
blobb5e503106c0522e9c4eee60b03f36485595600c1
1 #!/usr/bin/perl
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>.
18 use Modern::Perl;
20 use utf8;
22 use YAML;
24 use C4::Debug;
25 use C4::XSLT;
26 require C4::Context;
28 # work around spurious wide character warnings
29 use open ':std', ':encoding(utf8)';
31 use Test::More tests => 2;
32 use Test::MockModule;
33 use Test::Warn;
35 use Koha::Caches;
37 use MARC::Record;
38 use File::Spec;
39 use File::Basename;
40 use File::Find;
42 use File::Temp qw/ tempdir /;
43 use File::Path;
45 our $child;
46 our $datadir;
48 sub index_sample_records_and_launch_zebra {
49 my ($datadir, $marc_type) = @_;
51 my $sourcedir = dirname(__FILE__) . "/data";
52 unlink("$datadir/zebra.log");
53 if (-f "$sourcedir/${marc_type}/zebraexport/biblio/exported_records") {
54 my $zebra_bib_cfg = 'zebra-biblios-dom.cfg';
55 system("zebraidx -c $datadir/etc/koha/zebradb/$zebra_bib_cfg -v none,fatal -g iso2709 -d biblios init");
56 system("zebraidx -c $datadir/etc/koha/zebradb/$zebra_bib_cfg -v none,fatal -g iso2709 -d biblios update $sourcedir/${marc_type}/zebraexport/biblio");
57 system("zebraidx -c $datadir/etc/koha/zebradb/$zebra_bib_cfg -v none,fatal -g iso2709 -d biblios commit");
59 # ... and add large bib records, if present
60 if (-f "$sourcedir/${marc_type}/zebraexport/large_biblio/exported_records.xml") {
61 my $zebra_bib_cfg = 'zebra-biblios-dom.cfg';
62 system("zebraidx -c $datadir/etc/koha/zebradb/$zebra_bib_cfg -v none,fatal -g marcxml -d biblios update $sourcedir/${marc_type}/zebraexport/large_biblio");
63 system("zebraidx -c $datadir/etc/koha/zebradb/$zebra_bib_cfg -v none,fatal -g marcxml -d biblios commit");
65 if (-f "$sourcedir/${marc_type}/zebraexport/authority/exported_records") {
66 my $zebra_auth_cfg = 'zebra-authorities-dom.cfg';
67 system("zebraidx -c $datadir/etc/koha/zebradb/$zebra_auth_cfg -v none,fatal -g iso2709 -d authorities init");
68 system("zebraidx -c $datadir/etc/koha/zebradb/$zebra_auth_cfg -v none,fatal -g iso2709 -d authorities update $sourcedir/${marc_type}/zebraexport/authority");
69 system("zebraidx -c $datadir/etc/koha/zebradb/$zebra_auth_cfg -v none,fatal -g iso2709 -d authorities commit");
72 $child = fork();
73 if ($child == 0) {
74 exec("zebrasrv -f $datadir/etc/koha-conf.xml -v none,request -l $datadir/zebra.log");
75 exit;
78 sleep(1);
81 sub cleanup {
82 if ($child) {
83 kill 9, $child;
85 # Clean up the Zebra files since the child process was just shot
86 rmtree $datadir;
90 # Fall back to make sure that the Zebra process
91 # and files get cleaned up
92 END {
93 cleanup();
96 our $QueryStemming = 0;
97 our $QueryAutoTruncate = 0;
98 our $QueryWeightFields = 0;
99 our $QueryFuzzy = 0;
100 our $UseQueryParser = 0;
101 our $SearchEngine = 'Zebra';
102 our $marcflavour = 'MARC21';
103 our $htdocs = File::Spec->rel2abs(dirname($0));
104 my @htdocs = split /\//, $htdocs;
105 $htdocs[-2] = 'koha-tmpl';
106 $htdocs[-1] = 'opac-tmpl';
107 $htdocs = join '/', @htdocs;
108 our $contextmodule = new Test::MockModule('C4::Context');
109 $contextmodule->mock('preference', sub {
110 my ($self, $pref) = @_;
111 if ($pref eq 'marcflavour') {
112 return $marcflavour;
113 } elsif ($pref eq 'QueryStemming') {
114 return $QueryStemming;
115 } elsif ($pref eq 'QueryAutoTruncate') {
116 return $QueryAutoTruncate;
117 } elsif ($pref eq 'QueryWeightFields') {
118 return $QueryWeightFields;
119 } elsif ($pref eq 'QueryFuzzy') {
120 return $QueryFuzzy;
121 } elsif ($pref eq 'UseQueryParser') {
122 return $UseQueryParser;
123 } elsif ($pref eq 'SearchEngine') {
124 return $SearchEngine;
125 } elsif ($pref eq 'maxRecordsForFacets') {
126 return 20;
127 } elsif ($pref eq 'FacetLabelTruncationLength') {
128 return 20;
129 } elsif ($pref eq 'FacetMaxCount') {
130 return 20;
131 } elsif ($pref eq 'OpacHiddenItems') {
132 return '';
133 } elsif ($pref eq 'opacthemes') {
134 return 'bootstrap';
135 } elsif ($pref eq 'opaclanguages') {
136 return 'en';
137 } elsif ($pref eq 'AlternateHoldingsField') {
138 return '490av';
139 } elsif ($pref eq 'AuthoritySeparator') {
140 return '--';
141 } elsif ($pref eq 'DisplayLibraryFacets') {
142 return 'holding';
143 } elsif ($pref eq 'UNIMARCAuthorsFacetsSeparator') {
144 return '--';
145 } elsif ($pref eq 'casAuthentication' or $pref eq 'casLogout' or $pref eq 'casServerUrl' ) {
146 return '';
147 } elsif ($pref eq 'template') {
148 return 'prog';
149 } elsif ($pref eq 'OPACXSLTResultsDisplay') {
150 return C4::XSLT::_get_best_default_xslt_filename($htdocs, 'bootstrap','en',$marcflavour . 'slim2OPACResults.xsl');
151 } elsif ($pref eq 'BiblioDefaultView') {
152 return 'normal';
153 } elsif ($pref eq 'IdRef') {
154 return '0';
155 } elsif ($pref eq 'IntranetBiblioDefaultView') {
156 return 'normal';
157 } elsif ($pref eq 'OPACBaseURL') {
158 return 'http://library.mydnsname.org';
159 } elsif ($pref eq 'OPACResultsLibrary') {
160 return 'homebranch';
161 } elsif ($pref eq 'OpacSuppression') {
162 return '0';
163 } elsif ($pref eq 'OPACURLOpenInNewWindow') {
164 return '0';
165 } elsif ($pref eq 'TraceCompleteSubfields') {
166 return '0';
167 } elsif ($pref eq 'TraceSubjectSubdivisions') {
168 return '0';
169 } elsif ($pref eq 'TrackClicks') {
170 return '0';
171 } elsif ($pref eq 'URLLinkText') {
172 return q{};
173 } elsif ($pref eq 'UseAuthoritiesForTracings') {
174 return '1';
175 } elsif ($pref eq 'UseControlNumber') {
176 return '0';
177 } elsif ($pref eq 'UseICU') {
178 return '0';
179 } elsif ($pref eq 'viewISBD') {
180 return '1';
181 } else {
182 warn "The syspref $pref was requested but I don't know what to say; this indicates that the test requires updating"
183 unless $pref =~ m/(XSLT|item|branch|holding|image)/i;
184 return 0;
187 $contextmodule->mock('queryparser', sub {
188 my $QParser = Koha::QueryParser::Driver::PQF->new();
189 $QParser->load_config("$datadir/etc/searchengine/queryparser.yaml");
190 return $QParser;
193 our $bibliomodule = new Test::MockModule('C4::Biblio');
195 sub mock_GetMarcSubfieldStructure {
196 my $marc_type = shift;
197 if ($marc_type eq 'marc21') {
198 $bibliomodule->mock('GetMarcSubfieldStructure', sub {
199 return {
200 'biblio.biblionumber' => [{ tagfield => '999', tagsubfield => 'c' }],
201 'biblio.isbn' => [{ tagfield => '020', tagsubfield => 'a' }],
202 'biblio.title' => [{ tagfield => '245', tagsubfield => 'a' }],
203 'biblio.notes' => [{ tagfield => '500', tagsubfield => 'a' }],
204 'items.barcode' => [{ tagfield => '952', tagsubfield => 'p' }],
205 'items.booksellerid' => [{ tagfield => '952', tagsubfield => 'e' }],
206 'items.ccode' => [{ tagfield => '952', tagsubfield => '8' }],
207 'items.cn_sort' => [{ tagfield => '952', tagsubfield => '6' }],
208 'items.cn_source' => [{ tagfield => '952', tagsubfield => '2' }],
209 'items.coded_location_qualifier' => [{ tagfield => '952', tagsubfield => 'f' }],
210 'items.copynumber' => [{ tagfield => '952', tagsubfield => 't' }],
211 'items.damaged' => [{ tagfield => '952', tagsubfield => '4' }],
212 'items.dateaccessioned' => [{ tagfield => '952', tagsubfield => 'd' }],
213 'items.datelastborrowed' => [{ tagfield => '952', tagsubfield => 's' }],
214 'items.datelastseen' => [{ tagfield => '952', tagsubfield => 'r' }],
215 'items.enumchron' => [{ tagfield => '952', tagsubfield => 'h' }],
216 'items.holdingbranch' => [{ tagfield => '952', tagsubfield => 'b' }],
217 'items.homebranch' => [{ tagfield => '952', tagsubfield => 'a' }],
218 'items.issues' => [{ tagfield => '952', tagsubfield => 'l' }],
219 'items.itemcallnumber' => [{ tagfield => '952', tagsubfield => 'o' }],
220 'items.itemlost' => [{ tagfield => '952', tagsubfield => '1' }],
221 'items.itemnotes' => [{ tagfield => '952', tagsubfield => 'z' }],
222 'items.itemnumber' => [{ tagfield => '952', tagsubfield => '9' }],
223 'items.itype' => [{ tagfield => '952', tagsubfield => 'y' }],
224 'items.location' => [{ tagfield => '952', tagsubfield => 'c' }],
225 'items.materials' => [{ tagfield => '952', tagsubfield => '3' }],
226 'items.nonpublicnote' => [{ tagfield => '952', tagsubfield => 'x' }],
227 'items.notforloan' => [{ tagfield => '952', tagsubfield => '7' }],
228 'items.onloan' => [{ tagfield => '952', tagsubfield => 'q' }],
229 'items.price' => [{ tagfield => '952', tagsubfield => 'g' }],
230 'items.renewals' => [{ tagfield => '952', tagsubfield => 'm' }],
231 'items.replacementprice' => [{ tagfield => '952', tagsubfield => 'v' }],
232 'items.replacementpricedate' => [{ tagfield => '952', tagsubfield => 'w' }],
233 'items.reserves' => [{ tagfield => '952', tagsubfield => 'n' }],
234 'items.restricted' => [{ tagfield => '952', tagsubfield => '5' }],
235 'items.stack' => [{ tagfield => '952', tagsubfield => 'j' }],
236 'items.uri' => [{ tagfield => '952', tagsubfield => 'u' }],
237 'items.withdrawn' => [{ tagfield => '952', tagsubfield => '0' }],
243 sub run_marc21_search_tests {
244 $datadir = tempdir();
245 system(dirname(__FILE__) . "/zebra_config.pl $datadir marc21");
247 Koha::Caches->get_instance('config')->flush_all;
249 mock_GetMarcSubfieldStructure('marc21');
250 my $context = new C4::Context("$datadir/etc/koha-conf.xml");
251 $context->set_context();
253 use_ok('C4::Search');
255 # set search syspreferences to a known starting point
256 $QueryStemming = 0;
257 $QueryAutoTruncate = 0;
258 $QueryWeightFields = 0;
259 $QueryFuzzy = 0;
260 $UseQueryParser = 0;
261 $marcflavour = 'MARC21';
263 my $indexes = C4::Search::getIndexes();
264 is(scalar(grep(/^ti$/, @$indexes)), 1, "Title index supported");
265 is(scalar(grep(/^arl$/, @$indexes)), 1, "Accelerated reading level index supported");
266 is(scalar(grep(/^arp$/, @$indexes)), 1, "Accelerated reading point index supported");
268 my $bibliomodule = new Test::MockModule('C4::Biblio');
270 my %branches = (
271 'CPL' => { 'branchaddress1' => 'Jefferson Summit', 'branchcode' => 'CPL', 'branchname' => 'Centerville', },
272 'FFL' => { 'branchaddress1' => 'River Station', 'branchcode' => 'FFL', 'branchname' => 'Fairfield', },
273 'FPL' => { 'branchaddress1' => 'Hickory Squere', 'branchcode' => 'FPL', 'branchname' => 'Fairview', },
274 'FRL' => { 'branchaddress1' => 'Smith Heights', 'branchcode' => 'FRL', 'branchname' => 'Franklin', },
275 'IPT' => { 'branchaddress1' => '', 'branchcode' => 'IPT', 'branchname' => "Institut Protestant de Théologie", },
276 'LPL' => { 'branchaddress1' => 'East Hills', 'branchcode' => 'LPL', 'branchname' => 'Liberty', },
277 'MPL' => { 'branchaddress1' => '372 Forest Street', 'branchcode' => 'MPL', 'branchname' => 'Midway', },
278 'PVL' => { 'branchaddress1' => 'Meadow Grove', 'branchcode' => 'PVL', 'branchname' => 'Pleasant Valley', },
279 'RPL' => { 'branchaddress1' => 'Johnson Terrace', 'branchcode' => 'RPL', 'branchname' => 'Riverside', },
280 'SPL' => { 'branchaddress1' => 'Highland Boulevard', 'branchcode' => 'SPL', 'branchname' => 'Springfield', },
281 'S' => { 'branchaddress1' => '', 'branchcode' => 'S', 'branchname' => 'Test', },
282 'TPL' => { 'branchaddress1' => 'Valley Way', 'branchcode' => 'TPL', 'branchname' => 'Troy', },
283 'UPL' => { 'branchaddress1' => 'Chestnut Hollow', 'branchcode' => 'UPL', 'branchname' => 'Union', },
285 my %itemtypes = (
286 'BK' => { 'imageurl' => 'bridge/book.gif', 'summary' => '', 'itemtype' => 'BK', 'description' => 'Books' },
287 'CF' => { 'imageurl' => 'bridge/computer_file.gif', 'summary' => '', 'itemtype' => 'CF', 'description' => 'Computer Files' },
288 'CR' => { 'imageurl' => 'bridge/periodical.gif', 'summary' => '', 'itemtype' => 'CR', 'description' => 'Continuing Resources' },
289 'MP' => { 'imageurl' => 'bridge/map.gif', 'summary' => '', 'itemtype' => 'MP', 'description' => 'Maps' },
290 'MU' => { 'imageurl' => 'bridge/sound.gif', 'summary' => '', 'itemtype' => 'MU', 'description' => 'Music' },
291 'MX' => { 'imageurl' => 'bridge/kit.gif', 'summary' => '', 'itemtype' => 'MX', 'description' => 'Mixed Materials' },
292 'REF' => { 'imageurl' => '', 'summary' => '', 'itemtype' => 'REF', 'description' => 'Reference' },
293 'VM' => { 'imageurl' => 'bridge/dvd.gif', 'summary' => '', 'itemtype' => 'VM', 'description' => 'Visual Materials' },
296 index_sample_records_and_launch_zebra($datadir, 'marc21');
298 my ($biblionumber, $title);
299 my $record = MARC::Record->new;
301 $record->add_fields(
302 [ '020', ' ', ' ', a => '9788522421718' ],
303 [ '245', '0', '0', a => 'Administração da produção /' ]
305 ($biblionumber,undef,$title) = FindDuplicate($record);
306 is($biblionumber, 51, 'Found duplicate with ISBN');
308 $record = MARC::Record->new;
310 $record->add_fields(
311 [ '100', '1', ' ', a => 'Carter, Philip J.' ],
312 [ '245', '1', '4', a => 'Test your emotional intelligence :' ]
314 ($biblionumber,undef,$title) = FindDuplicate($record);
315 is($biblionumber, 203, 'Found duplicate with author/title');
317 # Testing SimpleSearch
319 my ( $error, $marcresults, $total_hits ) = SimpleSearch("book", 0, 9);
321 is(scalar @$marcresults, 9, "SimpleSearch retrieved requested number of records");
322 is($total_hits, 101, "SimpleSearch for 'book' matched right number of records");
323 is($error, undef, "SimpleSearch does not return an error when successful");
325 my $marcresults2;
326 ( $error, $marcresults2, $total_hits ) = SimpleSearch("book", 5, 5);
327 is($marcresults->[5], $marcresults2->[0], "SimpleSearch cursor functions");
329 ( $error, $marcresults, $total_hits ) = SimpleSearch("kw=book", 0, 10);
330 is($total_hits, 101, "SimpleSearch handles simple CCL");
332 ( $error, $marcresults, $total_hits ) = SimpleSearch("Music-number=49631-2", 0, 10);
333 is($total_hits, 1, "SimpleSearch on music publisher number works (bug 8252)");
334 ( $error, $marcresults, $total_hits ) = SimpleSearch("Identifier-publisher-for-music=49631-2", 0, 10);
335 is($total_hits, 1, "SimpleSearch on music publisher number works using Identifier-publisher-for-music (bug 8252)");
337 # Testing getRecords
339 my $results_hashref;
340 my $facets_loop;
341 ( undef, $results_hashref, $facets_loop ) =
342 getRecords('kw:book', 'book', [], [ 'biblioserver' ], '19', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
343 is($results_hashref->{biblioserver}->{hits}, 101, "getRecords keyword search for 'book' matched right number of records");
344 is(scalar @{$results_hashref->{biblioserver}->{RECORDS}}, 19, "getRecords returned requested number of records");
345 my $record5 = $results_hashref->{biblioserver}->{RECORDS}->[5];
346 ( undef, $results_hashref, $facets_loop ) =
347 getRecords('kw:book', 'book', [], [ 'biblioserver' ], '20', 5, undef, \%branches, \%itemtypes, 'ccl', undef);
348 ok(!defined $results_hashref->{biblioserver}->{RECORDS}->[0] &&
349 !defined $results_hashref->{biblioserver}->{RECORDS}->[1] &&
350 !defined $results_hashref->{biblioserver}->{RECORDS}->[2] &&
351 !defined $results_hashref->{biblioserver}->{RECORDS}->[3] &&
352 !defined $results_hashref->{biblioserver}->{RECORDS}->[4] &&
353 $results_hashref->{biblioserver}->{RECORDS}->[5] eq $record5, "getRecords cursor works");
355 ( undef, $results_hashref, $facets_loop ) =
356 getRecords('ti:book', 'ti:book', [], [ 'biblioserver' ], '20', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
357 is($results_hashref->{biblioserver}->{hits}, 11, "getRecords title search for 'book' matched right number of records");
359 ( undef, $results_hashref, $facets_loop ) =
360 getRecords('au:Lessig', 'au:Lessig', [], [ 'biblioserver' ], '20', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
361 is($results_hashref->{biblioserver}->{hits}, 4, "getRecords title search for 'Australia' matched right number of records");
363 ( undef, $results_hashref, $facets_loop ) =
364 getRecords('salud', 'salud', [], [ 'biblioserver' ], '19', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
365 ok(MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[0],'UTF-8')->title_proper() =~ m/^Efectos del ambiente/ &&
366 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[7],'UTF-8')->title_proper() eq 'Salud y seguridad de los trabajadores del sector salud: manual para gerentes y administradores^ies' &&
367 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[18],'UTF-8')->title_proper() =~ m/^Indicadores de resultados identificados/
368 , "Simple relevance sorting in getRecords matches old behavior");
370 ( undef, $results_hashref, $facets_loop ) =
371 getRecords('salud', 'salud', [ 'author_az' ], [ 'biblioserver' ], '38', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
372 ok(MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[0],'UTF-8')->title_proper() =~ m/la enfermedad laboral\^ies$/ &&
373 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[6],'UTF-8')->title_proper() =~ m/^Indicadores de resultados identificados/ &&
374 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[18],'UTF-8')->title_proper() eq 'World health statistics 2009^ien'
375 , "Simple ascending author sorting in getRecords matches old behavior");
377 ( undef, $results_hashref, $facets_loop ) =
378 getRecords('salud', 'salud', [ 'author_za' ], [ 'biblioserver' ], '38', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
379 ok(MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[0],'UTF-8')->title_proper() eq 'World health statistics 2009^ien' &&
380 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[12],'UTF-8')->title_proper() =~ m/^Indicadores de resultados identificados/ &&
381 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[18],'UTF-8')->title_proper() =~ m/la enfermedad laboral\^ies$/
382 , "Simple descending author sorting in getRecords matches old behavior");
384 ( undef, $results_hashref, $facets_loop ) =
385 getRecords('salud', 'salud', [ 'pubdate_asc' ], [ 'biblioserver' ], '38', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
386 ok(MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[0],'UTF-8')->title_proper() eq 'Manual de higiene industrial^ies' &&
387 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[7],'UTF-8')->title_proper() =~ m/seguridad e higiene del trabajo\^ies$/ &&
388 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[18],'UTF-8')->title_proper() =~ m/^Indicadores de resultados identificados/
389 , "Simple ascending publication date sorting in getRecords matches old behavior");
391 ( undef, $results_hashref, $facets_loop ) =
392 getRecords('salud', 'salud', [ 'pubdate_dsc' ], [ 'biblioserver' ], '38', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
393 ok(MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[0],'UTF-8')->title_proper() =~ m/^Estado de salud/ &&
394 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[7],'UTF-8')->title_proper() eq 'World health statistics 2009^ien' &&
395 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[18],'UTF-8')->title_proper() eq 'Manual de higiene industrial^ies'
396 , "Simple descending publication date sorting in getRecords matches old behavior");
398 ( undef, $results_hashref, $facets_loop ) =
399 getRecords('books', 'books', [ 'relevance' ], [ 'biblioserver' ], '20', 0, undef, \%branches, \%itemtypes, undef, 1);
400 $record = MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[0]);
401 is($record->title_proper(), 'Books', "Scan returned requested item");
402 is($record->subfield('100', 'a'), 2, "Scan returned correct number of records matching term");
403 # Time to test buildQuery and searchResults too.
405 my ( $query, $simple_query, $query_cgi,
406 $query_desc, $limit, $limit_cgi, $limit_desc,
407 $query_type );
408 ( $error, $query, $simple_query, $query_cgi,
409 $query_desc, $limit, $limit_cgi, $limit_desc,
410 $query_type ) = buildQuery([], [ 'salud' ], [], [], [], 0, 'en');
411 like($query, qr/kw\W.*salud/, "Built CCL keyword query");
413 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
414 is($results_hashref->{biblioserver}->{hits}, 19, "getRecords generated keyword search for 'salud' matched right number of records");
416 my @newresults = searchResults({'interface' => 'opac'}, $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 18, 0, 0,
417 $results_hashref->{'biblioserver'}->{"RECORDS"});
418 is(scalar @newresults,18, "searchResults returns requested number of hits");
420 ( $error, $query, $simple_query, $query_cgi,
421 $query_desc, $limit, $limit_cgi, $limit_desc,
422 $query_type ) = buildQuery([ 'and' ], [ 'salud', 'higiene' ], [], [], [], 0, 'en');
423 like($query, qr/kw\W.*salud\W.*and.*kw\W.*higiene/, "Built composed explicit-and CCL keyword query");
425 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
426 is($results_hashref->{biblioserver}->{hits}, 3, "getRecords generated composed keyword search for 'salud' explicit-and 'higiene' matched right number of records");
428 ( $error, $query, $simple_query, $query_cgi,
429 $query_desc, $limit, $limit_cgi, $limit_desc,
430 $query_type ) = buildQuery([ 'or' ], [ 'salud', 'higiene' ], [], [], [], 0, 'en');
431 like($query, qr/kw\W.*salud\W.*or.*kw\W.*higiene/, "Built composed explicit-or CCL keyword query");
433 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
434 is($results_hashref->{biblioserver}->{hits}, 20, "getRecords generated composed keyword search for 'salud' explicit-or 'higiene' matched right number of records");
436 ( $error, $query, $simple_query, $query_cgi,
437 $query_desc, $limit, $limit_cgi, $limit_desc,
438 $query_type ) = buildQuery([], [ 'salud', 'higiene' ], [], [], [], 0, 'en');
439 like($query, qr/kw\W.*salud\W.*and.*kw\W.*higiene/, "Built composed implicit-and CCL keyword query");
441 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
442 is($results_hashref->{biblioserver}->{hits}, 3, "getRecords generated composed keyword search for 'salud' implicit-and 'higiene' matched right number of records");
444 ( $error, $query, $simple_query, $query_cgi,
445 $query_desc, $limit, $limit_cgi, $limit_desc,
446 $query_type ) = buildQuery([], [ 'salud' ], [ 'kw' ], [ 'su-to:Laboratorios' ], [], 0, 'en');
447 like($query, qr/kw\W.*salud\W*and\W*su-to\W.*Laboratorios/, "Faceted query generated correctly");
448 unlike($query_desc, qr/Laboratorios/, "Facets not included in query description");
450 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
451 is($results_hashref->{biblioserver}->{hits}, 2, "getRecords generated faceted search matched right number of records");
454 ( $error, $query, $simple_query, $query_cgi,
455 $query_desc, $limit, $limit_cgi, $limit_desc,
456 $query_type ) = buildQuery([], [ '' ], [ 'kw' ], [ 'mc-itype:MP', 'mc-itype:MU' ], [], 0, 'en');
458 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
459 is($results_hashref->{biblioserver}->{hits}, 2, "getRecords generated mc-faceted search matched right number of records");
462 ( $error, $query, $simple_query, $query_cgi,
463 $query_desc, $limit, $limit_cgi, $limit_desc,
464 $query_type ) = buildQuery([], [ '' ], [ 'kw' ], [ 'mc-loc:GEN', 'branch:FFL' ], [], 0, 'en');
466 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
467 is($results_hashref->{biblioserver}->{hits}, 2, "getRecords generated multi-faceted search matched right number of records");
469 ( $error, $query, $simple_query, $query_cgi,
470 $query_desc, $limit, $limit_cgi, $limit_desc,
471 $query_type ) = buildQuery([], [ 'NEKLS' ], [ 'Code-institution' ], [], [], 0, 'en');
472 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
473 is($results_hashref->{biblioserver}->{hits}, 12,
474 'search using index whose name contains "ns" returns expected results (bug 10271)');
476 $UseQueryParser = 1;
477 ( $error, $query, $simple_query, $query_cgi,
478 $query_desc, $limit, $limit_cgi, $limit_desc,
479 $query_type ) = buildQuery([], [ 'book' ], [ 'kw' ], [], [], 0, 'en');
480 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
481 is($results_hashref->{biblioserver}->{hits}, 101, "Search for 'book' with index set to 'kw' returns 101 hits");
482 ( $error, $query, $simple_query, $query_cgi,
483 $query_desc, $limit, $limit_cgi, $limit_desc,
484 $query_type ) = buildQuery([ 'and' ], [ 'book', 'another' ], [ 'kw', 'kw' ], [], [], 0, 'en');
485 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
486 is($results_hashref->{biblioserver}->{hits}, 1, "Search for 'kw:book && kw:another' returns 1 hit");
487 $UseQueryParser = 0;
489 # FIXME: the availability limit does not actually work, so for the moment we
490 # are just checking that it behaves consistently
491 ( $error, $query, $simple_query, $query_cgi,
492 $query_desc, $limit, $limit_cgi, $limit_desc,
493 $query_type ) = buildQuery([], [ '' ], [ 'kw' ], [ 'available' ], [], 0, 'en');
495 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
496 is($results_hashref->{biblioserver}->{hits}, 26, "getRecords generated availability-limited search matched right number of records");
498 @newresults = searchResults({'interface'=>'opac'}, $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 17, 0, 0,
499 $results_hashref->{'biblioserver'}->{"RECORDS"});
500 my $allavailable = 'true';
501 foreach my $result (@newresults) {
502 $allavailable = 'false' unless $result->{availablecount} > 0;
504 is ($allavailable, 'true', 'All records have at least one item available');
507 ( $error, $query, $simple_query, $query_cgi,
508 $query_desc, $limit, $limit_cgi, $limit_desc,
509 $query_type ) = buildQuery([], [ 'pqf=@attr 1=_ALLRECORDS @attr 2=103 ""' ], [], [], [], 0, 'en');
511 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
512 is($results_hashref->{biblioserver}->{hits}, 180, "getRecords on _ALLRECORDS PQF returned all records");
514 ( $error, $query, $simple_query, $query_cgi,
515 $query_desc, $limit, $limit_cgi, $limit_desc,
516 $query_type ) = buildQuery([], [ 'pqf=@attr 1=1016 "Lessig"' ], [], [], [], 0, 'en');
518 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
519 is($results_hashref->{biblioserver}->{hits}, 4, "getRecords PQF author search for Lessig returned proper number of matches");
521 ( $error, $query, $simple_query, $query_cgi,
522 $query_desc, $limit, $limit_cgi, $limit_desc,
523 $query_type ) = buildQuery([], [ 'ccl=au:Lessig' ], [], [], [], 0, 'en');
525 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
526 is($results_hashref->{biblioserver}->{hits}, 4, "getRecords CCL author search for Lessig returned proper number of matches");
528 ( $error, $query, $simple_query, $query_cgi,
529 $query_desc, $limit, $limit_cgi, $limit_desc,
530 $query_type ) = buildQuery([], [ 'cql=dc.author any lessig' ], [], [], [], 0, 'en');
532 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
533 is($results_hashref->{biblioserver}->{hits}, 4, "getRecords CQL author search for Lessig returned proper number of matches");
535 $QueryStemming = $QueryAutoTruncate = $QueryFuzzy = 0;
536 $QueryWeightFields = 1;
537 ( $error, $query, $simple_query, $query_cgi,
538 $query_desc, $limit, $limit_cgi, $limit_desc,
539 $query_type ) = buildQuery([], [ 'salud' ], [ 'kw' ], [], [], 0, 'en');
541 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
542 is($results_hashref->{biblioserver}->{hits}, 19, "Weighted query returned correct number of results");
543 is(MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[0],'UTF-8')->title_proper(), 'Salud y seguridad de los trabajadores del sector salud: manual para gerentes y administradores^ies', "Weighted query returns best match first");
545 $QueryStemming = $QueryWeightFields = $QueryFuzzy = 0;
546 $QueryAutoTruncate = 1;
547 ( $error, $query, $simple_query, $query_cgi,
548 $query_desc, $limit, $limit_cgi, $limit_desc,
549 $query_type ) = buildQuery([], [ 'medic' ], [ 'kw' ], [], [], 0, 'en');
551 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
552 is($results_hashref->{biblioserver}->{hits}, 5, "Search for 'medic' returns matches with automatic truncation on");
554 ( $error, $query, $simple_query, $query_cgi,
555 $query_desc, $limit, $limit_cgi, $limit_desc,
556 $query_type ) = buildQuery([], [ 'medic*' ], [ 'kw' ], [], [], 0, 'en');
558 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
559 is($results_hashref->{biblioserver}->{hits}, 5, "Search for 'medic*' returns matches with automatic truncation on");
561 $QueryStemming = $QueryFuzzy = $QueryAutoTruncate = 0;
562 $QueryWeightFields = 1;
563 ( $error, $query, $simple_query, $query_cgi,
564 $query_desc, $limit, $limit_cgi, $limit_desc,
565 $query_type ) = buildQuery([], [ 'web application' ], [ 'kw' ], [], [], 0, 'en');
566 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
567 is($results_hashref->{biblioserver}->{hits}, 1, "Search for 'web application' returns one hit with QueryWeightFields on");
569 ( $error, $query, $simple_query, $query_cgi,
570 $query_desc, $limit, $limit_cgi, $limit_desc,
571 $query_type ) = buildQuery([], [ 'web "application' ], [ 'kw' ], [], [], 0, 'en');
572 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
573 is($results_hashref->{biblioserver}->{hits}, 1, "Search for 'web \"application' returns one hit with QueryWeightFields on (bug 7518)");
575 $QueryStemming = $QueryWeightFields = $QueryFuzzy = $QueryAutoTruncate = 0;
576 ( $error, $query, $simple_query, $query_cgi,
577 $query_desc, $limit, $limit_cgi, $limit_desc,
578 $query_type ) = buildQuery([], [ 'medic' ], [ 'kw' ], [], [], 0, 'en');
580 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
581 is($results_hashref->{biblioserver}->{hits}, undef, "Search for 'medic' returns no matches with automatic truncation off");
583 ( $error, $query, $simple_query, $query_cgi,
584 $query_desc, $limit, $limit_cgi, $limit_desc,
585 $query_type ) = buildQuery([], [ 'medic*' ], [ 'kw' ], [], [], 0, 'en');
587 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
588 is($results_hashref->{biblioserver}->{hits}, 5, "Search for 'medic*' returns matches with automatic truncation off");
590 $QueryStemming = $QueryWeightFields = 1;
591 $QueryFuzzy = $QueryAutoTruncate = 0;
592 ( $error, $query, $simple_query, $query_cgi,
593 $query_desc, $limit, $limit_cgi, $limit_desc,
594 $query_type ) = buildQuery([], [ 'pressed' ], [ 'kw' ], [], [], 0, 'en');
596 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
597 is($results_hashref->{biblioserver}->{hits}, 7, "Search for 'pressed' returns matches when stemming (and query weighting) is on");
599 $QueryStemming = $QueryWeightFields = $QueryFuzzy = $QueryAutoTruncate = 0;
600 ( $error, $query, $simple_query, $query_cgi,
601 $query_desc, $limit, $limit_cgi, $limit_desc,
602 $query_type ) = buildQuery([], [ 'pressed' ], [ 'kw' ], [], [], 0, 'en');
604 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
605 is($results_hashref->{biblioserver}->{hits}, undef, "Search for 'pressed' returns no matches when stemming is off");
607 ( $error, $query, $simple_query, $query_cgi,
608 $query_desc, $limit, $limit_cgi, $limit_desc,
609 $query_type ) = buildQuery([], [ 'ccl=an:42' ], [], ['available'], [], 0, 'en');
610 is( $query, "an:42 and ( ( allrecords,AlwaysMatches:'' not onloan,AlwaysMatches:'') and (lost,st-numeric=0) )", 'buildQuery should add the available part to the query if requested with ccl' );
611 is( $query_desc, 'an:42', 'buildQuery should remove the available part from the query' );
613 ( $error, $query, $simple_query, $query_cgi,
614 $query_desc, $limit, $limit_cgi, $limit_desc,
615 $query_type ) = buildQuery([], [ 0 ], [ 'su,phr' ], [], [], 0, 'en');
616 is($query, 'su,phr=0 ', 'buildQuery should keep 0 value');
618 # Let's see what happens when we pass bad data into these routines.
619 # We have to catch warnings since we're not very good about returning errors.
621 warning_like { ( $error, $marcresults, $total_hits ) = SimpleSearch("@==ccl blah", 0, 9) } qr/CCL parsing error/,
622 "SimpleSearch warns about CCL parsing error with nonsense query";
623 isnt($error, undef, "SimpleSearch returns an error when passed gibberish");
625 warning_like {( undef, $results_hashref, $facets_loop ) =
626 getRecords('kw:book', 'book', [], [ 'biblioserver' ], '19', 0, undef, \%branches, \%itemtypes, 'nonsense', undef) }
627 qr/Unknown query_type/, "getRecords warns about unknown query type";
629 warning_like {( undef, $results_hashref, $facets_loop ) =
630 getRecords('pqf=@attr 1=4 "title"', 'pqf=@attr 1=4 "title"', [], [ 'biblioserver' ], '19', 0, undef, \%branches, \%itemtypes, '', undef) }
631 qr/WARNING: query problem/, "getRecords warns when query type is not specified for non-CCL query";
633 # Let's just test a few other bits and bobs, just for fun
635 ($error, $results_hashref, $facets_loop) = getRecords("Godzina pąsowej róży","Godzina pąsowej róży",[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
636 @newresults = searchResults({'interface'=>'intranet'}, $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 17, 0, 0,
637 $results_hashref->{'biblioserver'}->{"RECORDS"});
638 is($newresults[0]->{'alternateholdings_count'}, 1, 'Alternate holdings filled in correctly');
641 ## Regression test for Bug 10741
643 # make one of the test items appear to be in transit
644 my $circ_module = new Test::MockModule('C4::Circulation');
645 $circ_module->mock('GetTransfers', sub {
646 my $itemnumber = shift // -1;
647 if ($itemnumber == 11) {
648 return ('2013-07-19', 'MPL', 'CPL');
649 } else {
650 return;
654 ($error, $results_hashref, $facets_loop) = getRecords("TEST12121212","TEST12121212",[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
655 @newresults = searchResults({'interface'=>'intranet'}, $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 17, 0, 0,
656 $results_hashref->{'biblioserver'}->{"RECORDS"});
657 ok(!exists($newresults[0]->{norequests}), 'presence of a transit does not block hold request action (bug 10741)');
659 ## Regression test for bug 10684
660 ( undef, $results_hashref, $facets_loop ) =
661 getRecords('ti:punctuation', 'punctuation', [], [ 'biblioserver' ], '19', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
662 is($results_hashref->{biblioserver}->{hits}, 1, "search for ti:punctuation returned expected number of records");
663 warning_like { @newresults = searchResults({'intranet' => 'intranet'}, $query_desc,
664 $results_hashref->{'biblioserver'}->{'hits'}, 20, 0, 0,
665 $results_hashref->{'biblioserver'}->{"RECORDS"}) }
666 qr/^ERROR DECODING RECORD - Tag "50%" is not a valid tag/,
667 "Warning is raised correctly for invalid tags in MARC::Record";
668 is(scalar(@newresults), 0, 'a record that cannot be parsed by MARC::Record is simply skipped (bug 10684)');
670 # Testing exploding indexes
671 my $term;
672 my $searchmodule = new Test::MockModule('C4::Search');
673 $searchmodule->mock('SimpleSearch', sub {
674 my $query = shift;
676 is($query, "he:$term", "Searching for expected term '$term' for exploding") or return '', [], 0;
678 my $record = MARC::Record->new;
679 if ($query =~ m/Arizona/) {
680 $record->add_fields(
681 [ '001', '1234' ],
682 [ '151', ' ', ' ', a => 'Arizona' ],
683 [ '551', ' ', ' ', a => 'United States', w => 'g' ],
684 [ '551', ' ', ' ', a => 'Maricopa County', w => 'h' ],
685 [ '551', ' ', ' ', a => 'Navajo County', w => 'h' ],
686 [ '551', ' ', ' ', a => 'Pima County', w => 'h' ],
687 [ '551', ' ', ' ', a => 'New Mexico' ],
690 return '', [ $record->as_usmarc() ], 1;
693 $UseQueryParser = 1;
694 $term = 'Arizona';
695 ( $error, $query, $simple_query, $query_cgi,
696 $query_desc, $limit, $limit_cgi, $limit_desc,
697 $query_type ) = buildQuery([], [ $term ], [ 'su-br' ], [ ], [], 0, 'en');
698 matchesExplodedTerms("Advanced search for broader subjects", $query, 'Arizona', 'United States');
700 ( $error, $query, $simple_query, $query_cgi,
701 $query_desc, $limit, $limit_cgi, $limit_desc,
702 $query_type ) = buildQuery([], [ $term ], [ 'su-na' ], [ ], [], 0, 'en');
703 matchesExplodedTerms("Advanced search for narrower subjects", $query, 'Arizona', 'Maricopa County', 'Navajo County', 'Pima County');
705 ( $error, $query, $simple_query, $query_cgi,
706 $query_desc, $limit, $limit_cgi, $limit_desc,
707 $query_type ) = buildQuery([], [ $term ], [ 'su-rl' ], [ ], [], 0, 'en');
708 matchesExplodedTerms("Advanced search for related subjects", $query, 'Arizona', 'United States', 'Maricopa County', 'Navajo County', 'Pima County');
710 ( $error, $query, $simple_query, $query_cgi,
711 $query_desc, $limit, $limit_cgi, $limit_desc,
712 $query_type ) = buildQuery([], [ "$term", 'history' ], [ 'su-rl', 'kw' ], [ ], [], 0, 'en');
713 matchesExplodedTerms("Advanced search for related subjects and keyword 'history' searches related subjects", $query, 'Arizona', 'United States', 'Maricopa County', 'Navajo County', 'Pima County');
714 like($query, qr/history/, "Advanced search for related subjects and keyword 'history' searches for 'history'");
716 ( $error, $query, $simple_query, $query_cgi,
717 $query_desc, $limit, $limit_cgi, $limit_desc,
718 $query_type ) = buildQuery([], [ 'history', "$term" ], [ 'kw', 'su-rl' ], [ ], [], 0, 'en');
719 matchesExplodedTerms("Order of terms doesn't matter for advanced search", $query, 'Arizona', 'United States', 'Maricopa County', 'Navajo County', 'Pima County');
720 like($query, qr/history/, "Order of terms doesn't matter for advanced search");
722 ( $error, $query, $simple_query, $query_cgi,
723 $query_desc, $limit, $limit_cgi, $limit_desc,
724 $query_type ) = buildQuery([], [ "su-br($term)" ], [ ], [ ], [], 0, 'en');
725 matchesExplodedTerms("Simple search for broader subjects", $query, 'Arizona', 'United States');
727 ( $error, $query, $simple_query, $query_cgi,
728 $query_desc, $limit, $limit_cgi, $limit_desc,
729 $query_type ) = buildQuery([], [ "su-na($term)" ], [ ], [ ], [], 0, 'en');
730 matchesExplodedTerms("Simple search for narrower subjects", $query, 'Arizona', 'Maricopa County', 'Navajo County', 'Pima County');
732 ( $error, $query, $simple_query, $query_cgi,
733 $query_desc, $limit, $limit_cgi, $limit_desc,
734 $query_type ) = buildQuery([], [ "su-rl($term)" ], [ ], [ ], [], 0, 'en');
735 matchesExplodedTerms("Simple search for related subjects", $query, 'Arizona', 'United States', 'Maricopa County', 'Navajo County', 'Pima County');
737 ( $error, $query, $simple_query, $query_cgi,
738 $query_desc, $limit, $limit_cgi, $limit_desc,
739 $query_type ) = buildQuery([], [ "history && su-rl($term)" ], [ ], [ ], [], 0, 'en');
740 matchesExplodedTerms("Simple search for related subjects and keyword 'history' searches related subjects", $query, 'Arizona', 'United States', 'Maricopa County', 'Navajo County', 'Pima County');
741 like($query, qr/history/, "Simple search for related subjects and keyword 'history' searches for 'history'");
743 sub matchesExplodedTerms {
744 my ($message, $query, @terms) = @_;
745 my $match = '(' . join ('|', map { " \@attr 1=Subject \@attr 4=1 \"$_\"" } @terms) . "){" . scalar(@terms) . "}";
746 like($query, qr/$match/, $message);
749 # authority records
750 use_ok('C4::AuthoritiesMarc');
751 $UseQueryParser = 0;
753 my ($auths, $count) = SearchAuthorities(
754 ['mainentry'], ['and'], [''], ['starts'],
755 ['shakespeare'], 0, 10, '', '', 1
757 is($count, 1, 'MARC21 authorities: one hit on mainentry starts with "shakespeare"');
758 ($auths, $count) = SearchAuthorities(
759 ['mainentry'], ['and'], [''], ['starts'],
760 ['shakespeare'], 0, 10, '', 'HeadingAsc', 1
762 is($count, 1, 'MARC21 authorities: one hit on mainentry starts with "shakespeare" sorted by heading ascending');
763 ($auths, $count) = SearchAuthorities(
764 ['mainentry'], ['and'], [''], ['starts'],
765 ['shakespeare'], 0, 10, '', 'HeadingDsc', 1
767 is($count, 1, 'MARC21 authorities: one hit on mainentry starts with "shakespeare" sorted by heading descending');
768 ($auths, $count) = SearchAuthorities(
769 ['match'], ['and'], [''], ['contains'],
770 ['沙士北亞威廉姆'], 0, 10, '', '', 1
772 is($count, 1, 'MARC21 authorities: one hit on match contains "沙士北亞威廉姆"');
773 ($auths, $count) = SearchAuthorities(
774 ['LC-card-number'], ['and'], [''], ['contains'],
775 ['99282477'], 0, 10, '', '', 1
777 is($count, 1, 'MARC21 authorities: one hit on LC-card-number contains "99282477"');
778 ($auths, $count) = SearchAuthorities(
779 ['all'], ['and'], [''], ['contains'],
780 ['professional wrestler'], 0, 10, '', '', 1
782 is($count, 1, 'MARC21 authorities: one hit on "all" (entire record) contains "professional wrestler"');
784 $UseQueryParser = 1;
786 ($auths, $count) = SearchAuthorities(
787 ['mainentry'], ['and'], [''], ['starts'],
788 ['shakespeare'], 0, 10, '', '', 1
790 is($count, 1, 'MARC21 authorities: one hit on mainentry starts with "shakespeare" (QP)');
791 ($auths, $count) = SearchAuthorities(
792 ['mainentry'], ['and'], [''], ['starts'],
793 ['shakespeare'], 0, 10, '', 'HeadingAsc', 1
795 is($count, 1, 'MARC21 authorities: one hit on mainentry starts with "shakespeare" sorted by heading ascending (QP)');
796 ($auths, $count) = SearchAuthorities(
797 ['mainentry'], ['and'], [''], ['starts'],
798 ['shakespeare'], 0, 10, '', 'HeadingDsc', 1
800 is($count, 1, 'MARC21 authorities: one hit on mainentry starts with "shakespeare" sorted by heading descending (QP)');
801 ($auths, $count) = SearchAuthorities(
802 ['match'], ['and'], [''], ['contains'],
803 ['沙士北亞威廉姆'], 0, 10, '', '', 1
805 is($count, 1, 'MARC21 authorities: one hit on match contains "沙士北亞威廉姆" (QP)');
807 # retrieve records that are larger than the MARC limit of 99,999 octets
808 ( undef, $results_hashref, $facets_loop ) =
809 getRecords('ti:marc the large record', '', [], [ 'biblioserver' ], '20', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
810 is($results_hashref->{biblioserver}->{hits}, 1, "Can do a search that retrieves an over-large bib record (bug 11096)");
811 @newresults = searchResults({'interface' =>'opac'}, $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 10, 0, 0,
812 $results_hashref->{'biblioserver'}->{"RECORDS"});
813 is($newresults[0]->{title}, 'Marc the Large Record', 'Able to render the title for over-large bib record (bug 11096)');
814 is($newresults[0]->{biblionumber}, '300', 'Over-large bib record has the correct biblionumber (bug 11096)');
815 like($newresults[0]->{notes}, qr/This is large note #550/, 'Able to render the notes field for over-large bib record (bug 11096)');
817 # notforloancount should be returned as part of searchResults output
818 ok( defined $newresults[0]->{notforloancount},
819 '\'notforloancount\' defined in searchResults output (Bug 12419)');
820 is( $newresults[0]->{notforloancount}, 2,
821 '\'notforloancount\' == 2 (Bug 12419)');
823 # verify that we don't attempt to sort if no results were returned
824 # because of a query error
825 warning_like {( undef, $results_hashref, $facets_loop ) =
826 getRecords('ccl=( AND )', '', ['title_az'], [ 'biblioserver' ], '20', 0, undef, \%branches, \%itemtypes, 'ccl', undef)
827 } qr/WARNING: query problem with/, 'got warning instead of crash when attempting to run invalid query (bug 9578)';
829 # Test facet calculation
830 my $facets_counter = {};
831 my $facets = C4::Koha::getFacets();
832 # Create a record with a 100$z field
833 my $marc_record = MARC::Record->new;
834 $marc_record->add_fields(
835 [ '001', '1234' ],
836 [ '100', ' ', ' ', a => 'Cohen Arazi, Tomas' ],
837 [ '100', 'z', ' ', a => 'Tomasito' ],
838 [ '245', ' ', ' ', a => 'First try' ]
840 C4::Search::_get_facets_data_from_record( $marc_record, $facets, $facets_counter );
841 is_deeply( { au => { 'Cohen Arazi, Tomas' => 1 } }, $facets_counter,
842 "_get_facets_data_from_record doesn't count 100\$z (Bug 12788)");
843 $marc_record = MARC::Record->new;
844 $marc_record->add_fields(
845 [ '001', '1234' ],
846 [ '100', ' ', ' ', a => 'Cohen Arazi, Tomas' ],
847 [ '100', 'z', ' ', a => 'Tomasito' ],
848 [ '245', ' ', ' ', a => 'Second try' ]
850 C4::Search::_get_facets_data_from_record( $marc_record, $facets, $facets_counter );
851 is_deeply( { au => { 'Cohen Arazi, Tomas' => 2 } }, $facets_counter,
852 "_get_facets_data_from_record correctly counts author facet twice");
854 # Test _get_facets_info
855 my $facets_info = C4::Search::_get_facets_info( $facets );
856 my $expected_facets_info_marc21 = {
857 'au' => { 'expanded' => undef,
858 'label_value' => "Authors" },
859 'ccode' => { 'expanded' => undef,
860 'label_value' => "CollectionCodes" },
861 'holdingbranch' => { 'expanded' => undef,
862 'label_value' => "HoldingLibrary" },
863 'itype' => { 'expanded' => undef,
864 'label_value' => "ItemTypes" },
865 'location' => { 'expanded' => undef,
866 'label_value' => "Location" },
867 'se' => { 'expanded' => undef,
868 'label_value' => "Series" },
869 'su-geo' => { 'expanded' => undef,
870 'label_value' => "Places" },
871 'su-to' => { 'expanded' => undef,
872 'label_value' => "Topics" },
873 'su-ut' => { 'expanded' => undef,
874 'label_value' => "Titles" }
876 delete $expected_facets_info_marc21->{holdingbranch}
877 if Koha::Libraries->count == 1;
878 is_deeply( $facets_info, $expected_facets_info_marc21,
879 "_get_facets_info returns the correct data");
881 cleanup();
884 sub run_unimarc_search_tests {
885 $datadir = tempdir();
886 system(dirname(__FILE__) . "/zebra_config.pl $datadir unimarc");
888 Koha::Caches->get_instance('config')->flush_all;
890 mock_GetMarcSubfieldStructure('unimarc');
891 my $context = new C4::Context("$datadir/etc/koha-conf.xml");
892 $context->set_context();
894 use_ok('C4::Search');
896 # set search syspreferences to a known starting point
897 $QueryStemming = 0;
898 $QueryAutoTruncate = 0;
899 $QueryWeightFields = 0;
900 $QueryFuzzy = 0;
901 $UseQueryParser = 0;
902 $marcflavour = 'UNIMARC';
904 index_sample_records_and_launch_zebra($datadir, 'unimarc');
906 my ( $error, $marcresults, $total_hits ) = SimpleSearch("ti=Järnvägarnas efterfrågan och den svenska industrin", 0, 10);
907 is($total_hits, 1, 'UNIMARC title search');
908 ( $error, $marcresults, $total_hits ) = SimpleSearch("ta=u", 0, 10);
909 is($total_hits, 1, 'UNIMARC target audience = u');
910 ( $error, $marcresults, $total_hits ) = SimpleSearch("ta=k", 0, 10);
911 is($total_hits, 4, 'UNIMARC target audience = k');
912 ( $error, $marcresults, $total_hits ) = SimpleSearch("ta=m", 0, 10);
913 is($total_hits, 3, 'UNIMARC target audience = m');
914 ( $error, $marcresults, $total_hits ) = SimpleSearch("item=EXCLU DU PRET", 0, 10);
915 is($total_hits, 1, 'UNIMARC generic item index (bug 10037)');
917 # authority records
918 use_ok('C4::AuthoritiesMarc');
919 $UseQueryParser = 0;
921 my ($auths, $count) = SearchAuthorities(
922 ['mainentry'], ['and'], [''], ['contains'],
923 ['wil'], 0, 10, '', '', 1
925 is($count, 11, 'UNIMARC authorities: hits on mainentry contains "wil"');
926 ($auths, $count) = SearchAuthorities(
927 ['match'], ['and'], [''], ['contains'],
928 ['wil'], 0, 10, '', '', 1
930 is($count, 11, 'UNIMARC authorities: hits on match contains "wil"');
931 ($auths, $count) = SearchAuthorities(
932 ['mainentry'], ['and'], [''], ['contains'],
933 ['michel'], 0, 20, '', '', 1
935 is($count, 14, 'UNIMARC authorities: hits on mainentry contains "michel"');
936 ($auths, $count) = SearchAuthorities(
937 ['mainmainentry'], ['and'], [''], ['exact'],
938 ['valley'], 0, 20, '', '', 1
940 is($count, 1, 'UNIMARC authorities: hits on mainmainentry = "valley"');
941 ($auths, $count) = SearchAuthorities(
942 ['mainmainentry'], ['and'], [''], ['exact'],
943 ['vall'], 0, 20, '', '', 1
945 is($count, 0, 'UNIMARC authorities: no hits on mainmainentry = "vall"');
946 ($auths, $count) = SearchAuthorities(
947 ['Any'], ['and'], [''], ['starts'],
948 ['jean'], 0, 30, '', '', 1
950 is($count, 24, 'UNIMARC authorities: hits on any starts with "jean"');
952 # Test _get_facets_info
953 my $facets = C4::Koha::getFacets();
954 my $facets_info = C4::Search::_get_facets_info( $facets );
955 my $expected_facets_info_unimarc = {
956 'au' => { 'expanded' => undef,
957 'label_value' => "Authors" },
958 'ccode' => { 'expanded' => undef,
959 'label_value' => "CollectionCodes" },
960 'holdingbranch' => { 'expanded' => undef,
961 'label_value' => "HoldingLibrary" },
962 'location' => { 'expanded' => undef,
963 'label_value' => "Location" },
964 'se' => { 'expanded' => undef,
965 'label_value' => "Series" },
966 'su-geo' => { 'expanded' => undef,
967 'label_value' => "Places" },
968 'su-to' => { 'expanded' => undef,
969 'label_value' => "Topics" },
970 'su-ut' => { 'expanded' => undef,
971 'label_value' => "Titles" }
973 delete $expected_facets_info_unimarc->{holdingbranch}
974 if Koha::Libraries->count == 1;
975 is_deeply( $facets_info, $expected_facets_info_unimarc,
976 "_get_facets_info returns the correct data");
978 cleanup();
981 subtest 'MARC21 + DOM' => sub {
982 plan tests => 112;
983 run_marc21_search_tests();
986 subtest 'UNIMARC + DOM' => sub {
987 plan tests => 14;
988 run_unimarc_search_tests();
991 # Make sure that following tests are not using our config settings
992 Koha::Caches->get_instance('config')->flush_all;