3 # This file is part of Koha.
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.
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.
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>.
20 use Test::More tests => 8;
22 use t::lib::TestBuilder;
28 use_ok('Koha::Object');
29 use_ok('Koha::RefundLostItemFeeRule');
30 use_ok('Koha::RefundLostItemFeeRules');
33 my $schema = Koha::Database->new->schema;
34 my $builder = t::lib::TestBuilder->new;
36 subtest 'Koha::RefundLostItemFeeRule::delete() tests' => sub {
41 $schema->storage->txn_begin;
44 $schema->resultset('RefundLostItemFeeRule')->search()->delete;
46 my $generated_default_rule = $builder->build({
47 source => 'RefundLostItemFeeRule',
52 my $generated_other_rule = $builder->build({
53 source => 'RefundLostItemFeeRule'
56 my $default_rule = Koha::RefundLostItemFeeRules->find({
58 ok( defined $default_rule, 'Default rule created' );
59 ok( $default_rule->in_storage, 'Default rule actually in storage');
61 my $other_rule = Koha::RefundLostItemFeeRules->find({
62 branchcode => $generated_other_rule->{ branchcode }
64 ok( defined $other_rule, 'Other rule created' );
65 ok( $other_rule->in_storage, 'Other rule actually in storage');
67 # deleting the regular rule
69 ok( !$other_rule->in_storage, 'Other rule deleted from storage' );
71 # Rollback transaction
72 $schema->storage->txn_rollback;
75 subtest 'Koha::RefundLostItemFeeRules::_default_rule() tests' => sub {
80 $schema->storage->txn_begin;
83 $schema->resultset('RefundLostItemFeeRule')->search()->delete;
85 my $generated_default_rule = $builder->build({
86 source => 'RefundLostItemFeeRule',
92 my $generated_other_rule = $builder->build({
93 source => 'RefundLostItemFeeRule'
96 my $default_rule = Koha::RefundLostItemFeeRules->find({
98 ok( defined $default_rule, 'Default rule created' );
99 ok( $default_rule->in_storage, 'Default rule actually in storage');
100 ok( Koha::RefundLostItemFeeRules->_default_rule, 'Default rule is set to refund' );
102 # Change default rule to "Don't refund"
103 $default_rule->refund(0);
104 $default_rule->store;
105 # Re-read from DB, to be sure
106 $default_rule = Koha::RefundLostItemFeeRules->find({
107 branchcode => '*' });
108 ok( !Koha::RefundLostItemFeeRules->_default_rule, 'Default rule is set to not refund' );
110 $default_rule->delete;
111 ok( !$default_rule->in_storage, 'Default rule effectively deleted from storage' );
113 ok( Koha::RefundLostItemFeeRules->_default_rule, 'Default rule is set to refund if no default rule is present' );
115 # Rollback transaction
116 $schema->storage->txn_rollback;
119 subtest 'Koha::RefundLostItemFeeRules::_effective_branch_rule() tests' => sub {
124 $schema->storage->txn_begin;
127 $schema->resultset('RefundLostItemFeeRule')->search()->delete;
129 my $default_rule = $builder->build({
130 source => 'RefundLostItemFeeRule',
136 my $specific_rule_false = $builder->build({
137 source => 'RefundLostItemFeeRule',
142 my $specific_rule_true = $builder->build({
143 source => 'RefundLostItemFeeRule',
149 is( Koha::RefundLostItemFeeRules->_effective_branch_rule( $specific_rule_true->{ branchcode } ),
150 1,'Specific rule is applied (true)');
151 is( Koha::RefundLostItemFeeRules->_effective_branch_rule( $specific_rule_false->{ branchcode } ),
152 0,'Specific rule is applied (false)');
153 # Delete specific rules
154 Koha::RefundLostItemFeeRules->find({ branchcode => $specific_rule_false->{ branchcode } })->delete;
155 is( Koha::RefundLostItemFeeRules->_effective_branch_rule( $specific_rule_false->{ branchcode } ),
156 1,'No specific rule defined, fallback to global (true)');
158 # Rollback transaction
159 $schema->storage->txn_rollback;
162 subtest 'Koha::RefundLostItemFeeRules::_choose_branch() tests' => sub {
167 $schema->storage->txn_begin;
170 current_branch => 'current_branch_code',
171 item_holding_branch => 'item_holding_branch_code',
172 item_home_branch => 'item_home_branch_code'
175 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
177 is( Koha::RefundLostItemFeeRules->_choose_branch( $params ),
178 'current_branch_code', 'CheckinLibrary is honoured');
180 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHomeBranch' );
181 is( Koha::RefundLostItemFeeRules->_choose_branch( $params ),
182 'item_home_branch_code', 'ItemHomeBranch is honoured');
184 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHoldingBranch' );
185 is( Koha::RefundLostItemFeeRules->_choose_branch( $params ),
186 'item_holding_branch_code', 'ItemHoldingBranch is honoured');
188 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
190 Koha::RefundLostItemFeeRules->_choose_branch();
192 is( ref($@), 'Koha::Exceptions::MissingParameter',
193 'Missing parameter exception' );
194 is( $@->message, 'CheckinLibrary requires the current_branch param',
195 'Exception message is correct' );
197 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHomeBranch' );
199 Koha::RefundLostItemFeeRules->_choose_branch();
201 is( ref($@), 'Koha::Exceptions::MissingParameter',
202 'Missing parameter exception' );
203 is( $@->message, 'ItemHomeBranch requires the item_home_branch param',
204 'Exception message is correct' );
206 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHoldingBranch' );
208 Koha::RefundLostItemFeeRules->_choose_branch();
210 is( ref($@), 'Koha::Exceptions::MissingParameter',
211 'Missing parameter exception' );
212 is( $@->message, 'ItemHoldingBranch requires the item_holding_branch param',
213 'Exception message is correct' );
215 # Rollback transaction
216 $schema->storage->txn_rollback;
219 subtest 'Koha::RefundLostItemFeeRules::should_refund() tests' => sub {
224 $schema->storage->txn_begin;
226 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
228 $schema->resultset('RefundLostItemFeeRule')->search()->delete;
230 my $default_rule = $builder->build({
231 source => 'RefundLostItemFeeRule',
237 my $specific_rule_false = $builder->build({
238 source => 'RefundLostItemFeeRule',
243 my $specific_rule_true = $builder->build({
244 source => 'RefundLostItemFeeRule',
249 # Make sure we have an unused branchcode
250 my $specific_rule_dummy = $builder->build({
251 source => 'RefundLostItemFeeRule'
253 my $branch_without_rule = $specific_rule_dummy->{ branchcode };
254 Koha::RefundLostItemFeeRules
255 ->find({ branchcode => $branch_without_rule })
259 current_branch => $specific_rule_true->{ branchcode },
260 # patron_branch => $specific_rule_false->{ branchcode },
261 item_holding_branch => $branch_without_rule,
262 item_home_branch => $branch_without_rule
265 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
266 is( Koha::RefundLostItemFeeRules->should_refund( $params ),
267 1,'Specific rule is applied (true)');
269 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHomeBranch' );
270 is( Koha::RefundLostItemFeeRules->should_refund( $params ),
271 1,'No rule for branch, global rule applied (true)');
273 # Change the default value just to try
274 Koha::RefundLostItemFeeRules->find({ branchcode => '*' })->refund(0)->store;
275 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHoldingBranch' );
276 is( Koha::RefundLostItemFeeRules->should_refund( $params ),
277 0,'No rule for branch, global rule applied (false)');
279 # Rollback transaction
280 $schema->storage->txn_rollback;