Bug 18887: Port max_holds rules to new CirculationRules system
[srvgit] / t / db_dependent / Holds.t
index ad87b26..25e12e0 100755 (executable)
@@ -7,22 +7,26 @@ use t::lib::TestBuilder;
 
 use C4::Context;
 
-use Test::More tests => 58;
+use Test::More tests => 56;
 use MARC::Record;
-use C4::Biblio;
+use Koha::Patrons;
 use C4::Items;
-use C4::Members;
+use C4::Biblio;
+use C4::Reserves;
 use C4::Calendar;
+
 use Koha::Database;
 use Koha::DateUtils qw( dt_from_string output_pref );
 use Koha::Biblios;
 use Koha::Holds;
+use Koha::Items;
+use Koha::Libraries;
 use Koha::Patrons;
+use Koha::CirculationRules;
 
 BEGIN {
     use FindBin;
     use lib $FindBin::Bin;
-    use_ok('C4::Reserves');
 }
 
 my $schema  = Koha::Database->new->schema;
@@ -58,12 +62,12 @@ my ($item_bibnum, $item_bibitemnum, $itemnumber)
 # Create some borrowers
 my @borrowernumbers;
 foreach (1..$borrowers_count) {
-    my $borrowernumber = AddMember(
+    my $borrowernumber = Koha::Patron->new({
         firstname =>  'my firstname',
         surname => 'my surname ' . $_,
         categorycode => $category->{categorycode},
         branchcode => $branch_1,
-    );
+    })->store->borrowernumber;
     push @borrowernumbers, $borrowernumber;
 }
 
@@ -121,19 +125,13 @@ ok( $hold_branch == $hold->branch(), "branch method returns stashed branch" );
 my $hold_found = $hold->found();
 $hold->set({ found => 'W'})->store();
 is( Koha::Holds->waiting()->count(), 1, "Koha::Holds->waiting returns waiting holds" );
+is( Koha::Holds->unfilled()->count(), 4, "Koha::Holds->unfilled returns unfilled holds" );
 
 my $patron = Koha::Patrons->find( $borrowernumbers[0] );
 $holds = $patron->holds;
 is( $holds->next->borrowernumber, $borrowernumbers[0], "Test Koha::Patron->holds");
 
 
-ok( GetReserveCount( $borrowernumbers[0] ), "Test GetReserveCount()" );
-
-
-CancelReserve({ 'reserve_id' => $reserve_id });
-$holds = $biblio->holds;
-is( $holds->count, $borrowers_count - 1, "Test CancelReserve()" );
-
 $holds = $item->current_holds;
 $first_hold = $holds->next;
 $borrowernumber = $first_hold->borrowernumber;
@@ -148,22 +146,31 @@ ModReserve({
     suspend_until => output_pref( { dt => dt_from_string( "2013-01-01", "iso" ), dateonly => 1 } ),
 });
 
-my $reserve = GetReserve( $reserve_id );
-ok( $reserve->{'priority'} eq '4', "Test GetReserve(), priority changed correctly" );
-ok( $reserve->{'suspend'}, "Test GetReserve(), suspend hold" );
-is( $reserve->{'suspend_until'}, '2013-01-01 00:00:00', "Test GetReserve(), suspend until date" );
+$hold = Koha::Holds->find( $reserve_id );
+ok( $hold->priority eq '4', "Test ModReserve, priority changed correctly" );
+ok( $hold->suspend, "Test ModReserve, suspend hold" );
+is( $hold->suspend_until, '2013-01-01 00:00:00', "Test ModReserve, suspend until date" );
+
+ModReserve({ # call without reserve_id
+    rank          => '3',
+    biblionumber  => $item_bibnum,
+    itemnumber    => $itemnumber,
+    borrowernumber => $borrowernumber,
+});
+$hold = Koha::Holds->find( $reserve_id );
+ok( $hold->priority eq '3', "Test ModReserve, priority changed correctly" );
 
 ToggleSuspend( $reserve_id );
-$reserve = GetReserve( $reserve_id );
-ok( !$reserve->{'suspend'}, "Test ToggleSuspend(), no date" );
+$hold = Koha::Holds->find( $reserve_id );
+ok( ! $hold->suspend, "Test ToggleSuspend(), no date" );
 
 ToggleSuspend( $reserve_id, '2012-01-01' );
