@@ -, +, @@ $ kshell k$ prove t/db_dependent/Koha/Items.t --- Koha/Items.pm | 20 ++++++++++++++++---- t/db_dependent/Koha/Items.t | 12 ++++++++++-- 2 files changed, 26 insertions(+), 6 deletions(-) --- a/Koha/Items.pm +++ a/Koha/Items.pm @@ -65,13 +65,25 @@ sub filter_by_visible_in_opac { my $rules = $params->{rules} // {}; - my $search_params; + my $rules_params; foreach my $field (keys %$rules){ - $search_params->{$field}->{'-not_in'} = $rules->{$field}; + $rules_params->{$field}->{'-not_in'} = $rules->{$field}; } - $search_params->{itemlost}->{'<='} = 0 - if C4::Context->preference('hidelostitems'); + my $itemlost_params; + $itemlost_params = { + itemlost => [ 0, undef, '' ] # things Perl evaluates to false + } if C4::Context->preference('hidelostitems'); + + my $search_params; + if ( $rules_params and $itemlost_params ) { + $search_params = { + '-and' => [ $rules_params, $itemlost_params ] + }; + } + else { + $search_params = $rules_params // $itemlost_params; + } return $self->search( $search_params ); } --- a/t/db_dependent/Koha/Items.t +++ a/t/db_dependent/Koha/Items.t @@ -1371,17 +1371,25 @@ subtest 'filter_by_visible_in_opac() tests' => sub { withdrawn => 5 } ); + my $item_6 = $builder->build_sample_item( + { + biblionumber => $biblio->biblionumber, + itemlost => 2, + itype => $itype_1->itemtype, + withdrawn => 5 + } + ); my $rules = {}; t::lib::Mocks::mock_preference( 'hidelostitems', 0 ); is( $biblio->items->filter_by_visible_in_opac->count, - 5, 'No rules passed, hidelostitems unset' ); + 6, 'No rules passed, hidelostitems unset' ); t::lib::Mocks::mock_preference( 'hidelostitems', 1 ); is( $biblio->items->filter_by_visible_in_opac( { rules => $rules } )->count, - 4, + 3, 'No rules passed, hidelostitems set' ); --