@@ -, +, @@ fallback to userenv if required $ kshell k$ prove t/db_dependent/Koha/Patron/Attribute/Types.t --- 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(-) --- a/Koha/Objects/Limit/Library.pm +++ a/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; --- a/Koha/Patron/Categories.pm +++ a/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 --- a/t/db_dependent/Koha/Patron/Attribute/Types.t +++ a/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; }; --