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