Bug 22660: DBRev 20.06.00.019
[koha.git] / t / Budgets / CanUserModifyBudget.t
blob91f4c77791daa23d1adaef3e46ac77ee14783996
1 #!/usr/bin/perl
3 use Modern::Perl;
4 use Test::More tests => 134;
6 use C4::Budgets;
7 use t::lib::Mocks;
9 # Avoid "redefined subroutine" warnings
10 local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
11 *C4::Budgets::GetBudgetUsers = \&Mock_GetBudgetUsers;
13 my %budgetusers = (
14 1 => [],
15 2 => [1],
16 3 => [2],
17 4 => [3],
18 5 => [],
19 6 => [1],
20 7 => [2],
21 8 => [3],
22 9 => [],
23 10 => [1],
24 11 => [2],
25 12 => [3],
26 13 => [],
27 14 => [1],
28 15 => [2],
29 16 => [3],
32 my $borrower1 = {
33 borrowernumber => 1
35 my $borrower2 = {
36 borrowernumber => 2
39 my $budget1 = {
40 budget_id => 1,
41 budget_permission => 0,
42 budget_owner_id => undef,
43 budget_branchcode => undef,
45 my $budget2 = {
46 budget_id => 2,
47 budget_permission => 0,
48 budget_owner_id => undef,
49 budget_branchcode => 'B1',
51 my $budget3 = {
52 budget_id => 3,
53 budget_permission => 0,
54 budget_owner_id => 1,
55 budget_branchcode => undef,
57 my $budget4 = {
58 budget_id => 4,
59 budget_permission => 0,
60 budget_owner_id => 1,
61 budget_branchcode => 'B1',
63 my $budget5 = {
64 budget_id => 5,
65 budget_permission => 1,
66 budget_owner_id => undef,
67 budget_branchcode => undef,
69 my $budget6 = {
70 budget_id => 6,
71 budget_permission => 1,
72 budget_owner_id => undef,
73 budget_branchcode => 'B1',
75 my $budget7 = {
76 budget_id => 7,
77 budget_permission => 1,
78 budget_owner_id => 1,
79 budget_branchcode => undef,
81 my $budget8 = {
82 budget_id => 8,
83 budget_permission => 1,
84 budget_owner_id => 1,
85 budget_branchcode => 'B1',
87 my $budget9 = {
88 budget_id => 9,
89 budget_permission => 2,
90 budget_owner_id => undef,
91 budget_branchcode => undef,
93 my $budget10 = {
94 budget_id => 10,
95 budget_permission => 2,
96 budget_owner_id => undef,
97 budget_branchcode => 'B1',
99 my $budget11 = {
100 budget_id => 11,
101 budget_permission => 2,
102 budget_owner_id => 1,
103 budget_branchcode => undef,
105 my $budget12 = {
106 budget_id => 12,
107 budget_permission => 2,
108 budget_owner_id => 1,
109 budget_branchcode => 'B1',
111 my $budget13 = {
112 budget_id => 13,
113 budget_permission => 3,
114 budget_owner_id => undef,
115 budget_branchcode => undef,
117 my $budget14 = {
118 budget_id => 14,
119 budget_permission => 3,
120 budget_owner_id => undef,
121 budget_branchcode => 'B1',
123 my $budget15 = {
124 budget_id => 15,
125 budget_permission => 3,
126 budget_owner_id => 1,
127 budget_branchcode => undef,
129 my $budget16 = {
130 budget_id => 16,
131 budget_permission => 3,
132 budget_owner_id => 1,
133 budget_branchcode => 'B1',
136 ok( !CanUserModifyBudget( 0, undef, {} ), "CanUserModifyBudget evaluates to false if DB user is passed" );
138 ok (CanUserModifyBudget($borrower1, $budget1, {superlibrarian => 1}));
139 ok (CanUserModifyBudget($borrower1, $budget1, {
140 acquisition => {
141 budget_manage_all => 1
143 }));
144 ok (CanUserModifyBudget($borrower1, $budget1, {acquisition => 1}));
146 ok (!CanUserModifyBudget($borrower1, $budget1, {}));
147 ok (!CanUserModifyBudget($borrower1, $budget1, {acquisition => 0}));
149 my $flags = {acquisition => {budget_modify => 1}};
151 t::lib::Mocks::mock_userenv({ branchcode => 'B1' });
153 # Restriction is 'none'
154 ok (CanUserModifyBudget($borrower1, $budget1, $flags));
155 ok (CanUserModifyBudget($borrower1, $budget2, $flags));
156 ok (CanUserModifyBudget($borrower1, $budget3, $flags));
157 ok (CanUserModifyBudget($borrower1, $budget4, $flags));
158 ok (CanUserModifyBudget($borrower2, $budget1, $flags));
159 ok (CanUserModifyBudget($borrower2, $budget2, $flags));
160 ok (CanUserModifyBudget($borrower2, $budget3, $flags));
161 ok (CanUserModifyBudget($borrower2, $budget4, $flags));
163 # Restriction is 'owner'
164 ok (CanUserModifyBudget($borrower1, $budget5, $flags));
165 ok (CanUserModifyBudget($borrower1, $budget6, $flags));
166 ok (CanUserModifyBudget($borrower1, $budget7, $flags));
167 ok (CanUserModifyBudget($borrower1, $budget8, $flags));
168 ok (CanUserModifyBudget($borrower2, $budget5, $flags));
169 ok (CanUserModifyBudget($borrower2, $budget6, $flags));
170 ok (!CanUserModifyBudget($borrower2, $budget7, $flags));
171 ok (!CanUserModifyBudget($borrower2, $budget8, $flags));
173 # Restriction is 'owner, users and library'
174 ok (CanUserModifyBudget($borrower1, $budget9, $flags));
175 ok (CanUserModifyBudget($borrower1, $budget10, $flags));
176 ok (CanUserModifyBudget($borrower1, $budget11, $flags));
177 ok (CanUserModifyBudget($borrower1, $budget12, $flags));
178 ok (CanUserModifyBudget($borrower2, $budget9, $flags));
179 ok (CanUserModifyBudget($borrower2, $budget10, $flags));
180 ok (CanUserModifyBudget($borrower2, $budget11, $flags));
181 ok (CanUserModifyBudget($borrower2, $budget12, $flags));
183 # Restriction is 'owner and users'
184 ok (!CanUserModifyBudget($borrower1, $budget13, $flags)); # no owner, no user
185 ok (CanUserModifyBudget($borrower1, $budget14, $flags));
186 ok (CanUserModifyBudget($borrower1, $budget15, $flags));
187 ok (CanUserModifyBudget($borrower1, $budget16, $flags));
188 ok (!CanUserModifyBudget($borrower2, $budget13, $flags)); # no owner, no user
189 ok (!CanUserModifyBudget($borrower2, $budget14, $flags)); # No owner and user list contains borrower1
190 ok (CanUserModifyBudget($borrower2, $budget15, $flags));
191 ok (!CanUserModifyBudget($borrower2, $budget16, $flags));
193 t::lib::Mocks::mock_userenv({ branchcode => 'B2' });
195 # Restriction is 'none'
196 ok (CanUserModifyBudget($borrower1, $budget1, $flags));
197 ok (CanUserModifyBudget($borrower1, $budget2, $flags));
198 ok (CanUserModifyBudget($borrower1, $budget3, $flags));
199 ok (CanUserModifyBudget($borrower1, $budget4, $flags));
200 ok (CanUserModifyBudget($borrower2, $budget1, $flags));
201 ok (CanUserModifyBudget($borrower2, $budget2, $flags));
202 ok (CanUserModifyBudget($borrower2, $budget3, $flags));
203 ok (CanUserModifyBudget($borrower2, $budget4, $flags));
205 # Restriction is 'owner'
206 ok (CanUserModifyBudget($borrower1, $budget5, $flags));
207 ok (CanUserModifyBudget($borrower1, $budget6, $flags));
208 ok (CanUserModifyBudget($borrower1, $budget7, $flags));
209 ok (CanUserModifyBudget($borrower1, $budget8, $flags));
210 ok (CanUserModifyBudget($borrower2, $budget5, $flags));
211 ok (CanUserModifyBudget($borrower2, $budget6, $flags));
212 ok (!CanUserModifyBudget($borrower2, $budget7, $flags));
213 ok (!CanUserModifyBudget($borrower2, $budget8, $flags));
215 # Restriction is 'owner, users and library'
216 ok (CanUserModifyBudget($borrower1, $budget9, $flags));
217 ok (CanUserModifyBudget($borrower1, $budget10, $flags));
218 ok (CanUserModifyBudget($borrower1, $budget11, $flags));
219 ok (CanUserModifyBudget($borrower1, $budget12, $flags));
220 ok (CanUserModifyBudget($borrower2, $budget9, $flags));
221 ok (!CanUserModifyBudget($borrower2, $budget10, $flags)); # Limited to library B1
222 ok (CanUserModifyBudget($borrower2, $budget11, $flags));
223 ok (!CanUserModifyBudget($borrower2, $budget12, $flags));
225 # Restriction is 'owner and users'
226 ok (!CanUserModifyBudget($borrower1, $budget13, $flags)); # No owner, no user
227 ok (CanUserModifyBudget($borrower1, $budget14, $flags));
228 ok (CanUserModifyBudget($borrower1, $budget15, $flags));
229 ok (CanUserModifyBudget($borrower1, $budget16, $flags));
230 ok (!CanUserModifyBudget($borrower2, $budget13, $flags)); # No owner, no user
231 ok (!CanUserModifyBudget($borrower2, $budget14, $flags)); # No owner and user list contains borrower1
232 ok (CanUserModifyBudget($borrower2, $budget15, $flags));
233 ok (!CanUserModifyBudget($borrower2, $budget16, $flags));
236 # Same tests as above, without budget_modify permission
237 # All tests should failed
238 $flags = {acquisition => {order_manage => 1}};
240 t::lib::Mocks::mock_userenv({ branchcode => 'B1' });
242 # Restriction is 'none'
243 ok (!CanUserModifyBudget($borrower1, $budget1, $flags));
244 ok (!CanUserModifyBudget($borrower1, $budget2, $flags));
245 ok (!CanUserModifyBudget($borrower1, $budget3, $flags));
246 ok (!CanUserModifyBudget($borrower1, $budget4, $flags));
247 ok (!CanUserModifyBudget($borrower2, $budget1, $flags));
248 ok (!CanUserModifyBudget($borrower2, $budget2, $flags));
249 ok (!CanUserModifyBudget($borrower2, $budget3, $flags));
250 ok (!CanUserModifyBudget($borrower2, $budget4, $flags));
252 # Restriction is 'owner'
253 ok (!CanUserModifyBudget($borrower1, $budget5, $flags));
254 ok (!CanUserModifyBudget($borrower1, $budget6, $flags));
255 ok (!CanUserModifyBudget($borrower1, $budget7, $flags));
256 ok (!CanUserModifyBudget($borrower1, $budget8, $flags));
257 ok (!CanUserModifyBudget($borrower2, $budget5, $flags));
258 ok (!CanUserModifyBudget($borrower2, $budget6, $flags));
259 ok (!CanUserModifyBudget($borrower2, $budget7, $flags));
260 ok (!CanUserModifyBudget($borrower2, $budget8, $flags));
262 # Restriction is 'owner, users and library'
263 ok (!CanUserModifyBudget($borrower1, $budget9, $flags));
264 ok (!CanUserModifyBudget($borrower1, $budget10, $flags));
265 ok (!CanUserModifyBudget($borrower1, $budget11, $flags));
266 ok (!CanUserModifyBudget($borrower1, $budget12, $flags));
267 ok (!CanUserModifyBudget($borrower2, $budget9, $flags));
268 ok (!CanUserModifyBudget($borrower2, $budget10, $flags));
269 ok (!CanUserModifyBudget($borrower2, $budget11, $flags));
270 ok (!CanUserModifyBudget($borrower2, $budget12, $flags));
272 # Restriction is 'owner and users'
273 ok (!CanUserModifyBudget($borrower1, $budget13, $flags));
274 ok (!CanUserModifyBudget($borrower1, $budget14, $flags));
275 ok (!CanUserModifyBudget($borrower1, $budget15, $flags));
276 ok (!CanUserModifyBudget($borrower1, $budget16, $flags));
277 ok (!CanUserModifyBudget($borrower2, $budget13, $flags));
278 ok (!CanUserModifyBudget($borrower2, $budget14, $flags));
279 ok (!CanUserModifyBudget($borrower2, $budget15, $flags));
280 ok (!CanUserModifyBudget($borrower2, $budget16, $flags));
283 t::lib::Mocks::mock_userenv({ branchcode => 'B2' });
285 # Restriction is 'none'
286 ok (!CanUserModifyBudget($borrower1, $budget1, $flags));
287 ok (!CanUserModifyBudget($borrower1, $budget2, $flags));
288 ok (!CanUserModifyBudget($borrower1, $budget3, $flags));
289 ok (!CanUserModifyBudget($borrower1, $budget4, $flags));
290 ok (!CanUserModifyBudget($borrower2, $budget1, $flags));
291 ok (!CanUserModifyBudget($borrower2, $budget2, $flags));
292 ok (!CanUserModifyBudget($borrower2, $budget3, $flags));
293 ok (!CanUserModifyBudget($borrower2, $budget4, $flags));
295 # Restriction is 'owner'
296 ok (!CanUserModifyBudget($borrower1, $budget5, $flags));
297 ok (!CanUserModifyBudget($borrower1, $budget6, $flags));
298 ok (!CanUserModifyBudget($borrower1, $budget7, $flags));
299 ok (!CanUserModifyBudget($borrower1, $budget8, $flags));
300 ok (!CanUserModifyBudget($borrower2, $budget5, $flags));
301 ok (!CanUserModifyBudget($borrower2, $budget6, $flags));
302 ok (!CanUserModifyBudget($borrower2, $budget7, $flags));
303 ok (!CanUserModifyBudget($borrower2, $budget8, $flags));
305 # Restriction is 'owner, users and library'
306 ok (!CanUserModifyBudget($borrower1, $budget9, $flags));
307 ok (!CanUserModifyBudget($borrower1, $budget10, $flags));
308 ok (!CanUserModifyBudget($borrower1, $budget11, $flags));
309 ok (!CanUserModifyBudget($borrower1, $budget12, $flags));
310 ok (!CanUserModifyBudget($borrower2, $budget9, $flags));
311 ok (!CanUserModifyBudget($borrower2, $budget10, $flags));
312 ok (!CanUserModifyBudget($borrower2, $budget11, $flags));
313 ok (!CanUserModifyBudget($borrower2, $budget12, $flags));
315 # Restriction is 'owner and users'
316 ok (!CanUserModifyBudget($borrower1, $budget13, $flags));
317 ok (!CanUserModifyBudget($borrower1, $budget14, $flags));
318 ok (!CanUserModifyBudget($borrower1, $budget15, $flags));
319 ok (!CanUserModifyBudget($borrower1, $budget16, $flags));
320 ok (!CanUserModifyBudget($borrower2, $budget13, $flags));
321 ok (!CanUserModifyBudget($borrower2, $budget14, $flags));
322 ok (!CanUserModifyBudget($borrower2, $budget15, $flags));
323 ok (!CanUserModifyBudget($borrower2, $budget16, $flags));
326 # Mocked subs
328 # C4::Acquisition::GetBudgetUsers
329 sub Mock_GetBudgetUsers {
330 my ($budget_id) = @_;
332 return @{ $budgetusers{$budget_id} };