3c7b235f17944ff05e5134508d038a0090eea41b
[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 => 8;
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   => '*',
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   => '*',
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   => '*',
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   => '*',
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   => '*',
150             categorycode => undef,
151             itemtype     => undef,
152             rule_name    => 'refund',
153         }
154     )->next();
155     ok( !Koha::RefundLostItemFeeRules->_default_rule, 'Default rule is set to not refund' );
156
157     $default_rule->delete;
158     ok( !$default_rule->_result->in_storage, 'Default rule effectively deleted from storage' );
159
160     ok( Koha::RefundLostItemFeeRules->_default_rule, 'Default rule is set to refund if no default rule is present' );
161
162     # Rollback transaction
163     $schema->storage->txn_rollback;
164 };
165
166 subtest 'Koha::RefundLostItemFeeRules::_effective_branch_rule() tests' => sub {
167
168     plan tests => 3;
169
170     # Start transaction
171     $schema->storage->txn_begin;
172
173     # Clean the table
174     $schema->resultset('CirculationRule')->search()->delete;
175
176     my $default_rule = $builder->build(
177         {
178             source => 'CirculationRule',
179             value  => {
180                 branchcode   => '*',
181                 categorycode => undef,
182                 itemtype     => undef,
183                 rule_name    => 'refund',
184                 rule_value   => 1,
185             }
186         }
187     );
188     my $specific_rule_false = $builder->build(
189         {
190             source => 'CirculationRule',
191             value  => {
192                 categorycode => undef,
193                 itemtype     => undef,
194                 rule_name    => 'refund',
195                 rule_value   => 0,
196             }
197         }
198     );
199     my $specific_rule_true = $builder->build(
200         {
201             source => 'CirculationRule',
202             value  => {
203                 categorycode => undef,
204                 itemtype     => undef,
205                 rule_name    => 'refund',
206                 rule_value   => 1,
207             }
208         }
209     );
210
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)');
219
220     # Rollback transaction
221     $schema->storage->txn_rollback;
222 };
223
224 subtest 'Koha::RefundLostItemFeeRules::_choose_branch() tests' => sub {
225
226     plan tests => 9;
227
228     # Start transaction
229     $schema->storage->txn_begin;
230
231     my $params = {
232         current_branch => 'current_branch_code',
233         item_holding_branch => 'item_holding_branch_code',
234         item_home_branch => 'item_home_branch_code'
235     };
236
237     t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
238
239     is( Koha::RefundLostItemFeeRules->_choose_branch( $params ),
240         'current_branch_code', 'CheckinLibrary is honoured');
241
242     t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHomeBranch' );
243     is( Koha::RefundLostItemFeeRules->_choose_branch( $params ),
244         'item_home_branch_code', 'ItemHomeBranch is honoured');
245
246     t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHoldingBranch' );
247     is( Koha::RefundLostItemFeeRules->_choose_branch( $params ),
248         'item_holding_branch_code', 'ItemHoldingBranch is honoured');
249
250     t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
251     eval {
252         Koha::RefundLostItemFeeRules->_choose_branch();
253     };
254     is( ref($@), 'Koha::Exceptions::MissingParameter',
255         'Missing parameter exception' );
256     is( $@->message, 'CheckinLibrary requires the current_branch param',
257         'Exception message is correct' );
258
259     t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHomeBranch' );
260     eval {
261         Koha::RefundLostItemFeeRules->_choose_branch();
262     };
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' );
267
268     t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHoldingBranch' );
269     eval {
270         Koha::RefundLostItemFeeRules->_choose_branch();
271     };
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' );
276
277     # Rollback transaction
278     $schema->storage->txn_rollback;
279 };
280
281 subtest 'Koha::RefundLostItemFeeRules::should_refund() tests' => sub {
282
283     plan tests => 3;
284
285     # Start transaction
286     $schema->storage->txn_begin;
287
288     t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
289
290     $schema->resultset('CirculationRule')->search()->delete;
291
292     my $default_rule = $builder->build(
293         {
294             source => 'CirculationRule',
295             value  => {
296                 branchcode   => '*',
297                 categorycode => undef,
298                 itemtype     => undef,
299                 rule_name    => 'refund',
300                 rule_value   => 1
301             }
302         }
303     );
304     my $specific_rule_false = $builder->build(
305         {
306             source => 'CirculationRule',
307             value  => {
308                 categorycode => undef,
309                 itemtype     => undef,
310                 rule_name    => 'refund',
311                 rule_value   => 0
312             }
313         }
314     );
315     my $specific_rule_true = $builder->build(
316         {
317             source => 'CirculationRule',
318             value  => {
319                 categorycode => undef,
320                 itemtype     => undef,
321                 rule_name    => 'refund',
322                 rule_value   => 1
323             }
324         }
325     );
326     # Make sure we have an unused branchcode
327     my $specific_rule_dummy = $builder->build(
328         {
329             source => 'CirculationRule',
330             value  => {
331                 categorycode => undef,
332                 itemtype     => undef,
333                 rule_name    => 'refund',
334             }
335         }
336     );
337     my $branch_without_rule = $specific_rule_dummy->{ branchcode };
338     Koha::CirculationRules
339         ->search(
340             {
341                 branchcode   => $branch_without_rule,
342                 categorycode => undef,
343                 itemtype     => undef,
344                 rule_name    => 'refund'
345             }
346           )
347         ->next
348         ->delete;
349
350     my $params = {
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
355     };
356
357     t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
358     is( Koha::RefundLostItemFeeRules->should_refund( $params ),
359           1,'Specific rule is applied (true)');
360
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)');
364
365     # Change the default value just to try
366     Koha::CirculationRules->search({ branchcode => '*', 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)');
370
371     # Rollback transaction
372     $schema->storage->txn_rollback;
373 };
374