From 304fc036b5784d6036d6342718b1c67af8adc8f3 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 19 Nov 2021 13:27:44 +0100 Subject: [PATCH] Bug 29087: Prevent filter_by_for_hold to crash if default holdallowed is not_allowed Signed-off-by: David Nind --- Koha/Items.pm | 30 +++++++++++++-- t/db_dependent/Koha/Holds.t | 7 ++++ t/db_dependent/Koha/Items.t | 74 +++++++++++++++++++++++++++++++++++++ 3 files changed, 107 insertions(+), 4 deletions(-) diff --git a/Koha/Items.pm b/Koha/Items.pm index f3edb77685..9245dfb7d4 100644 --- a/Koha/Items.pm +++ b/Koha/Items.pm @@ -61,15 +61,37 @@ placing a hold on one of those items. sub filter_by_for_hold { my ($self) = @_; - my @hold_not_allowed_itypes = Koha::CirculationRules->search( + my $default_rule = Koha::CirculationRules->get_effective_rule( { rule_name => 'holdallowed', - branchcode => undef, - categorycode => undef, rule_value => 'not_allowed', } - )->get_column('itemtype'); + ); + my @hold_not_allowed_itypes; + if ( $default_rule ) { + @hold_not_allowed_itypes = Koha::ItemTypes->search->get_column('itemtype'); + my @hold_allowed_itypes = Koha::CirculationRules->search( + { + rule_name => 'holdallowed', + rule_value => { '!=' => 'not_allowed' }, + branchcode => undef, + categorycode => undef, + } + )->get_column('itemtype'); + @hold_not_allowed_itypes = array_minus( @hold_not_allowed_itypes, @hold_allowed_itypes ) + } else { + @hold_not_allowed_itypes = Koha::CirculationRules->search( + { + rule_name => 'holdallowed', + branchcode => undef, + categorycode => undef, + rule_value => 'not_allowed', + } + )->get_column('itemtype'); + } + push @hold_not_allowed_itypes, Koha::ItemTypes->search({ notforloan => 1 })->get_column('itemtype'); + @hold_not_allowed_itypes = uniq @hold_not_allowed_itypes; my $params = { itemlost => 0, diff --git a/t/db_dependent/Koha/Holds.t b/t/db_dependent/Koha/Holds.t index 4dad5db128..f1f32ad6a7 100755 --- a/t/db_dependent/Koha/Holds.t +++ b/t/db_dependent/Koha/Holds.t @@ -532,6 +532,13 @@ subtest 'Desks' => sub { subtest 'get_items_that_can_fill' => sub { plan tests => 6; + Koha::CirculationRules->search( + { + rule_name => 'holdallowed', + rule_value => 'not_allowed', + } + )->delete; + my $biblio = $builder->build_sample_biblio; my $itype_1 = $builder->build_object({ class => 'Koha::ItemTypes' }); # For 1, 2, 3, 4 my $itype_2 = $builder->build_object({ class => 'Koha::ItemTypes' }); diff --git a/t/db_dependent/Koha/Items.t b/t/db_dependent/Koha/Items.t index 5b78910311..3fc384ef95 100755 --- a/t/db_dependent/Koha/Items.t +++ b/t/db_dependent/Koha/Items.t @@ -1639,6 +1639,80 @@ subtest 'can_be_transferred' => sub { 'We get the same result also if we pass the from-library parameter.'); }; +subtest 'filter_by_for_hold' => sub { + plan tests => 12; + + Koha::CirculationRules->search( + { + rule_name => 'holdallowed', + rule_value => 'not_allowed', + } + )->delete; + + my $biblio = $builder->build_sample_biblio; + is( $biblio->items->filter_by_for_hold->count, 0, 'no item yet' ); + $builder->build_sample_item( { biblionumber => $biblio->biblionumber, notforloan => 1 } ); + is( $biblio->items->filter_by_for_hold->count, 0, 'no item for hold' ); + $builder->build_sample_item( { biblionumber => $biblio->biblionumber, notforloan => 0 } ); + is( $biblio->items->filter_by_for_hold->count, 1, '1 item for hold' ); + $builder->build_sample_item( { biblionumber => $biblio->biblionumber, notforloan => -1 } ); + is( $biblio->items->filter_by_for_hold->count, 2, '2 items for hold' ); + + $builder->build_sample_item( { biblionumber => $biblio->biblionumber, itemlost => 0 } ); + $builder->build_sample_item( { biblionumber => $biblio->biblionumber, itemlost => 1 } ); + is( $biblio->items->filter_by_for_hold->count, 3, '3 items for hold - itemlost' ); + + $builder->build_sample_item( { biblionumber => $biblio->biblionumber, withdrawn => 0 } ); + $builder->build_sample_item( { biblionumber => $biblio->biblionumber, withdrawn => 1 } ); + is( $biblio->items->filter_by_for_hold->count, 4, '4 items for hold - withdrawn' ); + + $builder->build_sample_item( { biblionumber => $biblio->biblionumber, damaged => 0 } ); + $builder->build_sample_item( { biblionumber => $biblio->biblionumber, damaged => 1 } ); + t::lib::Mocks::mock_preference('AllowHoldsOnDamagedItems', 0); + is( $biblio->items->filter_by_for_hold->count, 5, '5 items for hold - not damaged if not AllowHoldsOnDamagedItems' ); + t::lib::Mocks::mock_preference('AllowHoldsOnDamagedItems', 1); + is( $biblio->items->filter_by_for_hold->count, 6, '6 items for hold - damaged if AllowHoldsOnDamagedItems' ); + + my $itemtype = $builder->build_object({ class => 'Koha::ItemTypes' }); + my $not_holdable_itemtype = $itemtype->itemtype; + $builder->build_sample_item( + { + biblionumber => $biblio->biblionumber, + itype => $not_holdable_itemtype, + } + ); + Koha::CirculationRules->set_rule( + { + branchcode => undef, + itemtype => $not_holdable_itemtype, + rule_name => 'holdallowed', + rule_value => 'not_allowed', + } + ); + is( $biblio->items->filter_by_for_hold->count, 6, '6 items for hold - holdallowed=not_allowed' ); + + # Remove rule, test notforloan on itemtype + Koha::CirculationRules->set_rule( + { + branchcode => undef, + itemtype => $not_holdable_itemtype, + rule_name => 'holdallowed', + rule_value => undef, + } + ); + is( $biblio->items->filter_by_for_hold->count, 7, '7 items for hold - rule deleted' ); + $itemtype->notforloan(1)->store; + is( $biblio->items->filter_by_for_hold->count, 6, '6 items for hold - notforloan' ); + + t::lib::Mocks::mock_preference('item-level_itypes', 0); + $biblio->biblioitem->itemtype($not_holdable_itemtype)->store; + is( $biblio->items->filter_by_for_hold->count, 0, '0 item-level_itypes=0' ); + + t::lib::Mocks::mock_preference('item-level_itypes', 1); + + $biblio->delete; +}; + # Reset nb_of_items prior to testing delete $nb_of_items = Koha::Items->search->count; -- 2.39.2