From 290cabb39e8302a05a32dddabcdc34b23ce63e39 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 xt
prove t/db_dependent/Circulation/issue.t

Sponsored-By: Waikato Institute of Technology, NZ
---
 t/db_dependent/Circulation/issue.t | 53 ++++++++++++++++++++++++++----
 1 file changed, 46 insertions(+), 7 deletions(-)

diff --git a/t/db_dependent/Circulation/issue.t b/t/db_dependent/Circulation/issue.t
index 81d5e6742c5..5e3fedb7847 100755
--- a/t/db_dependent/Circulation/issue.t
+++ b/t/db_dependent/Circulation/issue.t
@@ -17,9 +17,8 @@
 
 use Modern::Perl;
 
-use Test::More tests => 50;
+use Test::More tests => 52;
 use DateTime::Duration;
-
 use t::lib::Mocks;
 use t::lib::TestBuilder;
 
@@ -75,9 +74,15 @@ $dbh->do(q|DELETE FROM statistics|);
 # Generate sample datas
 my $itemtype = $builder->build(
     {   source => 'Itemtype',
-        value  => { notforloan => undef, rentalcharge => 0 }
+        value  => { notforloan => undef, rentalcharge => 0, updatenotforloan => 1 }
     }
 )->{itemtype};
+my $itemtype2 = $builder->build(
+    {   source => 'Itemtype',
+        value  => { notforloan => undef, updatenotforloan => 0 }
+    }
+)->{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};
@@ -124,10 +129,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';
@@ -391,10 +403,14 @@ my $itemnumber = Koha::Item->new(
         homebranch     => $branchcode_1,
         holdingbranch  => $branchcode_1,
         notforloan     => 1,
-        itype          => $itemtype
+        itype          => $itemtype,
+        biblioitemnumber => $biblioitemnumber
     },
 )->store->itemnumber;
 
+# item-level_itypes syspref is set to 'specific item'
+t::lib::Mocks::mock_preference( 'item-level_itypes', '1' );
+
 t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckin', q{} );
 t::lib::Mocks::mock_preference( 'CataloguingLog', 1 );
 my $log_count_before = $schema->resultset('ActionLog')->search({module => 'CATALOGUING'})->count();
@@ -403,16 +419,38 @@ 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 = "
+$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 updates notforloan value from 1 to 9 with setting "1: 9" for the item type returned} );
 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 does not update notforloan value from 9 with setting "1: 9" for the item type returned} );
+
+# 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 1, q{UpdateNotForLoanStatusOnCheckin does not update notforloan value from 1 when there is no setting for the items' itemtype} );
+
+# Test UpdateNotForLoanStatusOnCheckin with different biblioitems.itemtype and items.itype values
+
+# item-level_itypes syspref is set to 'bibliographic record'
+t::lib::Mocks::mock_preference( 'item-level_itypes', '0' );
+
+Koha::Items->find( $itemnumber )->biblioitemnumber( $biblioitemnumber2 )->store;
+Koha::Items->find( $itemnumber )->itype($itemtype)->store;
+AddReturn( 'barcode_3', $branchcode_1 );
+$item = Koha::Items->find( $itemnumber );
+ok( $item->notforloan eq 9, q{UpdateNotForLoanStatusOnCheckin updates notforloan value from 1 to 9 when the authoritative item type has a setting "1: 9" for that item type} );
 
 my $itemnumber2 = Koha::Item->new(
     {
@@ -496,6 +534,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.20.1