From e0adcf6de49f20c03fbff5fbe006506a8c49dc41 Mon Sep 17 00:00:00 2001
From: Alex Buckley <alexbuckley@catalyst.net.nz>
Date: Fri, 22 May 2020 04:43:02 +0000
Subject: [PATCH] Bug 25560: Unit tests

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

Sponsored-By: Waikato Institute of Technology, NZ
---
 t/db_dependent/Circulation/issue.t | 42 ++++++++++++++++++++++++++++++++++++--
 t/db_dependent/Koha/Object.t       |  4 +++-
 2 files changed, 43 insertions(+), 3 deletions(-)

diff --git a/t/db_dependent/Circulation/issue.t b/t/db_dependent/Circulation/issue.t
index 2a75da7de4..db463443da 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 => 46;
+use Test::More tests => 50;
 use DateTime::Duration;
 
 use t::lib::Mocks;
@@ -79,6 +79,12 @@ my $itemtype = $builder->build(
         value  => { notforloan => undef, rentalcharge => 0 }
     }
 )->{itemtype};
+my $itemtype2 = $builder->build(
+    {   source => 'Itemtype',
+        value  => { notforloan => undef, updatenotforloanstatusoncheckin => 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};
@@ -125,10 +131,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';
@@ -395,7 +408,8 @@ my $itemnumber = Koha::Item->new(
         homebranch     => $branchcode_1,
         holdingbranch  => $branchcode_1,
         notforloan     => 1,
-        itype          => $itemtype
+        itype          => $itemtype,
+        biblioitemnumber => $biblioitemnumber
     },
 )->store->itemnumber;
 
@@ -413,6 +427,29 @@ 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"} );
 
+# Item type exclusions from UpdateNotForLoanStatusOnCheckin syspref
+t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckin', '9: 1');
+AddReturn( 'barcode_3', $branchcode_1 );
+$item = Koha::Items->find( $itemnumber );
+ok( $item->notforloan eq 1, q{UpdateNotForLoanStatusOnCheckin updates notforloan value from 9 to 1 when the items type has itemtypes.updatenotforloanstatusoncheckin = 1} );
+
+Koha::Items->find( $itemnumber )->itype($itemtype2)->store;
+t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckin', '1: 9');
+AddReturn( 'barcode_3', $branchcode_1 );
+$item = Koha::Items->find( $itemnumber );
+ok( $item->notforloan eq 1, q{UpdateNotForLoanStatusOnCheckin does not update notforloan value when itemtypes.updatenotforloanstatusoncheckin = 0} );
+
+t::lib::Mocks::mock_preference( 'item-level_itypes', '0' );
+AddReturn( 'barcode_3', $branchcode_1 );
+$item = Koha::Items->find( $itemnumber );
+ok( $item->notforloan eq 9, q{UpdateNotForLoanStatusOnCheckin updates notforloan value when item-level_itypes = 0, returned item biblio-level itemtype enables updatenotforloanstatusoncheckin & item level itype disables updatenotforloanstatusoncheckin} );
+
+Koha::Items->find( $itemnumber )->biblioitemnumber($biblioitemnumber2)->store;
+t::lib::Mocks::mock_preference( 'UpdateNotForLoanStatusOnCheckin', '9: 1');
+AddReturn( 'barcode_3', $branchcode_1 );
+$item = Koha::Items->find( $itemnumber );
+ok( $item->notforloan eq 9, q{UpdateNotForLoanStatusOnCheckin does not update notforloanvalue when item-level_itypes=0, returned item biblio-level itemtype enables updatenotforloanstatusoncheckin & item level itype disables updatenotforloanstatusoncheck} );
+
 my $itemnumber2 = Koha::Item->new(
     {
         biblionumber   => $biblionumber,
@@ -475,6 +512,7 @@ ok( $item2->permanent_location eq '' , q{UpdateItemLocationOnCheckin does not up
 
 
 # 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,
diff --git a/t/db_dependent/Koha/Object.t b/t/db_dependent/Koha/Object.t
index b78283ce25..384e7e306e 100755
--- a/t/db_dependent/Koha/Object.t
+++ b/t/db_dependent/Koha/Object.t
@@ -604,7 +604,7 @@ subtest "Test update method" => sub {
 
 subtest 'store() tests' => sub {
 
-    plan tests => 16;
+    plan tests => 17;
 
     # Using Koha::ApiKey to test Koha::Object>-store
     # Simple object with foreign keys and unique key
@@ -701,6 +701,7 @@ subtest 'store() tests' => sub {
                 rentalcharge    => "",
                 notforloan      => "",
                 hideinopac      => "",
+                updatenotforloanstatusoncheckin => "",
             }
         )->store;
     };
@@ -708,6 +709,7 @@ subtest 'store() tests' => sub {
     is( $itemtype->rentalcharge, undef, 'decimal DEFAULT NULL should default to null');
     is( $itemtype->notforloan, undef, 'int DEFAULT NULL should default to null');
     is( $itemtype->hideinopac, 0, 'int NOT NULL DEFAULT 0 should default to 0');
+    is( $itemtype->updatenotforloanstatusoncheckin, 1, 'int NOT NULL DEFAULT 1 should default to 1');
 
     subtest 'Bad value tests' => sub {
 
-- 
2.11.0