From bcbcfe6168c1547612c9fb511f0f837cb4be2a83 Mon Sep 17 00:00:00 2001 From: Martin Renvoize Date: Wed, 3 Jun 2020 14:13:22 +0100 Subject: [PATCH] Bug 25663: Add unit tests This patch adds unit tests for a new get_lostretun_policy method which is getting added to Koha::CirculationRules. Test plan: 1/ Read the additions to t/db_dependent/CirculationRules.t code and compare to the t/db_dependent/RefundLostItemFeeRule.t. 2/ Run the test after applying the patch that adds the new method 3/ If the tests all pass, signoff Signed-off-by: Kyle M Hall --- t/db_dependent/Koha/CirculationRules.t | 127 ++++++++++++++++++++++++- 1 file changed, 126 insertions(+), 1 deletion(-) diff --git a/t/db_dependent/Koha/CirculationRules.t b/t/db_dependent/Koha/CirculationRules.t index f24542e2b6..1473c235d7 100644 --- a/t/db_dependent/Koha/CirculationRules.t +++ b/t/db_dependent/Koha/CirculationRules.t @@ -19,12 +19,13 @@ use Modern::Perl; -use Test::More tests => 3; +use Test::More tests => 4; use Test::Exception; use Koha::CirculationRules; use Koha::Database; +use t::lib::Mocks; use t::lib::TestBuilder; use t::lib::Mocks; @@ -376,3 +377,127 @@ subtest 'get_effective_daysmode' => sub { $schema->storage->txn_rollback; }; + +subtest 'get_lostreturn_policy() tests' => sub { + plan tests => 6; + + $schema->storage->txn_begin; + + $schema->resultset('CirculationRule')->search()->delete; + + my $default_rule = $builder->build( + { + source => 'CirculationRule', + value => { + branchcode => undef, + categorycode => undef, + itemtype => undef, + rule_name => 'refund', + rule_value => 1 + } + } + ); + my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode}; + my $specific_rule_false = $builder->build( + { + source => 'CirculationRule', + value => { + branchcode => $branchcode, + categorycode => undef, + itemtype => undef, + rule_name => 'refund', + rule_value => 0 + } + } + ); + my $branchcode2 = $builder->build( { source => 'Branch' } )->{branchcode}; + my $specific_rule_true = $builder->build( + { + source => 'CirculationRule', + value => { + branchcode => $branchcode2, + categorycode => undef, + itemtype => undef, + rule_name => 'refund', + rule_value => 1 + } + } + ); + # Make sure we have an unused branchcode + my $branchcode3 = $builder->build( { source => 'Branch' } )->{branchcode}; + my $specific_rule_dummy = $builder->build( + { + source => 'CirculationRule', + value => { + branchcode => $branchcode3, + categorycode => undef, + itemtype => undef, + rule_name => 'refund', + } + } + ); + my $branch_without_rule = $specific_rule_dummy->{ branchcode }; + Koha::CirculationRules + ->search( + { + branchcode => $branch_without_rule, + categorycode => undef, + itemtype => undef, + rule_name => 'refund' + } + ) + ->next + ->delete; + + my $item = $builder->build_sample_item( + { + homebranch => $specific_rule_true->{branchcode}, + holdingbranch => $specific_rule_false->{branchcode} + } + ); + my $params = { + return_branch => $specific_rule_true->{ branchcode }, + item => $item + }; + + # Specific rules + t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' ); + is( Koha::CirculationRules->get_lostreturn_policy( $params ), + 1,'Specific rule for checkin branch is applied (true)'); + + t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHomeBranch' ); + is( Koha::CirculationRules->get_lostreturn_policy( $params ), + 1,'Specific rule for home branch is applied (true)'); + + t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHoldingBranch' ); + is( Koha::CirculationRules->get_lostreturn_policy( $params ), + 0,'Specific rule for holoding branch is applied (false)'); + + # Default rule check + t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' ); + $params->{return_branch} = $branch_without_rule; + is( Koha::CirculationRules->get_lostreturn_policy( $params ), + 1,'No rule for branch, global rule applied (true)'); + + # Change the default value just to try + Koha::CirculationRules->search({ branchcode => undef, rule_name => 'refund' })->next->rule_value(0)->store; + is( Koha::CirculationRules->get_lostreturn_policy( $params ), + 0,'No rule for branch, global rule applied (false)'); + + # No default rule defined check + Koha::CirculationRules + ->search( + { + branchcode => undef, + categorycode => undef, + itemtype => undef, + rule_name => 'refund' + } + ) + ->next + ->delete; + is( Koha::CirculationRules->get_lostreturn_policy( $params ), + 1,'No rule for branch, no default rule, fallback default (true)'); + + $schema->storage->txn_rollback; +}; -- 2.20.1