Bug 27673: Replace YAML with YAML::XS
[koha-ffzg.git] / t / db_dependent / Search.t
old mode 100644 (file)
new mode 100755 (executable)
index 771a7f4..3b4e6d3
@@ -19,8 +19,6 @@ use Modern::Perl;
 
 use utf8;
 
-use YAML;
-
 use C4::Debug;
 use C4::AuthoritiesMarc qw( SearchAuthorities );
 use C4::XSLT;
@@ -29,9 +27,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 +93,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 +110,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 +135,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 +177,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';
@@ -195,7 +200,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 +210,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,7 +258,7 @@ 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');
@@ -269,7 +275,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 +293,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,7 +498,6 @@ ok(MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[0],'
     }
     is ($allavailable, 'true', 'All records have at least one item available');
 
-    use Test::MockModule;
     my $mocked_xslt = Test::MockModule->new('Koha::XSLT::Base');
     $mocked_xslt->mock( 'transform', sub {
         my ($self, $xml) = @_;
@@ -504,6 +509,54 @@ ok(MARC::Record::new_from_xml($results_hashref->{biblioserver}->{RECORDS}->[0],'
 
     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,
     $query_type ) = buildQuery([], [ 'pqf=@attr 1=_ALLRECORDS @attr 2=103 ""' ], [], [], [], 0, 'en');
@@ -613,7 +666,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.
@@ -641,7 +700,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) {
@@ -773,7 +832,7 @@ 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');
@@ -853,7 +912,7 @@ sub run_unimarc_search_tests {
 }
 
 subtest 'MARC21 + DOM' => sub {
-    plan tests => 85;
+    plan tests => 88;
     run_marc21_search_tests();
 };
 
@@ -862,6 +921,42 @@ subtest 'UNIMARC + DOM' => sub {
     run_unimarc_search_tests();
 };
 
+
+subtest 'FindDuplicate' => sub {
+    plan tests => 3;
+    Koha::Caches->get_instance('config')->flush_all;
+    t::lib::Mocks::mock_preference('marcflavour', 'marc21' );
+    mock_GetMarcSubfieldStructure('marc21');
+    my $searcher = Test::MockModule->new('C4::Search');
+    $searcher->mock('SimpleSearch', sub {
+        warn shift @_;
+        return 1;
+    });
+
+    my $record = MARC::Record->new;
+    $record->add_fields(
+            [ '100', '0', '0', a => 'Morgenstern, Erin' ],
+            [ '245', '0', '0', a => 'The night circus /' ]
+    );
+    warning_is { C4::Search::FindDuplicate($record);}
+        q/ti,ext:"The night circus \/" and au,ext:"Morgenstern, Erin"/,"Term correctly formed";
+
+    $record = MARC::Record->new;
+    $record->add_fields(
+            [ '245', '0', '0', a => 'The book of nothing /' ]
+    );
+    warning_is { C4::Search::FindDuplicate($record);}
+        q/ti,ext:"The book of nothing \/"/,"Term correctly formed";
+
+    $record = MARC::Record->new;
+    $record->add_fields(
+            [ '245', '0', '0', a => 'Frog and toad all year /' ]
+    );
+    warning_is { C4::Search::FindDuplicate($record);}
+        q/ti,ext:"Frog and toad all year \/"/,"Term correctly formed";
+
+};
+
 # Make sure that following tests are not using our config settings
 Koha::Caches->get_instance('config')->flush_all;