Bug 22330: Transfer limits should be respected for placing holds in staff interface...
[koha.git] / t / db_dependent / HoldsQueue.t
index c71a516..1dce46c 100755 (executable)
@@ -8,7 +8,7 @@
 
 use Modern::Perl;
 
-use Test::More tests => 42;
+use Test::More tests => 48;
 use Data::Dumper;
 
 use C4::Calendar;
@@ -16,6 +16,8 @@ use C4::Context;
 use C4::Members;
 use Koha::Database;
 use Koha::DateUtils;
+use Koha::Items;
+use Koha::Holds;
 
 use t::lib::TestBuilder;
 use t::lib::Mocks;
@@ -33,6 +35,9 @@ my $dbh = C4::Context->dbh;
 
 my $builder = t::lib::TestBuilder->new;
 
+t::lib::Mocks::mock_preference( 'UseBranchTransferLimits',  '0' );
+t::lib::Mocks::mock_preference( 'BranchTransferLimitsType', 'itemtype' );
+
 my $library1 = $builder->build({
     source => 'Branch',
 });
@@ -356,6 +361,49 @@ C4::HoldsQueue::CreateQueue();
 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
 is( $holds_queue->[0]->{cardnumber}, $borrower3->{cardnumber}, "Holds queue giving priority to patron who's home library matches item's home library");
 
+### Test branch transfer limits ###
+t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'HomeLibrary');
+t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'holdingbranch');
+t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '1' );
+C4::Context->clear_syspref_cache();
+$dbh->do("DELETE FROM reserves");
+$sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
+$sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[1], 2 );
+
+$dbh->do("DELETE FROM items");
+# barcode, homebranch, holdingbranch, itemtype
+$items_insert_sth->execute( $barcode, $branchcodes[2], $branchcodes[2] );
+my $item = Koha::Items->find( { barcode => $barcode } );
+
+my $limit1 = Koha::Item::Transfer::Limit->new(
+    {
+        toBranch   => $branchcodes[0],
+        fromBranch => $branchcodes[2],
+        itemtype   => $item->effective_itemtype,
+    }
+)->store();
+
+C4::HoldsQueue::CreateQueue();
+$holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
+is( $holds_queue->[0]->{cardnumber}, $borrower2->{cardnumber}, "Holds queue skips hold transfer that would violate branch transfer limits");
+
+my $limit2 = Koha::Item::Transfer::Limit->new(
+    {
+        toBranch   => $branchcodes[1],
+        fromBranch => $branchcodes[2],
+        itemtype   => $item->effective_itemtype,
+    }
+)->store();
+
+C4::HoldsQueue::CreateQueue();
+$holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
+is( $holds_queue->[0]->{cardnumber}, undef, "Holds queue doesn't fill hold where all available items would violate branch transfer limits");
+
+$limit1->delete();
+$limit2->delete();
+t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '0' );
+### END Test branch transfer limits ###
+
 # Test holdingbranch = patron branch
 t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'HomeLibrary');
 t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'holdingbranch');
@@ -560,21 +608,21 @@ $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
 C4::HoldsQueue::CreateQueue();
 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
 is( @$holds_queue, 1, "Hold where pickup branch matches home branch targeted" );
-CancelReserve( { reserve_id => $reserve_id } );
+Koha::Holds->find( $reserve_id )->cancel;
 
 # Holding branch matches pickup branch
 $reserve_id = AddReserve( $library_B, $borrowernumber, $biblionumber, '', 1 );
 C4::HoldsQueue::CreateQueue();
 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
 is( @$holds_queue, 0, "Hold where pickup ne home, pickup eq home not targeted" );
-CancelReserve( { reserve_id => $reserve_id } );
+Koha::Holds->find( $reserve_id )->cancel;
 
 # Neither branch matches pickup branch
 $reserve_id = AddReserve( $library_C, $borrowernumber, $biblionumber, '', 1 );
 C4::HoldsQueue::CreateQueue();
 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
 is( @$holds_queue, 0, "Hold where pickup ne home, pickup ne holding not targeted" );
-CancelReserve( { reserve_id => $reserve_id } );
+Koha::Holds->find( $reserve_id )->cancel;
 
 # With hold_fulfillment_policy = holdingbranch, hold should only be picked up if pickup branch = holdingbranch
 $dbh->do("DELETE FROM default_circ_rules");
@@ -585,21 +633,21 @@ $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
 C4::HoldsQueue::CreateQueue();
 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
 is( @$holds_queue, 0, "Hold where pickup eq home, pickup ne holding not targeted" );
-CancelReserve( { reserve_id => $reserve_id } );
+Koha::Holds->find( $reserve_id )->cancel;
 
 # Holding branch matches pickup branch
 $reserve_id = AddReserve( $library_B, $borrowernumber, $biblionumber, '', 1 );
 C4::HoldsQueue::CreateQueue();
 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
 is( @$holds_queue, 1, "Hold where pickup ne home, pickup eq holding targeted" );
-CancelReserve( { reserve_id => $reserve_id } );
+Koha::Holds->find( $reserve_id )->cancel;
 
 # Neither branch matches pickup branch
 $reserve_id = AddReserve( $library_C, $borrowernumber, $biblionumber, '', 1 );
 C4::HoldsQueue::CreateQueue();
 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
 is( @$holds_queue, 0, "Hold where pickup ne home, pickup ne holding not targeted" );
-CancelReserve( { reserve_id => $reserve_id } );
+Koha::Holds->find( $reserve_id )->cancel;
 
 # With hold_fulfillment_policy = any, hold should be pikcup up reguardless of matching home or holding branch
 $dbh->do("DELETE FROM default_circ_rules");
