3 # Copyright 2020 Koha Development team
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>.
23 use Test::More tests => 7;
24 use Test::Deep qw( cmp_methods );
27 use Koha::CirculationRules;
31 use t::lib::TestBuilder;
33 my $schema = Koha::Database->new->schema;
34 my $builder = t::lib::TestBuilder->new;
36 subtest 'get_effective_issuing_rule' => sub {
39 $schema->storage->txn_begin;
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 'Performance' => sub {
100 my $worst_case = timethis(500,
101 sub { Koha::CirculationRules->get_effective_rule({
102 branchcode => 'nonexistent',
103 categorycode => 'nonexistent',
104 itemtype => 'nonexistent',
105 rule_name => 'nonexistent',
109 my $mid_case = timethis(500,
110 sub { Koha::CirculationRules->get_effective_rule({
111 branchcode => $branchcode,
112 categorycode => 'nonexistent',
113 itemtype => 'nonexistent',
114 rule_name => 'nonexistent',
118 my $sec_best_case = timethis(500,
119 sub { Koha::CirculationRules->get_effective_rule({
120 branchcode => $branchcode,
121 categorycode => $categorycode,
122 itemtype => 'nonexistent',
123 rule_name => 'nonexistent',
127 my $best_case = timethis(500,
128 sub { Koha::CirculationRules->get_effective_rule({
129 branchcode => $branchcode,
130 categorycode => $categorycode,
131 itemtype => $itemtype,
132 rule_name => 'nonexistent',
136 ok($worst_case, 'In worst case, get_effective_issuing_rule finds matching'
137 .' rule '.sprintf('%.2f', $worst_case->iters/$worst_case->cpu_a)
138 .' times per second.');
139 ok($mid_case, 'In mid case, get_effective_issuing_rule finds matching'
140 .' rule '.sprintf('%.2f', $mid_case->iters/$mid_case->cpu_a)
141 .' times per second.');
142 ok($sec_best_case, 'In second best case, get_effective_issuing_rule finds matching'
143 .' rule '.sprintf('%.2f', $sec_best_case->iters/$sec_best_case->cpu_a)
144 .' times per second.');
145 ok($best_case, 'In best case, get_effective_issuing_rule finds matching'
146 .' rule '.sprintf('%.2f', $best_case->iters/$best_case->cpu_a)
147 .' times per second.');
150 $schema->storage->txn_rollback;
154 subtest 'set_rule' => sub {
157 $schema->storage->txn_begin;
159 my $branchcode = $builder->build({ source => 'Branch' })->{'branchcode'};
160 my $categorycode = $builder->build({ source => 'Category' })->{'categorycode'};
161 my $itemtype = $builder->build({ source => 'Itemtype' })->{'itemtype'};
163 subtest 'Correct call' => sub {
166 Koha::CirculationRules->delete;
169 Koha::CirculationRules->set_rule( {
170 branchcode => $branchcode,
171 rule_name => 'lostreturn',
174 }, 'setting lostreturn with branch' );
177 Koha::CirculationRules->set_rule( {
178 branchcode => $branchcode,
179 categorycode => $categorycode,
180 rule_name => 'patron_maxissueqty',
183 }, 'setting patron_maxissueqty with branch/category succeeds' );
186 Koha::CirculationRules->set_rule( {
187 branchcode => $branchcode,
188 itemtype => $itemtype,
189 rule_name => 'holdallowed',
192 }, 'setting holdallowed with branch/itemtype succeeds' );
195 Koha::CirculationRules->set_rule( {
196 branchcode => $branchcode,
197 categorycode => $categorycode,
198 itemtype => $itemtype,
202 }, 'setting fine with branch/category/itemtype succeeds' );
205 subtest 'Call with missing params' => sub {
208 Koha::CirculationRules->delete;
211 Koha::CirculationRules->set_rule( {
212 rule_name => 'lostreturn',
215 }, qr/branchcode/, 'setting lostreturn without branch fails' );
218 Koha::CirculationRules->set_rule( {
219 branchcode => $branchcode,
220 rule_name => 'patron_maxissueqty',
223 }, qr/categorycode/, 'setting patron_maxissueqty without categorycode fails' );
226 Koha::CirculationRules->set_rule( {
227 branchcode => $branchcode,
228 rule_name => 'holdallowed',
231 }, qr/itemtype/, 'setting holdallowed without itemtype fails' );
234 Koha::CirculationRules->set_rule( {
235 branchcode => $branchcode,
236 categorycode => $categorycode,
240 }, qr/itemtype/, 'setting fine without itemtype fails' );
243 subtest 'Call with extra params' => sub {
246 Koha::CirculationRules->delete;
249 Koha::CirculationRules->set_rule( {
250 branchcode => $branchcode,
251 categorycode => $categorycode,
252 rule_name => 'lostreturn',
255 }, qr/categorycode/, 'setting lostreturn with categorycode fails' );
258 Koha::CirculationRules->set_rule( {
259 branchcode => $branchcode,
260 categorycode => $categorycode,
261 itemtype => $itemtype,
262 rule_name => 'patron_maxissueqty',
265 }, qr/itemtype/, 'setting patron_maxissueqty with itemtype fails' );
268 Koha::CirculationRules->set_rule( {
269 branchcode => $branchcode,
270 rule_name => 'holdallowed',
271 categorycode => $categorycode,
272 itemtype => $itemtype,
275 }, qr/categorycode/, 'setting holdallowed with categorycode fails' );
278 $schema->storage->txn_rollback;
281 subtest 'clone' => sub {
284 $schema->storage->txn_begin;
286 my $branchcode = $builder->build({ source => 'Branch' })->{'branchcode'};
287 my $categorycode = $builder->build({ source => 'Category' })->{'categorycode'};
288 my $itemtype = $builder->build({ source => 'Itemtype' })->{'itemtype'};
290 subtest 'Clone multiple rules' => sub {
293 Koha::CirculationRules->delete;
295 Koha::CirculationRule->new({
297 categorycode => $categorycode,
298 itemtype => $itemtype,
303 Koha::CirculationRule->new({
305 categorycode => $categorycode,
306 itemtype => $itemtype,
307 rule_name => 'lengthunit',
308 rule_value => 'days',
311 Koha::CirculationRules->search({ branchcode => undef })->clone($branchcode);
313 my $rule_fine = Koha::CirculationRules->get_effective_rule({
314 branchcode => $branchcode,
315 categorycode => $categorycode,
316 itemtype => $itemtype,
319 my $rule_lengthunit = Koha::CirculationRules->get_effective_rule({
320 branchcode => $branchcode,
321 categorycode => $categorycode,
322 itemtype => $itemtype,
323 rule_name => 'lengthunit',
329 branchcode => $branchcode,
330 categorycode => $categorycode,
331 itemtype => $itemtype,
335 'When I attempt to get cloned fine rule,'
336 .' then the above one is returned.'
341 branchcode => $branchcode,
342 categorycode => $categorycode,
343 itemtype => $itemtype,
344 rule_name => 'lengthunit',
345 rule_value => 'days',
347 'When I attempt to get cloned lengthunit rule,'
348 .' then the above one is returned.'
353 subtest 'Clone one rule' => sub {
356 Koha::CirculationRules->delete;
358 Koha::CirculationRule->new({
360 categorycode => $categorycode,
361 itemtype => $itemtype,
366 my $rule = Koha::CirculationRules->search({ branchcode => undef })->next;
367 $rule->clone($branchcode);
369 my $cloned_rule = Koha::CirculationRules->get_effective_rule({
370 branchcode => $branchcode,
371 categorycode => $categorycode,
372 itemtype => $itemtype,
379 branchcode => $branchcode,
380 categorycode => $categorycode,
381 itemtype => $itemtype,
385 'When I attempt to get cloned fine rule,'
386 .' then the above one is returned.'
391 $schema->storage->txn_rollback;
394 subtest 'set_rule + get_effective_rule' => sub {
397 $schema->storage->txn_begin;
399 my $categorycode = $builder->build_object( { class => 'Koha::Patron::Categories' } )->categorycode;
400 my $itemtype = $builder->build_object( { class => 'Koha::ItemTypes' } )->itemtype;
401 my $branchcode = $builder->build_object( { class => 'Koha::Libraries' } )->branchcode;
402 my $branchcode_2 = $builder->build_object( { class => 'Koha::Libraries' } )->branchcode;
403 my $rule_name = 'maxissueqty';
404 my $default_rule_value = 1;
407 Koha::CirculationRules->delete;
409 throws_ok { Koha::CirculationRules->get_effective_rule }
410 'Koha::Exceptions::MissingParameter',
411 "Exception should be raised if get_effective_rule is called without rule_name parameter";
413 $rule = Koha::CirculationRules->get_effective_rule(
415 branchcode => $branchcode,
416 categorycode => $categorycode,
417 itemtype => $itemtype,
418 rule_name => $rule_name,
421 is( $rule, undef, 'Undef should be returned if no rule exist' );
423 Koha::CirculationRules->set_rule(
428 rule_name => $rule_name,
429 rule_value => $default_rule_value,
433 $rule = Koha::CirculationRules->get_effective_rule(
436 categorycode => undef,
438 rule_name => $rule_name,
441 is( $rule->rule_value, $default_rule_value, 'undef means default' );
442 $rule = Koha::CirculationRules->get_effective_rule(
447 rule_name => $rule_name,
451 is( $rule->rule_value, $default_rule_value, '* means default' );
453 $rule = Koha::CirculationRules->get_effective_rule(
455 branchcode => $branchcode_2,
458 rule_name => $rule_name,
461 is( $rule->rule_value, 1,
462 'Default rule is returned if there is no rule for this branchcode' );
464 subtest 'test rules that cannot be blank' => sub {
466 foreach my $no_blank_rule ( ('holdallowed','hold_fulfillment_policy','returnbranch') ){
467 Koha::CirculationRules->set_rule(
469 branchcode => $branchcode,
471 rule_name => $no_blank_rule,
476 $rule = Koha::CirculationRules->get_effective_rule(
478 branchcode => $branchcode,
479 categorycode => undef,
481 rule_name => $no_blank_rule,
484 is( $rule, undef, 'Rules that cannot be blank are not set when passed blank string' );
489 subtest 'test rule matching with different combinations of rule scopes' => sub {
490 my ( $tests, $order ) = _prepare_tests_for_rule_scope_combinations(
492 branchcode => $branchcode,
493 categorycode => $categorycode,
494 itemtype => $itemtype,
499 plan tests => 2**scalar @$order;
501 foreach my $test (@$tests) {
502 my $rule_params = {%$test};
503 $rule_params->{rule_name} = $rule_name;
504 my $rule_value = $rule_params->{rule_value} = int( rand(10) );
506 Koha::CirculationRules->set_rule($rule_params);
508 my $rule = Koha::CirculationRules->get_effective_rule(
510 branchcode => $branchcode,
511 categorycode => $categorycode,
512 itemtype => $itemtype,
513 rule_name => $rule_name,
517 my $scope_output = '';
518 foreach my $key ( values @$order ) {
519 $scope_output .= " $key" if $test->{$key} ne '*';
522 is( $rule->rule_value, $rule_value,
524 . ( $scope_output ? $scope_output : ' nothing' ) );
528 my $our_branch_rules = Koha::CirculationRules->search({branchcode => $branchcode});
529 is( $our_branch_rules->count, 4, "We added 8 rules");
530 $our_branch_rules->delete;
531 is( $our_branch_rules->count, 0, "We deleted 8 rules");
533 $schema->storage->txn_rollback;
536 subtest 'get_onshelfholds_policy() tests' => sub {
540 $schema->storage->txn_begin;
542 my $item = $builder->build_sample_item();
544 my $circ_rules = Koha::CirculationRules->new;
546 $circ_rules->search({ rule_name => 'onshelfholds' })->delete;
548 $circ_rules->set_rule(
553 rule_name => 'onshelfholds',
558 is( $circ_rules->get_onshelfholds_policy({ item => $item }), 1, 'If rule_value is set on a matching rule, return it' );
559 # Delete the rule (i.e. get_effective_rule returns undef)
561 is( $circ_rules->get_onshelfholds_policy({ item => $item }), 0, 'If no matching rule, fallback to 0' );
563 $schema->storage->txn_rollback;
566 subtest 'get_effective_daysmode' => sub {
569 $schema->storage->txn_begin;
571 my $item_1 = $builder->build_sample_item();
572 my $item_2 = $builder->build_sample_item();
575 Koha::CirculationRules->search( { rule_name => 'daysmode' } )->delete;
577 # Default value 'Datedue' at pref level
578 t::lib::Mocks::mock_preference( 'useDaysMode', 'Datedue' );
581 Koha::CirculationRules->get_effective_daysmode(
583 categorycode => undef,
584 itemtype => $item_1->effective_itemtype,
589 'daysmode default to pref value if the rule does not exist'
592 Koha::CirculationRules->set_rule(
597 rule_name => 'daysmode',
598 rule_value => 'Calendar',
601 Koha::CirculationRules->set_rule(
605 itemtype => $item_1->effective_itemtype,
606 rule_name => 'daysmode',
607 rule_value => 'Days',
612 Koha::CirculationRules->get_effective_daysmode(
614 categorycode => undef,
615 itemtype => $item_1->effective_itemtype,
620 "daysmode for item_1 is the specific rule"
623 Koha::CirculationRules->get_effective_daysmode(
625 categorycode => undef,
626 itemtype => $item_2->effective_itemtype,
631 "daysmode for item_2 is the one defined for the default circ rule"
634 Koha::CirculationRules->set_rule(
638 itemtype => $item_2->effective_itemtype,
639 rule_name => 'daysmode',
645 Koha::CirculationRules->get_effective_daysmode(
647 categorycode => undef,
648 itemtype => $item_2->effective_itemtype,
653 'daysmode default to pref value if the rule exists but set to""'
656 $schema->storage->txn_rollback;
659 subtest 'get_lostreturn_policy() tests' => sub {
662 $schema->storage->txn_begin;
664 $schema->resultset('CirculationRule')->search()->delete;
666 my $default_rule_charge = $builder->build(
668 source => 'CirculationRule',
671 categorycode => undef,
673 rule_name => 'lostreturn',
674 rule_value => 'charge'
678 my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
679 my $specific_rule_false = $builder->build(
681 source => 'CirculationRule',
683 branchcode => $branchcode,
684 categorycode => undef,
686 rule_name => 'lostreturn',
691 my $branchcode2 = $builder->build( { source => 'Branch' } )->{branchcode};
692 my $specific_rule_refund = $builder->build(
694 source => 'CirculationRule',
696 branchcode => $branchcode2,
697 categorycode => undef,
699 rule_name => 'lostreturn',
700 rule_value => 'refund'
704 my $branchcode3 = $builder->build( { source => 'Branch' } )->{branchcode};
705 my $specific_rule_restore = $builder->build(
707 source => 'CirculationRule',
709 branchcode => $branchcode3,
710 categorycode => undef,
712 rule_name => 'lostreturn',
713 rule_value => 'restore'
718 # Make sure we have an unused branchcode
719 my $branchcode4 = $builder->build( { source => 'Branch' } )->{branchcode};
720 my $specific_rule_dummy = $builder->build(
722 source => 'CirculationRule',
724 branchcode => $branchcode4,
725 categorycode => undef,
727 rule_name => 'lostreturn',
728 rule_value => 'refund'
732 my $branch_without_rule = $specific_rule_dummy->{ branchcode };
733 Koha::CirculationRules
736 branchcode => $branch_without_rule,
737 categorycode => undef,
739 rule_name => 'lostreturn',
740 rule_value => 'refund'
746 my $item = $builder->build_sample_item(
748 homebranch => $specific_rule_restore->{branchcode},
749 holdingbranch => $specific_rule_false->{branchcode}
753 return_branch => $specific_rule_refund->{ branchcode },
758 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
759 is( Koha::CirculationRules->get_lostreturn_policy( $params ),
760 'refund','Specific rule for checkin branch is applied (refund)');
762 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHomeBranch' );
763 is( Koha::CirculationRules->get_lostreturn_policy( $params ),
764 'restore','Specific rule for home branch is applied (restore)');
766 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHoldingBranch' );
767 is( Koha::CirculationRules->get_lostreturn_policy( $params ),
768 0,'Specific rule for holding branch is applied (false)');
771 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
772 $params->{return_branch} = $branch_without_rule;
773 is( Koha::CirculationRules->get_lostreturn_policy( $params ),
774 'charge','No rule for branch, global rule applied (charge)');
776 # Change the default value just to try
777 Koha::CirculationRules->search({ branchcode => undef, rule_name => 'lostreturn' })->next->rule_value(0)->store;
778 is( Koha::CirculationRules->get_lostreturn_policy( $params ),
779 0,'No rule for branch, global rule applied (false)');
781 # No default rule defined check
782 Koha::CirculationRules
786 categorycode => undef,
788 rule_name => 'lostreturn'
793 is( Koha::CirculationRules->get_lostreturn_policy( $params ),
794 'refund','No rule for branch, no default rule, fallback default (refund)');
796 # Fallback to ItemHoldBranch if CheckinLibrary is undefined
797 $params->{return_branch} = undef;
798 is( Koha::CirculationRules->get_lostreturn_policy( $params ),
799 'restore','return_branch undefined, fallback to ItemHomeBranch rule (restore)');
801 $schema->storage->txn_rollback;
805 my ( $rule, $expected, $message ) = @_;
807 ok( $rule, $message ) ?
808 cmp_methods( $rule, [ %$expected ], $message ) :
812 sub _prepare_tests_for_rule_scope_combinations {
813 my ( $scope, $rule_name ) = @_;
815 # Here we create a combinations of 1s and 0s the following way
826 # (the number of columns equals to the amount of rule scopes)
827 # The ... symbolizes possible future scopes.
829 # - 0 equals to circulation rule scope with any value (aka. *)
830 # - 1 equals to circulation rule scope exact value, e.g.
831 # "CPL" (for branchcode).
833 # The order is the same as the weight of scopes when sorting circulation
834 # rules. So the first column of numbers is the scope with most weight.
835 # This is defined by C<$order> which will be assigned next.
837 # We must maintain the order in order to keep the test valid. This should be
838 # equal to Koha/CirculationRules.pm "order_by" of C<get_effective_rule> sub.
839 # Let's explicitly define the order and fail test if we are missing a scope:
840 my $order = [ 'branchcode', 'categorycode', 'itemtype' ];
841 is( join(", ", sort keys %$scope),
842 join(", ", sort @$order), 'Missing a scope!' ) if keys %$scope ne scalar @$order;
845 foreach my $value ( glob( "{0,1}" x keys %$scope || 1 ) ) {
846 my $test = { %$scope };
847 for ( my $i=0; $i < keys %$scope; $i++ ) {
848 $test->{$order->[$i]} = '*' unless substr( $value, $i, 1 );
853 return \@tests, $order;