From f24b281057fd6a52e59da3ff5136a5aee88b5acb Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Thu, 28 Jan 2021 17:16:24 -0300 Subject: [PATCH] Bug 23830: Make Koha::AuthorisedValues use Koha::Objects::Limit::Library This patch makes Koha::AuthorisedValues inherit the 'search_with_library_limits' method from Koha::Objects::Limit::Library. This was left out of the original implementation and this patch makes this library have a similar implementation to that of Koha::ItemTypes, etc. To test: 1. Run: $ kshell k$ prove t/db_dependent/AuthorisedValues.t => SUCCESS: Tests pass! 2. Apply this patch 3. Notice Koha::AuthorisedValues now inherits this new library 4. Notice the overloaded ->search method is removed 5. Repeat (1) => SUCCESS: The behavior of the newly used method on the tests is the same as before. We only changed the method name and how we pass the library_id. 6. Sign off :-D Signed-off-by: Martin Renvoize Signed-off-by: Jonathan Druart --- Koha/AuthorisedValues.pm | 29 +---------------------------- t/db_dependent/AuthorisedValues.t | 6 +++--- 2 files changed, 4 insertions(+), 31 deletions(-) diff --git a/Koha/AuthorisedValues.pm b/Koha/AuthorisedValues.pm index a4916d571e..b23190da72 100644 --- a/Koha/AuthorisedValues.pm +++ b/Koha/AuthorisedValues.pm @@ -27,7 +27,7 @@ use Koha::AuthorisedValue; use Koha::MarcSubfieldStructures; use Koha::Cache::Memory::Lite; -use base qw(Koha::Objects); +use base qw(Koha::Objects Koha::Objects::Limit::Library); =head1 NAME @@ -39,33 +39,6 @@ Koha::AuthorisedValues - Koha Authorised value Object set class =cut -=head3 Koha::AuthorisedValues->search(); - -my @objects = Koha::AuthorisedValues->search($params); - -=cut - -sub search { - my ( $self, $params, $attributes ) = @_; - - my $branchcode = $params->{branchcode}; - delete( $params->{branchcode} ); - - my $or = - $branchcode - ? { - '-or' => [ - 'authorised_values_branches.branchcode' => undef, - 'authorised_values_branches.branchcode' => $branchcode, - ] - } - : {}; - my $join = $branchcode ? { join => 'authorised_values_branches' } : {}; - $attributes //= {}; - $attributes = { %$attributes, %$join }; - return $self->SUPER::search( { %$params, %$or, }, $attributes ); -} - sub search_by_marc_field { my ( $self, $params ) = @_; my $frameworkcode = $params->{frameworkcode} || ''; diff --git a/t/db_dependent/AuthorisedValues.t b/t/db_dependent/AuthorisedValues.t index 4c297c4818..3c22185e33 100755 --- a/t/db_dependent/AuthorisedValues.t +++ b/t/db_dependent/AuthorisedValues.t @@ -119,14 +119,14 @@ my $branchcode2 = $builder->build({ source => 'Branch' })->{branchcode}; $av1->add_library_limit( $branchcode1 ); -@authorised_values = Koha::AuthorisedValues->new()->search( { category => 'av_for_testing', branchcode => $branchcode1 } ); +@authorised_values = Koha::AuthorisedValues->search_with_library_limits( { category => 'av_for_testing' }, {}, $branchcode1 ); is( @authorised_values, 3, "Search including value with a branch limit ( branch can use the limited value ) gives correct number of results" ); -@authorised_values = Koha::AuthorisedValues->new()->search( { category => 'av_for_testing', branchcode => $branchcode2 } ); +@authorised_values = Koha::AuthorisedValues->search_with_library_limits( { category => 'av_for_testing' }, {}, $branchcode2 ); is( @authorised_values, 2, "Search including value with a branch limit ( branch *cannot* use the limited value ) gives correct number of results" ); $av1->del_library_limit( $branchcode1 ); -@authorised_values = Koha::AuthorisedValues->new()->search( { category => 'av_for_testing', branchcode => $branchcode2 } ); +@authorised_values = Koha::AuthorisedValues->search_with_library_limits( { category => 'av_for_testing' }, {}, $branchcode2 ); is( @authorised_values, 3, "Branch limitation deleted successfully" ); $av1->add_library_limit( $branchcode1 ); -- 2.39.5