From a10b6cf41c4274234cf88330c64082feb4bec715 Mon Sep 17 00:00:00 2001 From: Nick Clemens Date: Wed, 1 Apr 2020 12:37:44 +0000 Subject: [PATCH] Bug 25033: Add filter_by_user_branch to Koha/Suggestions.pm --- Koha/Suggestions.pm | 39 ++++++++++++++++++++++++ t/db_dependent/Koha/Suggestions.t | 63 ++++++++++++++++++++++++++++++++++++++- 2 files changed, 101 insertions(+), 1 deletion(-) diff --git a/Koha/Suggestions.pm b/Koha/Suggestions.pm index 39f4a525fa..a62b047af8 100644 --- a/Koha/Suggestions.pm +++ b/Koha/Suggestions.pm @@ -37,6 +37,45 @@ Koha::Suggestions - Koha Suggestion object set class =cut +=head3 filter_by_user_branch + +Koha::Suggestions->filter_by_user_branch(1); + +Return suggestions that belong to user's branch, or suggestions they can view + +=head4 options + +=over 4 + +=item 1 (optional) - explicitly limit to suggestions form users branch, otherwise +check users ability to view suggestions + +=back + +=cut + +sub filter_by_user_branch { + my ($self, $option) = @_; + + # filter on user branch + if ( + $option || + C4::Context->preference('IndependentBranches') + && !C4::Context->IsSuperLibrarian() + ) + { + # If IndependentBranches is set and the logged in user is not superlibrarian + # Then we want to filter by the user's library (i.e. cannot see suggestions from other libraries) + my $userenv = C4::Context->userenv; + if ($userenv) { + return $self->search({ 'me.branchcode' => $userenv->{branch} }); + } + } else { + return $self; + } +} + + =head3 type =cut diff --git a/t/db_dependent/Koha/Suggestions.t b/t/db_dependent/Koha/Suggestions.t index 480c387cb2..5f38296ac6 100644 --- a/t/db_dependent/Koha/Suggestions.t +++ b/t/db_dependent/Koha/Suggestions.t @@ -19,7 +19,7 @@ use Modern::Perl; -use Test::More tests => 6; +use Test::More tests => 7; use Test::Exception; use Koha::Suggestion; @@ -28,6 +28,7 @@ use Koha::Database; use Koha::DateUtils; use t::lib::TestBuilder; +use t::lib::Mocks; my $schema = Koha::Database->new->schema; $schema->storage->txn_begin; @@ -171,3 +172,63 @@ subtest 'constraints' => sub { $schema->storage->dbh->{PrintError} = $print_error; $schema->storage->txn_rollback; }; + +subtest 'filter_by_user_branch' => sub { + plan tests => 9; + $schema->storage->txn_begin; + + my $pre_count = Koha::Suggestions->search()->count; + + my $branch = $builder->build_object( { class => "Koha::Libraries" } ); + + my $patron1 = $builder->build_object({class => "Koha::Patrons"}); + my $patron2 = $builder->build_object({class => "Koha::Patrons"}); + + #Create a suggestion at each branch + foreach( $branch->branchcode, $patron1->branchcode, $patron2->branchcode){ + my $suggestion = $builder->build_object( + { + class => "Koha::Suggestions", + value => { + suggestedby => $patron1->borrowernumber, + branchcode => $_, + managedby => undef, + acceptedby => undef, + rejectedby => undef, + budgetid => undef, + } + } + ); + } + + my $unfiltered_count = Koha::Suggestions->search()->count; + is( $unfiltered_count, $pre_count + 3, "We added 3 new suggestions" ); + + t::lib::Mocks::mock_preference( 'IndependentBranches', 1 ); + t::lib::Mocks::mock_userenv({ branchcode => $patron1->branchcode, flags => 1 }); + my $implicitly_filtered = Koha::Suggestions->search()->filter_by_user_branch()->count; + my $explicitly_filtered = Koha::Suggestions->search()->filter_by_user_branch(1)->count; + is( $implicitly_filtered, $unfiltered_count, "If not explicitly filtering with IndependentBranches we get all suggestions for a superlibrarian"); + is( $explicitly_filtered, 1, "If explicitly filtering with IndependentBranches we get only suggestions for a superlibrarian's branch"); + + t::lib::Mocks::mock_userenv({ branchcode => $patron2->branchcode, flags => 0 }); + $implicitly_filtered = Koha::Suggestions->search()->filter_by_user_branch()->count; + $explicitly_filtered = Koha::Suggestions->search()->filter_by_user_branch(1)->count; + is( $implicitly_filtered, 1, "If not explicitly filtering with IndependentBranches we get only suggestions for a regular librarian's branch"); + is( $explicitly_filtered, 1, "If explicitly filtering with IndependentBranchs we get only suggestions for a regular librarian's branch"); + + t::lib::Mocks::mock_preference( 'IndependentBranches', 0 ); + t::lib::Mocks::mock_userenv({ branchcode => $patron1->branchcode, flags => 1 }); + $implicitly_filtered = Koha::Suggestions->search()->filter_by_user_branch()->count; + $explicitly_filtered = Koha::Suggestions->search()->filter_by_user_branch(1)->count; + is( $implicitly_filtered, $unfiltered_count, "If not explicitly filtering we get all suggestions for a superlibrarian"); + is( $explicitly_filtered, 1, "If explicitly filtering we get only suggestions for a superlibrarian's branch"); + + t::lib::Mocks::mock_userenv({ branchcode => $patron2->branchcode, flags => 0 }); + $implicitly_filtered = Koha::Suggestions->search()->filter_by_user_branch()->count; + $explicitly_filtered = Koha::Suggestions->search()->filter_by_user_branch(1)->count; + is( $implicitly_filtered, $unfiltered_count, "If not explicitly filtering we get all suggestions for a regular librarian"); + is( $explicitly_filtered, 1, "If explicitly filtering we get only suggestions for a regular librarian's branch"); + +}; + -- 2.11.0