Merge branch 'bug_9239' into 3.12-master
[koha.git] / t / db_dependent / Search.t
1 #!/usr/bin/perl
2 #
3 # This Koha test module is a stub!
4 # Add more tests here!!!
5
6 use strict;
7 use warnings;
8 use utf8;
9
10 use YAML;
11
12 use C4::Debug;
13 require C4::Context;
14
15 use Test::More tests => 78;
16 use Test::MockModule;
17 use MARC::Record;
18 use File::Spec;
19 use File::Basename;
20 use File::Find;
21 use Test::Warn;
22 use File::Temp qw/ tempdir /;
23 use File::Path;
24 use DBI;
25
26 my $datadir = tempdir();
27 system(dirname(__FILE__) . "/zebra_config.pl $datadir");
28 my $sourcedir = dirname(__FILE__) . "/data";
29
30 my $QueryStemming = 0;
31 my $QueryAutoTruncate = 0;
32 my $QueryWeightFields = 0;
33 my $QueryFuzzy = 0;
34 my $QueryRemoveStopwords = 0;
35 my $UseQueryParser = 0;
36 my $contextmodule = new Test::MockModule('C4::Context');
37 $contextmodule->mock('_new_dbh', sub {
38     my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
39     || die "Cannot create handle: $DBI::errstr\n";
40     return $dbh });
41 $contextmodule->mock('preference', sub {
42     my ($self, $pref) = @_;
43     if ($pref eq 'NoZebra') {
44         return 0;
45     } elsif ($pref eq 'marcflavour') {
46         return 'MARC21';
47     } elsif ($pref eq 'QueryStemming') {
48         return $QueryStemming;
49     } elsif ($pref eq 'QueryAutoTruncate') {
50         return $QueryAutoTruncate;
51     } elsif ($pref eq 'QueryWeightFields') {
52         return $QueryWeightFields;
53     } elsif ($pref eq 'QueryFuzzy') {
54         return $QueryFuzzy;
55     } elsif ($pref eq 'QueryRemoveStopwords') {
56         return $QueryRemoveStopwords;
57     } elsif ($pref eq 'UseQueryParser') {
58         return $UseQueryParser;
59     } elsif ($pref eq 'maxRecordsForFacets') {
60         return 20;
61     } elsif ($pref eq 'FacetLabelTruncationLength') {
62         return 20;
63     } elsif ($pref eq 'OpacHiddenItems') {
64         return '';
65     } elsif ($pref eq 'AlternateHoldingsField') {
66         return '490av';
67     } else {
68         warn "The syspref $pref was requested but I don't know what to say; this indicates that the test requires updating"
69             unless $pref =~ m/(XSLT|item|branch|holding|image|insecure)/i;
70         return 0;
71     }
72 });
73 $contextmodule->mock('marcfromkohafield', sub {
74     my %hash = (
75         '' => {
76             'biblio.biblionumber' => [ '999', 'c' ],
77             'items.barcode' => ['952', 'p' ],
78             'items.booksellerid' => ['952', 'e' ],
79             'items.ccode' => ['952', '8' ],
80             'items.cn_sort' => ['952', '6' ],
81             'items.cn_source' => ['952', '2' ],
82             'items.coded_location_qualifier' => ['952', 'f' ],
83             'items.copynumber' => ['952', 't' ],
84             'items.damaged' => ['952', '4' ],
85             'items.dateaccessioned' => ['952', 'd' ],
86             'items.datelastborrowed' => ['952', 's' ],
87             'items.datelastseen' => ['952', 'r' ],
88             'items.enumchron' => ['952', 'h' ],
89             'items.holdingbranch' => ['952', 'b' ],
90             'items.homebranch' => ['952', 'a' ],
91             'items.issues' => ['952', 'l' ],
92             'items.itemcallnumber' => ['952', 'o' ],
93             'items.itemlost' => ['952', '1' ],
94             'items.itemnotes' => ['952', 'z' ],
95             'items.itemnumber' => ['952', '9' ],
96             'items.itype' => ['952', 'y' ],
97             'items.location' => ['952', 'c' ],
98             'items.materials' => ['952', '3' ],
99             'items.nonpublicnote' => ['952', 'x' ],
100             'items.notforloan' => ['952', '7' ],
101             'items.onloan' => ['952', 'q' ],
102             'items.price' => ['952', 'g' ],
103             'items.renewals' => ['952', 'm' ],
104             'items.replacementprice' => ['952', 'v' ],
105             'items.replacementpricedate' => ['952', 'w' ],
106             'items.reserves' => ['952', 'n' ],
107             'items.restricted' => ['952', '5' ],
108             'items.stack' => ['952', 'j' ],
109             'items.uri' => ['952', 'u' ],
110             'items.wthdrawn' => ['952', '0' ]
111             }
112         );
113         return \%hash;
114 });
115 $contextmodule->mock('queryparser', sub {
116     my $QParser     = Koha::QueryParser::Driver::PQF->new();
117     $QParser->load_config("$datadir/etc/searchengine/queryparser.yaml");
118     return $QParser;
119 });
120 my $context = new C4::Context("$datadir/etc/koha-conf.xml");
121 $context->set_context();
122
123 use_ok('C4::Search');
124
125 foreach my $string ("Leçon","modèles") {
126     my @results=C4::Search::_remove_stopwords($string,"kw");
127     $debug && warn "$string ",Dump(@results);
128     ok($results[0] eq $string,"$string is not modified");
129 }
130
131 foreach my $string ("A book about the stars") {
132     my @results=C4::Search::_remove_stopwords($string,"kw");
133     $debug && warn "$string ",Dump(@results);
134     ok($results[0] ne $string,"$results[0] from $string");
135 }
136
137 my $indexes = C4::Search::getIndexes();
138 is(scalar(grep(/^ti$/, @$indexes)), 1, "Title index supported");
139
140 my $bibliomodule = new Test::MockModule('C4::Biblio');
141 $bibliomodule->mock('_get_inverted_marc_field_map', sub {
142     my %hash = (
143         '' => {
144             '245' => { 'sfs' => { 'a' => [ [ 'biblio', 'title' ] ], 'b' => [ [ 'bibliosubtitle', 'subtitle' ] ] },
145                 'list' => [ [ 'a', 'biblio', 'title' ], [ 'b', 'bibliosubtitle', 'subtitle' ] ]
146             },
147             '100' => {
148                 'sfs' => { 'a' => [ [ 'biblio', 'author' ] ] },
149                 'list' => [ [ 'a', 'biblio', 'author' ] ]
150             },
151             '999' => {
152                 'sfs' => { 'c' => [ [ 'biblio', 'biblionumber' ] ], 'd' => [ [ 'biblioitems', 'biblioitemnumber' ] ] },
153                 'list' => [ [ 'd', 'biblioitems', 'biblioitemnumber' ], [ 'c', 'biblio', 'biblionumber' ] ]
154             },
155             '020' => {
156                 'sfs' => { 'a' => [ [ 'biblioitems', 'isbn' ] ] },
157                 'list' => [ [ 'a', 'biblioitems', 'isbn' ] ]
158             }
159         }
160     );
161     return \%hash;
162 });
163 my $dbh = C4::Context->dbh;
164 $dbh->{mock_add_resultset} = {
165     sql     => 'SHOW COLUMNS FROM items',
166     results => [
167         [ 'itemnumber' ], [ 'biblionumber' ], [ 'biblioitemnumber' ],
168         [ 'barcode' ], [ 'dateaccessioned' ], [ 'booksellerid' ],
169         [ 'homebranch' ], [ 'price' ], [ 'replacementprice' ],
170         [ 'replacementpricedate' ], [ 'datelastborrowed' ], [ 'datelastseen' ],
171         [ 'stack' ], [ 'notforloan' ], [ 'damaged' ],
172         [ 'itemlost' ], [ 'wthdrawn' ], [ 'itemcallnumber' ],
173         [ 'issues' ], [ 'renewals' ], [ 'reserves' ],
174         [ 'restricted' ], [ 'itemnotes' ], [ 'nonpublicnote' ],
175         [ 'holdingbranch' ], [ 'paidfor' ], [ 'timestamp' ],
176         [ 'location' ], [ 'permanent_location' ], [ 'onloan' ],
177         [ 'cn_source' ], [ 'cn_sort' ], [ 'ccode' ],
178         [ 'materials' ], [ 'uri' ], [ 'itype' ],
179         [ 'more_subfields_xml' ], [ 'enumchron' ], [ 'copynumber' ],
180         [ 'stocknumber' ],
181     ]
182 };
183
184 my %branches = (
185     'CPL' => { 'branchaddress1' => 'Jefferson Summit', 'branchcode' => 'CPL', 'branchname' => 'Centerville', },
186     'FFL' => { 'branchaddress1' => 'River Station', 'branchcode' => 'FFL', 'branchname' => 'Fairfield', },
187     'FPL' => { 'branchaddress1' => 'Hickory Squere', 'branchcode' => 'FPL', 'branchname' => 'Fairview', },
188     'FRL' => { 'branchaddress1' => 'Smith Heights', 'branchcode' => 'FRL', 'branchname' => 'Franklin', },
189     'IPT' => { 'branchaddress1' => '', 'branchcode' => 'IPT', 'branchname' => "Institut Protestant de Théologie", },
190     'LPL' => { 'branchaddress1' => 'East Hills', 'branchcode' => 'LPL', 'branchname' => 'Liberty', },
191     'MPL' => { 'branchaddress1' => '372 Forest Street', 'branchcode' => 'MPL', 'branchname' => 'Midway', },
192     'PVL' => { 'branchaddress1' => 'Meadow Grove', 'branchcode' => 'PVL', 'branchname' => 'Pleasant Valley', },
193     'RPL' => { 'branchaddress1' => 'Johnson Terrace', 'branchcode' => 'RPL', 'branchname' => 'Riverside', },
194     'SPL' => { 'branchaddress1' => 'Highland Boulevard', 'branchcode' => 'SPL', 'branchname' => 'Springfield', },
195     'S'   => { 'branchaddress1' => '', 'branchcode' => 'S', 'branchname' => 'Test', },
196     'TPL' => { 'branchaddress1' => 'Valley Way', 'branchcode' => 'TPL', 'branchname' => 'Troy', },
197     'UPL' => { 'branchaddress1' => 'Chestnut Hollow', 'branchcode' => 'UPL', 'branchname' => 'Union', },
198 );
199 my %itemtypes = (
200     'BK' => { 'imageurl' => 'bridge/book.gif', 'summary' => '', 'itemtype' => 'BK', 'description' => 'Books' },
201     'CF' => { 'imageurl' => 'bridge/computer_file.gif', 'summary' => '', 'itemtype' => 'CF', 'description' => 'Computer Files' },
202     'CR' => { 'imageurl' => 'bridge/periodical.gif', 'summary' => '', 'itemtype' => 'CR', 'description' => 'Continuing Resources' },
203     'MP' => { 'imageurl' => 'bridge/map.gif', 'summary' => '', 'itemtype' => 'MP', 'description' => 'Maps' },
204     'MU' => { 'imageurl' => 'bridge/sound.gif', 'summary' => '', 'itemtype' => 'MU', 'description' => 'Music' },
205     'MX' => { 'imageurl' => 'bridge/kit.gif', 'summary' => '', 'itemtype' => 'MX', 'description' => 'Mixed Materials' },
206     'REF' => { 'imageurl' => '', 'summary' => '', 'itemtype' => 'REF', 'description' => 'Reference' },
207     'VM' => { 'imageurl' => 'bridge/dvd.gif', 'summary' => '', 'itemtype' => 'VM', 'description' => 'Visual Materials' },
208 );
209
210 unlink("$datadir/zebra.log");
211 system("zebraidx -c $datadir/etc/koha/zebradb/zebra-biblios.cfg  -v none,fatal,warn  -g iso2709 -d biblios init");
212 system("zebraidx -c $datadir/etc/koha/zebradb/zebra-biblios.cfg  -v none,fatal,warn   -g iso2709 -d biblios update $sourcedir/zebraexport/biblio");
213 system("zebraidx -c $datadir/etc/koha/zebradb/zebra-biblios.cfg  -v none,fatal,warn  -g iso2709 -d biblios commit");
214
215 my $child = fork();
216 if ($child == 0) {
217     exec("zebrasrv -f $datadir/etc/koha-conf.xml -v none,request -l $datadir/zebra.log");
218     exit;
219 }
220
221 sleep(1);
222
223 my ($biblionumber, $title);
224 my $record = MARC::Record->new;
225
226 $record->add_fields(
227         [ '020', ' ', ' ', a => '9788522421718' ],
228         [ '245', '0', '0', a => 'Administração da produção /' ]
229         );
230 ($biblionumber,undef,$title) = FindDuplicate($record);
231 is($biblionumber, 51, 'Found duplicate with ISBN');
232
233 $record = MARC::Record->new;
234
235 $record->add_fields(
236         [ '100', '1', ' ', a => 'Carter, Philip J.' ],
237         [ '245', '1', '4', a => 'Test your emotional intelligence :' ]
238         );
239 ($biblionumber,undef,$title) = FindDuplicate($record);
240 is($biblionumber, 203, 'Found duplicate with author/title');
241
242 # Testing SimpleSearch
243
244 my ( $error, $marcresults, $total_hits ) = SimpleSearch("book", 0, 9);
245
246 is(scalar @$marcresults, 9, "SimpleSearch retrieved requested number of records");
247 is($total_hits, 101, "SimpleSearch for 'book' matched right number of records");
248 is($error, undef, "SimpleSearch does not return an error when successful");
249
250 my $marcresults2;
251 ( $error, $marcresults2, $total_hits ) = SimpleSearch("book", 5, 5);
252 is($marcresults->[5], $marcresults2->[0], "SimpleSearch cursor functions");
253
254 ( $error, $marcresults, $total_hits ) = SimpleSearch("kw=book", 0, 10);
255 is($total_hits, 101, "SimpleSearch handles simple CCL");
256
257 # Testing getRecords
258
259 my $results_hashref;
260 my $facets_loop;
261 ( undef, $results_hashref, $facets_loop ) =
262     getRecords('kw:book', 'book', [], [ 'biblioserver' ], '19', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
263 is($results_hashref->{biblioserver}->{hits}, 101, "getRecords keyword search for 'book' matched right number of records");
264 is(scalar @{$results_hashref->{biblioserver}->{RECORDS}}, 19, "getRecords returned requested number of records");
265 my $record5 = $results_hashref->{biblioserver}->{RECORDS}->[5];
266 ( undef, $results_hashref, $facets_loop ) =
267     getRecords('kw:book', 'book', [], [ 'biblioserver' ], '20', 5, undef, \%branches, \%itemtypes, 'ccl', undef);
268 ok(!defined $results_hashref->{biblioserver}->{RECORDS}->[0] &&
269     !defined $results_hashref->{biblioserver}->{RECORDS}->[1] &&
270     !defined $results_hashref->{biblioserver}->{RECORDS}->[2] &&
271     !defined $results_hashref->{biblioserver}->{RECORDS}->[3] &&
272     !defined $results_hashref->{biblioserver}->{RECORDS}->[4] &&
273     $results_hashref->{biblioserver}->{RECORDS}->[5] eq $record5, "getRecords cursor works");
274
275 ( undef, $results_hashref, $facets_loop ) =
276     getRecords('ti:book', 'ti:book', [], [ 'biblioserver' ], '20', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
277 is($results_hashref->{biblioserver}->{hits}, 11, "getRecords title search for 'book' matched right number of records");
278
279 ( undef, $results_hashref, $facets_loop ) =
280     getRecords('au:Lessig', 'au:Lessig', [], [ 'biblioserver' ], '20', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
281 is($results_hashref->{biblioserver}->{hits}, 4, "getRecords title search for 'Australia' matched right number of records");
282
283 ( undef, $results_hashref, $facets_loop ) =
284     getRecords('salud', 'salud', [], [ 'biblioserver' ], '19', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
285 ok(MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[0])->title_proper() =~ m/^Efectos del ambiente/ &&
286     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' &&
287     MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[18])->title_proper() =~ m/^Indicadores de resultados identificados/
288     , "Simple relevance sorting in getRecords matches old behavior");
289
290 ( undef, $results_hashref, $facets_loop ) =
291     getRecords('salud', 'salud', [ 'author_az' ], [ 'biblioserver' ], '38', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
292 ok(MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[0])->title_proper() =~ m/la enfermedad laboral\^ies$/ &&
293     MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[6])->title_proper() =~ m/^Indicadores de resultados identificados/ &&
294     MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[18])->title_proper() eq 'World health statistics 2009^ien'
295     , "Simple ascending author sorting in getRecords matches old behavior");
296
297 ( undef, $results_hashref, $facets_loop ) =
298     getRecords('salud', 'salud', [ 'author_za' ], [ 'biblioserver' ], '38', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
299 ok(MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[0])->title_proper() eq 'World health statistics 2009^ien' &&
300     MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[12])->title_proper() =~ m/^Indicadores de resultados identificados/ &&
301     MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[18])->title_proper() =~ m/la enfermedad laboral\^ies$/
302     , "Simple descending author sorting in getRecords matches old behavior");
303
304 ( undef, $results_hashref, $facets_loop ) =
305     getRecords('salud', 'salud', [ 'pubdate_asc' ], [ 'biblioserver' ], '38', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
306 ok(MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[0])->title_proper() eq 'Manual de higiene industrial^ies' &&
307     MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[7])->title_proper() =~ m/seguridad e higiene del trabajo\^ies$/ &&
308     MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[18])->title_proper() =~ m/^Indicadores de resultados identificados/
309     , "Simple ascending publication date sorting in getRecords matches old behavior");
310
311 ( undef, $results_hashref, $facets_loop ) =
312     getRecords('salud', 'salud', [ 'pubdate_dsc' ], [ 'biblioserver' ], '38', 0, undef, \%branches, \%itemtypes, 'ccl', undef);
313 ok(MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[0])->title_proper() =~ m/^Estado de salud/ &&
314     MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[7])->title_proper() eq 'World health statistics 2009^ien' &&
315     MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[18])->title_proper() eq 'Manual de higiene industrial^ies'
316     , "Simple descending publication date sorting in getRecords matches old behavior");
317
318 ( undef, $results_hashref, $facets_loop ) =
319     getRecords('books', 'books', [ 'relevance' ], [ 'biblioserver' ], '20', 0, undef, \%branches, \%itemtypes, undef, 1);
320 $record = MARC::Record::new_from_usmarc($results_hashref->{biblioserver}->{RECORDS}->[0]);
321 is($record->title_proper(), 'books', "Scan returned requested item");
322 is($record->subfield('100', 'a'), 2, "Scan returned correct number of records matching term");
323
324 # Time to test buildQuery and searchResults too.
325
326 my ( $query, $simple_query, $query_cgi,
327 $query_desc, $limit, $limit_cgi, $limit_desc,
328 $stopwords_removed, $query_type );
329 ( $error, $query, $simple_query, $query_cgi,
330 $query_desc, $limit, $limit_cgi, $limit_desc,
331 $stopwords_removed, $query_type ) = buildQuery([], [ 'salud' ], [], [], [], 0, 'en');
332 like($query, qr/kw\W.*salud/, "Built CCL keyword query");
333
334 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
335 is($results_hashref->{biblioserver}->{hits}, 19, "getRecords generated keyword search for 'salud' matched right number of records");
336
337 my @newresults = searchResults('opac', $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 18, 0, 0,
338     $results_hashref->{'biblioserver'}->{"RECORDS"});
339 is(scalar @newresults,18, "searchResults returns requested number of hits");
340
341 ( $error, $query, $simple_query, $query_cgi,
342 $query_desc, $limit, $limit_cgi, $limit_desc,
343 $stopwords_removed, $query_type ) = buildQuery([ 'and' ], [ 'salud', 'higiene' ], [], [], [], 0, 'en');
344 like($query, qr/kw\W.*salud\W.*and.*kw\W.*higiene/, "Built composed explicit-and CCL keyword query");
345
346 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
347 is($results_hashref->{biblioserver}->{hits}, 3, "getRecords generated composed keyword search for 'salud' explicit-and 'higiene' matched right number of records");
348
349 ( $error, $query, $simple_query, $query_cgi,
350 $query_desc, $limit, $limit_cgi, $limit_desc,
351 $stopwords_removed, $query_type ) = buildQuery([ 'or' ], [ 'salud', 'higiene' ], [], [], [], 0, 'en');
352 like($query, qr/kw\W.*salud\W.*or.*kw\W.*higiene/, "Built composed explicit-or CCL keyword query");
353
354 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
355 is($results_hashref->{biblioserver}->{hits}, 20, "getRecords generated composed keyword search for 'salud' explicit-or 'higiene' matched right number of records");
356
357 ( $error, $query, $simple_query, $query_cgi,
358 $query_desc, $limit, $limit_cgi, $limit_desc,
359 $stopwords_removed, $query_type ) = buildQuery([], [ 'salud', 'higiene' ], [], [], [], 0, 'en');
360 like($query, qr/kw\W.*salud\W.*and.*kw\W.*higiene/, "Built composed implicit-and CCL keyword query");
361
362 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
363 is($results_hashref->{biblioserver}->{hits}, 3, "getRecords generated composed keyword search for 'salud' implicit-and 'higiene' matched right number of records");
364
365 ( $error, $query, $simple_query, $query_cgi,
366 $query_desc, $limit, $limit_cgi, $limit_desc,
367 $stopwords_removed, $query_type ) = buildQuery([], [ 'salud' ], [ 'kw' ], [ 'su-to:Laboratorios' ], [], 0, 'en');
368 like($query, qr/kw\W.*salud\W*and\W*su-to\W.*Laboratorios/, "Faceted query generated correctly");
369 unlike($query_desc, qr/Laboratorios/, "Facets not included in query description");
370
371 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
372 is($results_hashref->{biblioserver}->{hits}, 2, "getRecords generated faceted search matched right number of records");
373
374
375 ( $error, $query, $simple_query, $query_cgi,
376 $query_desc, $limit, $limit_cgi, $limit_desc,
377 $stopwords_removed, $query_type ) = buildQuery([], [ '' ], [ 'kw' ], [ 'mc-itype:MP', 'mc-itype:MU' ], [], 0, 'en');
378
379 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
380 is($results_hashref->{biblioserver}->{hits}, 2, "getRecords generated mc-faceted search matched right number of records");
381
382
383 ( $error, $query, $simple_query, $query_cgi,
384 $query_desc, $limit, $limit_cgi, $limit_desc,
385 $stopwords_removed, $query_type ) = buildQuery([], [ '' ], [ 'kw' ], [ 'mc-loc:GEN', 'branch:FFL' ], [], 0, 'en');
386
387 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
388 is($results_hashref->{biblioserver}->{hits}, 2, "getRecords generated multi-faceted search matched right number of records");
389
390
391 # FIXME: the availability limit does not actually work, so for the moment we
392 # are just checking that it behaves consistently
393 ( $error, $query, $simple_query, $query_cgi,
394 $query_desc, $limit, $limit_cgi, $limit_desc,
395 $stopwords_removed, $query_type ) = buildQuery([], [ '' ], [ 'kw' ], [ 'available' ], [], 0, 'en');
396
397 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
398 is($results_hashref->{biblioserver}->{hits}, 26, "getRecords generated availability-limited search matched right number of records");
399
400 @newresults = searchResults('opac', $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 17, 0, 0,
401     $results_hashref->{'biblioserver'}->{"RECORDS"});
402 my $allavailable = 'true';
403 foreach my $result (@newresults) {
404     $allavailable = 'false' unless $result->{availablecount} > 0;
405 }
406 is ($allavailable, 'true', 'All records have at least one item available');
407
408
409 ( $error, $query, $simple_query, $query_cgi,
410 $query_desc, $limit, $limit_cgi, $limit_desc,
411 $stopwords_removed, $query_type ) = buildQuery([], [ 'pqf=@attr 1=_ALLRECORDS @attr 2=103 ""' ], [], [], [], 0, 'en');
412
413 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
414 is($results_hashref->{biblioserver}->{hits}, 178, "getRecords on _ALLRECORDS PQF returned all records");
415
416 ( $error, $query, $simple_query, $query_cgi,
417 $query_desc, $limit, $limit_cgi, $limit_desc,
418 $stopwords_removed, $query_type ) = buildQuery([], [ 'pqf=@attr 1=1016 "Lessig"' ], [], [], [], 0, 'en');
419
420 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
421 is($results_hashref->{biblioserver}->{hits}, 4, "getRecords PQF author search for Lessig returned proper number of matches");
422
423 ( $error, $query, $simple_query, $query_cgi,
424 $query_desc, $limit, $limit_cgi, $limit_desc,
425 $stopwords_removed, $query_type ) = buildQuery([], [ 'ccl=au:Lessig' ], [], [], [], 0, 'en');
426
427 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
428 is($results_hashref->{biblioserver}->{hits}, 4, "getRecords CCL author search for Lessig returned proper number of matches");
429
430 ( $error, $query, $simple_query, $query_cgi,
431 $query_desc, $limit, $limit_cgi, $limit_desc,
432 $stopwords_removed, $query_type ) = buildQuery([], [ 'cql=dc.author any lessig' ], [], [], [], 0, 'en');
433
434 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
435 is($results_hashref->{biblioserver}->{hits}, 4, "getRecords CQL author search for Lessig returned proper number of matches");
436
437 $QueryStemming = $QueryAutoTruncate = $QueryFuzzy = $QueryRemoveStopwords = 0;
438 $QueryWeightFields = 1;
439 ( $error, $query, $simple_query, $query_cgi,
440 $query_desc, $limit, $limit_cgi, $limit_desc,
441 $stopwords_removed, $query_type ) = buildQuery([], [ 'salud' ], [ 'kw' ], [], [], 0, 'en');
442
443 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
444 is($results_hashref->{biblioserver}->{hits}, 19, "Weighted query returned correct number of results");
445 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");
446
447 $QueryStemming = $QueryWeightFields = $QueryFuzzy = $QueryRemoveStopwords = 0;
448 $QueryAutoTruncate = 1;
449 ( $error, $query, $simple_query, $query_cgi,
450 $query_desc, $limit, $limit_cgi, $limit_desc,
451 $stopwords_removed, $query_type ) = buildQuery([], [ 'medic' ], [ 'kw' ], [], [], 0, 'en');
452
453 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
454 is($results_hashref->{biblioserver}->{hits}, 5, "Search for 'medic' returns matches  with automatic truncation on");
455
456 ( $error, $query, $simple_query, $query_cgi,
457 $query_desc, $limit, $limit_cgi, $limit_desc,
458 $stopwords_removed, $query_type ) = buildQuery([], [ 'medic*' ], [ 'kw' ], [], [], 0, 'en');
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}, 5, "Search for 'medic*' returns matches with automatic truncation on");
462
463 $QueryStemming = $QueryWeightFields = $QueryFuzzy = $QueryRemoveStopwords = $QueryAutoTruncate = 0;
464 ( $error, $query, $simple_query, $query_cgi,
465 $query_desc, $limit, $limit_cgi, $limit_desc,
466 $stopwords_removed, $query_type ) = buildQuery([], [ 'medic' ], [ 'kw' ], [], [], 0, 'en');
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}, undef, "Search for 'medic' returns no matches with automatic truncation off");
470
471 ( $error, $query, $simple_query, $query_cgi,
472 $query_desc, $limit, $limit_cgi, $limit_desc,
473 $stopwords_removed, $query_type ) = buildQuery([], [ 'medic*' ], [ 'kw' ], [], [], 0, 'en');
474
475 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
476 is($results_hashref->{biblioserver}->{hits}, 5, "Search for 'medic*' returns matches with automatic truncation off");
477
478 $QueryStemming = $QueryWeightFields = 1;
479 $QueryFuzzy = $QueryRemoveStopwords = $QueryAutoTruncate = 0;
480 ( $error, $query, $simple_query, $query_cgi,
481 $query_desc, $limit, $limit_cgi, $limit_desc,
482 $stopwords_removed, $query_type ) = buildQuery([], [ 'pressed' ], [ 'kw' ], [], [], 0, 'en');
483
484 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
485 is($results_hashref->{biblioserver}->{hits}, 7, "Search for 'pressed' returns matches when stemming (and query weighting) is on");
486
487 $QueryStemming = $QueryWeightFields = $QueryFuzzy = $QueryRemoveStopwords = $QueryAutoTruncate = 0;
488 ( $error, $query, $simple_query, $query_cgi,
489 $query_desc, $limit, $limit_cgi, $limit_desc,
490 $stopwords_removed, $query_type ) = buildQuery([], [ 'pressed' ], [ 'kw' ], [], [], 0, 'en');
491
492 ($error, $results_hashref, $facets_loop) = getRecords($query,$simple_query,[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
493 is($results_hashref->{biblioserver}->{hits}, undef, "Search for 'pressed' returns no matches when stemming is off");
494
495 # Let's see what happens when we pass bad data into these routines.
496 # We have to catch warnings since we're not very good about returning errors.
497
498 warning_like { ( $error, $marcresults, $total_hits ) = SimpleSearch("@==ccl blah", 0, 9) } qr/CCL parsing error/,
499     "SimpleSearch warns about CCL parsing error with nonsense query";
500 isnt($error, undef, "SimpleSearch returns an error when passed gibberish");
501
502 warning_like {( undef, $results_hashref, $facets_loop ) =
503     getRecords('kw:book', 'book', [], [ 'biblioserver' ], '19', 0, undef, \%branches, \%itemtypes, 'nonsense', undef) }
504     qr/Unknown query_type/, "getRecords warns about unknown query type";
505
506 warning_like {( undef, $results_hashref, $facets_loop ) =
507     getRecords('pqf=@attr 1=4 "title"', 'pqf=@attr 1=4 "title"', [], [ 'biblioserver' ], '19', 0, undef, \%branches, \%itemtypes, '', undef) }
508     qr/WARNING: query problem/, "getRecords warns when query type is not specified for non-CCL query";
509
510 # Let's just test a few other bits and bobs, just for fun
511
512 ($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);
513 @newresults = searchResults('intranet', $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 17, 0, 0,
514     $results_hashref->{'biblioserver'}->{"RECORDS"});
515 is($newresults[0]->{'alternateholdings_count'}, 1, 'Alternate holdings filled in correctly');
516
517 END {
518     if ($child) {
519         kill 9, $child;
520
521         # Clean up the Zebra files since the child process was just shot
522         rmtree $datadir;
523     }
524 }
525
526 # Testing exploding indexes
527 my $term;
528 my $searchmodule = new Test::MockModule('C4::Search');
529 $searchmodule->mock('SimpleSearch', sub {
530     my $query = shift;
531
532     is($query, "he:$term", "Searching for expected term '$term' for exploding") or return '', [], 0;
533
534     my $record = MARC::Record->new;
535     if ($query =~ m/Arizona/) {
536         $record->add_fields(
537             [ '001', '1234' ],
538             [ '151', ' ', ' ', a => 'Arizona' ],
539             [ '551', ' ', ' ', a => 'United States', w => 'g' ],
540             [ '551', ' ', ' ', a => 'Maricopa County', w => 'h' ],
541             [ '551', ' ', ' ', a => 'Navajo County', w => 'h' ],
542             [ '551', ' ', ' ', a => 'Pima County', w => 'h' ],
543             [ '551', ' ', ' ', a => 'New Mexico' ],
544             );
545     }
546     return '', [ $record->as_usmarc() ], 1;
547 });
548
549 $UseQueryParser = 1;
550 $term = 'Arizona';
551 ( $error, $query, $simple_query, $query_cgi,
552 $query_desc, $limit, $limit_cgi, $limit_desc,
553 $stopwords_removed, $query_type ) = buildQuery([], [ $term ], [ 'su-br' ], [  ], [], 0, 'en');
554 matchesExplodedTerms("Advanced search for broader subjects", $query, 'Arizona', 'United States');
555
556 ( $error, $query, $simple_query, $query_cgi,
557 $query_desc, $limit, $limit_cgi, $limit_desc,
558 $stopwords_removed, $query_type ) = buildQuery([], [ $term ], [ 'su-na' ], [  ], [], 0, 'en');
559 matchesExplodedTerms("Advanced search for narrower subjects", $query, 'Arizona', 'Maricopa County', 'Navajo County', 'Pima County');
560
561 ( $error, $query, $simple_query, $query_cgi,
562 $query_desc, $limit, $limit_cgi, $limit_desc,
563 $stopwords_removed, $query_type ) = buildQuery([], [ $term ], [ 'su-rl' ], [  ], [], 0, 'en');
564 matchesExplodedTerms("Advanced search for related subjects", $query, 'Arizona', 'United States', 'Maricopa County', 'Navajo County', 'Pima County');
565
566 ( $error, $query, $simple_query, $query_cgi,
567 $query_desc, $limit, $limit_cgi, $limit_desc,
568 $stopwords_removed, $query_type ) = buildQuery([], [ "$term", 'history' ], [ 'su-rl', 'kw' ], [  ], [], 0, 'en');
569 matchesExplodedTerms("Advanced search for related subjects and keyword 'history' searches related subjects", $query, 'Arizona', 'United States', 'Maricopa County', 'Navajo County', 'Pima County');
570 like($query, qr/history/, "Advanced search for related subjects and keyword 'history' searches for 'history'");
571
572 ( $error, $query, $simple_query, $query_cgi,
573 $query_desc, $limit, $limit_cgi, $limit_desc,
574 $stopwords_removed, $query_type ) = buildQuery([], [ 'history', "$term" ], [ 'kw', 'su-rl' ], [  ], [], 0, 'en');
575 matchesExplodedTerms("Order of terms doesn't matter for advanced search", $query, 'Arizona', 'United States', 'Maricopa County', 'Navajo County', 'Pima County');
576 like($query, qr/history/, "Order of terms doesn't matter for advanced search");
577
578 ( $error, $query, $simple_query, $query_cgi,
579 $query_desc, $limit, $limit_cgi, $limit_desc,
580 $stopwords_removed, $query_type ) = buildQuery([], [ "su-br($term)" ], [  ], [  ], [], 0, 'en');
581 matchesExplodedTerms("Simple search for broader subjects", $query, 'Arizona', 'United States');
582
583 ( $error, $query, $simple_query, $query_cgi,
584 $query_desc, $limit, $limit_cgi, $limit_desc,
585 $stopwords_removed, $query_type ) = buildQuery([], [ "su-na($term)" ], [  ], [  ], [], 0, 'en');
586 matchesExplodedTerms("Simple search for narrower subjects", $query, 'Arizona', 'Maricopa County', 'Navajo County', 'Pima County');
587
588 ( $error, $query, $simple_query, $query_cgi,
589 $query_desc, $limit, $limit_cgi, $limit_desc,
590 $stopwords_removed, $query_type ) = buildQuery([], [ "su-rl($term)" ], [  ], [  ], [], 0, 'en');
591 matchesExplodedTerms("Simple search for related subjects", $query, 'Arizona', 'United States', 'Maricopa County', 'Navajo County', 'Pima County');
592
593 ( $error, $query, $simple_query, $query_cgi,
594 $query_desc, $limit, $limit_cgi, $limit_desc,
595 $stopwords_removed, $query_type ) = buildQuery([], [ "history && su-rl($term)" ], [  ], [  ], [], 0, 'en');
596 matchesExplodedTerms("Simple search for related subjects and keyword 'history' searches related subjects", $query, 'Arizona', 'United States', 'Maricopa County', 'Navajo County', 'Pima County');
597 like($query, qr/history/, "Simple search for related subjects and keyword 'history' searches for 'history'");
598
599 sub matchesExplodedTerms {
600     my ($message, $query, @terms) = @_;
601     my $match = '(' . join ('|', map { " \@attr 1=Subject \@attr 4=1 \"$_\"" } @terms) . "){" . scalar(@terms) . "}";
602     like($query, qr/$match/, $message);
603 }
604
605 1;