From ff3d99eded25202c565ca78b934e8809b719e247 Mon Sep 17 00:00:00 2001 From: Tomas Cohen Arazi Date: Tue, 13 Apr 2021 08:58:58 -0300 Subject: [PATCH] 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 Signed-off-by: Martin Renvoize Signed-off-by: Kyle M Hall Signed-off-by: Jonathan Druart --- Koha/Objects/Limit/Library.pm | 4 ++ Koha/Patron/Categories.pm | 6 +-- t/db_dependent/Koha/Patron/Attribute/Types.t | 48 +++++++++----------- 3 files changed, 27 insertions(+), 31 deletions(-) diff --git a/Koha/Objects/Limit/Library.pm b/Koha/Objects/Limit/Library.pm index 722ff887de..2f9d2d2ec6 100644 --- a/Koha/Objects/Limit/Library.pm +++ b/Koha/Objects/Limit/Library.pm @@ -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; diff --git a/Koha/Patron/Categories.pm b/Koha/Patron/Categories.pm index 7d651f1969..8ee320d0c5 100644 --- a/Koha/Patron/Categories.pm +++ b/Koha/Patron/Categories.pm @@ -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 diff --git a/t/db_dependent/Koha/Patron/Attribute/Types.t b/t/db_dependent/Koha/Patron/Attribute/Types.t index a8fd71d07f..3df82bcfba 100755 --- a/t/db_dependent/Koha/Patron/Attribute/Types.t +++ b/t/db_dependent/Koha/Patron/Attribute/Types.t @@ -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_2 - = Koha::Patron::Attribute::Type->new( { code => 'code_2', description => 'a description for code_2' } ) - ->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_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 ); + + 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 ); - # TODO test if filter_by_branch_limitations restriced on logged-in-user's branch + is( $results->count, 3, '3 attribute types are available with no library passed' ); $schema->storage->txn_rollback; }; -- 2.39.5