-$reserve = GetReserve( $reserve_id );
-is( $reserve->{'suspend_until'}, '2012-01-01 00:00:00', "Test ToggleSuspend(), with date" );
+$hold = Koha::Holds->find( $reserve_id );
+is( $hold->suspend_until, '2012-01-01 00:00:00', "Test ToggleSuspend(), with date" );
 
 AutoUnsuspendReserves();
-$reserve = GetReserve( $reserve_id );
-ok( !$reserve->{'suspend'}, "Test AutoUnsuspendReserves()" );
+$hold = Koha::Holds->find( $reserve_id );
+ok( ! $hold->suspend, "Test AutoUnsuspendReserves()" );
 
 SuspendAll(
     borrowernumber => $borrowernumber,
@@ -171,18 +178,18 @@ SuspendAll(
     suspend => 1,
     suspend_until => '2012-01-01',
 );
-$reserve = GetReserve( $reserve_id );
-is( $reserve->{'suspend'}, 1, "Test SuspendAll()" );
-is( $reserve->{'suspend_until'}, '2012-01-01 00:00:00', "Test SuspendAll(), with date" );
+$hold = Koha::Holds->find( $reserve_id );
+is( $hold->suspend, 1, "Test SuspendAll()" );
+is( $hold->suspend_until, '2012-01-01 00:00:00', "Test SuspendAll(), with date" );
 
 SuspendAll(
     borrowernumber => $borrowernumber,
     biblionumber   => $biblionumber,
     suspend => 0,
 );
-$reserve = GetReserve( $reserve_id );
-is( $reserve->{'suspend'}, 0, "Test resuming with SuspendAll()" );
-is( $reserve->{'suspend_until'}, undef, "Test resuming with SuspendAll(), should have no suspend until date" );
+$hold = Koha::Holds->find( $reserve_id );
+is( $hold->suspend, 0, "Test resuming with SuspendAll()" );
+is( $hold->suspend_until, undef, "Test resuming with SuspendAll(), should have no suspend until date" );
 
 # Add a new hold for the borrower whose hold we canceled earlier, this time at the bib level
 AddReserve(
@@ -200,39 +207,28 @@ AddReserve(
 );
 $patron = Koha::Patrons->find( $borrowernumber );
 $holds = $patron->holds;
-my $reserveid = C4::Reserves::GetReserveId(
-    {
-        biblionumber => $biblionumber,
-        borrowernumber => $borrowernumber
-    }
-);
-is( $reserveid, $holds->next->reserve_id, "Test GetReserveId" );
-ModReserveMinusPriority( $itemnumber, $reserve->{'reserve_id'} );
+my $reserveid = Koha::Holds->search({ biblionumber => $bibnum, borrowernumber => $borrowernumbers[0] })->next->reserve_id;
+ModReserveMinusPriority( $itemnumber, $reserveid );
 $holds = $patron->holds;
 is( $holds->next->itemnumber, $itemnumber, "Test ModReserveMinusPriority()" );
 
-
-my $reserve2 = GetReserveInfo( $reserve->{'reserve_id'} );
-ok( $reserve->{'reserve_id'} eq $reserve2->{'reserve_id'}, "Test GetReserveInfo()" );
-
-
 $holds = $biblio->holds;
 $hold = $holds->next;
-AlterPriority( 'top', $hold->reserve_id );
-$reserve = GetReserve( $reserve->{'reserve_id'} );
-is( $reserve->{'priority'}, '1', "Test AlterPriority(), move to top" );
+AlterPriority( 'top', $hold->reserve_id, undef, 2, 1, 6 );
+$hold = Koha::Holds->find( $reserveid );
+is( $hold->priority, '1', "Test AlterPriority(), move to top" );
 
-AlterPriority( 'down', $reserve->{'reserve_id'} );
-$reserve = GetReserve( $reserve->{'reserve_id'} );
-is( $reserve->{'priority'}, '2', "Test AlterPriority(), move down" );
+AlterPriority( 'down', $hold->reserve_id, undef, 2, 1, 6 );
+$hold = Koha::Holds->find( $reserveid );
+is( $hold->priority, '2', "Test AlterPriority(), move down" );
 
-AlterPriority( 'up', $reserve->{'reserve_id'} );
-$reserve = GetReserve( $reserve->{'reserve_id'} );
-is( $reserve->{'priority'}, '1', "Test AlterPriority(), move up" );
+AlterPriority( 'up', $hold->reserve_id, 1, 3, 1, 6 );
+$hold = Koha::Holds->find( $reserveid );
+is( $hold->priority, '1', "Test AlterPriority(), move up" );
 
-AlterPriority( 'bottom', $reserve->{'reserve_id'} );
-$reserve = GetReserve( $reserve->{'reserve_id'} );
-is( $reserve->{'priority'}, '5', "Test AlterPriority(), move to bottom" );
+AlterPriority( 'bottom', $hold->reserve_id, undef, 2, 1, 6 );
+$hold = Koha::Holds->find( $reserveid );
+is( $hold->priority, '6', "Test AlterPriority(), move to bottom" );
 
 # Regression test for bug 2394
 #
@@ -269,7 +265,7 @@ t::lib::Mocks::mock_preference('item-level_itypes', 1);
 # if IndependentBranches is OFF, a $branch_1 patron can reserve an $branch_2 item
 t::lib::Mocks::mock_preference('IndependentBranches', 0);
 ok(
-    CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber) eq 'OK',
+    CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'OK',
     '$branch_1 patron allowed to reserve $branch_2 item with IndependentBranches OFF (bug 2394)'
 );
 
@@ -277,80 +273,37 @@ ok(
 t::lib::Mocks::mock_preference('IndependentBranches', 1);
 t::lib::Mocks::mock_preference('canreservefromotherbranches', 0);
 ok(
-    CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber) eq 'cannotReserveFromOtherBranches',
+    CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'cannotReserveFromOtherBranches',
     '$branch_1 patron NOT allowed to reserve $branch_2 item with IndependentBranches ON ... (bug 2394)'
 );
 
 # ... unless canreservefromotherbranches is ON
 t::lib::Mocks::mock_preference('canreservefromotherbranches', 1);
 ok(
-    CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber) eq 'OK',
+    CanItemBeReserved($borrowernumbers[0], $foreign_itemnumber)->{status} eq 'OK',
     '... unless canreservefromotherbranches is ON (bug 2394)'
 );
 
