Bug 10640: (follow-up) bump up the number of expected tests in Bookseller.t
[koha.git] / t / db_dependent / Search.t
index 3a517a8..f786a69 100644 (file)
@@ -12,7 +12,7 @@ use YAML;
 use C4::Debug;
 require C4::Context;
 
-use Test::More tests => 57;
+use Test::More tests => 79;
 use Test::MockModule;
 use MARC::Record;
 use File::Spec;
@@ -32,6 +32,7 @@ my $QueryAutoTruncate = 0;
 my $QueryWeightFields = 0;
 my $QueryFuzzy = 0;
 my $QueryRemoveStopwords = 0;
+my $UseQueryParser = 0;
 my $contextmodule = new Test::MockModule('C4::Context');
 $contextmodule->mock('_new_dbh', sub {
     my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
@@ -39,9 +40,7 @@ $contextmodule->mock('_new_dbh', sub {
     return $dbh });
 $contextmodule->mock('preference', sub {
     my ($self, $pref) = @_;
-    if ($pref eq 'NoZebra') {
-        return 0;
-    } elsif ($pref eq 'marcflavour') {
+    if ($pref eq 'marcflavour') {
         return 'MARC21';
     } elsif ($pref eq 'QueryStemming') {
         return $QueryStemming;
@@ -53,6 +52,8 @@ $contextmodule->mock('preference', sub {
         return $QueryFuzzy;
     } elsif ($pref eq 'QueryRemoveStopwords') {
         return $QueryRemoveStopwords;
+    } elsif ($pref eq 'UseQueryParser') {
+        return $UseQueryParser;
     } elsif ($pref eq 'maxRecordsForFacets') {
         return 20;
     } elsif ($pref eq 'FacetLabelTruncationLength') {
@@ -63,7 +64,7 @@ $contextmodule->mock('preference', sub {
         return '490av';
     } else {
         warn "The syspref $pref was requested but I don't know what to say; this indicates that the test requires updating"
-            unless $pref =~ m/(XSLT|item|branch|holding|image|insecure)/i;
+            unless $pref =~ m/(XSLT|item|branch|holding|image)/i;
         return 0;
     }
 });
@@ -104,11 +105,16 @@ $contextmodule->mock('marcfromkohafield', sub {
             'items.restricted' => ['952', '5' ],
             'items.stack' => ['952', 'j' ],
             'items.uri' => ['952', 'u' ],
-            'items.wthdrawn' => ['952', '0' ]
+            'items.withdrawn' => ['952', '0' ]
             }
         );
         return \%hash;
 });
+$contextmodule->mock('queryparser', sub {
+    my $QParser     = Koha::QueryParser::Driver::PQF->new();
+    $QParser->load_config("$datadir/etc/searchengine/queryparser.yaml");
+    return $QParser;
+});
 my $context = new C4::Context("$datadir/etc/koha-conf.xml");
 $context->set_context();
 
@@ -156,12 +162,15 @@ my $dbh = C4::Context->dbh;
 $dbh->{mock_add_resultset} = {
     sql     => 'SHOW COLUMNS FROM items',
     results => [
+        [ 'rows' ], # seems like $sth->rows is getting called
+                    # implicitly, so we need this to make
+                    # DBD::Mock return all of the results
         [ 'itemnumber' ], [ 'biblionumber' ], [ 'biblioitemnumber' ],
         [ 'barcode' ], [ 'dateaccessioned' ], [ 'booksellerid' ],
         [ 'homebranch' ], [ 'price' ], [ 'replacementprice' ],
         [ 'replacementpricedate' ], [ 'datelastborrowed' ], [ 'datelastseen' ],
         [ 'stack' ], [ 'notforloan' ], [ 'damaged' ],
-        [ 'itemlost' ], [ 'wthdrawn' ], [ 'itemcallnumber' ],
+        [ 'itemlost' ], [ 'withdrawn' ], [ 'itemcallnumber' ],
         [ 'issues' ], [ 'renewals' ], [ 'reserves' ],
         [ 'restricted' ], [ 'itemnotes' ], [ 'nonpublicnote' ],
         [ 'holdingbranch' ], [ 'paidfor' ], [ 'timestamp' ],
@@ -506,6 +515,25 @@ warning_like {( undef, $results_hashref, $facets_loop ) =
     $results_hashref->{'biblioserver'}->{"RECORDS"});
 is($newresults[0]->{'alternateholdings_count'}, 1, 'Alternate holdings filled in correctly');
 
+
+## Regression test for Bug 10741
+
+# make one of the test items appear to be in transit
+my $circ_module = new Test::MockModule('C4::Circulation');
+$circ_module->mock('GetTransfers', sub {
+    my $itemnumber = shift;
+    if ($itemnumber == 11) {
+        return ('2013-07-19', 'MPL', 'CPL');
+    } else {
+        return;
+    }
+});
+
+($error, $results_hashref, $facets_loop) = getRecords("TEST12121212","TEST12121212",[ ], [ 'biblioserver' ],20,0,undef,\%branches,\%itemtypes,$query_type,0);
+@newresults = searchResults('intranet', $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 17, 0, 0,
+    $results_hashref->{'biblioserver'}->{"RECORDS"});
+ok(!exists($newresults[0]->{norequests}), 'presence of a transit does not block hold request action (bug 10741)');
+
 END {
     if ($child) {
         kill 9, $child;
@@ -515,4 +543,83 @@ END {
     }
 }
 
+# Testing exploding indexes
+my $term;
+my $searchmodule = new Test::MockModule('C4::Search');
+$searchmodule->mock('SimpleSearch', sub {
+    my $query = shift;
+
+    is($query, "he:$term", "Searching for expected term '$term' for exploding") or return '', [], 0;
+
+    my $record = MARC::Record->new;
+    if ($query =~ m/Arizona/) {
+        $record->add_fields(
+            [ '001', '1234' ],
+            [ '151', ' ', ' ', a => 'Arizona' ],
+            [ '551', ' ', ' ', a => 'United States', w => 'g' ],
+            [ '551', ' ', ' ', a => 'Maricopa County', w => 'h' ],
+            [ '551', ' ', ' ', a => 'Navajo County', w => 'h' ],
+            [ '551', ' ', ' ', a => 'Pima County', w => 'h' ],
+            [ '551', ' ', ' ', a => 'New Mexico' ],
+            );
+    }
+    return '', [ $record->as_usmarc() ], 1;
+});
+
+$UseQueryParser = 1;
+$term = 'Arizona';
+( $error, $query, $simple_query, $query_cgi,
+$query_desc, $limit, $limit_cgi, $limit_desc,
+$stopwords_removed, $query_type ) = buildQuery([], [ $term ], [ 'su-br' ], [  ], [], 0, 'en');
+matchesExplodedTerms("Advanced search for broader subjects", $query, 'Arizona', 'United States');
+
+( $error, $query, $simple_query, $query_cgi,
+$query_desc, $limit, $limit_cgi, $limit_desc,
+$stopwords_removed, $query_type ) = buildQuery([], [ $term ], [ 'su-na' ], [  ], [], 0, 'en');
+matchesExplodedTerms("Advanced search for narrower subjects", $query, 'Arizona', 'Maricopa County', 'Navajo County', 'Pima County');
+
+( $error, $query, $simple_query, $query_cgi,
+$query_desc, $limit, $limit_cgi, $limit_desc,
+$stopwords_removed, $query_type ) = buildQuery([], [ $term ], [ 'su-rl' ], [  ], [], 0, 'en');
+matchesExplodedTerms("Advanced search for related subjects", $query, 'Arizona', 'United States', 'Maricopa County', 'Navajo County', 'Pima County');
+
+( $error, $query, $simple_query, $query_cgi,
+$query_desc, $limit, $limit_cgi, $limit_desc,
+$stopwords_removed, $query_type ) = buildQuery([], [ "$term", 'history' ], [ 'su-rl', 'kw' ], [  ], [], 0, 'en');
+matchesExplodedTerms("Advanced search for related subjects and keyword 'history' searches related subjects", $query, 'Arizona', 'United States', 'Maricopa County', 'Navajo County', 'Pima County');
+like($query, qr/history/, "Advanced search for related subjects and keyword 'history' searches for 'history'");
+
+( $error, $query, $simple_query, $query_cgi,
+$query_desc, $limit, $limit_cgi, $limit_desc,
+$stopwords_removed, $query_type ) = buildQuery([], [ 'history', "$term" ], [ 'kw', 'su-rl' ], [  ], [], 0, 'en');
+matchesExplodedTerms("Order of terms doesn't matter for advanced search", $query, 'Arizona', 'United States', 'Maricopa County', 'Navajo County', 'Pima County');
+like($query, qr/history/, "Order of terms doesn't matter for advanced search");
+
+( $error, $query, $simple_query, $query_cgi,
+$query_desc, $limit, $limit_cgi, $limit_desc,
+$stopwords_removed, $query_type ) = buildQuery([], [ "su-br($term)" ], [  ], [  ], [], 0, 'en');
+matchesExplodedTerms("Simple search for broader subjects", $query, 'Arizona', 'United States');
+
+( $error, $query, $simple_query, $query_cgi,
+$query_desc, $limit, $limit_cgi, $limit_desc,
+$stopwords_removed, $query_type ) = buildQuery([], [ "su-na($term)" ], [  ], [  ], [], 0, 'en');
+matchesExplodedTerms("Simple search for narrower subjects", $query, 'Arizona', 'Maricopa County', 'Navajo County', 'Pima County');
+
+( $error, $query, $simple_query, $query_cgi,
+$query_desc, $limit, $limit_cgi, $limit_desc,
+$stopwords_removed, $query_type ) = buildQuery([], [ "su-rl($term)" ], [  ], [  ], [], 0, 'en');
+matchesExplodedTerms("Simple search for related subjects", $query, 'Arizona', 'United States', 'Maricopa County', 'Navajo County', 'Pima County');
+
+( $error, $query, $simple_query, $query_cgi,
+$query_desc, $limit, $limit_cgi, $limit_desc,
+$stopwords_removed, $query_type ) = buildQuery([], [ "history && su-rl($term)" ], [  ], [  ], [], 0, 'en');
+matchesExplodedTerms("Simple search for related subjects and keyword 'history' searches related subjects", $query, 'Arizona', 'United States', 'Maricopa County', 'Navajo County', 'Pima County');
+like($query, qr/history/, "Simple search for related subjects and keyword 'history' searches for 'history'");
+
+sub matchesExplodedTerms {
+    my ($message, $query, @terms) = @_;
+    my $match = '(' . join ('|', map { " \@attr 1=Subject \@attr 4=1 \"$_\"" } @terms) . "){" . scalar(@terms) . "}";
+    like($query, qr/$match/, $message);
+}
+
 1;