Bug 17600: Standardize our EXPORT_OK
[srvgit] / t / db_dependent / Search.t
old mode 100644 (file)
new mode 100755 (executable)
index 53f98c1..8efe5df
@@ -19,9 +19,6 @@ use Modern::Perl;
 
 use utf8;
 
-use YAML;
-
-use C4::Debug;
 use C4::AuthoritiesMarc qw( SearchAuthorities );
 use C4::XSLT;
 require C4::Context;
@@ -29,9 +26,10 @@ require C4::Context;
 # work around spurious wide character warnings
 use open ':std', ':encoding(utf8)';
 
-use Test::More tests => 2;
+use Test::More tests => 3;
 use Test::MockModule;
 use Test::Warn;
+use t::lib::Mocks;
 
 use Koha::Caches;
 
@@ -94,6 +92,12 @@ END {
     cleanup();
 }
 
+sub matchesExplodedTerms {
+    my ($message, $query, @terms) = @_;
+    my $match = '(' . join ('|', map { " \@attr 1=Subject \@attr 4=1 \"$_\"" } @terms) . "){" . scalar(@terms) . "}";
+    like($query, qr/$match/, $message);
+}
+
 our $QueryStemming = 0;
 our $QueryAutoTruncate = 0;
 our $QueryWeightFields = 0;
@@ -105,7 +109,7 @@ my @htdocs = split /\//, $htdocs;
 $htdocs[-2] = 'koha-tmpl';
 $htdocs[-1] = 'opac-tmpl';
 $htdocs = join '/', @htdocs;
