Bug 19469: Add ability to split view of holds view on record by pickup library and...
[koha.git] / t / db_dependent / Holds.t
index 93fe745..4c1be77 100755 (executable)
@@ -7,21 +7,24 @@ use t::lib::TestBuilder;
 
 use C4::Context;
 
-use Test::More tests => 55;
+use Test::More tests => 56;
 use MARC::Record;
-use C4::Biblio;
+use Koha::Patrons;
 use C4::Items;
+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::Patrons;
+use Koha::Items;
+use Koha::Libraries;
 
 BEGIN {
     use FindBin;
     use lib $FindBin::Bin;
-    use_ok('C4::Reserves');
 }
 
 my $schema  = Koha::Database->new->schema;
@@ -209,19 +212,19 @@ is( $holds->next->itemnumber, $itemnumber, "Test ModReserveMinusPriority()" );
 
 $holds = $biblio->holds;
 $hold = $holds->next;
-AlterPriority( 'top', $hold->reserve_id );
+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', $hold->reserve_id );
+AlterPriority( 'down', $hold->reserve_id, undef, 2, 1, 6 );
 $hold = Koha::Holds->find( $reserveid );
 is( $hold->priority, '2', "Test AlterPriority(), move down" );
 
-AlterPriority( 'up', $hold->reserve_id );
+AlterPriority( 'up', $hold->reserve_id, 1, 3, 1, 6 );
 $hold = Koha::Holds->find( $reserveid );
 is( $hold->priority, '1', "Test AlterPriority(), move up" );
 
-AlterPriority( 'bottom', $hold->reserve_id );
+AlterPriority( 'bottom', $hold->reserve_id, undef, 2, 1, 6 );
 $hold = Koha::Holds->find( $reserveid );
 is( $hold->priority, '6', "Test AlterPriority(), move to bottom" );
 
@@ -260,7 +263,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)'
 );
 
@@ -268,14 +271,14 @@ 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)'
 );
 
@@ -298,7 +301,7 @@ ok(
 
 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(
@@ -308,13 +311,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
@@ -328,19 +331,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)"
 );
 
@@ -369,18 +372,18 @@ $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',
+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 return 'cannotReserveFromOtherBranches'");
 
 ($item_bibnum, $item_bibitemnum, $itemnumber) = AddItem(
     { homebranch => $branch_1, holdingbranch => $branch_1, itype => 'CAN' } , $bibnum);
-is(CanItemBeReserved($borrowernumbers[0], $itemnumber), 'OK',
+is(CanItemBeReserved($borrowernumbers[0], $itemnumber)->{status}, 'OK',
     "CanItemBeReserved should return 'OK'");
 
 # Bug 12632
@@ -402,14 +405,111 @@ $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');
+
+    ( $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 = $schema->resultset('DefaultBorrowerCircRule')->new(
+        {
+            categorycode => $category->{categorycode},
+            max_holds    => 3,
+        }
+    )->insert();
+
+    my $rule_branch = $schema->resultset('BranchBorrowerCircRule')->new(
+        {
+            branchcode   => $branch_1,
+            categorycode => $category->{categorycode},
+            max_holds    => 5,
+        }
+    )->insert();
+
+    $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->max_holds(3);
+    $rule_branch->insert();
+
+    $ret = CanItemBeReserved( $borrowernumbers[0], $itemnumber );
+    is( $ret->{status}, 'tooManyReserves', 'Patron cannot place hold with only a branch/category rule of 3' );
+
+    $rule_branch->max_holds(5);
+    $rule_branch->update();
+    $rule_all->max_holds(5);
+    $rule_all->insert();
+
+    $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 {