From f2872b7fa81f4ff78cde641918b0a63ef635984f Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Sat, 19 Dec 2020 10:21:18 -0300 Subject: [PATCH] Bug 24254: Read the OpacHiddenItems preference internally After discussing the 'rules' parameter usefulness we decided it was not the best idea, and the gains in terms of 'performance' would me meaningless (in-memory caching of sysprefs). This patch makes a really minor tweak to the tests so they mock the C4::Context->yaml_preference method, but keeping the same original rules to highlight no behaviour change takes place. Then the rules parameter is removed from the calls, and the tests should keep passing. A minor change to make $rules = undef is made to highlight the // {} behaviour when reading the syspref.. To test: 1. Apply this patch 2. Run: $ kshell k$ prove t/db_dependent/Koha/Items.t => SUCCESS: Tests pass! 3. Sign off :-D Signed-off-by: Tomas Cohen Arazi Signed-off-by: Kyle M Hall Signed-off-by: Martin Renvoize Signed-off-by: Andrew Fuerste-Henry (cherry picked from commit 2369428d7049f9847ed4ac578684a81fe03df4d0) Signed-off-by: Victor Grousset/tuxayo --- Koha/Items.pm | 7 +++---- t/db_dependent/Koha/Items.t | 23 +++++++++++++++-------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/Koha/Items.pm b/Koha/Items.pm index 174e41020e..4c82e35a7e 100644 --- a/Koha/Items.pm +++ b/Koha/Items.pm @@ -39,18 +39,17 @@ Koha::Items - Koha Item object set class =head3 filter_by_visible_in_opac - my $filered_items = $items->filter_by_visible_in_opac({ rules => $rules }); + my $filered_items = $items->filter_by_visible_in_opac; Returns a new resultset, containing those items that are not expected to be hidden in OPAC. -If no I are passed, it returns the whole resultset, with the only caveat that the -I system preference is honoured. +The I and I system preferences are honoured. =cut sub filter_by_visible_in_opac { my ($self, $params) = @_; - my $rules = $params->{rules} // {}; + my $rules = C4::Context->yaml_preference('OpacHiddenItems') // {}; my $rules_params; foreach my $field (keys %$rules){ diff --git a/t/db_dependent/Koha/Items.t b/t/db_dependent/Koha/Items.t index baa04581f0..7f9964c0e0 100644 --- a/t/db_dependent/Koha/Items.t +++ b/t/db_dependent/Koha/Items.t @@ -236,41 +236,48 @@ subtest 'filter_by_visible_in_opac() tests' => sub { } ); - my $rules = {}; + my $rules = undef; + + my $mocked_context = Test::MockModule->new('C4::Context'); + $mocked_context->mock( 'yaml_preference', sub { + return $rules; + }); t::lib::Mocks::mock_preference( 'hidelostitems', 0 ); is( $biblio->items->filter_by_visible_in_opac->count, 6, 'No rules passed, hidelostitems unset' ); + $rules = {}; + t::lib::Mocks::mock_preference( 'hidelostitems', 1 ); is( - $biblio->items->filter_by_visible_in_opac( { rules => $rules } )->count, + $biblio->items->filter_by_visible_in_opac->count, 3, 'No rules passed, hidelostitems set' ); $rules = { withdrawn => [ 1, 2 ] }; is( - $biblio->items->filter_by_visible_in_opac( { rules => $rules } )->count, + $biblio->items->filter_by_visible_in_opac->count, 2, 'Rules on withdrawn, hidelostitems set' ); $rules = { itype => [ $itype_1->itemtype ] }; is( - $biblio->items->filter_by_visible_in_opac( { rules => $rules } )->count, + $biblio->items->filter_by_visible_in_opac->count, 2, 'Rules on itype, hidelostitems set' ); $rules = { withdrawn => [ 1, 2 ], itype => [ $itype_1->itemtype ] }; is( - $biblio->items->filter_by_visible_in_opac( { rules => $rules } )->count, + $biblio->items->filter_by_visible_in_opac->count, 1, 'Rules on itype and withdrawn, hidelostitems set' ); is( - $biblio->items->filter_by_visible_in_opac( { rules => $rules } ) + $biblio->items->filter_by_visible_in_opac ->next->itemnumber, $item_4->itemnumber, 'The right item is returned' @@ -278,12 +285,12 @@ subtest 'filter_by_visible_in_opac() tests' => sub { $rules = { withdrawn => [ 1, 2 ], itype => [ $itype_2->itemtype ] }; is( - $biblio->items->filter_by_visible_in_opac( { rules => $rules } )->count, + $biblio->items->filter_by_visible_in_opac->count, 1, 'Rules on itype and withdrawn, hidelostitems set' ); is( - $biblio->items->filter_by_visible_in_opac( { rules => $rules } ) + $biblio->items->filter_by_visible_in_opac ->next->itemnumber, $item_5->itemnumber, 'The right item is returned' -- 2.39.5