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>.
20 use Test::More tests => 8;
22 use t::lib::TestBuilder;
28 use_ok('Koha::Object');
29 use_ok('Koha::RefundLostItemFeeRule');
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 {
41 $schema->storage->txn_begin;
44 $schema->resultset('RefundLostItemFeeRule')->search()->delete;
46 my $generated_default_rule = $builder->build({
47 source => 'RefundLostItemFeeRule',
52 my $generated_other_rule = $builder->build({
53 source => 'RefundLostItemFeeRule'
56 my $default_rule = Koha::RefundLostItemFeeRules->find({
58 ok( defined $default_rule, 'Default rule created' );
59 ok( $default_rule->in_storage, 'Default rule actually in storage');
61 my $other_rule = Koha::RefundLostItemFeeRules->find({
62 branchcode => $generated_other_rule->{ branchcode }
64 ok( defined $other_rule, 'Other rule created' );
65 ok( $other_rule->in_storage, 'Other rule actually in storage');
67 # deleting the regular rule
69 ok( !$other_rule->in_storage, 'Other rule deleted from storage' );
71 # deleting the default rule
73 $default_rule->delete;
75 is( ref($@), 'Koha::Exceptions::CannotDeleteDefault',
76 'Exception on deleting default' );
77 ok( $default_rule->in_storage, 'Default rule still in storage' );
79 # Rollback transaction
80 $schema->storage->txn_rollback;
83 subtest 'Koha::RefundLostItemFeeRules::_default_rule() tests' => sub {
88 $schema->storage->txn_begin;
91 $schema->resultset('RefundLostItemFeeRule')->search()->delete;
93 my $generated_default_rule = $builder->build({
94 source => 'RefundLostItemFeeRule',
100 my $generated_other_rule = $builder->build({
101 source => 'RefundLostItemFeeRule'
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' );
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' );
118 # Rollback transaction
119 $schema->storage->txn_rollback;
122 subtest 'Koha::RefundLostItemFeeRules::_effective_branch_rule() tests' => sub {
127 $schema->storage->txn_begin;
130 $schema->resultset('RefundLostItemFeeRule')->search()->delete;
132 my $default_rule = $builder->build({
133 source => 'RefundLostItemFeeRule',
139 my $specific_rule_false = $builder->build({
140 source => 'RefundLostItemFeeRule',
145 my $specific_rule_true = $builder->build({
146 source => 'RefundLostItemFeeRule',
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)');
161 # Rollback transaction
162 $schema->storage->txn_rollback;
165 subtest 'Koha::RefundLostItemFeeRules::_choose_branch() tests' => sub {
170 $schema->storage->txn_begin;
173 current_branch => 'current_branch_code',
174 item_holding_branch => 'item_holding_branch_code',
175 item_home_branch => 'item_home_branch_code'
178 my $syspref = Koha::Config::SysPrefs->find_or_create({
179 variable => 'RefundLostOnReturnControl',
180 value => 'CheckinLibrary' });
182 is( Koha::RefundLostItemFeeRules->_choose_branch( $params ),
183 'current_branch_code', 'CheckinLibrary is honoured');
185 $syspref->value( 'ItemHomeBranch' )->store;
186 is( Koha::RefundLostItemFeeRules->_choose_branch( $params ),
187 'item_home_branch_code', 'ItemHomeBranch is honoured');
189 $syspref->value( 'ItemHoldingBranch' )->store;
190 is( Koha::RefundLostItemFeeRules->_choose_branch( $params ),
191 'item_holding_branch_code', 'ItemHoldingBranch is honoured');
193 $syspref->value( 'CheckinLibrary' )->store;
195 Koha::RefundLostItemFeeRules->_choose_branch();
197 is( ref($@), 'Koha::Exceptions::MissingParameter',
198 'Missing parameter exception' );
199 is( $@->message, 'CheckinLibrary requires the current_branch param',
200 'Exception message is correct' );
202 $syspref->value( 'ItemHomeBranch' )->store;
204 Koha::RefundLostItemFeeRules->_choose_branch();
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' );
211 $syspref->value( 'ItemHoldingBranch' )->store;
213 Koha::RefundLostItemFeeRules->_choose_branch();
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' );
220 # Rollback transaction
221 $schema->storage->txn_rollback;
224 subtest 'Koha::RefundLostItemFeeRules::should_refund() tests' => sub {
229 $schema->storage->txn_begin;
231 my $syspref = Koha::Config::SysPrefs->find_or_create({
232 variable => 'RefundLostOnReturnControl',
233 value => 'CheckinLibrary' });
235 $schema->resultset('RefundLostItemFeeRule')->search()->delete;
237 my $default_rule = $builder->build({
238 source => 'RefundLostItemFeeRule',
244 my $specific_rule_false = $builder->build({
245 source => 'RefundLostItemFeeRule',
250 my $specific_rule_true = $builder->build({
251 source => 'RefundLostItemFeeRule',
256 # Make sure we have an unused branchcode
257 my $specific_rule_dummy = $builder->build({
258 source => 'RefundLostItemFeeRule'
260 my $branch_without_rule = $specific_rule_dummy->{ branchcode };
261 Koha::RefundLostItemFeeRules
262 ->find({ branchcode => $branch_without_rule })
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
272 $syspref->value( 'CheckinLibrary' )->store;
273 is( Koha::RefundLostItemFeeRules->should_refund( $params ),
274 1,'Specific rule is applied (true)');
276 $syspref->value( 'ItemHomeBranch' )->store;
277 is( Koha::RefundLostItemFeeRules->should_refund( $params ),
278 1,'No rule for branch, global rule applied (true)');
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)');
286 # Rollback transaction
287 $schema->storage->txn_rollback;