Bug 8995: (QA follow-up) Mock required sysprefs in the tests
[koha.git] / t / db_dependent / Search.t
blob60da42ff55a743df4989aa628e88406001d52552
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 } elsif ($pref eq 'EasyAnalyticalRecords') {
182 return '0';
183 } elsif ($pref eq 'OpenURLResolverURL') {
184 return '0';
185 } elsif ($pref eq 'OPACShowOpenURL') {
186 return '0';
187 } elsif ($pref eq 'OpenURLText') {
188 return '0';
189 } else {
190 warn "The syspref $pref was requested but I don't know what to say; this indicates that the test requires updating"
191 unless $pref =~ m/(XSLT|item|branch|holding|image)/i;
192 return 0;
195 $contextmodule->mock('queryparser', sub {
196 my $QParser = Koha::QueryParser::Driver::PQF->new();
197 $QParser->load_config("$datadir/etc/searchengine/queryparser.yaml");
198 return $QParser;
201 our $bibliomodule = new Test::MockModule('C4::Biblio');
203 sub mock_GetMarcSubfieldStructure {
204 my $marc_type = shift;
205 if ($marc_type eq 'marc21') {
206 $bibliomodule->mock('GetMarcSubfieldStructure', sub {
207 return {
208 'biblio.biblionumber' => [{ tagfield => '999', tagsubfield => 'c' }],
209 'biblio.isbn' => [{ tagfield => '020', tagsubfield => 'a' }],
210 'biblio.title' => [{ tagfield => '245', tagsubfield => 'a' }],
211 'biblio.notes' => [{ tagfield => '500', tagsubfield => 'a' }],
212 'items.barcode' => [{ tagfield => '952', tagsubfield => 'p' }],
213 'items.booksellerid' => [{ tagfield => '952', tagsubfield => 'e' }],
214 'items.ccode' => [{ tagfield => '952', tagsubfield => '8' }],
215 'items.cn_sort' => [{ tagfield => '952', tagsubfield => '6' }],
216 'items.cn_source' => [{ tagfield => '952', tagsubfield => '2' }],
217 'items.coded_location_qualifier' => [{ tagfield => '952', tagsubfield => 'f' }],
218 'items.copynumber' => [{ tagfield => '952', tagsubfield => 't' }],
219 'items.damaged' => [{ tagfield => '952', tagsubfield => '4' }],
220 'items.dateaccessioned' => [{ tagfield => '952', tagsubfield => 'd' }],
221 'items.datelastborrowed' => [{ tagfield => '952', tagsubfield => 's' }],
222 'items.datelastseen' => [{ tagfield => '952', tagsubfield => 'r' }],
223 'items.enumchron' => [{ tagfield => '952', tagsubfield => 'h' }],
224 'items.holdingbranch' => [{ tagfield => '952', tagsubfield => 'b' }],
225 'items.homebranch' => [{ tagfield => '952', tagsubfield => 'a' }],
226 'items.issues' => [{ tagfield => '952', tagsubfield => 'l' }],
227 'items.itemcallnumber' => [{ tagfield => '952', tagsubfield => 'o' }],
228 'items.itemlost' => [{ tagfield => '952', tagsubfield => '1' }],
229 'items.itemnotes' => [{ tagfield => '952', tagsubfield => 'z' }],
230 'items.itemnumber' => [{ tagfield => '952', tagsubfield => '9' }],
231 'items.itype' => [{ tagfield => '952', tagsubfield => 'y' }],
232 'items.location' => [{ tagfield => '952', tagsubfield => 'c' }],
233 'items.materials' => [{ tagfield => '952', tagsubfield => '3' }],
234 'items.nonpublicnote' => [{ tagfield => '952', tagsubfield => 'x' }],
235 'items.notforloan' => [{ tagfield => '952', tagsubfield => '7' }],
236 'items.onloan' => [{ tagfield => '952', tagsubfield => 'q' }],
237 'items.price' => [{ tagfield => '952', tagsubfield => 'g' }],
238 'items.renewals' => [{ tagfield => '952', tagsubfield => 'm' }],
239 'items.replacementprice' => [{ tagfield => '952', tagsubfield => 'v' }],
240 'items.replacementpricedate' => [{ tagfield => '952', tagsubfield => 'w' }],
241 'items.reserves' => [{ tagfield => '952', tagsubfield => 'n' }],
242 'items.restricted' => [{ tagfield => '952', tagsubfield => '5' }],
243 'items.stack' => [{ tagfield => '952', tagsubfield => 'j' }],
244 'items.uri' => [{ tagfield => '952', tagsubfield => 'u' }],
245 'items.withdrawn' => [{ tagfield => '952', tagsubfield => '0' }],
251 sub run_marc21_search_tests {
252 $datadir = tempdir();
253 system(dirname(__FILE__) . "/zebra_config.pl $datadir marc21");
255 Koha::Caches->get_instance('config')->flush_all;
257 mock_GetMarcSubfieldStructure('marc21');
258 my $context = new C4::Context("$datadir/etc/koha-conf.xml");
259 $context->set_context();
261 use_ok('C4::Search');
263 # set search syspreferences to a known starting point
264 $QueryStemming = 0;
265 $QueryAutoTruncate = 0;
266 $QueryWeightFields = 0;
267 $QueryFuzzy = 0;
268 $UseQueryParser = 0;
269 $marcflavour = 'MARC21';
271 my $indexes = C4::Search::getIndexes();
272 is(scalar(grep(/^ti$/, @$indexes)), 1, "Title index supported");
273 is(scalar(grep(/^arl$/, @$indexes)), 1, "Accelerated reading level index supported");
274 is(scalar(grep(/^arp$/, @$indexes)), 1, "Accelerated reading point index supported");
276 my $bibliomodule = new Test::MockModule('C4::Biblio');
278 my %branches = (
279 'CPL' => { 'branchaddress1' => 'Jefferson Summit', 'branchcode' => 'CPL', 'branchname' => 'Centerville', },
280 'FFL' => { 'branchaddress1' => 'River Station', 'branchcode' => 'FFL', 'branchname' => 'Fairfield', },
281 'FPL' => { 'branchaddress1' => 'Hickory Squere', 'branchcode' => 'FPL', 'branchname' => 'Fairview', },
282 'FRL' => { 'branchaddress1' => 'Smith Heights', 'branchcode' => 'FRL', 'branchname' => 'Franklin', },
283 'IPT' => { 'branchaddress1' => '', 'branchcode' => 'IPT', 'branchname' => "Institut Protestant de Théologie", },
284 'LPL' => { 'branchaddress1' => 'East Hills', 'branchcode' => 'LPL', 'branchname' => 'Liberty', },
285 'MPL' => { 'branchaddress1' => '372 Forest Street', 'branchcode' => 'MPL', 'branchname' => 'Midway', },
286 'PVL' => { 'branchaddress1' => 'Meadow Grove', 'branchcode' => 'PVL', 'branchname' => 'Pleasant Valley', },
287 'RPL' => { 'branchaddress1' => 'Johnson Terrace', 'branchcode' => 'RPL', 'branchname' => 'Riverside', },
288 'SPL' => { 'branchaddress1' => 'Highland Boulevard', 'branchcode' => 'SPL', 'branchname' => 'Springfield', },
289 'S' => { 'branchaddress1' => '', 'branchcode' => 'S', 'branchname' => 'Test', },
290 'TPL' => { 'branchaddress1' => 'Valley Way', 'branchcode' => 'TPL', 'branchname' => 'Troy', },
291 'UPL' => { 'branchaddress1' => 'Chestnut Hollow', 'branchcode' => 'UPL', 'branchname' => 'Union', },
293 my %itemtypes = (
294 'BK' => { 'imageurl' => 'bridge/book.gif', 'summary' => '', 'itemtype' => 'BK', 'description' => 'Books' },
295 'CF' => { 'imageurl' => 'bridge/computer_file.gif', 'summary' => '', 'itemtype' => 'CF', 'description' => 'Computer Files' },
296 'CR' => { 'imageurl' => 'bridge/periodical.gif', 'summary' => '', 'itemtype' => 'CR', 'description' => 'Continuing Resources' },
297 'MP' => { 'imageurl' => 'bridge/map.gif', 'summary' => '', 'itemtype' => 'MP', 'description' => 'Maps' },
298 'MU' => { 'imageurl' => 'bridge/sound.gif', 'summary' => '', 'itemtype' => 'MU', 'description' => 'Music' },
299 'MX' => { 'imageurl' => 'bridge/kit.gif', 'summary' => '', 'itemtype' => 'MX', 'description' => 'Mixed Materials' },
300 'REF' => { 'imageurl' => '', 'summary' => '', 'itemtype' => 'REF', 'description' => 'Reference' },
301 'VM' => { 'imageurl' => 'bridge/dvd.gif', 'summary' => '', 'itemtype' => 'VM', 'description' => 'Visual Materials' },
304 index_sample_records_and_launch_zebra($datadir, 'marc21');
306 my ($biblionumber, $title);
307 my $record = MARC::Record->new;
309 $record->add_fields(
310 [ '020', ' ', ' ', a => '9788522421718' ],
311 [ '245', '0', '0', a => 'Administração da produção /' ]
313 ($biblionumber,undef,$title) = FindDuplicate($record);
314 is($biblionumber, 51, 'Found duplicate with ISBN');
316 $record = MARC::Record->new;
318 $record->add_fields(
319 [ '100', '1', ' ', a => 'Carter, Philip J.' ],
320 [ '245', '1', '4', a => 'Test your emotional intelligence :' ]
322 ($biblionumber,undef,$title) = FindDuplicate($record);
323 is($biblionumber, 203, 'Found duplicate with author/title');
325 # Testing SimpleSearch
327 my ( $error, $marcresults, $total_hits ) = SimpleSearch("book", 0, 9);
329 is(scalar @$marcresults, 9, "SimpleSearch retrieved requested number of records");
330 is($total_hits, 101, "SimpleSearch for 'book' matched right number of records");
331 is($error, undef, "SimpleSearch does not return an error when successful");
333 my $marcresults2;
334 ( $error, $marcresults2, $total_hits ) = SimpleSearch("book", 5, 5);
335 is($marcresults->[5], $marcresults2->[0], "SimpleSearch cursor functions");
337 ( $error, $marcresults, $total_hits ) = SimpleSearch("kw=book", 0, 10);
338 is($total_hits, 101, "SimpleSearch handles simple CCL");
340 ( $error, $marcresults, $total_hits ) = SimpleSearch("Music-number=49631-2", 0, 10);
341 is($total_hits, 1, "SimpleSearch on music publisher number works (bug 8252)");
342 ( $error, $marcresults, $total_hits ) = SimpleSearch("Identifier-publisher-for-music=49631-2", 0, 10);
343 is($total_hits, 1, "SimpleSearch on music publisher number works using Identifier-publisher-for-music (bug 8252)");
345 # Testing getRecords
347 my $results_hashref;
348 my $facets_loop;
349 ( undef, $results_hashref, $facets_loop ) =
350 getRecords('kw:book', 'book', [], [ 'biblioserver' ], '19', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
351 is($results_hashref->{biblioserver}->{hits}, 101, "getRecords keyword search for 'book' matched right number of records");
352 is(scalar @{$results_hashref->{biblioserver}->{RECORDS}}, 19, "getRecords returned requested number of records");
353 my $record5 = $results_hashref->{biblioserver}->{RECORDS}->[5];
354 ( undef, $results_hashref, $facets_loop ) =
355 getRecords('kw:book', 'book', [], [ 'biblioserver' ], '20', 5, undef, \%branches, \%itemtypes, 'ccl', undef);
356 ok(!defined $results_hashref->{biblioserver}->{RECORDS}->[0] &&
357 !defined $results_hashref->{biblioserver}->{RECORDS}->[1] &&
358 !defined $results_hashref->{biblioserver}->{RECORDS}->[2] &&
359 !defined $results_hashref->{biblioserver}->{RECORDS}->[3] &&
360 !defined $results_hashref->{biblioserver}->{RECORDS}->[4] &&
361 $results_hashref->{biblioserver}->{RECORDS}->[5] eq $record5, "getRecords cursor works");
363 ( undef, $results_hashref, $facets_loop ) =
364 getRecords('ti:book', 'ti:book', [], [ 'biblioserver' ], '20', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
365 is($results_hashref->{biblioserver}->{hits}, 11, "getRecords title search for 'book' matched right number of records");
367 ( undef, $results_hashref, $facets_loop ) =
368 getRecords('au:Lessig', 'au:Lessig', [], [ 'biblioserver' ], '20', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
369 is($results_hashref->{biblioserver}->{hits}, 4, "getRecords title search for 'Australia' matched right number of records");
371 ( undef, $results_hashref, $facets_loop ) =
372 getRecords('salud', 'salud', [], [ 'biblioserver' ], '19', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
373 ok(MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[0],'UTF-8')->title_proper() =~ m/^Efectos del ambiente/ &&
374 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' &&
375 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[18],'UTF-8')->title_proper() =~ m/^Indicadores de resultados identificados/
376 , "Simple relevance sorting in getRecords matches old behavior");
378 ( undef, $results_hashref, $facets_loop ) =
379 getRecords('salud', 'salud', [ 'author_az' ], [ 'biblioserver' ], '38', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
380 ok(MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[0],'UTF-8')->title_proper() =~ m/la enfermedad laboral\^ies$/ &&
381 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[6],'UTF-8')->title_proper() =~ m/^Indicadores de resultados identificados/ &&
382 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[18],'UTF-8')->title_proper() eq 'World health statistics 2009^ien'
383 , "Simple ascending author sorting in getRecords matches old behavior");
385 ( undef, $results_hashref, $facets_loop ) =
386 getRecords('salud', 'salud', [ 'author_za' ], [ 'biblioserver' ], '38', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
387 ok(MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[0],'UTF-8')->title_proper() eq 'World health statistics 2009^ien' &&
388 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[12],'UTF-8')->title_proper() =~ m/^Indicadores de resultados identificados/ &&
389 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[18],'UTF-8')->title_proper() =~ m/la enfermedad laboral\^ies$/
390 , "Simple descending author sorting in getRecords matches old behavior");
392 ( undef, $results_hashref, $facets_loop ) =
393 getRecords('salud', 'salud', [ 'pubdate_asc' ], [ 'biblioserver' ], '38', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
394 ok(MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[0],'UTF-8')->title_proper() eq 'Manual de higiene industrial^ies' &&
395 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[7],'UTF-8')->title_proper() =~ m/seguridad e higiene del trabajo\^ies$/ &&
396 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[18],'UTF-8')->title_proper() =~ m/^Indicadores de resultados identificados/
397 , "Simple ascending publication date sorting in getRecords matches old behavior");
399 ( undef, $results_hashref, $facets_loop ) =
400 getRecords('salud', 'salud', [ 'pubdate_dsc' ], [ 'biblioserver' ], '38', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
401 ok(MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[0],'UTF-8')->title_proper() =~ m/^Estado de salud/ &&
402 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[7],'UTF-8')->title_proper() eq 'World health statistics 2009^ien' &&
403 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[18],'UTF-8')->title_proper() eq 'Manual de higiene industrial^ies'
404 , "Simple descending publication date sorting in getRecords matches old behavior");
406 ( undef, $results_hashref, $facets_loop ) =
407 getRecords('books', 'books', [ 'relevance' ], [ 'biblioserver' ], '20', 0, undef, \%branches, \%itemtypes, undef, 1);
408 $record = MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[0]);
409 is($record->title_proper(), 'Books', "Scan returned requested item");
410 is($record->subfield('100', 'a'), 2, "Scan returned correct number of records matching term");
411 # Time to test buildQuery and searchResults too.
413 my ( $query, $simple_query, $query_cgi,
414 $query_desc, $limit, $limit_cgi, $limit_desc,
415 $query_type );
416 ( $error, $query, $simple_query, $query_cgi,
417 $query_desc, $limit, $limit_cgi, $limit_desc,
418 $query_type ) = buildQuery([], [ 'salud' ], [], [], [], 0, 'en');
419 like($query, qr/kw\W.*salud/, "Built CCL keyword query");
421 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
422 is($results_hashref->{biblioserver}->{hits}, 19, "getRecords generated keyword search for 'salud' matched right number of records");
424 my @newresults = searchResults({'interface' => 'opac'}, $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 18, 0, 0,
425 $results_hashref->{'biblioserver'}->{"RECORDS"});
426 is(scalar @newresults,18, "searchResults returns requested number of hits");
428 ( $error, $query, $simple_query, $query_cgi,
429 $query_desc, $limit, $limit_cgi, $limit_desc,
430 $query_type ) = buildQuery([ 'and' ], [ 'salud', 'higiene' ], [], [], [], 0, 'en');
431 like($query, qr/kw\W.*salud\W.*and.*kw\W.*higiene/, "Built composed explicit-and 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}, 3, "getRecords generated composed keyword search for 'salud' explicit-and '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([ 'or' ], [ 'salud', 'higiene' ], [], [], [], 0, 'en');
439 like($query, qr/kw\W.*salud\W.*or.*kw\W.*higiene/, "Built composed explicit-or 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}, 20, "getRecords generated composed keyword search for 'salud' explicit-or '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', 'higiene' ], [], [], [], 0, 'en');
447 like($query, qr/kw\W.*salud\W.*and.*kw\W.*higiene/, "Built composed implicit-and CCL keyword query");
449 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
450 is($results_hashref->{biblioserver}->{hits}, 3, "getRecords generated composed keyword search for 'salud' implicit-and 'higiene' matched right number of records");
452 ( $error, $query, $simple_query, $query_cgi,
453 $query_desc, $limit, $limit_cgi, $limit_desc,
454 $query_type ) = buildQuery([], [ 'salud' ], [ 'kw' ], [ 'su-to:Laboratorios' ], [], 0, 'en');
455 like($query, qr/kw\W.*salud\W*and\W*su-to\W.*Laboratorios/, "Faceted query generated correctly");
456 unlike($query_desc, qr/Laboratorios/, "Facets not included in query description");
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 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-itype:MP', 'mc-itype:MU' ], [], 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 mc-faceted search matched right number of records");
470 ( $error, $query, $simple_query, $query_cgi,
471 $query_desc, $limit, $limit_cgi, $limit_desc,
472 $query_type ) = buildQuery([], [ '' ], [ 'kw' ], [ 'mc-loc:GEN', 'branch:FFL' ], [], 0, 'en');
474 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
475 is($results_hashref->{biblioserver}->{hits}, 2, "getRecords generated multi-faceted search matched right number of records");
477 ( $error, $query, $simple_query, $query_cgi,
478 $query_desc, $limit, $limit_cgi, $limit_desc,
479 $query_type ) = buildQuery([], [ 'NEKLS' ], [ 'Code-institution' ], [], [], 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}, 12,
482 'search using index whose name contains "ns" returns expected results (bug 10271)');
484 $UseQueryParser = 1;
485 ( $error, $query, $simple_query, $query_cgi,
486 $query_desc, $limit, $limit_cgi, $limit_desc,
487 $query_type ) = buildQuery([], [ 'book' ], [ 'kw' ], [], [], 0, 'en');
488 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
489 is($results_hashref->{biblioserver}->{hits}, 101, "Search for 'book' with index set to 'kw' returns 101 hits");
490 ( $error, $query, $simple_query, $query_cgi,
491 $query_desc, $limit, $limit_cgi, $limit_desc,
492 $query_type ) = buildQuery([ 'and' ], [ 'book', 'another' ], [ 'kw', 'kw' ], [], [], 0, 'en');
493 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
494 is($results_hashref->{biblioserver}->{hits}, 1, "Search for 'kw:book && kw:another' returns 1 hit");
495 $UseQueryParser = 0;
497 # FIXME: the availability limit does not actually work, so for the moment we
498 # are just checking that it behaves consistently
499 ( $error, $query, $simple_query, $query_cgi,
500 $query_desc, $limit, $limit_cgi, $limit_desc,
501 $query_type ) = buildQuery([], [ '' ], [ 'kw' ], [ 'available' ], [], 0, 'en');
503 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
504 is($results_hashref->{biblioserver}->{hits}, 26, "getRecords generated availability-limited search matched right number of records");
506 @newresults = searchResults({'interface'=>'opac'}, $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 17, 0, 0,
507 $results_hashref->{'biblioserver'}->{"RECORDS"});
508 my $allavailable = 'true';
509 foreach my $result (@newresults) {
510 $allavailable = 'false' unless $result->{availablecount} > 0;
512 is ($allavailable, 'true', 'All records have at least one item available');
515 ( $error, $query, $simple_query, $query_cgi,
516 $query_desc, $limit, $limit_cgi, $limit_desc,
517 $query_type ) = buildQuery([], [ 'pqf=@attr 1=_ALLRECORDS @attr 2=103 ""' ], [], [], [], 0, 'en');
519 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
520 is($results_hashref->{biblioserver}->{hits}, 180, "getRecords on _ALLRECORDS PQF returned all records");
522 ( $error, $query, $simple_query, $query_cgi,
523 $query_desc, $limit, $limit_cgi, $limit_desc,
524 $query_type ) = buildQuery([], [ 'pqf=@attr 1=1016 "Lessig"' ], [], [], [], 0, 'en');
526 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
527 is($results_hashref->{biblioserver}->{hits}, 4, "getRecords PQF author search for Lessig returned proper number of matches");
529 ( $error, $query, $simple_query, $query_cgi,
530 $query_desc, $limit, $limit_cgi, $limit_desc,
531 $query_type ) = buildQuery([], [ 'ccl=au:Lessig' ], [], [], [], 0, 'en');
533 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
534 is($results_hashref->{biblioserver}->{hits}, 4, "getRecords CCL author search for Lessig returned proper number of matches");
536 ( $error, $query, $simple_query, $query_cgi,
537 $query_desc, $limit, $limit_cgi, $limit_desc,
538 $query_type ) = buildQuery([], [ 'cql=dc.author any lessig' ], [], [], [], 0, 'en');
540 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
541 is($results_hashref->{biblioserver}->{hits}, 4, "getRecords CQL author search for Lessig returned proper number of matches");
543 $QueryStemming = $QueryAutoTruncate = $QueryFuzzy = 0;
544 $QueryWeightFields = 1;
545 ( $error, $query, $simple_query, $query_cgi,
546 $query_desc, $limit, $limit_cgi, $limit_desc,
547 $query_type ) = buildQuery([], [ 'salud' ], [ 'kw' ], [], [], 0, 'en');
549 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
550 is($results_hashref->{biblioserver}->{hits}, 19, "Weighted query returned correct number of results");
551 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");
553 $QueryStemming = $QueryWeightFields = $QueryFuzzy = 0;
554 $QueryAutoTruncate = 1;
555 ( $error, $query, $simple_query, $query_cgi,
556 $query_desc, $limit, $limit_cgi, $limit_desc,
557 $query_type ) = buildQuery([], [ 'medic' ], [ 'kw' ], [], [], 0, 'en');
559 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
560 is($results_hashref->{biblioserver}->{hits}, 5, "Search for 'medic' returns matches with automatic truncation on");
562 ( $error, $query, $simple_query, $query_cgi,
563 $query_desc, $limit, $limit_cgi, $limit_desc,
564 $query_type ) = buildQuery([], [ 'medic*' ], [ '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}, 5, "Search for 'medic*' returns matches with automatic truncation on");
569 $QueryStemming = $QueryFuzzy = $QueryAutoTruncate = 0;
570 $QueryWeightFields = 1;
571 ( $error, $query, $simple_query, $query_cgi,
572 $query_desc, $limit, $limit_cgi, $limit_desc,
573 $query_type ) = buildQuery([], [ 'web application' ], [ 'kw' ], [], [], 0, 'en');
574 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
575 is($results_hashref->{biblioserver}->{hits}, 1, "Search for 'web application' returns one hit with QueryWeightFields on");
577 ( $error, $query, $simple_query, $query_cgi,
578 $query_desc, $limit, $limit_cgi, $limit_desc,
579 $query_type ) = buildQuery([], [ 'web "application' ], [ '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}, 1, "Search for 'web \"application' returns one hit with QueryWeightFields on (bug 7518)");
583 $QueryStemming = $QueryWeightFields = $QueryFuzzy = $QueryAutoTruncate = 0;
584 ( $error, $query, $simple_query, $query_cgi,
585 $query_desc, $limit, $limit_cgi, $limit_desc,
586 $query_type ) = buildQuery([], [ 'medic' ], [ 'kw' ], [], [], 0, 'en');
588 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
589 is($results_hashref->{biblioserver}->{hits}, undef, "Search for 'medic' returns no matches with automatic truncation off");
591 ( $error, $query, $simple_query, $query_cgi,
592 $query_desc, $limit, $limit_cgi, $limit_desc,
593 $query_type ) = buildQuery([], [ 'medic*' ], [ 'kw' ], [], [], 0, 'en');
595 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
596 is($results_hashref->{biblioserver}->{hits}, 5, "Search for 'medic*' returns matches with automatic truncation off");
598 $QueryStemming = $QueryWeightFields = 1;
599 $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}, 7, "Search for 'pressed' returns matches when stemming (and query weighting) is on");
607 $QueryStemming = $QueryWeightFields = $QueryFuzzy = $QueryAutoTruncate = 0;
608 ( $error, $query, $simple_query, $query_cgi,
609 $query_desc, $limit, $limit_cgi, $limit_desc,
610 $query_type ) = buildQuery([], [ 'pressed' ], [ 'kw' ], [], [], 0, 'en');
612 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
613 is($results_hashref->{biblioserver}->{hits}, undef, "Search for 'pressed' returns no matches when stemming is off");
615 ( $error, $query, $simple_query, $query_cgi,
616 $query_desc, $limit, $limit_cgi, $limit_desc,
617 $query_type ) = buildQuery([], [ 'ccl=an:42' ], [], ['available'], [], 0, 'en');
618 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' );
619 is( $query_desc, 'an:42', 'buildQuery should remove the available part from the query' );
621 ( $error, $query, $simple_query, $query_cgi,
622 $query_desc, $limit, $limit_cgi, $limit_desc,
623 $query_type ) = buildQuery([], [ 0 ], [ 'su,phr' ], [], [], 0, 'en');
624 is($query, 'su,phr=0 ', 'buildQuery should keep 0 value');
626 # Let's see what happens when we pass bad data into these routines.
627 # We have to catch warnings since we're not very good about returning errors.
629 warning_like { ( $error, $marcresults, $total_hits ) = SimpleSearch("@==ccl blah", 0, 9) } qr/CCL parsing error/,
630 "SimpleSearch warns about CCL parsing error with nonsense query";
631 isnt($error, undef, "SimpleSearch returns an error when passed gibberish");
633 warning_like {( undef, $results_hashref, $facets_loop ) =
634 getRecords('kw:book', 'book', [], [ 'biblioserver' ], '19', 0, undef, \%branches, \%itemtypes, 'nonsense', undef) }
635 qr/Unknown query_type/, "getRecords warns about unknown query type";
637 warning_like {( undef, $results_hashref, $facets_loop ) =
638 getRecords('pqf=@attr 1=4 "title"', 'pqf=@attr 1=4 "title"', [], [ 'biblioserver' ], '19', 0, undef, \%branches, \%itemtypes, '', undef) }
639 qr/WARNING: query problem/, "getRecords warns when query type is not specified for non-CCL query";
641 # Let's just test a few other bits and bobs, just for fun
643 ($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);
644 @newresults = searchResults({'interface'=>'intranet'}, $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 17, 0, 0,
645 $results_hashref->{'biblioserver'}->{"RECORDS"});
646 is($newresults[0]->{'alternateholdings_count'}, 1, 'Alternate holdings filled in correctly');
649 ## Regression test for Bug 10741
651 # make one of the test items appear to be in transit
652 my $circ_module = new Test::MockModule('C4::Circulation');
653 $circ_module->mock('GetTransfers', sub {
654 my $itemnumber = shift // -1;
655 if ($itemnumber == 11) {
656 return ('2013-07-19', 'MPL', 'CPL');
657 } else {
658 return;
662 ($error, $results_hashref, $facets_loop) = getRecords("TEST12121212","TEST12121212",[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
663 @newresults = searchResults({'interface'=>'intranet'}, $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 17, 0, 0,
664 $results_hashref->{'biblioserver'}->{"RECORDS"});
665 ok(!exists($newresults[0]->{norequests}), 'presence of a transit does not block hold request action (bug 10741)');
667 ## Regression test for bug 10684
668 ( undef, $results_hashref, $facets_loop ) =
669 getRecords('ti:punctuation', 'punctuation', [], [ 'biblioserver' ], '19', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
670 is($results_hashref->{biblioserver}->{hits}, 1, "search for ti:punctuation returned expected number of records");
671 warning_like { @newresults = searchResults({'intranet' => 'intranet'}, $query_desc,
672 $results_hashref->{'biblioserver'}->{'hits'}, 20, 0, 0,
673 $results_hashref->{'biblioserver'}->{"RECORDS"}) }
674 qr/^ERROR DECODING RECORD - Tag "50%" is not a valid tag/,
675 "Warning is raised correctly for invalid tags in MARC::Record";
676 is(scalar(@newresults), 0, 'a record that cannot be parsed by MARC::Record is simply skipped (bug 10684)');
678 # Testing exploding indexes
679 my $term;
680 my $searchmodule = new Test::MockModule('C4::Search');
681 $searchmodule->mock('SimpleSearch', sub {
682 my $query = shift;
684 is($query, "he:$term", "Searching for expected term '$term' for exploding") or return '', [], 0;
686 my $record = MARC::Record->new;
687 if ($query =~ m/Arizona/) {
688 $record->add_fields(
689 [ '001', '1234' ],
690 [ '151', ' ', ' ', a => 'Arizona' ],
691 [ '551', ' ', ' ', a => 'United States', w => 'g' ],
692 [ '551', ' ', ' ', a => 'Maricopa County', w => 'h' ],
693 [ '551', ' ', ' ', a => 'Navajo County', w => 'h' ],
694 [ '551', ' ', ' ', a => 'Pima County', w => 'h' ],
695 [ '551', ' ', ' ', a => 'New Mexico' ],
698 return '', [ $record->as_usmarc() ], 1;
701 $UseQueryParser = 1;
702 $term = 'Arizona';
703 ( $error, $query, $simple_query, $query_cgi,
704 $query_desc, $limit, $limit_cgi, $limit_desc,
705 $query_type ) = buildQuery([], [ $term ], [ 'su-br' ], [ ], [], 0, 'en');
706 matchesExplodedTerms("Advanced search for broader subjects", $query, 'Arizona', 'United States');
708 ( $error, $query, $simple_query, $query_cgi,
709 $query_desc, $limit, $limit_cgi, $limit_desc,
710 $query_type ) = buildQuery([], [ $term ], [ 'su-na' ], [ ], [], 0, 'en');
711 matchesExplodedTerms("Advanced search for narrower subjects", $query, 'Arizona', 'Maricopa County', 'Navajo County', 'Pima County');
713 ( $error, $query, $simple_query, $query_cgi,
714 $query_desc, $limit, $limit_cgi, $limit_desc,
715 $query_type ) = buildQuery([], [ $term ], [ 'su-rl' ], [ ], [], 0, 'en');
716 matchesExplodedTerms("Advanced search for related subjects", $query, 'Arizona', 'United States', 'Maricopa County', 'Navajo County', 'Pima County');
718 ( $error, $query, $simple_query, $query_cgi,
719 $query_desc, $limit, $limit_cgi, $limit_desc,
720 $query_type ) = buildQuery([], [ "$term", 'history' ], [ 'su-rl', 'kw' ], [ ], [], 0, 'en');
721 matchesExplodedTerms("Advanced search for related subjects and keyword 'history' searches related subjects", $query, 'Arizona', 'United States', 'Maricopa County', 'Navajo County', 'Pima County');
722 like($query, qr/history/, "Advanced search for related subjects and keyword 'history' searches for 'history'");
724 ( $error, $query, $simple_query, $query_cgi,
725 $query_desc, $limit, $limit_cgi, $limit_desc,
726 $query_type ) = buildQuery([], [ 'history', "$term" ], [ 'kw', 'su-rl' ], [ ], [], 0, 'en');
727 matchesExplodedTerms("Order of terms doesn't matter for advanced search", $query, 'Arizona', 'United States', 'Maricopa County', 'Navajo County', 'Pima County');
728 like($query, qr/history/, "Order of terms doesn't matter for advanced search");
730 ( $error, $query, $simple_query, $query_cgi,
731 $query_desc, $limit, $limit_cgi, $limit_desc,
732 $query_type ) = buildQuery([], [ "su-br($term)" ], [ ], [ ], [], 0, 'en');
733 matchesExplodedTerms("Simple search for broader subjects", $query, 'Arizona', 'United States');
735 ( $error, $query, $simple_query, $query_cgi,
736 $query_desc, $limit, $limit_cgi, $limit_desc,
737 $query_type ) = buildQuery([], [ "su-na($term)" ], [ ], [ ], [], 0, 'en');
738 matchesExplodedTerms("Simple search for narrower subjects", $query, 'Arizona', 'Maricopa County', 'Navajo County', 'Pima County');
740 ( $error, $query, $simple_query, $query_cgi,
741 $query_desc, $limit, $limit_cgi, $limit_desc,
742 $query_type ) = buildQuery([], [ "su-rl($term)" ], [ ], [ ], [], 0, 'en');
743 matchesExplodedTerms("Simple search for related subjects", $query, 'Arizona', 'United States', 'Maricopa County', 'Navajo County', 'Pima County');
745 ( $error, $query, $simple_query, $query_cgi,
746 $query_desc, $limit, $limit_cgi, $limit_desc,
747 $query_type ) = buildQuery([], [ "history && su-rl($term)" ], [ ], [ ], [], 0, 'en');
748 matchesExplodedTerms("Simple search for related subjects and keyword 'history' searches related subjects", $query, 'Arizona', 'United States', 'Maricopa County', 'Navajo County', 'Pima County');
749 like($query, qr/history/, "Simple search for related subjects and keyword 'history' searches for 'history'");
751 sub matchesExplodedTerms {
752 my ($message, $query, @terms) = @_;
753 my $match = '(' . join ('|', map { " \@attr 1=Subject \@attr 4=1 \"$_\"" } @terms) . "){" . scalar(@terms) . "}";
754 like($query, qr/$match/, $message);
757 # authority records
758 use_ok('C4::AuthoritiesMarc');
759 $UseQueryParser = 0;
761 my ($auths, $count) = SearchAuthorities(
762 ['mainentry'], ['and'], [''], ['starts'],
763 ['shakespeare'], 0, 10, '', '', 1
765 is($count, 1, 'MARC21 authorities: one hit on mainentry starts with "shakespeare"');
766 ($auths, $count) = SearchAuthorities(
767 ['mainentry'], ['and'], [''], ['starts'],
768 ['shakespeare'], 0, 10, '', 'HeadingAsc', 1
770 is($count, 1, 'MARC21 authorities: one hit on mainentry starts with "shakespeare" sorted by heading ascending');
771 ($auths, $count) = SearchAuthorities(
772 ['mainentry'], ['and'], [''], ['starts'],
773 ['shakespeare'], 0, 10, '', 'HeadingDsc', 1
775 is($count, 1, 'MARC21 authorities: one hit on mainentry starts with "shakespeare" sorted by heading descending');
776 ($auths, $count) = SearchAuthorities(
777 ['match'], ['and'], [''], ['contains'],
778 ['沙士北亞威廉姆'], 0, 10, '', '', 1
780 is($count, 1, 'MARC21 authorities: one hit on match contains "沙士北亞威廉姆"');
781 ($auths, $count) = SearchAuthorities(
782 ['LC-card-number'], ['and'], [''], ['contains'],
783 ['99282477'], 0, 10, '', '', 1
785 is($count, 1, 'MARC21 authorities: one hit on LC-card-number contains "99282477"');
786 ($auths, $count) = SearchAuthorities(
787 ['all'], ['and'], [''], ['contains'],
788 ['professional wrestler'], 0, 10, '', '', 1
790 is($count, 1, 'MARC21 authorities: one hit on "all" (entire record) contains "professional wrestler"');
792 $UseQueryParser = 1;
794 ($auths, $count) = SearchAuthorities(
795 ['mainentry'], ['and'], [''], ['starts'],
796 ['shakespeare'], 0, 10, '', '', 1
798 is($count, 1, 'MARC21 authorities: one hit on mainentry starts with "shakespeare" (QP)');
799 ($auths, $count) = SearchAuthorities(
800 ['mainentry'], ['and'], [''], ['starts'],
801 ['shakespeare'], 0, 10, '', 'HeadingAsc', 1
803 is($count, 1, 'MARC21 authorities: one hit on mainentry starts with "shakespeare" sorted by heading ascending (QP)');
804 ($auths, $count) = SearchAuthorities(
805 ['mainentry'], ['and'], [''], ['starts'],
806 ['shakespeare'], 0, 10, '', 'HeadingDsc', 1
808 is($count, 1, 'MARC21 authorities: one hit on mainentry starts with "shakespeare" sorted by heading descending (QP)');
809 ($auths, $count) = SearchAuthorities(
810 ['match'], ['and'], [''], ['contains'],
811 ['沙士北亞威廉姆'], 0, 10, '', '', 1
813 is($count, 1, 'MARC21 authorities: one hit on match contains "沙士北亞威廉姆" (QP)');
815 # retrieve records that are larger than the MARC limit of 99,999 octets
816 ( undef, $results_hashref, $facets_loop ) =
817 getRecords('ti:marc the large record', '', [], [ 'biblioserver' ], '20', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
818 is($results_hashref->{biblioserver}->{hits}, 1, "Can do a search that retrieves an over-large bib record (bug 11096)");
819 @newresults = searchResults({'interface' =>'opac'}, $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 10, 0, 0,
820 $results_hashref->{'biblioserver'}->{"RECORDS"});
821 is($newresults[0]->{title}, 'Marc the Large Record', 'Able to render the title for over-large bib record (bug 11096)');
822 is($newresults[0]->{biblionumber}, '300', 'Over-large bib record has the correct biblionumber (bug 11096)');
823 like($newresults[0]->{notes}, qr/This is large note #550/, 'Able to render the notes field for over-large bib record (bug 11096)');
825 # notforloancount should be returned as part of searchResults output
826 ok( defined $newresults[0]->{notforloancount},
827 '\'notforloancount\' defined in searchResults output (Bug 12419)');
828 is( $newresults[0]->{notforloancount}, 2,
829 '\'notforloancount\' == 2 (Bug 12419)');
831 # verify that we don't attempt to sort if no results were returned
832 # because of a query error
833 warning_like {( undef, $results_hashref, $facets_loop ) =
834 getRecords('ccl=( AND )', '', ['title_az'], [ 'biblioserver' ], '20', 0, undef, \%branches, \%itemtypes, 'ccl', undef)
835 } qr/WARNING: query problem with/, 'got warning instead of crash when attempting to run invalid query (bug 9578)';
837 # Test facet calculation
838 my $facets_counter = {};
839 my $facets = C4::Koha::getFacets();
840 # Create a record with a 100$z field
841 my $marc_record = MARC::Record->new;
842 $marc_record->add_fields(
843 [ '001', '1234' ],
844 [ '100', ' ', ' ', a => 'Cohen Arazi, Tomas' ],
845 [ '100', 'z', ' ', a => 'Tomasito' ],
846 [ '245', ' ', ' ', a => 'First try' ]
848 C4::Search::_get_facets_data_from_record( $marc_record, $facets, $facets_counter );
849 is_deeply( { au => { 'Cohen Arazi, Tomas' => 1 } }, $facets_counter,
850 "_get_facets_data_from_record doesn't count 100\$z (Bug 12788)");
851 $marc_record = MARC::Record->new;
852 $marc_record->add_fields(
853 [ '001', '1234' ],
854 [ '100', ' ', ' ', a => 'Cohen Arazi, Tomas' ],
855 [ '100', 'z', ' ', a => 'Tomasito' ],
856 [ '245', ' ', ' ', a => 'Second try' ]
858 C4::Search::_get_facets_data_from_record( $marc_record, $facets, $facets_counter );
859 is_deeply( { au => { 'Cohen Arazi, Tomas' => 2 } }, $facets_counter,
860 "_get_facets_data_from_record correctly counts author facet twice");
862 # Test _get_facets_info
863 my $facets_info = C4::Search::_get_facets_info( $facets );
864 my $expected_facets_info_marc21 = {
865 'au' => { 'expanded' => undef,
866 'label_value' => "Authors" },
867 'ccode' => { 'expanded' => undef,
868 'label_value' => "CollectionCodes" },
869 'holdingbranch' => { 'expanded' => undef,
870 'label_value' => "HoldingLibrary" },
871 'itype' => { 'expanded' => undef,
872 'label_value' => "ItemTypes" },
873 'location' => { 'expanded' => undef,
874 'label_value' => "Location" },
875 'se' => { 'expanded' => undef,
876 'label_value' => "Series" },
877 'su-geo' => { 'expanded' => undef,
878 'label_value' => "Places" },
879 'su-to' => { 'expanded' => undef,
880 'label_value' => "Topics" },
881 'su-ut' => { 'expanded' => undef,
882 'label_value' => "Titles" }
884 delete $expected_facets_info_marc21->{holdingbranch}
885 if Koha::Libraries->count == 1;
886 is_deeply( $facets_info, $expected_facets_info_marc21,
887 "_get_facets_info returns the correct data");
889 cleanup();
892 sub run_unimarc_search_tests {
893 $datadir = tempdir();
894 system(dirname(__FILE__) . "/zebra_config.pl $datadir unimarc");
896 Koha::Caches->get_instance('config')->flush_all;
898 mock_GetMarcSubfieldStructure('unimarc');
899 my $context = new C4::Context("$datadir/etc/koha-conf.xml");
900 $context->set_context();
902 use_ok('C4::Search');
904 # set search syspreferences to a known starting point
905 $QueryStemming = 0;
906 $QueryAutoTruncate = 0;
907 $QueryWeightFields = 0;
908 $QueryFuzzy = 0;
909 $UseQueryParser = 0;
910 $marcflavour = 'UNIMARC';
912 index_sample_records_and_launch_zebra($datadir, 'unimarc');
914 my ( $error, $marcresults, $total_hits ) = SimpleSearch("ti=Järnvägarnas efterfrågan och den svenska industrin", 0, 10);
915 is($total_hits, 1, 'UNIMARC title search');
916 ( $error, $marcresults, $total_hits ) = SimpleSearch("ta=u", 0, 10);
917 is($total_hits, 1, 'UNIMARC target audience = u');
918 ( $error, $marcresults, $total_hits ) = SimpleSearch("ta=k", 0, 10);
919 is($total_hits, 4, 'UNIMARC target audience = k');
920 ( $error, $marcresults, $total_hits ) = SimpleSearch("ta=m", 0, 10);
921 is($total_hits, 3, 'UNIMARC target audience = m');
922 ( $error, $marcresults, $total_hits ) = SimpleSearch("item=EXCLU DU PRET", 0, 10);
923 is($total_hits, 1, 'UNIMARC generic item index (bug 10037)');
925 # authority records
926 use_ok('C4::AuthoritiesMarc');
927 $UseQueryParser = 0;
929 my ($auths, $count) = SearchAuthorities(
930 ['mainentry'], ['and'], [''], ['contains'],
931 ['wil'], 0, 10, '', '', 1
933 is($count, 11, 'UNIMARC authorities: hits on mainentry contains "wil"');
934 ($auths, $count) = SearchAuthorities(
935 ['match'], ['and'], [''], ['contains'],
936 ['wil'], 0, 10, '', '', 1
938 is($count, 11, 'UNIMARC authorities: hits on match contains "wil"');
939 ($auths, $count) = SearchAuthorities(
940 ['mainentry'], ['and'], [''], ['contains'],
941 ['michel'], 0, 20, '', '', 1
943 is($count, 14, 'UNIMARC authorities: hits on mainentry contains "michel"');
944 ($auths, $count) = SearchAuthorities(
945 ['mainmainentry'], ['and'], [''], ['exact'],
946 ['valley'], 0, 20, '', '', 1
948 is($count, 1, 'UNIMARC authorities: hits on mainmainentry = "valley"');
949 ($auths, $count) = SearchAuthorities(
950 ['mainmainentry'], ['and'], [''], ['exact'],
951 ['vall'], 0, 20, '', '', 1
953 is($count, 0, 'UNIMARC authorities: no hits on mainmainentry = "vall"');
954 ($auths, $count) = SearchAuthorities(
955 ['Any'], ['and'], [''], ['starts'],
956 ['jean'], 0, 30, '', '', 1
958 is($count, 24, 'UNIMARC authorities: hits on any starts with "jean"');
960 # Test _get_facets_info
961 my $facets = C4::Koha::getFacets();
962 my $facets_info = C4::Search::_get_facets_info( $facets );
963 my $expected_facets_info_unimarc = {
964 'au' => { 'expanded' => undef,
965 'label_value' => "Authors" },
966 'ccode' => { 'expanded' => undef,
967 'label_value' => "CollectionCodes" },
968 'holdingbranch' => { 'expanded' => undef,
969 'label_value' => "HoldingLibrary" },
970 'location' => { 'expanded' => undef,
971 'label_value' => "Location" },
972 'se' => { 'expanded' => undef,
973 'label_value' => "Series" },
974 'su-geo' => { 'expanded' => undef,
975 'label_value' => "Places" },
976 'su-to' => { 'expanded' => undef,
977 'label_value' => "Topics" },
978 'su-ut' => { 'expanded' => undef,
979 'label_value' => "Titles" }
981 delete $expected_facets_info_unimarc->{holdingbranch}
982 if Koha::Libraries->count == 1;
983 is_deeply( $facets_info, $expected_facets_info_unimarc,
984 "_get_facets_info returns the correct data");
986 cleanup();
989 subtest 'MARC21 + DOM' => sub {
990 plan tests => 112;
991 run_marc21_search_tests();
994 subtest 'UNIMARC + DOM' => sub {
995 plan tests => 14;
996 run_unimarc_search_tests();
999 # Make sure that following tests are not using our config settings
1000 Koha::Caches->get_instance('config')->flush_all;