Bug 17600: Standardize our EXPORT_OK
[srvgit] / t / db_dependent / HoldsQueue.t
index 8c8bb43..30f4ec3 100755 (executable)
@@ -8,12 +8,13 @@
 
 use Modern::Perl;
 
-use Test::More tests => 48;
+use Test::More tests => 57;
 use Data::Dumper;
 
-use C4::Calendar;
+use C4::Calendar qw( new insert_single_holiday );
 use C4::Context;
 use C4::Members;
+use C4::Circulation qw( AddIssue AddReturn );
 use Koha::Database;
 use Koha::DateUtils;
 use Koha::Items;
@@ -26,8 +27,8 @@ use t::lib::Mocks;
 BEGIN {
     use FindBin;
     use lib $FindBin::Bin;
-    use_ok('C4::Reserves');
-    use_ok('C4::HoldsQueue');
+    use_ok('C4::Reserves', qw( AddReserve ModReserve ModReserveAffect ));
+    use_ok('C4::HoldsQueue', qw( TransportCostMatrix GetHoldsQueueItems CreateQueue UpdateTransportCostMatrix GetPendingHoldRequestsForBib ));
 }
 
 my $schema = Koha::Database->schema;
@@ -114,7 +115,14 @@ $dbh->do("DELETE FROM reserves");
 my $bibitems = undef;
 my $priority = 1;
 # Make a reserve
-AddReserve ( $borrower_branchcode, $borrowernumber, $biblionumber, $bibitems,  $priority );
+AddReserve(
+    {
+        branchcode     => $borrower_branchcode,
+        borrowernumber => $borrowernumber,
+        biblionumber   => $biblionumber,
+        priority       => $priority,
+    }
+);
 #                           $resdate, $expdate, $notes, $title, $checkitem, $found
 $dbh->do("UPDATE reserves SET reservedate = DATE_SUB( reservedate, INTERVAL 1 DAY )");
 
@@ -194,35 +202,40 @@ $library3 = $builder->build({
 });
 @branchcodes = ( $library1->{branchcode}, $library2->{branchcode}, $library3->{branchcode} );
 
+my $category = $builder->build({ source => 'Category', value => {exclude_from_local_holds_priority => 0} } );
+
 my $borrower1 = $builder->build({
     source => 'Borrower',
     value => {
         branchcode => $branchcodes[0],
+        categorycode => $category->{categorycode},
     },
 });
 my $borrower2 = $builder->build({
     source => 'Borrower',
     value => {
         branchcode => $branchcodes[1],
+        categorycode => $category->{categorycode},
     },
 });
 my $borrower3 = $builder->build({
     source => 'Borrower',
     value => {
         branchcode => $branchcodes[2],
+        categorycode => $category->{categorycode},
     },
 });
 
 $dbh->do(qq{
     INSERT INTO biblio (
-        frameworkcode, 
-        author, 
-        title, 
+        frameworkcode,
+        author,
+        title,
         datecreated
     ) VALUES (
-        'SER', 
-        'Koha test', 
-        '$TITLE', 
+        'SER',
+        'Koha test',
+        '$TITLE',
         '2011-02-01'
     )
 });
