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