Bug 23830: Make Koha::AuthorisedValues use Koha::Objects::Limit::Library
authorTomas Cohen Arazi <tomascohen@theke.io>
Thu, 28 Jan 2021 20:16:24 +0000 (17:16 -0300)
committerJonathan Druart <jonathan.druart@bugs.koha-community.org>
Mon, 8 Feb 2021 16:09:34 +0000 (17:09 +0100)
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 <martin.renvoize@ptfs-europe.com>
Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Koha/AuthorisedValues.pm
t/db_dependent/AuthorisedValues.t

index a4916d5..b23190d 100644 (file)
@@ -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} || '';
index 4c297c4..3c22185 100755 (executable)
@@ -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 );