From 96dee7fea6f92a324e1e43bf2ba98f97e2e72417 Mon Sep 17 00:00:00 2001
From: Alex Buckley <alexbuckley@catalyst.net.nz>
Date: Mon, 1 Aug 2022 18:58:19 +0000
Subject: [PATCH] Bug 25560: Unit tests

Test plan:
1. Run tests
sudo koha-shell <instancename>
prove t/db_dependent/Circulation/issue.t

Sponsored-By: Waikato Institute of Technology, NZ

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>
---
 t/db_dependent/Circulation/issue.t | 38 ++++++++++++++++++++++++++----
 1 file changed, 33 insertions(+), 5 deletions(-)

diff --git a/t/db_dependent/Circulation/issue.t b/t/db_dependent/Circulation/issue.t
index f388dab0076..9a28963b6c5 100755
--- a/t/db_dependent/Circulation/issue.t
+++ b/t/db_dependent/Circulation/issue.t
@@ -17,7 +17,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 49;
+use Test::More tests => 50;
 use DateTime::Duration;
 
 use t::lib::Mocks;
@@ -77,6 +77,11 @@ my $itemtype = $builder->build(
         value  => { notforloan => undef, rentalcharge => 0 }
     }
 )->{itemtype};
+my $itemtype2 = $builder->build(
+    {   source => 'Itemtype',
+        value  => { notforloan => undef }
+    }
+)->{itemtype};
 my $branchcode_1 = $builder->build({ source => 'Branch' })->{branchcode};
 my $branchcode_2 = $builder->build({ source => 'Branch' })->{branchcode};
 my $branchcode_3 = $builder->build({ source => 'Branch' })->{branchcode};
@@ -123,10 +128,17 @@ my $daysago10 = output_pref(
 
 # Add biblio and item
 my $record = MARC::Record->new();
+my $record2 = MARC::Record->new();
 $record->append_fields(
+    MARC::Field->new( '942', '0', '0', c => $itemtype ),
+    MARC::Field->new( '952', '0', '0', a => $branchcode_1 ) );
+
+$record2->append_fields(
+    MARC::Field->new( '942', '0', '0', c => $itemtype2 ),
     MARC::Field->new( '952', '0', '0', a => $branchcode_1 ) );
 
 my ( $biblionumber, $biblioitemnumber ) = C4::Biblio::AddBiblio( $record, '' );
+my ( $biblionumber2, $biblioitemnumber2 ) = C4::Biblio::AddBiblio( $record2, '' );
 
 my $barcode_1 = 'barcode_1';
 my $barcode_2 = 'barcode_2';
@@ -385,7 +397,8 @@ my $itemnumber = Koha::Item->new(
         homebranch     => $branchcode_1,
         holdingbranch  => $branchcode_1,
         notforloan     => 1,
-        itype          => $itemtype
+        itype          => $itemtype,
+        biblioitemnumber => $biblioitemnumber
     },
 )->store->itemnumber;
 
@@ -397,16 +410,30 @@ AddReturn( 'barcode_3', $branchcode_1 );
 my $item = Koha::Items->find( $itemnumber );
 ok( $item->notforloan eq 1, 'UpdateNotForLoanStatusOnCheckin does not modify value when not enabled' );
 
-t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckin', '1: 9' );
+my $updatenotforloanstatusoncheckin = "
+_ALL_:\n
+ 1: 0\n
+ 9: 1\n\n
+$itemtype:\n
+  1: 9
+";
+t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckin', $updatenotforloanstatusoncheckin );
 AddReturn( 'barcode_3', $branchcode_1 );
 $item = Koha::Items->find( $itemnumber );
-ok( $item->notforloan eq 9, q{UpdateNotForLoanStatusOnCheckin updates notforloan value from 1 to 9 with setting "1: 9"} );
+ok( $item->notforloan eq 9, q{UpdateNotForLoanStatusOnCheckin prioritises item type specific rule over _ALL_ rules} );
 my $log_count_after = $schema->resultset('ActionLog')->search({module => 'CATALOGUING'})->count();
 is($log_count_before, $log_count_after, "Change from UpdateNotForLoanStatusOnCheckin is not logged");
 
 AddReturn( 'barcode_3', $branchcode_1 );
 $item = Koha::Items->find( $itemnumber );
-ok( $item->notforloan eq 9, q{UpdateNotForLoanStatusOnCheckin does not update notforloan value from 9 with setting "1: 9"} );
+ok( $item->notforloan eq 9, q{UpdateNotForLoanStatusOnCheckin uses item type specific rules even if they do not target the returned items' notforloan value} );
+
+# Change the returning item to an item type without a rule
+Koha::Items->find( $itemnumber )->itype( $itemtype2 )->store;
+Koha::Items->find( $itemnumber )->notforloan( 1 )->store;
+AddReturn( 'barcode_3', $branchcode_1 );
+$item = Koha::Items->find( $itemnumber );
+ok( $item->notforloan eq 0, q{UpdateNotForLoanStatusOnCheckin _ALL_ rules are applied if there are no specific item type rule matching the returned item} );
 
 t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckin', '1: ONLYMESSAGE' );
 $item->notforloan(1)->store;
@@ -496,6 +523,7 @@ is( $item3->location, 'CART', q{UpdateItemLocationOnCheckin updates location val
 
 
 # Bug 14640 - Cancel the hold on checking out if asked
+Koha::Items->find({ barcode => $barcode_1 })->notforloan('0')->store;
 my $reserve_id = AddReserve(
     {
         branchcode     => $branchcode_1,
-- 
2.30.2