From 8b7c666de702678367a19827a8f7810dd87ff67c Mon Sep 17 00:00:00 2001
From: Nick Clemens <nick@bywatersolutions.com>
Date: Tue, 22 Oct 2024 19:54:42 +0000
Subject: [PATCH] Bug 38148: Unit tests

Signed-off-by: Nick Clemens <nick@bywatersolutions.com>
---
 t/db_dependent/Koha/Items.t | 126 +++++++++++++++++++++++++++---------
 1 file changed, 95 insertions(+), 31 deletions(-)

diff --git a/t/db_dependent/Koha/Items.t b/t/db_dependent/Koha/Items.t
index 58e1c1f113f..6112f7dcfec 100755
--- a/t/db_dependent/Koha/Items.t
+++ b/t/db_dependent/Koha/Items.t
@@ -1998,7 +1998,7 @@ subtest 'search_ordered' => sub {
 
 subtest 'filter_by_for_hold' => sub {
 
-    plan tests => 15;
+    plan tests => 10;
 
     $schema->storage->txn_begin;
 
@@ -2035,36 +2035,100 @@ subtest 'filter_by_for_hold' => sub {
         { biblionumber => $biblio->biblionumber, notforloan => 0, itype => $holdable_itemtype } );
     is( $biblio->items->filter_by_for_hold->count, 1, 'hold allowed despite default rule' );
 
-    # Reset items and circ rules to remove default rule
-    $biblio->items->delete;
-    Koha::CirculationRules->search(
-        {
-            rule_name  => 'holdallowed',
-            rule_value => 'not_allowed',
-        }
-    )->delete;
-
-    $builder->build_sample_item( { biblionumber => $biblio->biblionumber, notforloan => 1 } );
-    is( $biblio->items->filter_by_for_hold->count, 0, 'no item for hold' );
-    $builder->build_sample_item( { biblionumber => $biblio->biblionumber, notforloan => 0 } );
-    is( $biblio->items->filter_by_for_hold->count, 1, '1 item for hold' );
-    $builder->build_sample_item( { biblionumber => $biblio->biblionumber, notforloan => -1 } );
-    is( $biblio->items->filter_by_for_hold->count, 2, '2 items for hold' );
-
-    $builder->build_sample_item( { biblionumber => $biblio->biblionumber, itemlost => 0, library => $library->id } );
-    $builder->build_sample_item( { biblionumber => $biblio->biblionumber, itemlost => 1, library => $library->id } );
-    is( $biblio->items->filter_by_for_hold->count, 3, '3 items for hold - itemlost' );
-
-    $builder->build_sample_item( { biblionumber => $biblio->biblionumber, withdrawn => 0, library => $library->id } );
-    $builder->build_sample_item( { biblionumber => $biblio->biblionumber, withdrawn => 1, library => $library->id } );
-    is( $biblio->items->filter_by_for_hold->count, 4, '4 items for hold - withdrawn' );
-
-    $builder->build_sample_item( { biblionumber => $biblio->biblionumber, damaged => 0 } );
-    $builder->build_sample_item( { biblionumber => $biblio->biblionumber, damaged => 1 } );
-    t::lib::Mocks::mock_preference('AllowHoldsOnDamagedItems', 0);
-    is( $biblio->items->filter_by_for_hold->count, 5, '5 items for hold - not damaged if not AllowHoldsOnDamagedItems' );
-    t::lib::Mocks::mock_preference('AllowHoldsOnDamagedItems', 1);
-    is( $biblio->items->filter_by_for_hold->count, 6, '6 items for hold - damaged if AllowHoldsOnDamagedItems' );
+    subtest "No default rule" => sub {
+        plan tests => 7;
+
+        # Reset items and circ rules to remove default rule
+        $biblio->items->delete;
+        Koha::CirculationRules->search(
+            {
+                rule_name  => 'holdallowed',
+                rule_value => 'not_allowed',
+            }
+        )->delete;
+
+        $builder->build_sample_item( { biblionumber => $biblio->biblionumber, notforloan => 1 } );
+        is( $biblio->items->filter_by_for_hold->count, 0, 'no item for hold' );
+        $builder->build_sample_item( { biblionumber => $biblio->biblionumber, notforloan => 0 } );
+        is( $biblio->items->filter_by_for_hold->count, 1, '1 item for hold' );
+        $builder->build_sample_item( { biblionumber => $biblio->biblionumber, notforloan => -1 } );
+        is( $biblio->items->filter_by_for_hold->count, 2, '2 items for hold' );
+
+        $builder->build_sample_item(
+            { biblionumber => $biblio->biblionumber, itemlost => 0, library => $library->id } );
+        $builder->build_sample_item(
+            { biblionumber => $biblio->biblionumber, itemlost => 1, library => $library->id } );
+        is( $biblio->items->filter_by_for_hold->count, 3, '3 items for hold - itemlost' );
+
+        $builder->build_sample_item(
+            { biblionumber => $biblio->biblionumber, withdrawn => 0, library => $library->id } );
+        $builder->build_sample_item(
+            { biblionumber => $biblio->biblionumber, withdrawn => 1, library => $library->id } );
+        is( $biblio->items->filter_by_for_hold->count, 4, '4 items for hold - withdrawn' );
+
+        $builder->build_sample_item( { biblionumber => $biblio->biblionumber, damaged => 0 } );
+        $builder->build_sample_item( { biblionumber => $biblio->biblionumber, damaged => 1 } );
+        t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 );
+        is(
+            $biblio->items->filter_by_for_hold->count, 5,
+            '5 items for hold - not damaged if not AllowHoldsOnDamagedItems'
+        );
+        t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 1 );
+        is( $biblio->items->filter_by_for_hold->count, 6, '6 items for hold - damaged if AllowHoldsOnDamagedItems' );
+    };
+
+    subtest "Default rule from_any_library" => sub {
+
+        plan tests => 7;
+
+        $biblio->items->delete;
+        Koha::CirculationRules->set_rule(
+            {
+                branchcode => undef,
+                itemtype   => undef,
+                rule_name  => 'holdallowed',
+                rule_value => 'from_any_library',
+            }
+        );
+
+        $builder->build_sample_item( { biblionumber => $biblio->biblionumber, notforloan => 1 } );
+        is( $biblio->items->filter_by_for_hold->count, 0, 'no item for hold' );
+        $builder->build_sample_item( { biblionumber => $biblio->biblionumber, notforloan => 0 } );
+        is( $biblio->items->filter_by_for_hold->count, 1, '1 item for hold' );
+        $builder->build_sample_item( { biblionumber => $biblio->biblionumber, notforloan => -1 } );
+        is( $biblio->items->filter_by_for_hold->count, 2, '2 items for hold' );
+
+        $builder->build_sample_item(
+            { biblionumber => $biblio->biblionumber, itemlost => 0, library => $library->id } );
+        $builder->build_sample_item(
+            { biblionumber => $biblio->biblionumber, itemlost => 1, library => $library->id } );
+        is( $biblio->items->filter_by_for_hold->count, 3, '3 items for hold - itemlost' );
+
+        $builder->build_sample_item(
+            { biblionumber => $biblio->biblionumber, withdrawn => 0, library => $library->id } );
+        $builder->build_sample_item(
+            { biblionumber => $biblio->biblionumber, withdrawn => 1, library => $library->id } );
+        is( $biblio->items->filter_by_for_hold->count, 4, '4 items for hold - withdrawn' );
+
+        $builder->build_sample_item( { biblionumber => $biblio->biblionumber, damaged => 0 } );
+        $builder->build_sample_item( { biblionumber => $biblio->biblionumber, damaged => 1 } );
+        t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 );
+        is(
+            $biblio->items->filter_by_for_hold->count, 5,
+            '5 items for hold - not damaged if not AllowHoldsOnDamagedItems'
+        );
+        t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 1 );
+        is( $biblio->items->filter_by_for_hold->count, 6, '6 items for hold - damaged if AllowHoldsOnDamagedItems' );
+
+        Koha::CirculationRules->search(
+            {
+                rule_name  => 'holdallowed',
+                branchcode => undef,
+                itemtype   => undef,
+                rule_value => 'from_any_library',
+            }
+        )->delete;
+    };
 
     $builder->build_sample_item(
         {
-- 
2.39.5