84a373a008bf5215e2ec17b14c040cfcc2029278
[koha-equinox.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::RefundLostItemFeeRule');
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('RefundLostItemFeeRule')->search()->delete;
45
46     my $generated_default_rule = $builder->build({
47         source => 'RefundLostItemFeeRule',
48         value  => {
49             branchcode => '*'
50         }
51     });
52     my $generated_other_rule = $builder->build({
53         source => 'RefundLostItemFeeRule'
54     });
55
56     my $default_rule = Koha::RefundLostItemFeeRules->find({
57             branchcode => '*' });
58     ok( defined $default_rule, 'Default rule created' );
59     ok( $default_rule->in_storage, 'Default rule actually in storage');
60
61     my $other_rule = Koha::RefundLostItemFeeRules->find({
62             branchcode => $generated_other_rule->{ branchcode }
63     });
64     ok( defined $other_rule, 'Other rule created' );
65     ok( $other_rule->in_storage, 'Other rule actually in storage');
66
67     # deleting the regular rule
68     $other_rule->delete;
69     ok( !$other_rule->in_storage, 'Other rule deleted from storage' );
70
71     # Rollback transaction
72     $schema->storage->txn_rollback;
73 };
74
75 subtest 'Koha::RefundLostItemFeeRules::_default_rule() tests' => sub {
76
77     plan tests => 6;
78
79     # Start transaction
80     $schema->storage->txn_begin;
81
82     # Clean the table
83     $schema->resultset('RefundLostItemFeeRule')->search()->delete;
84
85     my $generated_default_rule = $builder->build({
86         source => 'RefundLostItemFeeRule',
87         value  => {
88             branchcode => '*',
89             refund     => 1
90         }
91     });
92     my $generated_other_rule = $builder->build({
93         source => 'RefundLostItemFeeRule'
94     });
95
96     my $default_rule = Koha::RefundLostItemFeeRules->find({
97             branchcode => '*' });
98     ok( defined $default_rule, 'Default rule created' );
99     ok( $default_rule->in_storage, 'Default rule actually in storage');
100     ok( Koha::RefundLostItemFeeRules->_default_rule, 'Default rule is set to refund' );
101
102     # Change default rule to "Don't refund"
103     $default_rule->refund(0);
104     $default_rule->store;
105     # Re-read from DB, to be sure
106     $default_rule = Koha::RefundLostItemFeeRules->find({
107             branchcode => '*' });
108     ok( !Koha::RefundLostItemFeeRules->_default_rule, 'Default rule is set to not refund' );
109
110     $default_rule->delete;
111     ok( !$default_rule->in_storage, 'Default rule effectively deleted from storage' );
112
113     ok( Koha::RefundLostItemFeeRules->_default_rule, 'Default rule is set to refund if no default rule is present' );
114
115     # Rollback transaction
116     $schema->storage->txn_rollback;
117 };
118
119 subtest 'Koha::RefundLostItemFeeRules::_effective_branch_rule() tests' => sub {
120
121     plan tests => 3;
122
123     # Start transaction
124     $schema->storage->txn_begin;
125
126     # Clean the table
127     $schema->resultset('RefundLostItemFeeRule')->search()->delete;
128
129     my $default_rule = $builder->build({
130         source => 'RefundLostItemFeeRule',
131         value  => {
132             branchcode => '*',
133             refund     => 1
134         }
135     });
136     my $specific_rule_false = $builder->build({
137         source => 'RefundLostItemFeeRule',
138         value  => {
139             refund => 0
140         }
141     });
142     my $specific_rule_true = $builder->build({
143         source => 'RefundLostItemFeeRule',
144         value  => {
145             refund => 1
146         }
147     });
148
149     is( Koha::RefundLostItemFeeRules->_effective_branch_rule( $specific_rule_true->{ branchcode } ),
150           1,'Specific rule is applied (true)');
151     is( Koha::RefundLostItemFeeRules->_effective_branch_rule( $specific_rule_false->{ branchcode } ),
152           0,'Specific rule is applied (false)');
153     # Delete specific rules
154     Koha::RefundLostItemFeeRules->find({ branchcode => $specific_rule_false->{ branchcode } })->delete;
155     is( Koha::RefundLostItemFeeRules->_effective_branch_rule( $specific_rule_false->{ branchcode } ),
156           1,'No specific rule defined, fallback to global (true)');
157
158     # Rollback transaction
159     $schema->storage->txn_rollback;
160 };
161
162 subtest 'Koha::RefundLostItemFeeRules::_choose_branch() tests' => sub {
163
164     plan tests => 9;
165
166     # Start transaction
167     $schema->storage->txn_begin;
168
169     my $params = {
170         current_branch => 'current_branch_code',
171         item_holding_branch => 'item_holding_branch_code',
172         item_home_branch => 'item_home_branch_code'
173     };
174
175     t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
176
177     is( Koha::RefundLostItemFeeRules->_choose_branch( $params ),
178         'current_branch_code', 'CheckinLibrary is honoured');
179
180     t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHomeBranch' );
181     is( Koha::RefundLostItemFeeRules->_choose_branch( $params ),
182         'item_home_branch_code', 'ItemHomeBranch is honoured');
183
184     t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHoldingBranch' );
185     is( Koha::RefundLostItemFeeRules->_choose_branch( $params ),
186         'item_holding_branch_code', 'ItemHoldingBranch is honoured');
187
188     t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
189     eval {
190         Koha::RefundLostItemFeeRules->_choose_branch();
191     };
192     is( ref($@), 'Koha::Exceptions::MissingParameter',
193         'Missing parameter exception' );
194     is( $@->message, 'CheckinLibrary requires the current_branch param',
195         'Exception message is correct' );
196
197     t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHomeBranch' );
198     eval {
199         Koha::RefundLostItemFeeRules->_choose_branch();
200     };
201     is( ref($@), 'Koha::Exceptions::MissingParameter',
202         'Missing parameter exception' );
203     is( $@->message, 'ItemHomeBranch requires the item_home_branch param',
204         'Exception message is correct' );
205
206     t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHoldingBranch' );
207     eval {
208         Koha::RefundLostItemFeeRules->_choose_branch();
209     };
210     is( ref($@), 'Koha::Exceptions::MissingParameter',
211         'Missing parameter exception' );
212     is( $@->message, 'ItemHoldingBranch requires the item_holding_branch param',
213         'Exception message is correct' );
214
215     # Rollback transaction
216     $schema->storage->txn_rollback;
217 };
218
219 subtest 'Koha::RefundLostItemFeeRules::should_refund() tests' => sub {
220
221     plan tests => 3;
222
223     # Start transaction
224     $schema->storage->txn_begin;
225
226     t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
227
228     $schema->resultset('RefundLostItemFeeRule')->search()->delete;
229
230     my $default_rule = $builder->build({
231         source => 'RefundLostItemFeeRule',
232         value  => {
233             branchcode => '*',
234             refund     => 1
235         }
236     });
237     my $specific_rule_false = $builder->build({
238         source => 'RefundLostItemFeeRule',
239         value  => {
240             refund => 0
241         }
242     });
243     my $specific_rule_true = $builder->build({
244         source => 'RefundLostItemFeeRule',
245         value  => {
246             refund => 1
247         }
248     });
249     # Make sure we have an unused branchcode
250     my $specific_rule_dummy = $builder->build({
251         source => 'RefundLostItemFeeRule'
252     });
253     my $branch_without_rule = $specific_rule_dummy->{ branchcode };
254     Koha::RefundLostItemFeeRules
255         ->find({ branchcode => $branch_without_rule })
256         ->delete;
257
258     my $params = {
259         current_branch => $specific_rule_true->{ branchcode },
260         # patron_branch  => $specific_rule_false->{ branchcode },
261         item_holding_branch => $branch_without_rule,
262         item_home_branch => $branch_without_rule
263     };
264
265     t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
266     is( Koha::RefundLostItemFeeRules->should_refund( $params ),
267           1,'Specific rule is applied (true)');
268
269     t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHomeBranch' );
270     is( Koha::RefundLostItemFeeRules->should_refund( $params ),
271          1,'No rule for branch, global rule applied (true)');
272
273     # Change the default value just to try
274     Koha::RefundLostItemFeeRules->find({ branchcode => '*' })->refund(0)->store;
275     t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHoldingBranch' );
276     is( Koha::RefundLostItemFeeRules->should_refund( $params ),
277          0,'No rule for branch, global rule applied (false)');
278
279     # Rollback transaction
280     $schema->storage->txn_rollback;
281 };
282