Bug 20434: Update UNIMARC framework - authorised values
[koha.git] / t / db_dependent / RefundLostItemFeeRule.t
blob459916596130637fd2717cf1aff2bfbb5675a6a6
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 $generated_other_rule = $builder->build(
59 source => 'CirculationRule',
60 value => {
61 categorycode => undef,
62 itemtype => undef,
63 rule_name => 'refund',
68 my $default_rule = Koha::CirculationRules->search(
70 branchcode => undef,
71 categorycode => undef,
72 itemtype => undef,
73 rule_name => 'refund',
75 )->next();
76 ok( defined $default_rule, 'Default rule created' );
77 ok( $default_rule->_result->in_storage, 'Default rule actually in storage');
79 my $other_rule = Koha::CirculationRules->search(
81 branchcode => $generated_other_rule->{branchcode},
82 categorycode => undef,
83 itemtype => undef,
84 rule_name => 'refund',
86 )->next();
87 ok( defined $other_rule, 'Other rule created' );
88 ok( $other_rule->_result->in_storage, 'Other rule actually in storage');
90 # deleting the regular rule
91 $other_rule->delete;
92 ok( !$other_rule->_result->in_storage, 'Other rule deleted from storage' );
94 # Rollback transaction
95 $schema->storage->txn_rollback;
98 subtest 'Koha::RefundLostItemFeeRules::_default_rule() tests' => sub {
100 plan tests => 6;
102 # Start transaction
103 $schema->storage->txn_begin;
105 # Clean the table
106 $schema->resultset('CirculationRule')->search()->delete;
108 my $generated_default_rule = $builder->build(
110 source => 'CirculationRule',
111 value => {
112 branchcode => undef,
113 categorycode => undef,
114 itemtype => undef,
115 rule_name => 'refund',
116 rule_value => 1,
120 my $generated_other_rule = $builder->build(
122 source => 'CirculationRule',
123 value => {
124 categorycode => undef,
125 itemtype => undef,
126 rule_name => 'refund',
131 my $default_rule = Koha::CirculationRules->search(
133 branchcode => undef,
134 categorycode => undef,
135 itemtype => undef,
136 rule_name => 'refund',
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' );
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(
149 branchcode => undef,
150 categorycode => undef,
151 itemtype => undef,
152 rule_name => 'refund',
154 )->next();
155 ok( !Koha::RefundLostItemFeeRules->_default_rule, 'Default rule is set to not refund' );
157 $default_rule->delete;
158 ok( !$default_rule->_result->in_storage, 'Default rule effectively deleted from storage' );
160 ok( Koha::RefundLostItemFeeRules->_default_rule, 'Default rule is set to refund if no default rule is present' );
162 # Rollback transaction
163 $schema->storage->txn_rollback;
166 subtest 'Koha::RefundLostItemFeeRules::_effective_branch_rule() tests' => sub {
168 plan tests => 3;
170 # Start transaction
171 $schema->storage->txn_begin;
173 # Clean the table
174 $schema->resultset('CirculationRule')->search()->delete;
176 my $default_rule = $builder->build(
178 source => 'CirculationRule',
179 value => {
180 branchcode => undef,
181 categorycode => undef,
182 itemtype => undef,
183 rule_name => 'refund',
184 rule_value => 1,
188 my $specific_rule_false = $builder->build(
190 source => 'CirculationRule',
191 value => {
192 categorycode => undef,
193 itemtype => undef,
194 rule_name => 'refund',
195 rule_value => 0,
199 my $specific_rule_true = $builder->build(
201 source => 'CirculationRule',
202 value => {
203 categorycode => undef,
204 itemtype => undef,
205 rule_name => 'refund',
206 rule_value => 1,
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)');
220 # Rollback transaction
221 $schema->storage->txn_rollback;
224 subtest 'Koha::RefundLostItemFeeRules::_choose_branch() tests' => sub {
226 plan tests => 9;
228 # Start transaction
229 $schema->storage->txn_begin;
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'
237 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
239 is( Koha::RefundLostItemFeeRules->_choose_branch( $params ),
240 'current_branch_code', 'CheckinLibrary is honoured');
242 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHomeBranch' );
243 is( Koha::RefundLostItemFeeRules->_choose_branch( $params ),
244 'item_home_branch_code', 'ItemHomeBranch is honoured');
246 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHoldingBranch' );
247 is( Koha::RefundLostItemFeeRules->_choose_branch( $params ),
248 'item_holding_branch_code', 'ItemHoldingBranch is honoured');
250 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
251 eval {
252 Koha::RefundLostItemFeeRules->_choose_branch();
254 is( ref($@), 'Koha::Exceptions::MissingParameter',
255 'Missing parameter exception' );
256 is( $@->message, 'CheckinLibrary requires the current_branch param',
257 'Exception message is correct' );
259 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHomeBranch' );
260 eval {
261 Koha::RefundLostItemFeeRules->_choose_branch();
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' );
268 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHoldingBranch' );
269 eval {
270 Koha::RefundLostItemFeeRules->_choose_branch();
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' );
277 # Rollback transaction
278 $schema->storage->txn_rollback;
281 subtest 'Koha::RefundLostItemFeeRules::should_refund() tests' => sub {
283 plan tests => 3;
285 # Start transaction
286 $schema->storage->txn_begin;
288 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
290 $schema->resultset('CirculationRule')->search()->delete;
292 my $default_rule = $builder->build(
294 source => 'CirculationRule',
295 value => {
296 branchcode => undef,
297 categorycode => undef,
298 itemtype => undef,
299 rule_name => 'refund',
300 rule_value => 1
304 my $specific_rule_false = $builder->build(
306 source => 'CirculationRule',
307 value => {
308 categorycode => undef,
309 itemtype => undef,
310 rule_name => 'refund',
311 rule_value => 0
315 my $specific_rule_true = $builder->build(
317 source => 'CirculationRule',
318 value => {
319 categorycode => undef,
320 itemtype => undef,
321 rule_name => 'refund',
322 rule_value => 1
326 # Make sure we have an unused branchcode
327 my $specific_rule_dummy = $builder->build(
329 source => 'CirculationRule',
330 value => {
331 categorycode => undef,
332 itemtype => undef,
333 rule_name => 'refund',
337 my $branch_without_rule = $specific_rule_dummy->{ branchcode };
338 Koha::CirculationRules
339 ->search(
341 branchcode => $branch_without_rule,
342 categorycode => undef,
343 itemtype => undef,
344 rule_name => 'refund'
347 ->next
348 ->delete;
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
357 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
358 is( Koha::RefundLostItemFeeRules->should_refund( $params ),
359 1,'Specific rule is applied (true)');
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)');
365 # Change the default value just to try
366 Koha::CirculationRules->search({ branchcode => undef, 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)');
371 # Rollback transaction
372 $schema->storage->txn_rollback;
375 subtest 'Koha::RefundLostItemFeeRules::find() tests' => sub {
377 plan tests => 5;
379 # Start transaction
380 $schema->storage->txn_begin;
382 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
384 $schema->resultset('CirculationRule')->search()->delete;
386 my $default_non_refund = $builder->build(
388 source => 'CirculationRule',
389 value => {
390 branchcode => undef,
391 categorycode => undef,
392 itemtype => undef,
393 rule_name => 'non_refund_rule',
394 rule_value => 1
399 ok(defined Koha::RefundLostItemFeeRules->find($default_non_refund->{id}), 'Find should continue to work when passed an id');
401 my $specific_non_refund = $builder->build(
403 source => 'CirculationRule',
404 value => {
405 categorycode => undef,
406 itemtype => undef,
407 rule_name => 'non_refund_rule',
408 rule_value => 0
413 ok(!defined Koha::RefundLostItemFeeRules->find({ branchcode => undef }), 'Non refund default rules are not found');
414 ok(!defined Koha::RefundLostItemFeeRules->find({ branchcode => $specific_non_refund->{branchcode} }), 'Non refund specific rules are not found');
416 my $default_refund = $builder->build(
418 source => 'CirculationRule',
419 value => {
420 branchcode => undef,
421 categorycode => undef,
422 itemtype => undef,
423 rule_name => 'refund',
424 rule_value => 1
428 my $specific_refund = $builder->build(
430 source => 'CirculationRule',
431 value => {
432 categorycode => undef,
433 itemtype => undef,
434 rule_name => 'refund',
435 rule_value => 0
440 ok(defined Koha::RefundLostItemFeeRules->find({ branchcode => undef }), 'Refund default rules are found');
441 ok(defined Koha::RefundLostItemFeeRules->find({ branchcode => $specific_refund->{branchcode} }), 'Refund specific rules are found');
443 # Rollback transaction
444 $schema->storage->txn_rollback;