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 use Data::Printer colored => 1;
156 ok( !Koha::RefundLostItemFeeRules->_default_rule, 'Default rule is set to not refund' );
158 $default_rule->delete;
159 ok( !$default_rule->_result->in_storage, 'Default rule effectively deleted from storage' );
161 ok( Koha::RefundLostItemFeeRules->_default_rule, 'Default rule is set to refund if no default rule is present' );
163 # Rollback transaction
164 $schema->storage->txn_rollback;
167 subtest 'Koha::RefundLostItemFeeRules::_effective_branch_rule() tests' => sub {
172 $schema->storage->txn_begin;
175 $schema->resultset('CirculationRule')->search()->delete;
177 my $default_rule = $builder->build(
179 source => 'CirculationRule',
182 categorycode => undef,
184 rule_name => 'refund',
189 my $specific_rule_false = $builder->build(
191 source => 'CirculationRule',
193 categorycode => undef,
195 rule_name => 'refund',
200 my $specific_rule_true = $builder->build(
202 source => 'CirculationRule',
204 categorycode => undef,
206 rule_name => 'refund',
212 is( Koha::RefundLostItemFeeRules->_effective_branch_rule( $specific_rule_true->{ branchcode } ),
213 1,'Specific rule is applied (true)');
214 is( Koha::RefundLostItemFeeRules->_effective_branch_rule( $specific_rule_false->{ branchcode } ),
215 0,'Specific rule is applied (false)');
216 # Delete specific rules
217 Koha::RefundLostItemFeeRules->find({ branchcode => $specific_rule_false->{ branchcode } })->delete;
218 is( Koha::RefundLostItemFeeRules->_effective_branch_rule( $specific_rule_false->{ branchcode } ),
219 1,'No specific rule defined, fallback to global (true)');
221 # Rollback transaction
222 $schema->storage->txn_rollback;
225 subtest 'Koha::RefundLostItemFeeRules::_choose_branch() tests' => sub {
230 $schema->storage->txn_begin;
233 current_branch => 'current_branch_code',
234 item_holding_branch => 'item_holding_branch_code',
235 item_home_branch => 'item_home_branch_code'
238 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
240 is( Koha::RefundLostItemFeeRules->_choose_branch( $params ),
241 'current_branch_code', 'CheckinLibrary is honoured');
243 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHomeBranch' );
244 is( Koha::RefundLostItemFeeRules->_choose_branch( $params ),
245 'item_home_branch_code', 'ItemHomeBranch is honoured');
247 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHoldingBranch' );
248 is( Koha::RefundLostItemFeeRules->_choose_branch( $params ),
249 'item_holding_branch_code', 'ItemHoldingBranch is honoured');
251 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
253 Koha::RefundLostItemFeeRules->_choose_branch();
255 is( ref($@), 'Koha::Exceptions::MissingParameter',
256 'Missing parameter exception' );
257 is( $@->message, 'CheckinLibrary requires the current_branch param',
258 'Exception message is correct' );
260 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHomeBranch' );
262 Koha::RefundLostItemFeeRules->_choose_branch();
264 is( ref($@), 'Koha::Exceptions::MissingParameter',
265 'Missing parameter exception' );
266 is( $@->message, 'ItemHomeBranch requires the item_home_branch param',
267 'Exception message is correct' );
269 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHoldingBranch' );
271 Koha::RefundLostItemFeeRules->_choose_branch();
273 is( ref($@), 'Koha::Exceptions::MissingParameter',
274 'Missing parameter exception' );
275 is( $@->message, 'ItemHoldingBranch requires the item_holding_branch param',
276 'Exception message is correct' );
278 # Rollback transaction
279 $schema->storage->txn_rollback;
282 subtest 'Koha::RefundLostItemFeeRules::should_refund() tests' => sub {
287 $schema->storage->txn_begin;
289 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
291 $schema->resultset('CirculationRule')->search()->delete;
293 my $default_rule = $builder->build(
295 source => 'CirculationRule',
298 categorycode => undef,
300 rule_name => 'refund',
305 my $specific_rule_false = $builder->build(
307 source => 'CirculationRule',
309 categorycode => undef,
311 rule_name => 'refund',
316 my $specific_rule_true = $builder->build(
318 source => 'CirculationRule',
320 categorycode => undef,
322 rule_name => 'refund',
327 # Make sure we have an unused branchcode
328 my $specific_rule_dummy = $builder->build(
330 source => 'CirculationRule',
332 categorycode => undef,
334 rule_name => 'refund',
338 my $branch_without_rule = $specific_rule_dummy->{ branchcode };
339 Koha::CirculationRules
342 branchcode => $branch_without_rule,
343 categorycode => undef,
345 rule_name => 'refund'
352 current_branch => $specific_rule_true->{ branchcode },
353 # patron_branch => $specific_rule_false->{ branchcode },
354 item_holding_branch => $branch_without_rule,
355 item_home_branch => $branch_without_rule
358 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
359 is( Koha::RefundLostItemFeeRules->should_refund( $params ),
360 1,'Specific rule is applied (true)');
362 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHomeBranch' );
363 is( Koha::RefundLostItemFeeRules->should_refund( $params ),
364 1,'No rule for branch, global rule applied (true)');
366 # Change the default value just to try
367 Koha::CirculationRules->search({ branchcode => undef, rule_name => 'refund' })->next->rule_value(0)->store;
368 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHoldingBranch' );
369 is( Koha::RefundLostItemFeeRules->should_refund( $params ),
370 0,'No rule for branch, global rule applied (false)');
372 # Rollback transaction
373 $schema->storage->txn_rollback;
376 subtest 'Koha::RefundLostItemFeeRules::find() tests' => sub {
381 $schema->storage->txn_begin;
383 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
385 $schema->resultset('CirculationRule')->search()->delete;
387 my $default_non_refund = $builder->build(
389 source => 'CirculationRule',
392 categorycode => undef,
394 rule_name => 'non_refund_rule',
399 my $specific_non_refund = $builder->build(
401 source => 'CirculationRule',
403 categorycode => undef,
405 rule_name => 'non_refund_rule',
411 ok(!defined Koha::RefundLostItemFeeRules->find({ branchcode => undef }), 'Non refund default rules are not found');
412 ok(!defined Koha::RefundLostItemFeeRules->find({ branchcode => $specific_non_refund->{branchcode} }), 'Non refund specific rules are not found');
414 my $default_refund = $builder->build(
416 source => 'CirculationRule',
419 categorycode => undef,
421 rule_name => 'refund',
426 my $specific_refund = $builder->build(
428 source => 'CirculationRule',
430 categorycode => undef,
432 rule_name => 'refund',
438 ok(defined Koha::RefundLostItemFeeRules->find({ branchcode => undef }), 'Refund default rules are found');
439 ok(defined Koha::RefundLostItemFeeRules->find({ branchcode => $specific_refund->{branchcode} }), 'Refund specific rules are found');
441 # Rollback transaction
442 $schema->storage->txn_rollback;