Bug 23271: (QA follow-up) Make search_with_library_limits fallback to userenv if required

This patch makes  the generic method rely on C4::Context->userenv if the
library_id is not passed.

To test:
1. Apply this patch
2. Run:
   $ kshell
  k$ prove t/db_dependent/Koha/Patron/Attribute/Types.t
=> SUCCESS: Tests pass!
3. Sign off :-D

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>

Signed-off-by: Kyle M Hall <kyle@bywatersolutions.com>

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
This commit is contained in:
Tomas Cohen Arazi 2021-04-13 08:58:58 -03:00 committed by Jonathan Druart
parent 99eaab903f
commit ff3d99eded
3 changed files with 27 additions and 31 deletions

View File

@ -17,6 +17,7 @@ package Koha::Objects::Limit::Library;
use Modern::Perl;
use C4::Context;
use Koha::Database;
=head1 NAME
@ -53,6 +54,9 @@ limits
sub search_with_library_limits {
my ( $self, $params, $attributes, $library_id ) = @_;
$library_id //= C4::Context->userenv->{branch}
if defined C4::Context->userenv;
return $self->search( $params, $attributes ) unless $library_id;
my $library_limits = $self->object_class()->_library_limits;

View File

@ -17,10 +17,6 @@ package Koha::Patron::Categories;
use Modern::Perl;
use Carp;
use C4::Context; # Sigh...
use Koha::Database;
use Koha::Patron::Category;
@ -33,7 +29,7 @@ Koha::Patron::Categories - Koha Patron Category Object set class
=head1 API
=head2 Class Methods
=head2 Class methods
=cut

View File

@ -388,31 +388,19 @@ subtest 'replace_library_limits() tests' => sub {
subtest 'search_with_library_limits() tests' => sub {
plan tests => 4;
plan tests => 5;
$schema->storage->txn_begin;
# Cleanup before running the tests
Koha::Patron::Attribute::Types->search()->delete();
my $object_code_1
= Koha::Patron::Attribute::Type->new( { code => 'code_1', description => 'a description for code_1' } )
->store();
my $object_code_1 = $builder->build_object({ class => 'Koha::Patron::Attribute::Types', value => { code => 'code_1' } });
my $object_code_2 = $builder->build_object({ class => 'Koha::Patron::Attribute::Types', value => { code => 'code_2' } });
my $object_code_3 = $builder->build_object({ class => 'Koha::Patron::Attribute::Types', value => { code => 'code_3' } });
my $object_code_4 = $builder->build_object({ class => 'Koha::Patron::Attribute::Types', value => { code => 'code_4' } });
my $object_code_2
= Koha::Patron::Attribute::Type->new( { code => 'code_2', description => 'a description for code_2' } )
->store();
my $object_code_3
= Koha::Patron::Attribute::Type->new( { code => 'code_3', description => 'a description for code_3' } )
->store();
my $object_code_4
= Koha::Patron::Attribute::Type->new( { code => 'code_4', description => 'a description for code_4' } )
->store();
is( Koha::Patron::Attribute::Types->search()->count,
4, 'Three objects created' );
is( Koha::Patron::Attribute::Types->search()->count, 4, 'Three objects created' );
my $branch_1 = $builder->build( { source => 'Branch' } )->{branchcode};
my $branch_2 = $builder->build( { source => 'Branch' } )->{branchcode};
@ -421,15 +409,23 @@ subtest 'search_with_library_limits() tests' => sub {
$object_code_2->library_limits( [$branch_2] );
$object_code_3->library_limits( [ $branch_1, $branch_2 ] );
is( Koha::Patron::Attribute::Types->search_with_library_limits( {}, {}, $branch_1 )->count,
3, '3 attribute types are available for the specified branch' );
is( Koha::Patron::Attribute::Types->search_with_library_limits( {}, {}, $branch_2 )->count,
3, '3 attribute types are available for the specified branch' );
is( Koha::Patron::Attribute::Types->search_with_library_limits( {}, {}, undef )->count,
4, '4 attribute types are available with no library passed'
);
my $results = Koha::Patron::Attribute::Types->search_with_library_limits( {}, { order_by => 'code' }, $branch_1 );
# TODO test if filter_by_branch_limitations restriced on logged-in-user's branch
is( $results->count, 3, '3 attribute types are available for the specified branch' );
$results = Koha::Patron::Attribute::Types->search_with_library_limits( {}, { order_by => 'code' }, $branch_2 );
is( $results->count, 3, '3 attribute types are available for the specified branch' );
$results = Koha::Patron::Attribute::Types->search_with_library_limits( {}, { order_by => 'code' }, undef );
is( $results->count, 4, 'All attribute types are available with no library pssed' );
t::lib::Mocks::mock_userenv({ branchcode => $branch_2 });
$results = Koha::Patron::Attribute::Types->search_with_library_limits( {}, { order_by => 'code' }, undef );
is( $results->count, 3, '3 attribute types are available with no library passed' );
$schema->storage->txn_rollback;
};