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