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