Bug 18930: (follow-up) Amend Koha::RefundLostItemFeeRules->find
[koha.git] / t / db_dependent / RefundLostItemFeeRule.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Test::More tests => 9;
21 use t::lib::Mocks;
22 use t::lib::TestBuilder;
23
24 use C4::Context;
25 use Koha::Database;
26
27 BEGIN {
28     use_ok('Koha::Object');
29     use_ok('Koha::CirculationRule');
30     use_ok('Koha::RefundLostItemFeeRules');
31 }
32
33 my $schema = Koha::Database->new->schema;
34 my $builder = t::lib::TestBuilder->new;
35
36 subtest 'Koha::RefundLostItemFeeRule::delete() tests' => sub {
37
38     plan tests => 5;
39
40     # Start transaction
41     $schema->storage->txn_begin;
42
43     # Clean the table
44     $schema->resultset('CirculationRule')->search()->delete;
45
46     my $generated_default_rule = $builder->build(
47         {
48             source => 'CirculationRule',
49             value  => {
50                 branchcode   => undef,
51                 categorycode => undef,
52                 itemtype     => undef,
53                 rule_name    => 'refund',
54             }
55         }
56     );
57     my $generated_other_rule = $builder->build(
58         {
59             source => 'CirculationRule',
60             value  => {
61                 categorycode => undef,
62                 itemtype     => undef,
63                 rule_name    => 'refund',
64             }
65         }
66     );
67
68     my $default_rule = Koha::CirculationRules->search(
69         {
70             branchcode   => undef,
71             categorycode => undef,
72             itemtype     => undef,
73             rule_name    => 'refund',
74         }
75     )->next();
76     ok( defined $default_rule, 'Default rule created' );
77     ok( $default_rule->_result->in_storage, 'Default rule actually in storage');
78
79     my $other_rule = Koha::CirculationRules->search(
80         {
81             branchcode   => $generated_other_rule->{branchcode},
82             categorycode => undef,
83             itemtype     => undef,
84             rule_name    => 'refund',
85         }
86     )->next();
87     ok( defined $other_rule, 'Other rule created' );
88     ok( $other_rule->_result->in_storage, 'Other rule actually in storage');
89
90     # deleting the regular rule
91     $other_rule->delete;
92     ok( !$other_rule->_result->in_storage, 'Other rule deleted from storage' );
93
94     # Rollback transaction
95     $schema->storage->txn_rollback;
96 };
97
98 subtest 'Koha::RefundLostItemFeeRules::_default_rule() tests' => sub {
99
100     plan tests => 6;
101
102     # Start transaction
103     $schema->storage->txn_begin;
104
105     # Clean the table
106     $schema->resultset('CirculationRule')->search()->delete;
107
108     my $generated_default_rule = $builder->build(
109         {
110             source => 'CirculationRule',
111             value  => {
112                 branchcode   => undef,
113                 categorycode => undef,
114                 itemtype     => undef,
115                 rule_name    => 'refund',
116                 rule_value   => 1,
117             }
118         }
119     );
120     my $generated_other_rule = $builder->build(
121         {
122             source => 'CirculationRule',
123             value  => {
124                 categorycode => undef,
125                 itemtype     => undef,
126                 rule_name    => 'refund',
127             }
128         }
129     );
130
131     my $default_rule = Koha::CirculationRules->search(
132         {
133             branchcode   => undef,
134             categorycode => undef,
135             itemtype     => undef,
136             rule_name    => 'refund',
137         }
138     )->next();
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' );
142
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(
148         {
149             branchcode   => undef,
150             categorycode => undef,
151             itemtype     => undef,
152             rule_name    => 'refund',
153         }
154     )->next();
155     use Data::Printer colored => 1;
156     ok( !Koha::RefundLostItemFeeRules->_default_rule, 'Default rule is set to not refund' );
157
158     $default_rule->delete;
159     ok( !$default_rule->_result->in_storage, 'Default rule effectively deleted from storage' );
160
161     ok( Koha::RefundLostItemFeeRules->_default_rule, 'Default rule is set to refund if no default rule is present' );
162
163     # Rollback transaction
164     $schema->storage->txn_rollback;
165 };
166
167 subtest 'Koha::RefundLostItemFeeRules::_effective_branch_rule() tests' => sub {
168
169     plan tests => 3;
170
171     # Start transaction
172     $schema->storage->txn_begin;
173
174     # Clean the table
175     $schema->resultset('CirculationRule')->search()->delete;
176
177     my $default_rule = $builder->build(
178         {
179             source => 'CirculationRule',
180             value  => {
181                 branchcode   => undef,
182                 categorycode => undef,
183                 itemtype     => undef,
184                 rule_name    => 'refund',
185                 rule_value   => 1,
186             }
187         }
188     );
189     my $specific_rule_false = $builder->build(
190         {
191             source => 'CirculationRule',
192             value  => {
193                 categorycode => undef,
194                 itemtype     => undef,
195                 rule_name    => 'refund',
196                 rule_value   => 0,
197             }
198         }
199     );
200     my $specific_rule_true = $builder->build(
201         {
202             source => 'CirculationRule',
203             value  => {
204                 categorycode => undef,
205                 itemtype     => undef,
206                 rule_name    => 'refund',
207                 rule_value   => 1,
208             }
209         }
210     );
211
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)');
220
221     # Rollback transaction
222     $schema->storage->txn_rollback;
223 };
224
225 subtest 'Koha::RefundLostItemFeeRules::_choose_branch() tests' => sub {
226
227     plan tests => 9;
228
229     # Start transaction
230     $schema->storage->txn_begin;
231
232     my $params = {
233         current_branch => 'current_branch_code',
234         item_holding_branch => 'item_holding_branch_code',
235         item_home_branch => 'item_home_branch_code'
236     };
237
238     t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
239
240     is( Koha::RefundLostItemFeeRules->_choose_branch( $params ),
241         'current_branch_code', 'CheckinLibrary is honoured');
242
243     t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHomeBranch' );
244     is( Koha::RefundLostItemFeeRules->_choose_branch( $params ),
245         'item_home_branch_code', 'ItemHomeBranch is honoured');
246
247     t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHoldingBranch' );
248     is( Koha::RefundLostItemFeeRules->_choose_branch( $params ),
249         'item_holding_branch_code', 'ItemHoldingBranch is honoured');
250
251     t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
252     eval {
253         Koha::RefundLostItemFeeRules->_choose_branch();
254     };
255     is( ref($@), 'Koha::Exceptions::MissingParameter',
256         'Missing parameter exception' );
257     is( $@->message, 'CheckinLibrary requires the current_branch param',
258         'Exception message is correct' );
259
260     t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHomeBranch' );
261     eval {
262         Koha::RefundLostItemFeeRules->_choose_branch();
263     };
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' );
268
269     t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHoldingBranch' );
270     eval {
271         Koha::RefundLostItemFeeRules->_choose_branch();
272     };
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' );
277
278     # Rollback transaction
279     $schema->storage->txn_rollback;
280 };
281
282 subtest 'Koha::RefundLostItemFeeRules::should_refund() tests' => sub {
283
284     plan tests => 3;
285
286     # Start transaction
287     $schema->storage->txn_begin;
288
289     t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
290
291     $schema->resultset('CirculationRule')->search()->delete;
292
293     my $default_rule = $builder->build(
294         {
295             source => 'CirculationRule',
296             value  => {
297                 branchcode   => undef,
298                 categorycode => undef,
299                 itemtype     => undef,
300                 rule_name    => 'refund',
301                 rule_value   => 1
302             }
303         }
304     );
305     my $specific_rule_false = $builder->build(
306         {
307             source => 'CirculationRule',
308             value  => {
309                 categorycode => undef,
310                 itemtype     => undef,
311                 rule_name    => 'refund',
312                 rule_value   => 0
313             }
314         }
315     );
316     my $specific_rule_true = $builder->build(
317         {
318             source => 'CirculationRule',
319             value  => {
320                 categorycode => undef,
321                 itemtype     => undef,
322                 rule_name    => 'refund',
323                 rule_value   => 1
324             }
325         }
326     );
327     # Make sure we have an unused branchcode
328     my $specific_rule_dummy = $builder->build(
329         {
330             source => 'CirculationRule',
331             value  => {
332                 categorycode => undef,
333                 itemtype     => undef,
334                 rule_name    => 'refund',
335             }
336         }
337     );
338     my $branch_without_rule = $specific_rule_dummy->{ branchcode };
339     Koha::CirculationRules
340         ->search(
341             {
342                 branchcode   => $branch_without_rule,
343                 categorycode => undef,
344                 itemtype     => undef,
345                 rule_name    => 'refund'
346             }
347           )
348         ->next
349         ->delete;
350
351     my $params = {
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
356     };
357
358     t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
359     is( Koha::RefundLostItemFeeRules->should_refund( $params ),
360           1,'Specific rule is applied (true)');
361
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)');
365
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)');
371
372     # Rollback transaction
373     $schema->storage->txn_rollback;
374 };
375
376 subtest 'Koha::RefundLostItemFeeRules::find() tests' => sub {
377
378     plan tests => 4;
379
380     # Start transaction
381     $schema->storage->txn_begin;
382
383     t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
384
385     $schema->resultset('CirculationRule')->search()->delete;
386
387     my $default_non_refund = $builder->build(
388         {
389             source => 'CirculationRule',
390             value  => {
391                 branchcode   => undef,
392                 categorycode => undef,
393                 itemtype     => undef,
394                 rule_name    => 'non_refund_rule',
395                 rule_value   => 1
396             }
397         }
398     );
399     my $specific_non_refund = $builder->build(
400         {
401             source => 'CirculationRule',
402             value  => {
403                 categorycode => undef,
404                 itemtype     => undef,
405                 rule_name    => 'non_refund_rule',
406                 rule_value   => 0
407             }
408         }
409     );
410
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');
413
414     my $default_refund = $builder->build(
415         {
416             source => 'CirculationRule',
417             value  => {
418                 branchcode   => undef,
419                 categorycode => undef,
420                 itemtype     => undef,
421                 rule_name    => 'refund',
422                 rule_value   => 1
423             }
424         }
425     );
426     my $specific_refund = $builder->build(
427         {
428             source => 'CirculationRule',
429             value  => {
430                 categorycode => undef,
431                 itemtype     => undef,
432                 rule_name    => 'refund',
433                 rule_value   => 0
434             }
435         }
436     );
437
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');
440
441     # Rollback transaction
442     $schema->storage->txn_rollback;
443 };