3 # This Koha test module is a stub!
4 # Add more tests here!!!
15 use Test::More tests => 172;
22 use File::Temp qw/ tempdir /;
29 sub index_sample_records_and_launch_zebra {
30 my ($datadir, $indexing_mode, $marc_type) = @_;
32 my $sourcedir = dirname(__FILE__) . "/data";
33 unlink("$datadir/zebra.log");
34 my $zebra_bib_cfg = ($indexing_mode eq 'dom') ? 'zebra-biblios-dom.cfg' : 'zebra-biblios.cfg';
35 system("zebraidx -c $datadir/etc/koha/zebradb/$zebra_bib_cfg -v none,fatal,warn -g iso2709 -d biblios init");
36 system("zebraidx -c $datadir/etc/koha/zebradb/$zebra_bib_cfg -v none,fatal,warn -g iso2709 -d biblios update $sourcedir/${marc_type}/zebraexport/biblio");
37 system("zebraidx -c $datadir/etc/koha/zebradb/$zebra_bib_cfg -v none,fatal,warn -g iso2709 -d biblios commit");
41 exec("zebrasrv -f $datadir/etc/koha-conf.xml -v none,request -l $datadir/zebra.log");
52 # Clean up the Zebra files since the child process was just shot
57 # Fall back to make sure that the Zebra process
58 # and files get cleaned up
63 our $QueryStemming = 0;
64 our $QueryAutoTruncate = 0;
65 our $QueryWeightFields = 0;
67 our $QueryRemoveStopwords = 0;
68 our $UseQueryParser = 0;
69 our $marcflavour = 'MARC21';
70 our $contextmodule = new Test::MockModule('C4::Context');
71 $contextmodule->mock('_new_dbh', sub {
72 my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
73 || die "Cannot create handle: $DBI::errstr\n";
75 $contextmodule->mock('preference', sub {
76 my ($self, $pref) = @_;
77 if ($pref eq 'marcflavour') {
79 } elsif ($pref eq 'QueryStemming') {
80 return $QueryStemming;
81 } elsif ($pref eq 'QueryAutoTruncate') {
82 return $QueryAutoTruncate;
83 } elsif ($pref eq 'QueryWeightFields') {
84 return $QueryWeightFields;
85 } elsif ($pref eq 'QueryFuzzy') {
87 } elsif ($pref eq 'QueryRemoveStopwords') {
88 return $QueryRemoveStopwords;
89 } elsif ($pref eq 'UseQueryParser') {
90 return $UseQueryParser;
91 } elsif ($pref eq 'maxRecordsForFacets') {
93 } elsif ($pref eq 'FacetLabelTruncationLength') {
95 } elsif ($pref eq 'OpacHiddenItems') {
97 } elsif ($pref eq 'AlternateHoldingsField') {
100 warn "The syspref $pref was requested but I don't know what to say; this indicates that the test requires updating"
101 unless $pref =~ m/(XSLT|item|branch|holding|image)/i;
105 $contextmodule->mock('queryparser', sub {
106 my $QParser = Koha::QueryParser::Driver::PQF->new();
107 $QParser->load_config("$datadir/etc/searchengine/queryparser.yaml");
111 sub mock_marcfromkohafield {
112 my $marc_type = shift;
113 if ($marc_type eq 'marc21') {
114 $contextmodule->mock('marcfromkohafield', sub {
117 'biblio.biblionumber' => [ '999', 'c' ],
118 'items.barcode' => ['952', 'p' ],
119 'items.booksellerid' => ['952', 'e' ],
120 'items.ccode' => ['952', '8' ],
121 'items.cn_sort' => ['952', '6' ],
122 'items.cn_source' => ['952', '2' ],
123 'items.coded_location_qualifier' => ['952', 'f' ],
124 'items.copynumber' => ['952', 't' ],
125 'items.damaged' => ['952', '4' ],
126 'items.dateaccessioned' => ['952', 'd' ],
127 'items.datelastborrowed' => ['952', 's' ],
128 'items.datelastseen' => ['952', 'r' ],
129 'items.enumchron' => ['952', 'h' ],
130 'items.holdingbranch' => ['952', 'b' ],
131 'items.homebranch' => ['952', 'a' ],
132 'items.issues' => ['952', 'l' ],
133 'items.itemcallnumber' => ['952', 'o' ],
134 'items.itemlost' => ['952', '1' ],
135 'items.itemnotes' => ['952', 'z' ],
136 'items.itemnumber' => ['952', '9' ],
137 'items.itype' => ['952', 'y' ],
138 'items.location' => ['952', 'c' ],
139 'items.materials' => ['952', '3' ],
140 'items.nonpublicnote' => ['952', 'x' ],
141 'items.notforloan' => ['952', '7' ],
142 'items.onloan' => ['952', 'q' ],
143 'items.price' => ['952', 'g' ],
144 'items.renewals' => ['952', 'm' ],
145 'items.replacementprice' => ['952', 'v' ],
146 'items.replacementpricedate' => ['952', 'w' ],
147 'items.reserves' => ['952', 'n' ],
148 'items.restricted' => ['952', '5' ],
149 'items.stack' => ['952', 'j' ],
150 'items.uri' => ['952', 'u' ],
151 'items.withdrawn' => ['952', '0' ]
158 sub run_marc21_search_tests {
159 my $indexing_mode = shift;
160 $datadir = tempdir();
161 system(dirname(__FILE__) . "/zebra_config.pl $datadir marc21 $indexing_mode");
163 mock_marcfromkohafield('marc21');
164 my $context = new C4::Context("$datadir/etc/koha-conf.xml");
165 $context->set_context();
167 use_ok('C4::Search');
169 # set search syspreferences to a known starting point
171 $QueryAutoTruncate = 0;
172 $QueryWeightFields = 0;
174 $QueryRemoveStopwords = 0;
176 $marcflavour = 'MARC21';
178 foreach my $string ("Leçon","modèles") {
179 my @results=C4::Search::_remove_stopwords($string,"kw");
180 $debug && warn "$string ",Dump(@results);
181 ok($results[0] eq $string,"$string is not modified");
184 foreach my $string ("A book about the stars") {
185 my @results=C4::Search::_remove_stopwords($string,"kw");
186 $debug && warn "$string ",Dump(@results);
187 ok($results[0] ne $string,"$results[0] from $string");
190 my $indexes = C4::Search::getIndexes();
191 is(scalar(grep(/^ti$/, @$indexes)), 1, "Title index supported");
193 my $bibliomodule = new Test::MockModule('C4::Biblio');
194 $bibliomodule->mock('_get_inverted_marc_field_map', sub {
197 '245' => { 'sfs' => { 'a' => [ [ 'biblio', 'title' ] ], 'b' => [ [ 'bibliosubtitle', 'subtitle' ] ] },
198 'list' => [ [ 'a', 'biblio', 'title' ], [ 'b', 'bibliosubtitle', 'subtitle' ] ]
201 'sfs' => { 'a' => [ [ 'biblio', 'author' ] ] },
202 'list' => [ [ 'a', 'biblio', 'author' ] ]
205 'sfs' => { 'c' => [ [ 'biblio', 'biblionumber' ] ], 'd' => [ [ 'biblioitems', 'biblioitemnumber' ] ] },
206 'list' => [ [ 'd', 'biblioitems', 'biblioitemnumber' ], [ 'c', 'biblio', 'biblionumber' ] ]
209 'sfs' => { 'a' => [ [ 'biblioitems', 'isbn' ] ] },
210 'list' => [ [ 'a', 'biblioitems', 'isbn' ] ]
216 my $dbh = C4::Context->dbh;
217 $dbh->{mock_add_resultset} = {
218 sql => 'SHOW COLUMNS FROM items',
220 [ 'rows' ], # seems like $sth->rows is getting called
221 # implicitly, so we need this to make
222 # DBD::Mock return all of the results
223 [ 'itemnumber' ], [ 'biblionumber' ], [ 'biblioitemnumber' ],
224 [ 'barcode' ], [ 'dateaccessioned' ], [ 'booksellerid' ],
225 [ 'homebranch' ], [ 'price' ], [ 'replacementprice' ],
226 [ 'replacementpricedate' ], [ 'datelastborrowed' ], [ 'datelastseen' ],
227 [ 'stack' ], [ 'notforloan' ], [ 'damaged' ],
228 [ 'itemlost' ], [ 'withdrawn' ], [ 'itemcallnumber' ],
229 [ 'issues' ], [ 'renewals' ], [ 'reserves' ],
230 [ 'restricted' ], [ 'itemnotes' ], [ 'nonpublicnote' ],
231 [ 'holdingbranch' ], [ 'paidfor' ], [ 'timestamp' ],
232 [ 'location' ], [ 'permanent_location' ], [ 'onloan' ],
233 [ 'cn_source' ], [ 'cn_sort' ], [ 'ccode' ],
234 [ 'materials' ], [ 'uri' ], [ 'itype' ],
235 [ 'more_subfields_xml' ], [ 'enumchron' ], [ 'copynumber' ],
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.gif', 'summary' => '', 'itemtype' => 'BK', 'description' => 'Books' },
257 'CF' => { 'imageurl' => 'bridge/computer_file.gif', 'summary' => '', 'itemtype' => 'CF', 'description' => 'Computer Files' },
258 'CR' => { 'imageurl' => 'bridge/periodical.gif', 'summary' => '', 'itemtype' => 'CR', 'description' => 'Continuing Resources' },
259 'MP' => { 'imageurl' => 'bridge/map.gif', 'summary' => '', 'itemtype' => 'MP', 'description' => 'Maps' },
260 'MU' => { 'imageurl' => 'bridge/sound.gif', 'summary' => '', 'itemtype' => 'MU', 'description' => 'Music' },
261 'MX' => { 'imageurl' => 'bridge/kit.gif', 'summary' => '', 'itemtype' => 'MX', 'description' => 'Mixed Materials' },
262 'REF' => { 'imageurl' => '', 'summary' => '', 'itemtype' => 'REF', 'description' => 'Reference' },
263 'VM' => { 'imageurl' => 'bridge/dvd.gif', 'summary' => '', 'itemtype' => 'VM', 'description' => 'Visual Materials' },
266 index_sample_records_and_launch_zebra($datadir, $indexing_mode, 'marc21');
268 my ($biblionumber, $title);
269 my $record = MARC::Record->new;
272 [ '020', ' ', ' ', a => '9788522421718' ],
273 [ '245', '0', '0', a => 'Administração da produção /' ]
275 ($biblionumber,undef,$title) = FindDuplicate($record);
276 is($biblionumber, 51, 'Found duplicate with ISBN');
278 $record = MARC::Record->new;
281 [ '100', '1', ' ', a => 'Carter, Philip J.' ],
282 [ '245', '1', '4', a => 'Test your emotional intelligence :' ]
284 ($biblionumber,undef,$title) = FindDuplicate($record);
285 is($biblionumber, 203, 'Found duplicate with author/title');
287 # Testing SimpleSearch
289 my ( $error, $marcresults, $total_hits ) = SimpleSearch("book", 0, 9);
291 is(scalar @$marcresults, 9, "SimpleSearch retrieved requested number of records");
292 is($total_hits, 101, "SimpleSearch for 'book' matched right number of records");
293 is($error, undef, "SimpleSearch does not return an error when successful");
296 ( $error, $marcresults2, $total_hits ) = SimpleSearch("book", 5, 5);
297 is($marcresults->[5], $marcresults2->[0], "SimpleSearch cursor functions");
299 ( $error, $marcresults, $total_hits ) = SimpleSearch("kw=book", 0, 10);
300 is($total_hits, 101, "SimpleSearch handles simple CCL");
302 ( $error, $marcresults, $total_hits ) = SimpleSearch("Music-number=49631-2", 0, 10);
303 is($total_hits, 1, "SimpleSearch on music publisher number works (bug 8252)");
304 ( $error, $marcresults, $total_hits ) = SimpleSearch("Identifier-publisher-for-music=49631-2", 0, 10);
305 is($total_hits, 1, "SimpleSearch on music publisher number works using Identifier-publisher-for-music (bug 8252)");
311 ( undef, $results_hashref, $facets_loop ) =
312 getRecords('kw:book', 'book', [], [ 'biblioserver' ], '19', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
313 is($results_hashref->{biblioserver}->{hits}, 101, "getRecords keyword search for 'book' matched right number of records");
314 is(scalar @{$results_hashref->{biblioserver}->{RECORDS}}, 19, "getRecords returned requested number of records");
315 my $record5 = $results_hashref->{biblioserver}->{RECORDS}->[5];
316 ( undef, $results_hashref, $facets_loop ) =
317 getRecords('kw:book', 'book', [], [ 'biblioserver' ], '20', 5, undef, \%branches, \%itemtypes, 'ccl', undef);
318 ok(!defined $results_hashref->{biblioserver}->{RECORDS}->[0] &&
319 !defined $results_hashref->{biblioserver}->{RECORDS}->[1] &&
320 !defined $results_hashref->{biblioserver}->{RECORDS}->[2] &&
321 !defined $results_hashref->{biblioserver}->{RECORDS}->[3] &&
322 !defined $results_hashref->{biblioserver}->{RECORDS}->[4] &&
323 $results_hashref->{biblioserver}->{RECORDS}->[5] eq $record5, "getRecords cursor works");
325 ( undef, $results_hashref, $facets_loop ) =
326 getRecords('ti:book', 'ti:book', [], [ 'biblioserver' ], '20', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
327 is($results_hashref->{biblioserver}->{hits}, 11, "getRecords title search for 'book' matched right number of records");
329 ( undef, $results_hashref, $facets_loop ) =
330 getRecords('au:Lessig', 'au:Lessig', [], [ 'biblioserver' ], '20', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
331 is($results_hashref->{biblioserver}->{hits}, 4, "getRecords title search for 'Australia' matched right number of records");
333 ( undef, $results_hashref, $facets_loop ) =
334 getRecords('salud', 'salud', [], [ 'biblioserver' ], '19', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
335 ok(MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[0])->title_proper() =~ m/^Efectos del ambiente/ &&
336 MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[7])->title_proper() eq 'Salud y seguridad de los trabajadores del sector salud: manual para gerentes y administradores^ies' &&
337 MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[18])->title_proper() =~ m/^Indicadores de resultados identificados/
338 , "Simple relevance sorting in getRecords matches old behavior");
340 ( undef, $results_hashref, $facets_loop ) =
341 getRecords('salud', 'salud', [ 'author_az' ], [ 'biblioserver' ], '38', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
342 ok(MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[0])->title_proper() =~ m/la enfermedad laboral\^ies$/ &&
343 MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[6])->title_proper() =~ m/^Indicadores de resultados identificados/ &&
344 MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[18])->title_proper() eq 'World health statistics 2009^ien'
345 , "Simple ascending author sorting in getRecords matches old behavior");
347 ( undef, $results_hashref, $facets_loop ) =
348 getRecords('salud', 'salud', [ 'author_za' ], [ 'biblioserver' ], '38', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
349 ok(MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[0])->title_proper() eq 'World health statistics 2009^ien' &&
350 MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[12])->title_proper() =~ m/^Indicadores de resultados identificados/ &&
351 MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[18])->title_proper() =~ m/la enfermedad laboral\^ies$/
352 , "Simple descending author sorting in getRecords matches old behavior");
354 ( undef, $results_hashref, $facets_loop ) =
355 getRecords('salud', 'salud', [ 'pubdate_asc' ], [ 'biblioserver' ], '38', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
356 ok(MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[0])->title_proper() eq 'Manual de higiene industrial^ies' &&
357 MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[7])->title_proper() =~ m/seguridad e higiene del trabajo\^ies$/ &&
358 MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[18])->title_proper() =~ m/^Indicadores de resultados identificados/
359 , "Simple ascending publication date sorting in getRecords matches old behavior");
361 ( undef, $results_hashref, $facets_loop ) =
362 getRecords('salud', 'salud', [ 'pubdate_dsc' ], [ 'biblioserver' ], '38', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
363 ok(MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[0])->title_proper() =~ m/^Estado de salud/ &&
364 MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[7])->title_proper() eq 'World health statistics 2009^ien' &&
365 MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[18])->title_proper() eq 'Manual de higiene industrial^ies'
366 , "Simple descending publication date sorting in getRecords matches old behavior");
368 ( undef, $results_hashref, $facets_loop ) =
369 getRecords('books', 'books', [ 'relevance' ], [ 'biblioserver' ], '20', 0, undef, \%branches, \%itemtypes, undef, 1);
370 $record = MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[0]);
371 is($record->title_proper(), 'books', "Scan returned requested item");
372 is($record->subfield('100', 'a'), 2, "Scan returned correct number of records matching term");
374 # Time to test buildQuery and searchResults too.
376 my ( $query, $simple_query, $query_cgi,
377 $query_desc, $limit, $limit_cgi, $limit_desc,
378 $stopwords_removed, $query_type );
379 ( $error, $query, $simple_query, $query_cgi,
380 $query_desc, $limit, $limit_cgi, $limit_desc,
381 $stopwords_removed, $query_type ) = buildQuery([], [ 'salud' ], [], [], [], 0, 'en');
382 like($query, qr/kw\W.*salud/, "Built CCL keyword query");
384 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
385 is($results_hashref->{biblioserver}->{hits}, 19, "getRecords generated keyword search for 'salud' matched right number of records");
387 my @newresults = searchResults('opac', $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 18, 0, 0,
388 $results_hashref->{'biblioserver'}->{"RECORDS"});
389 is(scalar @newresults,18, "searchResults returns requested number of hits");
391 ( $error, $query, $simple_query, $query_cgi,
392 $query_desc, $limit, $limit_cgi, $limit_desc,
393 $stopwords_removed, $query_type ) = buildQuery([ 'and' ], [ 'salud', 'higiene' ], [], [], [], 0, 'en');
394 like($query, qr/kw\W.*salud\W.*and.*kw\W.*higiene/, "Built composed explicit-and CCL keyword query");
396 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
397 is($results_hashref->{biblioserver}->{hits}, 3, "getRecords generated composed keyword search for 'salud' explicit-and 'higiene' matched right number of records");
399 ( $error, $query, $simple_query, $query_cgi,
400 $query_desc, $limit, $limit_cgi, $limit_desc,
401 $stopwords_removed, $query_type ) = buildQuery([ 'or' ], [ 'salud', 'higiene' ], [], [], [], 0, 'en');
402 like($query, qr/kw\W.*salud\W.*or.*kw\W.*higiene/, "Built composed explicit-or CCL keyword query");
404 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
405 is($results_hashref->{biblioserver}->{hits}, 20, "getRecords generated composed keyword search for 'salud' explicit-or 'higiene' matched right number of records");
407 ( $error, $query, $simple_query, $query_cgi,
408 $query_desc, $limit, $limit_cgi, $limit_desc,
409 $stopwords_removed, $query_type ) = buildQuery([], [ 'salud', 'higiene' ], [], [], [], 0, 'en');
410 like($query, qr/kw\W.*salud\W.*and.*kw\W.*higiene/, "Built composed implicit-and CCL keyword query");
412 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
413 is($results_hashref->{biblioserver}->{hits}, 3, "getRecords generated composed keyword search for 'salud' implicit-and 'higiene' matched right number of records");
415 ( $error, $query, $simple_query, $query_cgi,
416 $query_desc, $limit, $limit_cgi, $limit_desc,
417 $stopwords_removed, $query_type ) = buildQuery([], [ 'salud' ], [ 'kw' ], [ 'su-to:Laboratorios' ], [], 0, 'en');
418 like($query, qr/kw\W.*salud\W*and\W*su-to\W.*Laboratorios/, "Faceted query generated correctly");
419 unlike($query_desc, qr/Laboratorios/, "Facets not included in query description");
421 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
422 is($results_hashref->{biblioserver}->{hits}, 2, "getRecords generated faceted search matched right number of records");
425 ( $error, $query, $simple_query, $query_cgi,
426 $query_desc, $limit, $limit_cgi, $limit_desc,
427 $stopwords_removed, $query_type ) = buildQuery([], [ '' ], [ 'kw' ], [ 'mc-itype:MP', 'mc-itype:MU' ], [], 0, 'en');
429 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
430 is($results_hashref->{biblioserver}->{hits}, 2, "getRecords generated mc-faceted search matched right number of records");
433 ( $error, $query, $simple_query, $query_cgi,
434 $query_desc, $limit, $limit_cgi, $limit_desc,
435 $stopwords_removed, $query_type ) = buildQuery([], [ '' ], [ 'kw' ], [ 'mc-loc:GEN', 'branch:FFL' ], [], 0, 'en');
437 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
438 is($results_hashref->{biblioserver}->{hits}, 2, "getRecords generated multi-faceted search matched right number of records");
441 # FIXME: the availability limit does not actually work, so for the moment we
442 # are just checking that it behaves consistently
443 ( $error, $query, $simple_query, $query_cgi,
444 $query_desc, $limit, $limit_cgi, $limit_desc,
445 $stopwords_removed, $query_type ) = buildQuery([], [ '' ], [ 'kw' ], [ 'available' ], [], 0, 'en');
447 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
448 is($results_hashref->{biblioserver}->{hits}, 26, "getRecords generated availability-limited search matched right number of records");
450 @newresults = searchResults('opac', $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 17, 0, 0,
451 $results_hashref->{'biblioserver'}->{"RECORDS"});
452 my $allavailable = 'true';
453 foreach my $result (@newresults) {
454 $allavailable = 'false' unless $result->{availablecount} > 0;
456 is ($allavailable, 'true', 'All records have at least one item available');
459 ( $error, $query, $simple_query, $query_cgi,
460 $query_desc, $limit, $limit_cgi, $limit_desc,
461 $stopwords_removed, $query_type ) = buildQuery([], [ 'pqf=@attr 1=_ALLRECORDS @attr 2=103 ""' ], [], [], [], 0, 'en');
463 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
464 is($results_hashref->{biblioserver}->{hits}, 178, "getRecords on _ALLRECORDS PQF returned all records");
466 ( $error, $query, $simple_query, $query_cgi,
467 $query_desc, $limit, $limit_cgi, $limit_desc,
468 $stopwords_removed, $query_type ) = buildQuery([], [ 'pqf=@attr 1=1016 "Lessig"' ], [], [], [], 0, 'en');
470 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
471 is($results_hashref->{biblioserver}->{hits}, 4, "getRecords PQF author search for Lessig returned proper number of matches");
473 ( $error, $query, $simple_query, $query_cgi,
474 $query_desc, $limit, $limit_cgi, $limit_desc,
475 $stopwords_removed, $query_type ) = buildQuery([], [ 'ccl=au:Lessig' ], [], [], [], 0, 'en');
477 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
478 is($results_hashref->{biblioserver}->{hits}, 4, "getRecords CCL author search for Lessig returned proper number of matches");
480 ( $error, $query, $simple_query, $query_cgi,
481 $query_desc, $limit, $limit_cgi, $limit_desc,
482 $stopwords_removed, $query_type ) = buildQuery([], [ 'cql=dc.author any lessig' ], [], [], [], 0, 'en');
484 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
485 is($results_hashref->{biblioserver}->{hits}, 4, "getRecords CQL author search for Lessig returned proper number of matches");
487 $QueryStemming = $QueryAutoTruncate = $QueryFuzzy = $QueryRemoveStopwords = 0;
488 $QueryWeightFields = 1;
489 ( $error, $query, $simple_query, $query_cgi,
490 $query_desc, $limit, $limit_cgi, $limit_desc,
491 $stopwords_removed, $query_type ) = buildQuery([], [ 'salud' ], [ 'kw' ], [], [], 0, 'en');
493 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
494 is($results_hashref->{biblioserver}->{hits}, 19, "Weighted query returned correct number of results");
495 if ($indexing_mode eq 'grs1') {
496 is(MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[0])->title_proper(), 'Salud y seguridad de los trabajadores del sector salud: manual para gerentes y administradores^ies', "Weighted query returns best match first");
498 local $TODO = "Query weighting does not behave exactly the same in DOM vs. GRS";
499 is(MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[0])->title_proper(), 'Salud y seguridad de los trabajadores del sector salud: manual para gerentes y administradores^ies', "Weighted query returns best match first");
502 $QueryStemming = $QueryWeightFields = $QueryFuzzy = $QueryRemoveStopwords = 0;
503 $QueryAutoTruncate = 1;
504 ( $error, $query, $simple_query, $query_cgi,
505 $query_desc, $limit, $limit_cgi, $limit_desc,
506 $stopwords_removed, $query_type ) = buildQuery([], [ 'medic' ], [ 'kw' ], [], [], 0, 'en');
508 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
509 is($results_hashref->{biblioserver}->{hits}, 5, "Search for 'medic' returns matches with automatic truncation on");
511 ( $error, $query, $simple_query, $query_cgi,
512 $query_desc, $limit, $limit_cgi, $limit_desc,
513 $stopwords_removed, $query_type ) = buildQuery([], [ 'medic*' ], [ 'kw' ], [], [], 0, 'en');
515 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
516 is($results_hashref->{biblioserver}->{hits}, 5, "Search for 'medic*' returns matches with automatic truncation on");
518 $QueryStemming = $QueryWeightFields = $QueryFuzzy = $QueryRemoveStopwords = $QueryAutoTruncate = 0;
519 ( $error, $query, $simple_query, $query_cgi,
520 $query_desc, $limit, $limit_cgi, $limit_desc,
521 $stopwords_removed, $query_type ) = buildQuery([], [ 'medic' ], [ 'kw' ], [], [], 0, 'en');
523 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
524 is($results_hashref->{biblioserver}->{hits}, undef, "Search for 'medic' returns no matches with automatic truncation off");
526 ( $error, $query, $simple_query, $query_cgi,
527 $query_desc, $limit, $limit_cgi, $limit_desc,
528 $stopwords_removed, $query_type ) = buildQuery([], [ 'medic*' ], [ 'kw' ], [], [], 0, 'en');
530 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
531 is($results_hashref->{biblioserver}->{hits}, 5, "Search for 'medic*' returns matches with automatic truncation off");
533 $QueryStemming = $QueryWeightFields = 1;
534 $QueryFuzzy = $QueryRemoveStopwords = $QueryAutoTruncate = 0;
535 ( $error, $query, $simple_query, $query_cgi,
536 $query_desc, $limit, $limit_cgi, $limit_desc,
537 $stopwords_removed, $query_type ) = buildQuery([], [ 'pressed' ], [ 'kw' ], [], [], 0, 'en');
539 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
540 is($results_hashref->{biblioserver}->{hits}, 7, "Search for 'pressed' returns matches when stemming (and query weighting) is on");
542 $QueryStemming = $QueryWeightFields = $QueryFuzzy = $QueryRemoveStopwords = $QueryAutoTruncate = 0;
543 ( $error, $query, $simple_query, $query_cgi,
544 $query_desc, $limit, $limit_cgi, $limit_desc,
545 $stopwords_removed, $query_type ) = buildQuery([], [ 'pressed' ], [ 'kw' ], [], [], 0, 'en');
547 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
548 is($results_hashref->{biblioserver}->{hits}, undef, "Search for 'pressed' returns no matches when stemming is off");
550 # Let's see what happens when we pass bad data into these routines.
551 # We have to catch warnings since we're not very good about returning errors.
553 warning_like { ( $error, $marcresults, $total_hits ) = SimpleSearch("@==ccl blah", 0, 9) } qr/CCL parsing error/,
554 "SimpleSearch warns about CCL parsing error with nonsense query";
555 isnt($error, undef, "SimpleSearch returns an error when passed gibberish");
557 warning_like {( undef, $results_hashref, $facets_loop ) =
558 getRecords('kw:book', 'book', [], [ 'biblioserver' ], '19', 0, undef, \%branches, \%itemtypes, 'nonsense', undef) }
559 qr/Unknown query_type/, "getRecords warns about unknown query type";
561 warning_like {( undef, $results_hashref, $facets_loop ) =
562 getRecords('pqf=@attr 1=4 "title"', 'pqf=@attr 1=4 "title"', [], [ 'biblioserver' ], '19', 0, undef, \%branches, \%itemtypes, '', undef) }
563 qr/WARNING: query problem/, "getRecords warns when query type is not specified for non-CCL query";
565 # Let's just test a few other bits and bobs, just for fun
567 ($error, $results_hashref, $facets_loop) = getRecords("Godzina pąsowej róży","Godzina pąsowej róży",[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
568 @newresults = searchResults('intranet', $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 17, 0, 0,
569 $results_hashref->{'biblioserver'}->{"RECORDS"});
570 is($newresults[0]->{'alternateholdings_count'}, 1, 'Alternate holdings filled in correctly');
573 ## Regression test for Bug 10741
575 # make one of the test items appear to be in transit
576 my $circ_module = new Test::MockModule('C4::Circulation');
577 $circ_module->mock('GetTransfers', sub {
578 my $itemnumber = shift;
579 if ($itemnumber == 11) {
580 return ('2013-07-19', 'MPL', 'CPL');
586 ($error, $results_hashref, $facets_loop) = getRecords("TEST12121212","TEST12121212",[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
587 @newresults = searchResults('intranet', $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 17, 0, 0,
588 $results_hashref->{'biblioserver'}->{"RECORDS"});
589 ok(!exists($newresults[0]->{norequests}), 'presence of a transit does not block hold request action (bug 10741)');
591 # Testing exploding indexes
593 my $searchmodule = new Test::MockModule('C4::Search');
594 $searchmodule->mock('SimpleSearch', sub {
597 is($query, "he:$term", "Searching for expected term '$term' for exploding") or return '', [], 0;
599 my $record = MARC::Record->new;
600 if ($query =~ m/Arizona/) {
603 [ '151', ' ', ' ', a => 'Arizona' ],
604 [ '551', ' ', ' ', a => 'United States', w => 'g' ],
605 [ '551', ' ', ' ', a => 'Maricopa County', w => 'h' ],
606 [ '551', ' ', ' ', a => 'Navajo County', w => 'h' ],
607 [ '551', ' ', ' ', a => 'Pima County', w => 'h' ],
608 [ '551', ' ', ' ', a => 'New Mexico' ],
611 return '', [ $record->as_usmarc() ], 1;
616 ( $error, $query, $simple_query, $query_cgi,
617 $query_desc, $limit, $limit_cgi, $limit_desc,
618 $stopwords_removed, $query_type ) = buildQuery([], [ $term ], [ 'su-br' ], [ ], [], 0, 'en');
619 matchesExplodedTerms("Advanced search for broader subjects", $query, 'Arizona', 'United States');
621 ( $error, $query, $simple_query, $query_cgi,
622 $query_desc, $limit, $limit_cgi, $limit_desc,
623 $stopwords_removed, $query_type ) = buildQuery([], [ $term ], [ 'su-na' ], [ ], [], 0, 'en');
624 matchesExplodedTerms("Advanced search for narrower subjects", $query, 'Arizona', 'Maricopa County', 'Navajo County', 'Pima County');
626 ( $error, $query, $simple_query, $query_cgi,
627 $query_desc, $limit, $limit_cgi, $limit_desc,
628 $stopwords_removed, $query_type ) = buildQuery([], [ $term ], [ 'su-rl' ], [ ], [], 0, 'en');
629 matchesExplodedTerms("Advanced search for related subjects", $query, 'Arizona', 'United States', 'Maricopa County', 'Navajo County', 'Pima County');
631 ( $error, $query, $simple_query, $query_cgi,
632 $query_desc, $limit, $limit_cgi, $limit_desc,
633 $stopwords_removed, $query_type ) = buildQuery([], [ "$term", 'history' ], [ 'su-rl', 'kw' ], [ ], [], 0, 'en');
634 matchesExplodedTerms("Advanced search for related subjects and keyword 'history' searches related subjects", $query, 'Arizona', 'United States', 'Maricopa County', 'Navajo County', 'Pima County');
635 like($query, qr/history/, "Advanced search for related subjects and keyword 'history' searches for 'history'");
637 ( $error, $query, $simple_query, $query_cgi,
638 $query_desc, $limit, $limit_cgi, $limit_desc,
639 $stopwords_removed, $query_type ) = buildQuery([], [ 'history', "$term" ], [ 'kw', 'su-rl' ], [ ], [], 0, 'en');
640 matchesExplodedTerms("Order of terms doesn't matter for advanced search", $query, 'Arizona', 'United States', 'Maricopa County', 'Navajo County', 'Pima County');
641 like($query, qr/history/, "Order of terms doesn't matter for advanced search");
643 ( $error, $query, $simple_query, $query_cgi,
644 $query_desc, $limit, $limit_cgi, $limit_desc,
645 $stopwords_removed, $query_type ) = buildQuery([], [ "su-br($term)" ], [ ], [ ], [], 0, 'en');
646 matchesExplodedTerms("Simple search for broader subjects", $query, 'Arizona', 'United States');
648 ( $error, $query, $simple_query, $query_cgi,
649 $query_desc, $limit, $limit_cgi, $limit_desc,
650 $stopwords_removed, $query_type ) = buildQuery([], [ "su-na($term)" ], [ ], [ ], [], 0, 'en');
651 matchesExplodedTerms("Simple search for narrower subjects", $query, 'Arizona', 'Maricopa County', 'Navajo County', 'Pima County');
653 ( $error, $query, $simple_query, $query_cgi,
654 $query_desc, $limit, $limit_cgi, $limit_desc,
655 $stopwords_removed, $query_type ) = buildQuery([], [ "su-rl($term)" ], [ ], [ ], [], 0, 'en');
656 matchesExplodedTerms("Simple search for related subjects", $query, 'Arizona', 'United States', 'Maricopa County', 'Navajo County', 'Pima County');
658 ( $error, $query, $simple_query, $query_cgi,
659 $query_desc, $limit, $limit_cgi, $limit_desc,
660 $stopwords_removed, $query_type ) = buildQuery([], [ "history && su-rl($term)" ], [ ], [ ], [], 0, 'en');
661 matchesExplodedTerms("Simple search for related subjects and keyword 'history' searches related subjects", $query, 'Arizona', 'United States', 'Maricopa County', 'Navajo County', 'Pima County');
662 like($query, qr/history/, "Simple search for related subjects and keyword 'history' searches for 'history'");
664 sub matchesExplodedTerms {
665 my ($message, $query, @terms) = @_;
666 my $match = '(' . join ('|', map { " \@attr 1=Subject \@attr 4=1 \"$_\"" } @terms) . "){" . scalar(@terms) . "}";
667 like($query, qr/$match/, $message);
673 sub run_unimarc_search_tests {
674 my $indexing_mode = shift;
675 $datadir = tempdir();
676 system(dirname(__FILE__) . "/zebra_config.pl $datadir unimarc $indexing_mode");
678 mock_marcfromkohafield('unimarc');
679 my $context = new C4::Context("$datadir/etc/koha-conf.xml");
680 $context->set_context();
682 use_ok('C4::Search');
684 # set search syspreferences to a known starting point
686 $QueryAutoTruncate = 0;
687 $QueryWeightFields = 0;
689 $QueryRemoveStopwords = 0;
691 $marcflavour = 'UNIMARC';
693 index_sample_records_and_launch_zebra($datadir, $indexing_mode, 'unimarc');
695 my ( $error, $marcresults, $total_hits ) = SimpleSearch("ti=Järnvägarnas efterfrågan och den svenska industrin", 0, 10);
696 is($total_hits, 1, 'UNIMARC title search');
697 ( $error, $marcresults, $total_hits ) = SimpleSearch("ta=u", 0, 10);
698 is($total_hits, 1, 'UNIMARC target audience = u');
699 ( $error, $marcresults, $total_hits ) = SimpleSearch("ta=k", 0, 10);
700 is($total_hits, 4, 'UNIMARC target audience = k');
701 ( $error, $marcresults, $total_hits ) = SimpleSearch("ta=m", 0, 10);
702 is($total_hits, 3, 'UNIMARC target audience = m');
707 run_marc21_search_tests('grs1');
708 run_marc21_search_tests('dom');
710 run_unimarc_search_tests('grs1');
711 run_unimarc_search_tests('dom');