Lines 1-459
Link Here
|
1 |
#!/usr/bin/perl |
|
|
2 |
|
3 |
# This file is part of Koha. |
4 |
# |
5 |
# Koha is free software; you can redistribute it and/or modify it |
6 |
# under the terms of the GNU General Public License as published by |
7 |
# the Free Software Foundation; either version 3 of the License, or |
8 |
# (at your option) any later version. |
9 |
# |
10 |
# Koha is distributed in the hope that it will be useful, but |
11 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
# GNU General Public License for more details. |
14 |
# |
15 |
# You should have received a copy of the GNU General Public License |
16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
17 |
|
18 |
use Modern::Perl; |
19 |
|
20 |
use Test::More tests => 9; |
21 |
use t::lib::Mocks; |
22 |
use t::lib::TestBuilder; |
23 |
|
24 |
use C4::Context; |
25 |
use Koha::Database; |
26 |
|
27 |
BEGIN { |
28 |
use_ok('Koha::Object'); |
29 |
use_ok('Koha::CirculationRule'); |
30 |
use_ok('Koha::RefundLostItemFeeRules'); |
31 |
} |
32 |
|
33 |
my $schema = Koha::Database->new->schema; |
34 |
my $builder = t::lib::TestBuilder->new; |
35 |
|
36 |
subtest 'Koha::RefundLostItemFeeRule::delete() tests' => sub { |
37 |
|
38 |
plan tests => 5; |
39 |
|
40 |
# Start transaction |
41 |
$schema->storage->txn_begin; |
42 |
|
43 |
# Clean the table |
44 |
$schema->resultset('CirculationRule')->search()->delete; |
45 |
|
46 |
my $generated_default_rule = $builder->build( |
47 |
{ |
48 |
source => 'CirculationRule', |
49 |
value => { |
50 |
branchcode => undef, |
51 |
categorycode => undef, |
52 |
itemtype => undef, |
53 |
rule_name => 'refund', |
54 |
} |
55 |
} |
56 |
); |
57 |
my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode}; |
58 |
my $generated_other_rule = $builder->build( |
59 |
{ |
60 |
source => 'CirculationRule', |
61 |
value => { |
62 |
branchcode => $branchcode, |
63 |
categorycode => undef, |
64 |
itemtype => undef, |
65 |
rule_name => 'refund', |
66 |
} |
67 |
} |
68 |
); |
69 |
|
70 |
my $default_rule = Koha::CirculationRules->search( |
71 |
{ |
72 |
branchcode => undef, |
73 |
categorycode => undef, |
74 |
itemtype => undef, |
75 |
rule_name => 'refund', |
76 |
} |
77 |
)->next(); |
78 |
ok( defined $default_rule, 'Default rule created' ); |
79 |
ok( $default_rule->_result->in_storage, 'Default rule actually in storage'); |
80 |
|
81 |
my $other_rule = Koha::CirculationRules->search( |
82 |
{ |
83 |
branchcode => $generated_other_rule->{branchcode}, |
84 |
categorycode => undef, |
85 |
itemtype => undef, |
86 |
rule_name => 'refund', |
87 |
} |
88 |
)->next(); |
89 |
ok( defined $other_rule, 'Other rule created' ); |
90 |
ok( $other_rule->_result->in_storage, 'Other rule actually in storage'); |
91 |
|
92 |
# deleting the regular rule |
93 |
$other_rule->delete; |
94 |
ok( !$other_rule->_result->in_storage, 'Other rule deleted from storage' ); |
95 |
|
96 |
# Rollback transaction |
97 |
$schema->storage->txn_rollback; |
98 |
}; |
99 |
|
100 |
subtest 'Koha::RefundLostItemFeeRules::_default_rule() tests' => sub { |
101 |
|
102 |
plan tests => 6; |
103 |
|
104 |
# Start transaction |
105 |
$schema->storage->txn_begin; |
106 |
|
107 |
# Clean the table |
108 |
$schema->resultset('CirculationRule')->search()->delete; |
109 |
|
110 |
my $generated_default_rule = $builder->build( |
111 |
{ |
112 |
source => 'CirculationRule', |
113 |
value => { |
114 |
branchcode => undef, |
115 |
categorycode => undef, |
116 |
itemtype => undef, |
117 |
rule_name => 'refund', |
118 |
rule_value => 1, |
119 |
} |
120 |
} |
121 |
); |
122 |
my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode}; |
123 |
my $generated_other_rule = $builder->build( |
124 |
{ |
125 |
source => 'CirculationRule', |
126 |
value => { |
127 |
branchcode => $branchcode, |
128 |
categorycode => undef, |
129 |
itemtype => undef, |
130 |
rule_name => 'refund', |
131 |
} |
132 |
} |
133 |
); |
134 |
|
135 |
my $default_rule = Koha::CirculationRules->search( |
136 |
{ |
137 |
branchcode => undef, |
138 |
categorycode => undef, |
139 |
itemtype => undef, |
140 |
rule_name => 'refund', |
141 |
} |
142 |
)->next(); |
143 |
ok( defined $default_rule, 'Default rule created' ); |
144 |
ok( $default_rule->_result->in_storage, 'Default rule actually in storage'); |
145 |
is( Koha::RefundLostItemFeeRules->_default_rule, 1, 'Default rule is set to refund' ); |
146 |
|
147 |
# Change default rule to "Don't refund" |
148 |
$default_rule->rule_value(0); |
149 |
$default_rule->store; |
150 |
# Re-read from DB, to be sure |
151 |
$default_rule = Koha::CirculationRules->search( |
152 |
{ |
153 |
branchcode => undef, |
154 |
categorycode => undef, |
155 |
itemtype => undef, |
156 |
rule_name => 'refund', |
157 |
} |
158 |
)->next(); |
159 |
ok( !Koha::RefundLostItemFeeRules->_default_rule, 'Default rule is set to not refund' ); |
160 |
|
161 |
$default_rule->delete; |
162 |
ok( !$default_rule->_result->in_storage, 'Default rule effectively deleted from storage' ); |
163 |
|
164 |
ok( Koha::RefundLostItemFeeRules->_default_rule, 'Default rule is set to refund if no default rule is present' ); |
165 |
|
166 |
# Rollback transaction |
167 |
$schema->storage->txn_rollback; |
168 |
}; |
169 |
|
170 |
subtest 'Koha::RefundLostItemFeeRules::_effective_branch_rule() tests' => sub { |
171 |
|
172 |
plan tests => 3; |
173 |
|
174 |
# Start transaction |
175 |
$schema->storage->txn_begin; |
176 |
|
177 |
# Clean the table |
178 |
$schema->resultset('CirculationRule')->search()->delete; |
179 |
|
180 |
my $default_rule = $builder->build( |
181 |
{ |
182 |
source => 'CirculationRule', |
183 |
value => { |
184 |
branchcode => undef, |
185 |
categorycode => undef, |
186 |
itemtype => undef, |
187 |
rule_name => 'refund', |
188 |
rule_value => 1, |
189 |
} |
190 |
} |
191 |
); |
192 |
my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode}; |
193 |
my $specific_rule_false = $builder->build( |
194 |
{ |
195 |
source => 'CirculationRule', |
196 |
value => { |
197 |
branchcode => $branchcode, |
198 |
categorycode => undef, |
199 |
itemtype => undef, |
200 |
rule_name => 'refund', |
201 |
rule_value => 0, |
202 |
} |
203 |
} |
204 |
); |
205 |
my $branchcode2 = $builder->build( { source => 'Branch' } )->{branchcode}; |
206 |
my $specific_rule_true = $builder->build( |
207 |
{ |
208 |
source => 'CirculationRule', |
209 |
value => { |
210 |
branchcode => $branchcode2, |
211 |
categorycode => undef, |
212 |
itemtype => undef, |
213 |
rule_name => 'refund', |
214 |
rule_value => 1, |
215 |
} |
216 |
} |
217 |
); |
218 |
|
219 |
is( Koha::RefundLostItemFeeRules->_effective_branch_rule( $specific_rule_true->{ branchcode } ), |
220 |
1,'Specific rule is applied (true)'); |
221 |
is( Koha::RefundLostItemFeeRules->_effective_branch_rule( $specific_rule_false->{ branchcode } ), |
222 |
0,'Specific rule is applied (false)'); |
223 |
# Delete specific rules |
224 |
Koha::RefundLostItemFeeRules->find({ branchcode => $specific_rule_false->{ branchcode } })->delete; |
225 |
is( Koha::RefundLostItemFeeRules->_effective_branch_rule( $specific_rule_false->{ branchcode } ), |
226 |
1,'No specific rule defined, fallback to global (true)'); |
227 |
|
228 |
# Rollback transaction |
229 |
$schema->storage->txn_rollback; |
230 |
}; |
231 |
|
232 |
subtest 'Koha::RefundLostItemFeeRules::_choose_branch() tests' => sub { |
233 |
|
234 |
plan tests => 9; |
235 |
|
236 |
# Start transaction |
237 |
$schema->storage->txn_begin; |
238 |
|
239 |
my $params = { |
240 |
current_branch => 'current_branch_code', |
241 |
item_holding_branch => 'item_holding_branch_code', |
242 |
item_home_branch => 'item_home_branch_code' |
243 |
}; |
244 |
|
245 |
t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' ); |
246 |
|
247 |
is( Koha::RefundLostItemFeeRules->_choose_branch( $params ), |
248 |
'current_branch_code', 'CheckinLibrary is honoured'); |
249 |
|
250 |
t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHomeBranch' ); |
251 |
is( Koha::RefundLostItemFeeRules->_choose_branch( $params ), |
252 |
'item_home_branch_code', 'ItemHomeBranch is honoured'); |
253 |
|
254 |
t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHoldingBranch' ); |
255 |
is( Koha::RefundLostItemFeeRules->_choose_branch( $params ), |
256 |
'item_holding_branch_code', 'ItemHoldingBranch is honoured'); |
257 |
|
258 |
t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' ); |
259 |
eval { |
260 |
Koha::RefundLostItemFeeRules->_choose_branch(); |
261 |
}; |
262 |
is( ref($@), 'Koha::Exceptions::MissingParameter', |
263 |
'Missing parameter exception' ); |
264 |
is( $@->message, 'CheckinLibrary requires the current_branch param', |
265 |
'Exception message is correct' ); |
266 |
|
267 |
t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHomeBranch' ); |
268 |
eval { |
269 |
Koha::RefundLostItemFeeRules->_choose_branch(); |
270 |
}; |
271 |
is( ref($@), 'Koha::Exceptions::MissingParameter', |
272 |
'Missing parameter exception' ); |
273 |
is( $@->message, 'ItemHomeBranch requires the item_home_branch param', |
274 |
'Exception message is correct' ); |
275 |
|
276 |
t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHoldingBranch' ); |
277 |
eval { |
278 |
Koha::RefundLostItemFeeRules->_choose_branch(); |
279 |
}; |
280 |
is( ref($@), 'Koha::Exceptions::MissingParameter', |
281 |
'Missing parameter exception' ); |
282 |
is( $@->message, 'ItemHoldingBranch requires the item_holding_branch param', |
283 |
'Exception message is correct' ); |
284 |
|
285 |
# Rollback transaction |
286 |
$schema->storage->txn_rollback; |
287 |
}; |
288 |
|
289 |
subtest 'Koha::RefundLostItemFeeRules::should_refund() tests' => sub { |
290 |
|
291 |
plan tests => 3; |
292 |
|
293 |
# Start transaction |
294 |
$schema->storage->txn_begin; |
295 |
|
296 |
t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' ); |
297 |
|
298 |
$schema->resultset('CirculationRule')->search()->delete; |
299 |
|
300 |
my $default_rule = $builder->build( |
301 |
{ |
302 |
source => 'CirculationRule', |
303 |
value => { |
304 |
branchcode => undef, |
305 |
categorycode => undef, |
306 |
itemtype => undef, |
307 |
rule_name => 'refund', |
308 |
rule_value => 1 |
309 |
} |
310 |
} |
311 |
); |
312 |
my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode}; |
313 |
my $specific_rule_false = $builder->build( |
314 |
{ |
315 |
source => 'CirculationRule', |
316 |
value => { |
317 |
branchcode => $branchcode, |
318 |
categorycode => undef, |
319 |
itemtype => undef, |
320 |
rule_name => 'refund', |
321 |
rule_value => 0 |
322 |
} |
323 |
} |
324 |
); |
325 |
my $branchcode2 = $builder->build( { source => 'Branch' } )->{branchcode}; |
326 |
my $specific_rule_true = $builder->build( |
327 |
{ |
328 |
source => 'CirculationRule', |
329 |
value => { |
330 |
branchcode => $branchcode2, |
331 |
categorycode => undef, |
332 |
itemtype => undef, |
333 |
rule_name => 'refund', |
334 |
rule_value => 1 |
335 |
} |
336 |
} |
337 |
); |
338 |
# Make sure we have an unused branchcode |
339 |
my $branchcode3 = $builder->build( { source => 'Branch' } )->{branchcode}; |
340 |
my $specific_rule_dummy = $builder->build( |
341 |
{ |
342 |
source => 'CirculationRule', |
343 |
value => { |
344 |
branchcode => $branchcode3, |
345 |
categorycode => undef, |
346 |
itemtype => undef, |
347 |
rule_name => 'refund', |
348 |
} |
349 |
} |
350 |
); |
351 |
my $branch_without_rule = $specific_rule_dummy->{ branchcode }; |
352 |
Koha::CirculationRules |
353 |
->search( |
354 |
{ |
355 |
branchcode => $branch_without_rule, |
356 |
categorycode => undef, |
357 |
itemtype => undef, |
358 |
rule_name => 'refund' |
359 |
} |
360 |
) |
361 |
->next |
362 |
->delete; |
363 |
|
364 |
my $params = { |
365 |
current_branch => $specific_rule_true->{ branchcode }, |
366 |
# patron_branch => $specific_rule_false->{ branchcode }, |
367 |
item_holding_branch => $branch_without_rule, |
368 |
item_home_branch => $branch_without_rule |
369 |
}; |
370 |
|
371 |
t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' ); |
372 |
is( Koha::RefundLostItemFeeRules->should_refund( $params ), |
373 |
1,'Specific rule is applied (true)'); |
374 |
|
375 |
t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHomeBranch' ); |
376 |
is( Koha::RefundLostItemFeeRules->should_refund( $params ), |
377 |
1,'No rule for branch, global rule applied (true)'); |
378 |
|
379 |
# Change the default value just to try |
380 |
Koha::CirculationRules->search({ branchcode => undef, rule_name => 'refund' })->next->rule_value(0)->store; |
381 |
t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHoldingBranch' ); |
382 |
is( Koha::RefundLostItemFeeRules->should_refund( $params ), |
383 |
0,'No rule for branch, global rule applied (false)'); |
384 |
|
385 |
# Rollback transaction |
386 |
$schema->storage->txn_rollback; |
387 |
}; |
388 |
|
389 |
subtest 'Koha::RefundLostItemFeeRules::find() tests' => sub { |
390 |
|
391 |
plan tests => 5; |
392 |
|
393 |
# Start transaction |
394 |
$schema->storage->txn_begin; |
395 |
|
396 |
t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' ); |
397 |
|
398 |
$schema->resultset('CirculationRule')->search()->delete; |
399 |
|
400 |
my $default_non_refund = $builder->build( |
401 |
{ |
402 |
source => 'CirculationRule', |
403 |
value => { |
404 |
branchcode => undef, |
405 |
categorycode => undef, |
406 |
itemtype => undef, |
407 |
rule_name => 'non_refund_rule', |
408 |
rule_value => 1 |
409 |
} |
410 |
} |
411 |
); |
412 |
|
413 |
ok(defined Koha::RefundLostItemFeeRules->find($default_non_refund->{id}), 'Find should continue to work when passed an id'); |
414 |
|
415 |
my $specific_non_refund = $builder->build( |
416 |
{ |
417 |
source => 'CirculationRule', |
418 |
value => { |
419 |
categorycode => undef, |
420 |
itemtype => undef, |
421 |
rule_name => 'non_refund_rule', |
422 |
rule_value => 0 |
423 |
} |
424 |
} |
425 |
); |
426 |
|
427 |
ok(!defined Koha::RefundLostItemFeeRules->find({ branchcode => undef }), 'Non refund default rules are not found'); |
428 |
ok(!defined Koha::RefundLostItemFeeRules->find({ branchcode => $specific_non_refund->{branchcode} }), 'Non refund specific rules are not found'); |
429 |
|
430 |
my $default_refund = $builder->build( |
431 |
{ |
432 |
source => 'CirculationRule', |
433 |
value => { |
434 |
branchcode => undef, |
435 |
categorycode => undef, |
436 |
itemtype => undef, |
437 |
rule_name => 'refund', |
438 |
rule_value => 1 |
439 |
} |
440 |
} |
441 |
); |
442 |
my $specific_refund = $builder->build( |
443 |
{ |
444 |
source => 'CirculationRule', |
445 |
value => { |
446 |
categorycode => undef, |
447 |
itemtype => undef, |
448 |
rule_name => 'refund', |
449 |
rule_value => 0 |
450 |
} |
451 |
} |
452 |
); |
453 |
|
454 |
ok(defined Koha::RefundLostItemFeeRules->find({ branchcode => undef }), 'Refund default rules are found'); |
455 |
ok(defined Koha::RefundLostItemFeeRules->find({ branchcode => $specific_refund->{branchcode} }), 'Refund specific rules are found'); |
456 |
|
457 |
# Rollback transaction |
458 |
$schema->storage->txn_rollback; |
459 |
}; |