4 use Test
::More tests
=> 133;
8 # Avoid "redefined subroutine" warnings
9 local $SIG{__WARN__
} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
10 *C4
::Budgets
::GetBudgetUsers
= \
&Mock_GetBudgetUsers
;
11 *C4
::Context
::userenv
= \
&Mock_userenv
;
41 budget_permission
=> 0,
42 budget_owner_id
=> undef,
43 budget_branchcode
=> undef,
47 budget_permission
=> 0,
48 budget_owner_id
=> undef,
49 budget_branchcode
=> 'B1',
53 budget_permission
=> 0,
55 budget_branchcode
=> undef,
59 budget_permission
=> 0,
61 budget_branchcode
=> 'B1',
65 budget_permission
=> 1,
66 budget_owner_id
=> undef,
67 budget_branchcode
=> undef,
71 budget_permission
=> 1,
72 budget_owner_id
=> undef,
73 budget_branchcode
=> 'B1',
77 budget_permission
=> 1,
79 budget_branchcode
=> undef,
83 budget_permission
=> 1,
85 budget_branchcode
=> 'B1',
89 budget_permission
=> 2,
90 budget_owner_id
=> undef,
91 budget_branchcode
=> undef,
95 budget_permission
=> 2,
96 budget_owner_id
=> undef,
97 budget_branchcode
=> 'B1',
101 budget_permission
=> 2,
102 budget_owner_id
=> 1,
103 budget_branchcode
=> undef,
107 budget_permission
=> 2,
108 budget_owner_id
=> 1,
109 budget_branchcode
=> 'B1',
113 budget_permission
=> 3,
114 budget_owner_id
=> undef,
115 budget_branchcode
=> undef,
119 budget_permission
=> 3,
120 budget_owner_id
=> undef,
121 budget_branchcode
=> 'B1',
125 budget_permission
=> 3,
126 budget_owner_id
=> 1,
127 budget_branchcode
=> undef,
131 budget_permission
=> 3,
132 budget_owner_id
=> 1,
133 budget_branchcode
=> 'B1',
139 ok
(CanUserModifyBudget
($borrower1, $budget1, {superlibrarian
=> 1}));
140 ok
(CanUserModifyBudget
($borrower1, $budget1, {
142 budget_manage_all
=> 1
145 ok
(CanUserModifyBudget
($borrower1, $budget1, {acquisition
=> 1}));
147 ok
(!CanUserModifyBudget
($borrower1, $budget1, {}));
148 ok
(!CanUserModifyBudget
($borrower1, $budget1, {acquisition
=> 0}));
150 my $flags = {acquisition
=> {budget_modify
=> 1}};
152 $userenv->{branch
} = 'B1';
154 # Restriction is 'none'
155 ok
(CanUserModifyBudget
($borrower1, $budget1, $flags));
156 ok
(CanUserModifyBudget
($borrower1, $budget2, $flags));
157 ok
(CanUserModifyBudget
($borrower1, $budget3, $flags));
158 ok
(CanUserModifyBudget
($borrower1, $budget4, $flags));
159 ok
(CanUserModifyBudget
($borrower2, $budget1, $flags));
160 ok
(CanUserModifyBudget
($borrower2, $budget2, $flags));
161 ok
(CanUserModifyBudget
($borrower2, $budget3, $flags));
162 ok
(CanUserModifyBudget
($borrower2, $budget4, $flags));
164 # Restriction is 'owner'
165 ok
(CanUserModifyBudget
($borrower1, $budget5, $flags));
166 ok
(CanUserModifyBudget
($borrower1, $budget6, $flags));
167 ok
(CanUserModifyBudget
($borrower1, $budget7, $flags));
168 ok
(CanUserModifyBudget
($borrower1, $budget8, $flags));
169 ok
(CanUserModifyBudget
($borrower2, $budget5, $flags));
170 ok
(CanUserModifyBudget
($borrower2, $budget6, $flags));
171 ok
(!CanUserModifyBudget
($borrower2, $budget7, $flags));
172 ok
(!CanUserModifyBudget
($borrower2, $budget8, $flags));
174 # Restriction is 'owner, users and library'
175 ok
(CanUserModifyBudget
($borrower1, $budget9, $flags));
176 ok
(CanUserModifyBudget
($borrower1, $budget10, $flags));
177 ok
(CanUserModifyBudget
($borrower1, $budget11, $flags));
178 ok
(CanUserModifyBudget
($borrower1, $budget12, $flags));
179 ok
(CanUserModifyBudget
($borrower2, $budget9, $flags));
180 ok
(CanUserModifyBudget
($borrower2, $budget10, $flags));
181 ok
(CanUserModifyBudget
($borrower2, $budget11, $flags));
182 ok
(CanUserModifyBudget
($borrower2, $budget12, $flags));
184 # Restriction is 'owner and users'
185 ok
(!CanUserModifyBudget
($borrower1, $budget13, $flags)); # no owner, no user
186 ok
(CanUserModifyBudget
($borrower1, $budget14, $flags));
187 ok
(CanUserModifyBudget
($borrower1, $budget15, $flags));
188 ok
(CanUserModifyBudget
($borrower1, $budget16, $flags));
189 ok
(!CanUserModifyBudget
($borrower2, $budget13, $flags)); # no owner, no user
190 ok
(!CanUserModifyBudget
($borrower2, $budget14, $flags)); # No owner and user list contains borrower1
191 ok
(CanUserModifyBudget
($borrower2, $budget15, $flags));
192 ok
(!CanUserModifyBudget
($borrower2, $budget16, $flags));
195 $userenv->{branch
} = 'B2';
197 # Restriction is 'none'
198 ok
(CanUserModifyBudget
($borrower1, $budget1, $flags));
199 ok
(CanUserModifyBudget
($borrower1, $budget2, $flags));
200 ok
(CanUserModifyBudget
($borrower1, $budget3, $flags));
201 ok
(CanUserModifyBudget
($borrower1, $budget4, $flags));
202 ok
(CanUserModifyBudget
($borrower2, $budget1, $flags));
203 ok
(CanUserModifyBudget
($borrower2, $budget2, $flags));
204 ok
(CanUserModifyBudget
($borrower2, $budget3, $flags));
205 ok
(CanUserModifyBudget
($borrower2, $budget4, $flags));
207 # Restriction is 'owner'
208 ok
(CanUserModifyBudget
($borrower1, $budget5, $flags));
209 ok
(CanUserModifyBudget
($borrower1, $budget6, $flags));
210 ok
(CanUserModifyBudget
($borrower1, $budget7, $flags));
211 ok
(CanUserModifyBudget
($borrower1, $budget8, $flags));
212 ok
(CanUserModifyBudget
($borrower2, $budget5, $flags));
213 ok
(CanUserModifyBudget
($borrower2, $budget6, $flags));
214 ok
(!CanUserModifyBudget
($borrower2, $budget7, $flags));
215 ok
(!CanUserModifyBudget
($borrower2, $budget8, $flags));
217 # Restriction is 'owner, users and library'
218 ok
(CanUserModifyBudget
($borrower1, $budget9, $flags));
219 ok
(CanUserModifyBudget
($borrower1, $budget10, $flags));
220 ok
(CanUserModifyBudget
($borrower1, $budget11, $flags));
221 ok
(CanUserModifyBudget
($borrower1, $budget12, $flags));
222 ok
(CanUserModifyBudget
($borrower2, $budget9, $flags));
223 ok
(!CanUserModifyBudget
($borrower2, $budget10, $flags)); # Limited to library B1
224 ok
(CanUserModifyBudget
($borrower2, $budget11, $flags));
225 ok
(!CanUserModifyBudget
($borrower2, $budget12, $flags));
227 # Restriction is 'owner and users'
228 ok
(!CanUserModifyBudget
($borrower1, $budget13, $flags)); # No owner, no user
229 ok
(CanUserModifyBudget
($borrower1, $budget14, $flags));
230 ok
(CanUserModifyBudget
($borrower1, $budget15, $flags));
231 ok
(CanUserModifyBudget
($borrower1, $budget16, $flags));
232 ok
(!CanUserModifyBudget
($borrower2, $budget13, $flags)); # No owner, no user
233 ok
(!CanUserModifyBudget
($borrower2, $budget14, $flags)); # No owner and user list contains borrower1
234 ok
(CanUserModifyBudget
($borrower2, $budget15, $flags));
235 ok
(!CanUserModifyBudget
($borrower2, $budget16, $flags));
238 # Same tests as above, without budget_modify permission
239 # All tests should failed
240 $flags = {acquisition
=> {order_manage
=> 1}};
242 $userenv->{branch
} = 'B1';
244 # Restriction is 'none'
245 ok
(!CanUserModifyBudget
($borrower1, $budget1, $flags));
246 ok
(!CanUserModifyBudget
($borrower1, $budget2, $flags));
247 ok
(!CanUserModifyBudget
($borrower1, $budget3, $flags));
248 ok
(!CanUserModifyBudget
($borrower1, $budget4, $flags));
249 ok
(!CanUserModifyBudget
($borrower2, $budget1, $flags));
250 ok
(!CanUserModifyBudget
($borrower2, $budget2, $flags));
251 ok
(!CanUserModifyBudget
($borrower2, $budget3, $flags));
252 ok
(!CanUserModifyBudget
($borrower2, $budget4, $flags));
254 # Restriction is 'owner'
255 ok
(!CanUserModifyBudget
($borrower1, $budget5, $flags));
256 ok
(!CanUserModifyBudget
($borrower1, $budget6, $flags));
257 ok
(!CanUserModifyBudget
($borrower1, $budget7, $flags));
258 ok
(!CanUserModifyBudget
($borrower1, $budget8, $flags));
259 ok
(!CanUserModifyBudget
($borrower2, $budget5, $flags));
260 ok
(!CanUserModifyBudget
($borrower2, $budget6, $flags));
261 ok
(!CanUserModifyBudget
($borrower2, $budget7, $flags));
262 ok
(!CanUserModifyBudget
($borrower2, $budget8, $flags));
264 # Restriction is 'owner, users and library'
265 ok
(!CanUserModifyBudget
($borrower1, $budget9, $flags));
266 ok
(!CanUserModifyBudget
($borrower1, $budget10, $flags));
267 ok
(!CanUserModifyBudget
($borrower1, $budget11, $flags));
268 ok
(!CanUserModifyBudget
($borrower1, $budget12, $flags));
269 ok
(!CanUserModifyBudget
($borrower2, $budget9, $flags));
270 ok
(!CanUserModifyBudget
($borrower2, $budget10, $flags));
271 ok
(!CanUserModifyBudget
($borrower2, $budget11, $flags));
272 ok
(!CanUserModifyBudget
($borrower2, $budget12, $flags));
274 # Restriction is 'owner and users'
275 ok
(!CanUserModifyBudget
($borrower1, $budget13, $flags));
276 ok
(!CanUserModifyBudget
($borrower1, $budget14, $flags));
277 ok
(!CanUserModifyBudget
($borrower1, $budget15, $flags));
278 ok
(!CanUserModifyBudget
($borrower1, $budget16, $flags));
279 ok
(!CanUserModifyBudget
($borrower2, $budget13, $flags));
280 ok
(!CanUserModifyBudget
($borrower2, $budget14, $flags));
281 ok
(!CanUserModifyBudget
($borrower2, $budget15, $flags));
282 ok
(!CanUserModifyBudget
($borrower2, $budget16, $flags));
285 $userenv->{branch
} = 'B2';
287 # Restriction is 'none'
288 ok
(!CanUserModifyBudget
($borrower1, $budget1, $flags));
289 ok
(!CanUserModifyBudget
($borrower1, $budget2, $flags));
290 ok
(!CanUserModifyBudget
($borrower1, $budget3, $flags));
291 ok
(!CanUserModifyBudget
($borrower1, $budget4, $flags));
292 ok
(!CanUserModifyBudget
($borrower2, $budget1, $flags));
293 ok
(!CanUserModifyBudget
($borrower2, $budget2, $flags));
294 ok
(!CanUserModifyBudget
($borrower2, $budget3, $flags));
295 ok
(!CanUserModifyBudget
($borrower2, $budget4, $flags));
297 # Restriction is 'owner'
298 ok
(!CanUserModifyBudget
($borrower1, $budget5, $flags));
299 ok
(!CanUserModifyBudget
($borrower1, $budget6, $flags));
300 ok
(!CanUserModifyBudget
($borrower1, $budget7, $flags));
301 ok
(!CanUserModifyBudget
($borrower1, $budget8, $flags));
302 ok
(!CanUserModifyBudget
($borrower2, $budget5, $flags));
303 ok
(!CanUserModifyBudget
($borrower2, $budget6, $flags));
304 ok
(!CanUserModifyBudget
($borrower2, $budget7, $flags));
305 ok
(!CanUserModifyBudget
($borrower2, $budget8, $flags));
307 # Restriction is 'owner, users and library'
308 ok
(!CanUserModifyBudget
($borrower1, $budget9, $flags));
309 ok
(!CanUserModifyBudget
($borrower1, $budget10, $flags));
310 ok
(!CanUserModifyBudget
($borrower1, $budget11, $flags));
311 ok
(!CanUserModifyBudget
($borrower1, $budget12, $flags));
312 ok
(!CanUserModifyBudget
($borrower2, $budget9, $flags));
313 ok
(!CanUserModifyBudget
($borrower2, $budget10, $flags));
314 ok
(!CanUserModifyBudget
($borrower2, $budget11, $flags));
315 ok
(!CanUserModifyBudget
($borrower2, $budget12, $flags));
317 # Restriction is 'owner and users'
318 ok
(!CanUserModifyBudget
($borrower1, $budget13, $flags));
319 ok
(!CanUserModifyBudget
($borrower1, $budget14, $flags));
320 ok
(!CanUserModifyBudget
($borrower1, $budget15, $flags));
321 ok
(!CanUserModifyBudget
($borrower1, $budget16, $flags));
322 ok
(!CanUserModifyBudget
($borrower2, $budget13, $flags));
323 ok
(!CanUserModifyBudget
($borrower2, $budget14, $flags));
324 ok
(!CanUserModifyBudget
($borrower2, $budget15, $flags));
325 ok
(!CanUserModifyBudget
($borrower2, $budget16, $flags));
330 # C4::Acquisition::GetBudgetUsers
331 sub Mock_GetBudgetUsers
{
332 my ($budget_id) = @_;
334 return @
{ $budgetusers{$budget_id} };
337 # C4::Context->userenv