From 56a213acc541698c8d2b07d8e612348dd42b4645 Mon Sep 17 00:00:00 2001
From: Nick Clemens <nick@bywatersolutions.com>
Date: Thu, 6 Jan 2022 16:03:56 +0000
Subject: [PATCH] Bug 29802: Unit tests

prove -v t/db_dependent/Koha/Items.t
prove -v t/db_dependent/XSLT.t

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
---
 t/db_dependent/Koha/Items.t | 14 +++++++++++++-
 t/db_dependent/XSLT.t       | 11 ++++++++++-
 2 files changed, 23 insertions(+), 2 deletions(-)

diff --git a/t/db_dependent/Koha/Items.t b/t/db_dependent/Koha/Items.t
index 410dada550c..a9f07164337 100755
--- a/t/db_dependent/Koha/Items.t
+++ b/t/db_dependent/Koha/Items.t
@@ -1587,7 +1587,7 @@ $schema->storage->txn_rollback;
 
 subtest 'filter_by_visible_in_opac() tests' => sub {
 
-    plan tests => 12;
+    plan tests => 14;
 
     $schema->storage->txn_begin;
 
@@ -1688,6 +1688,18 @@ subtest 'filter_by_visible_in_opac() tests' => sub {
         'No rules passed, hidelostitems set, patron exception changes nothing'
     );
 
+    $rules = { biblionumber => [ $biblio->biblionumber ] };
+    is(
+        $biblio->items->filter_by_visible_in_opac->count,
+        0,
+        'Biblionumber rule successfully hides all items'
+    );
+
+    my $biblio2 = $builder->build_sample_biblio;
+    $rules = { biblionumber => [ $biblio2->biblionumber ] };
+    my $prefetched = $biblio->items->search({},{ prefetch => ['branchtransfers','reserves'] })->filter_by_visible_in_opac;
+    ok( $prefetched->next, "Can retrieve object when prefetching and hiding on a duplicated column");
+
     $rules = { withdrawn => [ 1, 2 ], copynumber => [ 2 ] };
     is(
         $biblio->items->filter_by_visible_in_opac->count,
diff --git a/t/db_dependent/XSLT.t b/t/db_dependent/XSLT.t
index 4c9be6bb4f5..23a7340f723 100755
--- a/t/db_dependent/XSLT.t
+++ b/t/db_dependent/XSLT.t
@@ -160,11 +160,12 @@ $schema->storage->txn_rollback;
 
 subtest 'buildKohaItemsNamespace() including/omitting items tests' => sub {
 
-    plan tests => 20;
+    plan tests => 23;
 
     $schema->storage->txn_begin;
 
     my $biblio = $builder->build_sample_biblio;
+    my $biblio2 = $builder->build_sample_biblio;
 
     # Have two known libraries for testing purposes
     my $library_1 = $builder->build_object({ class => 'Koha::Libraries' });
@@ -187,6 +188,14 @@ subtest 'buildKohaItemsNamespace() including/omitting items tests' => sub {
     like(   $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 present in the XML' );
     like(   $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 present in the XML' );
     unlike( $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 not present in the XML' );
+
+    t::lib::Mocks::mock_preference('OpacHiddenItems', 'biblionumber: ['.$biblio2->biblionumber.']');
+    my $hid_rs = $biblio->items->search({ "me.itemnumber" => { '!=' => $item_3->itemnumber } })->filter_by_visible_in_opac();
+    $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, undef, $hid_rs );
+    like(   $xml, qr{<homebranch>$library_1_name</homebranch>}, '$item_1 present in the XML' );
+    like(   $xml, qr{<homebranch>$library_2_name</homebranch>}, '$item_2 present in the XML' );
+    unlike( $xml, qr{<homebranch>$library_3_name</homebranch>}, '$item_3 not present in the XML' );
+
     ## Test passing one item in hidden_items and items_rs
     $xml = C4::XSLT::buildKohaItemsNamespace( $biblio->biblionumber, [ $item_1->itemnumber ], $items_rs->reset );
 
-- 
2.25.1