3 # Copyright 2016 Koha-Suomi Oy
5 # This file is part of Koha
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
22 use Test::More tests => 3;
23 use Test::Deep qw( cmp_methods );
28 use Koha::CirculationRules;
30 use t::lib::TestBuilder;
33 my $schema = Koha::Database->new->schema;
34 $schema->storage->txn_begin;
36 my $builder = t::lib::TestBuilder->new;
38 subtest 'get_effective_issuing_rule' => sub {
41 my $categorycode = $builder->build({ source => 'Category' })->{'categorycode'};
42 my $itemtype = $builder->build({ source => 'Itemtype' })->{'itemtype'};
43 my $branchcode = $builder->build({ source => 'Branch' })->{'branchcode'};
45 subtest 'Call with undefined values' => sub {
49 Koha::CirculationRules->delete;
51 is(Koha::CirculationRules->search->count, 0, 'There are no issuing rules.');
52 # undef, undef, undef => 1
53 $rule = Koha::CirculationRules->get_effective_rule({
55 categorycode => undef,
60 is($rule, undef, 'When I attempt to get effective issuing rule by'
61 .' providing undefined values, then undef is returned.');
63 # undef, undef, undef => 2
65 Koha::CirculationRule->new(
68 categorycode => undef,
74 'Given I added an issuing rule branchcode => undef,'
75 .' categorycode => undef, itemtype => undef,');
76 $rule = Koha::CirculationRules->get_effective_rule({
78 categorycode => undef,
86 categorycode => undef,
91 'When I attempt to get effective'
92 .' issuing rule by providing undefined values, then the above one is'
97 subtest 'Get effective issuing rule in correct order' => sub {
101 Koha::CirculationRules->delete;
102 is(Koha::CirculationRules->search->count, 0, 'There are no issuing rules.');
103 $rule = Koha::CirculationRules->get_effective_rule({
104 branchcode => $branchcode,
105 categorycode => $categorycode,
106 itemtype => $itemtype,
109 is($rule, undef, 'When I attempt to get effective issuing rule, then undef'
112 # undef, undef, undef => 5
113 ok(Koha::CirculationRule->new({
115 categorycode => undef,
119 })->store, 'Given I added an issuing rule branchcode => undef, categorycode => undef, itemtype => undef,');
120 $rule = Koha::CirculationRules->get_effective_rule({
121 branchcode => $branchcode,
122 categorycode => $categorycode,
123 itemtype => $itemtype,
130 categorycode => undef,
135 'When I attempt to get effective issuing rule,'
136 .' then the above one is returned.'
139 # undef, undef, undef => 5
140 # undef, undef, $itemtype => 7
142 Koha::CirculationRule->new(
145 categorycode => undef,
146 itemtype => $itemtype,
151 "Given I added an issuing rule branchcode => undef, categorycode => undef, itemtype => $itemtype,"
153 $rule = Koha::CirculationRules->get_effective_rule({
154 branchcode => $branchcode,
155 categorycode => $categorycode,
156 itemtype => $itemtype,
163 categorycode => undef,
164 itemtype => $itemtype,
168 'When I attempt to get effective issuing rule,'
169 .' then the above one is returned.'
172 # undef, undef, undef => 5
173 # undef, undef, $itemtype => 7
174 # undef, $categorycode, undef => 9
176 Koha::CirculationRule->new(
179 categorycode => $categorycode,
185 "Given I added an issuing rule branchcode => undef, categorycode => $categorycode, itemtype => undef,"
187 $rule = Koha::CirculationRules->get_effective_rule({
188 branchcode => $branchcode,
189 categorycode => $categorycode,
190 itemtype => $itemtype,
197 categorycode => $categorycode,
202 'When I attempt to get effective issuing rule,'
203 .' then the above one is returned.'
206 # undef, undef, undef => 5
207 # undef, undef, $itemtype => 7
208 # undef, $categorycode, undef => 9
209 # undef, $categorycode, $itemtype => 11
211 Koha::CirculationRule->new(
214 categorycode => $categorycode,
215 itemtype => $itemtype,
220 "Given I added an issuing rule branchcode => undef, categorycode => $categorycode, itemtype => $itemtype,"
222 $rule = Koha::CirculationRules->get_effective_rule({
223 branchcode => $branchcode,
224 categorycode => $categorycode,
225 itemtype => $itemtype,
232 categorycode => $categorycode,
233 itemtype => $itemtype,
237 'When I attempt to get effective issuing rule,'
238 .' then the above one is returned.'
241 # undef, undef, undef => 5
242 # undef, undef, $itemtype => 7
243 # undef, $categorycode, undef => 9
244 # undef, $categorycode, $itemtype => 11
245 # $branchcode, undef, undef => 13
247 Koha::CirculationRule->new(
249 branchcode => $branchcode,
250 categorycode => undef,
256 "Given I added an issuing rule branchcode => $branchcode, categorycode => undef, itemtype => undef,"
258 $rule = Koha::CirculationRules->get_effective_rule({
259 branchcode => $branchcode,
260 categorycode => $categorycode,
261 itemtype => $itemtype,
267 branchcode => $branchcode,
268 categorycode => undef,
273 'When I attempt to get effective issuing rule,'
274 .' then the above one is returned.'
277 # undef, undef, undef => 5
278 # undef, undef, $itemtype => 7
279 # undef, $categorycode, undef => 9
280 # undef, $categorycode, $itemtype => 11
281 # $branchcode, undef, undef => 13
282 # $branchcode, undef, $itemtype => 15
284 Koha::CirculationRule->new(
286 branchcode => $branchcode,
287 categorycode => undef,
288 itemtype => $itemtype,
293 "Given I added an issuing rule branchcode => $branchcode, categorycode => undef, itemtype => $itemtype,"
295 $rule = Koha::CirculationRules->get_effective_rule({
296 branchcode => $branchcode,
297 categorycode => $categorycode,
298 itemtype => $itemtype,
304 branchcode => $branchcode,
305 categorycode => undef,
306 itemtype => $itemtype,
310 'When I attempt to get effective issuing rule,'
311 .' then the above one is returned.'
314 # undef, undef, undef => 5
315 # undef, undef, $itemtype => 7
316 # undef, $categorycode, undef => 9
317 # undef, $categorycode, $itemtype => 11
318 # $branchcode, undef, undef => 13
319 # $branchcode, undef, $itemtype => 15
320 # $branchcode, $categorycode, undef => 17
322 Koha::CirculationRule->new(
324 branchcode => $branchcode,
325 categorycode => $categorycode,
331 "Given I added an issuing rule branchcode => $branchcode, categorycode => $categorycode, itemtype => undef,"
333 $rule = Koha::CirculationRules->get_effective_rule({
334 branchcode => $branchcode,
335 categorycode => $categorycode,
336 itemtype => $itemtype,
342 branchcode => $branchcode,
343 categorycode => $categorycode,
348 'When I attempt to get effective issuing rule,'
349 .' then the above one is returned.'
352 # undef, undef, undef => 5
353 # undef, undef, $itemtype => 7
354 # undef, $categorycode, undef => 9
355 # undef, $categorycode, $itemtype => 11
356 # $branchcode, undef, undef => 13
357 # $branchcode, undef, $itemtype => 15
358 # $branchcode, $categorycode, undef => 17
359 # $branchcode, $categorycode, $itemtype => 19
361 Koha::CirculationRule->new(
363 branchcode => $branchcode,
364 categorycode => $categorycode,
365 itemtype => $itemtype,
370 "Given I added an issuing rule branchcode => $branchcode, categorycode => $categorycode, itemtype => $itemtype,"
372 $rule = Koha::CirculationRules->get_effective_rule({
373 branchcode => $branchcode,
374 categorycode => $categorycode,
375 itemtype => $itemtype,
381 branchcode => $branchcode,
382 categorycode => $categorycode,
383 itemtype => $itemtype,
387 'When I attempt to get effective issuing rule,'
388 .' then the above one is returned.'
392 subtest 'Performance' => sub {
395 my $worst_case = timethis(500,
396 sub { Koha::CirculationRules->get_effective_rule({
397 branchcode => 'nonexistent',
398 categorycode => 'nonexistent',
399 itemtype => 'nonexistent',
400 rule_name => 'nonexistent',
404 my $mid_case = timethis(500,
405 sub { Koha::CirculationRules->get_effective_rule({
406 branchcode => $branchcode,
407 categorycode => 'nonexistent',
408 itemtype => 'nonexistent',
409 rule_name => 'nonexistent',
413 my $sec_best_case = timethis(500,
414 sub { Koha::CirculationRules->get_effective_rule({
415 branchcode => $branchcode,
416 categorycode => $categorycode,
417 itemtype => 'nonexistent',
418 rule_name => 'nonexistent',
422 my $best_case = timethis(500,
423 sub { Koha::CirculationRules->get_effective_rule({
424 branchcode => $branchcode,
425 categorycode => $categorycode,
426 itemtype => $itemtype,
427 rule_name => 'nonexistent',
431 ok($worst_case, 'In worst case, get_effective_issuing_rule finds matching'
432 .' rule '.sprintf('%.2f', $worst_case->iters/$worst_case->cpu_a)
433 .' times per second.');
434 ok($mid_case, 'In mid case, get_effective_issuing_rule finds matching'
435 .' rule '.sprintf('%.2f', $mid_case->iters/$mid_case->cpu_a)
436 .' times per second.');
437 ok($sec_best_case, 'In second best case, get_effective_issuing_rule finds matching'
438 .' rule '.sprintf('%.2f', $sec_best_case->iters/$sec_best_case->cpu_a)
439 .' times per second.');
440 ok($best_case, 'In best case, get_effective_issuing_rule finds matching'
441 .' rule '.sprintf('%.2f', $best_case->iters/$best_case->cpu_a)
442 .' times per second.');
446 subtest 'set_rule' => sub {
449 my $branchcode = $builder->build({ source => 'Branch' })->{'branchcode'};
450 my $categorycode = $builder->build({ source => 'Category' })->{'categorycode'};
451 my $itemtype = $builder->build({ source => 'Itemtype' })->{'itemtype'};
453 subtest 'Correct call' => sub {
456 Koha::CirculationRules->delete;
459 Koha::CirculationRules->set_rule( {
460 branchcode => $branchcode,
461 rule_name => 'refund',
464 }, 'setting refund with branch' );
467 Koha::CirculationRules->set_rule( {
468 branchcode => $branchcode,
469 categorycode => $categorycode,
470 rule_name => 'patron_maxissueqty',
473 }, 'setting patron_maxissueqty with branch/category succeeds' );
476 Koha::CirculationRules->set_rule( {
477 branchcode => $branchcode,
478 itemtype => $itemtype,
479 rule_name => 'holdallowed',
482 }, 'setting holdallowed with branch/itemtype succeeds' );
485 Koha::CirculationRules->set_rule( {
486 branchcode => $branchcode,
487 categorycode => $categorycode,
488 itemtype => $itemtype,
492 }, 'setting fine with branch/category/itemtype succeeds' );
495 subtest 'Call with missing params' => sub {
498 Koha::CirculationRules->delete;
501 Koha::CirculationRules->set_rule( {
502 rule_name => 'refund',
505 }, qr/branchcode/, 'setting refund without branch fails' );
508 Koha::CirculationRules->set_rule( {
509 branchcode => $branchcode,
510 rule_name => 'patron_maxissueqty',
513 }, qr/categorycode/, 'setting patron_maxissueqty without categorycode fails' );
516 Koha::CirculationRules->set_rule( {
517 branchcode => $branchcode,
518 rule_name => 'holdallowed',
521 }, qr/itemtype/, 'setting holdallowed without itemtype fails' );
524 Koha::CirculationRules->set_rule( {
525 branchcode => $branchcode,
526 categorycode => $categorycode,
530 }, qr/itemtype/, 'setting fine without itemtype fails' );
533 subtest 'Call with extra params' => sub {
536 Koha::CirculationRules->delete;
539 Koha::CirculationRules->set_rule( {
540 branchcode => $branchcode,
541 categorycode => $categorycode,
542 rule_name => 'refund',
545 }, qr/categorycode/, 'setting refund with categorycode fails' );
548 Koha::CirculationRules->set_rule( {
549 branchcode => $branchcode,
550 categorycode => $categorycode,
551 itemtype => $itemtype,
552 rule_name => 'patron_maxissueqty',
555 }, qr/itemtype/, 'setting patron_maxissueqty with itemtype fails' );
558 Koha::CirculationRules->set_rule( {
559 branchcode => $branchcode,
560 rule_name => 'holdallowed',
561 categorycode => $categorycode,
562 itemtype => $itemtype,
565 }, qr/categorycode/, 'setting holdallowed with categorycode fails' );
569 subtest 'clone' => sub {
572 my $branchcode = $builder->build({ source => 'Branch' })->{'branchcode'};
573 my $categorycode = $builder->build({ source => 'Category' })->{'categorycode'};
574 my $itemtype = $builder->build({ source => 'Itemtype' })->{'itemtype'};
576 subtest 'Clone multiple rules' => sub {
579 Koha::CirculationRules->delete;
581 Koha::CirculationRule->new({
583 categorycode => $categorycode,
584 itemtype => $itemtype,
589 Koha::CirculationRule->new({
591 categorycode => $categorycode,
592 itemtype => $itemtype,
593 rule_name => 'lengthunit',
594 rule_value => 'days',
597 Koha::CirculationRules->search({ branchcode => undef })->clone($branchcode);
599 my $rule_fine = Koha::CirculationRules->get_effective_rule({
600 branchcode => $branchcode,
601 categorycode => $categorycode,
602 itemtype => $itemtype,
605 my $rule_lengthunit = Koha::CirculationRules->get_effective_rule({
606 branchcode => $branchcode,
607 categorycode => $categorycode,
608 itemtype => $itemtype,
609 rule_name => 'lengthunit',
615 branchcode => $branchcode,
616 categorycode => $categorycode,
617 itemtype => $itemtype,
621 'When I attempt to get cloned fine rule,'
622 .' then the above one is returned.'
627 branchcode => $branchcode,
628 categorycode => $categorycode,
629 itemtype => $itemtype,
630 rule_name => 'lengthunit',
631 rule_value => 'days',
633 'When I attempt to get cloned lengthunit rule,'
634 .' then the above one is returned.'
639 subtest 'Clone one rule' => sub {
642 Koha::CirculationRules->delete;
644 Koha::CirculationRule->new({
646 categorycode => $categorycode,
647 itemtype => $itemtype,
652 my $rule = Koha::CirculationRules->search({ branchcode => undef })->next;
653 $rule->clone($branchcode);
655 my $cloned_rule = Koha::CirculationRules->get_effective_rule({
656 branchcode => $branchcode,
657 categorycode => $categorycode,
658 itemtype => $itemtype,
665 branchcode => $branchcode,
666 categorycode => $categorycode,
667 itemtype => $itemtype,
671 'When I attempt to get cloned fine rule,'
672 .' then the above one is returned.'
679 my ( $rule, $expected, $message ) = @_;
681 ok( $rule, $message ) ?
682 cmp_methods( $rule, [ %$expected ], $message ) :
686 $schema->storage->txn_rollback;