From 524a479029276c2e04ea556d4922d025992681ef Mon Sep 17 00:00:00 2001
From: Nick Clemens <nick@bywatersolutions.com>
Date: Wed, 4 Oct 2017 15:21:57 +0000
Subject: [PATCH] Bug 14576: Unit tests

---
 t/db_dependent/Circulation/Returns.t | 55 +++++-------------------------------
 t/db_dependent/Circulation/issue.t   | 54 ++++++++++++++++++++++++++++++++++-
 t/db_dependent/UsageStats.t          |  2 --
 3 files changed, 60 insertions(+), 51 deletions(-)

diff --git a/t/db_dependent/Circulation/Returns.t b/t/db_dependent/Circulation/Returns.t
index a8e4dda..5b0c60a 100644
--- a/t/db_dependent/Circulation/Returns.t
+++ b/t/db_dependent/Circulation/Returns.t
@@ -17,7 +17,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 5;
+use Test::More tests => 6;
 use Test::MockModule;
 use Test::Warn;
 
@@ -60,47 +60,6 @@ my $rule = Koha::IssuingRule->new(
 );
 $rule->store();
 
-subtest "InProcessingToShelvingCart tests" => sub {
-
-    plan tests => 2;
-
-    $branch = $builder->build({ source => 'Branch' })->{ branchcode };
-    my $permanent_location = 'TEST';
-    my $location           = 'PROC';
-
-    # Create a biblio record with biblio-level itemtype
-    my $record = MARC::Record->new();
-    my ( $biblionumber, $biblioitemnumber ) = AddBiblio( $record, '' );
-    my $built_item = $builder->build({
-        source => 'Item',
-        value  => {
-            biblionumber  => $biblionumber,
-            homebranch    => $branch,
-            holdingbranch => $branch,
-            location      => $location,
-            permanent_location => $permanent_location
-        }
-    });
-    my $barcode = $built_item->{ barcode };
-    my $itemnumber = $built_item->{ itemnumber };
-    my $item;
-
-    t::lib::Mocks::mock_preference( "InProcessingToShelvingCart", 1 );
-    AddReturn( $barcode, $branch );
-    $item = GetItem( $itemnumber );
-    is( $item->{location}, 'CART',
-        "InProcessingToShelvingCart functions as intended" );
-
-    $item->{location} = $location;
-    ModItem( $item, undef, $itemnumber );
-
-    t::lib::Mocks::mock_preference( "InProcessingToShelvingCart", 0 );
-    AddReturn( $barcode, $branch );
-    $item = GetItem( $itemnumber );
-    is( $item->{location}, $permanent_location,
-        "InProcessingToShelvingCart functions as intended" );
-};
-
 
 subtest "AddReturn logging on statistics table (item-level_itypes=1)" => sub {
 
@@ -175,13 +134,12 @@ subtest "AddReturn logging on statistics table (item-level_itypes=1)" => sub {
 
     is( $stat->itemtype, $ilevel_itemtype,
         "item-level itype recorded on statistics for return");
-    warning_like { AddIssue( $borrower, $item_without_itemtype->{ barcode } ) }
-                 [qr/^item-level_itypes set but no itemtype set for item/,
-                 qr/^item-level_itypes set but no itemtype set for item/],
+    warnings_like { AddIssue( $borrower, $item_without_itemtype->{ barcode } ) }
+                 [qr/^item-level_itypes set but no itemtype set for item/,qr/^item-level_itypes set but no itemtype set for item/],
                  'Item without itemtype set raises warning on AddIssue';
-    warning_like { AddReturn( $item_without_itemtype->{ barcode }, $branch ) }
-                 qr/^item-level_itypes set but no itemtype set for item/,
-                 'Item without itemtype set raises warning on AddReturn';
+    warnings_like { AddReturn( $item_without_itemtype->{ barcode }, $branch ) }
+                [qr/^item-level_itypes set but no itemtype set for item/,qr/^item-level_itypes set but no itemtype set for item/],
+                 'Item without itemtype set raises warnings on AddReturn';
     # Test biblio-level itemtype was recorded on the 'statistics' table
     $stat = $schema->resultset('Statistic')->search({
         branch     => $branch,
@@ -335,6 +293,7 @@ subtest 'Handle ids duplication' => sub {
 
     is( Koha::Checkouts->find( $issue_id )->issue_id, $issue_id, 'The issues entry should not have been removed' );
 };
+$schema->storage->txn_rollback;
 
 subtest 'BlockReturnOfLostItems' => sub {
     plan tests => 3;
diff --git a/t/db_dependent/Circulation/issue.t b/t/db_dependent/Circulation/issue.t
index 68160dd..f375721 100644
--- 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 => 32;
+use Test::More tests => 44;
 use DateTime::Duration;
 
 use t::lib::Mocks;
@@ -371,6 +371,58 @@ AddReturn( 'barcode_3', $branchcode_1 );
 $item = GetItem( $itemnumber );
 ok( $item->{notforloan} eq 9, q{UpdateNotForLoanStatusOnCheckin does not update notforloan value from 9 with setting "1: 9"} );
 
+my $itemnumber2;
+($biblionumber, $biblioitemnumber, $itemnumber2) = C4::Items::AddItem(
+    {
+        barcode        => 'barcode_4',
+        itemcallnumber => 'callnumber4',
+        homebranch     => $branchcode_1,
+        holdingbranch  => $branchcode_1,
+        location => 'FIC',
+        itype          => $itemtype
+    },
+    $biblionumber
+);
+
+t::lib::Mocks::mock_preference( 'UpdateItemLocationOnCheckin', q{} );
+AddReturn( 'barcode_4', $branchcode_1 );
+my $item2 = GetItem( $itemnumber2 );
+ok( $item2->{location} eq 'FIC', 'UpdateItemLocationOnCheckin does not modify value when not enabled' );
+
+t::lib::Mocks::mock_preference( 'UpdateItemLocationOnCheckin', 'FIC: GEN' );
+AddReturn( 'barcode_4', $branchcode_1 );
+$item2 = GetItem( $itemnumber2 );
+ok( $item2->{location} eq 'GEN', q{UpdateItemLocationOnCheckin updates location value from 'FIC' to 'GEN' with setting "FIC: GEN"} );
+ok( $item2->{permanent_location} eq 'GEN', q{UpdateItemLocationOnCheckin updates permanent_location value from 'FIC' to 'GEN' with setting "FIC: GEN"} );
+AddReturn( 'barcode_4', $branchcode_1 );
+$item2 = GetItem( $itemnumber2 );
+ok( $item2->{location} eq 'GEN', q{UpdateItemLocationOnCheckin does not update location value from 'GEN' with setting "FIC: GEN"} );
+
+t::lib::Mocks::mock_preference( 'UpdateItemLocationOnCheckin', '_ALL_: CART' );
+AddReturn( 'barcode_4', $branchcode_1 );
+$item2 = GetItem( $itemnumber2 );
+ok( $item2->{location} eq 'CART', q{UpdateItemLocationOnCheckin updates location value from 'GEN' with setting "_ALL_: CART"} );
+ok( $item2->{permanent_location} eq 'GEN', q{UpdateItemLocationOnCheckin does not update permanent_location value from 'GEN' with setting "_ALL_: CART"} );
+AddIssue( $borrower_1, 'barcode_4', $daysago10,0, $today, '' );
+$item2 = GetItem( $itemnumber2 );
+ok( $item2->{location} eq 'GEN', q{Location updates from 'CART' to permanent location on issue} );
+
+t::lib::Mocks::mock_preference( 'UpdateItemLocationOnCheckin', "GEN: _BLANK_\n_BLANK_: PROC\nPROC: _PERM_" );
+AddReturn( 'barcode_4', $branchcode_1 );
+$item2 = GetItem( $itemnumber2 );
+ok( $item2->{location} eq '', q{UpdateItemLocationOnCheckin updates location value from 'GEN' to '' with setting "GEN: _BLANK_"} );
+AddReturn( 'barcode_4', $branchcode_1 );
+$item2 = GetItem( $itemnumber2 );
+ok( $item2->{location} eq 'PROC' , q{UpdateItemLocationOnCheckin updates location value from '' to 'PROC' with setting "_BLANK_: PROC"} );
+ok( $item2->{permanent_location} eq '' , q{UpdateItemLocationOnCheckin does not update permanent_location value from '' to 'PROC' with setting "_BLANK_: PROC"} );
+AddReturn( 'barcode_4', $branchcode_1 );
+$item2 = GetItem( $itemnumber2 );
+ok( $item2->{location} eq '' , q{UpdateItemLocationOnCheckin updates location value from 'PROC' to '' with setting "PROC: _PERM_" } );
+ok( $item2->{permanent_location} eq '' , q{UpdateItemLocationOnCheckin does not update permanent_location from '' with setting "PROC: _PERM_" } );
+
+
+
+
 # Bug 14640 - Cancel the hold on checking out if asked
 my $reserve_id = AddReserve($branchcode_1, $borrower_id1, $biblionumber,
     undef,  1, undef, undef, "a note", "a title", undef, '');
diff --git a/t/db_dependent/UsageStats.t b/t/db_dependent/UsageStats.t
index f2770f7..a29bf8c 100644
--- a/t/db_dependent/UsageStats.t
+++ b/t/db_dependent/UsageStats.t
@@ -397,7 +397,6 @@ sub mocking_systempreferences_to_a_set_value {
         CircControl
         HomeOrHoldingBranch
         HomeOrHoldingBranchReturn
-        InProcessingToShelvingCart
         IssueLostItem
         IssuingInProcess
         ManInvInNoissuesCharge
@@ -406,7 +405,6 @@ sub mocking_systempreferences_to_a_set_value {
         RenewalSendNotice
         RentalsInNoissuesCharge
         ReturnBeforeExpiry
-        ReturnToShelvingCart
         TransfersMaxDaysWarning
         UseBranchTransferLimits
         useDaysMode
-- 
2.1.4