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