Bug 22087: Add missing filters
[koha.git] / t / db_dependent / RefundLostItemFeeRule.t
blobda53f3ab1bcde8c7e6510854db91c308aa028569
1 #!/usr/bin/perl
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>.
18 use Modern::Perl;
20 use Test::More tests => 9;
21 use t::lib::Mocks;
22 use t::lib::TestBuilder;
24 use C4::Context;
25 use Koha::Database;
27 BEGIN {
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 {
38 plan tests => 5;
40 # Start transaction
41 $schema->storage->txn_begin;
43 # Clean the table
44 $schema->resultset('CirculationRule')->search()->delete;
46 my $generated_default_rule = $builder->build(
48 source => 'CirculationRule',
49 value => {
50 branchcode => undef,
51 categorycode => undef,
52 itemtype => undef,
53 rule_name => 'refund',
57 my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
58 my $generated_other_rule = $builder->build(
60 source => 'CirculationRule',
61 value => {
62 branchcode => $branchcode,
63 categorycode => undef,
64 itemtype => undef,
65 rule_name => 'refund',
70 my $default_rule = Koha::CirculationRules->search(
72 branchcode => undef,
73 categorycode => undef,
74 itemtype => undef,
75 rule_name => 'refund',
77 )->next();
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,
85 itemtype => undef,
86 rule_name => 'refund',
88 )->next();
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
93 $other_rule->delete;
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 {
102 plan tests => 6;
104 # Start transaction
105 $schema->storage->txn_begin;
107 # Clean the table
108 $schema->resultset('CirculationRule')->search()->delete;
110 my $generated_default_rule = $builder->build(
112 source => 'CirculationRule',
113 value => {
114 branchcode => undef,
115 categorycode => undef,
116 itemtype => undef,
117 rule_name => 'refund',
118 rule_value => 1,
122 my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
123 my $generated_other_rule = $builder->build(
125 source => 'CirculationRule',
126 value => {
127 branchcode => $branchcode,
128 categorycode => undef,
129 itemtype => undef,
130 rule_name => 'refund',
135 my $default_rule = Koha::CirculationRules->search(
137 branchcode => undef,
138 categorycode => undef,
139 itemtype => undef,
140 rule_name => 'refund',
142 )->next();
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(
153 branchcode => undef,
154 categorycode => undef,
155 itemtype => undef,
156 rule_name => 'refund',
158 )->next();
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 {
172 plan tests => 3;
174 # Start transaction
175 $schema->storage->txn_begin;
177 # Clean the table
178 $schema->resultset('CirculationRule')->search()->delete;
180 my $default_rule = $builder->build(
182 source => 'CirculationRule',
183 value => {
184 branchcode => undef,
185 categorycode => undef,
186 itemtype => undef,
187 rule_name => 'refund',
188 rule_value => 1,
192 my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
193 my $specific_rule_false = $builder->build(
195 source => 'CirculationRule',
196 value => {
197 branchcode => $branchcode,
198 categorycode => undef,
199 itemtype => undef,
200 rule_name => 'refund',
201 rule_value => 0,
205 my $branchcode2 = $builder->build( { source => 'Branch' } )->{branchcode};
206 my $specific_rule_true = $builder->build(
208 source => 'CirculationRule',
209 value => {
210 branchcode => $branchcode2,
211 categorycode => undef,
212 itemtype => undef,
213 rule_name => 'refund',
214 rule_value => 1,
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 {
234 plan tests => 9;
236 # Start transaction
237 $schema->storage->txn_begin;
239 my $params = {
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' );
259 eval {
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' );
268 eval {
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' );
277 eval {
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 {
291 plan tests => 3;
293 # Start transaction
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',
303 value => {
304 branchcode => undef,
305 categorycode => undef,
306 itemtype => undef,
307 rule_name => 'refund',
308 rule_value => 1
312 my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
313 my $specific_rule_false = $builder->build(
315 source => 'CirculationRule',
316 value => {
317 branchcode => $branchcode,
318 categorycode => undef,
319 itemtype => undef,
320 rule_name => 'refund',
321 rule_value => 0
325 my $branchcode2 = $builder->build( { source => 'Branch' } )->{branchcode};
326 my $specific_rule_true = $builder->build(
328 source => 'CirculationRule',
329 value => {
330 branchcode => $branchcode2,
331 categorycode => undef,
332 itemtype => undef,
333 rule_name => 'refund',
334 rule_value => 1
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',
343 value => {
344 branchcode => $branchcode3,
345 categorycode => undef,
346 itemtype => undef,
347 rule_name => 'refund',
351 my $branch_without_rule = $specific_rule_dummy->{ branchcode };
352 Koha::CirculationRules
353 ->search(
355 branchcode => $branch_without_rule,
356 categorycode => undef,
357 itemtype => undef,
358 rule_name => 'refund'
361 ->next
362 ->delete;
364 my $params = {
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 {
391 plan tests => 5;
393 # Start transaction
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',
403 value => {
404 branchcode => undef,
405 categorycode => undef,
406 itemtype => undef,
407 rule_name => 'non_refund_rule',
408 rule_value => 1
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',
418 value => {
419 categorycode => undef,
420 itemtype => undef,
421 rule_name => 'non_refund_rule',
422 rule_value => 0
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',
433 value => {
434 branchcode => undef,
435 categorycode => undef,
436 itemtype => undef,
437 rule_name => 'refund',
438 rule_value => 1
442 my $specific_refund = $builder->build(
444 source => 'CirculationRule',
445 value => {
446 categorycode => undef,
447 itemtype => undef,
448 rule_name => 'refund',
449 rule_value => 0
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;