Bug 15082: t/db_dependent/Search.t shouldn't mock the DB connection
[koha.git] / t / db_dependent / Search.t
blobcac37e88aba80b04246fbb85399556d35df1047e
1 #!/usr/bin/perl
3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
18 use Modern::Perl;
20 use utf8;
22 use YAML;
24 use C4::Debug;
25 require C4::Context;
27 # work around spurious wide character warnings
28 use open ':std', ':encoding(utf8)';
30 use Test::More tests => 4;
31 use Test::MockModule;
32 use MARC::Record;
33 use File::Spec;
34 use File::Basename;
35 use File::Find;
36 use Test::Warn;
37 use File::Temp qw/ tempdir /;
38 use File::Path;
40 our $child;
41 our $datadir;
43 sub index_sample_records_and_launch_zebra {
44 my ($datadir, $indexing_mode, $marc_type) = @_;
46 my $sourcedir = dirname(__FILE__) . "/data";
47 unlink("$datadir/zebra.log");
48 if (-f "$sourcedir/${marc_type}/zebraexport/biblio/exported_records") {
49 my $zebra_bib_cfg = ($indexing_mode eq 'dom') ? 'zebra-biblios-dom.cfg' : 'zebra-biblios.cfg';
50 system("zebraidx -c $datadir/etc/koha/zebradb/$zebra_bib_cfg -v none,fatal,warn -g iso2709 -d biblios init");
51 system("zebraidx -c $datadir/etc/koha/zebradb/$zebra_bib_cfg -v none,fatal,warn -g iso2709 -d biblios update $sourcedir/${marc_type}/zebraexport/biblio");
52 system("zebraidx -c $datadir/etc/koha/zebradb/$zebra_bib_cfg -v none,fatal,warn -g iso2709 -d biblios commit");
54 # ... and add large bib records, if present
55 if (-f "$sourcedir/${marc_type}/zebraexport/large_biblio_${indexing_mode}/exported_records.xml") {
56 my $zebra_bib_cfg = ($indexing_mode eq 'dom') ? 'zebra-biblios-dom.cfg' : 'zebra-biblios.cfg';
57 system("zebraidx -c $datadir/etc/koha/zebradb/$zebra_bib_cfg -v none,fatal,warn -g marcxml -d biblios update $sourcedir/${marc_type}/zebraexport/large_biblio_${indexing_mode}");
58 system("zebraidx -c $datadir/etc/koha/zebradb/$zebra_bib_cfg -v none,fatal,warn -g marcxml -d biblios commit");
60 if (-f "$sourcedir/${marc_type}/zebraexport/authority/exported_records") {
61 my $zebra_auth_cfg = ($indexing_mode eq 'dom') ? 'zebra-authorities-dom.cfg' : 'zebra-authorities.cfg';
62 system("zebraidx -c $datadir/etc/koha/zebradb/$zebra_auth_cfg -v none,fatal,warn -g iso2709 -d authorities init");
63 system("zebraidx -c $datadir/etc/koha/zebradb/$zebra_auth_cfg -v none,fatal,warn -g iso2709 -d authorities update $sourcedir/${marc_type}/zebraexport/authority");
64 system("zebraidx -c $datadir/etc/koha/zebradb/$zebra_auth_cfg -v none,fatal,warn -g iso2709 -d authorities commit");
67 $child = fork();
68 if ($child == 0) {
69 exec("zebrasrv -f $datadir/etc/koha-conf.xml -v none,request -l $datadir/zebra.log");
70 exit;
73 sleep(1);
76 sub cleanup {
77 if ($child) {
78 kill 9, $child;
80 # Clean up the Zebra files since the child process was just shot
81 rmtree $datadir;
85 # Fall back to make sure that the Zebra process
86 # and files get cleaned up
87 END {
88 cleanup();
91 our $QueryStemming = 0;
92 our $QueryAutoTruncate = 0;
93 our $QueryWeightFields = 0;
94 our $QueryFuzzy = 0;
95 our $QueryRemoveStopwords = 0;
96 our $UseQueryParser = 0;
97 our $marcflavour = 'MARC21';
98 our $contextmodule = new Test::MockModule('C4::Context');
99 $contextmodule->mock('preference', sub {
100 my ($self, $pref) = @_;
101 if ($pref eq 'marcflavour') {
102 return $marcflavour;
103 } elsif ($pref eq 'QueryStemming') {
104 return $QueryStemming;
105 } elsif ($pref eq 'QueryAutoTruncate') {
106 return $QueryAutoTruncate;
107 } elsif ($pref eq 'QueryWeightFields') {
108 return $QueryWeightFields;
109 } elsif ($pref eq 'QueryFuzzy') {
110 return $QueryFuzzy;
111 } elsif ($pref eq 'QueryRemoveStopwords') {
112 return $QueryRemoveStopwords;
113 } elsif ($pref eq 'UseQueryParser') {
114 return $UseQueryParser;
115 } elsif ($pref eq 'maxRecordsForFacets') {
116 return 20;
117 } elsif ($pref eq 'FacetLabelTruncationLength') {
118 return 20;
119 } elsif ($pref eq 'FacetMaxCount') {
120 return 20;
121 } elsif ($pref eq 'OpacHiddenItems') {
122 return '';
123 } elsif ($pref eq 'opacthemes') {
124 return 'bootstrap';
125 } elsif ($pref eq 'opaclanguages') {
126 return 'en';
127 } elsif ($pref eq 'AlternateHoldingsField') {
128 return '490av';
129 } elsif ($pref eq 'AuthoritySeparator') {
130 return '--';
131 } elsif ($pref eq 'DisplayLibraryFacets') {
132 return 'holding';
133 } elsif ($pref eq 'UNIMARCAuthorsFacetsSeparator') {
134 return '--';
135 } else {
136 warn "The syspref $pref was requested but I don't know what to say; this indicates that the test requires updating"
137 unless $pref =~ m/(XSLT|item|branch|holding|image)/i;
138 return 0;
141 $contextmodule->mock('queryparser', sub {
142 my $QParser = Koha::QueryParser::Driver::PQF->new();
143 $QParser->load_config("$datadir/etc/searchengine/queryparser.yaml");
144 return $QParser;
147 sub mock_marcfromkohafield {
148 my $marc_type = shift;
149 if ($marc_type eq 'marc21') {
150 $contextmodule->mock('marcfromkohafield', sub {
151 return {
152 '' => {
153 'biblio.biblionumber' => [ '999', 'c' ],
154 'items.barcode' => ['952', 'p' ],
155 'items.booksellerid' => ['952', 'e' ],
156 'items.ccode' => ['952', '8' ],
157 'items.cn_sort' => ['952', '6' ],
158 'items.cn_source' => ['952', '2' ],
159 'items.coded_location_qualifier' => ['952', 'f' ],
160 'items.copynumber' => ['952', 't' ],
161 'items.damaged' => ['952', '4' ],
162 'items.dateaccessioned' => ['952', 'd' ],
163 'items.datelastborrowed' => ['952', 's' ],
164 'items.datelastseen' => ['952', 'r' ],
165 'items.enumchron' => ['952', 'h' ],
166 'items.holdingbranch' => ['952', 'b' ],
167 'items.homebranch' => ['952', 'a' ],
168 'items.issues' => ['952', 'l' ],
169 'items.itemcallnumber' => ['952', 'o' ],
170 'items.itemlost' => ['952', '1' ],
171 'items.itemnotes' => ['952', 'z' ],
172 'items.itemnumber' => ['952', '9' ],
173 'items.itype' => ['952', 'y' ],
174 'items.location' => ['952', 'c' ],
175 'items.materials' => ['952', '3' ],
176 'items.nonpublicnote' => ['952', 'x' ],
177 'items.notforloan' => ['952', '7' ],
178 'items.onloan' => ['952', 'q' ],
179 'items.price' => ['952', 'g' ],
180 'items.renewals' => ['952', 'm' ],
181 'items.replacementprice' => ['952', 'v' ],
182 'items.replacementpricedate' => ['952', 'w' ],
183 'items.reserves' => ['952', 'n' ],
184 'items.restricted' => ['952', '5' ],
185 'items.stack' => ['952', 'j' ],
186 'items.uri' => ['952', 'u' ],
187 'items.withdrawn' => ['952', '0' ]
194 sub run_marc21_search_tests {
195 my $indexing_mode = shift;
196 $datadir = tempdir();
197 system(dirname(__FILE__) . "/zebra_config.pl $datadir marc21 $indexing_mode");
199 mock_marcfromkohafield('marc21');
200 my $context = new C4::Context("$datadir/etc/koha-conf.xml");
201 $context->set_context();
203 is($context->config('zebra_bib_index_mode'),$indexing_mode,
204 "zebra_bib_index_mode is properly set to '$indexing_mode' in the created koha-conf.xml file (BZ11499)");
205 is($context->config('zebra_auth_index_mode'),$indexing_mode,
206 "zebra_auth_index_mode is properly set to '$indexing_mode' in the created koha-conf.xml file (BZ11499)");
208 use_ok('C4::Search');
210 # set search syspreferences to a known starting point
211 $QueryStemming = 0;
212 $QueryAutoTruncate = 0;
213 $QueryWeightFields = 0;
214 $QueryFuzzy = 0;
215 $QueryRemoveStopwords = 0;
216 $UseQueryParser = 0;
217 $marcflavour = 'MARC21';
219 foreach my $string ("Leçon","modèles") {
220 my @results=C4::Search::_remove_stopwords($string,"kw");
221 $debug && warn "$string ",Dump(@results);
222 ok($results[0] eq $string,"$string is not modified");
225 foreach my $string ("A book about the stars") {
226 my @results=C4::Search::_remove_stopwords($string,"kw");
227 $debug && warn "$string ",Dump(@results);
228 ok($results[0] ne $string,"$results[0] from $string");
231 my $indexes = C4::Search::getIndexes();
232 is(scalar(grep(/^ti$/, @$indexes)), 1, "Title index supported");
234 my $bibliomodule = new Test::MockModule('C4::Biblio');
235 $bibliomodule->mock('_get_inverted_marc_field_map', sub {
236 my %hash = (
237 '' => {
238 '245' => { 'sfs' => { 'a' => [ [ 'biblio', 'title' ] ], 'b' => [ [ 'bibliosubtitle', 'subtitle' ] ] },
239 'list' => [ [ 'a', 'biblio', 'title' ], [ 'b', 'bibliosubtitle', 'subtitle' ] ]
241 '100' => {
242 'sfs' => { 'a' => [ [ 'biblio', 'author' ] ] },
243 'list' => [ [ 'a', 'biblio', 'author' ] ]
245 '999' => {
246 'sfs' => { 'c' => [ [ 'biblio', 'biblionumber' ] ], 'd' => [ [ 'biblioitems', 'biblioitemnumber' ] ] },
247 'list' => [ [ 'd', 'biblioitems', 'biblioitemnumber' ], [ 'c', 'biblio', 'biblionumber' ] ]
249 '020' => {
250 'sfs' => { 'a' => [ [ 'biblioitems', 'isbn' ] ] },
251 'list' => [ [ 'a', 'biblioitems', 'isbn' ] ]
253 '500' => {
254 'sfs' => { 'a' => [ [ 'biblioitems', 'notes' ] ] },
255 'list' => [ [ 'a', 'biblioitems', 'notes' ] ]
259 return \%hash;
262 my %branches = (
263 'CPL' => { 'branchaddress1' => 'Jefferson Summit', 'branchcode' => 'CPL', 'branchname' => 'Centerville', },
264 'FFL' => { 'branchaddress1' => 'River Station', 'branchcode' => 'FFL', 'branchname' => 'Fairfield', },
265 'FPL' => { 'branchaddress1' => 'Hickory Squere', 'branchcode' => 'FPL', 'branchname' => 'Fairview', },
266 'FRL' => { 'branchaddress1' => 'Smith Heights', 'branchcode' => 'FRL', 'branchname' => 'Franklin', },
267 'IPT' => { 'branchaddress1' => '', 'branchcode' => 'IPT', 'branchname' => "Institut Protestant de Théologie", },
268 'LPL' => { 'branchaddress1' => 'East Hills', 'branchcode' => 'LPL', 'branchname' => 'Liberty', },
269 'MPL' => { 'branchaddress1' => '372 Forest Street', 'branchcode' => 'MPL', 'branchname' => 'Midway', },
270 'PVL' => { 'branchaddress1' => 'Meadow Grove', 'branchcode' => 'PVL', 'branchname' => 'Pleasant Valley', },
271 'RPL' => { 'branchaddress1' => 'Johnson Terrace', 'branchcode' => 'RPL', 'branchname' => 'Riverside', },
272 'SPL' => { 'branchaddress1' => 'Highland Boulevard', 'branchcode' => 'SPL', 'branchname' => 'Springfield', },
273 'S' => { 'branchaddress1' => '', 'branchcode' => 'S', 'branchname' => 'Test', },
274 'TPL' => { 'branchaddress1' => 'Valley Way', 'branchcode' => 'TPL', 'branchname' => 'Troy', },
275 'UPL' => { 'branchaddress1' => 'Chestnut Hollow', 'branchcode' => 'UPL', 'branchname' => 'Union', },
277 my %itemtypes = (
278 'BK' => { 'imageurl' => 'bridge/book.gif', 'summary' => '', 'itemtype' => 'BK', 'description' => 'Books' },
279 'CF' => { 'imageurl' => 'bridge/computer_file.gif', 'summary' => '', 'itemtype' => 'CF', 'description' => 'Computer Files' },
280 'CR' => { 'imageurl' => 'bridge/periodical.gif', 'summary' => '', 'itemtype' => 'CR', 'description' => 'Continuing Resources' },
281 'MP' => { 'imageurl' => 'bridge/map.gif', 'summary' => '', 'itemtype' => 'MP', 'description' => 'Maps' },
282 'MU' => { 'imageurl' => 'bridge/sound.gif', 'summary' => '', 'itemtype' => 'MU', 'description' => 'Music' },
283 'MX' => { 'imageurl' => 'bridge/kit.gif', 'summary' => '', 'itemtype' => 'MX', 'description' => 'Mixed Materials' },
284 'REF' => { 'imageurl' => '', 'summary' => '', 'itemtype' => 'REF', 'description' => 'Reference' },
285 'VM' => { 'imageurl' => 'bridge/dvd.gif', 'summary' => '', 'itemtype' => 'VM', 'description' => 'Visual Materials' },
288 index_sample_records_and_launch_zebra($datadir, $indexing_mode, 'marc21');
290 my ($biblionumber, $title);
291 my $record = MARC::Record->new;
293 $record->add_fields(
294 [ '020', ' ', ' ', a => '9788522421718' ],
295 [ '245', '0', '0', a => 'Administração da produção /' ]
297 ($biblionumber,undef,$title) = FindDuplicate($record);
298 is($biblionumber, 51, 'Found duplicate with ISBN');
300 $record = MARC::Record->new;
302 $record->add_fields(
303 [ '100', '1', ' ', a => 'Carter, Philip J.' ],
304 [ '245', '1', '4', a => 'Test your emotional intelligence :' ]
306 ($biblionumber,undef,$title) = FindDuplicate($record);
307 is($biblionumber, 203, 'Found duplicate with author/title');
309 # Testing SimpleSearch
311 my ( $error, $marcresults, $total_hits ) = SimpleSearch("book", 0, 9);
313 is(scalar @$marcresults, 9, "SimpleSearch retrieved requested number of records");
314 is($total_hits, 101, "SimpleSearch for 'book' matched right number of records");
315 is($error, undef, "SimpleSearch does not return an error when successful");
317 my $marcresults2;
318 ( $error, $marcresults2, $total_hits ) = SimpleSearch("book", 5, 5);
319 is($marcresults->[5], $marcresults2->[0], "SimpleSearch cursor functions");
321 ( $error, $marcresults, $total_hits ) = SimpleSearch("kw=book", 0, 10);
322 is($total_hits, 101, "SimpleSearch handles simple CCL");
324 ( $error, $marcresults, $total_hits ) = SimpleSearch("Music-number=49631-2", 0, 10);
325 is($total_hits, 1, "SimpleSearch on music publisher number works (bug 8252)");
326 ( $error, $marcresults, $total_hits ) = SimpleSearch("Identifier-publisher-for-music=49631-2", 0, 10);
327 is($total_hits, 1, "SimpleSearch on music publisher number works using Identifier-publisher-for-music (bug 8252)");
329 # Testing getRecords
331 my $results_hashref;
332 my $facets_loop;
333 ( undef, $results_hashref, $facets_loop ) =
334 getRecords('kw:book', 'book', [], [ 'biblioserver' ], '19', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
335 is($results_hashref->{biblioserver}->{hits}, 101, "getRecords keyword search for 'book' matched right number of records");
336 is(scalar @{$results_hashref->{biblioserver}->{RECORDS}}, 19, "getRecords returned requested number of records");
337 my $record5 = $results_hashref->{biblioserver}->{RECORDS}->[5];
338 ( undef, $results_hashref, $facets_loop ) =
339 getRecords('kw:book', 'book', [], [ 'biblioserver' ], '20', 5, undef, \%branches, \%itemtypes, 'ccl', undef);
340 ok(!defined $results_hashref->{biblioserver}->{RECORDS}->[0] &&
341 !defined $results_hashref->{biblioserver}->{RECORDS}->[1] &&
342 !defined $results_hashref->{biblioserver}->{RECORDS}->[2] &&
343 !defined $results_hashref->{biblioserver}->{RECORDS}->[3] &&
344 !defined $results_hashref->{biblioserver}->{RECORDS}->[4] &&
345 $results_hashref->{biblioserver}->{RECORDS}->[5] eq $record5, "getRecords cursor works");
347 ( undef, $results_hashref, $facets_loop ) =
348 getRecords('ti:book', 'ti:book', [], [ 'biblioserver' ], '20', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
349 is($results_hashref->{biblioserver}->{hits}, 11, "getRecords title search for 'book' matched right number of records");
351 ( undef, $results_hashref, $facets_loop ) =
352 getRecords('au:Lessig', 'au:Lessig', [], [ 'biblioserver' ], '20', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
353 is($results_hashref->{biblioserver}->{hits}, 4, "getRecords title search for 'Australia' matched right number of records");
355 if ( $indexing_mode eq 'dom' ) {
356 ( undef, $results_hashref, $facets_loop ) =
357 getRecords('salud', 'salud', [], [ 'biblioserver' ], '19', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
358 ok(MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[0],'UTF-8')->title_proper() =~ m/^Efectos del ambiente/ &&
359 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' &&
360 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[18],'UTF-8')->title_proper() =~ m/^Indicadores de resultados identificados/
361 , "Simple relevance sorting in getRecords matches old behavior");
363 ( undef, $results_hashref, $facets_loop ) =
364 getRecords('salud', 'salud', [ 'author_az' ], [ 'biblioserver' ], '38', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
365 ok(MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[0],'UTF-8')->title_proper() =~ m/la enfermedad laboral\^ies$/ &&
366 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[6],'UTF-8')->title_proper() =~ m/^Indicadores de resultados identificados/ &&
367 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[18],'UTF-8')->title_proper() eq 'World health statistics 2009^ien'
368 , "Simple ascending author sorting in getRecords matches old behavior");
370 ( undef, $results_hashref, $facets_loop ) =
371 getRecords('salud', 'salud', [ 'author_za' ], [ 'biblioserver' ], '38', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
372 ok(MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[0],'UTF-8')->title_proper() eq 'World health statistics 2009^ien' &&
373 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[12],'UTF-8')->title_proper() =~ m/^Indicadores de resultados identificados/ &&
374 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[18],'UTF-8')->title_proper() =~ m/la enfermedad laboral\^ies$/
375 , "Simple descending author sorting in getRecords matches old behavior");
377 ( undef, $results_hashref, $facets_loop ) =
378 getRecords('salud', 'salud', [ 'pubdate_asc' ], [ 'biblioserver' ], '38', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
379 ok(MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[0],'UTF-8')->title_proper() eq 'Manual de higiene industrial^ies' &&
380 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[7],'UTF-8')->title_proper() =~ m/seguridad e higiene del trabajo\^ies$/ &&
381 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[18],'UTF-8')->title_proper() =~ m/^Indicadores de resultados identificados/
382 , "Simple ascending publication date sorting in getRecords matches old behavior");
384 ( undef, $results_hashref, $facets_loop ) =
385 getRecords('salud', 'salud', [ 'pubdate_dsc' ], [ 'biblioserver' ], '38', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
386 ok(MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[0],'UTF-8')->title_proper() =~ m/^Estado de salud/ &&
387 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[7],'UTF-8')->title_proper() eq 'World health statistics 2009^ien' &&
388 MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[18],'UTF-8')->title_proper() eq 'Manual de higiene industrial^ies'
389 , "Simple descending publication date sorting in getRecords matches old behavior");
391 } elsif ( $indexing_mode eq 'grs1' ){
392 ( undef, $results_hashref, $facets_loop ) =
393 getRecords('salud', 'salud', [], [ 'biblioserver' ], '19', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
394 ok(MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[0])->title_proper() =~ m/^Efectos del ambiente/ &&
395 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' &&
396 MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[18])->title_proper() =~ m/^Indicadores de resultados identificados/
397 , "Simple relevance sorting in getRecords matches old behavior");
399 ( undef, $results_hashref, $facets_loop ) =
400 getRecords('salud', 'salud', [ 'author_az' ], [ 'biblioserver' ], '38', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
401 ok(MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[0])->title_proper() =~ m/la enfermedad laboral\^ies$/ &&
402 MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[6])->title_proper() =~ m/^Indicadores de resultados identificados/ &&
403 MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[18])->title_proper() eq 'World health statistics 2009^ien'
404 , "Simple ascending author sorting in getRecords matches old behavior");
406 ( undef, $results_hashref, $facets_loop ) =
407 getRecords('salud', 'salud', [ 'author_za' ], [ 'biblioserver' ], '38', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
408 ok(MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[0])->title_proper() eq 'World health statistics 2009^ien' &&
409 MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[12])->title_proper() =~ m/^Indicadores de resultados identificados/ &&
410 MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[18])->title_proper() =~ m/la enfermedad laboral\^ies$/
411 , "Simple descending author sorting in getRecords matches old behavior");
413 ( undef, $results_hashref, $facets_loop ) =
414 getRecords('salud', 'salud', [ 'pubdate_asc' ], [ 'biblioserver' ], '38', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
415 ok(MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[0])->title_proper() eq 'Manual de higiene industrial^ies' &&
416 MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[7])->title_proper() =~ m/seguridad e higiene del trabajo\^ies$/ &&
417 MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[18])->title_proper() =~ m/^Indicadores de resultados identificados/
418 , "Simple ascending publication date sorting in getRecords matches old behavior");
420 ( undef, $results_hashref, $facets_loop ) =
421 getRecords('salud', 'salud', [ 'pubdate_dsc' ], [ 'biblioserver' ], '38', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
422 ok(MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[0])->title_proper() =~ m/^Estado de salud/ &&
423 MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[7])->title_proper() eq 'World health statistics 2009^ien' &&
424 MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[18])->title_proper() eq 'Manual de higiene industrial^ies'
425 , "Simple descending publication date sorting in getRecords matches old behavior");
428 ( undef, $results_hashref, $facets_loop ) =
429 getRecords('books', 'books', [ 'relevance' ], [ 'biblioserver' ], '20', 0, undef, \%branches, \%itemtypes, undef, 1);
430 $record = MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[0]);
431 is($record->title_proper(), 'Books', "Scan returned requested item");
432 is($record->subfield('100', 'a'), 2, "Scan returned correct number of records matching term");
433 # Time to test buildQuery and searchResults too.
435 my ( $query, $simple_query, $query_cgi,
436 $query_desc, $limit, $limit_cgi, $limit_desc,
437 $stopwords_removed, $query_type );
438 ( $error, $query, $simple_query, $query_cgi,
439 $query_desc, $limit, $limit_cgi, $limit_desc,
440 $stopwords_removed, $query_type ) = buildQuery([], [ 'salud' ], [], [], [], 0, 'en');
441 like($query, qr/kw\W.*salud/, "Built CCL keyword query");
443 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
444 is($results_hashref->{biblioserver}->{hits}, 19, "getRecords generated keyword search for 'salud' matched right number of records");
446 my @newresults = searchResults('opac', $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 18, 0, 0,
447 $results_hashref->{'biblioserver'}->{"RECORDS"});
448 is(scalar @newresults,18, "searchResults returns requested number of hits");
450 ( $error, $query, $simple_query, $query_cgi,
451 $query_desc, $limit, $limit_cgi, $limit_desc,
452 $stopwords_removed, $query_type ) = buildQuery([ 'and' ], [ 'salud', 'higiene' ], [], [], [], 0, 'en');
453 like($query, qr/kw\W.*salud\W.*and.*kw\W.*higiene/, "Built composed explicit-and CCL keyword query");
455 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
456 is($results_hashref->{biblioserver}->{hits}, 3, "getRecords generated composed keyword search for 'salud' explicit-and 'higiene' matched right number of records");
458 ( $error, $query, $simple_query, $query_cgi,
459 $query_desc, $limit, $limit_cgi, $limit_desc,
460 $stopwords_removed, $query_type ) = buildQuery([ 'or' ], [ 'salud', 'higiene' ], [], [], [], 0, 'en');
461 like($query, qr/kw\W.*salud\W.*or.*kw\W.*higiene/, "Built composed explicit-or CCL keyword query");
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}, 20, "getRecords generated composed keyword search for 'salud' explicit-or 'higiene' matched right number of records");
466 ( $error, $query, $simple_query, $query_cgi,
467 $query_desc, $limit, $limit_cgi, $limit_desc,
468 $stopwords_removed, $query_type ) = buildQuery([], [ 'salud', 'higiene' ], [], [], [], 0, 'en');
469 like($query, qr/kw\W.*salud\W.*and.*kw\W.*higiene/, "Built composed implicit-and CCL keyword query");
471 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
472 is($results_hashref->{biblioserver}->{hits}, 3, "getRecords generated composed keyword search for 'salud' implicit-and 'higiene' matched right number of records");
474 ( $error, $query, $simple_query, $query_cgi,
475 $query_desc, $limit, $limit_cgi, $limit_desc,
476 $stopwords_removed, $query_type ) = buildQuery([], [ 'salud' ], [ 'kw' ], [ 'su-to:Laboratorios' ], [], 0, 'en');
477 like($query, qr/kw\W.*salud\W*and\W*su-to\W.*Laboratorios/, "Faceted query generated correctly");
478 unlike($query_desc, qr/Laboratorios/, "Facets not included in query description");
480 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
481 is($results_hashref->{biblioserver}->{hits}, 2, "getRecords generated faceted search matched right number of records");
484 ( $error, $query, $simple_query, $query_cgi,
485 $query_desc, $limit, $limit_cgi, $limit_desc,
486 $stopwords_removed, $query_type ) = buildQuery([], [ '' ], [ 'kw' ], [ 'mc-itype:MP', 'mc-itype:MU' ], [], 0, 'en');
488 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
489 is($results_hashref->{biblioserver}->{hits}, 2, "getRecords generated mc-faceted search matched right number of records");
492 ( $error, $query, $simple_query, $query_cgi,
493 $query_desc, $limit, $limit_cgi, $limit_desc,
494 $stopwords_removed, $query_type ) = buildQuery([], [ '' ], [ 'kw' ], [ 'mc-loc:GEN', 'branch:FFL' ], [], 0, 'en');
496 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
497 is($results_hashref->{biblioserver}->{hits}, 2, "getRecords generated multi-faceted search matched right number of records");
499 ( $error, $query, $simple_query, $query_cgi,
500 $query_desc, $limit, $limit_cgi, $limit_desc,
501 $stopwords_removed, $query_type ) = buildQuery([], [ 'NEKLS' ], [ 'Code-institution' ], [], [], 0, 'en');
502 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
503 is($results_hashref->{biblioserver}->{hits}, 12,
504 'search using index whose name contains "ns" returns expected results (bug 10271)');
506 $UseQueryParser = 1;
507 ( $error, $query, $simple_query, $query_cgi,
508 $query_desc, $limit, $limit_cgi, $limit_desc,
509 $stopwords_removed, $query_type ) = buildQuery([], [ 'book' ], [ 'kw' ], [], [], 0, 'en');
510 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
511 is($results_hashref->{biblioserver}->{hits}, 101, "Search for 'book' with index set to 'kw' returns 101 hits");
512 ( $error, $query, $simple_query, $query_cgi,
513 $query_desc, $limit, $limit_cgi, $limit_desc,
514 $stopwords_removed, $query_type ) = buildQuery([ 'and' ], [ 'book', 'another' ], [ 'kw', '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}, 1, "Search for 'kw:book && kw:another' returns 1 hit");
517 $UseQueryParser = 0;
519 # FIXME: the availability limit does not actually work, so for the moment we
520 # are just checking that it behaves consistently
521 ( $error, $query, $simple_query, $query_cgi,
522 $query_desc, $limit, $limit_cgi, $limit_desc,
523 $stopwords_removed, $query_type ) = buildQuery([], [ '' ], [ 'kw' ], [ 'available' ], [], 0, 'en');
525 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
526 is($results_hashref->{biblioserver}->{hits}, 26, "getRecords generated availability-limited search matched right number of records");
528 @newresults = searchResults('opac', $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 17, 0, 0,
529 $results_hashref->{'biblioserver'}->{"RECORDS"});
530 my $allavailable = 'true';
531 foreach my $result (@newresults) {
532 $allavailable = 'false' unless $result->{availablecount} > 0;
534 is ($allavailable, 'true', 'All records have at least one item available');
537 ( $error, $query, $simple_query, $query_cgi,
538 $query_desc, $limit, $limit_cgi, $limit_desc,
539 $stopwords_removed, $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,undef,\%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 $stopwords_removed, $query_type ) = buildQuery([], [ 'pqf=@attr 1=1016 "Lessig"' ], [], [], [], 0, 'en');
548 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%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 $stopwords_removed, $query_type ) = buildQuery([], [ 'ccl=au:Lessig' ], [], [], [], 0, 'en');
555 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%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 $stopwords_removed, $query_type ) = buildQuery([], [ 'cql=dc.author any lessig' ], [], [], [], 0, 'en');
562 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%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 = $QueryRemoveStopwords = 0;
566 $QueryWeightFields = 1;
567 ( $error, $query, $simple_query, $query_cgi,
568 $query_desc, $limit, $limit_cgi, $limit_desc,
569 $stopwords_removed, $query_type ) = buildQuery([], [ 'salud' ], [ 'kw' ], [], [], 0, 'en');
571 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
572 is($results_hashref->{biblioserver}->{hits}, 19, "Weighted query returned correct number of results");
573 if ($indexing_mode eq 'grs1') {
574 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");
575 } else {
576 local $TODO = "Query weighting does not behave exactly the same in DOM vs. GRS";
577 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");
580 $QueryStemming = $QueryWeightFields = $QueryFuzzy = $QueryRemoveStopwords = 0;
581 $QueryAutoTruncate = 1;
582 ( $error, $query, $simple_query, $query_cgi,
583 $query_desc, $limit, $limit_cgi, $limit_desc,
584 $stopwords_removed, $query_type ) = buildQuery([], [ 'medic' ], [ 'kw' ], [], [], 0, 'en');
586 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
587 is($results_hashref->{biblioserver}->{hits}, 5, "Search for 'medic' returns matches with automatic truncation on");
589 ( $error, $query, $simple_query, $query_cgi,
590 $query_desc, $limit, $limit_cgi, $limit_desc,
591 $stopwords_removed, $query_type ) = buildQuery([], [ 'medic*' ], [ 'kw' ], [], [], 0, 'en');
593 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
594 is($results_hashref->{biblioserver}->{hits}, 5, "Search for 'medic*' returns matches with automatic truncation on");
596 $QueryStemming = $QueryFuzzy = $QueryRemoveStopwords = $QueryAutoTruncate = 0;
597 $QueryWeightFields = 1;
598 ( $error, $query, $simple_query, $query_cgi,
599 $query_desc, $limit, $limit_cgi, $limit_desc,
600 $stopwords_removed, $query_type ) = buildQuery([], [ 'web application' ], [ 'kw' ], [], [], 0, 'en');
601 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
602 is($results_hashref->{biblioserver}->{hits}, 1, "Search for 'web application' returns one hit with QueryWeightFields on");
604 ( $error, $query, $simple_query, $query_cgi,
605 $query_desc, $limit, $limit_cgi, $limit_desc,
606 $stopwords_removed, $query_type ) = buildQuery([], [ 'web "application' ], [ 'kw' ], [], [], 0, 'en');
607 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
608 is($results_hashref->{biblioserver}->{hits}, 1, "Search for 'web \"application' returns one hit with QueryWeightFields on (bug 7518)");
610 $QueryStemming = $QueryWeightFields = $QueryFuzzy = $QueryRemoveStopwords = $QueryAutoTruncate = 0;
611 ( $error, $query, $simple_query, $query_cgi,
612 $query_desc, $limit, $limit_cgi, $limit_desc,
613 $stopwords_removed, $query_type ) = buildQuery([], [ 'medic' ], [ 'kw' ], [], [], 0, 'en');
615 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
616 is($results_hashref->{biblioserver}->{hits}, undef, "Search for 'medic' returns no matches with automatic truncation off");
618 ( $error, $query, $simple_query, $query_cgi,
619 $query_desc, $limit, $limit_cgi, $limit_desc,
620 $stopwords_removed, $query_type ) = buildQuery([], [ 'medic*' ], [ 'kw' ], [], [], 0, 'en');
622 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
623 is($results_hashref->{biblioserver}->{hits}, 5, "Search for 'medic*' returns matches with automatic truncation off");
625 $QueryStemming = $QueryWeightFields = 1;
626 $QueryFuzzy = $QueryRemoveStopwords = $QueryAutoTruncate = 0;
627 ( $error, $query, $simple_query, $query_cgi,
628 $query_desc, $limit, $limit_cgi, $limit_desc,
629 $stopwords_removed, $query_type ) = buildQuery([], [ 'pressed' ], [ 'kw' ], [], [], 0, 'en');
631 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
632 is($results_hashref->{biblioserver}->{hits}, 7, "Search for 'pressed' returns matches when stemming (and query weighting) is on");
634 $QueryStemming = $QueryWeightFields = $QueryFuzzy = $QueryRemoveStopwords = $QueryAutoTruncate = 0;
635 ( $error, $query, $simple_query, $query_cgi,
636 $query_desc, $limit, $limit_cgi, $limit_desc,
637 $stopwords_removed, $query_type ) = buildQuery([], [ 'pressed' ], [ 'kw' ], [], [], 0, 'en');
639 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
640 is($results_hashref->{biblioserver}->{hits}, undef, "Search for 'pressed' returns no matches when stemming is off");
642 # Let's see what happens when we pass bad data into these routines.
643 # We have to catch warnings since we're not very good about returning errors.
645 warning_like { ( $error, $marcresults, $total_hits ) = SimpleSearch("@==ccl blah", 0, 9) } qr/CCL parsing error/,
646 "SimpleSearch warns about CCL parsing error with nonsense query";
647 isnt($error, undef, "SimpleSearch returns an error when passed gibberish");
649 warning_like {( undef, $results_hashref, $facets_loop ) =
650 getRecords('kw:book', 'book', [], [ 'biblioserver' ], '19', 0, undef, \%branches, \%itemtypes, 'nonsense', undef) }
651 qr/Unknown query_type/, "getRecords warns about unknown query type";
653 warning_like {( undef, $results_hashref, $facets_loop ) =
654 getRecords('pqf=@attr 1=4 "title"', 'pqf=@attr 1=4 "title"', [], [ 'biblioserver' ], '19', 0, undef, \%branches, \%itemtypes, '', undef) }
655 qr/WARNING: query problem/, "getRecords warns when query type is not specified for non-CCL query";
657 # Let's just test a few other bits and bobs, just for fun
659 ($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);
660 @newresults = searchResults('intranet', $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 17, 0, 0,
661 $results_hashref->{'biblioserver'}->{"RECORDS"});
662 is($newresults[0]->{'alternateholdings_count'}, 1, 'Alternate holdings filled in correctly');
665 ## Regression test for Bug 10741
667 # make one of the test items appear to be in transit
668 my $circ_module = new Test::MockModule('C4::Circulation');
669 $circ_module->mock('GetTransfers', sub {
670 my $itemnumber = shift // -1;
671 if ($itemnumber == 11) {
672 return ('2013-07-19', 'MPL', 'CPL');
673 } else {
674 return;
678 ($error, $results_hashref, $facets_loop) = getRecords("TEST12121212","TEST12121212",[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
679 @newresults = searchResults('intranet', $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 17, 0, 0,
680 $results_hashref->{'biblioserver'}->{"RECORDS"});
681 ok(!exists($newresults[0]->{norequests}), 'presence of a transit does not block hold request action (bug 10741)');
683 ## Regression test for bug 10684
684 ( undef, $results_hashref, $facets_loop ) =
685 getRecords('ti:punctuation', 'punctuation', [], [ 'biblioserver' ], '19', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
686 is($results_hashref->{biblioserver}->{hits}, 1, "search for ti:punctuation returned expected number of records");
687 warning_like { @newresults = searchResults('intranet', $query_desc,
688 $results_hashref->{'biblioserver'}->{'hits'}, 20, 0, 0,
689 $results_hashref->{'biblioserver'}->{"RECORDS"}) }
690 qr/^ERROR DECODING RECORD - Tag "50%" is not a valid tag/,
691 "Warning is raised correctly for invalid tags in MARC::Record";
692 is(scalar(@newresults), 0, 'a record that cannot be parsed by MARC::Record is simply skipped (bug 10684)');
694 # Testing exploding indexes
695 my $term;
696 my $searchmodule = new Test::MockModule('C4::Search');
697 $searchmodule->mock('SimpleSearch', sub {
698 my $query = shift;
700 is($query, "he:$term", "Searching for expected term '$term' for exploding") or return '', [], 0;
702 my $record = MARC::Record->new;
703 if ($query =~ m/Arizona/) {
704 $record->add_fields(
705 [ '001', '1234' ],
706 [ '151', ' ', ' ', a => 'Arizona' ],
707 [ '551', ' ', ' ', a => 'United States', w => 'g' ],
708 [ '551', ' ', ' ', a => 'Maricopa County', w => 'h' ],
709 [ '551', ' ', ' ', a => 'Navajo County', w => 'h' ],
710 [ '551', ' ', ' ', a => 'Pima County', w => 'h' ],
711 [ '551', ' ', ' ', a => 'New Mexico' ],
714 return '', [ $record->as_usmarc() ], 1;
717 $UseQueryParser = 1;
718 $term = 'Arizona';
719 ( $error, $query, $simple_query, $query_cgi,
720 $query_desc, $limit, $limit_cgi, $limit_desc,
721 $stopwords_removed, $query_type ) = buildQuery([], [ $term ], [ 'su-br' ], [ ], [], 0, 'en');
722 matchesExplodedTerms("Advanced search for broader subjects", $query, 'Arizona', 'United States');
724 ( $error, $query, $simple_query, $query_cgi,
725 $query_desc, $limit, $limit_cgi, $limit_desc,
726 $stopwords_removed, $query_type ) = buildQuery([], [ $term ], [ 'su-na' ], [ ], [], 0, 'en');
727 matchesExplodedTerms("Advanced search for narrower subjects", $query, 'Arizona', 'Maricopa County', 'Navajo County', 'Pima County');
729 ( $error, $query, $simple_query, $query_cgi,
730 $query_desc, $limit, $limit_cgi, $limit_desc,
731 $stopwords_removed, $query_type ) = buildQuery([], [ $term ], [ 'su-rl' ], [ ], [], 0, 'en');
732 matchesExplodedTerms("Advanced search for related subjects", $query, 'Arizona', 'United States', 'Maricopa County', 'Navajo County', 'Pima County');
734 ( $error, $query, $simple_query, $query_cgi,
735 $query_desc, $limit, $limit_cgi, $limit_desc,
736 $stopwords_removed, $query_type ) = buildQuery([], [ "$term", 'history' ], [ 'su-rl', 'kw' ], [ ], [], 0, 'en');
737 matchesExplodedTerms("Advanced search for related subjects and keyword 'history' searches related subjects", $query, 'Arizona', 'United States', 'Maricopa County', 'Navajo County', 'Pima County');
738 like($query, qr/history/, "Advanced search for related subjects and keyword 'history' searches for 'history'");
740 ( $error, $query, $simple_query, $query_cgi,
741 $query_desc, $limit, $limit_cgi, $limit_desc,
742 $stopwords_removed, $query_type ) = buildQuery([], [ 'history', "$term" ], [ 'kw', 'su-rl' ], [ ], [], 0, 'en');
743 matchesExplodedTerms("Order of terms doesn't matter for advanced search", $query, 'Arizona', 'United States', 'Maricopa County', 'Navajo County', 'Pima County');
744 like($query, qr/history/, "Order of terms doesn't matter for advanced search");
746 ( $error, $query, $simple_query, $query_cgi,
747 $query_desc, $limit, $limit_cgi, $limit_desc,
748 $stopwords_removed, $query_type ) = buildQuery([], [ "su-br($term)" ], [ ], [ ], [], 0, 'en');
749 matchesExplodedTerms("Simple search for broader subjects", $query, 'Arizona', 'United States');
751 ( $error, $query, $simple_query, $query_cgi,
752 $query_desc, $limit, $limit_cgi, $limit_desc,
753 $stopwords_removed, $query_type ) = buildQuery([], [ "su-na($term)" ], [ ], [ ], [], 0, 'en');
754 matchesExplodedTerms("Simple search for narrower subjects", $query, 'Arizona', 'Maricopa County', 'Navajo County', 'Pima County');
756 ( $error, $query, $simple_query, $query_cgi,
757 $query_desc, $limit, $limit_cgi, $limit_desc,
758 $stopwords_removed, $query_type ) = buildQuery([], [ "su-rl($term)" ], [ ], [ ], [], 0, 'en');
759 matchesExplodedTerms("Simple search for related subjects", $query, 'Arizona', 'United States', 'Maricopa County', 'Navajo County', 'Pima County');
761 ( $error, $query, $simple_query, $query_cgi,
762 $query_desc, $limit, $limit_cgi, $limit_desc,
763 $stopwords_removed, $query_type ) = buildQuery([], [ "history && su-rl($term)" ], [ ], [ ], [], 0, 'en');
764 matchesExplodedTerms("Simple search for related subjects and keyword 'history' searches related subjects", $query, 'Arizona', 'United States', 'Maricopa County', 'Navajo County', 'Pima County');
765 like($query, qr/history/, "Simple search for related subjects and keyword 'history' searches for 'history'");
767 sub matchesExplodedTerms {
768 my ($message, $query, @terms) = @_;
769 my $match = '(' . join ('|', map { " \@attr 1=Subject \@attr 4=1 \"$_\"" } @terms) . "){" . scalar(@terms) . "}";
770 like($query, qr/$match/, $message);
773 # authority records
774 use_ok('C4::AuthoritiesMarc');
775 $UseQueryParser = 0;
777 my ($auths, $count) = SearchAuthorities(
778 ['mainentry'], ['and'], [''], ['starts'],
779 ['shakespeare'], 0, 10, '', '', 1
781 is($count, 1, 'MARC21 authorities: one hit on mainentry starts with "shakespeare"');
782 ($auths, $count) = SearchAuthorities(
783 ['mainentry'], ['and'], [''], ['starts'],
784 ['shakespeare'], 0, 10, '', 'HeadingAsc', 1
786 is($count, 1, 'MARC21 authorities: one hit on mainentry starts with "shakespeare" sorted by heading ascending');
787 ($auths, $count) = SearchAuthorities(
788 ['mainentry'], ['and'], [''], ['starts'],
789 ['shakespeare'], 0, 10, '', 'HeadingDsc', 1
791 is($count, 1, 'MARC21 authorities: one hit on mainentry starts with "shakespeare" sorted by heading descending');
792 ($auths, $count) = SearchAuthorities(
793 ['match'], ['and'], [''], ['contains'],
794 ['沙士北亞威廉姆'], 0, 10, '', '', 1
796 is($count, 1, 'MARC21 authorities: one hit on match contains "沙士北亞威廉姆"');
798 $UseQueryParser = 1;
800 ($auths, $count) = SearchAuthorities(
801 ['mainentry'], ['and'], [''], ['starts'],
802 ['shakespeare'], 0, 10, '', '', 1
804 is($count, 1, 'MARC21 authorities: one hit on mainentry starts with "shakespeare" (QP)');
805 ($auths, $count) = SearchAuthorities(
806 ['mainentry'], ['and'], [''], ['starts'],
807 ['shakespeare'], 0, 10, '', 'HeadingAsc', 1
809 is($count, 1, 'MARC21 authorities: one hit on mainentry starts with "shakespeare" sorted by heading ascending (QP)');
810 ($auths, $count) = SearchAuthorities(
811 ['mainentry'], ['and'], [''], ['starts'],
812 ['shakespeare'], 0, 10, '', 'HeadingDsc', 1
814 is($count, 1, 'MARC21 authorities: one hit on mainentry starts with "shakespeare" sorted by heading descending (QP)');
815 ($auths, $count) = SearchAuthorities(
816 ['match'], ['and'], [''], ['contains'],
817 ['沙士北亞威廉姆'], 0, 10, '', '', 1
819 is($count, 1, 'MARC21 authorities: one hit on match contains "沙士北亞威廉姆" (QP)');
821 # retrieve records that are larger than the MARC limit of 99,999 octets
822 ( undef, $results_hashref, $facets_loop ) =
823 getRecords('ti:marc the large record', '', [], [ 'biblioserver' ], '20', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
824 is($results_hashref->{biblioserver}->{hits}, 1, "Can do a search that retrieves an over-large bib record (bug 11096)");
825 @newresults = searchResults('opac', $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 10, 0, 0,
826 $results_hashref->{'biblioserver'}->{"RECORDS"});
827 is($newresults[0]->{title}, 'Marc the Large Record', 'Able to render the title for over-large bib record (bug 11096)');
828 is($newresults[0]->{biblionumber}, '300', 'Over-large bib record has the correct biblionumber (bug 11096)');
829 like($newresults[0]->{notes}, qr/This is large note #550/, 'Able to render the notes field for over-large bib record (bug 11096)');
831 # notforloancount should be returned as part of searchResults output
832 ok( defined $newresults[0]->{notforloancount},
833 '\'notforloancount\' defined in searchResults output (Bug 12419)');
834 is( $newresults[0]->{notforloancount}, 2,
835 '\'notforloancount\' == 2 (Bug 12419)');
837 # verify that we don't attempt to sort if no results were returned
838 # because of a query error
839 warning_like {( undef, $results_hashref, $facets_loop ) =
840 getRecords('ccl=( AND )', '', ['title_az'], [ 'biblioserver' ], '20', 0, undef, \%branches, \%itemtypes, 'ccl', undef)
841 } qr/WARNING: query problem with/, 'got warning instead of crash when attempting to run invalid query (bug 9578)';
843 # Test facet calculation
844 my $facets_counter = {};
845 my $facets = C4::Koha::getFacets();
846 # Create a record with a 100$z field
847 my $marc_record = MARC::Record->new;
848 $marc_record->add_fields(
849 [ '001', '1234' ],
850 [ '100', ' ', ' ', a => 'Cohen Arazi, Tomas' ],
851 [ '100', 'z', ' ', a => 'Tomasito' ],
852 [ '245', ' ', ' ', a => 'First try' ]
854 C4::Search::_get_facets_data_from_record( $marc_record, $facets, $facets_counter );
855 is_deeply( { au => { 'Cohen Arazi, Tomas' => 1 } }, $facets_counter,
856 "_get_facets_data_from_record doesn't count 100\$z (Bug 12788)");
857 $marc_record = MARC::Record->new;
858 $marc_record->add_fields(
859 [ '001', '1234' ],
860 [ '100', ' ', ' ', a => 'Cohen Arazi, Tomas' ],
861 [ '100', 'z', ' ', a => 'Tomasito' ],
862 [ '245', ' ', ' ', a => 'Second try' ]
864 C4::Search::_get_facets_data_from_record( $marc_record, $facets, $facets_counter );
865 is_deeply( { au => { 'Cohen Arazi, Tomas' => 2 } }, $facets_counter,
866 "_get_facets_data_from_record correctly counts author facet twice");
868 # Test _get_facets_info
869 my $facets_info = C4::Search::_get_facets_info( $facets );
870 my $expected_facets_info_marc21 = {
871 'au' => { 'expanded' => undef,
872 'label_value' => "Authors" },
873 'holdingbranch' => { 'expanded' => undef,
874 'label_value' => "HoldingLibrary" },
875 'itype' => { 'expanded' => undef,
876 'label_value' => "ItemTypes" },
877 'location' => { 'expanded' => undef,
878 'label_value' => "Location" },
879 'se' => { 'expanded' => undef,
880 'label_value' => "Series" },
881 'su-geo' => { 'expanded' => undef,
882 'label_value' => "Places" },
883 'su-to' => { 'expanded' => undef,
884 'label_value' => "Topics" },
885 'su-ut' => { 'expanded' => undef,
886 'label_value' => "Titles" }
888 is_deeply( $facets_info, $expected_facets_info_marc21,
889 "_get_facets_info returns the correct data");
891 cleanup();
894 sub run_unimarc_search_tests {
895 my $indexing_mode = shift;
896 $datadir = tempdir();
897 system(dirname(__FILE__) . "/zebra_config.pl $datadir unimarc $indexing_mode");
899 mock_marcfromkohafield('unimarc');
900 my $context = new C4::Context("$datadir/etc/koha-conf.xml");
901 $context->set_context();
903 use_ok('C4::Search');
905 # set search syspreferences to a known starting point
906 $QueryStemming = 0;
907 $QueryAutoTruncate = 0;
908 $QueryWeightFields = 0;
909 $QueryFuzzy = 0;
910 $QueryRemoveStopwords = 0;
911 $UseQueryParser = 0;
912 $marcflavour = 'UNIMARC';
914 index_sample_records_and_launch_zebra($datadir, $indexing_mode, 'unimarc');
916 my ( $error, $marcresults, $total_hits ) = SimpleSearch("ti=Järnvägarnas efterfrågan och den svenska industrin", 0, 10);
917 is($total_hits, 1, 'UNIMARC title search');
918 ( $error, $marcresults, $total_hits ) = SimpleSearch("ta=u", 0, 10);
919 is($total_hits, 1, 'UNIMARC target audience = u');
920 ( $error, $marcresults, $total_hits ) = SimpleSearch("ta=k", 0, 10);
921 is($total_hits, 4, 'UNIMARC target audience = k');
922 ( $error, $marcresults, $total_hits ) = SimpleSearch("ta=m", 0, 10);
923 is($total_hits, 3, 'UNIMARC target audience = m');
924 ( $error, $marcresults, $total_hits ) = SimpleSearch("item=EXCLU DU PRET", 0, 10);
925 is($total_hits, 1, 'UNIMARC generic item index (bug 10037)');
927 # authority records
928 use_ok('C4::AuthoritiesMarc');
929 $UseQueryParser = 0;
931 my ($auths, $count) = SearchAuthorities(
932 ['mainentry'], ['and'], [''], ['contains'],
933 ['wil'], 0, 10, '', '', 1
935 is($count, 11, 'UNIMARC authorities: hits on mainentry contains "wil"');
936 ($auths, $count) = SearchAuthorities(
937 ['match'], ['and'], [''], ['contains'],
938 ['wil'], 0, 10, '', '', 1
940 is($count, 11, 'UNIMARC authorities: hits on match contains "wil"');
941 ($auths, $count) = SearchAuthorities(
942 ['mainentry'], ['and'], [''], ['contains'],
943 ['michel'], 0, 20, '', '', 1
945 is($count, 14, 'UNIMARC authorities: hits on mainentry contains "michel"');
946 ($auths, $count) = SearchAuthorities(
947 ['mainmainentry'], ['and'], [''], ['exact'],
948 ['valley'], 0, 20, '', '', 1
950 is($count, 1, 'UNIMARC authorities: hits on mainmainentry = "valley"');
951 ($auths, $count) = SearchAuthorities(
952 ['mainmainentry'], ['and'], [''], ['exact'],
953 ['vall'], 0, 20, '', '', 1
955 is($count, 0, 'UNIMARC authorities: no hits on mainmainentry = "vall"');
956 ($auths, $count) = SearchAuthorities(
957 ['Any'], ['and'], [''], ['starts'],
958 ['jean'], 0, 30, '', '', 1
960 is($count, 24, 'UNIMARC authorities: hits on any starts with "jean"');
962 # Test _get_facets_info
963 my $facets = C4::Koha::getFacets();
964 my $facets_info = C4::Search::_get_facets_info( $facets );
965 my $expected_facets_info_unimarc = {
966 'au' => { 'expanded' => undef,
967 'label_value' => "Authors" },
968 'holdingbranch' => { 'expanded' => undef,
969 'label_value' => "HoldingLibrary" },
970 'location' => { 'expanded' => undef,
971 'label_value' => "Location" },
972 'se' => { 'expanded' => undef,
973 'label_value' => "Series" },
974 'su-geo' => { 'expanded' => undef,
975 'label_value' => "Places" },
976 'su-to' => { 'expanded' => undef,
977 'label_value' => "Topics" },
978 'su-ut' => { 'expanded' => undef,
979 'label_value' => "Titles" }
981 is_deeply( $facets_info, $expected_facets_info_unimarc,
982 "_get_facets_info returns the correct data");
984 cleanup();
987 subtest 'MARC21 + GRS-1' => sub {
988 plan tests => 110;
989 run_marc21_search_tests('grs1');
992 subtest 'MARC21 + DOM' => sub {
993 plan tests => 110;
994 run_marc21_search_tests('dom');
997 subtest 'UNIMARC + GRS-1' => sub {
998 plan tests => 14;
999 run_unimarc_search_tests('grs1');
1002 subtest 'UNIMARC + DOM' => sub {
1003 plan tests => 14;
1004 run_unimarc_search_tests('dom');