Bug 19382: (follow-up) Alternate tests
[koha.git] / t / Acquisition / CanUserManageBasket.t
bloba862983f702fbb4c9f38efe46a2640551744f282
1 #!/usr/bin/perl
3 use Modern::Perl;
4 use Test::More tests => 43;
6 use C4::Acquisition;
8 # Avoid "redefined subroutine" warnings
9 local $SIG{__WARN__} = sub { warn $_[0] unless $_[0] =~ /redefined/ };
11 *C4::Acquisition::GetBasketUsers = \&Mock_GetBasketUsers;
12 *C4::Context::preference = \&Mock_preference;
14 my $AcqViewBaskets;
15 my %basketusers = (
16 1 => [],
17 2 => [1, 2],
18 3 => [],
19 4 => [1, 2],
22 # Borrowers
23 my $borrower1 = {
24 borrowernumber => 1,
25 branchcode => 'B1',
28 my $borrower2 = {
29 borrowernumber => 2,
30 branchcode => 'B2',
33 my $borrower3 = {
34 borrowernumber => 3,
35 branchcode => 'B3',
38 # Baskets
39 my $basket1 = {
40 basketno => 1,
41 authorisedby => 1
44 my $basket2 = {
45 basketno => 2,
46 authorisedby => 2,
49 my $basket3 = {
50 basketno => 3,
51 authorisedby => 3,
52 branch => 'B1'
55 my $basket4 = {
56 basketno => 4,
57 authorisedby => 4,
58 branch => 'B1'
62 my $flags = {
63 acquisition => {
64 order_manage => 1
68 #################
69 # Start testing #
70 #################
72 # ----------------------
73 # AcqViewBaskets = 'all'
74 # ----------------------
76 $AcqViewBaskets = 'all';
78 # Simple cases where user can't manage basket
79 ok( not CanUserManageBasket($borrower1, $basket1, {}) );
80 ok( not CanUserManageBasket($borrower1, $basket1, {
81 acquisition => 0
82 }) );
83 ok( not CanUserManageBasket($borrower1, $basket1, {
84 acquisition => {
85 order_manage => 0
87 }) );
89 # Simple cases where user can manage basket
90 ok( CanUserManageBasket($borrower1, $basket1, {
91 superlibrarian => 1
92 }) );
93 ok( CanUserManageBasket($borrower1, $basket1, {
94 acquisition => 1
95 }) );
96 ok( CanUserManageBasket($borrower1, $basket1, {
97 acquisition => {
98 order_manage_all => 1
100 }) );
102 ok( CanUserManageBasket($borrower1, $basket1, $flags) );
103 ok( CanUserManageBasket($borrower1, $basket2, $flags) );
104 ok( CanUserManageBasket($borrower1, $basket3, $flags) );
105 ok( CanUserManageBasket($borrower1, $basket4, $flags) );
106 ok( CanUserManageBasket($borrower2, $basket1, $flags) );
107 ok( CanUserManageBasket($borrower2, $basket2, $flags) );
108 ok( CanUserManageBasket($borrower2, $basket3, $flags) );
109 ok( CanUserManageBasket($borrower2, $basket4, $flags) );
111 # -------------------------
112 # AcqViewBaskets = 'branch'
113 # -------------------------
115 $AcqViewBaskets = 'branch';
117 # Simple cases where user can't manage basket
118 ok( not CanUserManageBasket($borrower1, $basket1, {}) );
119 ok( not CanUserManageBasket($borrower1, $basket1, {
120 acquisition => 0
121 }) );
122 ok( not CanUserManageBasket($borrower1, $basket1, {
123 acquisition => {
124 order_manage => 0
126 }) );
128 # Simple cases where user can manage basket
129 ok( CanUserManageBasket($borrower1, $basket1, {
130 superlibrarian => 1
131 }) );
132 ok( CanUserManageBasket($borrower1, $basket1, {
133 acquisition => 1
134 }) );
135 ok( CanUserManageBasket($borrower1, $basket1, {
136 acquisition => {
137 order_manage_all => 1
139 }) );
141 ok( CanUserManageBasket($borrower1, $basket1, $flags) );
142 ok( CanUserManageBasket($borrower1, $basket2, $flags) );
143 ok( CanUserManageBasket($borrower1, $basket3, $flags) );
144 ok( CanUserManageBasket($borrower1, $basket4, $flags) );
145 ok( CanUserManageBasket($borrower2, $basket1, $flags) );
146 ok( CanUserManageBasket($borrower2, $basket2, $flags) );
147 # borrower2 is not on the same branch as basket3 and basket4
148 ok( not CanUserManageBasket($borrower2, $basket3, $flags) );
149 ok( not CanUserManageBasket($borrower2, $basket4, $flags) );
151 # -----------------------
152 # AcqViewBaskets = 'user'
153 # -----------------------
155 $AcqViewBaskets = 'user';
157 # Simple cases where user can't manage basket
158 ok( not CanUserManageBasket($borrower1, $basket1, {}) );
159 ok( not CanUserManageBasket($borrower1, $basket1, {
160 acquisition => 0
161 }) );
162 ok( not CanUserManageBasket($borrower1, $basket1, {
163 acquisition => {
164 order_manage => 0
166 }) );
168 # Simple cases where user can manage basket
169 ok( CanUserManageBasket($borrower1, $basket1, {
170 superlibrarian => 1
171 }) );
172 ok( CanUserManageBasket($borrower1, $basket1, {
173 acquisition => 1
174 }) );
175 ok( CanUserManageBasket($borrower1, $basket1, {
176 acquisition => {
177 order_manage_all => 1
179 }) );
181 ok( CanUserManageBasket($borrower1, $basket1, $flags) );
182 ok( CanUserManageBasket($borrower1, $basket2, $flags) );
183 # basket3 is not managed or created by borrower1
184 ok( not CanUserManageBasket($borrower1, $basket3, $flags) );
185 ok( CanUserManageBasket($borrower1, $basket4, $flags) );
186 # basket 1 is not managed or created by borrower2
187 ok( not CanUserManageBasket($borrower2, $basket1, $flags) );
188 ok( CanUserManageBasket($borrower2, $basket2, $flags) );
189 # basket 3 is not managed or created by borrower2
190 ok( not CanUserManageBasket($borrower2, $basket3, $flags) );
191 ok( CanUserManageBasket($borrower2, $basket4, $flags) );
193 # basket 4 is not managed by borrower3 but by other borrowers
194 ok( not CanUserManageBasket($borrower3, $basket4, $flags) );
196 # Mocked subs
198 # C4::Acquisition::GetBasketUsers
199 sub Mock_GetBasketUsers {
200 my ($basketno) = @_;
202 return @{ $basketusers{$basketno} };
205 # C4::Context->preference
206 sub Mock_preference {
207 my ($self, $variable) = @_;
208 if (lc($variable) eq 'acqviewbaskets') {
209 return $AcqViewBaskets;