Bug 22330: Transfer limits should be respected for placing holds in staff interface...
[koha.git] / t / db_dependent / HoldsQueue.t
index f0141fb..1dce46c 100755 (executable)
@@ -8,20 +8,19 @@
 
 use Modern::Perl;
 
-use Test::More tests => 26;
+use Test::More tests => 48;
 use Data::Dumper;
 
-use C4::Branch;
 use C4::Calendar;
 use C4::Context;
 use C4::Members;
 use Koha::Database;
 use Koha::DateUtils;
-use Koha::ItemType;
+use Koha::Items;
+use Koha::Holds;
 
 use t::lib::TestBuilder;
-
-use Koha::ItemTypes;
+use t::lib::Mocks;
 
 BEGIN {
     use FindBin;
@@ -36,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',
 });
@@ -51,7 +53,6 @@ my $TITLE = "Test Holds Queue XXX";
 my $borrower = $builder->build({
     source => 'Borrower',
     value => {
-        categorycode => 'S',
         branchcode => $library1->{branchcode},
     }
 });
@@ -62,12 +63,12 @@ my $borrower_branchcode = $borrower->{branchcode};
 my @branchcodes = ( $library1->{branchcode}, $library2->{branchcode}, $library3->{branchcode} );
 my @other_branches = ( $library2->{branchcode}, $library3->{branchcode} );
 my $least_cost_branch_code = pop @other_branches;
-my $itemtype = Koha::ItemTypes->search({ notforloan => 1 })->next;
-$itemtype or BAIL_OUT("No adequate itemtype"); #FIXME Should be $itemtype = $itemtype->itemtype
+my $itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
 
 #Set up the stage
 # Sysprefs and cost matrix
-C4::Context->set_preference('HoldsQueueSkipClosed', 0);
+t::lib::Mocks::mock_preference('HoldsQueueSkipClosed', 0);
+t::lib::Mocks::mock_preference('LocalHoldsPriority', 0);
 $dbh->do("UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQueueWeight'", undef,
          join( ',', @other_branches, $borrower_branchcode, $least_cost_branch_code));
 $dbh->do("UPDATE systempreferences SET value = '0' WHERE variable = 'RandomizeHoldsQueueWeight'");
@@ -91,8 +92,8 @@ $dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated)
           VALUES             ('SER', 'Koha test', '$TITLE', '2011-02-01')");
 my $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
   or BAIL_OUT("Cannot find newly created biblio record");
