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