Bug 17664: Silence non-zebra warnings in t/db_dependent/Search.t
[koha.git] / t / db_dependent / Filter_MARC_ViewPolicy.t
index 09e567b..64759fa 100644 (file)
@@ -29,16 +29,20 @@ use List::MoreUtils qw/any/;
 use MARC::Record;
 use MARC::Field;
 use C4::Context;
-use Koha::Cache qw/flush_all/;
+use C4::Biblio;
+use Koha::Caches;
 use Koha::Database;
-use English qw/-no_match_vars/;
-
-$OUTPUT_AUTOFLUSH = 1;
 
 BEGIN {
     use_ok('Koha::RecordProcessor');
 }
 
+my $dbh = C4::Context->dbh;
+
+my $database = Koha::Database->new();
+my $schema   = $database->schema();
+$dbh->{RaiseError} = 1;
+
 sub run_hiding_tests {
 
     my $interface = shift;
@@ -54,26 +58,15 @@ sub run_hiding_tests {
         'intranet' => [ -8, -7, -4, -3, -2, 2, 3, 5, 8 ]
     };
 
-    my $dbh = C4::Context->dbh;
-
-    my $database = Koha::Database->new();
-    my $schema   = $database->schema();
-    $dbh->{RaiseError} = 1;
-
-    #$ENV{'DEBUG'} = '1'; # Turn on debugging.
-
+    my ( $isbn_field, $isbn_subfield ) =
+      GetMarcFromKohaField( 'biblioitems.isbn', q{} );
+    my $update_sql = q{UPDATE marc_subfield_structure SET hidden=? };
+    my $sth        = $dbh->prepare($update_sql);
     foreach my $hidden_value (@valid_hidden_values) {
 
-        $schema->storage->txn_begin();
-
-        my $update_sql =
-            q{UPDATE marc_subfield_structure SET hidden=? }
-          . q{WHERE tagfield='020' OR }
-          . q{      tagfield='008';};
-        my $sth = $dbh->prepare($update_sql);
         $sth->execute($hidden_value);
 
-        my $cache = Koha::Cache->get_instance();
+        my $cache = Koha::Caches->get_instance();
         $cache->flush_all();    # easy way to ensure DB is queried again.
 
         my $processor = Koha::RecordProcessor->new(
@@ -101,10 +94,10 @@ sub run_hiding_tests {
         if ( any { $_ == $hidden_value } @{ $hidden->{$interface} } ) {
 
             # Subfield and controlfield are set to be hidden
-            is( $filtered_record->field('020'),
+            is( $filtered_record->field($isbn_field),
                 undef,
                 "Data field has been deleted because of hidden=$hidden_value" );
-            isnt( $unfiltered_record->field('020'), undef,
+            isnt( $unfiltered_record->field($isbn_field), undef,
 "Data field has been deleted in the original record because of hidden=$hidden_value"
             );
 
@@ -120,10 +113,10 @@ sub run_hiding_tests {
 
         }
         else {
-            isnt( $filtered_record->field('020'), undef,
+            isnt( $filtered_record->field($isbn_field), undef,
                 "Data field hasn't been deleted because of hidden=$hidden_value"
             );
-            isnt( $unfiltered_record->field('020'), undef,
+            isnt( $unfiltered_record->field($isbn_field), undef,
 "Data field hasn't been deleted in the original record because of hidden=$hidden_value"
             );
 
@@ -135,25 +128,59 @@ sub run_hiding_tests {
 "Control field hasn't been deleted in the original record because of hidden=$hidden_value"
             );
 
+            # force all the hidden values the same, so filtered and unfiltered
+            # records should be identical.
             is_deeply( $filtered_record, $unfiltered_record,
                 'Records are the same' );
         }
 
-        $schema->storage->txn_rollback();
     }
+
+    $sth->execute(-1); # -1 is visible in opac and intranet.
+
+    my $cache = Koha::Caches->get_instance();
+    $cache->flush_all();    # easy way to ensure DB is queried again.
+
+    my $shouldhidemarc = Koha::Filter::MARC::ViewPolicy->should_hide_marc(
+        {
+            frameworkcode => q{},
+            interface     => $interface
+        }
+    );
+    my @hiddenfields = grep { $shouldhidemarc->{$_}==1 } keys %{$shouldhidemarc};
+
+    $sth->execute(8); # 8 is invisible in opac and intranet.
+
+    $cache->flush_all();    # easy way to ensure DB is queried again.
+
+    $shouldhidemarc = Koha::Filter::MARC::ViewPolicy->should_hide_marc(
+        {
+            frameworkcode => q{},
+            interface     => $interface
+        }
+    );
+    my @keyvalues = keys %{$shouldhidemarc};
+    my @visiblefields = grep { $shouldhidemarc->{$_}==1 } @keyvalues;
+
+    is(scalar @hiddenfields,0,'Should Hide MARC - Full Visibility');
+    is_deeply(\@visiblefields,\@keyvalues,'Should Hide MARC - No Visibility');
     return;
 }
 
 sub create_marc_record {
 
+    my ( $title_field, $title_subfield ) =
+      GetMarcFromKohaField( 'biblio.title', q{} );
+    my ( $isbn_field, $isbn_subfield ) =
+      GetMarcFromKohaField( 'biblioitems.isbn', q{} );
     my $isbn        = '0590353403';
     my $title       = 'Foundation';
     my $marc_record = MARC::Record->new;
     my @fields      = (
         MARC::Field->new( '003', 'AR-CdUBM' ),
         MARC::Field->new( '008', '######suuuu####ag_||||__||||_0||_|_uuu|d' ),
-        MARC::Field->new( '020', q{}, q{}, 'a' => $isbn ),
-        MARC::Field->new( '245', q{}, q{}, 'a' => $title ),
+        MARC::Field->new( $isbn_field,  q{}, q{}, $isbn_subfield  => $isbn ),
+        MARC::Field->new( $title_field, q{}, q{}, $title_subfield => $title ),
     );
 
     $marc_record->insert_fields_ordered(@fields);
@@ -163,16 +190,19 @@ sub create_marc_record {
 
 subtest 'Koha::Filter::MARC::ViewPolicy opac tests' => sub {
 
-    plan tests => 102;
+    plan tests => 104;
 
+    $schema->storage->txn_begin();
     run_hiding_tests('opac');
+    $schema->storage->txn_rollback();
 };
 
 subtest 'Koha::Filter::MARC::ViewPolicy intranet tests' => sub {
 
-    plan tests => 102;
+    plan tests => 104;
 
+    $schema->storage->txn_begin();
     run_hiding_tests('intranet');
+    $schema->storage->txn_rollback();
 };
 
-1;