-# Regression test for bug 11336
-($bibnum, $title, $bibitemnum) = create_helper_biblio('DUMMY');
-($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
-AddReserve(
-    $branch_1,
-    $borrowernumbers[0],
-    $bibnum,
-    '',
-    1,
-);
-
-my $reserveid1 = C4::Reserves::GetReserveId(
-    {
-        biblionumber => $bibnum,
-        borrowernumber => $borrowernumbers[0]
-    }
-);
-
-($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
-AddReserve(
-    $branch_1,
-    $borrowernumbers[1],
-    $bibnum,
-    '',
-    2,
-);
-my $reserveid2 = C4::Reserves::GetReserveId(
-    {
-        biblionumber => $bibnum,
-        borrowernumber => $borrowernumbers[1]
-    }
-);
-
-CancelReserve({ reserve_id => $reserveid1 });
-
-$reserve2 = GetReserve( $reserveid2 );
-is( $reserve2->{priority}, 1, "After cancelreserve, the 2nd reserve becomes the first on the waiting list" );
-
-($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
-AddReserve(
-    $branch_1,
-    $borrowernumbers[0],
-    $bibnum,
-    '',
-    2,
-);
-my $reserveid3 = C4::Reserves::GetReserveId(
-    {
-        biblionumber => $bibnum,
-        borrowernumber => $borrowernumbers[0]
-    }
-);
-
-my $reserve3 = GetReserve( $reserveid3 );
-is( $reserve3->{priority}, 2, "New reserve for patron 0, the reserve has a priority = 2" );
-
-ModReserve({ reserve_id => $reserveid2, rank => 'del' });
-$reserve3 = GetReserve( $reserveid3 );
-is( $reserve3->{priority}, 1, "After ModReserve, the 3rd reserve becomes the first on the waiting list" );
+{
+    # Regression test for bug 11336 # Test if ModReserve correctly recalculate the priorities
+    ($bibnum, $title, $bibitemnum) = create_helper_biblio('DUMMY');
+    ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
+    my $reserveid1 = AddReserve($branch_1, $borrowernumbers[0], $bibnum, '', 1);
+    ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
+    my $reserveid2 = AddReserve($branch_1, $borrowernumbers[1], $bibnum, '', 2);
+    ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem({ homebranch => $branch_1, holdingbranch => $branch_1 } , $bibnum);
+    my $reserveid3 = AddReserve($branch_1, $borrowernumbers[2], $bibnum, '', 3);
+    my $hhh = Koha::Holds->search({ biblionumber => $bibnum });
+    my $hold3 = Koha::Holds->find( $reserveid3 );
+    is( $hold3->priority, 3, "The 3rd hold should have a priority set to 3" );
+    ModReserve({ reserve_id => $reserveid1, rank => 'del' });
+    ModReserve({ reserve_id => $reserveid2, rank => 'del' });
+    is( $hold3->discard_changes->priority, 1, "After ModReserve, the 3rd reserve becomes the first on the waiting list" );
+}
 
 ModItem({ damaged => 1 }, $item_bibnum, $itemnumber);
 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 1 );
-is( CanItemBeReserved( $borrowernumbers[0], $itemnumber), 'OK', "Patron can reserve damaged item with AllowHoldsOnDamagedItems enabled" );
+is( CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status}, 'OK', "Patron can reserve damaged item with AllowHoldsOnDamagedItems enabled" );
 ok( defined( ( CheckReserves($itemnumber) )[1] ), "Hold can be trapped for damaged item with AllowHoldsOnDamagedItems enabled" );
 
 $hold = Koha::Hold->new(
@@ -360,13 +313,13 @@ $hold = Koha::Hold->new(
         biblionumber   => $item_bibnum,
     }
 )->store();
-is( CanItemBeReserved( $borrowernumbers[0], $itemnumber ),
+is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
     'itemAlreadyOnHold',
     "Patron cannot place a second item level hold for a given item" );
 $hold->delete();
 
 t::lib::Mocks::mock_preference( 'AllowHoldsOnDamagedItems', 0 );
-ok( CanItemBeReserved( $borrowernumbers[0], $itemnumber) eq 'damaged', "Patron cannot reserve damaged item with AllowHoldsOnDamagedItems disabled" );
+ok( CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'damaged', "Patron cannot reserve damaged item with AllowHoldsOnDamagedItems disabled" );
 ok( !defined( ( CheckReserves($itemnumber) )[1] ), "Hold cannot be trapped for damaged item with AllowHoldsOnDamagedItems disabled" );
 
 # Regression test for bug 9532
@@ -380,19 +333,19 @@ AddReserve(
     1,
 );
 is(
-    CanItemBeReserved( $borrowernumbers[0], $itemnumber), 'tooManyReserves',
+    CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status}, 'tooManyReserves',
     "cannot request item if policy that matches on item-level item type forbids it"
 );
 ModItem({ itype => 'CAN' }, $item_bibnum, $itemnumber);
 ok(
-    CanItemBeReserved( $borrowernumbers[0], $itemnumber) eq 'OK',
+    CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'OK',
     "can request item if policy that matches on item type allows it"
 );
 
 t::lib::Mocks::mock_preference('item-level_itypes', 0);
 ModItem({ itype => undef }, $item_bibnum, $itemnumber);
 ok(
-    CanItemBeReserved( $borrowernumbers[0], $itemnumber) eq 'tooManyReserves',
+    CanItemBeReserved( $borrowernumbers[0], $itemnumber)->{status} eq 'tooManyReserves',
     "cannot request item if policy that matches on bib-level item type forbids it (bug 9532)"
 );
 
