Bug 15407: Koha::Patron::Categories - replace GetBorrowercategory
[koha.git] / t / db_dependent / Reserves.t
index 4e6fd6f..771008b 100755 (executable)
@@ -18,6 +18,8 @@
 use Modern::Perl;
 
 use Test::More tests => 72;
+use Test::MockModule;
+use Test::Warn;
 
 use MARC::Record;
 use DateTime::Duration;
@@ -31,6 +33,7 @@ use Koha::Holds;
 use t::lib::Mocks;
 
 use Koha::DateUtils;
+use Koha::Patron::Categories;
 
 use Data::Dumper;
 BEGIN {
@@ -38,9 +41,10 @@ BEGIN {
 }
 
 # a very minimal mack of userenv for use by the test of DelItemCheck
-*C4::Context::userenv = sub {
-    return {};
-};
+my $module = new Test::MockModule('C4::Context');
+$module->mock('userenv', sub {
+    { }
+});
 
 my $dbh = C4::Context->dbh;
 
@@ -60,7 +64,7 @@ foreach my $addbra ('CPL', 'FPL', 'RPL') {
 
 # Add categories if not existing
 foreach my $addcat ('S', 'PT') {
-    $dbh->do("INSERT INTO categories (categorycode,hidelostitems,category_type) VALUES (?,?,?)",undef,($addcat, 0, $addcat eq 'S'? 'S': 'A')) unless GetBorrowercategory($addcat);
+    $dbh->do("INSERT INTO categories (categorycode,hidelostitems,category_type) VALUES (?,?,?)",undef,($addcat, 0, $addcat eq 'S'? 'S': 'A')) unless Koha::Patron::Categories->find($addcat);
 }
 
 # Create a helper biblio
@@ -128,21 +132,21 @@ is($status, "Reserved", "CheckReserves Test 2");
 is($status, "Reserved", "CheckReserves Test 3");
 
 my $ReservesControlBranch = C4::Context->preference('ReservesControlBranch');
-C4::Context->set_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
+t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
 ok(
     'ItemHomeLib' eq GetReservesControlBranch(
         { homebranch => 'ItemHomeLib' },
         { branchcode => 'PatronHomeLib' }
     ), "GetReservesControlBranch returns item home branch when set to ItemHomeLibrary"
 );
-C4::Context->set_preference( 'ReservesControlBranch', 'PatronLibrary' );
+t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'PatronLibrary' );
 ok(
     'PatronHomeLib' eq GetReservesControlBranch(
         { homebranch => 'ItemHomeLib' },
         { branchcode => 'PatronHomeLib' }
     ), "GetReservesControlBranch returns patron home branch when set to PatronLibrary"
 );
-C4::Context->set_preference( 'ReservesControlBranch', $ReservesControlBranch );
+t::lib::Mocks::mock_preference( 'ReservesControlBranch', $ReservesControlBranch );
 
 ###
 ### Regression test for bug 10272
@@ -265,7 +269,7 @@ AddReserve('CPL',  $requesters{'CPL'}, $bibnum2,
            $title,      $checkitem, $found);
 
 # Ensure that the item's home library controls hold policy lookup
-C4::Context->set_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
+t::lib::Mocks::mock_preference( 'ReservesControlBranch', 'ItemHomeLibrary' );
 
 my $messages;
 # Return the CPL item at FPL.  The hold that should be triggered is
@@ -279,6 +283,10 @@ is( $messages->{ResFound}->{borrowernumber},
 # Return the FPL item at FPL.  The hold that should be triggered is
 # the one placed by the RPL patron, as that patron is first in line
 # and RPL imposes no restrictions on whose holds its items can fill.
+
+# Ensure that the preference 'LocalHoldsPriority' is not set (Bug 15244):
+t::lib::Mocks::mock_preference( 'LocalHoldsPriority', '' );
+
 (undef, $messages, undef, undef) = AddReturn('bug10272_FPL', 'FPL');
 is( $messages->{ResFound}->{borrowernumber},
     $requesters{'RPL'},
@@ -320,7 +328,7 @@ is( $status, 'Reserved', 'CheckReserves also returns reserve with lookahead');
 
 # Test 9761b: Add a reserve with future date, CheckReserve should not return it
 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
-C4::Context->set_preference('AllowHoldDateInFuture', 1);
+t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
 $resdate= dt_from_string();
 $resdate->add_duration(DateTime::Duration->new(days => 4));
 $resdate=output_pref($resdate);
@@ -340,13 +348,13 @@ is( $status, 'Reserved', 'CheckReserves returns future reserve with sufficient l
 # Test 9761d: Check ResFound message of AddReturn for future hold
 # Note that AddReturn is in Circulation.pm, but this test really pertains to reserves; AddReturn uses the ConfirmFutureHolds pref when calling CheckReserves
 # In this test we do not need an issued item; it is just a 'checkin'
-C4::Context->set_preference('ConfirmFutureHolds', 0);
+t::lib::Mocks::mock_preference('ConfirmFutureHolds', 0);
 (my $doreturn, $messages)= AddReturn('97531','CPL');
 is($messages->{ResFound}//'', '', 'AddReturn does not care about future reserve when ConfirmFutureHolds is off');
-C4::Context->set_preference('ConfirmFutureHolds', 3);
+t::lib::Mocks::mock_preference('ConfirmFutureHolds', 3);
 ($doreturn, $messages)= AddReturn('97531','CPL');
 is(exists $messages->{ResFound}?1:0, 0, 'AddReturn ignores future reserve beyond ConfirmFutureHolds days');
-C4::Context->set_preference('ConfirmFutureHolds', 7);
+t::lib::Mocks::mock_preference('ConfirmFutureHolds', 7);
 ($doreturn, $messages)= AddReturn('97531','CPL');
 is(exists $messages->{ResFound}?1:0, 1, 'AddReturn considers future reserve within ConfirmFutureHolds days');
 
@@ -366,7 +374,7 @@ is($new_count, $hold_notice_count + 1, 'patron not notified a second time (bug 1
 # avoiding the not_same_branch error
 t::lib::Mocks::mock_preference('IndependentBranches', 0);
 is(
-    DelItemCheck($dbh, $bibnum, $itemnumber),
+    DelItemCheck( $bibnum, $itemnumber),
     'book_reserved',
     'item that is captured to fill a hold cannot be deleted',
 );
@@ -377,8 +385,8 @@ ok(defined($letter), 'can successfully generate hold slip (bug 10949)');
 # Tests for bug 9788: Does GetReservesFromItemnumber return a future wait?
 # 9788a: GetReservesFromItemnumber does not return future next available hold
 $dbh->do("DELETE FROM reserves WHERE biblionumber=?",undef,($bibnum));
-C4::Context->set_preference('ConfirmFutureHolds', 2);
-C4::Context->set_preference('AllowHoldDateInFuture', 1);
+t::lib::Mocks::mock_preference('ConfirmFutureHolds', 2);
+t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
 $resdate= dt_from_string();
 $resdate->add_duration(DateTime::Duration->new(days => 2));
 $resdate=output_pref($resdate);
@@ -430,7 +438,7 @@ AddReserve('CPL',  $requesters{'CPL2'}, $bibnum,
 $p = C4::Reserves::CalculatePriority($bibnum);
 is($p, 2, 'CalculatePriority should now return priority 2');
 #add another future hold
-C4::Context->set_preference('AllowHoldDateInFuture', 1);
+t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
 $resdate= dt_from_string();
 $resdate->add_duration(DateTime::Duration->new(days => 1));
 AddReserve('CPL',  $requesters{'CPL3'}, $bibnum,
@@ -443,12 +451,6 @@ $p = C4::Reserves::CalculatePriority($bibnum, $resdate);
 is($p, 3, 'CalculatePriority should now return priority 3');
 # End of tests for bug 8918
 
-# Test for bug 5144
-$reserve_id = AddReserve('CPL',  $requesters{'CPL3'}, $bibnum,
-           $bibitems,  $p, output_pref($resdate), $expdate, $notes,
-           $title,      $checkitem, $found);
-is( $reserve_id, undef, 'Attempt to add a second reserve on a given record for the same patron fails.' );
-
 # Tests for cancel reserves by users from OPAC.
 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
 AddReserve('CPL',  $requesters{'CPL'}, $item_bibnum,
@@ -496,7 +498,7 @@ is($cancancel, 0, 'Reserve in waiting status cant be canceled');
 ####### Testing Bug 13113 - Prevent juvenile/children from reserving ageRestricted material >>>
        ####
 
-C4::Context->set_preference( 'AgeRestrictionMarker', 'FSK|PEGI|Age|K' );
+t::lib::Mocks::mock_preference( 'AgeRestrictionMarker', 'FSK|PEGI|Age|K' );
 
 #Reserving an not-agerestricted Biblio by a Borrower with no dateofbirth is tested previously.
 
@@ -573,6 +575,9 @@ ok( $bz14464_reserve, 'Bug 14464 - 1st reserve correctly created' );
 
 CancelReserve({ reserve_id => $bz14464_reserve, charge_cancel_fee => 1 });
 
+my $old_reserve = Koha::Database->new()->schema()->resultset('OldReserve')->find( $bz14464_reserve );
+is($old_reserve->get_column('found'), 'W', 'Bug 14968 - Keep found column from reserve');
+
 ( undef, undef, $bz14464_fines ) = GetMemberIssuesAndFines( $borrowernumber );
 is( !$bz14464_fines || $bz14464_fines==0, 1, 'Bug 14464 - No fines after cancelling reserve with no charge configured' );
 
@@ -625,8 +630,8 @@ is( int( $bz14464_fines ), 42, 'Bug 14464 - Fine applied after cancelling reserv
 # tests for MoveReserve in relation to ConfirmFutureHolds (BZ 14526)
 #   hold from A pos 1, today, no fut holds: MoveReserve should fill it
 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
-C4::Context->set_preference('ConfirmFutureHolds', 0);
-C4::Context->set_preference('AllowHoldDateInFuture', 1);
+t::lib::Mocks::mock_preference('ConfirmFutureHolds', 0);
+t::lib::Mocks::mock_preference('AllowHoldDateInFuture', 1);
 AddReserve('CPL',  $borrowernumber, $item_bibnum,
     $bibitems,  1, undef, $expdate, $notes, $title, $checkitem, '');
 MoveReserve( $itemnumber, $borrowernumber );
@@ -649,7 +654,7 @@ MoveReserve( $itemnumber, $borrowernumber );
 is( $status, 'Reserved', 'MoveReserve did not fill future hold');
 $dbh->do('DELETE FROM reserves', undef, ($bibnum));
 #   hold from A pos 1, tomorrow, fut holds=2: MoveReserve should fill it
-C4::Context->set_preference('ConfirmFutureHolds', 2);
+t::lib::Mocks::mock_preference('ConfirmFutureHolds', 2);
 AddReserve('CPL',  $borrowernumber, $item_bibnum,
     $bibitems,  1, $resdate, $expdate, $notes, $title, $checkitem, '');
 MoveReserve( $itemnumber, $borrowernumber );