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