@@ -421,19 +374,19 @@ $dbh->do(q{
 ($bibnum, $title, $bibitemnum) = create_helper_biblio('CANNOT');
 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
     { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CANNOT' } , $bibnum);
-is(CanItemBeReserved($borrowernumbers[0], $itemnumber), 'notReservable',
-    "CanItemBeReserved should returns 'notReservable'");
+is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'notReservable',
+    "CanItemBeReserved should return 'notReservable'");
 
 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
     { homebranch => $branch_2, holdingbranch => $branch_1, itype => 'CAN' } , $bibnum);
-is(CanItemBeReserved($borrowernumbers[0], $itemnumber),
+is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status},
     'cannotReserveFromOtherBranches',
-    "CanItemBeReserved should returns 'cannotReserveFromOtherBranches'");
+    "CanItemBeReserved should return 'cannotReserveFromOtherBranches'");
 
 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
     { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CAN' } , $bibnum);
-is(CanItemBeReserved($borrowernumbers[0], $itemnumber), 'OK',
-    "CanItemBeReserved should returns 'OK'");
+is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'OK',
+    "CanItemBeReserved should return 'OK'");
 
 # Bug 12632
 t::lib::Mocks::mock_preference( 'item-level_itypes',     1 );
@@ -454,14 +407,117 @@ $dbh->do(
     {},
     '*', '*', 'ONLY1', 1, 99
 );
-is( CanItemBeReserved( $borrowernumbers[0], $itemnumber ),
+is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
     'OK', 'Patron can reserve item with hold limit of 1, no holds placed' );
 
 my $res_id = AddReserve( $branch_1, $borrowernumbers[0], $bibnum, '', 1, );
 
