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