-our $contextmodule = new Test::MockModule('C4::Context');
+our $contextmodule = Test::MockModule->new('C4::Context');
 $contextmodule->mock('preference', sub {
     my ($self, $pref) = @_;
     if ($pref eq 'marcflavour') {
@@ -130,7 +134,7 @@ $contextmodule->mock('preference', sub {
         return '';
     } elsif ($pref eq 'opacthemes') {
         return 'bootstrap';
-    } elsif ($pref eq 'opaclanguages') {
+    } elsif ($pref eq 'OPACLanguages') {
         return 'en';
     } elsif ($pref eq 'AlternateHoldingsField') {
         return '490av';
@@ -172,7 +176,7 @@ $contextmodule->mock('preference', sub {
         return '1';
     } elsif ($pref eq 'UseControlNumber') {
         return '0';
-    } elsif ($pref eq 'UseICU') {
+    } elsif ($pref eq 'UseICUStyleQuotes') {
         return '0';
     } elsif ($pref eq 'viewISBD') {
         return '1';
@@ -188,6 +192,8 @@ $contextmodule->mock('preference', sub {
         return '0';
     } elsif ($pref eq 'OPACPlayMusicalInscripts') {
         return '0';
+    } elsif ($pref eq 'Reference_NFL_Statuses') {
+        return '0';
     } 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)/i;
@@ -195,7 +201,7 @@ $contextmodule->mock('preference', sub {
     }
 });
 
-our $bibliomodule = new Test::MockModule('C4::Biblio');
+our $bibliomodule = Test::MockModule->new('C4::Biblio');
 
 sub mock_GetMarcSubfieldStructure {
     my $marc_type = shift;
@@ -205,6 +211,7 @@ sub mock_GetMarcSubfieldStructure {
                     'biblio.biblionumber' => [{ tagfield =>  '999', tagsubfield => 'c' }],
                     'biblio.isbn' => [{ tagfield => '020', tagsubfield => 'a' }],
                     'biblio.title' => [{ tagfield => '245', tagsubfield => 'a' }],
+                    'biblio.author' => [{ tagfield => '100', tagsubfield => 'a' }],
                     'biblio.notes' => [{ tagfield => '500', tagsubfield => 'a' }],
                     'items.barcode' => [{ tagfield => '952', tagsubfield => 'p' }],
                     'items.booksellerid' => [{ tagfield => '952', tagsubfield => 'e' }],
@@ -252,10 +259,10 @@ sub run_marc21_search_tests {
     Koha::Caches->get_instance('config')->flush_all;
 
     mock_GetMarcSubfieldStructure('marc21');
-    my $context = new C4::Context("$datadir/etc/koha-conf.xml");
+    my $context = C4::Context->new("$datadir/etc/koha-conf.xml");
     $context->set_context();
 
-    use_ok('C4::Search');
+    use_ok('C4::Search', qw( getIndexes FindDuplicate SimpleSearch getRecords buildQuery searchResults ));
 
     # set search syspreferences to a known starting point
     $QueryStemming = 0;
@@ -269,7 +276,7 @@ sub run_marc21_search_tests {
     is(scalar(grep(/^arl$/, @$indexes)), 1, "Accelerated reading level index supported");
     is(scalar(grep(/^arp$/, @$indexes)), 1, "Accelerated reading point index supported");
 
-    my $bibliomodule = new Test::MockModule('C4::Biblio');
+    my $bibliomodule = Test::MockModule->new('C4::Biblio');
 
     my %branches = (
         'CPL' => { 'branchaddress1' => 'Jefferson Summit', 'branchcode' => 'CPL', 'branchname' => 'Centerville', },
@@ -287,14 +294,14 @@ sub run_marc21_search_tests {
         'UPL' => { 'branchaddress1' => 'Chestnut Hollow', 'branchcode' => 'UPL', 'branchname' => 'Union', },
     );
     my %itemtypes = (
-        'BK' => { 'imageurl' => 'bridge/book.gif', 'summary' => '', 'itemtype' => 'BK', 'description' => 'Books' },
-        'CF' => { 'imageurl' => 'bridge/computer_file.gif', 'summary' => '', 'itemtype' => 'CF', 'description' => 'Computer Files' },
-        'CR' => { 'imageurl' => 'bridge/periodical.gif', 'summary' => '', 'itemtype' => 'CR', 'description' => 'Continuing Resources' },
-        'MP' => { 'imageurl' => 'bridge/map.gif', 'summary' => '', 'itemtype' => 'MP', 'description' => 'Maps' },
-        'MU' => { 'imageurl' => 'bridge/sound.gif', 'summary' => '', 'itemtype' => 'MU', 'description' => 'Music' },
-        'MX' => { 'imageurl' => 'bridge/kit.gif', 'summary' => '', 'itemtype' => 'MX', 'description' => 'Mixed Materials' },
+        'BK' => { 'imageurl' => 'bridge/book.png', 'summary' => '', 'itemtype' => 'BK', 'description' => 'Books' },
+        'CF' => { 'imageurl' => 'bridge/computer_file.png', 'summary' => '', 'itemtype' => 'CF', 'description' => 'Computer Files' },
+        'CR' => { 'imageurl' => 'bridge/periodical.png', 'summary' => '', 'itemtype' => 'CR', 'description' => 'Continuing Resources' },
+        'MP' => { 'imageurl' => 'bridge/map.png', 'summary' => '', 'itemtype' => 'MP', 'description' => 'Maps' },
+        'MU' => { 'imageurl' => 'bridge/sound.png', 'summary' => '', 'itemtype' => 'MU', 'description' => 'Music' },
+        'MX' => { 'imageurl' => 'bridge/kit.png', 'summary' => '', 'itemtype' => 'MX', 'description' => 'Mixed Materials' },
         'REF' => { 'imageurl' => '', 'summary' => '', 'itemtype' => 'REF', 'description' => 'Reference' },
-        'VM' => { 'imageurl' => 'bridge/dvd.gif', 'summary' => '', 'itemtype' => 'VM', 'description' => 'Visual Materials' },
+        'VM' => { 'imageurl' => 'bridge/dvd.png', 'summary' => '', 'itemtype' => 'VM', 'description' => 'Visual Materials' },
     );
 
     index_sample_records_and_launch_zebra($datadir, 'marc21');
@@ -492,6 +499,64 @@ ok(MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[0],'
     }
     is ($allavailable, 'true', 'All records have at least one item available');
 
+    my $mocked_xslt = Test::MockModule->new('Koha::XSLT::Base');
+    $mocked_xslt->mock( 'transform', sub {
+        my ($self, $xml) = @_;
+        return $xml;
+    });
+
+    @newresults = searchResults({'interface'=>'opac'}, $query_desc, $results_hashref->{'biblioserver'}->{'hits'}, 17, 0, 0,
+        $results_hashref->{'biblioserver'}->{"RECORDS"}, { anonymous_session => 1 });
+
+    like( $newresults[0]->{XSLTResultsRecord}, qr/<variable name="anonymous_session">1<\/variable>/, "Variable injected correctly" );
+
+    my $biblio_id = $newresults[0]->{biblionumber};
+    my $fw = C4::Biblio::GetFrameworkCode($biblio_id);
+
+    my $dbh = C4::Context->dbh;
+    # Hide subfield 'p' in OPAC
+    $dbh->do(qq{
+        UPDATE marc_subfield_structure
+        SET hidden=4
+        WHERE frameworkcode='$fw' AND
+              tagfield=952 AND
+              tagsubfield='p';
+    });
+
+    # Hide subfield 'y' in Staff
+    $dbh->do(qq{
+        UPDATE marc_subfield_structure
+        SET hidden=-7
+        WHERE frameworkcode='$fw' AND
+              tagfield=952 AND
+              tagsubfield='y';
+    });
+
+    Koha::Caches->get_instance->flush_all;
+
+    @newresults = searchResults(
+        { 'interface' => 'opac' },
+        $query_desc,
+        $results_hashref->{'biblioserver'}->{'hits'},
+        17,
+        0,
+        0,
+        $results_hashref->{'biblioserver'}->{"RECORDS"}
+    );
+
+    unlike( $newresults[0]->{XSLTResultsRecord}, qr/<subfield code="p">TEST11111<\/subfield>/, '952\$p hidden in OPAC' );
+
+    @newresults = searchResults(
+        { 'interface' => 'intranet' },
+        $query_desc,
+        $results_hashref->{'biblioserver'}->{'hits'},
+        17,
+        0,
+        0,
+        $results_hashref->{'biblioserver'}->{"RECORDS"}
+    );
+
+    unlike( $newresults[0]->{XSLTResultsRecord}, qr/<subfield code="y">Books<\/subfield>/, '952\$y hidden on staff interface' );
 
     ( $error, $query, $simple_query, $query_cgi,
     $query_desc, $limit, $limit_cgi, $limit_desc,
@@ -602,7 +667,13 @@ ok(MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[0],'
     ( $error, $query, $simple_query, $query_cgi,
     $query_desc, $limit, $limit_cgi, $limit_desc,
     $query_type ) = buildQuery([], [ 0 ], [ 'su,phr' ], [], [], 0, 'en');
-    is($query, 'su,phr=0 ', 'buildQuery should keep 0 value');
+    is($query, 'su,phr=(rk=(0)) ', 'buildQuery should keep 0 value');
+
+    # Bug 23086
+    ( $error, $query, $simple_query, $query_cgi,
+    $query_desc, $limit, $limit_cgi, $limit_desc,
+    $query_type ) = buildQuery([], [], [], [ 'mc-ccode:NF(IC'], [], 0, 'en');
+    like($query, qr/ccode="NF\(IC"/, "Limit quoted correctly");
 
     # Let's see what happens when we pass bad data into these routines.
     # We have to catch warnings since we're not very good about returning errors.
@@ -630,7 +701,7 @@ ok(MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[0],'
     ## Regression test for Bug 10741
 
     # make one of the test items appear to be in transit
-    my $circ_module = new Test::MockModule('C4::Circulation');
+    my $circ_module = Test::MockModule->new('C4::Circulation');
     $circ_module->mock('GetTransfers', sub {
         my $itemnumber = shift // -1;
         if ($itemnumber == 11) {
@@ -656,29 +727,6 @@ ok(MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[0],'
                 "Warning is raised correctly for invalid tags in MARC::Record";
     is(scalar(@newresults), 0, 'a record that cannot be parsed by MARC::Record is simply skipped (bug 10684)');
 
-    # 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;
-    });
-
     my ($auths, $count) = SearchAuthorities(
         ['mainentry'], ['and'], [''], ['starts'],
         ['shakespeare'], 0, 10, '', '', 1
@@ -785,10 +833,10 @@ sub run_unimarc_search_tests {
     Koha::Caches->get_instance('config')->flush_all;
 
     mock_GetMarcSubfieldStructure('unimarc');
-    my $context = new C4::Context("$datadir/etc/koha-conf.xml");
+    my $context = C4::Context->new("$datadir/etc/koha-conf.xml");
     $context->set_context();
 
-    use_ok('C4::Search');
+    use_ok('C4::Search', qw( getIndexes FindDuplicate SimpleSearch getRecords buildQuery searchResults ));
 
     # set search syspreferences to a known starting point
     $QueryStemming = 0;
@@ -811,7 +859,7 @@ sub run_unimarc_search_tests {
     is($total_hits, 1, 'UNIMARC generic item index (bug 10037)');
 
     # authority records
-    use_ok('C4::AuthoritiesMarc');
+    use_ok('C4::AuthoritiesMarc', qw( SearchAuthorities ));
 
     my ($auths, $count) = SearchAuthorities(
         ['mainentry'], ['and'], [''], ['contains'],
@@ -865,7 +913,7 @@ sub run_unimarc_search_tests {
 }
 
 subtest 'MARC21 + DOM' => sub {
-    plan tests => 84;
+    plan tests => 88;
     run_marc21_search_tests();
 };
 
@@ -874,6 +922,53 @@ subtest 'UNIMARC + DOM' => sub {
     run_unimarc_search_tests();
 };
 
+
+subtest 'FindDuplicate' => sub {
+    plan tests => 6;
+    Koha::Caches->get_instance('config')->flush_all;
+    t::lib::Mocks::mock_preference('marcflavour', 'marc21' );
+    mock_GetMarcSubfieldStructure('marc21');
+    my $z_searcher = Test::MockModule->new('C4::Search');
+    $z_searcher->mock('SimpleSearch', sub {
+        warn shift @_;
+        return 1;
+    });
+    my $e_searcher = Test::MockModule->new('Koha::SearchEngine::Elasticsearch::Search');
+    $e_searcher->mock('simple_search_compat', sub {
+        shift @_;
+        warn shift @_;
+        return 1;
+    });
+
+    my $record_1 = MARC::Record->new;
+    $record_1 ->add_fields(
+            [ '100', '0', '0', a => 'Morgenstern, Erin' ],
+            [ '245', '0', '0', a => 'The night circus /' ]
+    );
+    my $record_2 = MARC::Record->new;
+    $record_2 ->add_fields(
+            [ '245', '0', '0', a => 'The book of nothing /' ]
+    );
+    my $record_3 = MARC::Record->new;
+    $record_3->add_fields(
+            [ '245', '0', '0', a => 'Frog and toad all year /' ]
+    );
+
+    foreach my $engine ('Zebra','Elasticsearch'){
+        t::lib::Mocks::mock_preference('searchEngine', $engine );
+
+        warning_is { C4::Search::FindDuplicate($record_1);}
+            q/ti,ext:"The night circus \/" and au,ext:"Morgenstern, Erin"/,"Term correctly formed and passed to $engine";
+
+        warning_is { C4::Search::FindDuplicate($record_2);}
+            q/ti,ext:"The book of nothing \/"/,"Term correctly formed and passed to $engine";
+
+        warning_is { C4::Search::FindDuplicate($record_3);}
+            q/ti,ext:"Frog and toad all year \/"/,"Term correctly formed and passed to $engine";
+    }
+
+};
+
 # Make sure that following tests are not using our config settings
 Koha::Caches->get_instance('config')->flush_all;