-is( CanItemBeReserved( $borrowernumbers[0], $itemnumber ),
+is( CanItemBeReserved( $borrowernumbers[0], $itemnumber )->{status},
     'tooManyReserves', 'Patron cannot reserve item with hold limit of 1, 1 bib level hold placed' );
 
+subtest 'Test max_holds per library/patron category' => sub {
+    plan tests => 6;
+
+    $dbh->do('DELETE FROM reserves');
+    $dbh->do('DELETE FROM issuingrules');
+    $dbh->do('DELETE FROM circulation_rules');
+
+    ( $bibnum, $title, $bibitemnum ) = create_helper_biblio('TEST');
+    ( $item_bibnum, $item_bibitemnum, $itemnumber ) =
+      AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 },
+        $bibnum );
+    $dbh->do(
+        q{
+            INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
+            VALUES (?, ?, ?, ?, ?)
+        },
+        {},
+        '*', '*', 'TEST', 99, 99
+    );
+    AddReserve( $branch_1, $borrowernumbers[0], $bibnum, '', 1, );
+    AddReserve( $branch_1, $borrowernumbers[0], $bibnum, '', 1, );
+    AddReserve( $branch_1, $borrowernumbers[0], $bibnum, '', 1, );
+
+    my $count =
+      Koha::Holds->search( { borrowernumber => $borrowernumbers[0] } )->count();
+    is( $count, 3, 'Patron now has 3 holds' );
+
+    my $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
+    is( $ret->{status}, 'OK', 'Patron can place hold with no borrower circ rules' );
+
+    my $rule_all = Koha::CirculationRules->set_rule(
+        {
+            categorycode => $category->{categorycode},
+            branchcode   => undef,
+            itemtype     => undef,
+            rule_name    => 'max_holds',
+            rule_value   => 3,
+        }
+    );
+
+    my $rule_branch = Koha::CirculationRules->set_rule(
+        {
+            branchcode   => $branch_1,
+            categorycode => $category->{categorycode},
+            itemtype     => undef,
+            rule_name    => 'max_holds',
+            rule_value   => 5,
+        }
+    );
+
+    $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
+    is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 3' );
+
+    $rule_branch->delete();
+
+    $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
+    is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a category rule of 3' );
+
+    $rule_all->delete();
+    $rule_branch->rule_value(3);
+    $rule_branch->store();
+
+    $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
+    is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a branch/category rule of 3' );
+
+    $rule_branch->rule_value(5);
+    $rule_branch->update();
+    $rule_branch->rule_value(5);
+    $rule_branch->store();
+
+    $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
+    is( $ret->{status}, 'OK', 'Patron can place hold with branch/category rule of 5, category rule of 5' );
+};
+
+subtest 'Pickup location availability tests' => sub {
+    plan tests => 3;
+
+    my ( $bibnum, $title, $bibitemnum ) = create_helper_biblio('ONLY1');
+    my ( $item_bibnum, $item_bibitemnum, $itemnumber )
+    = AddItem( { homebranch => $branch_1, holdingbranch => $branch_1 }, $bibnum );
+    #Add a default rule to allow some holds
+    $dbh->do(
+        q{INSERT INTO issuingrules (categorycode, branchcode, itemtype, reservesallowed, holds_per_record)
+          VALUES (?, ?, ?, ?, ?)},
+        {},
+        '*', '*', '*', 25, 99
+    );
+    my $item = Koha::Items->find($itemnumber);
+    my $branch_to = $builder->build({ source => 'Branch' })->{ branchcode };
+    my $library = Koha::Libraries->find($branch_to);
+    my $patron = $builder->build({ source => 'Borrower' })->{ borrowernumber };
+
+    t::lib::Mocks::mock_preference('UseBranchTransferLimits', 1);
+    t::lib::Mocks::mock_preference('BranchTransferLimitsType', 'itemtype');
+    $library->pickup_location('1')->store;
+    is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
+       'OK', 'Library is a pickup location');
+    $library->pickup_location('0')->store;
+    is(CanItemBeReserved($patron, $item->itemnumber, $branch_to)->{status},
+       'libraryNotPickupLocation', 'Library is not a pickup location');
+    is(CanItemBeReserved($patron, $item->itemnumber, 'nonexistent')->{status},
+       'libraryNotFound', 'Cannot set unknown library as pickup location');
+};
 
 # Helper method to set up a Biblio.
 sub create_helper_biblio {