From d69f64474809e30c3144ea342bcec83563741eab Mon Sep 17 00:00:00 2001 From: Alex Buckley Date: Thu, 9 Dec 2021 19:44:56 +0000 Subject: [PATCH] Bug 23172: Unit test for holds queue Test plan: 1. sudo koha-shell 2. prove t/db_dependent/HoldsQueue.t Sponsored-By: Brimbank Library, Australia --- t/db_dependent/HoldsQueue.t | 300 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 284 insertions(+), 16 deletions(-) diff --git a/t/db_dependent/HoldsQueue.t b/t/db_dependent/HoldsQueue.t index 7067931c35..cb8462e13a 100755 --- a/t/db_dependent/HoldsQueue.t +++ b/t/db_dependent/HoldsQueue.t @@ -8,7 +8,7 @@ use Modern::Perl; -use Test::More tests => 58; +use Test::More tests => 59; use Data::Dumper; use C4::Calendar qw( new insert_single_holiday ); @@ -43,12 +43,15 @@ t::lib::Mocks::mock_preference( 'BranchTransferLimitsType', 'itemtype' ); my $library1 = $builder->build({ source => 'Branch', + value => { pickup_location => 1 } }); my $library2 = $builder->build({ source => 'Branch', + value => { pickup_location => 1 } }); my $library3 = $builder->build({ source => 'Branch', + value => { pickup_location => 1 } }); my $TITLE = "Test Holds Queue XXX"; @@ -114,6 +117,21 @@ foreach ( $borrower_branchcode, $least_cost_branch_code, @other_branches ) { $dbh->do("DELETE FROM reserves"); my $bibitems = undef; my $priority = 1; + +# Set circulation rule +Koha::CirculationRules->set_rules( + { + categorycode => undef, + branchcode => undef, + itemtype => undef, + rules => { + reservesallowed => 25, + holds_per_record => 99, + } + } +); + + # Make a reserve AddReserve( { @@ -193,12 +211,15 @@ $itemtype = $builder->build({ source => 'Itemtype', value => { notforloan => 0 } $library1 = $builder->build({ source => 'Branch', + value => { pickup_location => 1 } }); $library2 = $builder->build({ source => 'Branch', + value => { pickup_location => 1 } }); $library3 = $builder->build({ source => 'Branch', + value => { pickup_location => 1 } }); @branchcodes = ( $library1->{branchcode}, $library2->{branchcode}, $library3->{branchcode} ); @@ -314,6 +335,18 @@ Koha::CirculationRules->set_rule( itemtype => undef, } ); +Koha::CirculationRules->set_rules( + { + branchcode => undef, + itemtype => undef, + categorycode => undef, + rules => { + reservesallowed => 25, + holds_per_record => 99 + + } + } +); C4::HoldsQueue::CreateQueue(); $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} }); is( @$holds_queue, 2, "Holds queue filling correct number for default holds policy 'from home library'" ); @@ -351,6 +384,18 @@ Koha::CirculationRules->set_rule( itemtype => undef, } ); +Koha::CirculationRules->set_rules( + { + categorycode => undef, + branchcode => undef, + itemtype => undef, + rules => { + reservesallowed => 25, + holds_per_record => 99, + } + } +); + C4::HoldsQueue::CreateQueue(); $holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} }); is( @$holds_queue, 3, "Holds queue filling correct number for holds for default holds policy 'from any library'" ); @@ -370,7 +415,7 @@ t::lib::Mocks::mock_preference( 'HoldsQueueSkipClosed', 0 ); t::lib::Mocks::mock_preference('LocalHoldsPriority', 1); $dbh->do("DELETE FROM circulation_rules"); -Koha::CirculationRules->set_rule( +Koha::CirculationRules->set_rules( { rule_name => 'holdallowed', rule_value => 'from_any_library', @@ -378,6 +423,18 @@ Koha::CirculationRules->set_rule( itemtype => undef, } ); +Koha::CirculationRules->set_rules( + { + categorycode => undef, + branchcode => undef, + itemtype => undef, + rules => { + reservesallowed => 25, + holds_per_record => 99, + } + } +); + $dbh->do("DELETE FROM issues"); # Test homebranch = patron branch @@ -656,6 +713,18 @@ Koha::CirculationRules->set_rules( } } ); +Koha::CirculationRules->set_rules( + { + branchcode => undef, + itemtype => undef, + categorycode => undef, + rules => { + reservesallowed => 25, + holds_per_record => 99 + + } + } +); # Home branch matches pickup branch $reserve_id = AddReserve( @@ -717,6 +786,17 @@ Koha::CirculationRules->set_rules( } } ); +Koha::CirculationRules->set_rules( + { + categorycode => undef, + branchcode => undef, + itemtype => undef, + rules => { + reservesallowed => 25, + holds_per_record => 99, + } + } +); # Home branch matches pickup branch $reserve_id = AddReserve( @@ -775,6 +855,17 @@ Koha::CirculationRules->set_rules( } } ); +Koha::CirculationRules->set_rules( + { + categorycode => undef, + branchcode => undef, + itemtype => undef, + rules => { + reservesallowed => 25, + holds_per_record => 99, + } + } +); # Home branch matches pickup branch $reserve_id = AddReserve( @@ -866,6 +957,17 @@ Koha::CirculationRules->set_rules( } } ); +Koha::CirculationRules->set_rules( + { + categorycode => undef, + branchcode => undef, + itemtype => undef, + rules => { + reservesallowed => 25, + holds_per_record => 99, + } + } +); # Home branch matches pickup branch $reserve_id = AddReserve( @@ -924,8 +1026,8 @@ subtest "Test Local Holds Priority - Bib level" => sub { 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 $branch = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } ); + my $branch2 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } ); my $category = $builder->build_object( { class => 'Koha::Patron::Categories', value => {exclude_from_local_holds_priority => 0} }); my $local_patron = $builder->build_object( { @@ -992,8 +1094,8 @@ subtest "Test Local Holds Priority - Item level" => sub { 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 $branch = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } ); + my $branch2 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } ); my $category = $builder->build_object( { class => 'Koha::Patron::Categories', value => {exclude_from_local_holds_priority => 0} }); my $local_patron = $builder->build_object( { @@ -1061,8 +1163,8 @@ subtest "Test Local Holds Priority - Item level hold over Record level hold (Bug 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 $branch = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } ); + my $branch2 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } ); my $category = $builder->build_object( { class => 'Koha::Patron::Categories', value => {exclude_from_local_holds_priority => 0} }); my $local_patron = $builder->build_object( { @@ -1129,8 +1231,8 @@ subtest "Test Local Holds Priority - Get correct item for item level hold" => su 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 $branch = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } ); + my $branch2 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } ); my $category = $builder->build_object( { class => 'Koha::Patron::Categories', value => {exclude_from_local_holds_priority => 0} }); my $local_patron = $builder->build_object( { @@ -1209,11 +1311,23 @@ subtest "Test Local Holds Priority - Ensure no duplicate requests in holds queue $dbh->do("DELETE FROM circulation_rules"); Koha::Biblios->delete(); + Koha::CirculationRules->set_rules( + { + categorycode => undef, + branchcode => undef, + itemtype => undef, + rules => { + reservesallowed => 25, + holds_per_record => 99, + } + } + ); + 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 $branch = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } ); + my $branch2 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } ); my $category = $builder->build_object( { class => 'Koha::Patron::Categories', value => {exclude_from_local_holds_priority => 0} }); my $patron = $builder->build_object( { @@ -1275,7 +1389,21 @@ subtest "Item level holds info is preserved (Bug 25738)" => sub { $dbh->do("DELETE FROM reserves"); $dbh->do("DELETE FROM circulation_rules"); - my $library = $builder->build_object({ class => 'Koha::Libraries' }); + Koha::CirculationRules->set_rules( + { + categorycode => undef, + branchcode => undef, + itemtype => undef, + rules => { + reservesallowed => 25, + holds_per_record => 99, + } + } + ); + + + + my $library = $builder->build_object({ class => 'Koha::Libraries', value => { pickup_location => 1 } }); my $category = $builder->build_object( { class => 'Koha::Patron::Categories', value => {exclude_from_local_holds_priority => 0} }); my $patron_1 = $builder->build_object( { @@ -1368,8 +1496,8 @@ subtest 'Excludes from local holds priority' => sub { 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 $branch = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } ); + my $branch2 = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } ); 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( @@ -1659,6 +1787,134 @@ subtest "Test _checkHoldPolicy" => sub { $schema->storage->txn_rollback; }; +subtest "Test _checkCirculationRules" => sub { + + plan tests => 7; + + $schema->storage->txn_begin; + + # Create objects + my $library = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } ); + my $category_1 = $builder->build_object( { class => 'Koha::Patron::Categories' }); + my $category_2 = $builder->build_object( { class => 'Koha::Patron::Categories' }); + my $itype_1 = $builder->build_object( { class => 'Koha::ItemTypes' }); + my $itype_2 = $builder->build_object( { class => 'Koha::ItemTypes' }); + my $patron_1 = $builder->build_object( + { + class => "Koha::Patrons", + value => { + branchcode => $library->branchcode, + categorycode => $category_1->categorycode, + } + } + ); + my $patron_2 = $builder->build_object( + { + class => "Koha::Patrons", + value => { + branchcode => $library->branchcode, + categorycode => $category_2->categorycode, + } + } + ); + my $biblio = $builder->build_sample_biblio(); + my $item_1 = $builder->build_sample_item( + { + biblionumber => $biblio->biblionumber, + library => $library->branchcode, + itype => $itype_1->itemtype + } + ); + my $item_2 = $builder->build_sample_item( + { + biblionumber => $biblio->biblionumber, + library => $library->branchcode, + itype => $itype_2->itemtype + } + ); + + # Add reserves + # Title-level reserve for patron_1 + my $reserve_id_1 = AddReserve( + { + branchcode => $item_1->homebranch, + borrowernumber => $patron_1->borrowernumber, + biblionumber => $biblio->id, + priority => 1 + } + ); + # Title-level reserve for patron_2 + my $reserve_id_2 = AddReserve( + { + branchcode => $item_2->homebranch, + borrowernumber => $patron_2->borrowernumber, + biblionumber => $biblio->id, + priority => 2 + } + ); + + # Set 3 circulation rules + # The category for patron_1 cannot reserve item type 1 + Koha::CirculationRules->set_rules( + { + categorycode => $category_1->categorycode, + branchcode => undef, + itemtype => $itype_1->itemtype, + rules => { + reservesallowed => 0, + holds_per_record => 0, + } + } + ); + # The category for patron_2 can reserve item type 1 + Koha::CirculationRules->set_rules( + { + categorycode => $category_2->categorycode, + branchcode => undef, + itemtype => $itype_1->itemtype, + rules => { + reservesallowed => 25, + holds_per_record => 99, + } + } + ); + # All patron categories can reserve item type 2 + Koha::CirculationRules->set_rules( + { + categorycode => undef, + branchcode => undef, + itemtype => $itype_2->itemtype, + rules => { + reservesallowed => 25, + holds_per_record => 99, + } + } + ); + + # Get pending holds + my $requests = C4::HoldsQueue::GetPendingHoldRequestsForBib($biblio->biblionumber); + is( @$requests, 2, "Got correct number of holds"); + is( $requests->[0]->{borrowernumber}, $patron_1->id, "First hold has correct borrower"); + is( $requests->[1]->{borrowernumber}, $patron_2->id, "Second hold has correct borrower"); + + # Loop through the available items + # Check the circulation rules for each pending title-level reserve against the available + # item type. To determine if the reserving patron category is allowed to reserve the item + my $available_items = C4::HoldsQueue::GetItemsAvailableToFillHoldRequestsForBib($biblio->id); + foreach my $item ( @$available_items ) { + if ( $item->{itemnumber} == $item_1->itemnumber ) { + # Check item_1 against both title-level reserves + is( C4::HoldsQueue::_checkCirculationRules( $item, $requests->[0] ), 0, "_checkCirculationRule returns false - item_1 should not be mapped to patron_1 title-level reserve" ); + is( C4::HoldsQueue::_checkCirculationRules( $item, $requests->[1] ), 1, "_checkCirculationRule returns true - item_1 should be mapped to patron_2 title-level reserve" ); + } elsif ( $item->{itemnumber} == $item_2->itemnumber ) { + # Check item_2 against both title-level reserves + is( C4::HoldsQueue::_checkCirculationRules( $item, $requests->[0] ), 1, "_checkCirculationRule returns true - item_2 should be mapped to patron_1 title level reserve" ); + is( C4::HoldsQueue::_checkCirculationRules( $item, $requests->[1] ), 1, "_checkCirculationRule returns true - item_2 should be mapped to patron_2 title-level reserve" ); + } + } + $schema->storage->txn_rollback; +}; + sub dump_records { my ($tablename) = @_; return $dbh->selectall_arrayref("SELECT * from $tablename where borrowernumber = ?", { Slice => {} }, $borrowernumber); @@ -1670,7 +1926,7 @@ subtest 'Remove holds on check-in match' => sub { $schema->storage->txn_begin; - my $lib = $builder->build_object( { class => 'Koha::Libraries' } ); + my $lib = $builder->build_object( { class => 'Koha::Libraries', value => { pickup_location => 1 } } ); my $patron1 = $builder->build_object( { @@ -1685,6 +1941,18 @@ subtest 'Remove holds on check-in match' => sub { } ); + Koha::CirculationRules->set_rules( + { + categorycode => undef, + branchcode => undef, + itemtype => undef, + rules => { + reservesallowed => 25, + holds_per_record => 99, + } + } + ); + my $item = $builder->build_sample_item( { homebranch => $lib->branchcode, holdingbranch => $lib->branchcode } ); -- 2.11.0