Bug 11023: Automatic item modification by age (Was Toggle "new" status")
[koha.git] / t / db_dependent / Auth / haspermission.t
1 #!/usr/bin/perl
2
3 # Tests for C4::Auth::haspermission
4
5 # This file is part of Koha.
6 #
7 # Copyright 2016 Rijksmuseum
8 #
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21
22 use Modern::Perl;
23 use Test::More tests => 13;
24
25 use Koha::Database;
26 use t::lib::TestBuilder;
27 use C4::Auth qw(haspermission);
28
29 my $schema = Koha::Database->new->schema;
30 $schema->storage->txn_begin;
31
32 # Adding two borrowers and granular permissions for the second borrower
33 my $builder = t::lib::TestBuilder->new();
34 my $borr1 = $builder->build({
35     source => 'Borrower',
36     value  => {
37         surname => 'Superlib',
38         flags   => 1,
39     },
40 });
41 my $borr2 = $builder->build({
42     source => 'Borrower',
43     value  => {
44         surname => 'Bor2',
45         flags   => 2 + 4 + 2**11, # circulate, catalogue, acquisition
46     },
47 });
48 $builder->build({
49     source => 'UserPermission',
50     value  => {
51         borrowernumber => $borr2->{borrowernumber},
52         module_bit => 13, # tools
53         code => 'upload_local_cover_images',
54     },
55 });
56 $builder->build({
57     source => 'UserPermission',
58     value  => {
59         borrowernumber => $borr2->{borrowernumber},
60         module_bit => 13, # tools
61         code => 'batch_upload_patron_images',
62     },
63 });
64
65 # Check top level permission for superlibrarian
66 my $r = haspermission( $borr1->{userid},
67     { circulate => 1, editcatalogue => 1 } );
68 is( ref($r), 'HASH', 'Superlibrarian/circulate' );
69
70 # Check specific top level permission(s) for borr2
71 $r = haspermission( $borr2->{userid},
72     { circulate => 1, catalogue => 1 } );
73 is( ref($r), 'HASH', 'Borrower2/circulate' );
74 $r = haspermission( $borr2->{userid}, { updatecharges => 1 } );
75 is( $r, 0, 'Borrower2/updatecharges should fail' );
76
77 # Check granular permission with 1: means all subpermissions
78 $r = haspermission( $borr1->{userid}, { tools => 1 } );
79 is( ref($r), 'HASH', 'Superlibrarian/tools granular all' );
80 $r = haspermission( $borr2->{userid}, { tools => 1 } );
81 is( $r, 0, 'Borrower2/tools granular all should fail' );
82
83 # Check granular permission with *: means at least one subpermission
84 $r = haspermission( $borr1->{userid}, { tools => '*' } );
85 is( ref($r), 'HASH', 'Superlibrarian/tools granular *' );
86 $r = haspermission( $borr2->{userid}, { acquisition => '*' } );
87 is( ref($r), 'HASH', 'Borrower2/acq granular *' );
88 $r = haspermission( $borr2->{userid}, { tools => '*' } );
89 is( ref($r), 'HASH', 'Borrower2/tools granular *' );
90 $r = haspermission( $borr2->{userid}, { serials => '*' } );
91 is( $r, 0, 'Borrower2/serials granular * should fail' );
92
93 # Check granular permission with one or more specific subperms
94 $r = haspermission( $borr1->{userid}, { tools => 'edit_news' } );
95 is( ref($r), 'HASH', 'Superlibrarian/tools edit_news' );
96 $r = haspermission( $borr2->{userid}, { acquisition => 'budget_manage' } );
97 is( ref($r), 'HASH', 'Borrower2/acq budget_manage' );
98 $r = haspermission( $borr2->{userid},
99     { acquisition => 'budget_manage', tools => 'edit_news' } );
100 is( $r, 0, 'Borrower2/two granular should fail' );
101 $r = haspermission( $borr2->{userid}, {
102     tools => 'upload_local_cover_images',
103     tools => 'batch_upload_patron_images',
104 });
105 is( ref($r), 'HASH', 'Borrower2/tools granular two upload subperms' );
106
107 # End
108 $schema->storage->txn_rollback;