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;
33 use t::lib::Mocks::Zebra;
42 use File::Temp qw/ tempdir /;
45 # Fall back to make sure that the Zebra process
46 # and files get cleaned up
49 sub matchesExplodedTerms {
50 my ($message, $query, @terms) = @_;
51 my $match = '(' . join ('|', map { " \@attr 1=Subject \@attr 4=1 \"$_\"" } @terms) . "){" . scalar(@terms) . "}";
52 like($query, qr/$match/, $message);
55 our $QueryStemming = 0;
56 our $QueryAutoTruncate = 0;
57 our $QueryWeightFields = 0;
59 our $SearchEngine = 'Zebra';
60 our $marcflavour = 'MARC21';
61 our $htdocs = File::Spec->rel2abs(dirname($0));
62 my @htdocs = split /\//, $htdocs;
63 $htdocs[-2] = 'koha-tmpl';
64 $htdocs[-1] = 'opac-tmpl';
65 $htdocs = join '/', @htdocs;
66 our $contextmodule = Test::MockModule->new('C4::Context');
67 $contextmodule->mock('preference', sub {
68 my ($self, $pref) = @_;
69 if ($pref eq 'marcflavour') {
71 } elsif ($pref eq 'QueryStemming') {
72 return $QueryStemming;
73 } elsif ($pref eq 'QueryAutoTruncate') {
74 return $QueryAutoTruncate;
75 } elsif ($pref eq 'QueryWeightFields') {
76 return $QueryWeightFields;
77 } elsif ($pref eq 'QueryFuzzy') {
79 } elsif ($pref eq 'SearchEngine') {
81 } elsif ($pref eq 'maxRecordsForFacets') {
83 } elsif ($pref eq 'FacetLabelTruncationLength') {
85 } elsif ($pref eq 'FacetMaxCount') {
87 } elsif ($pref eq 'OpacHiddenItems') {
89 } elsif ($pref eq 'opacthemes') {
91 } elsif ($pref eq 'OPACLanguages') {
93 } elsif ($pref eq 'AlternateHoldingsField') {
95 } elsif ($pref eq 'AuthoritySeparator') {
97 } elsif ($pref eq 'DisplayLibraryFacets') {
99 } elsif ($pref eq 'UNIMARCAuthorsFacetsSeparator') {
101 } elsif ($pref eq 'casAuthentication' or $pref eq 'casLogout' or $pref eq 'casServerUrl' ) {
103 } elsif ($pref eq 'template') {
105 } elsif ($pref eq 'OPACXSLTResultsDisplay') {
106 return C4::XSLT::_get_best_default_xslt_filename($htdocs, 'bootstrap','en',$marcflavour . 'slim2OPACResults.xsl');
107 } elsif ($pref eq 'BiblioDefaultView') {
109 } elsif ($pref eq 'IdRef') {
111 } elsif ($pref eq 'IntranetBiblioDefaultView') {
113 } elsif ($pref eq 'OPACBaseURL') {
114 return 'http://library.mydnsname.org';
115 } elsif ($pref eq 'OPACResultsLibrary') {
117 } elsif ($pref eq 'OpacSuppression') {
119 } elsif ($pref eq 'OPACURLOpenInNewWindow') {
121 } elsif ($pref eq 'TraceCompleteSubfields') {
123 } elsif ($pref eq 'TraceSubjectSubdivisions') {
125 } elsif ($pref eq 'TrackClicks') {
127 } elsif ($pref eq 'URLLinkText') {
129 } elsif ($pref eq 'UseAuthoritiesForTracings') {
131 } elsif ($pref eq 'UseControlNumber') {
133 } elsif ($pref eq 'UseICUStyleQuotes') {
135 } elsif ($pref eq 'viewISBD') {
137 } elsif ($pref eq 'EasyAnalyticalRecords') {
139 } elsif ($pref eq 'OpenURLResolverURL') {
141 } elsif ($pref eq 'OPACShowOpenURL') {
143 } elsif ($pref eq 'OpenURLText') {
145 } elsif ($pref eq 'OPACShowMusicalInscripts') {
147 } elsif ($pref eq 'OPACPlayMusicalInscripts') {
149 } elsif ($pref eq 'Reference_NFL_Statuses') {
151 } elsif ($pref eq 'FacetOrder') {
152 return 'Alphabetical';
153 } elsif ($pref eq 'OPACResultsUnavailableGroupingBy') {
155 } elsif ($pref eq 'SearchLimitLibrary') {
158 warn "The syspref $pref was requested but I don't know what to say; this indicates that the test requires updating"
159 unless $pref =~ m/(XSLT|item|branch|holding|image)/i;
164 our $bibliomodule = Test::MockModule->new('C4::Biblio');
166 sub mock_GetMarcSubfieldStructure {
167 my $marc_type = shift;
168 if ($marc_type eq 'MARC21') {
169 $bibliomodule->mock('GetMarcSubfieldStructure', sub {
171 'biblio.biblionumber' => [{ tagfield => '999', tagsubfield => 'c' }],
172 'biblio.isbn' => [{ tagfield => '020', tagsubfield => 'a' }],
173 'biblio.title' => [{ tagfield => '245', tagsubfield => 'a' }],
174 'biblio.author' => [{ tagfield => '100', tagsubfield => 'a' }],
175 'biblio.notes' => [{ tagfield => '500', tagsubfield => 'a' }],
176 'items.barcode' => [{ tagfield => '952', tagsubfield => 'p' }],
177 'items.booksellerid' => [{ tagfield => '952', tagsubfield => 'e' }],
178 'items.ccode' => [{ tagfield => '952', tagsubfield => '8' }],
179 'items.cn_sort' => [{ tagfield => '952', tagsubfield => '6' }],
180 'items.cn_source' => [{ tagfield => '952', tagsubfield => '2' }],
181 'items.coded_location_qualifier' => [{ tagfield => '952', tagsubfield => 'f' }],
182 'items.copynumber' => [{ tagfield => '952', tagsubfield => 't' }],
183 'items.damaged' => [{ tagfield => '952', tagsubfield => '4' }],
184 'items.dateaccessioned' => [{ tagfield => '952', tagsubfield => 'd' }],
185 'items.datelastborrowed' => [{ tagfield => '952', tagsubfield => 's' }],
186 'items.datelastseen' => [{ tagfield => '952', tagsubfield => 'r' }],
187 'items.enumchron' => [{ tagfield => '952', tagsubfield => 'h' }],
188 'items.holdingbranch' => [{ tagfield => '952', tagsubfield => 'b' }],
189 'items.homebranch' => [{ tagfield => '952', tagsubfield => 'a' }],
190 'items.issues' => [{ tagfield => '952', tagsubfield => 'l' }],
191 'items.itemcallnumber' => [{ tagfield => '952', tagsubfield => 'o' }],
192 'items.itemlost' => [{ tagfield => '952', tagsubfield => '1' }],
193 'items.itemnotes' => [{ tagfield => '952', tagsubfield => 'z' }],
194 'items.itemnumber' => [{ tagfield => '952', tagsubfield => '9' }],
195 'items.itype' => [{ tagfield => '952', tagsubfield => 'y' }],
196 'items.location' => [{ tagfield => '952', tagsubfield => 'c' }],
197 'items.materials' => [{ tagfield => '952', tagsubfield => '3' }],
198 'items.nonpublicnote' => [{ tagfield => '952', tagsubfield => 'x' }],
199 'items.notforloan' => [{ tagfield => '952', tagsubfield => '7' }],
200 'items.onloan' => [{ tagfield => '952', tagsubfield => 'q' }],
201 'items.price' => [{ tagfield => '952', tagsubfield => 'g' }],
202 'items.renewals' => [{ tagfield => '952', tagsubfield => 'm' }],
203 'items.replacementprice' => [{ tagfield => '952', tagsubfield => 'v' }],
204 'items.replacementpricedate' => [{ tagfield => '952', tagsubfield => 'w' }],
205 'items.reserves' => [{ tagfield => '952', tagsubfield => 'n' }],
206 'items.restricted' => [{ tagfield => '952', tagsubfield => '5' }],
207 'items.stack' => [{ tagfield => '952', tagsubfield => 'j' }],
208 'items.uri' => [{ tagfield => '952', tagsubfield => 'u' }],
209 'items.withdrawn' => [{ tagfield => '952', tagsubfield => '0' }],
215 sub run_marc21_search_tests {
217 $marcflavour = 'MARC21';
218 my $mock_zebra = t::lib::Mocks::Zebra->new({marcflavour => $marcflavour});
219 push @cleanup, $mock_zebra;
221 mock_GetMarcSubfieldStructure($marcflavour);
223 use_ok('C4::Search', qw( getIndexes FindDuplicate SimpleSearch getRecords buildQuery searchResults ));
225 # set search syspreferences to a known starting point
227 $QueryAutoTruncate = 0;
228 $QueryWeightFields = 0;
231 my $indexes = C4::Search::getIndexes();
232 is(scalar(grep(/^ti$/, @$indexes)), 1, "Title index supported");
233 is(scalar(grep(/^arl$/, @$indexes)), 1, "Accelerated reading level index supported");
234 is(scalar(grep(/^arp$/, @$indexes)), 1, "Accelerated reading point index supported");
236 my $bibliomodule = Test::MockModule->new('C4::Biblio');
239 'CPL' => { 'branchaddress1' => 'Jefferson Summit', 'branchcode' => 'CPL', 'branchname' => 'Centerville', },
240 'FFL' => { 'branchaddress1' => 'River Station', 'branchcode' => 'FFL', 'branchname' => 'Fairfield', },
241 'FPL' => { 'branchaddress1' => 'Hickory Squere', 'branchcode' => 'FPL', 'branchname' => 'Fairview', },
242 'FRL' => { 'branchaddress1' => 'Smith Heights', 'branchcode' => 'FRL', 'branchname' => 'Franklin', },
243 'IPT' => { 'branchaddress1' => '', 'branchcode' => 'IPT', 'branchname' => "Institut Protestant de Théologie", },
244 'LPL' => { 'branchaddress1' => 'East Hills', 'branchcode' => 'LPL', 'branchname' => 'Liberty', },
245 'MPL' => { 'branchaddress1' => '372 Forest Street', 'branchcode' => 'MPL', 'branchname' => 'Midway', },
246 'PVL' => { 'branchaddress1' => 'Meadow Grove', 'branchcode' => 'PVL', 'branchname' => 'Pleasant Valley', },
247 'RPL' => { 'branchaddress1' => 'Johnson Terrace', 'branchcode' => 'RPL', 'branchname' => 'Riverside', },
248 'SPL' => { 'branchaddress1' => 'Highland Boulevard', 'branchcode' => 'SPL', 'branchname' => 'Springfield', },
249 'S' => { 'branchaddress1' => '', 'branchcode' => 'S', 'branchname' => 'Test', },
250 'TPL' => { 'branchaddress1' => 'Valley Way', 'branchcode' => 'TPL', 'branchname' => 'Troy', },
251 'UPL' => { 'branchaddress1' => 'Chestnut Hollow', 'branchcode' => 'UPL', 'branchname' => 'Union', },
254 'BK' => { 'imageurl' => 'bridge/book.png', 'summary' => '', 'itemtype' => 'BK', 'description' => 'Books' },
255 'CF' => { 'imageurl' => 'bridge/computer_file.png', 'summary' => '', 'itemtype' => 'CF', 'description' => 'Computer Files' },
256 'CR' => { 'imageurl' => 'bridge/periodical.png', 'summary' => '', 'itemtype' => 'CR', 'description' => 'Continuing Resources' },
257 'MP' => { 'imageurl' => 'bridge/map.png', 'summary' => '', 'itemtype' => 'MP', 'description' => 'Maps' },
258 'MU' => { 'imageurl' => 'bridge/sound.png', 'summary' => '', 'itemtype' => 'MU', 'description' => 'Music' },
259 'MX' => { 'imageurl' => 'bridge/kit.png', 'summary' => '', 'itemtype' => 'MX', 'description' => 'Mixed Materials' },
260 'REF' => { 'imageurl' => '', 'summary' => '', 'itemtype' => 'REF', 'description' => 'Reference' },
261 'VM' => { 'imageurl' => 'bridge/dvd.png', 'summary' => '', 'itemtype' => 'VM', 'description' => 'Visual Materials' },
264 my $sourcedir = dirname(__FILE__) . "/data";
265 $mock_zebra->load_records(
266 sprintf( "%s/%s/zebraexport/biblio", $sourcedir, lc($marcflavour) ),
267 'iso2709', 'biblios', 1 );
268 $mock_zebra->load_records(
270 "%s/%s/zebraexport/large_biblio",
271 $sourcedir, lc($marcflavour)
273 'marcxml', 'biblios', 0
275 $mock_zebra->load_records(
276 sprintf( "%s/%s/zebraexport/authority", $sourcedir, lc($marcflavour) ),
277 'iso2709', 'authorities', 1
280 $mock_zebra->launch_zebra;
282 my ($biblionumber, $title);
283 my $record = MARC::Record->new;
286 [ '020', ' ', ' ', a => '9788522421718' ],
287 [ '245', '0', '0', a => 'Administração da produção /' ]
289 ($biblionumber,undef,$title) = FindDuplicate($record);
290 is($biblionumber, 51, 'Found duplicate with ISBN');
292 $record = MARC::Record->new;
295 [ '100', '1', ' ', a => 'Carter, Philip J.' ],
296 [ '245', '1', '4', a => 'Test your emotional intelligence :' ]
298 ($biblionumber,undef,$title) = FindDuplicate($record);
299 is($biblionumber, 203, 'Found duplicate with author/title');
301 # Testing SimpleSearch
303 my ( $error, $marcresults, $total_hits ) = SimpleSearch("book", 0, 9);
305 is(scalar @$marcresults, 9, "SimpleSearch retrieved requested number of records");
306 is($total_hits, 101, "SimpleSearch for 'book' matched right number of records");
307 is($error, undef, "SimpleSearch does not return an error when successful");
310 ( $error, $marcresults2, $total_hits ) = SimpleSearch("book", 5, 5);
311 is($marcresults->[5], $marcresults2->[0], "SimpleSearch cursor functions");
313 ( $error, $marcresults, $total_hits ) = SimpleSearch("kw=book", 0, 10);
314 is($total_hits, 101, "SimpleSearch handles simple CCL");
316 ( $error, $marcresults, $total_hits ) = SimpleSearch("Music-number=49631-2", 0, 10);
317 is($total_hits, 1, "SimpleSearch on music publisher number works (bug 8252)");
318 ( $error, $marcresults, $total_hits ) = SimpleSearch("Identifier-publisher-for-music=49631-2", 0, 10);
319 is($total_hits, 1, "SimpleSearch on music publisher number works using Identifier-publisher-for-music (bug 8252)");
325 ( undef, $results_hashref, $facets_loop ) =
326 getRecords('kw:book', 'book', [], [ 'biblioserver' ], '19', 0, \%branches, \%itemtypes, 'ccl', undef);
327 is($results_hashref->{biblioserver}->{hits}, 101, "getRecords keyword search for 'book' matched right number of records");
328 is(scalar @{$results_hashref->{biblioserver}->{RECORDS}}, 19, "getRecords returned requested number of records");
329 my $record5 = $results_hashref->{biblioserver}->{RECORDS}->[5];
330 ( undef, $results_hashref, $facets_loop ) =
331 getRecords('kw:book', 'book', [], [ 'biblioserver' ], '20', 5, \%branches, \%itemtypes, 'ccl', undef);
332 ok(!defined $results_hashref->{biblioserver}->{RECORDS}->[0] &&
333 !defined $results_hashref->{biblioserver}->{RECORDS}->[1] &&
334 !defined $results_hashref->{biblioserver}->{RECORDS}->[2] &&
335 !defined $results_hashref->{biblioserver}->{RECORDS}->[3] &&
336 !defined $results_hashref->{biblioserver}->{RECORDS}->[4] &&
337 $results_hashref->{biblioserver}->{RECORDS}->[5] eq $record5, "getRecords cursor works");
339 ( undef, $results_hashref, $facets_loop ) =
340 getRecords('ti:book', 'ti:book', [], [ 'biblioserver' ], '20', 0, \%branches, \%itemtypes, 'ccl', undef);
341 is($results_hashref->{biblioserver}->{hits}, 11, "getRecords title search for 'book' matched right number of records");
343 ( undef, $results_hashref, $facets_loop ) =
344 getRecords('au:Lessig', 'au:Lessig', [], [ 'biblioserver' ], '20', 0, \%branches, \%itemtypes, 'ccl', undef);
345 is($results_hashref->{biblioserver}->{hits}, 4, "getRecords title search for 'Australia' matched right number of records");
347 ( undef, $results_hashref, $facets_loop ) =
348 getRecords('salud', 'salud', [], [ 'biblioserver' ], '19', 0, \%branches, \%itemtypes, 'ccl', undef);
349 ok(MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[0],'UTF-8')->title_proper() =~ m/^Efectos del ambiente/ &&
350 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' &&
351 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[18],'UTF-8')->title_proper() =~ m/^Indicadores de resultados identificados/
352 , "Simple relevance sorting in getRecords matches old behavior");
354 ( undef, $results_hashref, $facets_loop ) =
355 getRecords('salud', 'salud', [ 'author_az' ], [ 'biblioserver' ], '38', 0, \%branches, \%itemtypes, 'ccl', undef);
356 ok(MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[0],'UTF-8')->title_proper() =~ m/la enfermedad laboral\^ies$/ &&
357 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[6],'UTF-8')->title_proper() =~ m/^Indicadores de resultados identificados/ &&
358 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[18],'UTF-8')->title_proper() eq 'World health statistics 2009^ien'
359 , "Simple ascending author sorting in getRecords matches old behavior");
361 ( undef, $results_hashref, $facets_loop ) =
362 getRecords('salud', 'salud', [ 'author_za' ], [ 'biblioserver' ], '38', 0, \%branches, \%itemtypes, 'ccl', undef);
363 ok(MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[0],'UTF-8')->title_proper() eq 'World health statistics 2009^ien' &&
364 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[12],'UTF-8')->title_proper() =~ m/^Indicadores de resultados identificados/ &&
365 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[18],'UTF-8')->title_proper() =~ m/la enfermedad laboral\^ies$/
366 , "Simple descending author sorting in getRecords matches old behavior");
368 ( undef, $results_hashref, $facets_loop ) =
369 getRecords('salud', 'salud', [ 'pubdate_asc' ], [ 'biblioserver' ], '38', 0, \%branches, \%itemtypes, 'ccl', undef);
370 ok(MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[0],'UTF-8')->title_proper() eq 'Manual de higiene industrial^ies' &&
371 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[7],'UTF-8')->title_proper() =~ m/seguridad e higiene del trabajo\^ies$/ &&
372 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[18],'UTF-8')->title_proper() =~ m/^Indicadores de resultados identificados/
373 , "Simple ascending publication date sorting in getRecords matches old behavior");
375 ( undef, $results_hashref, $facets_loop ) =
376 getRecords('salud', 'salud', [ 'pubdate_dsc' ], [ 'biblioserver' ], '38', 0, \%branches, \%itemtypes, 'ccl', undef);
377 ok(MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[0],'UTF-8')->title_proper() =~ m/^Estado de salud/ &&
378 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[7],'UTF-8')->title_proper() eq 'World health statistics 2009^ien' &&
379 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[18],'UTF-8')->title_proper() eq 'Manual de higiene industrial^ies'
380 , "Simple descending publication date sorting in getRecords matches old behavior");
382 ( undef, $results_hashref, $facets_loop ) =
383 getRecords('books', 'books', [ 'relevance' ], [ 'biblioserver' ], '20', 0, \%branches, \%itemtypes, undef, 1);
384 $record = MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[0]);
385 is($record->title_proper(), 'Books', "Scan returned requested item");
386 is($record->subfield('100', 'a'), 2, "Scan returned correct number of records matching term");
387 # Time to test buildQuery and searchResults too.
389 my ( $query, $simple_query, $query_cgi,
390 $query_desc, $limit, $limit_cgi, $limit_desc,
392 ( $error, $query, $simple_query, $query_cgi,
393 $query_desc, $limit, $limit_cgi, $limit_desc,
394 $query_type ) = buildQuery([], [ 'salud' ], [], [], [], 0, 'en');
395 like($query, qr/kw\W.*salud/, "Built CCL keyword query");
397 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
398 is($results_hashref->{biblioserver}->{hits}, 19, "getRecords generated keyword search for 'salud' matched right number of records");
400 my @newresults = searchResults({'interface' => 'opac'}, $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 18, 0, 0,
401 $results_hashref->{'biblioserver'}->{"RECORDS"});
402 is(scalar @newresults,18, "searchResults returns requested number of hits");
404 ( $error, $query, $simple_query, $query_cgi,
405 $query_desc, $limit, $limit_cgi, $limit_desc,
406 $query_type ) = buildQuery([ 'AND' ], [ 'salud', 'higiene' ], [], [], [], 0, 'en');
407 like($query, qr/kw\W.*salud\W.*AND.*kw\W.*higiene/, "Built composed explicit-and CCL keyword query");
409 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
410 is($results_hashref->{biblioserver}->{hits}, 3, "getRecords generated composed keyword search for 'salud' explicit-and 'higiene' matched right number of records");
412 ( $error, $query, $simple_query, $query_cgi,
413 $query_desc, $limit, $limit_cgi, $limit_desc,
414 $query_type ) = buildQuery([ 'OR' ], [ 'salud', 'higiene' ], [], [], [], 0, 'en');
415 like($query, qr/kw\W.*salud\W.*OR.*kw\W.*higiene/, "Built composed explicit-or CCL keyword query");
417 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
418 is($results_hashref->{biblioserver}->{hits}, 20, "getRecords generated composed keyword search for 'salud' explicit-or 'higiene' matched right number of records");
420 ( $error, $query, $simple_query, $query_cgi,
421 $query_desc, $limit, $limit_cgi, $limit_desc,
422 $query_type ) = buildQuery([], [ 'salud', 'higiene' ], [], [], [], 0, 'en');
423 like($query, qr/kw\W.*salud\W.*AND.*kw\W.*higiene/, "Built composed implicit-and CCL keyword query");
425 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
426 is($results_hashref->{biblioserver}->{hits}, 3, "getRecords generated composed keyword search for 'salud' implicit-and 'higiene' matched right number of records");
428 ( $error, $query, $simple_query, $query_cgi,
429 $query_desc, $limit, $limit_cgi, $limit_desc,
430 $query_type ) = buildQuery([], [ 'salud' ], [ 'kw' ], [ 'su-to:Laboratorios' ], [], 0, 'en');
431 like($query, qr/kw\W.*salud\W*and\W*su-to\W.*Laboratorios/, "Faceted query generated correctly");
432 unlike($query_desc, qr/Laboratorios/, "Facets not included in query description");
434 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
435 is($results_hashref->{biblioserver}->{hits}, 2, "getRecords generated faceted search matched right number of records");
438 ( $error, $query, $simple_query, $query_cgi,
439 $query_desc, $limit, $limit_cgi, $limit_desc,
440 $query_type ) = buildQuery([], [ '' ], [ 'kw' ], [ 'mc-itype:MP', 'mc-itype:MU' ], [], 0, 'en');
442 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
443 is($results_hashref->{biblioserver}->{hits}, 2, "getRecords generated mc-faceted search matched right number of records");
446 ( $error, $query, $simple_query, $query_cgi,
447 $query_desc, $limit, $limit_cgi, $limit_desc,
448 $query_type ) = buildQuery([], [ '' ], [ 'kw' ], [ 'mc-loc:GEN', 'branch:FFL' ], [], 0, 'en');
450 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
451 is($results_hashref->{biblioserver}->{hits}, 2, "getRecords generated multi-faceted search matched right number of records");
453 ( $error, $query, $simple_query, $query_cgi,
454 $query_desc, $limit, $limit_cgi, $limit_desc,
455 $query_type ) = buildQuery([], [ 'NEKLS' ], [ 'Code-institution' ], [], [], 0, 'en');
456 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
457 is($results_hashref->{biblioserver}->{hits}, 12,
458 'search using index whose name contains "ns" returns expected results (bug 10271)');
460 ( $error, $query, $simple_query, $query_cgi,
461 $query_desc, $limit, $limit_cgi, $limit_desc,
462 $query_type ) = buildQuery([], [ '' ], [ 'kw' ], [ 'available' ], [], 0, 'en');
464 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
465 is($results_hashref->{biblioserver}->{hits}, 2, "getRecords generated availability-limited search matched right number of records");
467 @newresults = searchResults({'interface'=>'opac'}, $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 17, 0, 0,
468 $results_hashref->{'biblioserver'}->{"RECORDS"});
469 my $allavailable = 'true';
470 foreach my $result (@newresults) {
471 $allavailable = 'false' unless $result->{availablecount} > 0;
473 is ($allavailable, 'true', 'All records have at least one item available');
475 my $mocked_xslt = Test::MockModule->new('Koha::XSLT::Base');
476 $mocked_xslt->mock( 'transform', sub {
477 my ($self, $xml) = @_;
481 @newresults = searchResults({'interface'=>'opac'}, $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 17, 0, 0,
482 $results_hashref->{'biblioserver'}->{"RECORDS"}, { anonymous_session => 1 });
484 like( $newresults[0]->{XSLTResultsRecord}, qr/<variable name="anonymous_session">1<\/variable>/, "Variable injected correctly" );
486 my $biblio_id = $newresults[0]->{biblionumber};
487 my $fw = C4::Biblio::GetFrameworkCode($biblio_id);
489 my $dbh = C4::Context->dbh;
490 # FIXME This change is revert in END
491 # Hide subfield 'p' in OPAC
493 UPDATE marc_subfield_structure
495 WHERE frameworkcode='$fw' AND
500 # Hide subfield 'y' in Staff
502 UPDATE marc_subfield_structure
504 WHERE frameworkcode='$fw' AND
509 Koha::Caches->get_instance->flush_all;
511 @newresults = searchResults(
512 { 'interface' => 'opac' },
514 $results_hashref->{'biblioserver'}->{'hits'},
518 $results_hashref->{'biblioserver'}->{"RECORDS"}
521 unlike( $newresults[0]->{XSLTResultsRecord}, qr/<subfield code="p">TEST11111<\/subfield>/, '952\$p hidden in OPAC' );
523 @newresults = searchResults(
524 { 'interface' => 'intranet' },
526 $results_hashref->{'biblioserver'}->{'hits'},
530 $results_hashref->{'biblioserver'}->{"RECORDS"}
533 unlike( $newresults[0]->{XSLTResultsRecord}, qr/<subfield code="y">Books<\/subfield>/, '952\$y hidden on staff interface' );
535 ( $error, $query, $simple_query, $query_cgi,
536 $query_desc, $limit, $limit_cgi, $limit_desc,
537 $query_type ) = buildQuery([], [ 'pqf=@attr 1=_ALLRECORDS @attr 2=103 ""' ], [], [], [], 0, 'en');
539 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
540 is($results_hashref->{biblioserver}->{hits}, 180, "getRecords on _ALLRECORDS PQF returned all records");
542 ( $error, $query, $simple_query, $query_cgi,
543 $query_desc, $limit, $limit_cgi, $limit_desc,
544 $query_type ) = buildQuery([], [ 'pqf=@attr 1=1016 "Lessig"' ], [], [], [], 0, 'en');
546 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
547 is($results_hashref->{biblioserver}->{hits}, 4, "getRecords PQF author search for Lessig returned proper number of matches");
549 ( $error, $query, $simple_query, $query_cgi,
550 $query_desc, $limit, $limit_cgi, $limit_desc,
551 $query_type ) = buildQuery([], [ 'ccl=au:Lessig' ], [], [], [], 0, 'en');
553 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
554 is($results_hashref->{biblioserver}->{hits}, 4, "getRecords CCL author search for Lessig returned proper number of matches");
556 ( $error, $query, $simple_query, $query_cgi,
557 $query_desc, $limit, $limit_cgi, $limit_desc,
558 $query_type ) = buildQuery([], [ 'cql=dc.author any lessig' ], [], [], [], 0, 'en');
560 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
561 is($results_hashref->{biblioserver}->{hits}, 4, "getRecords CQL author search for Lessig returned proper number of matches");
563 $QueryStemming = $QueryAutoTruncate = $QueryFuzzy = 0;
564 $QueryWeightFields = 1;
565 ( $error, $query, $simple_query, $query_cgi,
566 $query_desc, $limit, $limit_cgi, $limit_desc,
567 $query_type ) = buildQuery([], [ 'salud' ], [ 'kw' ], [], [], 0, 'en');
569 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
570 is($results_hashref->{biblioserver}->{hits}, 19, "Weighted query returned correct number of results");
571 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");
573 $QueryStemming = $QueryWeightFields = $QueryFuzzy = 0;
574 $QueryAutoTruncate = 1;
575 ( $error, $query, $simple_query, $query_cgi,
576 $query_desc, $limit, $limit_cgi, $limit_desc,
577 $query_type ) = buildQuery([], [ 'medic' ], [ 'kw' ], [], [], 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}, 5, "Search for 'medic' returns matches with automatic truncation on");
582 ( $error, $query, $simple_query, $query_cgi,
583 $query_desc, $limit, $limit_cgi, $limit_desc,
584 $query_type ) = buildQuery([], [ 'medic*' ], [ 'kw' ], [], [], 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}, 5, "Search for 'medic*' returns matches with automatic truncation on");
589 $QueryStemming = $QueryFuzzy = $QueryAutoTruncate = 0;
590 $QueryWeightFields = 1;
591 ( $error, $query, $simple_query, $query_cgi,
592 $query_desc, $limit, $limit_cgi, $limit_desc,
593 $query_type ) = buildQuery([], [ 'web application' ], [ 'kw' ], [], [], 0, 'en');
594 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
595 is($results_hashref->{biblioserver}->{hits}, 1, "Search for 'web application' returns one hit with QueryWeightFields on");
597 ( $error, $query, $simple_query, $query_cgi,
598 $query_desc, $limit, $limit_cgi, $limit_desc,
599 $query_type ) = buildQuery([], [ 'web "application' ], [ 'kw' ], [], [], 0, 'en');
600 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
601 is($results_hashref->{biblioserver}->{hits}, 1, "Search for 'web \"application' returns one hit with QueryWeightFields on (bug 7518)");
603 $QueryStemming = $QueryWeightFields = $QueryFuzzy = $QueryAutoTruncate = 0;
604 ( $error, $query, $simple_query, $query_cgi,
605 $query_desc, $limit, $limit_cgi, $limit_desc,
606 $query_type ) = buildQuery([], [ 'medic' ], [ 'kw' ], [], [], 0, 'en');
608 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
609 is($results_hashref->{biblioserver}->{hits}, undef, "Search for 'medic' returns no matches with automatic truncation off");
611 ( $error, $query, $simple_query, $query_cgi,
612 $query_desc, $limit, $limit_cgi, $limit_desc,
613 $query_type ) = buildQuery([], [ 'medic*' ], [ 'kw' ], [], [], 0, 'en');
615 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
616 is($results_hashref->{biblioserver}->{hits}, 5, "Search for 'medic*' returns matches with automatic truncation off");
618 $QueryStemming = $QueryWeightFields = 1;
619 $QueryFuzzy = $QueryAutoTruncate = 0;
620 ( $error, $query, $simple_query, $query_cgi,
621 $query_desc, $limit, $limit_cgi, $limit_desc,
622 $query_type ) = buildQuery([], [ 'pressed' ], [ 'kw' ], [], [], 0, 'en');
624 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
625 is($results_hashref->{biblioserver}->{hits}, 7, "Search for 'pressed' returns matches when stemming (and query weighting) is on");
627 $QueryStemming = $QueryWeightFields = $QueryFuzzy = $QueryAutoTruncate = 0;
628 ( $error, $query, $simple_query, $query_cgi,
629 $query_desc, $limit, $limit_cgi, $limit_desc,
630 $query_type ) = buildQuery([], [ 'pressed' ], [ 'kw' ], [], [], 0, 'en');
632 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
633 is($results_hashref->{biblioserver}->{hits}, undef, "Search for 'pressed' returns no matches when stemming is off");
635 ( $error, $query, $simple_query, $query_cgi,
636 $query_desc, $limit, $limit_cgi, $limit_desc,
637 $query_type ) = buildQuery([], [ 'ccl=an:42' ], [], ['available'], [], 0, 'en');
638 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' );
639 is( $query_desc, 'an:42', 'buildQuery should remove the available part from the query' );
641 ( $error, $query, $simple_query, $query_cgi,
642 $query_desc, $limit, $limit_cgi, $limit_desc,
643 $query_type ) = buildQuery([], [ 0 ], [ 'su,phr' ], [], [], 0, 'en');
644 is($query, 'su,phr=(rk=(0)) ', 'buildQuery should keep 0 value in query');
645 is($query_cgi, 'idx=su%2Cphr&q=0', 'buildQuery should keep 0 value in query_cgi');
646 is($query_desc, 'su,phr: 0', 'buildQuery should keep 0 value in query_desc');
649 ( $error, $query, $simple_query, $query_cgi,
650 $query_desc, $limit, $limit_cgi, $limit_desc,
651 $query_type ) = buildQuery([], [], [], [ 'mc-ccode:NF(IC'], [], 0, 'en');
652 like($query, qr/ccode="NF\(IC"/, "Limit quoted correctly");
654 # Let's see what happens when we pass bad data into these routines.
655 # We have to catch warnings since we're not very good about returning errors.
657 warning_like { ( $error, $marcresults, $total_hits ) = SimpleSearch("@==ccl blah", 0, 9) } qr/CCL parsing error/,
658 "SimpleSearch warns about CCL parsing error with nonsense query";
659 isnt($error, undef, "SimpleSearch returns an error when passed gibberish");
661 warning_like {( undef, $results_hashref, $facets_loop ) =
662 getRecords('kw:book', 'book', [], [ 'biblioserver' ], '19', 0, \%branches, \%itemtypes, 'nonsense', undef) }
663 qr/Unknown query_type/, "getRecords warns about unknown query type";
665 warning_like {( undef, $results_hashref, $facets_loop ) =
666 getRecords('pqf=@attr 1=4 "title"', 'pqf=@attr 1=4 "title"', [], [ 'biblioserver' ], '19', 0, \%branches, \%itemtypes, '', undef) }
667 qr/WARNING: query problem/, "getRecords warns when query type is not specified for non-CCL query";
669 # Let's just test a few other bits and bobs, just for fun
671 ($error, $results_hashref, $facets_loop) = getRecords("Godzina pąsowej róży","Godzina pąsowej róży",[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
672 @newresults = searchResults({'interface'=>'intranet'}, $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 17, 0, 0,
673 $results_hashref->{'biblioserver'}->{"RECORDS"});
674 is($newresults[0]->{'alternateholdings_count'}, 1, 'Alternate holdings filled in correctly');
677 ## Regression test for Bug 10741
679 # make one of the test items appear to be in transit
680 my $circ_module = Test::MockModule->new('C4::Circulation');
681 $circ_module->mock('GetTransfers', sub {
682 my $itemnumber = shift // -1;
683 if ($itemnumber == 11) {
684 return ('2013-07-19', 'MPL', 'CPL');
690 ($error, $results_hashref, $facets_loop) = getRecords("TEST12121212","TEST12121212",[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
691 @newresults = searchResults({'interface'=>'intranet'}, $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 17, 0, 0,
692 $results_hashref->{'biblioserver'}->{"RECORDS"});
693 ok(!exists($newresults[0]->{norequests}), 'presence of a transit does not block hold request action (bug 10741)');
695 ## Regression test for bug 10684
696 ( undef, $results_hashref, $facets_loop ) =
697 getRecords('ti:punctuation', 'punctuation', [], [ 'biblioserver' ], '19', 0, \%branches, \%itemtypes, 'ccl', undef);
698 is($results_hashref->{biblioserver}->{hits}, 1, "search for ti:punctuation returned expected number of records");
699 warning_like { @newresults = searchResults({'intranet' => 'intranet'}, $query_desc,
700 $results_hashref->{'biblioserver'}->{'hits'}, 20, 0, 0,
701 $results_hashref->{'biblioserver'}->{"RECORDS"}) }
702 qr/^ERROR DECODING RECORD - Tag "50%" is not a valid tag/,
703 "Warning is raised correctly for invalid tags in MARC::Record";
704 is(scalar(@newresults), 0, 'a record that cannot be parsed by MARC::Record is simply skipped (bug 10684)');
706 my ($auths, $count) = SearchAuthorities(
707 ['mainentry'], ['and'], [''], ['starts'],
708 ['shakespeare'], 0, 10, '', '', 1
710 is($count, 1, 'MARC21 authorities: one hit on mainentry starts with "shakespeare"');
711 ($auths, $count) = SearchAuthorities(
712 ['mainentry'], ['and'], [''], ['starts'],
713 ['shakespeare'], 0, 10, '', 'HeadingAsc', 1
715 is($count, 1, 'MARC21 authorities: one hit on mainentry starts with "shakespeare" sorted by heading ascending');
716 ($auths, $count) = SearchAuthorities(
717 ['mainentry'], ['and'], [''], ['starts'],
718 ['shakespeare'], 0, 10, '', 'HeadingDsc', 1
720 is($count, 1, 'MARC21 authorities: one hit on mainentry starts with "shakespeare" sorted by heading descending');
721 ($auths, $count) = SearchAuthorities(
722 ['match'], ['and'], [''], ['contains'],
723 ['沙士北亞威廉姆'], 0, 10, '', '', 1
725 is($count, 1, 'MARC21 authorities: one hit on match contains "沙士北亞威廉姆"');
726 ($auths, $count) = SearchAuthorities(
727 ['LC-card-number'], ['and'], [''], ['contains'],
728 ['99282477'], 0, 10, '', '', 1
730 is($count, 1, 'MARC21 authorities: one hit on LC-card-number contains "99282477"');
731 ($auths, $count) = SearchAuthorities(
732 ['all'], ['and'], [''], ['contains'],
733 ['professional wrestler'], 0, 10, '', '', 1
735 is($count, 1, 'MARC21 authorities: one hit on "all" (entire record) contains "professional wrestler"');
737 # retrieve records that are larger than the MARC limit of 99,999 octets
738 ( undef, $results_hashref, $facets_loop ) =
739 getRecords('ti:marc the large record', '', [], [ 'biblioserver' ], '20', 0, \%branches, \%itemtypes, 'ccl', undef);
740 is($results_hashref->{biblioserver}->{hits}, 1, "Can do a search that retrieves an over-large bib record (bug 11096)");
741 @newresults = searchResults({'interface' =>'opac'}, $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 10, 0, 0,
742 $results_hashref->{'biblioserver'}->{"RECORDS"});
743 is($newresults[0]->{title}, 'Marc the Large Record', 'Able to render the title for over-large bib record (bug 11096)');
744 is($newresults[0]->{biblionumber}, '300', 'Over-large bib record has the correct biblionumber (bug 11096)');
745 like($newresults[0]->{notes}, qr/This is large note #550/, 'Able to render the notes field for over-large bib record (bug 11096)');
747 # notforloancount should be returned as part of searchResults output
748 ok( defined $newresults[0]->{notforloancount},
749 '\'notforloancount\' defined in searchResults output (Bug 12419)');
750 is( $newresults[0]->{notforloancount}, 2,
751 '\'notforloancount\' == 2 (Bug 12419)');
753 # verify that we don't attempt to sort if no results were returned
754 # because of a query error
755 warning_like {( undef, $results_hashref, $facets_loop ) =
756 getRecords('ccl=( AND )', '', ['title_az'], [ 'biblioserver' ], '20', 0, \%branches, \%itemtypes, 'ccl', undef)
757 } qr/WARNING: query problem with/, 'got warning instead of crash when attempting to run invalid query (bug 9578)';
759 # Test facet calculation
760 my $facets_counter = {};
761 my $facets = C4::Koha::getFacets();
762 # Create a record with a 100$z field
763 my $marc_record = MARC::Record->new;
764 $marc_record->add_fields(
766 [ '100', ' ', ' ', a => 'Cohen Arazi, Tomas' ],
767 [ '100', 'z', ' ', a => 'Tomasito' ],
768 [ '245', ' ', ' ', a => 'First try' ]
770 C4::Search::_get_facets_data_from_record( $marc_record, $facets, $facets_counter );
771 is_deeply( { au => { 'Cohen Arazi, Tomas' => 1 } }, $facets_counter,
772 "_get_facets_data_from_record doesn't count 100\$z (Bug 12788)");
773 $marc_record = MARC::Record->new;
774 $marc_record->add_fields(
776 [ '100', ' ', ' ', a => 'Cohen Arazi, Tomas' ],
777 [ '100', 'z', ' ', a => 'Tomasito' ],
778 [ '245', ' ', ' ', a => 'Second try' ]
780 C4::Search::_get_facets_data_from_record( $marc_record, $facets, $facets_counter );
781 is_deeply( { au => { 'Cohen Arazi, Tomas' => 2 } }, $facets_counter,
782 "_get_facets_data_from_record correctly counts author facet twice");
784 # Test _get_facets_info
785 my $facets_info = C4::Search::_get_facets_info( $facets );
786 my $expected_facets_info_marc21 = {
787 'au' => { 'label_value' => "Authors" },
788 'ccode' => { 'label_value' => "CollectionCodes" },
789 'holdingbranch' => { 'label_value' => "HoldingLibrary" },
790 'itype' => { 'label_value' => "ItemTypes" },
791 'location' => { 'label_value' => "Location" },
792 'se' => { 'label_value' => "Series" },
793 'su-geo' => { 'label_value' => "Places" },
794 'su-to' => { 'label_value' => "Topics" },
795 'su-ut' => { 'label_value' => "Titles" }
797 delete $expected_facets_info_marc21->{holdingbranch}
798 if Koha::Libraries->count == 1;
799 is_deeply( $facets_info, $expected_facets_info_marc21,
800 "_get_facets_info returns the correct data");
802 $mock_zebra->cleanup;
805 sub run_unimarc_search_tests {
807 $marcflavour = 'UNIMARC';
808 my $mock_zebra = t::lib::Mocks::Zebra->new({marcflavour => $marcflavour});
809 push @cleanup, $mock_zebra;
811 mock_GetMarcSubfieldStructure($marcflavour);
813 use_ok('C4::Search', qw( getIndexes FindDuplicate SimpleSearch getRecords buildQuery searchResults ));
815 # set search syspreferences to a known starting point
817 $QueryAutoTruncate = 0;
818 $QueryWeightFields = 0;
821 my $sourcedir = dirname(__FILE__) . "/data";
822 $mock_zebra->load_records(
823 sprintf( "%s/%s/zebraexport/biblio", $sourcedir, lc($marcflavour) ),
824 'iso2709', 'biblios', 1 );
825 $mock_zebra->load_records(
827 "%s/%s/zebraexport/large_biblio",
828 $sourcedir, lc($marcflavour)
830 'marcxml', 'biblios', 0
832 $mock_zebra->load_records(
833 sprintf( "%s/%s/zebraexport/authority", $sourcedir, lc($marcflavour) ),
834 'iso2709', 'authorities', 1
836 $mock_zebra->launch_zebra;
838 my ( $error, $marcresults, $total_hits ) = SimpleSearch("ti=Järnvägarnas efterfrågan och den svenska industrin", 0, 10);
839 is($total_hits, 1, 'UNIMARC title search');
840 ( $error, $marcresults, $total_hits ) = SimpleSearch("ta=u", 0, 10);
841 is($total_hits, 1, 'UNIMARC target audience = u');
842 ( $error, $marcresults, $total_hits ) = SimpleSearch("ta=k", 0, 10);
843 is($total_hits, 4, 'UNIMARC target audience = k');
844 ( $error, $marcresults, $total_hits ) = SimpleSearch("ta=m", 0, 10);
845 is($total_hits, 3, 'UNIMARC target audience = m');
846 ( $error, $marcresults, $total_hits ) = SimpleSearch("item=EXCLU DU PRET", 0, 10);
847 is($total_hits, 1, 'UNIMARC generic item index (bug 10037)');
850 use_ok('C4::AuthoritiesMarc', qw( SearchAuthorities ));
852 my ($auths, $count) = SearchAuthorities(
853 ['mainentry'], ['and'], [''], ['contains'],
854 ['wil'], 0, 10, '', '', 1
856 is($count, 11, 'UNIMARC authorities: hits on mainentry contains "wil"');
857 ($auths, $count) = SearchAuthorities(
858 ['match'], ['and'], [''], ['contains'],
859 ['wil'], 0, 10, '', '', 1
861 is($count, 11, 'UNIMARC authorities: hits on match contains "wil"');
862 ($auths, $count) = SearchAuthorities(
863 ['mainentry'], ['and'], [''], ['contains'],
864 ['michel'], 0, 20, '', '', 1
866 is($count, 14, 'UNIMARC authorities: hits on mainentry contains "michel"');
867 ($auths, $count) = SearchAuthorities(
868 ['mainmainentry'], ['and'], [''], ['exact'],
869 ['valley'], 0, 20, '', '', 1
871 is($count, 1, 'UNIMARC authorities: hits on mainmainentry = "valley"');
872 ($auths, $count) = SearchAuthorities(
873 ['mainmainentry'], ['and'], [''], ['exact'],
874 ['vall'], 0, 20, '', '', 1
876 is($count, 0, 'UNIMARC authorities: no hits on mainmainentry = "vall"');
877 ($auths, $count) = SearchAuthorities(
878 ['Any'], ['and'], [''], ['starts'],
879 ['jean'], 0, 30, '', '', 1
881 is($count, 24, 'UNIMARC authorities: hits on any starts with "jean"');
883 # Test _get_facets_info
884 my $facets = C4::Koha::getFacets();
885 my $facets_info = C4::Search::_get_facets_info( $facets );
886 my $expected_facets_info_unimarc = {
887 'au' => { 'label_value' => "Authors" },
888 'ccode' => { 'label_value' => "CollectionCodes" },
889 'holdingbranch' => { 'label_value' => "HoldingLibrary" },
890 'location' => { 'label_value' => "Location" },
891 'se' => { 'label_value' => "Series" },
892 'su-geo' => { 'label_value' => "Places" },
893 'su-to' => { 'label_value' => "Topics" }
895 delete $expected_facets_info_unimarc->{holdingbranch}
896 if Koha::Libraries->count == 1;
897 is_deeply( $facets_info, $expected_facets_info_unimarc,
898 "_get_facets_info returns the correct data");
900 $mock_zebra->cleanup;
903 subtest 'MARC21 + DOM' => sub {
905 run_marc21_search_tests();
908 subtest 'UNIMARC + DOM' => sub {
910 run_unimarc_search_tests();
914 subtest 'FindDuplicate' => sub {
916 Koha::Caches->get_instance('config')->flush_all;
917 t::lib::Mocks::mock_preference('marcflavour', 'MARC21' );
918 mock_GetMarcSubfieldStructure('MARC21');
919 my $z_searcher = Test::MockModule->new('C4::Search');
920 $z_searcher->mock('SimpleSearch', sub {
924 my $e_searcher = Test::MockModule->new('Koha::SearchEngine::Elasticsearch::Search');
925 $e_searcher->mock('simple_search_compat', sub {
931 my $record_1 = MARC::Record->new;
932 $record_1 ->add_fields(
933 [ '100', '0', '0', a => 'Morgenstern, Erin' ],
934 [ '245', '0', '0', a => 'The night circus /' ]
936 my $record_2 = MARC::Record->new;
937 $record_2 ->add_fields(
938 [ '245', '0', '0', a => 'The book of nothing /' ]
940 my $record_3 = MARC::Record->new;
941 $record_3->add_fields(
942 [ '245', '0', '0', a => 'Frog and toad all year /' ]
945 foreach my $engine ('Zebra','Elasticsearch'){
946 t::lib::Mocks::mock_preference('searchEngine', $engine );
948 warning_is { C4::Search::FindDuplicate($record_1);}
949 q/ti,ext:"The night circus \/" AND au,ext:"Morgenstern, Erin"/,"Term correctly formed and passed to $engine";
951 warning_is { C4::Search::FindDuplicate($record_2);}
952 q/ti,ext:"The book of nothing \/"/,"Term correctly formed and passed to $engine";
954 warning_is { C4::Search::FindDuplicate($record_3);}
955 q/ti,ext:"Frog and toad all year \/"/,"Term correctly formed and passed to $engine";
960 # Make sure that following tests are not using our config settings
961 Koha::Caches->get_instance('config')->flush_all;
965 $_->cleanup for @cleanup;
966 my $dbh = C4::Context->dbh;
967 # Restore visibility of subfields in OPAC
969 UPDATE marc_subfield_structure
971 WHERE tagfield=952 AND
972 ( tagsubfield IN ('p', 'y') )
975 Koha::Caches->get_instance->flush_all;