-$dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype)
-          VALUES                  ($biblionumber, '', '$itemtype')");
+$dbh->do("INSERT INTO biblioitems (biblionumber, itemtype)
+          VALUES                  ($biblionumber, '$itemtype')");
 my $biblioitemnumber = $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
   or BAIL_OUT("Cannot find newly created biblioitems record");
 
@@ -123,7 +124,7 @@ my $test_sth = $dbh->prepare("SELECT * FROM hold_fill_targets
                               WHERE borrowernumber = $borrowernumber");
 
 # We have a book available homed in borrower branch, no point fiddling with AutomaticItemReturn
-C4::Context->set_preference('AutomaticItemReturn', 0);
+t::lib::Mocks::mock_preference('AutomaticItemReturn', 0);
 test_queue ('take from homebranch',  0, $borrower_branchcode, $borrower_branchcode);
 test_queue ('take from homebranch',  1, $borrower_branchcode, $borrower_branchcode);
 
@@ -132,14 +133,14 @@ $dbh->do("DELETE FROM hold_fill_targets");
 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode')");
 $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode' AND holdingbranch = '$borrower_branchcode'");
 # test_queue will flush
-C4::Context->set_preference('AutomaticItemReturn', 1);
+t::lib::Mocks::mock_preference('AutomaticItemReturn', 1);
 # Not sure how to make this test more difficult - holding branch does not matter
 
 $dbh->do("DELETE FROM tmp_holdsqueue");
 $dbh->do("DELETE FROM hold_fill_targets");
 $dbh->do("DELETE FROM issues WHERE itemnumber IN (SELECT itemnumber FROM items WHERE homebranch = '$borrower_branchcode')");
 $dbh->do("DELETE FROM items WHERE homebranch = '$borrower_branchcode'");
-C4::Context->set_preference('AutomaticItemReturn', 0);
+t::lib::Mocks::mock_preference('AutomaticItemReturn', 0);
 # We have a book available held in borrower branch
 test_queue ('take from holdingbranch', 0, $borrower_branchcode, $borrower_branchcode);
 test_queue ('take from holdingbranch', 1, $borrower_branchcode, $borrower_branchcode);
@@ -178,9 +179,9 @@ $dbh->do("DELETE FROM default_branch_circ_rules");
 $dbh->do("DELETE FROM default_branch_item_rules");
 $dbh->do("DELETE FROM default_circ_rules");
 
-C4::Context->set_preference('UseTransportCostMatrix', 0);
+t::lib::Mocks::mock_preference('UseTransportCostMatrix', 0);
 
-$itemtype = Koha::ItemTypes->search->next->itemtype;
+$itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
 
 $library1 = $builder->build({
     source => 'Branch',
@@ -196,21 +197,18 @@ $library3 = $builder->build({
 my $borrower1 = $builder->build({
     source => 'Borrower',
     value => {
-        categorycode => 'S',
         branchcode => $branchcodes[0],
     },
 });
 my $borrower2 = $builder->build({
     source => 'Borrower',
     value => {
-        categorycode => 'S',
         branchcode => $branchcodes[1],
     },
 });
 my $borrower3 = $builder->build({
     source => 'Borrower',
     value => {
-        categorycode => 'S',
         branchcode => $branchcodes[2],
     },
 });
@@ -234,11 +232,9 @@ $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE tit
 $dbh->do(qq{
     INSERT INTO biblioitems (
         biblionumber, 
-        marcxml, 
         itemtype
     ) VALUES (
         $biblionumber, 
-        '', 
         '$itemtype'
     )
 });
@@ -307,7 +303,7 @@ is( $holds_queue->[1]->{cardnumber}, $borrower2->{cardnumber}, "Holds queue fill
 # 1 of which is coming from MPL. Let's enable HoldsQueueSkipClosed
 # and make today a holiday for MPL. When we run it again we should only
 # have 1 row in the holds queue
-C4::Context->set_preference('HoldsQueueSkipClosed', 1);
+t::lib::Mocks::mock_preference('HoldsQueueSkipClosed', 1);
 my $today = dt_from_string();
 C4::Calendar->new( branchcode => $branchcodes[0] )->insert_single_holiday(
     day         => $today->day(),
@@ -322,7 +318,7 @@ is( Koha::Calendar->new( branchcode => $branchcodes[0] )->is_holiday( $today ),
 C4::HoldsQueue::CreateQueue();
 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
 is( scalar( @$holds_queue ), 1, "Holds not filled with items from closed libraries" );
-C4::Context->set_preference('HoldsQueueSkipClosed', 0);
+t::lib::Mocks::mock_preference('HoldsQueueSkipClosed', 0);
 
 $dbh->do("DELETE FROM default_circ_rules");
 $dbh->do("INSERT INTO default_circ_rules ( holdallowed ) VALUES ( 2 )");
@@ -335,14 +331,136 @@ is( @$holds_queue, 3, "Holds queue filling correct number for holds for default
 # one of which is coming from MPL. Let's enable HoldsQueueSkipClosed
 # and use our previously created holiday for MPL
 # When we run it again we should only have 2 rows in the holds queue
-C4::Context->set_preference( 'HoldsQueueSkipClosed', 1 );
+t::lib::Mocks::mock_preference( 'HoldsQueueSkipClosed', 1 );
 C4::HoldsQueue::CreateQueue();
 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
 is( scalar( @$holds_queue ), 2, "Holds not filled with items from closed libraries" );
-C4::Context->set_preference( 'HoldsQueueSkipClosed', 0 );
+t::lib::Mocks::mock_preference( 'HoldsQueueSkipClosed', 0 );
+
+## Test LocalHoldsPriority
+t::lib::Mocks::mock_preference('LocalHoldsPriority', 1);
+
+$dbh->do("DELETE FROM default_circ_rules");
+$dbh->do("INSERT INTO default_circ_rules ( holdallowed ) VALUES ( 2 )");
+$dbh->do("DELETE FROM issues");
+
+# Test homebranch = patron branch
+t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'HomeLibrary');
+t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'homebranch');
+C4::Context->clear_syspref_cache();
+$dbh->do("DELETE FROM reserves");
+$sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
+$sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
+$sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[0], 3 );
+
+$dbh->do("DELETE FROM items");
+# barcode, homebranch, holdingbranch, itemtype
+$items_insert_sth->execute( $barcode + 4, $branchcodes[2], $branchcodes[0] );
+
+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');
+C4::Context->clear_syspref_cache();
+$dbh->do("DELETE FROM reserves");
+$sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
+$sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
+$sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[0], 3 );
+
+$dbh->do("DELETE FROM items");
+# barcode, homebranch, holdingbranch, itemtype
+$items_insert_sth->execute( $barcode + 4, $branchcodes[0], $branchcodes[2] );
+
+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 holding library");
+
+# Test holdingbranch = pickup branch
+t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'PickupLibrary');
+t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'holdingbranch');
+C4::Context->clear_syspref_cache();
+$dbh->do("DELETE FROM reserves");
+$sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
+$sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
+$sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[2], 3 );
+
+$dbh->do("DELETE FROM items");
+# barcode, homebranch, holdingbranch, itemtype
+$items_insert_sth->execute( $barcode + 4, $branchcodes[0], $branchcodes[2] );
+
+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 holding library");
+
+# Test homebranch = pickup branch
+t::lib::Mocks::mock_preference('LocalHoldsPriorityPatronControl', 'PickupLibrary');
+t::lib::Mocks::mock_preference('LocalHoldsPriorityItemControl', 'homebranch');
+C4::Context->clear_syspref_cache();
+$dbh->do("DELETE FROM reserves");
+$sth->execute( $borrower1->{borrowernumber}, $biblionumber, $branchcodes[0], 1 );
+$sth->execute( $borrower2->{borrowernumber}, $biblionumber, $branchcodes[0], 2 );
+$sth->execute( $borrower3->{borrowernumber}, $biblionumber, $branchcodes[2], 3 );
+
+$dbh->do("DELETE FROM items");
+# barcode, homebranch, holdingbranch, itemtype
+$items_insert_sth->execute( $barcode + 4, $branchcodes[2], $branchcodes[0] );
+
+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 holding library");
+
+t::lib::Mocks::mock_preference('LocalHoldsPriority', 0);
+## End testing of LocalHoldsPriority
+
 
 # Bug 14297
-$itemtype = Koha::ItemTypes->search->next->itemtype;
+$itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
 $borrowernumber = $borrower3->{borrowernumber};
 my $library_A = $library1->{branchcode};
 my $library_B = $library2->{branchcode};
@@ -367,7 +485,7 @@ $dbh->do("
 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
   or BAIL_OUT("Cannot find newly created biblio record");
 
-$dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')");
+$dbh->do("INSERT INTO biblioitems (biblionumber, itemtype) VALUES ($biblionumber, '$itemtype')");
 
 $biblioitemnumber =
   $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
@@ -399,7 +517,7 @@ is( @$holds_queue, 1, "Bug 14297 - Holds Queue building ignoring holds where pic
 # End Bug 14297
 
 # Bug 15062
-$itemtype = Koha::ItemTypes->search->next->itemtype;
+$itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
 $borrowernumber = $borrower2->{borrowernumber};
 $library_A = $library1->{branchcode};
 $library_B = $library2->{branchcode};
@@ -416,7 +534,7 @@ $dbh->do("DELETE FROM default_branch_item_rules");
 $dbh->do("DELETE FROM default_circ_rules");
 $dbh->do("DELETE FROM branch_item_rules");
 
-C4::Context->set_preference("UseTransportCostMatrix",1);
+t::lib::Mocks::mock_preference("UseTransportCostMatrix",1);
 
 my $tc_rs = $schema->resultset('TransportCost');
 $tc_rs->create({ frombranch => $library_A, tobranch => $library_B, cost => 0, disable_transfer => 1 });
@@ -429,7 +547,7 @@ $dbh->do("
 $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
   or BAIL_OUT("Cannot find newly created biblio record");
 
-$dbh->do("INSERT INTO biblioitems (biblionumber, marcxml, itemtype) VALUES ($biblionumber, '', '$itemtype')");
+$dbh->do("INSERT INTO biblioitems (biblionumber, itemtype) VALUES ($biblionumber, '$itemtype')");
 
 $biblioitemnumber =
   $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
@@ -446,6 +564,223 @@ $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice
 is( @$holds_queue, 0, "Bug 15062 - Holds queue with Transport Cost Matrix will transfer item even if transfers disabled");
 # End Bug 15062
 
+# Test hold_fulfillment_policy
+t::lib::Mocks::mock_preference( "UseTransportCostMatrix", 0 );
+$borrowernumber = $borrower3->{borrowernumber};
+$library_A = $library1->{branchcode};
+$library_B = $library2->{branchcode};
+$library_C = $library3->{branchcode};
+$dbh->do("DELETE FROM reserves");
+$dbh->do("DELETE FROM issues");
+$dbh->do("DELETE FROM items");
+$dbh->do("DELETE FROM biblio");
+$dbh->do("DELETE FROM biblioitems");
+$dbh->do("DELETE FROM transport_cost");
+$dbh->do("DELETE FROM tmp_holdsqueue");
+$dbh->do("DELETE FROM hold_fill_targets");
+$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");
+
+$dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')");
+
+$biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
+  or BAIL_OUT("Cannot find newly created biblio record");
+
+$dbh->do("INSERT INTO biblioitems (biblionumber, itemtype) VALUES ($biblionumber, '$itemtype')");
+
+$biblioitemnumber =
+  $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
+  or BAIL_OUT("Cannot find newly created biblioitems record");
+
+$dbh->do("
+    INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
+    VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_B', 0, 0, 0, 0, NULL, '$itemtype')
+");
+
+# With hold_fulfillment_policy = homebranch, hold should only be picked up if pickup branch = homebranch
+$dbh->do("DELETE FROM default_circ_rules");
+$dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'homebranch' )");
+
+# Home branch matches pickup branch
+$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" );
+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" );
+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" );
+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");
+$dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'holdingbranch' )");
+
+# Home branch matches pickup branch
+$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" );
+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" );
+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" );
+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");
+$dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'any' )");
+
+# Home branch matches pickup branch
+$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" );
+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" );
+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" );
+Koha::Holds->find( $reserve_id )->cancel;
+
+# End testing hold_fulfillment_policy
+
+# Test hold itemtype limit
+t::lib::Mocks::mock_preference( "UseTransportCostMatrix", 0 );
+my $wrong_itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
+my $right_itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } })->{itemtype};
+$borrowernumber = $borrower3->{borrowernumber};
+my $branchcode = $library1->{branchcode};
+$dbh->do("DELETE FROM reserves");
+$dbh->do("DELETE FROM issues");
+$dbh->do("DELETE FROM items");
+$dbh->do("DELETE FROM biblio");
+$dbh->do("DELETE FROM biblioitems");
+$dbh->do("DELETE FROM transport_cost");
+$dbh->do("DELETE FROM tmp_holdsqueue");
+$dbh->do("DELETE FROM hold_fill_targets");
+$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");
+
+$dbh->do("INSERT INTO biblio (frameworkcode, author, title, datecreated) VALUES ('', 'Koha test', '$TITLE', '2011-02-01')");
+
+$biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE title = '$TITLE'")
+  or BAIL_OUT("Cannot find newly created biblio record");
+
+$dbh->do("INSERT INTO biblioitems (biblionumber, itemtype) VALUES ($biblionumber, '$itemtype')");
+
+$biblioitemnumber =
+  $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioitems WHERE biblionumber = $biblionumber")
+  or BAIL_OUT("Cannot find newly created biblioitems record");
+
+$dbh->do("
+    INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
+    VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_B', 0, 0, 0, 0, NULL, '$right_itemtype')
+");
+
+# With hold_fulfillment_policy = homebranch, hold should only be picked up if pickup branch = homebranch
+$dbh->do("DELETE FROM default_circ_rules");
+$dbh->do("INSERT INTO default_circ_rules ( holdallowed, hold_fulfillment_policy ) VALUES ( 2, 'any' )");
+
+# Home branch matches pickup branch
+$reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, $wrong_itemtype );
+C4::HoldsQueue::CreateQueue();
+$holds_queue = $dbh->selectall_arrayref( "SELECT * FROM tmp_holdsqueue", { Slice => {} } );
+is( @$holds_queue, 0, "Item with incorrect itemtype not targeted" );
+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" );
+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" );
+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;
 
@@ -470,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 {