Bug 12164: Move the budget period clone logic into C4::Budgets
[koha.git] / t / Budgets / CanUserUseBudget.t
blobca94065319a4f7e89576ab14f6be5dcd835ff6a6
1 #!/usr/bin/perl
3 use Modern::Perl;
4 use Test::More tests => 69;
6 use C4::Budgets;
8 # Avoid "redefined subroutine" warnings
9 local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
11 *C4::Budgets::GetBudgetUsers = \&Mock_GetBudgetUsers;
12 *C4::Context::userenv = \&Mock_userenv;
14 my %budgetusers = (
15 1 => [],
16 2 => [1],
17 3 => [2],
18 4 => [3],
19 5 => [],
20 6 => [1],
21 7 => [2],
22 8 => [3],
23 9 => [],
24 10 => [1],
25 11 => [2],
26 12 => [3],
27 13 => [],
28 14 => [1],
29 15 => [2],
30 16 => [3],
33 my $borrower1 = {
34 borrowernumber => 1
36 my $borrower2 = {
37 borrowernumber => 2
40 my $budget1 = {
41 budget_id => 1,
42 budget_permission => 0,
43 budget_owner_id => undef,
44 budget_branchcode => undef,
46 my $budget2 = {
47 budget_id => 2,
48 budget_permission => 0,
49 budget_owner_id => undef,
50 budget_branchcode => 'B1',
52 my $budget3 = {
53 budget_id => 3,
54 budget_permission => 0,
55 budget_owner_id => 1,
56 budget_branchcode => undef,
58 my $budget4 = {
59 budget_id => 4,
60 budget_permission => 0,
61 budget_owner_id => 1,
62 budget_branchcode => 'B1',
64 my $budget5 = {
65 budget_id => 5,
66 budget_permission => 1,
67 budget_owner_id => undef,
68 budget_branchcode => undef,
70 my $budget6 = {
71 budget_id => 6,
72 budget_permission => 1,
73 budget_owner_id => undef,
74 budget_branchcode => 'B1',
76 my $budget7 = {
77 budget_id => 7,
78 budget_permission => 1,
79 budget_owner_id => 1,
80 budget_branchcode => undef,
82 my $budget8 = {
83 budget_id => 8,
84 budget_permission => 1,
85 budget_owner_id => 1,
86 budget_branchcode => 'B1',
88 my $budget9 = {
89 budget_id => 9,
90 budget_permission => 2,
91 budget_owner_id => undef,
92 budget_branchcode => undef,
94 my $budget10 = {
95 budget_id => 10,
96 budget_permission => 2,
97 budget_owner_id => undef,
98 budget_branchcode => 'B1',
100 my $budget11 = {
101 budget_id => 11,
102 budget_permission => 2,
103 budget_owner_id => 1,
104 budget_branchcode => undef,
106 my $budget12 = {
107 budget_id => 12,
108 budget_permission => 2,
109 budget_owner_id => 1,
110 budget_branchcode => 'B1',
112 my $budget13 = {
113 budget_id => 13,
114 budget_permission => 3,
115 budget_owner_id => undef,
116 budget_branchcode => undef,
118 my $budget14 = {
119 budget_id => 14,
120 budget_permission => 3,
121 budget_owner_id => undef,
122 budget_branchcode => 'B1',
124 my $budget15 = {
125 budget_id => 15,
126 budget_permission => 3,
127 budget_owner_id => 1,
128 budget_branchcode => undef,
130 my $budget16 = {
131 budget_id => 16,
132 budget_permission => 3,
133 budget_owner_id => 1,
134 budget_branchcode => 'B1',
137 my $userenv = {};
139 ok (CanUserUseBudget($borrower1, $budget1, {superlibrarian => 1}));
140 ok (CanUserUseBudget($borrower1, $budget1, {
141 acquisition => {
142 budget_manage_all => 1
144 }));
145 ok (CanUserUseBudget($borrower1, $budget1, {acquisition => 1}));
147 ok (!CanUserUseBudget($borrower1, $budget1, {}));
148 ok (!CanUserUseBudget($borrower1, $budget1, {acquisition => 0}));
150 my $flags = {acquisition => {order_manage => 1}};
152 $userenv->{branch} = 'B1';
154 # Restriction is 'none'
155 ok (CanUserUseBudget($borrower1, $budget1, $flags));
156 ok (CanUserUseBudget($borrower1, $budget2, $flags));
157 ok (CanUserUseBudget($borrower1, $budget3, $flags));
158 ok (CanUserUseBudget($borrower1, $budget4, $flags));
159 ok (CanUserUseBudget($borrower2, $budget1, $flags));
160 ok (CanUserUseBudget($borrower2, $budget2, $flags));
161 ok (CanUserUseBudget($borrower2, $budget3, $flags));
162 ok (CanUserUseBudget($borrower2, $budget4, $flags));
164 # Restriction is 'owner'
165 ok (CanUserUseBudget($borrower1, $budget5, $flags));
166 ok (CanUserUseBudget($borrower1, $budget6, $flags));
167 ok (CanUserUseBudget($borrower1, $budget7, $flags));
168 ok (CanUserUseBudget($borrower1, $budget8, $flags));
169 ok (CanUserUseBudget($borrower2, $budget5, $flags));
170 ok (CanUserUseBudget($borrower2, $budget6, $flags));
171 ok (!CanUserUseBudget($borrower2, $budget7, $flags));
172 ok (!CanUserUseBudget($borrower2, $budget8, $flags));
174 # Restriction is 'owner, users and library'
175 ok (CanUserUseBudget($borrower1, $budget9, $flags));
176 ok (CanUserUseBudget($borrower1, $budget10, $flags));
177 ok (CanUserUseBudget($borrower1, $budget11, $flags));
178 ok (CanUserUseBudget($borrower1, $budget12, $flags));
179 ok (CanUserUseBudget($borrower2, $budget9, $flags));
180 ok (CanUserUseBudget($borrower2, $budget10, $flags));
181 ok (CanUserUseBudget($borrower2, $budget11, $flags));
182 ok (CanUserUseBudget($borrower2, $budget12, $flags));
184 # Restriction is 'owner and users'
185 ok (!CanUserUseBudget($borrower1, $budget13, $flags)); # No user, no owner
186 ok (CanUserUseBudget($borrower1, $budget14, $flags));
187 ok (CanUserUseBudget($borrower1, $budget15, $flags));
188 ok (CanUserUseBudget($borrower1, $budget16, $flags));
189 ok (!CanUserUseBudget($borrower2, $budget13, $flags)); # No user, no owner
190 ok (!CanUserUseBudget($borrower2, $budget14, $flags)); # No owner and user list contains borrower1
191 ok (CanUserUseBudget($borrower2, $budget15, $flags));
192 ok (!CanUserUseBudget($borrower2, $budget16, $flags));
195 $userenv->{branch} = 'B2';
197 # Restriction is 'none'
198 ok (CanUserUseBudget($borrower1, $budget1, $flags));
199 ok (CanUserUseBudget($borrower1, $budget2, $flags));
200 ok (CanUserUseBudget($borrower1, $budget3, $flags));
201 ok (CanUserUseBudget($borrower1, $budget4, $flags));
202 ok (CanUserUseBudget($borrower2, $budget1, $flags));
203 ok (CanUserUseBudget($borrower2, $budget2, $flags));
204 ok (CanUserUseBudget($borrower2, $budget3, $flags));
205 ok (CanUserUseBudget($borrower2, $budget4, $flags));
207 # Restriction is 'owner'
208 ok (CanUserUseBudget($borrower1, $budget5, $flags));
209 ok (CanUserUseBudget($borrower1, $budget6, $flags));
210 ok (CanUserUseBudget($borrower1, $budget7, $flags));
211 ok (CanUserUseBudget($borrower1, $budget8, $flags));
212 ok (CanUserUseBudget($borrower2, $budget5, $flags));
213 ok (CanUserUseBudget($borrower2, $budget6, $flags));
214 ok (!CanUserUseBudget($borrower2, $budget7, $flags));
215 ok (!CanUserUseBudget($borrower2, $budget8, $flags));
217 # Restriction is 'owner, users and library'
218 ok (CanUserUseBudget($borrower1, $budget9, $flags));
219 ok (CanUserUseBudget($borrower1, $budget10, $flags));
220 ok (CanUserUseBudget($borrower1, $budget11, $flags));
221 ok (CanUserUseBudget($borrower1, $budget12, $flags));
222 ok (CanUserUseBudget($borrower2, $budget9, $flags));
223 ok (!CanUserUseBudget($borrower2, $budget10, $flags)); # Limited to library B1
224 ok (CanUserUseBudget($borrower2, $budget11, $flags));
225 ok (!CanUserUseBudget($borrower2, $budget12, $flags));
227 # Restriction is 'owner and users'
228 ok (!CanUserUseBudget($borrower1, $budget13, $flags)); # No owner, no user
229 ok (CanUserUseBudget($borrower1, $budget14, $flags));
230 ok (CanUserUseBudget($borrower1, $budget15, $flags));
231 ok (CanUserUseBudget($borrower1, $budget16, $flags));
232 ok (!CanUserUseBudget($borrower2, $budget13, $flags)); # No owner, no user
233 ok (!CanUserUseBudget($borrower2, $budget14, $flags)); # No owner and user list contains borrower1
234 ok (CanUserUseBudget($borrower2, $budget15, $flags));
235 ok (!CanUserUseBudget($borrower2, $budget16, $flags));
238 # Mocked subs
240 # C4::Acquisition::GetBudgetUsers
241 sub Mock_GetBudgetUsers {
242 my ($budget_id) = @_;
244 return @{ $budgetusers{$budget_id} };
247 # C4::Context->userenv
248 sub Mock_userenv {
249 return $userenv;