@@ -231,10 +244,10 @@ $biblionumber = $dbh->selectrow_array("SELECT biblionumber FROM biblio WHERE tit
 
 $dbh->do(qq{
     INSERT INTO biblioitems (
-        biblionumber, 
+        biblionumber,
         itemtype
     ) VALUES (
-        $biblionumber, 
+        $biblionumber,
         '$itemtype'
     )
 });
@@ -243,7 +256,7 @@ $biblioitemnumber = $dbh->selectrow_array("SELECT biblioitemnumber FROM biblioit
 
 $items_insert_sth = $dbh->prepare(qq{
     INSERT INTO items (
-        biblionumber, 
+        biblionumber,
         biblioitemnumber,
         barcode,
         homebranch,
@@ -253,7 +266,8 @@ $items_insert_sth = $dbh->prepare(qq{
         itemlost,
         withdrawn,
         onloan,
-        itype
+        itype,
+        exclude_from_local_holds_priority
     ) VALUES (
         $biblionumber,
         $biblioitemnumber,
@@ -265,7 +279,8 @@ $items_insert_sth = $dbh->prepare(qq{
         0,
         0,
         NULL,
-        '$itemtype'
+        '$itemtype',
+        0
     )
 });
 # Create 3 items from 2 branches ( branches are for borrowers 1 and 2 respectively )
@@ -276,7 +291,7 @@ $items_insert_sth->execute( $barcode + 2, $branchcodes[1], $branchcodes[1] );
 
 $dbh->do("DELETE FROM reserves");
 my $sth = $dbh->prepare(q{
-    INSERT INTO reserves ( 
+    INSERT INTO reserves (
         borrowernumber,
         biblionumber,
         branchcode,
@@ -294,9 +309,8 @@ $dbh->do("DELETE FROM circulation_rules");
 Koha::CirculationRules->set_rule(
     {
         rule_name    => 'holdallowed',
-        rule_value   => 1,
+        rule_value   => 'from_home_library',
         branchcode   => undef,
-        categorycode => undef,
         itemtype     => undef,
     }
 );
@@ -332,9 +346,8 @@ $dbh->do("DELETE FROM circulation_rules");
 Koha::CirculationRules->set_rule(
     {
         rule_name    => 'holdallowed',
-        rule_value   => 2,
+        rule_value   => 'from_any_library',
         branchcode   => undef,
-        categorycode => undef,
         itemtype     => undef,
     }
 );
@@ -360,9 +373,8 @@ $dbh->do("DELETE FROM circulation_rules");
 Koha::CirculationRules->set_rule(
     {
         rule_name    => 'holdallowed',
-        rule_value   => 2,
+        rule_value   => 'from_any_library',
         branchcode   => undef,
-        categorycode => undef,
         itemtype     => undef,
     }
 );
@@ -512,22 +524,21 @@ $biblioitemnumber =
   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_A', 0, 0, 0, 0, NULL, '$itemtype')
+    INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype, exclude_from_local_holds_priority)
+    VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype', 0)
 ");
 
 $dbh->do("
-    INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype)
-    VALUES ($biblionumber, $biblioitemnumber, '$library_B', '$library_B', 0, 0, 0, 0, NULL, '$itemtype')
+    INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype, exclude_from_local_holds_priority)
+    VALUES ($biblionumber, $biblioitemnumber, '$library_B', '$library_B', 0, 0, 0, 0, NULL, '$itemtype', 0)
 ");
 
 Koha::CirculationRules->set_rules(
     {
         branchcode   => $library_A,
         itemtype     => $itemtype,
-        categorycode => undef,
         rules        => {
-            holdallowed  => 2,
+            holdallowed  => 'from_any_library',
             returnbranch => 'homebranch',
         }
     }
@@ -537,7 +548,14 @@ $dbh->do( "UPDATE systempreferences SET value = ? WHERE variable = 'StaticHoldsQ
     undef, join( ',', $library_B, $library_A, $library_C ) );
 $dbh->do( "UPDATE systempreferences SET value = 0 WHERE variable = 'RandomizeHoldsQueueWeight'" );
 
-my $reserve_id = AddReserve ( $library_C, $borrowernumber, $biblionumber, '', 1 );
+my $reserve_id = AddReserve(
+    {
+        branchcode     => $library_C,
+        borrowernumber => $borrowernumber,
+        biblionumber   => $biblionumber,
+        priority       => 1,
+    }
+);
 C4::HoldsQueue::CreateQueue();
 $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
 is( @$holds_queue, 1, "Bug 14297 - Holds Queue building ignoring holds where pickup & home branch don't match and item is not from le");
@@ -577,11 +595,19 @@ $biblioitemnumber =
   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_A', 0, 0, 0, 0, NULL, '$itemtype')
+    INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype, exclude_from_local_holds_priority)
+    VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_A', 0, 0, 0, 0, NULL, '$itemtype', 0)
 ");
 
-$reserve_id = AddReserve ( $library_B, $borrowernumber, $biblionumber, '', 1 );
+$reserve_id = AddReserve(
+    {
+        branchcode     => $library_B,
+        borrowernumber => $borrowernumber,
+        biblionumber   => $biblionumber,
+        priority       => 1,
+    }
+);
+
 C4::HoldsQueue::CreateQueue();
 $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");
@@ -614,8 +640,8 @@ $biblioitemnumber =
   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')
+    INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype, exclude_from_local_holds_priority)
+    VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_B', 0, 0, 0, 0, NULL, '$itemtype', 0)
 ");
 
 # With hold_fulfillment_policy = homebranch, hold should only be picked up if pickup branch = homebranch
@@ -624,30 +650,56 @@ Koha::CirculationRules->set_rules(
     {
         branchcode   => undef,
         itemtype     => undef,
-        categorycode => undef,
         rules        => {
-            holdallowed             => 2,
+            holdallowed             => 'from_any_library',
             hold_fulfillment_policy => 'homebranch',
         }
     }
 );
 
 # Home branch matches pickup branch
-$reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
+$reserve_id = AddReserve(
+    {
+        branchcode     => $library_A,
+        borrowernumber => $borrowernumber,
+        biblionumber   => $biblionumber,
+        priority       => 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" );
+my $target_rs = $schema->resultset('HoldFillTarget');
+is( $target_rs->next->reserve_id, $reserve_id, "Reserve id correctly set in hold fill target for title level hold" );
 Koha::Holds->find( $reserve_id )->cancel;
 
 # Holding branch matches pickup branch
-$reserve_id = AddReserve( $library_B, $borrowernumber, $biblionumber, '', 1 );
+$reserve_id = AddReserve(
+    {
+        branchcode     => $library_B,
+        borrowernumber => $borrowernumber,
+        biblionumber   => $biblionumber,
+        priority       => 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 );
+$reserve_id = AddReserve(
+    {
+        branchcode     => $library_C,
+        borrowernumber => $borrowernumber,
+        biblionumber   => $biblionumber,
+        priority       => 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" );
@@ -659,30 +711,53 @@ Koha::CirculationRules->set_rules(
     {
         branchcode   => undef,
         itemtype     => undef,
-        categorycode => undef,
         rules        => {
-            holdallowed             => 2,
+            holdallowed             => 'from_any_library',
             hold_fulfillment_policy => 'holdingbranch',
         }
     }
 );
 
 # Home branch matches pickup branch
-$reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
+$reserve_id = AddReserve(
+    {
+        branchcode     => $library_A,
+        borrowernumber => $borrowernumber,
+        biblionumber   => $biblionumber,
+        priority       => 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 );
+$reserve_id = AddReserve(
+    {
+        branchcode     => $library_B,
+        borrowernumber => $borrowernumber,
+        biblionumber   => $biblionumber,
+        priority       => 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 );
+$reserve_id = AddReserve(
+    {
+        branchcode     => $library_C,
+        borrowernumber => $borrowernumber,
+        biblionumber   => $biblionumber,
+        priority       => 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" );
@@ -694,30 +769,53 @@ Koha::CirculationRules->set_rules(
     {
         branchcode   => undef,
         itemtype     => undef,
-        categorycode => undef,
         rules        => {
-            holdallowed             => 2,
+            holdallowed             => 'from_any_library',
             hold_fulfillment_policy => 'any',
         }
     }
 );
 
 # Home branch matches pickup branch
-$reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1 );
+$reserve_id = AddReserve(
+    {
+        branchcode     => $library_A,
+        borrowernumber => $borrowernumber,
+        biblionumber   => $biblionumber,
+        priority       => 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 );
+$reserve_id = AddReserve(
+    {
+        branchcode     => $library_B,
+        borrowernumber => $borrowernumber,
+        biblionumber   => $biblionumber,
+        priority       => 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 );
+$reserve_id = AddReserve(
+    {
+        branchcode     => $library_C,
+        borrowernumber => $borrowernumber,
+        biblionumber   => $biblionumber,
+        priority       => 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" );
@@ -752,8 +850,8 @@ $biblioitemnumber =
   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')
+    INSERT INTO items (biblionumber, biblioitemnumber, homebranch, holdingbranch, notforloan, damaged, itemlost, withdrawn, onloan, itype, exclude_from_local_holds_priority)
+    VALUES ($biblionumber, $biblioitemnumber, '$library_A', '$library_B', 0, 0, 0, 0, NULL, '$right_itemtype', 0)
 ");
 
 # With hold_fulfillment_policy = homebranch, hold should only be picked up if pickup branch = homebranch
@@ -762,30 +860,55 @@ Koha::CirculationRules->set_rules(
     {
         branchcode   => undef,
         itemtype     => undef,
-        categorycode => undef,
         rules        => {
-            holdallowed             => 2,
+            holdallowed             => 'from_any_library',
             hold_fulfillment_policy => 'any',
         }
     }
 );
 
 # Home branch matches pickup branch
-$reserve_id = AddReserve( $library_A, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, $wrong_itemtype );
+$reserve_id = AddReserve(
+    {
+        branchcode     => $library_A,
+        borrowernumber => $borrowernumber,
+        biblionumber   => $biblionumber,
+        priority       => 1,
+        itemtype       => $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 );
+$reserve_id = AddReserve(
+    {
+        branchcode     => $library_A,
+        borrowernumber => $borrowernumber,
+        biblionumber   => $biblionumber,
+        priority       => 1,
+        itemtype       => $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 );
+$reserve_id = AddReserve(
+    {
+        branchcode     => $library_A,
+        borrowernumber => $borrowernumber,
+        biblionumber   => $biblionumber,
+        priority       => 1,
+    }
+);
+
 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" );
@@ -794,33 +917,437 @@ 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');
+subtest "Test Local Holds Priority - Bib level" => sub {
+    plan tests => 3;
+
+    Koha::Biblios->delete();
+    t::lib::Mocks::mock_preference( 'LocalHoldsPriority', 1 );
+    t::lib::Mocks::mock_preference( 'LocalHoldsPriorityPatronControl', 'PickupLibrary' );
+    t::lib::Mocks::mock_preference( 'LocalHoldsPriorityItemControl', 'homebranch' );
+    my $branch  = $builder->build_object( { class => 'Koha::Libraries' } );
+    my $branch2 = $builder->build_object( { class => 'Koha::Libraries' } );
+    my $category = $builder->build_object( { class => 'Koha::Patron::Categories', value => {exclude_from_local_holds_priority => 0} });
+    my $local_patron = $builder->build_object(
+        {
+            class => "Koha::Patrons",
+            value => {
+                branchcode => $branch->branchcode,
+                categorycode => $category->categorycode
+            }
+        }
+    );
+    my $other_patron = $builder->build_object(
+        {
+            class => "Koha::Patrons",
+            value => {
+                branchcode => $branch2->branchcode,
+                categorycode => $category->categorycode
+            }
+        }
+    );
+    my $biblio = $builder->build_sample_biblio();
+    my $item   = $builder->build_sample_item(
+        {
+            biblionumber  => $biblio->biblionumber,
+            library    => $branch->branchcode,
+            exclude_from_local_holds_priority => 0,
+        }
+    );
+
+    my $reserve_id = AddReserve(
+        {
+            branchcode     => $branch2->branchcode,
+            borrowernumber => $other_patron->borrowernumber,
+            biblionumber   => $biblio->biblionumber,
+            priority       => 1,
+        }
+    );
+    my $reserve_id2 = AddReserve(
+        {
+            branchcode     => $item->homebranch,
+            borrowernumber => $local_patron->borrowernumber,
+            biblionumber   => $biblio->biblionumber,
+            priority       => 2,
+        }
+    );
 
-$dbh->do("DELETE FROM tmp_holdsqueue");
-$dbh->do("DELETE FROM hold_fill_targets");
-$dbh->do("DELETE FROM reserves");
+    C4::HoldsQueue::CreateQueue();
+
+    my $queue_rs = $schema->resultset('TmpHoldsqueue');
+    my $target_rs = $schema->resultset('HoldFillTarget');
+    is( $queue_rs->count(), 1,
+        "Hold queue contains one hold" );
+    is(
+        $queue_rs->next->borrowernumber,
+        $local_patron->borrowernumber,
+        "We should pick the local hold over the next available"
+    );
+    is( $target_rs->next->reserve_id, $reserve_id2, "Reserve id correctly set in hold fill target" );
+};
+
+subtest "Test Local Holds Priority - Item level" => sub {
+    plan tests => 2;
+
+    Koha::Biblios->delete();
+    t::lib::Mocks::mock_preference( 'LocalHoldsPriority', 1 );
+    t::lib::Mocks::mock_preference( 'LocalHoldsPriorityPatronControl', 'PickupLibrary' );
+    t::lib::Mocks::mock_preference( 'LocalHoldsPriorityItemControl', 'homebranch' );
+    my $branch  = $builder->build_object( { class => 'Koha::Libraries' } );
+    my $branch2 = $builder->build_object( { class => 'Koha::Libraries' } );
+    my $category = $builder->build_object( { class => 'Koha::Patron::Categories', value => {exclude_from_local_holds_priority => 0} });
+    my $local_patron = $builder->build_object(
+        {
+            class => "Koha::Patrons",
+            value => {
+                branchcode => $branch->branchcode,
+                categorycode => $category->categorycode
+            }
+        }
+    );
+    my $other_patron = $builder->build_object(
+        {
+            class => "Koha::Patrons",
+            value => {
+                branchcode => $branch2->branchcode,
+                categorycode => $category->categorycode
+            }
+        }
+    );
+    my $biblio = $builder->build_sample_biblio();
+    my $item   = $builder->build_sample_item(
+        {
+            biblionumber  => $biblio->biblionumber,
+            library    => $branch->branchcode,
+            exclude_from_local_holds_priority => 0,
+        }
+    );
+
+    my $reserve_id = AddReserve(
+        {
+            branchcode     => $branch2->branchcode,
+            borrowernumber => $other_patron->borrowernumber,
+            biblionumber   => $biblio->biblionumber,
+            priority       => 1,
+            itemnumber     => $item->id,
+        }
+    );
+    my $reserve_id2 = AddReserve(
+        {
+            branchcode     => $item->homebranch,
+            borrowernumber => $local_patron->borrowernumber,
+            biblionumber   => $biblio->biblionumber,
+            priority       => 2,
+            itemnumber     => $item->id,
+        }
+    );
 
-$item = Koha::Items->find( { biblionumber => $biblionumber } );
-$item->holdingbranch( $item->homebranch );
-$item->store();
+    C4::HoldsQueue::CreateQueue();
 
-my $item2 = Koha::Item->new( $item->unblessed );
-$item2->itemnumber( undef );
-$item2->store();
+    my $queue_rs = $schema->resultset('TmpHoldsqueue');
+    my $q = $queue_rs->next;
+    is( $queue_rs->count(), 1,
+        "Hold queue contains one hold" );
+    is(
+        $q->borrowernumber,
+        $local_patron->borrowernumber,
+        "We should pick the local hold over the next available"
+    );
+};
 
-my $item3 = Koha::Item->new( $item->unblessed );
-$item3->itemnumber( undef );
-$item3->store();
+subtest "Test Local Holds Priority - Item level hold over Record level hold (Bug 23934)" => sub {
+    plan tests => 2;
+
+    Koha::Biblios->delete();
+    t::lib::Mocks::mock_preference( 'LocalHoldsPriority', 1 );
+    t::lib::Mocks::mock_preference( 'LocalHoldsPriorityPatronControl', 'PickupLibrary' );
+    t::lib::Mocks::mock_preference( 'LocalHoldsPriorityItemControl', 'homebranch' );
+    my $branch  = $builder->build_object( { class => 'Koha::Libraries' } );
+    my $branch2 = $builder->build_object( { class => 'Koha::Libraries' } );
+    my $category = $builder->build_object( { class => 'Koha::Patron::Categories', value => {exclude_from_local_holds_priority => 0} });
+    my $local_patron = $builder->build_object(
+        {
+            class => "Koha::Patrons",
+            value => {
+                branchcode => $branch->branchcode,
+                categorycode => $category->categorycode,
+            }
+        }
+    );
+    my $other_patron = $builder->build_object(
+        {
+            class => "Koha::Patrons",
+            value => {
+                branchcode => $branch2->branchcode,
+                categorycode => $category->categorycode,
+            }
+        }
+    );
+    my $biblio = $builder->build_sample_biblio();
+    my $item   = $builder->build_sample_item(
+        {
+            biblionumber  => $biblio->biblionumber,
+            library    => $branch->branchcode,
+            exclude_from_local_holds_priority => 0,
+        }
+    );
+
+    my $reserve_id = AddReserve(
+        {
+            branchcode     => $branch2->branchcode,
+            borrowernumber => $other_patron->borrowernumber,
+            biblionumber   => $biblio->biblionumber,
+            priority       => 1,
+        }
+    );
+    my $reserve_id2 = AddReserve(
+        {
+            branchcode     => $item->homebranch,
+            borrowernumber => $local_patron->borrowernumber,
+            biblionumber   => $biblio->biblionumber,
+            priority       => 2,
+            itemnumber     => $item->id,
+        }
+    );
 
-$reserve_id = AddReserve( $item->homebranch, $borrowernumber, $biblionumber, '', 1, undef, undef, undef, undef, undef, undef, undef );
+    C4::HoldsQueue::CreateQueue();
 
-C4::HoldsQueue::CreateQueue();
+    my $queue_rs = $schema->resultset('TmpHoldsqueue');
+    my $q = $queue_rs->next;
+    is( $queue_rs->count(), 1,
+        "Hold queue contains one hold" );
+    is(
+        $q->borrowernumber,
+        $local_patron->borrowernumber,
+        "We should pick the local hold over the next available"
+    );
+};
 
-my $queue_rs = $schema->resultset('TmpHoldsqueue');
-is( $queue_rs->count(), 1, "Hold queue contains one hold from chosen from three possible items" );
+subtest "Test Local Holds Priority - Get correct item for item level hold" => sub {
+    plan tests => 3;
+
+    Koha::Biblios->delete();
+    t::lib::Mocks::mock_preference( 'LocalHoldsPriority', 1 );
+    t::lib::Mocks::mock_preference( 'LocalHoldsPriorityPatronControl', 'PickupLibrary' );
+    t::lib::Mocks::mock_preference( 'LocalHoldsPriorityItemControl', 'homebranch' );
+    my $branch  = $builder->build_object( { class => 'Koha::Libraries' } );
+    my $branch2 = $builder->build_object( { class => 'Koha::Libraries' } );
+    my $category = $builder->build_object( { class => 'Koha::Patron::Categories', value => {exclude_from_local_holds_priority => 0} });
+    my $local_patron = $builder->build_object(
+        {
+            class => "Koha::Patrons",
+            value => {
+                branchcode => $branch->branchcode,
+                categorycode => $category->categorycode,
+            }
+        }
+    );
+    my $other_patron = $builder->build_object(
+        {
+            class => "Koha::Patrons",
+            value => {
+                branchcode => $branch2->branchcode,
+                categorycode => $category->categorycode,
+            }
+        }
+    );
+    my $biblio = $builder->build_sample_biblio();
+
+    my $item1 = $builder->build_sample_item(
+        {
+            biblionumber  => $biblio->biblionumber,
+            library    => $branch->branchcode,
+            exclude_from_local_holds_priority => 0,
+        }
+    );
+    my $item2 = $builder->build_sample_item(
+        {
+            biblionumber  => $biblio->biblionumber,
+            library    => $branch->branchcode,
+            exclude_from_local_holds_priority => 0,
+        }
+    );
+    my $item3 = $builder->build_sample_item(
+        {
+            biblionumber  => $biblio->biblionumber,
+            library    => $branch->branchcode,
+            exclude_from_local_holds_priority => 0,
+        }
+    );
+
+    my $reserve_id2 =
+        AddReserve(
+            {
+                branchcode     => $item2->homebranch,
+                borrowernumber => $local_patron->borrowernumber,
+                biblionumber   => $biblio->biblionumber,
+                priority       => 2,
+                itemnumber     => $item2->id,
+            }
+        );
+
+
+    C4::HoldsQueue::CreateQueue();
+
+    my $queue_rs = $schema->resultset('TmpHoldsqueue');
+    my $q = $queue_rs->next;
+    is( $queue_rs->count(), 1,
+        "Hold queue contains one hold" );
+    is(
+        $q->borrowernumber,
+        $local_patron->borrowernumber,
+        "We should pick the local hold over the next available"
+    );
+    is( $q->itemnumber->id, $item2->id, "Got the correct item for item level local holds priority" );
+};
+
+subtest "Test Local Holds Priority - Ensure no duplicate requests in holds queue (Bug 18001)" => sub {
+    plan tests => 1;
+
+    $dbh->do("DELETE FROM tmp_holdsqueue");
+    $dbh->do("DELETE FROM hold_fill_targets");
+    $dbh->do("DELETE FROM reserves");
+    $dbh->do("DELETE FROM circulation_rules");
+    Koha::Biblios->delete();
+
+    t::lib::Mocks::mock_preference( 'LocalHoldsPriority', 1 );
+    t::lib::Mocks::mock_preference( 'LocalHoldsPriorityPatronControl', 'PickupLibrary' );
+    t::lib::Mocks::mock_preference( 'LocalHoldsPriorityItemControl', 'homebranch' );
+    my $branch  = $builder->build_object( { class => 'Koha::Libraries' } );
+    my $branch2 = $builder->build_object( { class => 'Koha::Libraries' } );
+    my $category = $builder->build_object( { class => 'Koha::Patron::Categories', value => {exclude_from_local_holds_priority => 0} });
+    my $patron  = $builder->build_object(
+        {
+            class => "Koha::Patrons",
+            value => {
+                branchcode => $branch->branchcode,
+                categorycode => $category->categorycode,
+            }
+        }
+    );
+    my $biblio = $builder->build_sample_biblio();
+    my $item1  = $builder->build_sample_item(
+        {
+            biblionumber => $biblio->biblionumber,
+            library      => $branch->branchcode,
+            exclude_from_local_holds_priority => 0,
+        }
+    );
+    my $item2 = $builder->build_sample_item(
+        {
+            biblionumber => $biblio->biblionumber,
+            library      => $branch->branchcode,
+            exclude_from_local_holds_priority => 0,
+        }
+    );
+
+    my $item3 = $builder->build_sample_item(
+        {
+            biblionumber => $biblio->biblionumber,
+            library      => $branch->branchcode,
+            exclude_from_local_holds_priority => 0,
+        }
+    );
+
+    $reserve_id = AddReserve(
+        {
+            branchcode     => $item1->homebranch,
+            borrowernumber => $patron->borrowernumber,
+            biblionumber   => $biblio->id,
+            priority       => 1
+        }
+    );
+
+    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 "Item level holds info is preserved (Bug 25738)" => sub {
+
+    plan tests => 4;
+
+    $dbh->do("DELETE FROM tmp_holdsqueue");
+    $dbh->do("DELETE FROM hold_fill_targets");
+    $dbh->do("DELETE FROM reserves");
+    $dbh->do("DELETE FROM circulation_rules");
+
+    my $library  = $builder->build_object({ class => 'Koha::Libraries' });
+    my $category = $builder->build_object( { class => 'Koha::Patron::Categories', value => {exclude_from_local_holds_priority => 0} });
+    my $patron_1 = $builder->build_object(
+        {
+            class => "Koha::Patrons",
+            value => {
+                branchcode => $library->branchcode,
+                categorycode => $category->categorycode
+            }
+        }
+    );
+
+    my $patron_2 = $builder->build_object(
+        {
+            class => "Koha::Patrons",
+            value => {
+                branchcode => $library->branchcode,
+                categorycode => $category->categorycode
+            }
+        }
+    );
+
+    my $biblio = $builder->build_sample_biblio();
+    my $item_1 = $builder->build_sample_item(
+        {
+            biblionumber => $biblio->biblionumber,
+            library      => $library->branchcode,
+            exclude_from_local_holds_priority => 0,
+        }
+    );
+    my $item_2 = $builder->build_sample_item(
+        {
+            biblionumber => $biblio->biblionumber,
+            library      => $library->branchcode,
+            exclude_from_local_holds_priority => 0,
+        }
+    );
+
+    # Add item-level hold for patron_1
+    my $reserve_id_1 = AddReserve(
+        {
+            branchcode     => $library->branchcode,
+            borrowernumber => $patron_1->borrowernumber,
+            biblionumber   => $biblio->id,
+            itemnumber     => $item_1->itemnumber,
+            priority       => 1
+        }
+    );
+
+    my $reserve_id_2 = AddReserve(
+        {
+            branchcode     => $library->branchcode,
+            borrowernumber => $patron_2->borrowernumber,
+            biblionumber   => $biblio->id,
+            priority       => 2
+        }
+    );
+
+    C4::HoldsQueue::CreateQueue();
+
+    my $queue_rs = $schema->resultset('TmpHoldsqueue');
+
+    is( $queue_rs->count(), 2, "Hold queue contains two holds" );
+
+    my $queue_line_1 = $queue_rs->next;
+    is( $queue_line_1->item_level_request, 1, 'Request is correctly advertised as item-level' );
+    my $target_rs = $schema->resultset('HoldFillTarget')->search({borrowernumber=>$patron_1->borrowernumber});;
+    is( $target_rs->next->reserve_id, $reserve_id_1, "Reserve id correctly set in hold fill target for item level hold" );
+
+    my $queue_line_2 = $queue_rs->next;
+    is( $queue_line_2->item_level_request, 0, 'Request is correctly advertised as biblio-level' );
+
+};
 
 subtest 'Trivial test for UpdateTransportCostMatrix' => sub {
     plan tests => 1;
@@ -832,6 +1359,120 @@ subtest 'Trivial test for UpdateTransportCostMatrix' => sub {
     is( $schema->resultset('TransportCost')->count, 2, 'UpdateTransportCostMatrix added two records' );
 };
 
+subtest 'Excludes from local holds priority' => sub {
+    plan tests => 8;
+
+    Koha::Holds->delete;
+
+    t::lib::Mocks::mock_preference( 'LocalHoldsPriority', 1 );
+    t::lib::Mocks::mock_preference( 'LocalHoldsPriorityPatronControl', 'PickupLibrary' );
+    t::lib::Mocks::mock_preference( 'LocalHoldsPriorityItemControl', 'homebranch' );
+
+    my $branch  = $builder->build_object( { class => 'Koha::Libraries' } );
+    my $branch2 = $builder->build_object( { class => 'Koha::Libraries' } );
+    my $category = $builder->build_object( { class => 'Koha::Patron::Categories', value => {exclude_from_local_holds_priority => 0} });
+    my $excluded_category = $builder->build_object( { class => 'Koha::Patron::Categories', value => {exclude_from_local_holds_priority => 1} });
+    my $local_patron_excluded = $builder->build_object(
+        {
+            class => "Koha::Patrons",
+            value => {
+                branchcode => $branch->branchcode,
+                categorycode => $excluded_category->categorycode
+            }
+        }
+    );
+
+    my $local_patron_not_excluded = $builder->build_object(
+        {
+            class => "Koha::Patrons",
+            value => {
+                branchcode => $branch->branchcode,
+                categorycode => $category->categorycode
+            }
+        }
+    );
+
+    my $other_patron = $builder->build_object(
+        {
+            class => "Koha::Patrons",
+            value => {
+                branchcode => $branch2->branchcode,
+                categorycode => $category->categorycode
+            }
+        }
+    );
+
+    my $item1  = $builder->build_sample_item(
+        {
+            library      => $branch->branchcode,
+            exclude_from_local_holds_priority => 0,
+        }
+    );
+
+    AddReserve(
+        {
+            branchcode     => $other_patron->branchcode,
+            borrowernumber => $other_patron->borrowernumber,
+            biblionumber   => $item1->biblionumber,
+            priority       => 1,
+            itemtype       => $item1->effective_itemtype
+        }
+    );
+
+    AddReserve(
+        {
+            branchcode     => $local_patron_excluded->branchcode,
+            borrowernumber => $local_patron_excluded->borrowernumber,
+            biblionumber   => $item1->biblionumber,
+            priority       => 2,
+            itemtype       => $item1->effective_itemtype
+        }
+    );
+
+    AddReserve(
+        {
+            branchcode     => $local_patron_not_excluded->branchcode,
+            borrowernumber => $local_patron_not_excluded->borrowernumber,
+            biblionumber   => $item1->biblionumber,
+            priority       => 3,
+            itemtype       => $item1->effective_itemtype
+        }
+    );
+
+    C4::HoldsQueue::CreateQueue();
+
+    my $queue_rs = $schema->resultset('TmpHoldsqueue');
+    my $next = $queue_rs->next;
+    is($queue_rs->count, 1, 'Only 1 patron queueud' );
+    is($next->borrowernumber, $local_patron_not_excluded->borrowernumber, 'Not excluded local patron is queued');
+
+    my $item2  = $builder->build_sample_item(
+        {
+            biblionumber => $item1->biblionumber,
+            library      => $branch->branchcode,
+            exclude_from_local_holds_priority => 1,
+        }
+    );
+    C4::HoldsQueue::CreateQueue();
+
+    $queue_rs = $schema->resultset('TmpHoldsqueue');
+    is( $queue_rs->count, 2, '2 patrons queued' );
+    $next = $queue_rs->next;
+    is($next->borrowernumber, $local_patron_not_excluded->borrowernumber, 'Not excluded local patron is queued');
+    $next = $queue_rs->next;
+    is($next->borrowernumber, $other_patron->borrowernumber, 'Other patron is queued');
+
+    $item1->exclude_from_local_holds_priority(1)->store;
+
+    C4::HoldsQueue::CreateQueue();
+
+    $queue_rs = $schema->resultset('TmpHoldsqueue');
+    is( $queue_rs->count, 2, '2 patrons queued' );
+    $next = $queue_rs->next;
+    is($next->borrowernumber, $other_patron->borrowernumber, 'Other patron is queued');
+    $next = $queue_rs->next;
+    is($next->borrowernumber, $local_patron_excluded->borrowernumber, 'Excluded local patron is queued');
+};
 # Cleanup
 $schema->storage->txn_rollback;
 
@@ -882,7 +1523,217 @@ sub test_queue {
 
 }
 
+subtest "Test _checkHoldPolicy" => sub {
+
+    plan tests => 25;
+
+    $schema->storage->txn_begin;
+
+    my $library1  = $builder->build_object( { class => 'Koha::Libraries' } );
+    my $library2  = $builder->build_object( { class => 'Koha::Libraries' } );
+    my $library_nongroup = $builder->build_object( { class => 'Koha::Libraries' } );
+    my $category = $builder->build_object( { class => 'Koha::Patron::Categories', value => {exclude_from_local_holds_priority => 0} });
+    my $patron  = $builder->build_object(
+        {
+            class => "Koha::Patrons",
+            value => {
+                branchcode => $library1->branchcode,
+                categorycode => $category->categorycode,
+            }
+        }
+    );
+    my $biblio = $builder->build_sample_biblio();
+    my $item1  = $builder->build_sample_item(
+        {
+            biblionumber => $biblio->biblionumber,
+            library      => $library1->branchcode,
+            exclude_from_local_holds_priority => 0,
+        }
+    );
+
+    $reserve_id = AddReserve(
+        {
+            branchcode     => $item1->homebranch,
+            borrowernumber => $patron->borrowernumber,
+            biblionumber   => $biblio->id,
+            priority       => 1
+        }
+    );
+    ok( $reserve_id, "Hold was created");
+    my $requests = C4::HoldsQueue::GetPendingHoldRequestsForBib($biblio->biblionumber);
+    is( @$requests, 1, "Got correct number of holds");
+
+    my $request = $requests->[0];
+    is( $request->{biblionumber}, $biblio->id, "Hold has correct biblio");
+    is( $request->{borrowernumber}, $patron->id, "Hold has correct borrower");
+    is( $request->{borrowerbranch}, $patron->branchcode, "Hold has correct borrowerbranch");
+
+    my $hold = Koha::Holds->find( $reserve_id );
+    ok( $hold, "Found hold" );
+
+    my $item = {
+        holdallowed              => 'from_home_library',
+        homebranch               => $request->{borrowerbranch}, # library1
+        hold_fulfillment_policy  => 'any'
+    };
+
+    # Base case should work
+    is( C4::HoldsQueue::_checkHoldPolicy( $item, $request ), 1, "_checkHoldPolicy returns true" );
+
+    # Test holdallowed = 'not_allowed'
+    $item->{holdallowed} = 'not_allowed';
+    is( C4::HoldsQueue::_checkHoldPolicy( $item, $request ), 0, "_checkHoldPolicy returns false if holdallowed = not_allowed" );
+
+    # Test holdallowed = 'from_home_library'
+    $item->{holdallowed} = 'from_home_library';
+    $item->{homebranch} = $library_nongroup->id;
+    is( C4::HoldsQueue::_checkHoldPolicy( $item, $request ), 0, "_checkHoldPolicy returns false if holdallowed = from_home_library and branches do not match" );
+
+    $item->{homebranch} = $request->{borrowerbranch};
+    is( C4::HoldsQueue::_checkHoldPolicy( $item, $request ), 1, "_checkHoldPolicy returns true if holdallowed = from_home_library and branches do match" );
+
+    # Test holdallowed = 3
+    $item->{holdallowed} = 'from_local_hold_group';
+    $item->{homebranch} = $library_nongroup->id;
+    is( C4::HoldsQueue::_checkHoldPolicy( $item, $request ), 0, "_checkHoldPolicy returns false if branchode doesn't match, holdallowed = from_local_hold_group and no group branches exist" );
+    $item->{homebranch} = $request->{borrowerbranch};
+    is( C4::HoldsQueue::_checkHoldPolicy( $item, $request ), 1, "_checkHoldPolicy returns true if branchode matches, holdallowed = from_local_hold_group and no group branches exist" );
+
+    # Create library groups hierarchy
+    my $rootgroup = $builder->build_object( { class => 'Koha::Library::Groups', value => {ft_local_hold_group => 1} } );
+    my $group1 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library1->branchcode}} );
+    my $group2 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library2->branchcode}} );
+
+    is( C4::HoldsQueue::_checkHoldPolicy( $item, $request ), 1, "_checkHoldPolicy returns true if holdallowed = from_local_hold_group and no group branches exist" );
+
+    $group1->delete;
+
+    # Test hold_fulfillment_policy = holdgroup
+    $item->{hold_fulfillment_policy} = 'holdgroup';
+    $item->{homebranch} = $library_nongroup->id;
+    is( C4::HoldsQueue::_checkHoldPolicy( $item, $request ), 0, "_checkHoldPolicy returns true if library is not part of hold group, branches don't match and hfp = holdgroup" );
+    $item->{homebranch} = $request->{borrowerbranch};
+    is( C4::HoldsQueue::_checkHoldPolicy( $item, $request ), 1, "_checkHoldPolicy returns true if library is not part of hold group, branches match and hfp = holdgroup" );
+
+    $group1 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library1->branchcode}} );
+    is( C4::HoldsQueue::_checkHoldPolicy( $item, $request ), 1, "_checkHoldPolicy returns true if library is part of hold group with hfp = holdgroup" );
+
+    $item->{homebranch} = $library2->id;
+    is( C4::HoldsQueue::_checkHoldPolicy( $item, $request ), 1, "_checkHoldPolicy returns true if library is part of hold group with hfp = holdgroup" );
+    $item->{homebranch} = $library1->id;
+
+    $group1->delete;
+
+    # Test hold_fulfillment_policy = homebranch
+    $item->{hold_fulfillment_policy} = 'homebranch';
+    $item->{homebranch} = $library_nongroup->id;
+    is( C4::HoldsQueue::_checkHoldPolicy( $item, $request ), 0, "_checkHoldPolicy returns false if hfp = homebranch and pickup branch != item homebranch" );
+
+    $item->{homebranch} = $request->{borrowerbranch};
+    is( C4::HoldsQueue::_checkHoldPolicy( $item, $request ), 1, "_checkHoldPolicy returns true if hfp = homebranch and pickup branch = item homebranch" );
+
+    # Test hold_fulfillment_policy = holdingbranch
+    $item->{hold_fulfillment_policy} = 'holdingbranch';
+    $item->{holdingbranch} = $library_nongroup->id;
+    is( C4::HoldsQueue::_checkHoldPolicy( $item, $request ), 0, "_checkHoldPolicy returns false if hfp = holdingbranch and pickup branch != item holdingbranch" );
+
+    $item->{holdingbranch} = $request->{borrowerbranch};
+    is( C4::HoldsQueue::_checkHoldPolicy( $item, $request ), 1, "_checkHoldPolicy returns true if hfp = holdingbranch and pickup branch = item holdingbranch" );
+
+    # Test hold_fulfillment_policy = patrongroup
+    $item->{hold_fulfillment_policy} = 'patrongroup';
+    $item->{borrowerbranch} = $library1->id;
+
+    $item->{homebranch} = $library_nongroup->id;
+    is( C4::HoldsQueue::_checkHoldPolicy( $item, $request ), 0, "_checkHoldPolicy returns false if library is not part of hold group, branches don't match, hfp = patrongroup" );
+    $item->{homebranch} = $request->{borrowerbranch};
+    is( C4::HoldsQueue::_checkHoldPolicy( $item, $request ), 1, "_checkHoldPolicy returns false if library is not part of hold group, branches match, hfp = patrongroup" );
+
+    $group1 = $builder->build_object( { class => 'Koha::Library::Groups', value => {parent_id => $rootgroup->id, branchcode => $library1->branchcode}} );
+    is( C4::HoldsQueue::_checkHoldPolicy( $item, $request ), 1, "_checkHoldPolicy returns true if library is part of hold group with hfp = holdgroup" );
+
+    $item->{borrowerbranch} = $library2->id;
+    is( C4::HoldsQueue::_checkHoldPolicy( $item, $request ), 1, "_checkHoldPolicy returns true if library is part of hold group with hfp = holdgroup" );
+    $item->{borrowerbranch} = $library1->id;
+
+    $schema->storage->txn_rollback;
+};
+
 sub dump_records {
     my ($tablename) = @_;
     return $dbh->selectall_arrayref("SELECT * from $tablename where borrowernumber = ?", { Slice => {} }, $borrowernumber);
 }
+
+subtest 'Remove holds on check-in match' => sub {
+
+    plan tests => 2;
+
+    $schema->storage->txn_begin;
+
+    my $lib = $builder->build_object( { class => 'Koha::Libraries' } );
+
+    my $patron1 = $builder->build_object(
+        {
+            class => 'Koha::Patrons',
+            value => { branchcode => $lib->branchcode }
+        }
+    );
+    my $patron2 = $builder->build_object(
+        {
+            class => 'Koha::Patrons',
+            value => { branchcode => $lib->branchcode }
+        }
+    );
+
+    my $item = $builder->build_sample_item(
+        { homebranch => $lib->branchcode, holdingbranch => $lib->branchcode } );
+
+    t::lib::Mocks::mock_userenv( { branchcode => $lib->branchcode } );
+
+    AddIssue( $patron1->unblessed, $item->barcode, dt_from_string );
+
+    my $hold_id = AddReserve(
+        {
+            branchcode     => $item->homebranch,
+            borrowernumber => $patron2->borrowernumber,
+            biblionumber   => $item->biblionumber,
+            itemnumber     => undef,
+            priority       => 1
+        }
+    );
+
+    my $hold = Koha::Holds->find($hold_id);
+
+    AddReturn( $item->barcode, $item->homebranch );
+
+    C4::HoldsQueue::CreateQueue();
+
+    my $sth = $dbh->prepare(q{
+        SELECT  COUNT(*)
+        FROM    tmp_holdsqueue q
+        INNER JOIN hold_fill_targets t
+        ON  q.borrowernumber = t.borrowernumber
+            AND q.biblionumber = t.biblionumber
+            AND q.itemnumber = t.itemnumber
+            AND q.item_level_request = t.item_level_request
+            AND q.holdingbranch = t.source_branchcode
+        WHERE t.reserve_id = ?
+    });
+    $sth->execute($hold->reserve_id);
+    my ($count_1) = $sth->fetchrow_array;
+
+    is( $count_1, 1, "Holds queue has one element" );
+
+    AddReturn( $item->barcode, $item->homebranch, undef, dt_from_string );
+
+    ModReserveAffect( $item->itemnumber, $hold->borrowernumber, 0,
+        $hold->reserve_id );
+
+    $sth->execute($hold->reserve_id);
+    my ($count_2) = $sth->fetchrow_array;
+
+    is( $count_2, 0,
+        "Holds queue has no elements, even when queue was not rebuilt" );
+
+    $schema->storage->txn_rollback;
+};