@@ -610,21 +658,21 @@ $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
 C4::HoldsQueue::CreateQueue();
 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
 is( @$holds_queue, 1, "Hold where pickup eq home, pickup ne holding targeted" );
-CancelReserve( { reserve_id => $reserve_id } );
+Koha::Holds->find( $reserve_id )->cancel;
 
 # Holding branch matches pickup branch
 $reserve_id = AddReserve( $library_B, $borrowernumber, $biblionumber, '', 1 );
 C4::HoldsQueue::CreateQueue();
 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
 is( @$holds_queue, 1, "Hold where pickup ne home, pickup eq holding targeted" );
-CancelReserve( { reserve_id => $reserve_id } );
+Koha::Holds->find( $reserve_id )->cancel;
 
 # Neither branch matches pickup branch
 $reserve_id = AddReserve( $library_C, $borrowernumber, $biblionumber, '', 1 );
 C4::HoldsQueue::CreateQueue();
 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
 is( @$holds_queue, 1, "Hold where pickup ne home, pickup ne holding targeted" );
-CancelReserve( { reserve_id => $reserve_id } );
+Koha::Holds->find( $reserve_id )->cancel;
 
 # End testing hold_fulfillment_policy
 
@@ -672,24 +720,67 @@ $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1, und
 C4::HoldsQueue::CreateQueue();
 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
 is( @$holds_queue, 0, "Item with incorrect itemtype not targeted" );
-CancelReserve( { reserve_id => $reserve_id } );
+Koha::Holds->find( $reserve_id )->cancel;
 
 # Holding branch matches pickup branch
 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, $right_itemtype );
 C4::HoldsQueue::CreateQueue();
 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
 is( @$holds_queue, 1, "Item with matching itemtype is targeted" );
-CancelReserve( { reserve_id => $reserve_id } );
+Koha::Holds->find( $reserve_id )->cancel;
 
 # Neither branch matches pickup branch
 $reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, undef );
 C4::HoldsQueue::CreateQueue();
 $holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
 is( @$holds_queue, 1, "Item targeted when hold itemtype is not set" );
-CancelReserve( { reserve_id => $reserve_id } );
+Koha::Holds->find( $reserve_id )->cancel;
 
 # End testing hold itemtype limit
 
+
+# Test Local Holds Priority - Bug 18001
+t::lib::Mocks::mock_preference('LocalHoldsPriority', 1);
+t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'PickupLibrary');
+t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'homebranch');
+
+$dbh->do("DELETE FROM tmp_holdsqueue");
+$dbh->do("DELETE FROM hold_fill_targets");
+$dbh->do("DELETE FROM reserves");
+$dbh->do("DELETE FROM default_branch_circ_rules");
+$dbh->do("DELETE FROM default_branch_item_rules");
+$dbh->do("DELETE FROM default_circ_rules");
+$dbh->do("DELETE FROM branch_item_rules");
+
+$item = Koha::Items->find( { biblionumber => $biblionumber } );
+$item->holdingbranch( $item->homebranch );
+$item->store();
+
+my $item2 = Koha::Item->new( $item->unblessed );
+$item2->itemnumber( undef );
+$item2->store();
+
+my $item3 = Koha::Item->new( $item->unblessed );
+$item3->itemnumber( undef );
+$item3->store();
+
+$reserve_id = AddReserve( $item->homebranch, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, undef );
+
+C4::HoldsQueue::CreateQueue();
+
+my $queue_rs = $schema->resultset('TmpHoldsqueue');
+is( $queue_rs->count(), 1, "Hold queue contains one hold from chosen from three possible items" );
+
+subtest 'Trivial test for UpdateTransportCostMatrix' => sub {
+    plan tests => 1;
+    my $recs = [
+        { frombranch => $library1->{branchcode}, tobranch => $library2->{branchcode}, cost => 1, disable_transfer => 0 },
+        { frombranch => $library2->{branchcode}, tobranch => $library3->{branchcode}, cost => 0, disable_transfer => 1 },
+    ];
+    C4::HoldsQueue::UpdateTransportCostMatrix( $recs );
+    is( $schema->resultset('TransportCost')->count, 2, 'UpdateTransportCostMatrix added two records' );
+};
+
 # Cleanup
 $schema->storage->txn_rollback;
 
@@ -714,6 +805,30 @@ sub test_queue {
     diag( "Wrong pick-up/hold for first target (pick_branch, hold_branch, reserves, hold_fill_targets, tmp_holdsqueue): "
         . Dumper ($pick_branch, $hold_branch, map dump_records($_), qw(reserves hold_fill_targets tmp_holdsqueue)) )
       unless $ok;
+
+    # Test enforcement of branch transfer limit
+    if ( $r->{pickbranch} ne $r->{holdingbranch} ) {
+        t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '1' );
+        my $limit = Koha::Item::Transfer::Limit->new(
+            {
+                toBranch   => $r->{pickbranch},
+                fromBranch => $r->{holdingbranch},
+                itemtype   => $r->{itype},
+            }
+        )->store();
+        C4::Context->clear_syspref_cache();
+        C4::HoldsQueue::CreateQueue();
+        $results = $dbh->selectall_arrayref( $test_sth, { Slice => {} } )
+          ;    # should be only one
+        my $s = $results->[0];
+        isnt( $r->{holdingbranch}, $s->{holdingbranch}, 'Hold is not trapped for pickup at a branch that cannot be transferred to');
+
+        $limit->delete();
+        t::lib::Mocks::mock_preference( 'UseBranchTransferLimits', '0' );
+        C4::Context->clear_syspref_cache();
+        C4::HoldsQueue::CreateQueue();
+    }
+
 }
 
 sub dump_records {