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
=> 9;
22 use t
::lib
::TestBuilder
;
28 use_ok
('Koha::Object');
29 use_ok
('Koha::CirculationRule');
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('CirculationRule')->search()->delete;
46 my $generated_default_rule = $builder->build(
48 source
=> 'CirculationRule',
51 categorycode
=> undef,
53 rule_name
=> 'refund',
57 my $generated_other_rule = $builder->build(
59 source
=> 'CirculationRule',
61 categorycode
=> undef,
63 rule_name
=> 'refund',
68 my $default_rule = Koha
::CirculationRules
->search(
71 categorycode
=> undef,
73 rule_name
=> 'refund',
76 ok
( defined $default_rule, 'Default rule created' );
77 ok
( $default_rule->_result->in_storage, 'Default rule actually in storage');
79 my $other_rule = Koha
::CirculationRules
->search(
81 branchcode
=> $generated_other_rule->{branchcode
},
82 categorycode
=> undef,
84 rule_name
=> 'refund',
87 ok
( defined $other_rule, 'Other rule created' );
88 ok
( $other_rule->_result->in_storage, 'Other rule actually in storage');
90 # deleting the regular rule
92 ok
( !$other_rule->_result->in_storage, 'Other rule deleted from storage' );
94 # Rollback transaction
95 $schema->storage->txn_rollback;
98 subtest
'Koha::RefundLostItemFeeRules::_default_rule() tests' => sub {
103 $schema->storage->txn_begin;
106 $schema->resultset('CirculationRule')->search()->delete;
108 my $generated_default_rule = $builder->build(
110 source
=> 'CirculationRule',
113 categorycode
=> undef,
115 rule_name
=> 'refund',
120 my $generated_other_rule = $builder->build(
122 source
=> 'CirculationRule',
124 categorycode
=> undef,
126 rule_name
=> 'refund',
131 my $default_rule = Koha
::CirculationRules
->search(
134 categorycode
=> undef,
136 rule_name
=> 'refund',
139 ok
( defined $default_rule, 'Default rule created' );
140 ok
( $default_rule->_result->in_storage, 'Default rule actually in storage');
141 is
( Koha
::RefundLostItemFeeRules
->_default_rule, 1, 'Default rule is set to refund' );
143 # Change default rule to "Don't refund"
144 $default_rule->rule_value(0);
145 $default_rule->store;
146 # Re-read from DB, to be sure
147 $default_rule = Koha
::CirculationRules
->search(
150 categorycode
=> undef,
152 rule_name
=> 'refund',
155 ok
( !Koha
::RefundLostItemFeeRules
->_default_rule, 'Default rule is set to not refund' );
157 $default_rule->delete;
158 ok
( !$default_rule->_result->in_storage, 'Default rule effectively deleted from storage' );
160 ok
( Koha
::RefundLostItemFeeRules
->_default_rule, 'Default rule is set to refund if no default rule is present' );
162 # Rollback transaction
163 $schema->storage->txn_rollback;
166 subtest
'Koha::RefundLostItemFeeRules::_effective_branch_rule() tests' => sub {
171 $schema->storage->txn_begin;
174 $schema->resultset('CirculationRule')->search()->delete;
176 my $default_rule = $builder->build(
178 source
=> 'CirculationRule',
181 categorycode
=> undef,
183 rule_name
=> 'refund',
188 my $specific_rule_false = $builder->build(
190 source
=> 'CirculationRule',
192 categorycode
=> undef,
194 rule_name
=> 'refund',
199 my $specific_rule_true = $builder->build(
201 source
=> 'CirculationRule',
203 categorycode
=> undef,
205 rule_name
=> 'refund',
211 is
( Koha
::RefundLostItemFeeRules
->_effective_branch_rule( $specific_rule_true->{ branchcode
} ),
212 1,'Specific rule is applied (true)');
213 is
( Koha
::RefundLostItemFeeRules
->_effective_branch_rule( $specific_rule_false->{ branchcode
} ),
214 0,'Specific rule is applied (false)');
215 # Delete specific rules
216 Koha
::RefundLostItemFeeRules
->find({ branchcode
=> $specific_rule_false->{ branchcode
} })->delete;
217 is
( Koha
::RefundLostItemFeeRules
->_effective_branch_rule( $specific_rule_false->{ branchcode
} ),
218 1,'No specific rule defined, fallback to global (true)');
220 # Rollback transaction
221 $schema->storage->txn_rollback;
224 subtest
'Koha::RefundLostItemFeeRules::_choose_branch() tests' => sub {
229 $schema->storage->txn_begin;
232 current_branch
=> 'current_branch_code',
233 item_holding_branch
=> 'item_holding_branch_code',
234 item_home_branch
=> 'item_home_branch_code'
237 t
::lib
::Mocks
::mock_preference
( 'RefundLostOnReturnControl', 'CheckinLibrary' );
239 is
( Koha
::RefundLostItemFeeRules
->_choose_branch( $params ),
240 'current_branch_code', 'CheckinLibrary is honoured');
242 t
::lib
::Mocks
::mock_preference
( 'RefundLostOnReturnControl', 'ItemHomeBranch' );
243 is
( Koha
::RefundLostItemFeeRules
->_choose_branch( $params ),
244 'item_home_branch_code', 'ItemHomeBranch is honoured');
246 t
::lib
::Mocks
::mock_preference
( 'RefundLostOnReturnControl', 'ItemHoldingBranch' );
247 is
( Koha
::RefundLostItemFeeRules
->_choose_branch( $params ),
248 'item_holding_branch_code', 'ItemHoldingBranch is honoured');
250 t
::lib
::Mocks
::mock_preference
( 'RefundLostOnReturnControl', 'CheckinLibrary' );
252 Koha
::RefundLostItemFeeRules
->_choose_branch();
254 is
( ref($@
), 'Koha::Exceptions::MissingParameter',
255 'Missing parameter exception' );
256 is
( $@
->message, 'CheckinLibrary requires the current_branch param',
257 'Exception message is correct' );
259 t
::lib
::Mocks
::mock_preference
( 'RefundLostOnReturnControl', 'ItemHomeBranch' );
261 Koha
::RefundLostItemFeeRules
->_choose_branch();
263 is
( ref($@
), 'Koha::Exceptions::MissingParameter',
264 'Missing parameter exception' );
265 is
( $@
->message, 'ItemHomeBranch requires the item_home_branch param',
266 'Exception message is correct' );
268 t
::lib
::Mocks
::mock_preference
( 'RefundLostOnReturnControl', 'ItemHoldingBranch' );
270 Koha
::RefundLostItemFeeRules
->_choose_branch();
272 is
( ref($@
), 'Koha::Exceptions::MissingParameter',
273 'Missing parameter exception' );
274 is
( $@
->message, 'ItemHoldingBranch requires the item_holding_branch param',
275 'Exception message is correct' );
277 # Rollback transaction
278 $schema->storage->txn_rollback;
281 subtest
'Koha::RefundLostItemFeeRules::should_refund() tests' => sub {
286 $schema->storage->txn_begin;
288 t
::lib
::Mocks
::mock_preference
( 'RefundLostOnReturnControl', 'CheckinLibrary' );
290 $schema->resultset('CirculationRule')->search()->delete;
292 my $default_rule = $builder->build(
294 source
=> 'CirculationRule',
297 categorycode
=> undef,
299 rule_name
=> 'refund',
304 my $specific_rule_false = $builder->build(
306 source
=> 'CirculationRule',
308 categorycode
=> undef,
310 rule_name
=> 'refund',
315 my $specific_rule_true = $builder->build(
317 source
=> 'CirculationRule',
319 categorycode
=> undef,
321 rule_name
=> 'refund',
326 # Make sure we have an unused branchcode
327 my $specific_rule_dummy = $builder->build(
329 source
=> 'CirculationRule',
331 categorycode
=> undef,
333 rule_name
=> 'refund',
337 my $branch_without_rule = $specific_rule_dummy->{ branchcode
};
338 Koha
::CirculationRules
341 branchcode
=> $branch_without_rule,
342 categorycode
=> undef,
344 rule_name
=> 'refund'
351 current_branch
=> $specific_rule_true->{ branchcode
},
352 # patron_branch => $specific_rule_false->{ branchcode },
353 item_holding_branch
=> $branch_without_rule,
354 item_home_branch
=> $branch_without_rule
357 t
::lib
::Mocks
::mock_preference
( 'RefundLostOnReturnControl', 'CheckinLibrary' );
358 is
( Koha
::RefundLostItemFeeRules
->should_refund( $params ),
359 1,'Specific rule is applied (true)');
361 t
::lib
::Mocks
::mock_preference
( 'RefundLostOnReturnControl', 'ItemHomeBranch' );
362 is
( Koha
::RefundLostItemFeeRules
->should_refund( $params ),
363 1,'No rule for branch, global rule applied (true)');
365 # Change the default value just to try
366 Koha
::CirculationRules
->search({ branchcode
=> undef, rule_name
=> 'refund' })->next->rule_value(0)->store;
367 t
::lib
::Mocks
::mock_preference
( 'RefundLostOnReturnControl', 'ItemHoldingBranch' );
368 is
( Koha
::RefundLostItemFeeRules
->should_refund( $params ),
369 0,'No rule for branch, global rule applied (false)');
371 # Rollback transaction
372 $schema->storage->txn_rollback;
375 subtest
'Koha::RefundLostItemFeeRules::find() tests' => sub {
380 $schema->storage->txn_begin;
382 t
::lib
::Mocks
::mock_preference
( 'RefundLostOnReturnControl', 'CheckinLibrary' );
384 $schema->resultset('CirculationRule')->search()->delete;
386 my $default_non_refund = $builder->build(
388 source
=> 'CirculationRule',
391 categorycode
=> undef,
393 rule_name
=> 'non_refund_rule',
398 my $specific_non_refund = $builder->build(
400 source
=> 'CirculationRule',
402 categorycode
=> undef,
404 rule_name
=> 'non_refund_rule',
410 ok
(!defined Koha
::RefundLostItemFeeRules
->find({ branchcode
=> undef }), 'Non refund default rules are not found');
411 ok
(!defined Koha
::RefundLostItemFeeRules
->find({ branchcode
=> $specific_non_refund->{branchcode
} }), 'Non refund specific rules are not found');
413 my $default_refund = $builder->build(
415 source
=> 'CirculationRule',
418 categorycode
=> undef,
420 rule_name
=> 'refund',
425 my $specific_refund = $builder->build(
427 source
=> 'CirculationRule',
429 categorycode
=> undef,
431 rule_name
=> 'refund',
437 ok
(defined Koha
::RefundLostItemFeeRules
->find({ branchcode
=> undef }), 'Refund default rules are found');
438 ok
(defined Koha
::RefundLostItemFeeRules
->find({ branchcode
=> $specific_refund->{branchcode
} }), 'Refund specific rules are found');
440 # Rollback transaction
441 $schema->storage->txn_rollback;