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') {
157 } elsif ($pref eq 'UseRecalls') {
160 warn "The syspref $pref was requested but I don't know what to say; this indicates that the test requires updating"
161 unless $pref =~ m/(XSLT|item|branch|holding|image)/i;
166 our $bibliomodule = Test::MockModule->new('C4::Biblio');
168 sub mock_GetMarcSubfieldStructure {
169 my $marc_type = shift;
170 if ($marc_type eq 'MARC21') {
171 $bibliomodule->mock('GetMarcSubfieldStructure', sub {
173 'biblio.biblionumber' => [{ tagfield => '999', tagsubfield => 'c' }],
174 'biblio.isbn' => [{ tagfield => '020', tagsubfield => 'a' }],
175 'biblio.title' => [{ tagfield => '245', tagsubfield => 'a' }],
176 'biblio.author' => [{ tagfield => '100', tagsubfield => 'a' }],
177 'biblio.notes' => [{ tagfield => '500', tagsubfield => 'a' }],
178 'items.barcode' => [{ tagfield => '952', tagsubfield => 'p' }],
179 'items.booksellerid' => [{ tagfield => '952', tagsubfield => 'e' }],
180 'items.ccode' => [{ tagfield => '952', tagsubfield => '8' }],
181 'items.cn_sort' => [{ tagfield => '952', tagsubfield => '6' }],
182 'items.cn_source' => [{ tagfield => '952', tagsubfield => '2' }],
183 'items.coded_location_qualifier' => [{ tagfield => '952', tagsubfield => 'f' }],
184 'items.copynumber' => [{ tagfield => '952', tagsubfield => 't' }],
185 'items.damaged' => [{ tagfield => '952', tagsubfield => '4' }],
186 'items.dateaccessioned' => [{ tagfield => '952', tagsubfield => 'd' }],
187 'items.datelastborrowed' => [{ tagfield => '952', tagsubfield => 's' }],
188 'items.datelastseen' => [{ tagfield => '952', tagsubfield => 'r' }],
189 'items.enumchron' => [{ tagfield => '952', tagsubfield => 'h' }],
190 'items.holdingbranch' => [{ tagfield => '952', tagsubfield => 'b' }],
191 'items.homebranch' => [{ tagfield => '952', tagsubfield => 'a' }],
192 'items.issues' => [{ tagfield => '952', tagsubfield => 'l' }],
193 'items.itemcallnumber' => [{ tagfield => '952', tagsubfield => 'o' }],
194 'items.itemlost' => [{ tagfield => '952', tagsubfield => '1' }],
195 'items.itemnotes' => [{ tagfield => '952', tagsubfield => 'z' }],
196 'items.itemnumber' => [{ tagfield => '952', tagsubfield => '9' }],
197 'items.itype' => [{ tagfield => '952', tagsubfield => 'y' }],
198 'items.location' => [{ tagfield => '952', tagsubfield => 'c' }],
199 'items.materials' => [{ tagfield => '952', tagsubfield => '3' }],
200 'items.nonpublicnote' => [{ tagfield => '952', tagsubfield => 'x' }],
201 'items.notforloan' => [{ tagfield => '952', tagsubfield => '7' }],
202 'items.onloan' => [{ tagfield => '952', tagsubfield => 'q' }],
203 'items.price' => [{ tagfield => '952', tagsubfield => 'g' }],
204 'items.renewals' => [{ tagfield => '952', tagsubfield => 'm' }],
205 'items.replacementprice' => [{ tagfield => '952', tagsubfield => 'v' }],
206 'items.replacementpricedate' => [{ tagfield => '952', tagsubfield => 'w' }],
207 'items.reserves' => [{ tagfield => '952', tagsubfield => 'n' }],
208 'items.restricted' => [{ tagfield => '952', tagsubfield => '5' }],
209 'items.stack' => [{ tagfield => '952', tagsubfield => 'j' }],
210 'items.uri' => [{ tagfield => '952', tagsubfield => 'u' }],
211 'items.withdrawn' => [{ tagfield => '952', tagsubfield => '0' }],
217 sub run_marc21_search_tests {
219 $marcflavour = 'MARC21';
220 my $mock_zebra = t::lib::Mocks::Zebra->new({marcflavour => $marcflavour});
221 push @cleanup, $mock_zebra;
223 mock_GetMarcSubfieldStructure($marcflavour);
225 use_ok('C4::Search', qw( getIndexes FindDuplicate SimpleSearch getRecords buildQuery searchResults ));
227 # set search syspreferences to a known starting point
229 $QueryAutoTruncate = 0;
230 $QueryWeightFields = 0;
233 my $indexes = C4::Search::getIndexes();
234 is(scalar(grep(/^ti$/, @$indexes)), 1, "Title index supported");
235 is(scalar(grep(/^arl$/, @$indexes)), 1, "Accelerated reading level index supported");
236 is(scalar(grep(/^arp$/, @$indexes)), 1, "Accelerated reading point index supported");
238 my $bibliomodule = Test::MockModule->new('C4::Biblio');
241 'CPL' => { 'branchaddress1' => 'Jefferson Summit', 'branchcode' => 'CPL', 'branchname' => 'Centerville', },
242 'FFL' => { 'branchaddress1' => 'River Station', 'branchcode' => 'FFL', 'branchname' => 'Fairfield', },
243 'FPL' => { 'branchaddress1' => 'Hickory Squere', 'branchcode' => 'FPL', 'branchname' => 'Fairview', },
244 'FRL' => { 'branchaddress1' => 'Smith Heights', 'branchcode' => 'FRL', 'branchname' => 'Franklin', },
245 'IPT' => { 'branchaddress1' => '', 'branchcode' => 'IPT', 'branchname' => "Institut Protestant de Théologie", },
246 'LPL' => { 'branchaddress1' => 'East Hills', 'branchcode' => 'LPL', 'branchname' => 'Liberty', },
247 'MPL' => { 'branchaddress1' => '372 Forest Street', 'branchcode' => 'MPL', 'branchname' => 'Midway', },
248 'PVL' => { 'branchaddress1' => 'Meadow Grove', 'branchcode' => 'PVL', 'branchname' => 'Pleasant Valley', },
249 'RPL' => { 'branchaddress1' => 'Johnson Terrace', 'branchcode' => 'RPL', 'branchname' => 'Riverside', },
250 'SPL' => { 'branchaddress1' => 'Highland Boulevard', 'branchcode' => 'SPL', 'branchname' => 'Springfield', },
251 'S' => { 'branchaddress1' => '', 'branchcode' => 'S', 'branchname' => 'Test', },
252 'TPL' => { 'branchaddress1' => 'Valley Way', 'branchcode' => 'TPL', 'branchname' => 'Troy', },
253 'UPL' => { 'branchaddress1' => 'Chestnut Hollow', 'branchcode' => 'UPL', 'branchname' => 'Union', },
256 'BK' => { 'imageurl' => 'bridge/book.png', 'summary' => '', 'itemtype' => 'BK', 'description' => 'Books' },
257 'CF' => { 'imageurl' => 'bridge/computer_file.png', 'summary' => '', 'itemtype' => 'CF', 'description' => 'Computer Files' },
258 'CR' => { 'imageurl' => 'bridge/periodical.png', 'summary' => '', 'itemtype' => 'CR', 'description' => 'Continuing Resources' },
259 'MP' => { 'imageurl' => 'bridge/map.png', 'summary' => '', 'itemtype' => 'MP', 'description' => 'Maps' },
260 'MU' => { 'imageurl' => 'bridge/sound.png', 'summary' => '', 'itemtype' => 'MU', 'description' => 'Music' },
261 'MX' => { 'imageurl' => 'bridge/kit.png', 'summary' => '', 'itemtype' => 'MX', 'description' => 'Mixed Materials' },
262 'REF' => { 'imageurl' => '', 'summary' => '', 'itemtype' => 'REF', 'description' => 'Reference' },
263 'VM' => { 'imageurl' => 'bridge/dvd.png', 'summary' => '', 'itemtype' => 'VM', 'description' => 'Visual Materials' },
266 my $sourcedir = dirname(__FILE__) . "/data";
267 $mock_zebra->load_records(
268 sprintf( "%s/%s/zebraexport/biblio", $sourcedir, lc($marcflavour) ),
269 'iso2709', 'biblios', 1 );
270 $mock_zebra->load_records(
272 "%s/%s/zebraexport/large_biblio",
273 $sourcedir, lc($marcflavour)
275 'marcxml', 'biblios', 0
277 $mock_zebra->load_records(
278 sprintf( "%s/%s/zebraexport/authority", $sourcedir, lc($marcflavour) ),
279 'iso2709', 'authorities', 1
282 $mock_zebra->launch_zebra;
284 my ($biblionumber, $title);
285 my $record = MARC::Record->new;
288 [ '020', ' ', ' ', a => '9788522421718' ],
289 [ '245', '0', '0', a => 'Administração da produção /' ]
291 ($biblionumber,undef,$title) = FindDuplicate($record);
292 is($biblionumber, 51, 'Found duplicate with ISBN');
294 $record = MARC::Record->new;
297 [ '100', '1', ' ', a => 'Carter, Philip J.' ],
298 [ '245', '1', '4', a => 'Test your emotional intelligence :' ]
300 ($biblionumber,undef,$title) = FindDuplicate($record);
301 is($biblionumber, 203, 'Found duplicate with author/title');
303 # Testing SimpleSearch
305 my ( $error, $marcresults, $total_hits ) = SimpleSearch("book", 0, 9);
307 is(scalar @$marcresults, 9, "SimpleSearch retrieved requested number of records");
308 is($total_hits, 101, "SimpleSearch for 'book' matched right number of records");
309 is($error, undef, "SimpleSearch does not return an error when successful");
312 ( $error, $marcresults2, $total_hits ) = SimpleSearch("book", 5, 5);
313 is($marcresults->[5], $marcresults2->[0], "SimpleSearch cursor functions");
315 ( $error, $marcresults, $total_hits ) = SimpleSearch("kw=book", 0, 10);
316 is($total_hits, 101, "SimpleSearch handles simple CCL");
318 ( $error, $marcresults, $total_hits ) = SimpleSearch("Music-number=49631-2", 0, 10);
319 is($total_hits, 1, "SimpleSearch on music publisher number works (bug 8252)");
320 ( $error, $marcresults, $total_hits ) = SimpleSearch("Identifier-publisher-for-music=49631-2", 0, 10);
321 is($total_hits, 1, "SimpleSearch on music publisher number works using Identifier-publisher-for-music (bug 8252)");
327 ( undef, $results_hashref, $facets_loop ) =
328 getRecords('kw:book', 'book', [], [ 'biblioserver' ], '19', 0, \%branches, \%itemtypes, 'ccl', undef);
329 is($results_hashref->{biblioserver}->{hits}, 101, "getRecords keyword search for 'book' matched right number of records");
330 is(scalar @{$results_hashref->{biblioserver}->{RECORDS}}, 19, "getRecords returned requested number of records");
331 my $record5 = $results_hashref->{biblioserver}->{RECORDS}->[5];
332 ( undef, $results_hashref, $facets_loop ) =
333 getRecords('kw:book', 'book', [], [ 'biblioserver' ], '20', 5, \%branches, \%itemtypes, 'ccl', undef);
334 ok(!defined $results_hashref->{biblioserver}->{RECORDS}->[0] &&
335 !defined $results_hashref->{biblioserver}->{RECORDS}->[1] &&
336 !defined $results_hashref->{biblioserver}->{RECORDS}->[2] &&
337 !defined $results_hashref->{biblioserver}->{RECORDS}->[3] &&
338 !defined $results_hashref->{biblioserver}->{RECORDS}->[4] &&
339 $results_hashref->{biblioserver}->{RECORDS}->[5] eq $record5, "getRecords cursor works");
341 ( undef, $results_hashref, $facets_loop ) =
342 getRecords('ti:book', 'ti:book', [], [ 'biblioserver' ], '20', 0, \%branches, \%itemtypes, 'ccl', undef);
343 is($results_hashref->{biblioserver}->{hits}, 11, "getRecords title search for 'book' matched right number of records");
345 ( undef, $results_hashref, $facets_loop ) =
346 getRecords('au:Lessig', 'au:Lessig', [], [ 'biblioserver' ], '20', 0, \%branches, \%itemtypes, 'ccl', undef);
347 is($results_hashref->{biblioserver}->{hits}, 4, "getRecords title search for 'Australia' matched right number of records");
349 ( undef, $results_hashref, $facets_loop ) =
350 getRecords('salud', 'salud', [], [ 'biblioserver' ], '19', 0, \%branches, \%itemtypes, 'ccl', undef);
351 ok(MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[0],'UTF-8')->title_proper() =~ m/^Efectos del ambiente/ &&
352 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' &&
353 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[18],'UTF-8')->title_proper() =~ m/^Indicadores de resultados identificados/
354 , "Simple relevance sorting in getRecords matches old behavior");
356 ( undef, $results_hashref, $facets_loop ) =
357 getRecords('salud', 'salud', [ 'author_az' ], [ 'biblioserver' ], '38', 0, \%branches, \%itemtypes, 'ccl', undef);
358 ok(MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[0],'UTF-8')->title_proper() =~ m/la enfermedad laboral\^ies$/ &&
359 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[6],'UTF-8')->title_proper() =~ m/^Indicadores de resultados identificados/ &&
360 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[18],'UTF-8')->title_proper() eq 'World health statistics 2009^ien'
361 , "Simple ascending author sorting in getRecords matches old behavior");
363 ( undef, $results_hashref, $facets_loop ) =
364 getRecords('salud', 'salud', [ 'author_za' ], [ 'biblioserver' ], '38', 0, \%branches, \%itemtypes, 'ccl', undef);
365 ok(MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[0],'UTF-8')->title_proper() eq 'World health statistics 2009^ien' &&
366 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[12],'UTF-8')->title_proper() =~ m/^Indicadores de resultados identificados/ &&
367 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[18],'UTF-8')->title_proper() =~ m/la enfermedad laboral\^ies$/
368 , "Simple descending author sorting in getRecords matches old behavior");
370 ( undef, $results_hashref, $facets_loop ) =
371 getRecords('salud', 'salud', [ 'pubdate_asc' ], [ 'biblioserver' ], '38', 0, \%branches, \%itemtypes, 'ccl', undef);
372 ok(MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[0],'UTF-8')->title_proper() eq 'Manual de higiene industrial^ies' &&
373 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[7],'UTF-8')->title_proper() =~ m/seguridad e higiene del trabajo\^ies$/ &&
374 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[18],'UTF-8')->title_proper() =~ m/^Indicadores de resultados identificados/
375 , "Simple ascending publication date sorting in getRecords matches old behavior");
377 ( undef, $results_hashref, $facets_loop ) =
378 getRecords('salud', 'salud', [ 'pubdate_dsc' ], [ 'biblioserver' ], '38', 0, \%branches, \%itemtypes, 'ccl', undef);
379 ok(MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[0],'UTF-8')->title_proper() =~ m/^Estado de salud/ &&
380 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[7],'UTF-8')->title_proper() eq 'World health statistics 2009^ien' &&
381 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[18],'UTF-8')->title_proper() eq 'Manual de higiene industrial^ies'
382 , "Simple descending publication date sorting in getRecords matches old behavior");
384 ( undef, $results_hashref, $facets_loop ) =
385 getRecords('books', 'books', [ 'relevance' ], [ 'biblioserver' ], '20', 0, \%branches, \%itemtypes, undef, 1);
386 $record = MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[0]);
387 is($record->title_proper(), 'Books', "Scan returned requested item");
388 is($record->subfield('100', 'a'), 2, "Scan returned correct number of records matching term");
389 # Time to test buildQuery and searchResults too.
391 my ( $query, $simple_query, $query_cgi,
392 $query_desc, $limit, $limit_cgi, $limit_desc,
394 ( $error, $query, $simple_query, $query_cgi,
395 $query_desc, $limit, $limit_cgi, $limit_desc,
396 $query_type ) = buildQuery([], [ 'salud' ], [], [], [], 0, 'en');
397 like($query, qr/kw\W.*salud/, "Built CCL keyword query");
399 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
400 is($results_hashref->{biblioserver}->{hits}, 19, "getRecords generated keyword search for 'salud' matched right number of records");
402 my @newresults = searchResults({'interface' => 'opac'}, $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 18, 0, 0,
403 $results_hashref->{'biblioserver'}->{"RECORDS"});
404 is(scalar @newresults,18, "searchResults returns requested number of hits");
406 ( $error, $query, $simple_query, $query_cgi,
407 $query_desc, $limit, $limit_cgi, $limit_desc,
408 $query_type ) = buildQuery([ 'AND' ], [ 'salud', 'higiene' ], [], [], [], 0, 'en');
409 like($query, qr/kw\W.*salud\W.*AND.*kw\W.*higiene/, "Built composed explicit-and CCL keyword query");
411 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
412 is($results_hashref->{biblioserver}->{hits}, 3, "getRecords generated composed keyword search for 'salud' explicit-and 'higiene' matched right number of records");
414 ( $error, $query, $simple_query, $query_cgi,
415 $query_desc, $limit, $limit_cgi, $limit_desc,
416 $query_type ) = buildQuery([ 'OR' ], [ 'salud', 'higiene' ], [], [], [], 0, 'en');
417 like($query, qr/kw\W.*salud\W.*OR.*kw\W.*higiene/, "Built composed explicit-or CCL keyword query");
419 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
420 is($results_hashref->{biblioserver}->{hits}, 20, "getRecords generated composed keyword search for 'salud' explicit-or 'higiene' matched right number of records");
422 ( $error, $query, $simple_query, $query_cgi,
423 $query_desc, $limit, $limit_cgi, $limit_desc,
424 $query_type ) = buildQuery([], [ 'salud', 'higiene' ], [], [], [], 0, 'en');
425 like($query, qr/kw\W.*salud\W.*AND.*kw\W.*higiene/, "Built composed implicit-and CCL keyword query");
427 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
428 is($results_hashref->{biblioserver}->{hits}, 3, "getRecords generated composed keyword search for 'salud' implicit-and 'higiene' matched right number of records");
430 ( $error, $query, $simple_query, $query_cgi,
431 $query_desc, $limit, $limit_cgi, $limit_desc,
432 $query_type ) = buildQuery([], [ 'salud' ], [ 'kw' ], [ 'su-to:Laboratorios' ], [], 0, 'en');
433 like($query, qr/kw\W.*salud\W*and\W*su-to\W.*Laboratorios/, "Faceted query generated correctly");
434 unlike($query_desc, qr/Laboratorios/, "Facets not included in query description");
436 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
437 is($results_hashref->{biblioserver}->{hits}, 2, "getRecords generated faceted search matched right number of records");
440 ( $error, $query, $simple_query, $query_cgi,
441 $query_desc, $limit, $limit_cgi, $limit_desc,
442 $query_type ) = buildQuery([], [ '' ], [ 'kw' ], [ 'mc-itype:MP', 'mc-itype:MU' ], [], 0, 'en');
444 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
445 is($results_hashref->{biblioserver}->{hits}, 2, "getRecords generated mc-faceted search matched right number of records");
448 ( $error, $query, $simple_query, $query_cgi,
449 $query_desc, $limit, $limit_cgi, $limit_desc,
450 $query_type ) = buildQuery([], [ '' ], [ 'kw' ], [ 'mc-loc:GEN', 'branch:FFL' ], [], 0, 'en');
452 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
453 is($results_hashref->{biblioserver}->{hits}, 2, "getRecords generated multi-faceted search matched right number of records");
455 ( $error, $query, $simple_query, $query_cgi,
456 $query_desc, $limit, $limit_cgi, $limit_desc,
457 $query_type ) = buildQuery([], [ 'NEKLS' ], [ 'Code-institution' ], [], [], 0, 'en');
458 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
459 is($results_hashref->{biblioserver}->{hits}, 12,
460 'search using index whose name contains "ns" returns expected results (bug 10271)');
462 ( $error, $query, $simple_query, $query_cgi,
463 $query_desc, $limit, $limit_cgi, $limit_desc,
464 $query_type ) = buildQuery([], [ '' ], [ 'kw' ], [ 'available' ], [], 0, 'en');
466 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
467 is($results_hashref->{biblioserver}->{hits}, 2, "getRecords generated availability-limited search matched right number of records");
469 @newresults = searchResults({'interface'=>'opac'}, $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 17, 0, 0,
470 $results_hashref->{'biblioserver'}->{"RECORDS"});
471 my $allavailable = 'true';
472 foreach my $result (@newresults) {
473 $allavailable = 'false' unless $result->{availablecount} > 0;
475 is ($allavailable, 'true', 'All records have at least one item available');
477 my $mocked_xslt = Test::MockModule->new('Koha::XSLT::Base');
478 $mocked_xslt->mock( 'transform', sub {
479 my ($self, $xml) = @_;
483 @newresults = searchResults({'interface'=>'opac'}, $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 17, 0, 0,
484 $results_hashref->{'biblioserver'}->{"RECORDS"}, { anonymous_session => 1 });
486 like( $newresults[0]->{XSLTResultsRecord}, qr/<variable name="anonymous_session">1<\/variable>/, "Variable injected correctly" );
488 my $biblio_id = $newresults[0]->{biblionumber};
489 my $fw = C4::Biblio::GetFrameworkCode($biblio_id);
491 my $dbh = C4::Context->dbh;
492 # FIXME This change is revert in END
493 # Hide subfield 'p' in OPAC
495 UPDATE marc_subfield_structure
497 WHERE frameworkcode='$fw' AND
502 # Hide subfield 'y' in Staff
504 UPDATE marc_subfield_structure
506 WHERE frameworkcode='$fw' AND
511 Koha::Caches->get_instance->flush_all;
513 @newresults = searchResults(
514 { 'interface' => 'opac' },
516 $results_hashref->{'biblioserver'}->{'hits'},
520 $results_hashref->{'biblioserver'}->{"RECORDS"}
523 unlike( $newresults[0]->{XSLTResultsRecord}, qr/<subfield code="p">TEST11111<\/subfield>/, '952\$p hidden in OPAC' );
525 @newresults = searchResults(
526 { 'interface' => 'intranet' },
528 $results_hashref->{'biblioserver'}->{'hits'},
532 $results_hashref->{'biblioserver'}->{"RECORDS"}
535 unlike( $newresults[0]->{XSLTResultsRecord}, qr/<subfield code="y">Books<\/subfield>/, '952\$y hidden on staff interface' );
537 ( $error, $query, $simple_query, $query_cgi,
538 $query_desc, $limit, $limit_cgi, $limit_desc,
539 $query_type ) = buildQuery([], [ 'pqf=@attr 1=_ALLRECORDS @attr 2=103 ""' ], [], [], [], 0, 'en');
541 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
542 is($results_hashref->{biblioserver}->{hits}, 180, "getRecords on _ALLRECORDS PQF returned all records");
544 ( $error, $query, $simple_query, $query_cgi,
545 $query_desc, $limit, $limit_cgi, $limit_desc,
546 $query_type ) = buildQuery([], [ 'pqf=@attr 1=1016 "Lessig"' ], [], [], [], 0, 'en');
548 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
549 is($results_hashref->{biblioserver}->{hits}, 4, "getRecords PQF author search for Lessig returned proper number of matches");
551 ( $error, $query, $simple_query, $query_cgi,
552 $query_desc, $limit, $limit_cgi, $limit_desc,
553 $query_type ) = buildQuery([], [ 'ccl=au:Lessig' ], [], [], [], 0, 'en');
555 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
556 is($results_hashref->{biblioserver}->{hits}, 4, "getRecords CCL author search for Lessig returned proper number of matches");
558 ( $error, $query, $simple_query, $query_cgi,
559 $query_desc, $limit, $limit_cgi, $limit_desc,
560 $query_type ) = buildQuery([], [ 'cql=dc.author any lessig' ], [], [], [], 0, 'en');
562 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
563 is($results_hashref->{biblioserver}->{hits}, 4, "getRecords CQL author search for Lessig returned proper number of matches");
565 $QueryStemming = $QueryAutoTruncate = $QueryFuzzy = 0;
566 $QueryWeightFields = 1;
567 ( $error, $query, $simple_query, $query_cgi,
568 $query_desc, $limit, $limit_cgi, $limit_desc,
569 $query_type ) = buildQuery([], [ 'salud' ], [ 'kw' ], [], [], 0, 'en');
571 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
572 is($results_hashref->{biblioserver}->{hits}, 19, "Weighted query returned correct number of results");
573 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");
575 $QueryStemming = $QueryWeightFields = $QueryFuzzy = 0;
576 $QueryAutoTruncate = 1;
577 ( $error, $query, $simple_query, $query_cgi,
578 $query_desc, $limit, $limit_cgi, $limit_desc,
579 $query_type ) = buildQuery([], [ 'medic' ], [ 'kw' ], [], [], 0, 'en');
581 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
582 is($results_hashref->{biblioserver}->{hits}, 5, "Search for 'medic' returns matches with automatic truncation on");
584 ( $error, $query, $simple_query, $query_cgi,
585 $query_desc, $limit, $limit_cgi, $limit_desc,
586 $query_type ) = buildQuery([], [ 'medic*' ], [ 'kw' ], [], [], 0, 'en');
588 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
589 is($results_hashref->{biblioserver}->{hits}, 5, "Search for 'medic*' returns matches with automatic truncation on");
591 $QueryStemming = $QueryFuzzy = $QueryAutoTruncate = 0;
592 $QueryWeightFields = 1;
593 ( $error, $query, $simple_query, $query_cgi,
594 $query_desc, $limit, $limit_cgi, $limit_desc,
595 $query_type ) = buildQuery([], [ 'web application' ], [ 'kw' ], [], [], 0, 'en');
596 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
597 is($results_hashref->{biblioserver}->{hits}, 1, "Search for 'web application' returns one hit with QueryWeightFields on");
599 ( $error, $query, $simple_query, $query_cgi,
600 $query_desc, $limit, $limit_cgi, $limit_desc,
601 $query_type ) = buildQuery([], [ 'web "application' ], [ 'kw' ], [], [], 0, 'en');
602 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
603 is($results_hashref->{biblioserver}->{hits}, 1, "Search for 'web \"application' returns one hit with QueryWeightFields on (bug 7518)");
605 $QueryStemming = $QueryWeightFields = $QueryFuzzy = $QueryAutoTruncate = 0;
606 ( $error, $query, $simple_query, $query_cgi,
607 $query_desc, $limit, $limit_cgi, $limit_desc,
608 $query_type ) = buildQuery([], [ 'medic' ], [ 'kw' ], [], [], 0, 'en');
610 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
611 is($results_hashref->{biblioserver}->{hits}, undef, "Search for 'medic' returns no matches with automatic truncation off");
613 ( $error, $query, $simple_query, $query_cgi,
614 $query_desc, $limit, $limit_cgi, $limit_desc,
615 $query_type ) = buildQuery([], [ 'medic*' ], [ 'kw' ], [], [], 0, 'en');
617 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
618 is($results_hashref->{biblioserver}->{hits}, 5, "Search for 'medic*' returns matches with automatic truncation off");
620 $QueryStemming = $QueryWeightFields = 1;
621 $QueryFuzzy = $QueryAutoTruncate = 0;
622 ( $error, $query, $simple_query, $query_cgi,
623 $query_desc, $limit, $limit_cgi, $limit_desc,
624 $query_type ) = buildQuery([], [ 'pressed' ], [ '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}, 7, "Search for 'pressed' returns matches when stemming (and query weighting) is on");
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([], [ 'pressed' ], [ '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 'pressed' returns no matches when stemming is off");
637 ( $error, $query, $simple_query, $query_cgi,
638 $query_desc, $limit, $limit_cgi, $limit_desc,
639 $query_type ) = buildQuery([], [ 'ccl=an:42' ], [], ['available'], [], 0, 'en');
640 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' );
641 is( $query_desc, 'an:42', 'buildQuery should remove the available part from the query' );
643 ( $error, $query, $simple_query, $query_cgi,
644 $query_desc, $limit, $limit_cgi, $limit_desc,
645 $query_type ) = buildQuery([], [ 0 ], [ 'su,phr' ], [], [], 0, 'en');
646 is($query, 'su,phr=(rk=(0)) ', 'buildQuery should keep 0 value in query');
647 is($query_cgi, 'idx=su%2Cphr&q=0', 'buildQuery should keep 0 value in query_cgi');
648 is($query_desc, 'su,phr: 0', 'buildQuery should keep 0 value in query_desc');
651 ( $error, $query, $simple_query, $query_cgi,
652 $query_desc, $limit, $limit_cgi, $limit_desc,
653 $query_type ) = buildQuery([], [], [], [ 'mc-ccode:NF(IC'], [], 0, 'en');
654 like($query, qr/ccode="NF\(IC"/, "Limit quoted correctly");
656 # Let's see what happens when we pass bad data into these routines.
657 # We have to catch warnings since we're not very good about returning errors.
659 warning_like { ( $error, $marcresults, $total_hits ) = SimpleSearch("@==ccl blah", 0, 9) } qr/CCL parsing error/,
660 "SimpleSearch warns about CCL parsing error with nonsense query";
661 isnt($error, undef, "SimpleSearch returns an error when passed gibberish");
663 warning_like {( undef, $results_hashref, $facets_loop ) =
664 getRecords('kw:book', 'book', [], [ 'biblioserver' ], '19', 0, \%branches, \%itemtypes, 'nonsense', undef) }
665 qr/Unknown query_type/, "getRecords warns about unknown query type";
667 warning_like {( undef, $results_hashref, $facets_loop ) =
668 getRecords('pqf=@attr 1=4 "title"', 'pqf=@attr 1=4 "title"', [], [ 'biblioserver' ], '19', 0, \%branches, \%itemtypes, '', undef) }
669 qr/WARNING: query problem/, "getRecords warns when query type is not specified for non-CCL query";
671 # Let's just test a few other bits and bobs, just for fun
673 ($error, $results_hashref, $facets_loop) = getRecords("Godzina pąsowej róży","Godzina pąsowej róży",[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
674 @newresults = searchResults({'interface'=>'intranet'}, $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 17, 0, 0,
675 $results_hashref->{'biblioserver'}->{"RECORDS"});
676 is($newresults[0]->{'alternateholdings_count'}, 1, 'Alternate holdings filled in correctly');
679 ## Regression test for Bug 10741
681 # make one of the test items appear to be in transit
682 my $circ_module = Test::MockModule->new('C4::Circulation');
683 $circ_module->mock('GetTransfers', sub {
684 my $itemnumber = shift // -1;
685 if ($itemnumber == 11) {
686 return ('2013-07-19', 'MPL', 'CPL');
692 ($error, $results_hashref, $facets_loop) = getRecords("TEST12121212","TEST12121212",[ ], [ 'biblioserver' ],20,0,\%branches,\%itemtypes,$query_type,0);
693 @newresults = searchResults({'interface'=>'intranet'}, $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 17, 0, 0,
694 $results_hashref->{'biblioserver'}->{"RECORDS"});
695 ok(!exists($newresults[0]->{norequests}), 'presence of a transit does not block hold request action (bug 10741)');
697 ## Regression test for bug 10684
698 ( undef, $results_hashref, $facets_loop ) =
699 getRecords('ti:punctuation', 'punctuation', [], [ 'biblioserver' ], '19', 0, \%branches, \%itemtypes, 'ccl', undef);
700 is($results_hashref->{biblioserver}->{hits}, 1, "search for ti:punctuation returned expected number of records");
701 warning_like { @newresults = searchResults({'intranet' => 'intranet'}, $query_desc,
702 $results_hashref->{'biblioserver'}->{'hits'}, 20, 0, 0,
703 $results_hashref->{'biblioserver'}->{"RECORDS"}) }
704 qr/^ERROR DECODING RECORD - Tag "50%" is not a valid tag/,
705 "Warning is raised correctly for invalid tags in MARC::Record";
706 is(scalar(@newresults), 0, 'a record that cannot be parsed by MARC::Record is simply skipped (bug 10684)');
708 my ($auths, $count) = SearchAuthorities(
709 ['mainentry'], ['and'], [''], ['starts'],
710 ['shakespeare'], 0, 10, '', '', 1
712 is($count, 1, 'MARC21 authorities: one hit on mainentry starts with "shakespeare"');
713 ($auths, $count) = SearchAuthorities(
714 ['mainentry'], ['and'], [''], ['starts'],
715 ['shakespeare'], 0, 10, '', 'HeadingAsc', 1
717 is($count, 1, 'MARC21 authorities: one hit on mainentry starts with "shakespeare" sorted by heading ascending');
718 ($auths, $count) = SearchAuthorities(
719 ['mainentry'], ['and'], [''], ['starts'],
720 ['shakespeare'], 0, 10, '', 'HeadingDsc', 1
722 is($count, 1, 'MARC21 authorities: one hit on mainentry starts with "shakespeare" sorted by heading descending');
723 ($auths, $count) = SearchAuthorities(
724 ['match'], ['and'], [''], ['contains'],
725 ['沙士北亞威廉姆'], 0, 10, '', '', 1
727 is($count, 1, 'MARC21 authorities: one hit on match contains "沙士北亞威廉姆"');
728 ($auths, $count) = SearchAuthorities(
729 ['LC-card-number'], ['and'], [''], ['contains'],
730 ['99282477'], 0, 10, '', '', 1
732 is($count, 1, 'MARC21 authorities: one hit on LC-card-number contains "99282477"');
733 ($auths, $count) = SearchAuthorities(
734 ['all'], ['and'], [''], ['contains'],
735 ['professional wrestler'], 0, 10, '', '', 1
737 is($count, 1, 'MARC21 authorities: one hit on "all" (entire record) contains "professional wrestler"');
739 # retrieve records that are larger than the MARC limit of 99,999 octets
740 ( undef, $results_hashref, $facets_loop ) =
741 getRecords('ti:marc the large record', '', [], [ 'biblioserver' ], '20', 0, \%branches, \%itemtypes, 'ccl', undef);
742 is($results_hashref->{biblioserver}->{hits}, 1, "Can do a search that retrieves an over-large bib record (bug 11096)");
743 @newresults = searchResults({'interface' =>'opac'}, $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 10, 0, 0,
744 $results_hashref->{'biblioserver'}->{"RECORDS"});
745 is($newresults[0]->{title}, 'Marc the Large Record', 'Able to render the title for over-large bib record (bug 11096)');
746 is($newresults[0]->{biblionumber}, '300', 'Over-large bib record has the correct biblionumber (bug 11096)');
747 like($newresults[0]->{notes}, qr/This is large note #550/, 'Able to render the notes field for over-large bib record (bug 11096)');
749 # notforloancount should be returned as part of searchResults output
750 ok( defined $newresults[0]->{notforloancount},
751 '\'notforloancount\' defined in searchResults output (Bug 12419)');
752 is( $newresults[0]->{notforloancount}, 2,
753 '\'notforloancount\' == 2 (Bug 12419)');
755 # verify that we don't attempt to sort if no results were returned
756 # because of a query error
757 warning_like {( undef, $results_hashref, $facets_loop ) =
758 getRecords('ccl=( AND )', '', ['title_az'], [ 'biblioserver' ], '20', 0, \%branches, \%itemtypes, 'ccl', undef)
759 } qr/WARNING: query problem with/, 'got warning instead of crash when attempting to run invalid query (bug 9578)';
761 # Test facet calculation
762 my $facets_counter = {};
763 my $facets = C4::Koha::getFacets();
764 # Create a record with a 100$z field
765 my $marc_record = MARC::Record->new;
766 $marc_record->add_fields(
768 [ '100', ' ', ' ', a => 'Cohen Arazi, Tomas' ],
769 [ '100', 'z', ' ', a => 'Tomasito' ],
770 [ '245', ' ', ' ', a => 'First try' ]
772 C4::Search::_get_facets_data_from_record( $marc_record, $facets, $facets_counter );
773 is_deeply( { au => { 'Cohen Arazi, Tomas' => 1 } }, $facets_counter,
774 "_get_facets_data_from_record doesn't count 100\$z (Bug 12788)");
775 $marc_record = MARC::Record->new;
776 $marc_record->add_fields(
778 [ '100', ' ', ' ', a => 'Cohen Arazi, Tomas' ],
779 [ '100', 'z', ' ', a => 'Tomasito' ],
780 [ '245', ' ', ' ', a => 'Second try' ]
782 C4::Search::_get_facets_data_from_record( $marc_record, $facets, $facets_counter );
783 is_deeply( { au => { 'Cohen Arazi, Tomas' => 2 } }, $facets_counter,
784 "_get_facets_data_from_record correctly counts author facet twice");
786 # Test _get_facets_info
787 my $facets_info = C4::Search::_get_facets_info( $facets );
788 my $expected_facets_info_marc21 = {
789 'au' => { 'label_value' => "Authors" },
790 'ccode' => { 'label_value' => "CollectionCodes" },
791 'holdingbranch' => { 'label_value' => "HoldingLibrary" },
792 'itype' => { 'label_value' => "ItemTypes" },
793 'location' => { 'label_value' => "Location" },
794 'se' => { 'label_value' => "Series" },
795 'su-geo' => { 'label_value' => "Places" },
796 'su-to' => { 'label_value' => "Topics" },
797 'su-ut' => { 'label_value' => "Titles" }
799 delete $expected_facets_info_marc21->{holdingbranch}
800 if Koha::Libraries->count == 1;
801 is_deeply( $facets_info, $expected_facets_info_marc21,
802 "_get_facets_info returns the correct data");
804 $mock_zebra->cleanup;
807 sub run_unimarc_search_tests {
809 $marcflavour = 'UNIMARC';
810 my $mock_zebra = t::lib::Mocks::Zebra->new({marcflavour => $marcflavour});
811 push @cleanup, $mock_zebra;
813 mock_GetMarcSubfieldStructure($marcflavour);
815 use_ok('C4::Search', qw( getIndexes FindDuplicate SimpleSearch getRecords buildQuery searchResults ));
817 # set search syspreferences to a known starting point
819 $QueryAutoTruncate = 0;
820 $QueryWeightFields = 0;
823 my $sourcedir = dirname(__FILE__) . "/data";
824 $mock_zebra->load_records(
825 sprintf( "%s/%s/zebraexport/biblio", $sourcedir, lc($marcflavour) ),
826 'iso2709', 'biblios', 1 );
827 $mock_zebra->load_records(
829 "%s/%s/zebraexport/large_biblio",
830 $sourcedir, lc($marcflavour)
832 'marcxml', 'biblios', 0
834 $mock_zebra->load_records(
835 sprintf( "%s/%s/zebraexport/authority", $sourcedir, lc($marcflavour) ),
836 'iso2709', 'authorities', 1
838 $mock_zebra->launch_zebra;
840 my ( $error, $marcresults, $total_hits ) = SimpleSearch("ti=Järnvägarnas efterfrågan och den svenska industrin", 0, 10);
841 is($total_hits, 1, 'UNIMARC title search');
842 ( $error, $marcresults, $total_hits ) = SimpleSearch("ta=u", 0, 10);
843 is($total_hits, 1, 'UNIMARC target audience = u');
844 ( $error, $marcresults, $total_hits ) = SimpleSearch("ta=k", 0, 10);
845 is($total_hits, 4, 'UNIMARC target audience = k');
846 ( $error, $marcresults, $total_hits ) = SimpleSearch("ta=m", 0, 10);
847 is($total_hits, 3, 'UNIMARC target audience = m');
848 ( $error, $marcresults, $total_hits ) = SimpleSearch("item=EXCLU DU PRET", 0, 10);
849 is($total_hits, 1, 'UNIMARC generic item index (bug 10037)');
852 use_ok('C4::AuthoritiesMarc', qw( SearchAuthorities ));
854 my ($auths, $count) = SearchAuthorities(
855 ['mainentry'], ['and'], [''], ['contains'],
856 ['wil'], 0, 10, '', '', 1
858 is($count, 11, 'UNIMARC authorities: hits on mainentry contains "wil"');
859 ($auths, $count) = SearchAuthorities(
860 ['match'], ['and'], [''], ['contains'],
861 ['wil'], 0, 10, '', '', 1
863 is($count, 11, 'UNIMARC authorities: hits on match contains "wil"');
864 ($auths, $count) = SearchAuthorities(
865 ['mainentry'], ['and'], [''], ['contains'],
866 ['michel'], 0, 20, '', '', 1
868 is($count, 14, 'UNIMARC authorities: hits on mainentry contains "michel"');
869 ($auths, $count) = SearchAuthorities(
870 ['mainmainentry'], ['and'], [''], ['exact'],
871 ['valley'], 0, 20, '', '', 1
873 is($count, 1, 'UNIMARC authorities: hits on mainmainentry = "valley"');
874 ($auths, $count) = SearchAuthorities(
875 ['mainmainentry'], ['and'], [''], ['exact'],
876 ['vall'], 0, 20, '', '', 1
878 is($count, 0, 'UNIMARC authorities: no hits on mainmainentry = "vall"');
879 ($auths, $count) = SearchAuthorities(
880 ['Any'], ['and'], [''], ['starts'],
881 ['jean'], 0, 30, '', '', 1
883 is($count, 24, 'UNIMARC authorities: hits on any starts with "jean"');
885 # Test _get_facets_info
886 my $facets = C4::Koha::getFacets();
887 my $facets_info = C4::Search::_get_facets_info( $facets );
888 my $expected_facets_info_unimarc = {
889 'au' => { 'label_value' => "Authors" },
890 'ccode' => { 'label_value' => "CollectionCodes" },
891 'holdingbranch' => { 'label_value' => "HoldingLibrary" },
892 'location' => { 'label_value' => "Location" },
893 'se' => { 'label_value' => "Series" },
894 'su-geo' => { 'label_value' => "Places" },
895 'su-to' => { 'label_value' => "Topics" }
897 delete $expected_facets_info_unimarc->{holdingbranch}
898 if Koha::Libraries->count == 1;
899 is_deeply( $facets_info, $expected_facets_info_unimarc,
900 "_get_facets_info returns the correct data");
902 $mock_zebra->cleanup;
905 subtest 'MARC21 + DOM' => sub {
907 run_marc21_search_tests();
910 subtest 'UNIMARC + DOM' => sub {
912 run_unimarc_search_tests();
916 subtest 'FindDuplicate' => sub {
918 Koha::Caches->get_instance('config')->flush_all;
919 t::lib::Mocks::mock_preference('marcflavour', 'MARC21' );
920 mock_GetMarcSubfieldStructure('MARC21');
921 my $z_searcher = Test::MockModule->new('C4::Search');
922 $z_searcher->mock('SimpleSearch', sub {
926 my $e_searcher = Test::MockModule->new('Koha::SearchEngine::Elasticsearch::Search');
927 $e_searcher->mock('simple_search_compat', sub {
933 my $record_1 = MARC::Record->new;
934 $record_1 ->add_fields(
935 [ '100', '0', '0', a => 'Morgenstern, Erin' ],
936 [ '245', '0', '0', a => 'The night circus /' ]
938 my $record_2 = MARC::Record->new;
939 $record_2 ->add_fields(
940 [ '245', '0', '0', a => 'The book of nothing /' ]
942 my $record_3 = MARC::Record->new;
943 $record_3->add_fields(
944 [ '245', '0', '0', a => 'Frog and toad all year /' ]
947 foreach my $engine ('Zebra','Elasticsearch'){
948 t::lib::Mocks::mock_preference('searchEngine', $engine );
950 warning_is { C4::Search::FindDuplicate($record_1);}
951 q/ti,ext:"The night circus \/" AND au,ext:"Morgenstern, Erin"/,"Term correctly formed and passed to $engine";
953 warning_is { C4::Search::FindDuplicate($record_2);}
954 q/ti,ext:"The book of nothing \/"/,"Term correctly formed and passed to $engine";
956 warning_is { C4::Search::FindDuplicate($record_3);}
957 q/ti,ext:"Frog and toad all year \/"/,"Term correctly formed and passed to $engine";
962 # Make sure that following tests are not using our config settings
963 Koha::Caches->get_instance('config')->flush_all;
967 $_->cleanup for @cleanup;
968 my $dbh = C4::Context->dbh;
969 # Restore visibility of subfields in OPAC
971 UPDATE marc_subfield_structure
973 WHERE tagfield=952 AND
974 ( tagsubfield IN ('p', 'y') )
977 Koha::Caches->get_instance->flush_all;