Bug 18989: (QA follow-up) Make Koha::Biblio->hidden_in_opac aware of OpacHiddenItemsH...
authorTomas Cohen Arazi <tomascohen@theke.io>
Fri, 23 Apr 2021 18:30:03 +0000 (15:30 -0300)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Wed, 12 May 2021 12:12:07 +0000 (14:12 +0200)
This patch makes the method aware of the new syspref. It will only eval
the rules against items if the OpacHiddenItemsHidesRecord syspref is
set.

Tests are added to reflect this.

To test:
1. Apply this patch
2. Run:
   $ kshell
  k$ prove t/db_dependent/Koha/Biblio.t
=> SUCCESS: Tests pass!
3. Sign off :-D

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Koha/Biblio.pm
t/db_dependent/Koha/Biblio.t

index 1d84f01..02d2aba 100644 (file)
@@ -234,7 +234,8 @@ sub pickup_locations {
     my $bool = $biblio->hidden_in_opac({ [ rules => $rules ] })
 
 Returns true if the biblio matches the hidding criteria defined in $rules.
-Returns false otherwise.
+Returns false otherwise. It involves the I<OpacHiddenItems> and
+I<OpacHiddenItemsHidesRecord> system preferences.
 
 Takes HASHref that can have the following parameters:
     OPTIONAL PARAMETERS:
@@ -254,6 +255,9 @@ sub hidden_in_opac {
 
     return 0 unless @items; # Do not hide if there is no item
 
+    # Ok, there are items, don't even try the rules unless OpacHiddenItemsHidesRecord
+    return 0 unless C4::Context->preference('OpacHiddenItemsHidesRecord');
+
     return !(any { !$_->hidden_in_opac({ rules => $rules }) } @items);
 }
 
index ea5e3f2..e7ecb4f 100755 (executable)
@@ -63,13 +63,26 @@ subtest 'metadata() tests' => sub {
 
 subtest 'hidden_in_opac() tests' => sub {
 
-    plan tests => 4;
+    plan tests => 6;
 
     $schema->storage->txn_begin;
 
     my $biblio = $builder->build_sample_biblio();
+    my $rules  = { withdrawn => [ 2 ] };
+
+    t::lib::Mocks::mock_preference( 'OpacHiddenItemsHidesRecord', 0 );
+
+    ok(
+        !$biblio->hidden_in_opac({ rules => $rules }),
+        'Biblio not hidden if there is no item attached (!OpacHiddenItemsHidesRecord)'
+    );
+
+    t::lib::Mocks::mock_preference( 'OpacHiddenItemsHidesRecord', 1 );
 
-    ok( !$biblio->hidden_in_opac({ rules => { withdrawn => [ 2 ] } }), 'Biblio not hidden if there is no item attached' );
+    ok(
+        !$biblio->hidden_in_opac({ rules => $rules }),
+        'Biblio not hidden if there is no item attached (OpacHiddenItemsHidesRecord)'
+    );
 
     my $item_1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
     my $item_2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
@@ -77,17 +90,24 @@ subtest 'hidden_in_opac() tests' => sub {
     $item_1->withdrawn( 1 )->store->discard_changes;
     $item_2->withdrawn( 1 )->store->discard_changes;
 
-    ok( !$biblio->hidden_in_opac({ rules => { withdrawn => [ 2 ] } }), 'Biblio not hidden' );
+    ok( !$biblio->hidden_in_opac({ rules => $rules }), 'Biblio not hidden' );
 
     $item_2->withdrawn( 2 )->store->discard_changes;
     $biblio->discard_changes; # refresh
 
-    ok( !$biblio->hidden_in_opac({ rules => { withdrawn => [ 2 ] } }), 'Biblio not hidden' );
+    ok( !$biblio->hidden_in_opac({ rules => $rules }), 'Biblio not hidden' );
 
     $item_1->withdrawn( 2 )->store->discard_changes;
     $biblio->discard_changes; # refresh
 
-    ok( $biblio->hidden_in_opac({ rules => { withdrawn => [ 2 ] } }), 'Biblio hidden' );
+    ok( $biblio->hidden_in_opac({ rules => $rules }), 'Biblio hidden' );
+
+    t::lib::Mocks::mock_preference( 'OpacHiddenItemsHidesRecord', 0 );
+    ok(
+        !$biblio->hidden_in_opac( { rules => $rules } ),
+        'Biblio hidden (!OpacHiddenItemsHidesRecord)'
+    );
+
 
     $schema->storage->txn_rollback;
 };