4 use Test
::More tests
=> 134;
9 # Avoid "redefined subroutine" warnings
10 local $SIG{__WARN__
} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
11 *C4
::Budgets
::GetBudgetUsers
= \
&Mock_GetBudgetUsers
;
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',
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, {
141 budget_manage_all
=> 1
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));
328 # C4::Acquisition::GetBudgetUsers
329 sub Mock_GetBudgetUsers
{
330 my ($budget_id) = @_;
332 return @
{ $budgetusers{$budget_id} };