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>.
22 use C4::AuthoritiesMarc qw( SearchAuthorities );
26 # work around spurious wide character warnings
27 use open ':std', ':encoding(utf8)';
29 use Test::More tests => 3;
41 use File::Temp qw/ tempdir /;
47 sub index_sample_records_and_launch_zebra {
48 my ($datadir, $marc_type) = @_;
50 my $sourcedir = dirname(__FILE__) . "/data";
51 unlink("$datadir/zebra.log");
52 if (-f "$sourcedir/${marc_type}/zebraexport/biblio/exported_records") {
53 my $zebra_bib_cfg = 'zebra-biblios-dom.cfg';
54 system("zebraidx -c $datadir/etc/koha/zebradb/$zebra_bib_cfg -v none,fatal -g iso2709 -d biblios init");
55 system("zebraidx -c $datadir/etc/koha/zebradb/$zebra_bib_cfg -v none,fatal -g iso2709 -d biblios update $sourcedir/${marc_type}/zebraexport/biblio");
56 system("zebraidx -c $datadir/etc/koha/zebradb/$zebra_bib_cfg -v none,fatal -g iso2709 -d biblios commit");
58 # ... and add large bib records, if present
59 if (-f "$sourcedir/${marc_type}/zebraexport/large_biblio/exported_records.xml") {
60 my $zebra_bib_cfg = 'zebra-biblios-dom.cfg';
61 system("zebraidx -c $datadir/etc/koha/zebradb/$zebra_bib_cfg -v none,fatal -g marcxml -d biblios update $sourcedir/${marc_type}/zebraexport/large_biblio");
62 system("zebraidx -c $datadir/etc/koha/zebradb/$zebra_bib_cfg -v none,fatal -g marcxml -d biblios commit");
64 if (-f "$sourcedir/${marc_type}/zebraexport/authority/exported_records") {
65 my $zebra_auth_cfg = 'zebra-authorities-dom.cfg';
66 system("zebraidx -c $datadir/etc/koha/zebradb/$zebra_auth_cfg -v none,fatal -g iso2709 -d authorities init");
67 system("zebraidx -c $datadir/etc/koha/zebradb/$zebra_auth_cfg -v none,fatal -g iso2709 -d authorities update $sourcedir/${marc_type}/zebraexport/authority");
68 system("zebraidx -c $datadir/etc/koha/zebradb/$zebra_auth_cfg -v none,fatal -g iso2709 -d authorities commit");
73 exec("zebrasrv -f $datadir/etc/koha-conf.xml -v none,request -l $datadir/zebra.log");
84 # Clean up the Zebra files since the child process was just shot
89 # Fall back to make sure that the Zebra process
90 # and files get cleaned up
95 sub matchesExplodedTerms {
96 my ($message, $query, @terms) = @_;
97 my $match = '(' . join ('|', map { " \@attr 1=Subject \@attr 4=1 \"$_\"" } @terms) . "){" . scalar(@terms) . "}";
98 like($query, qr/$match/, $message);
101 our $QueryStemming = 0;
102 our $QueryAutoTruncate = 0;
103 our $QueryWeightFields = 0;
105 our $SearchEngine = 'Zebra';
106 our $marcflavour = 'MARC21';
107 our $htdocs = File::Spec->rel2abs(dirname($0));
108 my @htdocs = split /\//, $htdocs;
109 $htdocs[-2] = 'koha-tmpl';
110 $htdocs[-1] = 'opac-tmpl';
111 $htdocs = join '/', @htdocs;
112 our $contextmodule = Test::MockModule->new('C4::Context');
113 $contextmodule->mock('preference', sub {
114 my ($self, $pref) = @_;
115 if ($pref eq 'marcflavour') {
117 } elsif ($pref eq 'QueryStemming') {
118 return $QueryStemming;
119 } elsif ($pref eq 'QueryAutoTruncate') {
120 return $QueryAutoTruncate;
121 } elsif ($pref eq 'QueryWeightFields') {
122 return $QueryWeightFields;
123 } elsif ($pref eq 'QueryFuzzy') {
125 } elsif ($pref eq 'SearchEngine') {
126 return $SearchEngine;
127 } elsif ($pref eq 'maxRecordsForFacets') {
129 } elsif ($pref eq 'FacetLabelTruncationLength') {
131 } elsif ($pref eq 'FacetMaxCount') {
133 } elsif ($pref eq 'OpacHiddenItems') {
135 } elsif ($pref eq 'opacthemes') {
137 } elsif ($pref eq 'OPACLanguages') {
139 } elsif ($pref eq 'AlternateHoldingsField') {
141 } elsif ($pref eq 'AuthoritySeparator') {
143 } elsif ($pref eq 'DisplayLibraryFacets') {
145 } elsif ($pref eq 'UNIMARCAuthorsFacetsSeparator') {
147 } elsif ($pref eq 'casAuthentication' or $pref eq 'casLogout' or $pref eq 'casServerUrl' ) {
149 } elsif ($pref eq 'template') {
151 } elsif ($pref eq 'OPACXSLTResultsDisplay') {
152 return C4::XSLT::_get_best_default_xslt_filename($htdocs, 'bootstrap','en',$marcflavour . 'slim2OPACResults.xsl');
153 } elsif ($pref eq 'BiblioDefaultView') {
155 } elsif ($pref eq 'IdRef') {
157 } elsif ($pref eq 'IntranetBiblioDefaultView') {
159 } elsif ($pref eq 'OPACBaseURL') {
160 return 'http://library.mydnsname.org';
161 } elsif ($pref eq 'OPACResultsLibrary') {
163 } elsif ($pref eq 'OpacSuppression') {
165 } elsif ($pref eq 'OPACURLOpenInNewWindow') {
167 } elsif ($pref eq 'TraceCompleteSubfields') {
169 } elsif ($pref eq 'TraceSubjectSubdivisions') {
171 } elsif ($pref eq 'TrackClicks') {
173 } elsif ($pref eq 'URLLinkText') {
175 } elsif ($pref eq 'UseAuthoritiesForTracings') {
177 } elsif ($pref eq 'UseControlNumber') {
179 } elsif ($pref eq 'UseICUStyleQuotes') {
181 } elsif ($pref eq 'viewISBD') {
183 } elsif ($pref eq 'EasyAnalyticalRecords') {
185 } elsif ($pref eq 'OpenURLResolverURL') {
187 } elsif ($pref eq 'OPACShowOpenURL') {
189 } elsif ($pref eq 'OpenURLText') {
191 } elsif ($pref eq 'OPACShowMusicalInscripts') {
193 } elsif ($pref eq 'OPACPlayMusicalInscripts') {
195 } elsif ($pref eq 'Reference_NFL_Statuses') {
198 warn "The syspref $pref was requested but I don't know what to say; this indicates that the test requires updating"
199 unless $pref =~ m/(XSLT|item|branch|holding|image)/i;
204 our $bibliomodule = Test::MockModule->new('C4::Biblio');
206 sub mock_GetMarcSubfieldStructure {
207 my $marc_type = shift;
208 if ($marc_type eq 'marc21') {
209 $bibliomodule->mock('GetMarcSubfieldStructure', sub {
211 'biblio.biblionumber' => [{ tagfield => '999', tagsubfield => 'c' }],
212 'biblio.isbn' => [{ tagfield => '020', tagsubfield => 'a' }],
213 'biblio.title' => [{ tagfield => '245', tagsubfield => 'a' }],
214 'biblio.author' => [{ tagfield => '100', tagsubfield => 'a' }],
215 'biblio.notes' => [{ tagfield => '500', tagsubfield => 'a' }],
216 'items.barcode' => [{ tagfield => '952', tagsubfield => 'p' }],
217 'items.booksellerid' => [{ tagfield => '952', tagsubfield => 'e' }],
218 'items.ccode' => [{ tagfield => '952', tagsubfield => '8' }],
219 'items.cn_sort' => [{ tagfield => '952', tagsubfield => '6' }],
220 'items.cn_source' => [{ tagfield => '952', tagsubfield => '2' }],
221 'items.coded_location_qualifier' => [{ tagfield => '952', tagsubfield => 'f' }],
222 'items.copynumber' => [{ tagfield => '952', tagsubfield => 't' }],
223 'items.damaged' => [{ tagfield => '952', tagsubfield => '4' }],
224 'items.dateaccessioned' => [{ tagfield => '952', tagsubfield => 'd' }],
225 'items.datelastborrowed' => [{ tagfield => '952', tagsubfield => 's' }],
226 'items.datelastseen' => [{ tagfield => '952', tagsubfield => 'r' }],
227 'items.enumchron' => [{ tagfield => '952', tagsubfield => 'h' }],
228 'items.holdingbranch' => [{ tagfield => '952', tagsubfield => 'b' }],
229 'items.homebranch' => [{ tagfield => '952', tagsubfield => 'a' }],
230 'items.issues' => [{ tagfield => '952', tagsubfield => 'l' }],
231 'items.itemcallnumber' => [{ tagfield => '952', tagsubfield => 'o' }],
232 'items.itemlost' => [{ tagfield => '952', tagsubfield => '1' }],
233 'items.itemnotes' => [{ tagfield => '952', tagsubfield => 'z' }],
234 'items.itemnumber' => [{ tagfield => '952', tagsubfield => '9' }],
235 'items.itype' => [{ tagfield => '952', tagsubfield => 'y' }],
236 'items.location' => [{ tagfield => '952', tagsubfield => 'c' }],
237 'items.materials' => [{ tagfield => '952', tagsubfield => '3' }],
238 'items.nonpublicnote' => [{ tagfield => '952', tagsubfield => 'x' }],
239 'items.notforloan' => [{ tagfield => '952', tagsubfield => '7' }],
240 'items.onloan' => [{ tagfield => '952', tagsubfield => 'q' }],
241 'items.price' => [{ tagfield => '952', tagsubfield => 'g' }],
242 'items.renewals' => [{ tagfield => '952', tagsubfield => 'm' }],
243 'items.replacementprice' => [{ tagfield => '952', tagsubfield => 'v' }],
244 'items.replacementpricedate' => [{ tagfield => '952', tagsubfield => 'w' }],
245 'items.reserves' => [{ tagfield => '952', tagsubfield => 'n' }],
246 'items.restricted' => [{ tagfield => '952', tagsubfield => '5' }],
247 'items.stack' => [{ tagfield => '952', tagsubfield => 'j' }],
248 'items.uri' => [{ tagfield => '952', tagsubfield => 'u' }],
249 'items.withdrawn' => [{ tagfield => '952', tagsubfield => '0' }],
255 sub run_marc21_search_tests {
256 $datadir = tempdir();
257 system(dirname(__FILE__) . "/zebra_config.pl $datadir marc21");
259 Koha::Caches->get_instance('config')->flush_all;
261 mock_GetMarcSubfieldStructure('marc21');
262 my $context = C4::Context->new("$datadir/etc/koha-conf.xml");
263 $context->set_context();
265 use_ok('C4::Search', qw( getIndexes FindDuplicate SimpleSearch getRecords buildQuery searchResults ));
267 # set search syspreferences to a known starting point
269 $QueryAutoTruncate = 0;
270 $QueryWeightFields = 0;
272 $marcflavour = 'MARC21';
274 my $indexes = C4::Search::getIndexes();
275 is(scalar(grep(/^ti$/, @$indexes)), 1, "Title index supported");
276 is(scalar(grep(/^arl$/, @$indexes)), 1, "Accelerated reading level index supported");
277 is(scalar(grep(/^arp$/, @$indexes)), 1, "Accelerated reading point index supported");
279 my $bibliomodule = Test::MockModule->new('C4::Biblio');
282 'CPL' => { 'branchaddress1' => 'Jefferson Summit', 'branchcode' => 'CPL', 'branchname' => 'Centerville', },
283 'FFL' => { 'branchaddress1' => 'River Station', 'branchcode' => 'FFL', 'branchname' => 'Fairfield', },
284 'FPL' => { 'branchaddress1' => 'Hickory Squere', 'branchcode' => 'FPL', 'branchname' => 'Fairview', },
285 'FRL' => { 'branchaddress1' => 'Smith Heights', 'branchcode' => 'FRL', 'branchname' => 'Franklin', },
286 'IPT' => { 'branchaddress1' => '', 'branchcode' => 'IPT', 'branchname' => "Institut Protestant de Théologie", },
287 'LPL' => { 'branchaddress1' => 'East Hills', 'branchcode' => 'LPL', 'branchname' => 'Liberty', },
288 'MPL' => { 'branchaddress1' => '372 Forest Street', 'branchcode' => 'MPL', 'branchname' => 'Midway', },
289 'PVL' => { 'branchaddress1' => 'Meadow Grove', 'branchcode' => 'PVL', 'branchname' => 'Pleasant Valley', },
290 'RPL' => { 'branchaddress1' => 'Johnson Terrace', 'branchcode' => 'RPL', 'branchname' => 'Riverside', },
291 'SPL' => { 'branchaddress1' => 'Highland Boulevard', 'branchcode' => 'SPL', 'branchname' => 'Springfield', },
292 'S' => { 'branchaddress1' => '', 'branchcode' => 'S', 'branchname' => 'Test', },
293 'TPL' => { 'branchaddress1' => 'Valley Way', 'branchcode' => 'TPL', 'branchname' => 'Troy', },
294 'UPL' => { 'branchaddress1' => 'Chestnut Hollow', 'branchcode' => 'UPL', 'branchname' => 'Union', },
297 'BK' => { 'imageurl' => 'bridge/book.png', 'summary' => '', 'itemtype' => 'BK', 'description' => 'Books' },
298 'CF' => { 'imageurl' => 'bridge/computer_file.png', 'summary' => '', 'itemtype' => 'CF', 'description' => 'Computer Files' },
299 'CR' => { 'imageurl' => 'bridge/periodical.png', 'summary' => '', 'itemtype' => 'CR', 'description' => 'Continuing Resources' },
300 'MP' => { 'imageurl' => 'bridge/map.png', 'summary' => '', 'itemtype' => 'MP', 'description' => 'Maps' },
301 'MU' => { 'imageurl' => 'bridge/sound.png', 'summary' => '', 'itemtype' => 'MU', 'description' => 'Music' },
302 'MX' => { 'imageurl' => 'bridge/kit.png', 'summary' => '', 'itemtype' => 'MX', 'description' => 'Mixed Materials' },
303 'REF' => { 'imageurl' => '', 'summary' => '', 'itemtype' => 'REF', 'description' => 'Reference' },
304 'VM' => { 'imageurl' => 'bridge/dvd.png', 'summary' => '', 'itemtype' => 'VM', 'description' => 'Visual Materials' },
307 index_sample_records_and_launch_zebra($datadir, 'marc21');
309 my ($biblionumber, $title);
310 my $record = MARC::Record->new;
313 [ '020', ' ', ' ', a => '9788522421718' ],
314 [ '245', '0', '0', a => 'Administração da produção /' ]
316 ($biblionumber,undef,$title) = FindDuplicate($record);
317 is($biblionumber, 51, 'Found duplicate with ISBN');
319 $record = MARC::Record->new;
322 [ '100', '1', ' ', a => 'Carter, Philip J.' ],
323 [ '245', '1', '4', a => 'Test your emotional intelligence :' ]
325 ($biblionumber,undef,$title) = FindDuplicate($record);
326 is($biblionumber, 203, 'Found duplicate with author/title');
328 # Testing SimpleSearch
330 my ( $error, $marcresults, $total_hits ) = SimpleSearch("book", 0, 9);
332 is(scalar @$marcresults, 9, "SimpleSearch retrieved requested number of records");
333 is($total_hits, 101, "SimpleSearch for 'book' matched right number of records");
334 is($error, undef, "SimpleSearch does not return an error when successful");
337 ( $error, $marcresults2, $total_hits ) = SimpleSearch("book", 5, 5);
338 is($marcresults->[5], $marcresults2->[0], "SimpleSearch cursor functions");
340 ( $error, $marcresults, $total_hits ) = SimpleSearch("kw=book", 0, 10);
341 is($total_hits, 101, "SimpleSearch handles simple CCL");
343 ( $error, $marcresults, $total_hits ) = SimpleSearch("Music-number=49631-2", 0, 10);
344 is($total_hits, 1, "SimpleSearch on music publisher number works (bug 8252)");
345 ( $error, $marcresults, $total_hits ) = SimpleSearch("Identifier-publisher-for-music=49631-2", 0, 10);
346 is($total_hits, 1, "SimpleSearch on music publisher number works using Identifier-publisher-for-music (bug 8252)");
352 ( undef, $results_hashref, $facets_loop ) =
353 getRecords('kw:book', 'book', [], [ 'biblioserver' ], '19', 0, \%branches, \%itemtypes, 'ccl', undef);
354 is($results_hashref->{biblioserver}->{hits}, 101, "getRecords keyword search for 'book' matched right number of records");
355 is(scalar @{$results_hashref->{biblioserver}->{RECORDS}}, 19, "getRecords returned requested number of records");
356 my $record5 = $results_hashref->{biblioserver}->{RECORDS}->[5];
357 ( undef, $results_hashref, $facets_loop ) =
358 getRecords('kw:book', 'book', [], [ 'biblioserver' ], '20', 5, \%branches, \%itemtypes, 'ccl', undef);
359 ok(!defined $results_hashref->{biblioserver}->{RECORDS}->[0] &&
360 !defined $results_hashref->{biblioserver}->{RECORDS}->[1] &&
361 !defined $results_hashref->{biblioserver}->{RECORDS}->[2] &&
362 !defined $results_hashref->{biblioserver}->{RECORDS}->[3] &&
363 !defined $results_hashref->{biblioserver}->{RECORDS}->[4] &&
364 $results_hashref->{biblioserver}->{RECORDS}->[5] eq $record5, "getRecords cursor works");
366 ( undef, $results_hashref, $facets_loop ) =
367 getRecords('ti:book', 'ti:book', [], [ 'biblioserver' ], '20', 0, \%branches, \%itemtypes, 'ccl', undef);
368 is($results_hashref->{biblioserver}->{hits}, 11, "getRecords title search for 'book' matched right number of records");
370 ( undef, $results_hashref, $facets_loop ) =
371 getRecords('au:Lessig', 'au:Lessig', [], [ 'biblioserver' ], '20', 0, \%branches, \%itemtypes, 'ccl', undef);
372 is($results_hashref->{biblioserver}->{hits}, 4, "getRecords title search for 'Australia' matched right number of records");
374 ( undef, $results_hashref, $facets_loop ) =
375 getRecords('salud', 'salud', [], [ 'biblioserver' ], '19', 0, \%branches, \%itemtypes, 'ccl', undef);
376 ok(MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[0],'UTF-8')->title_proper() =~ m/^Efectos del ambiente/ &&
377 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' &&
378 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[18],'UTF-8')->title_proper() =~ m/^Indicadores de resultados identificados/
379 , "Simple relevance sorting in getRecords matches old behavior");
381 ( undef, $results_hashref, $facets_loop ) =
382 getRecords('salud', 'salud', [ 'author_az' ], [ 'biblioserver' ], '38', 0, \%branches, \%itemtypes, 'ccl', undef);
383 ok(MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[0],'UTF-8')->title_proper() =~ m/la enfermedad laboral\^ies$/ &&
384 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[6],'UTF-8')->title_proper() =~ m/^Indicadores de resultados identificados/ &&
385 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[18],'UTF-8')->title_proper() eq 'World health statistics 2009^ien'
386 , "Simple ascending author sorting in getRecords matches old behavior");
388 ( undef, $results_hashref, $facets_loop ) =
389 getRecords('salud', 'salud', [ 'author_za' ], [ 'biblioserver' ], '38', 0, \%branches, \%itemtypes, 'ccl', undef);
390 ok(MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[0],'UTF-8')->title_proper() eq 'World health statistics 2009^ien' &&
391 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[12],'UTF-8')->title_proper() =~ m/^Indicadores de resultados identificados/ &&
392 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[18],'UTF-8')->title_proper() =~ m/la enfermedad laboral\^ies$/
393 , "Simple descending author sorting in getRecords matches old behavior");
395 ( undef, $results_hashref, $facets_loop ) =
396 getRecords('salud', 'salud', [ 'pubdate_asc' ], [ 'biblioserver' ], '38', 0, \%branches, \%itemtypes, 'ccl', undef);
397 ok(MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[0],'UTF-8')->title_proper() eq 'Manual de higiene industrial^ies' &&
398 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[7],'UTF-8')->title_proper() =~ m/seguridad e higiene del trabajo\^ies$/ &&
399 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[18],'UTF-8')->title_proper() =~ m/^Indicadores de resultados identificados/
400 , "Simple ascending publication date sorting in getRecords matches old behavior");
402 ( undef, $results_hashref, $facets_loop ) =
403 getRecords('salud', 'salud', [ 'pubdate_dsc' ], [ 'biblioserver' ], '38', 0, \%branches, \%itemtypes, 'ccl', undef);
404 ok(MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[0],'UTF-8')->title_proper() =~ m/^Estado de salud/ &&
405 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[7],'UTF-8')->title_proper() eq 'World health statistics 2009^ien' &&
406 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[18],'UTF-8')->title_proper() eq 'Manual de higiene industrial^ies'
407 , "Simple descending publication date sorting in getRecords matches old behavior");
409 ( undef, $results_hashref, $facets_loop ) =
410 getRecords('books', 'books', [ 'relevance' ], [ 'biblioserver' ], '20', 0, \%branches, \%itemtypes, undef, 1);
411 $record = MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[0]);
412 is($record->title_proper(), 'Books', "Scan returned requested item");
413 is($record->subfield('100', 'a'), 2, "Scan returned correct number of records matching term");
414 # Time to test buildQuery and searchResults too.
416 my ( $query, $simple_query, $query_cgi,
417 $query_desc, $limit, $limit_cgi, $limit_desc,
419 ( $error, $query, $simple_query, $query_cgi,
420 $query_desc, $limit, $limit_cgi, $limit_desc,
421 $query_type ) = buildQuery([], [ 'salud' ], [], [], [], 0, 'en');
422 like($query, qr/kw\W.*salud/, "Built CCL keyword query");
424 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
425 is($results_hashref->{biblioserver}->{hits}, 19, "getRecords generated keyword search for 'salud' matched right number of records");
427 my @newresults = searchResults({'interface' => 'opac'}, $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 18, 0, 0,
428 $results_hashref->{'biblioserver'}->{"RECORDS"});
429 is(scalar @newresults,18, "searchResults returns requested number of hits");
431 ( $error, $query, $simple_query, $query_cgi,
432 $query_desc, $limit, $limit_cgi, $limit_desc,
433 $query_type ) = buildQuery([ 'and' ], [ 'salud', 'higiene' ], [], [], [], 0, 'en');
434 like($query, qr/kw\W.*salud\W.*and.*kw\W.*higiene/, "Built composed explicit-and CCL keyword query");
436 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
437 is($results_hashref->{biblioserver}->{hits}, 3, "getRecords generated composed keyword search for 'salud' explicit-and 'higiene' matched right number of records");
439 ( $error, $query, $simple_query, $query_cgi,
440 $query_desc, $limit, $limit_cgi, $limit_desc,
441 $query_type ) = buildQuery([ 'or' ], [ 'salud', 'higiene' ], [], [], [], 0, 'en');
442 like($query, qr/kw\W.*salud\W.*or.*kw\W.*higiene/, "Built composed explicit-or CCL keyword query");
444 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
445 is($results_hashref->{biblioserver}->{hits}, 20, "getRecords generated composed keyword search for 'salud' explicit-or 'higiene' matched right number of records");
447 ( $error, $query, $simple_query, $query_cgi,
448 $query_desc, $limit, $limit_cgi, $limit_desc,
449 $query_type ) = buildQuery([], [ 'salud', 'higiene' ], [], [], [], 0, 'en');
450 like($query, qr/kw\W.*salud\W.*and.*kw\W.*higiene/, "Built composed implicit-and CCL keyword query");
452 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
453 is($results_hashref->{biblioserver}->{hits}, 3, "getRecords generated composed keyword search for 'salud' implicit-and 'higiene' matched right number of records");
455 ( $error, $query, $simple_query, $query_cgi,
456 $query_desc, $limit, $limit_cgi, $limit_desc,
457 $query_type ) = buildQuery([], [ 'salud' ], [ 'kw' ], [ 'su-to:Laboratorios' ], [], 0, 'en');
458 like($query, qr/kw\W.*salud\W*and\W*su-to\W.*Laboratorios/, "Faceted query generated correctly");
459 unlike($query_desc, qr/Laboratorios/, "Facets not included in query description");
461 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
462 is($results_hashref->{biblioserver}->{hits}, 2, "getRecords generated faceted search matched right number of records");
465 ( $error, $query, $simple_query, $query_cgi,
466 $query_desc, $limit, $limit_cgi, $limit_desc,
467 $query_type ) = buildQuery([], [ '' ], [ 'kw' ], [ 'mc-itype:MP', 'mc-itype:MU' ], [], 0, 'en');
469 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
470 is($results_hashref->{biblioserver}->{hits}, 2, "getRecords generated mc-faceted search matched right number of records");
473 ( $error, $query, $simple_query, $query_cgi,
474 $query_desc, $limit, $limit_cgi, $limit_desc,
475 $query_type ) = buildQuery([], [ '' ], [ 'kw' ], [ 'mc-loc:GEN', 'branch:FFL' ], [], 0, 'en');
477 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
478 is($results_hashref->{biblioserver}->{hits}, 2, "getRecords generated multi-faceted search matched right number of records");
480 ( $error, $query, $simple_query, $query_cgi,
481 $query_desc, $limit, $limit_cgi, $limit_desc,
482 $query_type ) = buildQuery([], [ 'NEKLS' ], [ 'Code-institution' ], [], [], 0, 'en');
483 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
484 is($results_hashref->{biblioserver}->{hits}, 12,
485 'search using index whose name contains "ns" returns expected results (bug 10271)');
487 ( $error, $query, $simple_query, $query_cgi,
488 $query_desc, $limit, $limit_cgi, $limit_desc,
489 $query_type ) = buildQuery([], [ '' ], [ 'kw' ], [ 'available' ], [], 0, 'en');
491 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
492 is($results_hashref->{biblioserver}->{hits}, 2, "getRecords generated availability-limited search matched right number of records");
494 @newresults = searchResults({'interface'=>'opac'}, $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 17, 0, 0,
495 $results_hashref->{'biblioserver'}->{"RECORDS"});
496 my $allavailable = 'true';
497 foreach my $result (@newresults) {
498 $allavailable = 'false' unless $result->{availablecount} > 0;
500 is ($allavailable, 'true', 'All records have at least one item available');
502 my $mocked_xslt = Test::MockModule->new('Koha::XSLT::Base');
503 $mocked_xslt->mock( 'transform', sub {
504 my ($self, $xml) = @_;
508 @newresults = searchResults({'interface'=>'opac'}, $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 17, 0, 0,
509 $results_hashref->{'biblioserver'}->{"RECORDS"}, { anonymous_session => 1 });
511 like( $newresults[0]->{XSLTResultsRecord}, qr/<variable name="anonymous_session">1<\/variable>/, "Variable injected correctly" );
513 my $biblio_id = $newresults[0]->{biblionumber};
514 my $fw = C4::Biblio::GetFrameworkCode($biblio_id);
516 my $dbh = C4::Context->dbh;
517 # Hide subfield 'p' in OPAC
519 UPDATE marc_subfield_structure
521 WHERE frameworkcode='$fw' AND
526 # Hide subfield 'y' in Staff
528 UPDATE marc_subfield_structure
530 WHERE frameworkcode='$fw' AND
535 Koha::Caches->get_instance->flush_all;
537 @newresults = searchResults(
538 { 'interface' => 'opac' },
540 $results_hashref->{'biblioserver'}->{'hits'},
544 $results_hashref->{'biblioserver'}->{"RECORDS"}
547 unlike( $newresults[0]->{XSLTResultsRecord}, qr/<subfield code="p">TEST11111<\/subfield>/, '952\$p hidden in OPAC' );
549 @newresults = searchResults(
550 { 'interface' => 'intranet' },
552 $results_hashref->{'biblioserver'}->{'hits'},
556 $results_hashref->{'biblioserver'}->{"RECORDS"}
559 unlike( $newresults[0]->{XSLTResultsRecord}, qr/<subfield code="y">Books<\/subfield>/, '952\$y hidden on staff interface' );
561 ( $error, $query, $simple_query, $query_cgi,
562 $query_desc, $limit, $limit_cgi, $limit_desc,
563 $query_type ) = buildQuery([], [ 'pqf=@attr 1=_ALLRECORDS @attr 2=103 ""' ], [], [], [], 0, 'en');
565 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
566 is($results_hashref->{biblioserver}->{hits}, 180, "getRecords on _ALLRECORDS PQF returned all records");
568 ( $error, $query, $simple_query, $query_cgi,
569 $query_desc, $limit, $limit_cgi, $limit_desc,
570 $query_type ) = buildQuery([], [ 'pqf=@attr 1=1016 "Lessig"' ], [], [], [], 0, 'en');
572 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
573 is($results_hashref->{biblioserver}->{hits}, 4, "getRecords PQF author search for Lessig returned proper number of matches");
575 ( $error, $query, $simple_query, $query_cgi,
576 $query_desc, $limit, $limit_cgi, $limit_desc,
577 $query_type ) = buildQuery([], [ 'ccl=au:Lessig' ], [], [], [], 0, 'en');
579 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
580 is($results_hashref->{biblioserver}->{hits}, 4, "getRecords CCL author search for Lessig returned proper number of matches");
582 ( $error, $query, $simple_query, $query_cgi,
583 $query_desc, $limit, $limit_cgi, $limit_desc,
584 $query_type ) = buildQuery([], [ 'cql=dc.author any lessig' ], [], [], [], 0, 'en');
586 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
587 is($results_hashref->{biblioserver}->{hits}, 4, "getRecords CQL author search for Lessig returned proper number of matches");
589 $QueryStemming = $QueryAutoTruncate = $QueryFuzzy = 0;
590 $QueryWeightFields = 1;
591 ( $error, $query, $simple_query, $query_cgi,
592 $query_desc, $limit, $limit_cgi, $limit_desc,
593 $query_type ) = buildQuery([], [ 'salud' ], [ 'kw' ], [], [], 0, 'en');
595 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
596 is($results_hashref->{biblioserver}->{hits}, 19, "Weighted query returned correct number of results");
597 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");
599 $QueryStemming = $QueryWeightFields = $QueryFuzzy = 0;
600 $QueryAutoTruncate = 1;
601 ( $error, $query, $simple_query, $query_cgi,
602 $query_desc, $limit, $limit_cgi, $limit_desc,
603 $query_type ) = buildQuery([], [ 'medic' ], [ 'kw' ], [], [], 0, 'en');
605 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
606 is($results_hashref->{biblioserver}->{hits}, 5, "Search for 'medic' returns matches with automatic truncation on");
608 ( $error, $query, $simple_query, $query_cgi,
609 $query_desc, $limit, $limit_cgi, $limit_desc,
610 $query_type ) = buildQuery([], [ 'medic*' ], [ 'kw' ], [], [], 0, 'en');
612 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
613 is($results_hashref->{biblioserver}->{hits}, 5, "Search for 'medic*' returns matches with automatic truncation on");
615 $QueryStemming = $QueryFuzzy = $QueryAutoTruncate = 0;
616 $QueryWeightFields = 1;
617 ( $error, $query, $simple_query, $query_cgi,
618 $query_desc, $limit, $limit_cgi, $limit_desc,
619 $query_type ) = buildQuery([], [ 'web application' ], [ 'kw' ], [], [], 0, 'en');
620 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
621 is($results_hashref->{biblioserver}->{hits}, 1, "Search for 'web application' returns one hit with QueryWeightFields on");
623 ( $error, $query, $simple_query, $query_cgi,
624 $query_desc, $limit, $limit_cgi, $limit_desc,
625 $query_type ) = buildQuery([], [ 'web "application' ], [ 'kw' ], [], [], 0, 'en');
626 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
627 is($results_hashref->{biblioserver}->{hits}, 1, "Search for 'web \"application' returns one hit with QueryWeightFields on (bug 7518)");
629 $QueryStemming = $QueryWeightFields = $QueryFuzzy = $QueryAutoTruncate = 0;
630 ( $error, $query, $simple_query, $query_cgi,
631 $query_desc, $limit, $limit_cgi, $limit_desc,
632 $query_type ) = buildQuery([], [ 'medic' ], [ 'kw' ], [], [], 0, 'en');
634 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
635 is($results_hashref->{biblioserver}->{hits}, undef, "Search for 'medic' returns no matches with automatic truncation off");
637 ( $error, $query, $simple_query, $query_cgi,
638 $query_desc, $limit, $limit_cgi, $limit_desc,
639 $query_type ) = buildQuery([], [ 'medic*' ], [ 'kw' ], [], [], 0, 'en');
641 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
642 is($results_hashref->{biblioserver}->{hits}, 5, "Search for 'medic*' returns matches with automatic truncation off");
644 $QueryStemming = $QueryWeightFields = 1;
645 $QueryFuzzy = $QueryAutoTruncate = 0;
646 ( $error, $query, $simple_query, $query_cgi,
647 $query_desc, $limit, $limit_cgi, $limit_desc,
648 $query_type ) = buildQuery([], [ 'pressed' ], [ 'kw' ], [], [], 0, 'en');
650 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
651 is($results_hashref->{biblioserver}->{hits}, 7, "Search for 'pressed' returns matches when stemming (and query weighting) is on");
653 $QueryStemming = $QueryWeightFields = $QueryFuzzy = $QueryAutoTruncate = 0;
654 ( $error, $query, $simple_query, $query_cgi,
655 $query_desc, $limit, $limit_cgi, $limit_desc,
656 $query_type ) = buildQuery([], [ 'pressed' ], [ 'kw' ], [], [], 0, 'en');
658 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
659 is($results_hashref->{biblioserver}->{hits}, undef, "Search for 'pressed' returns no matches when stemming is off");
661 ( $error, $query, $simple_query, $query_cgi,
662 $query_desc, $limit, $limit_cgi, $limit_desc,
663 $query_type ) = buildQuery([], [ 'ccl=an:42' ], [], ['available'], [], 0, 'en');
664 is( $query, "an:42 and ( (allrecords,AlwaysMatches='') and (not-onloan-count,st-numeric >= 1) and (lost,st-numeric=0) )", 'buildQuery should add the available part to the query if requested with ccl' );
665 is( $query_desc, 'an:42', 'buildQuery should remove the available part from the query' );
667 ( $error, $query, $simple_query, $query_cgi,
668 $query_desc, $limit, $limit_cgi, $limit_desc,
669 $query_type ) = buildQuery([], [ 0 ], [ 'su,phr' ], [], [], 0, 'en');
670 is($query, 'su,phr=(rk=(0)) ', 'buildQuery should keep 0 value');
673 ( $error, $query, $simple_query, $query_cgi,
674 $query_desc, $limit, $limit_cgi, $limit_desc,
675 $query_type ) = buildQuery([], [], [], [ 'mc-ccode:NF(IC'], [], 0, 'en');
676 like($query, qr/ccode="NF\(IC"/, "Limit quoted correctly");
678 # Let's see what happens when we pass bad data into these routines.
679 # We have to catch warnings since we're not very good about returning errors.
681 warning_like { ( $error, $marcresults, $total_hits ) = SimpleSearch("@==ccl blah", 0, 9) } qr/CCL parsing error/,
682 "SimpleSearch warns about CCL parsing error with nonsense query";
683 isnt($error, undef, "SimpleSearch returns an error when passed gibberish");
685 warning_like {( undef, $results_hashref, $facets_loop ) =
686 getRecords('kw:book', 'book', [], [ 'biblioserver' ], '19', 0, \%branches, \%itemtypes, 'nonsense', undef) }
687 qr/Unknown query_type/, "getRecords warns about unknown query type";
689 warning_like {( undef, $results_hashref, $facets_loop ) =
690 getRecords('pqf=@attr 1=4 "title"', 'pqf=@attr 1=4 "title"', [], [ 'biblioserver' ], '19', 0, \%branches, \%itemtypes, '', undef) }
691 qr/WARNING: query problem/, "getRecords warns when query type is not specified for non-CCL query";
693 # Let's just test a few other bits and bobs, just for fun
695 ($error, $results_hashref, $facets_loop) = getRecords("Godzina pąsowej róży","Godzina pąsowej róży",[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
696 @newresults = searchResults({'interface'=>'intranet'}, $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 17, 0, 0,
697 $results_hashref->{'biblioserver'}->{"RECORDS"});
698 is($newresults[0]->{'alternateholdings_count'}, 1, 'Alternate holdings filled in correctly');
701 ## Regression test for Bug 10741
703 # make one of the test items appear to be in transit
704 my $circ_module = Test::MockModule->new('C4::Circulation');
705 $circ_module->mock('GetTransfers', sub {
706 my $itemnumber = shift // -1;
707 if ($itemnumber == 11) {
708 return ('2013-07-19', 'MPL', 'CPL');
714 ($error, $results_hashref, $facets_loop) = getRecords("TEST12121212","TEST12121212",[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
715 @newresults = searchResults({'interface'=>'intranet'}, $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 17, 0, 0,
716 $results_hashref->{'biblioserver'}->{"RECORDS"});
717 ok(!exists($newresults[0]->{norequests}), 'presence of a transit does not block hold request action (bug 10741)');
719 ## Regression test for bug 10684
720 ( undef, $results_hashref, $facets_loop ) =
721 getRecords('ti:punctuation', 'punctuation', [], [ 'biblioserver' ], '19', 0, \%branches, \%itemtypes, 'ccl', undef);
722 is($results_hashref->{biblioserver}->{hits}, 1, "search for ti:punctuation returned expected number of records");
723 warning_like { @newresults = searchResults({'intranet' => 'intranet'}, $query_desc,
724 $results_hashref->{'biblioserver'}->{'hits'}, 20, 0, 0,
725 $results_hashref->{'biblioserver'}->{"RECORDS"}) }
726 qr/^ERROR DECODING RECORD - Tag "50%" is not a valid tag/,
727 "Warning is raised correctly for invalid tags in MARC::Record";
728 is(scalar(@newresults), 0, 'a record that cannot be parsed by MARC::Record is simply skipped (bug 10684)');
730 my ($auths, $count) = SearchAuthorities(
731 ['mainentry'], ['and'], [''], ['starts'],
732 ['shakespeare'], 0, 10, '', '', 1
734 is($count, 1, 'MARC21 authorities: one hit on mainentry starts with "shakespeare"');
735 ($auths, $count) = SearchAuthorities(
736 ['mainentry'], ['and'], [''], ['starts'],
737 ['shakespeare'], 0, 10, '', 'HeadingAsc', 1
739 is($count, 1, 'MARC21 authorities: one hit on mainentry starts with "shakespeare" sorted by heading ascending');
740 ($auths, $count) = SearchAuthorities(
741 ['mainentry'], ['and'], [''], ['starts'],
742 ['shakespeare'], 0, 10, '', 'HeadingDsc', 1
744 is($count, 1, 'MARC21 authorities: one hit on mainentry starts with "shakespeare" sorted by heading descending');
745 ($auths, $count) = SearchAuthorities(
746 ['match'], ['and'], [''], ['contains'],
747 ['沙士北亞威廉姆'], 0, 10, '', '', 1
749 is($count, 1, 'MARC21 authorities: one hit on match contains "沙士北亞威廉姆"');
750 ($auths, $count) = SearchAuthorities(
751 ['LC-card-number'], ['and'], [''], ['contains'],
752 ['99282477'], 0, 10, '', '', 1
754 is($count, 1, 'MARC21 authorities: one hit on LC-card-number contains "99282477"');
755 ($auths, $count) = SearchAuthorities(
756 ['all'], ['and'], [''], ['contains'],
757 ['professional wrestler'], 0, 10, '', '', 1
759 is($count, 1, 'MARC21 authorities: one hit on "all" (entire record) contains "professional wrestler"');
761 # retrieve records that are larger than the MARC limit of 99,999 octets
762 ( undef, $results_hashref, $facets_loop ) =
763 getRecords('ti:marc the large record', '', [], [ 'biblioserver' ], '20', 0, \%branches, \%itemtypes, 'ccl', undef);
764 is($results_hashref->{biblioserver}->{hits}, 1, "Can do a search that retrieves an over-large bib record (bug 11096)");
765 @newresults = searchResults({'interface' =>'opac'}, $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 10, 0, 0,
766 $results_hashref->{'biblioserver'}->{"RECORDS"});
767 is($newresults[0]->{title}, 'Marc the Large Record', 'Able to render the title for over-large bib record (bug 11096)');
768 is($newresults[0]->{biblionumber}, '300', 'Over-large bib record has the correct biblionumber (bug 11096)');
769 like($newresults[0]->{notes}, qr/This is large note #550/, 'Able to render the notes field for over-large bib record (bug 11096)');
771 # notforloancount should be returned as part of searchResults output
772 ok( defined $newresults[0]->{notforloancount},
773 '\'notforloancount\' defined in searchResults output (Bug 12419)');
774 is( $newresults[0]->{notforloancount}, 2,
775 '\'notforloancount\' == 2 (Bug 12419)');
777 # verify that we don't attempt to sort if no results were returned
778 # because of a query error
779 warning_like {( undef, $results_hashref, $facets_loop ) =
780 getRecords('ccl=( AND )', '', ['title_az'], [ 'biblioserver' ], '20', 0, \%branches, \%itemtypes, 'ccl', undef)
781 } qr/WARNING: query problem with/, 'got warning instead of crash when attempting to run invalid query (bug 9578)';
783 # Test facet calculation
784 my $facets_counter = {};
785 my $facets = C4::Koha::getFacets();
786 # Create a record with a 100$z field
787 my $marc_record = MARC::Record->new;
788 $marc_record->add_fields(
790 [ '100', ' ', ' ', a => 'Cohen Arazi, Tomas' ],
791 [ '100', 'z', ' ', a => 'Tomasito' ],
792 [ '245', ' ', ' ', a => 'First try' ]
794 C4::Search::_get_facets_data_from_record( $marc_record, $facets, $facets_counter );
795 is_deeply( { au => { 'Cohen Arazi, Tomas' => 1 } }, $facets_counter,
796 "_get_facets_data_from_record doesn't count 100\$z (Bug 12788)");
797 $marc_record = MARC::Record->new;
798 $marc_record->add_fields(
800 [ '100', ' ', ' ', a => 'Cohen Arazi, Tomas' ],
801 [ '100', 'z', ' ', a => 'Tomasito' ],
802 [ '245', ' ', ' ', a => 'Second try' ]
804 C4::Search::_get_facets_data_from_record( $marc_record, $facets, $facets_counter );
805 is_deeply( { au => { 'Cohen Arazi, Tomas' => 2 } }, $facets_counter,
806 "_get_facets_data_from_record correctly counts author facet twice");
808 # Test _get_facets_info
809 my $facets_info = C4::Search::_get_facets_info( $facets );
810 my $expected_facets_info_marc21 = {
811 'au' => { 'label_value' => "Authors" },
812 'ccode' => { 'label_value' => "CollectionCodes" },
813 'holdingbranch' => { 'label_value' => "HoldingLibrary" },
814 'itype' => { 'label_value' => "ItemTypes" },
815 'location' => { 'label_value' => "Location" },
816 'se' => { 'label_value' => "Series" },
817 'su-geo' => { 'label_value' => "Places" },
818 'su-to' => { 'label_value' => "Topics" },
819 'su-ut' => { 'label_value' => "Titles" }
821 delete $expected_facets_info_marc21->{holdingbranch}
822 if Koha::Libraries->count == 1;
823 is_deeply( $facets_info, $expected_facets_info_marc21,
824 "_get_facets_info returns the correct data");
829 sub run_unimarc_search_tests {
830 $datadir = tempdir();
831 system(dirname(__FILE__) . "/zebra_config.pl $datadir unimarc");
833 Koha::Caches->get_instance('config')->flush_all;
835 mock_GetMarcSubfieldStructure('unimarc');
836 my $context = C4::Context->new("$datadir/etc/koha-conf.xml");
837 $context->set_context();
839 use_ok('C4::Search', qw( getIndexes FindDuplicate SimpleSearch getRecords buildQuery searchResults ));
841 # set search syspreferences to a known starting point
843 $QueryAutoTruncate = 0;
844 $QueryWeightFields = 0;
846 $marcflavour = 'UNIMARC';
848 index_sample_records_and_launch_zebra($datadir, 'unimarc');
850 my ( $error, $marcresults, $total_hits ) = SimpleSearch("ti=Järnvägarnas efterfrågan och den svenska industrin", 0, 10);
851 is($total_hits, 1, 'UNIMARC title search');
852 ( $error, $marcresults, $total_hits ) = SimpleSearch("ta=u", 0, 10);
853 is($total_hits, 1, 'UNIMARC target audience = u');
854 ( $error, $marcresults, $total_hits ) = SimpleSearch("ta=k", 0, 10);
855 is($total_hits, 4, 'UNIMARC target audience = k');
856 ( $error, $marcresults, $total_hits ) = SimpleSearch("ta=m", 0, 10);
857 is($total_hits, 3, 'UNIMARC target audience = m');
858 ( $error, $marcresults, $total_hits ) = SimpleSearch("item=EXCLU DU PRET", 0, 10);
859 is($total_hits, 1, 'UNIMARC generic item index (bug 10037)');
862 use_ok('C4::AuthoritiesMarc', qw( SearchAuthorities ));
864 my ($auths, $count) = SearchAuthorities(
865 ['mainentry'], ['and'], [''], ['contains'],
866 ['wil'], 0, 10, '', '', 1
868 is($count, 11, 'UNIMARC authorities: hits on mainentry contains "wil"');
869 ($auths, $count) = SearchAuthorities(
870 ['match'], ['and'], [''], ['contains'],
871 ['wil'], 0, 10, '', '', 1
873 is($count, 11, 'UNIMARC authorities: hits on match contains "wil"');
874 ($auths, $count) = SearchAuthorities(
875 ['mainentry'], ['and'], [''], ['contains'],
876 ['michel'], 0, 20, '', '', 1
878 is($count, 14, 'UNIMARC authorities: hits on mainentry contains "michel"');
879 ($auths, $count) = SearchAuthorities(
880 ['mainmainentry'], ['and'], [''], ['exact'],
881 ['valley'], 0, 20, '', '', 1
883 is($count, 1, 'UNIMARC authorities: hits on mainmainentry = "valley"');
884 ($auths, $count) = SearchAuthorities(
885 ['mainmainentry'], ['and'], [''], ['exact'],
886 ['vall'], 0, 20, '', '', 1
888 is($count, 0, 'UNIMARC authorities: no hits on mainmainentry = "vall"');
889 ($auths, $count) = SearchAuthorities(
890 ['Any'], ['and'], [''], ['starts'],
891 ['jean'], 0, 30, '', '', 1
893 is($count, 24, 'UNIMARC authorities: hits on any starts with "jean"');
895 # Test _get_facets_info
896 my $facets = C4::Koha::getFacets();
897 my $facets_info = C4::Search::_get_facets_info( $facets );
898 my $expected_facets_info_unimarc = {
899 'au' => { 'label_value' => "Authors" },
900 'ccode' => { 'label_value' => "CollectionCodes" },
901 'holdingbranch' => { 'label_value' => "HoldingLibrary" },
902 'location' => { 'label_value' => "Location" },
903 'se' => { 'label_value' => "Series" },
904 'su-geo' => { 'label_value' => "Places" },
905 'su-to' => { 'label_value' => "Topics" }
907 delete $expected_facets_info_unimarc->{holdingbranch}
908 if Koha::Libraries->count == 1;
909 is_deeply( $facets_info, $expected_facets_info_unimarc,
910 "_get_facets_info returns the correct data");
915 subtest 'MARC21 + DOM' => sub {
917 run_marc21_search_tests();
920 subtest 'UNIMARC + DOM' => sub {
922 run_unimarc_search_tests();
926 subtest 'FindDuplicate' => sub {
928 Koha::Caches->get_instance('config')->flush_all;
929 t::lib::Mocks::mock_preference('marcflavour', 'marc21' );
930 mock_GetMarcSubfieldStructure('marc21');
931 my $z_searcher = Test::MockModule->new('C4::Search');
932 $z_searcher->mock('SimpleSearch', sub {
936 my $e_searcher = Test::MockModule->new('Koha::SearchEngine::Elasticsearch::Search');
937 $e_searcher->mock('simple_search_compat', sub {
943 my $record_1 = MARC::Record->new;
944 $record_1 ->add_fields(
945 [ '100', '0', '0', a => 'Morgenstern, Erin' ],
946 [ '245', '0', '0', a => 'The night circus /' ]
948 my $record_2 = MARC::Record->new;
949 $record_2 ->add_fields(
950 [ '245', '0', '0', a => 'The book of nothing /' ]
952 my $record_3 = MARC::Record->new;
953 $record_3->add_fields(
954 [ '245', '0', '0', a => 'Frog and toad all year /' ]
957 foreach my $engine ('Zebra','Elasticsearch'){
958 t::lib::Mocks::mock_preference('searchEngine', $engine );
960 warning_is { C4::Search::FindDuplicate($record_1);}
961 q/ti,ext:"The night circus \/" and au,ext:"Morgenstern, Erin"/,"Term correctly formed and passed to $engine";
963 warning_is { C4::Search::FindDuplicate($record_2);}
964 q/ti,ext:"The book of nothing \/"/,"Term correctly formed and passed to $engine";
966 warning_is { C4::Search::FindDuplicate($record_3);}
967 q/ti,ext:"Frog and toad all year \/"/,"Term correctly formed and passed to $engine";
972 # Make sure that following tests are not using our config settings
973 Koha::Caches->get_instance('config')->flush_all;