Bug 17676: Update default COLLATE of marc_subfield_structure
[koha.git] / t / Budgets / CanUserModifyBudget.t
blobbef423708121cab7e2631fc8636edfba93a043aa
1 #!/usr/bin/perl
3 use Modern::Perl;
4 use Test::More tests => 133;
6 use C4::Budgets;
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;
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 my $userenv = {};
139 ok (CanUserModifyBudget($borrower1, $budget1, {superlibrarian => 1}));
140 ok (CanUserModifyBudget($borrower1, $budget1, {
141 acquisition => {
142 budget_manage_all => 1
144 }));
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));
328 # Mocked subs
330 # C4::Acquisition::GetBudgetUsers
331 sub Mock_GetBudgetUsers {
332 my ($budget_id) = @_;
334 return @{ $budgetusers{$budget_id} };
337 # C4::Context->userenv
338 sub Mock_userenv {
339 return $userenv;