From 1d96c942092599df9b543747b56094c25885dec6 Mon Sep 17 00:00:00 2001 From: Arthur Suzuki Date: Thu, 11 Apr 2024 14:03:16 +0200 Subject: [PATCH] Bug 25408: Added unit tests for opacitemholds policy Signed-off-by: Pedro Amorim --- t/db_dependent/Reserves.t | 162 +++++++++++++++++++++++++++++++++----- 1 file changed, 144 insertions(+), 18 deletions(-) diff --git a/t/db_dependent/Reserves.t b/t/db_dependent/Reserves.t index 83b84bbc2d0..59f2ec2e3e2 100755 --- a/t/db_dependent/Reserves.t +++ b/t/db_dependent/Reserves.t @@ -978,22 +978,18 @@ subtest 'ChargeReserveFee tests' => sub { }; subtest 'reserves.item_level_hold' => sub { - plan tests => 2; + plan tests => 4; my $item = $builder->build_sample_item; - my $patron = $builder->build_object( - { - class => 'Koha::Patrons', - value => { branchcode => $item->homebranch } - } - ); + my $patron = $builder->build_object( { class => "Koha::Patrons" } ); + $patron->branchcode( $item->homebranch ); subtest 'item level hold' => sub { plan tests => 5; my $reserve_id = AddReserve( { branchcode => $item->homebranch, - borrowernumber => $patron->borrowernumber, + borrowernumber => $patron->id, biblionumber => $item->biblionumber, priority => 1, itemnumber => $item->itemnumber, @@ -1004,17 +1000,20 @@ subtest 'reserves.item_level_hold' => sub { is( $hold->item_level_hold, 1, 'item_level_hold should be set when AddReserve is called with a specific item' ); # Mark it waiting - ModReserveAffect( $item->itemnumber, $patron->borrowernumber, 1 ); + ModReserveAffect( $item->itemnumber, $patron->id, 1 ); my $mock = Test::MockModule->new('Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue'); - $mock->mock( 'enqueue', sub { - my ( $self, $args ) = @_; - is_deeply( - $args->{biblio_ids}, - [ $hold->biblionumber ], - "AlterPriority triggers a holds queue update for the related biblio" - ); - } ); + $mock->mock( + 'enqueue', + sub { + my ( $self, $args ) = @_; + is_deeply( + $args->{biblio_ids}, + [ $hold->biblionumber ], + "AlterPriority triggers a holds queue update for the related biblio" + ); + } + ); t::lib::Mocks::mock_preference( 'RealTimeHoldsQueue', 1 ); t::lib::Mocks::mock_preference( 'HoldsLog', 1 ); @@ -1064,7 +1063,10 @@ subtest 'reserves.item_level_hold' => sub { ); my $hold = Koha::Holds->find($reserve_id); - is( $hold->item_level_hold, 0, 'item_level_hold should not be set when AddReserve is called without a specific item' ); + is( + $hold->item_level_hold, 0, + 'item_level_hold should not be set when AddReserve is called without a specific item' + ); # Mark it waiting ModReserveAffect( $item->itemnumber, $patron->borrowernumber, 1 ); @@ -1081,6 +1083,130 @@ subtest 'reserves.item_level_hold' => sub { $hold->delete; }; + subtest 'test opacitemholds rules' => sub { + plan tests => 6; + + Koha::CirculationRules->set_rules( + { + branchcode => undef, + categorycode => undef, + itemtype => undef, + rules => { + reservesallowed => 25, + opacitemholds => 'F', + } + } + ); + + my $canreserve = C4::Reserves::CanBookBeReserved( $patron->id, $item->biblionumber ); + + is( + $canreserve->{status}, + 'recordHoldNotAllowed', + 'record-level holds should not be possible with opacitemholds set to "Force"' + ); + + $canreserve = C4::Reserves::CanItemBeReserved( $patron, $item ); + + is( + $canreserve->{status}, 'OK', + 'item-level holds should be possible with opacitemholds set to "Force"' + ); + + Koha::CirculationRules->set_rules( + { + branchcode => undef, + categorycode => undef, + itemtype => undef, + rules => { + reservesallowed => 25, + opacitemholds => 'N', + } + } + ); + + $canreserve = C4::Reserves::CanBookBeReserved( $patron->id, $item->biblionumber ); + + is( + $canreserve->{status}, 'OK', + 'record-level holds should be possible with opacitemholds set to "No"' + ); + + $canreserve = C4::Reserves::CanItemBeReserved( $patron, $item ); + + is( + $canreserve->{status}, 'recordHoldsOnly', + 'item-level holds should not be possible with opacitemholds set to "No"' + ); + + Koha::CirculationRules->set_rules( + { + branchcode => undef, + categorycode => undef, + itemtype => undef, + rules => { + reservesallowed => 25, + opacitemholds => 'Y', + } + } + ); + + $canreserve = C4::Reserves::CanBookBeReserved( $patron->id, $item->biblionumber ); + + is( + $canreserve->{status}, 'OK', + 'record-level holds should be possible with opacitemholds set to "Yes"' + ); + + $canreserve = C4::Reserves::CanItemBeReserved( $patron, $item ); + + is( + $canreserve->{status}, 'OK', + 'item-level holds should be possible with opacitemholds set to "Yes"' + ); + }; + + subtest 'test opacitemholds rules in staff context' => sub { + plan tests => 2; + + C4::Context->interface('intranet'); + Koha::CirculationRules->set_rules( + { + branchcode => undef, + categorycode => undef, + itemtype => undef, + rules => { + reservesallowed => 25, + opacitemholds => 'N', + } + } + ); + + my $canreserve = C4::Reserves::CanBookBeReserved( $patron->id, $item->biblionumber ); + + is( + $canreserve->{status}, 'OK', + 'record-level holds should be possible with opacitemholds set to "No"' + ); + + $canreserve = C4::Reserves::CanItemBeReserved( $patron, $item ); + + is( + $canreserve->{status}, 'OK', + 'item-level holds should still be possible in staff context, even with opacitemholds set to "No"' + ); + }; + + Koha::CirculationRules->set_rules( + { + branchcode => undef, + categorycode => undef, + itemtype => undef, + rules => { + opacitemholds => undef, + } + } + ); }; subtest 'MoveReserve additional test' => sub { -- 2.30.2