View | Details | Raw Unified | Return to bug 25033
Collapse All | Expand All

(-)a/Koha/Suggestions.pm (+39 lines)
Lines 37-42 Koha::Suggestions - Koha Suggestion object set class Link Here
37
37
38
=cut
38
=cut
39
39
40
=head3 filter_by_user_branch
41
42
Koha::Suggestions->filter_by_user_branch(1);
43
44
Return suggestions that belong to user's branch, or suggestions they can view
45
46
=head4 options
47
48
=over 4
49
50
=item 1 (optional)  - explicitly limit to suggestions form users branch, otherwise
51
check users ability to view suggestions
52
53
=back
54
55
=cut
56
57
sub filter_by_user_branch {
58
    my ($self, $option) = @_;
59
60
    # filter on user branch
61
    if (
62
        $option ||
63
        C4::Context->preference('IndependentBranches')
64
        && !C4::Context->IsSuperLibrarian()
65
    )
66
    {
67
        # If IndependentBranches is set and the logged in user is not superlibrarian
68
        # Then we want to filter by the user's library (i.e. cannot see suggestions from other libraries)
69
        my $userenv = C4::Context->userenv;
70
        if ($userenv) {
71
            return $self->search({ 'me.branchcode' => $userenv->{branch} });
72
        }
73
    } else {
74
        return $self;
75
    }
76
}
77
78
40
=head3 type
79
=head3 type
41
80
42
=cut
81
=cut
(-)a/t/db_dependent/Koha/Suggestions.t (-2 / +61 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 6;
22
use Test::More tests => 7;
23
use Test::Exception;
23
use Test::Exception;
24
24
25
use Koha::Suggestion;
25
use Koha::Suggestion;
Lines 28-33 use Koha::Database; Link Here
28
use Koha::DateUtils;
28
use Koha::DateUtils;
29
29
30
use t::lib::TestBuilder;
30
use t::lib::TestBuilder;
31
use t::lib::Mocks;
31
32
32
my $schema = Koha::Database->new->schema;
33
my $schema = Koha::Database->new->schema;
33
$schema->storage->txn_begin;
34
$schema->storage->txn_begin;
Lines 171-173 subtest 'constraints' => sub { Link Here
171
    $schema->storage->dbh->{PrintError} = $print_error;
172
    $schema->storage->dbh->{PrintError} = $print_error;
172
    $schema->storage->txn_rollback;
173
    $schema->storage->txn_rollback;
173
};
174
};
174
- 
175
176
subtest 'filter_by_user_branch' => sub {
177
    plan tests => 9;
178
    $schema->storage->txn_begin;
179
180
    my $pre_count = Koha::Suggestions->search()->count;
181
182
    my $branch = $builder->build_object( { class => "Koha::Libraries" } );
183
184
    my $patron1 = $builder->build_object({class => "Koha::Patrons"});
185
    my $patron2 = $builder->build_object({class => "Koha::Patrons"});
186
187
    #Create a suggestion at each branch
188
    foreach( $branch->branchcode, $patron1->branchcode, $patron2->branchcode){
189
        my $suggestion = $builder->build_object(
190
            {
191
                class => "Koha::Suggestions",
192
                value => {
193
                    suggestedby  => $patron1->borrowernumber,
194
                    branchcode   => $_,
195
                    managedby    => undef,
196
                    acceptedby   => undef,
197
                    rejectedby   => undef,
198
                    budgetid     => undef,
199
                }
200
            }
201
        );
202
    }
203
204
    my $unfiltered_count = Koha::Suggestions->search()->count;
205
    is( $unfiltered_count, $pre_count + 3, "We added 3 new suggestions" );
206
207
    t::lib::Mocks::mock_preference( 'IndependentBranches', 1 );
208
    t::lib::Mocks::mock_userenv({ branchcode => $patron1->branchcode, flags => 1 });
209
    my $implicitly_filtered = Koha::Suggestions->search()->filter_by_user_branch()->count;
210
    my $explicitly_filtered = Koha::Suggestions->search()->filter_by_user_branch(1)->count;
211
    is( $implicitly_filtered, $unfiltered_count, "If not explicitly filtering with IndependentBranches we get all suggestions for a superlibrarian");
212
    is( $explicitly_filtered, 1, "If explicitly filtering with IndependentBranches we get only suggestions for a superlibrarian's branch");
213
214
    t::lib::Mocks::mock_userenv({ branchcode => $patron2->branchcode, flags => 0 });
215
    $implicitly_filtered = Koha::Suggestions->search()->filter_by_user_branch()->count;
216
    $explicitly_filtered = Koha::Suggestions->search()->filter_by_user_branch(1)->count;
217
    is( $implicitly_filtered, 1, "If not explicitly filtering with IndependentBranches we get only suggestions for a regular librarian's branch");
218
    is( $explicitly_filtered, 1, "If explicitly filtering with IndependentBranchs we get only suggestions for a regular librarian's branch");
219
220
    t::lib::Mocks::mock_preference( 'IndependentBranches', 0 );
221
    t::lib::Mocks::mock_userenv({ branchcode => $patron1->branchcode, flags => 1 });
222
    $implicitly_filtered = Koha::Suggestions->search()->filter_by_user_branch()->count;
223
    $explicitly_filtered = Koha::Suggestions->search()->filter_by_user_branch(1)->count;
224
    is( $implicitly_filtered, $unfiltered_count, "If not explicitly filtering we get all suggestions for a superlibrarian");
225
    is( $explicitly_filtered, 1, "If explicitly filtering we get only suggestions for a superlibrarian's branch");
226
227
    t::lib::Mocks::mock_userenv({ branchcode => $patron2->branchcode, flags => 0 });
228
    $implicitly_filtered = Koha::Suggestions->search()->filter_by_user_branch()->count;
229
    $explicitly_filtered = Koha::Suggestions->search()->filter_by_user_branch(1)->count;
230
    is( $implicitly_filtered, $unfiltered_count, "If not explicitly filtering we get all suggestions for a regular librarian");
231
    is( $explicitly_filtered, 1, "If explicitly filtering we get only suggestions for a regular librarian's branch");
232
233
};

Return to bug 25033