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 $branchcode = $builder->build( { source
=> 'Branch' } )->{branchcode
};
58 my $generated_other_rule = $builder->build(
60 source
=> 'CirculationRule',
62 branchcode
=> $branchcode,
63 categorycode
=> undef,
65 rule_name
=> 'refund',
70 my $default_rule = Koha
::CirculationRules
->search(
73 categorycode
=> undef,
75 rule_name
=> 'refund',
78 ok
( defined $default_rule, 'Default rule created' );
79 ok
( $default_rule->_result->in_storage, 'Default rule actually in storage');
81 my $other_rule = Koha
::CirculationRules
->search(
83 branchcode
=> $generated_other_rule->{branchcode
},
84 categorycode
=> undef,
86 rule_name
=> 'refund',
89 ok
( defined $other_rule, 'Other rule created' );
90 ok
( $other_rule->_result->in_storage, 'Other rule actually in storage');
92 # deleting the regular rule
94 ok
( !$other_rule->_result->in_storage, 'Other rule deleted from storage' );
96 # Rollback transaction
97 $schema->storage->txn_rollback;
100 subtest
'Koha::RefundLostItemFeeRules::_default_rule() tests' => sub {
105 $schema->storage->txn_begin;
108 $schema->resultset('CirculationRule')->search()->delete;
110 my $generated_default_rule = $builder->build(
112 source
=> 'CirculationRule',
115 categorycode
=> undef,
117 rule_name
=> 'refund',
122 my $branchcode = $builder->build( { source
=> 'Branch' } )->{branchcode
};
123 my $generated_other_rule = $builder->build(
125 source
=> 'CirculationRule',
127 branchcode
=> $branchcode,
128 categorycode
=> undef,
130 rule_name
=> 'refund',
135 my $default_rule = Koha
::CirculationRules
->search(
138 categorycode
=> undef,
140 rule_name
=> 'refund',
143 ok
( defined $default_rule, 'Default rule created' );
144 ok
( $default_rule->_result->in_storage, 'Default rule actually in storage');
145 is
( Koha
::RefundLostItemFeeRules
->_default_rule, 1, 'Default rule is set to refund' );
147 # Change default rule to "Don't refund"
148 $default_rule->rule_value(0);
149 $default_rule->store;
150 # Re-read from DB, to be sure
151 $default_rule = Koha
::CirculationRules
->search(
154 categorycode
=> undef,
156 rule_name
=> 'refund',
159 ok
( !Koha
::RefundLostItemFeeRules
->_default_rule, 'Default rule is set to not refund' );
161 $default_rule->delete;
162 ok
( !$default_rule->_result->in_storage, 'Default rule effectively deleted from storage' );
164 ok
( Koha
::RefundLostItemFeeRules
->_default_rule, 'Default rule is set to refund if no default rule is present' );
166 # Rollback transaction
167 $schema->storage->txn_rollback;
170 subtest
'Koha::RefundLostItemFeeRules::_effective_branch_rule() tests' => sub {
175 $schema->storage->txn_begin;
178 $schema->resultset('CirculationRule')->search()->delete;
180 my $default_rule = $builder->build(
182 source
=> 'CirculationRule',
185 categorycode
=> undef,
187 rule_name
=> 'refund',
192 my $branchcode = $builder->build( { source
=> 'Branch' } )->{branchcode
};
193 my $specific_rule_false = $builder->build(
195 source
=> 'CirculationRule',
197 branchcode
=> $branchcode,
198 categorycode
=> undef,
200 rule_name
=> 'refund',
205 my $branchcode2 = $builder->build( { source
=> 'Branch' } )->{branchcode
};
206 my $specific_rule_true = $builder->build(
208 source
=> 'CirculationRule',
210 branchcode
=> $branchcode2,
211 categorycode
=> undef,
213 rule_name
=> 'refund',
219 is
( Koha
::RefundLostItemFeeRules
->_effective_branch_rule( $specific_rule_true->{ branchcode
} ),
220 1,'Specific rule is applied (true)');
221 is
( Koha
::RefundLostItemFeeRules
->_effective_branch_rule( $specific_rule_false->{ branchcode
} ),
222 0,'Specific rule is applied (false)');
223 # Delete specific rules
224 Koha
::RefundLostItemFeeRules
->find({ branchcode
=> $specific_rule_false->{ branchcode
} })->delete;
225 is
( Koha
::RefundLostItemFeeRules
->_effective_branch_rule( $specific_rule_false->{ branchcode
} ),
226 1,'No specific rule defined, fallback to global (true)');
228 # Rollback transaction
229 $schema->storage->txn_rollback;
232 subtest
'Koha::RefundLostItemFeeRules::_choose_branch() tests' => sub {
237 $schema->storage->txn_begin;
240 current_branch
=> 'current_branch_code',
241 item_holding_branch
=> 'item_holding_branch_code',
242 item_home_branch
=> 'item_home_branch_code'
245 t
::lib
::Mocks
::mock_preference
( 'RefundLostOnReturnControl', 'CheckinLibrary' );
247 is
( Koha
::RefundLostItemFeeRules
->_choose_branch( $params ),
248 'current_branch_code', 'CheckinLibrary is honoured');
250 t
::lib
::Mocks
::mock_preference
( 'RefundLostOnReturnControl', 'ItemHomeBranch' );
251 is
( Koha
::RefundLostItemFeeRules
->_choose_branch( $params ),
252 'item_home_branch_code', 'ItemHomeBranch is honoured');
254 t
::lib
::Mocks
::mock_preference
( 'RefundLostOnReturnControl', 'ItemHoldingBranch' );
255 is
( Koha
::RefundLostItemFeeRules
->_choose_branch( $params ),
256 'item_holding_branch_code', 'ItemHoldingBranch is honoured');
258 t
::lib
::Mocks
::mock_preference
( 'RefundLostOnReturnControl', 'CheckinLibrary' );
260 Koha
::RefundLostItemFeeRules
->_choose_branch();
262 is
( ref($@
), 'Koha::Exceptions::MissingParameter',
263 'Missing parameter exception' );
264 is
( $@
->message, 'CheckinLibrary requires the current_branch param',
265 'Exception message is correct' );
267 t
::lib
::Mocks
::mock_preference
( 'RefundLostOnReturnControl', 'ItemHomeBranch' );
269 Koha
::RefundLostItemFeeRules
->_choose_branch();
271 is
( ref($@
), 'Koha::Exceptions::MissingParameter',
272 'Missing parameter exception' );
273 is
( $@
->message, 'ItemHomeBranch requires the item_home_branch param',
274 'Exception message is correct' );
276 t
::lib
::Mocks
::mock_preference
( 'RefundLostOnReturnControl', 'ItemHoldingBranch' );
278 Koha
::RefundLostItemFeeRules
->_choose_branch();
280 is
( ref($@
), 'Koha::Exceptions::MissingParameter',
281 'Missing parameter exception' );
282 is
( $@
->message, 'ItemHoldingBranch requires the item_holding_branch param',
283 'Exception message is correct' );
285 # Rollback transaction
286 $schema->storage->txn_rollback;
289 subtest
'Koha::RefundLostItemFeeRules::should_refund() tests' => sub {
294 $schema->storage->txn_begin;
296 t
::lib
::Mocks
::mock_preference
( 'RefundLostOnReturnControl', 'CheckinLibrary' );
298 $schema->resultset('CirculationRule')->search()->delete;
300 my $default_rule = $builder->build(
302 source
=> 'CirculationRule',
305 categorycode
=> undef,
307 rule_name
=> 'refund',
312 my $branchcode = $builder->build( { source
=> 'Branch' } )->{branchcode
};
313 my $specific_rule_false = $builder->build(
315 source
=> 'CirculationRule',
317 branchcode
=> $branchcode,
318 categorycode
=> undef,
320 rule_name
=> 'refund',
325 my $branchcode2 = $builder->build( { source
=> 'Branch' } )->{branchcode
};
326 my $specific_rule_true = $builder->build(
328 source
=> 'CirculationRule',
330 branchcode
=> $branchcode2,
331 categorycode
=> undef,
333 rule_name
=> 'refund',
338 # Make sure we have an unused branchcode
339 my $branchcode3 = $builder->build( { source
=> 'Branch' } )->{branchcode
};
340 my $specific_rule_dummy = $builder->build(
342 source
=> 'CirculationRule',
344 branchcode
=> $branchcode3,
345 categorycode
=> undef,
347 rule_name
=> 'refund',
351 my $branch_without_rule = $specific_rule_dummy->{ branchcode
};
352 Koha
::CirculationRules
355 branchcode
=> $branch_without_rule,
356 categorycode
=> undef,
358 rule_name
=> 'refund'
365 current_branch
=> $specific_rule_true->{ branchcode
},
366 # patron_branch => $specific_rule_false->{ branchcode },
367 item_holding_branch
=> $branch_without_rule,
368 item_home_branch
=> $branch_without_rule
371 t
::lib
::Mocks
::mock_preference
( 'RefundLostOnReturnControl', 'CheckinLibrary' );
372 is
( Koha
::RefundLostItemFeeRules
->should_refund( $params ),
373 1,'Specific rule is applied (true)');
375 t
::lib
::Mocks
::mock_preference
( 'RefundLostOnReturnControl', 'ItemHomeBranch' );
376 is
( Koha
::RefundLostItemFeeRules
->should_refund( $params ),
377 1,'No rule for branch, global rule applied (true)');
379 # Change the default value just to try
380 Koha
::CirculationRules
->search({ branchcode
=> undef, rule_name
=> 'refund' })->next->rule_value(0)->store;
381 t
::lib
::Mocks
::mock_preference
( 'RefundLostOnReturnControl', 'ItemHoldingBranch' );
382 is
( Koha
::RefundLostItemFeeRules
->should_refund( $params ),
383 0,'No rule for branch, global rule applied (false)');
385 # Rollback transaction
386 $schema->storage->txn_rollback;
389 subtest
'Koha::RefundLostItemFeeRules::find() tests' => sub {
394 $schema->storage->txn_begin;
396 t
::lib
::Mocks
::mock_preference
( 'RefundLostOnReturnControl', 'CheckinLibrary' );
398 $schema->resultset('CirculationRule')->search()->delete;
400 my $default_non_refund = $builder->build(
402 source
=> 'CirculationRule',
405 categorycode
=> undef,
407 rule_name
=> 'non_refund_rule',
413 ok
(defined Koha
::RefundLostItemFeeRules
->find($default_non_refund->{id
}), 'Find should continue to work when passed an id');
415 my $specific_non_refund = $builder->build(
417 source
=> 'CirculationRule',
419 categorycode
=> undef,
421 rule_name
=> 'non_refund_rule',
427 ok
(!defined Koha
::RefundLostItemFeeRules
->find({ branchcode
=> undef }), 'Non refund default rules are not found');
428 ok
(!defined Koha
::RefundLostItemFeeRules
->find({ branchcode
=> $specific_non_refund->{branchcode
} }), 'Non refund specific rules are not found');
430 my $default_refund = $builder->build(
432 source
=> 'CirculationRule',
435 categorycode
=> undef,
437 rule_name
=> 'refund',
442 my $specific_refund = $builder->build(
444 source
=> 'CirculationRule',
446 categorycode
=> undef,
448 rule_name
=> 'refund',
454 ok
(defined Koha
::RefundLostItemFeeRules
->find({ branchcode
=> undef }), 'Refund default rules are found');
455 ok
(defined Koha
::RefundLostItemFeeRules
->find({ branchcode
=> $specific_refund->{branchcode
} }), 'Refund specific rules are found');
457 # Rollback transaction
458 $schema->storage->txn_rollback;