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 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
180 is( Koha::RefundLostItemFeeRules->_choose_branch( $params ),
181 'current_branch_code', 'CheckinLibrary is honoured');
183 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHomeBranch' );
184 is( Koha::RefundLostItemFeeRules->_choose_branch( $params ),
185 'item_home_branch_code', 'ItemHomeBranch is honoured');
187 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHoldingBranch' );
188 is( Koha::RefundLostItemFeeRules->_choose_branch( $params ),
189 'item_holding_branch_code', 'ItemHoldingBranch is honoured');
191 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
193 Koha::RefundLostItemFeeRules->_choose_branch();
195 is( ref($@), 'Koha::Exceptions::MissingParameter',
196 'Missing parameter exception' );
197 is( $@->message, 'CheckinLibrary requires the current_branch param',
198 'Exception message is correct' );
200 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHomeBranch' );
202 Koha::RefundLostItemFeeRules->_choose_branch();
204 is( ref($@), 'Koha::Exceptions::MissingParameter',
205 'Missing parameter exception' );
206 is( $@->message, 'ItemHomeBranch requires the item_home_branch param',
207 'Exception message is correct' );
209 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHoldingBranch' );
211 Koha::RefundLostItemFeeRules->_choose_branch();
213 is( ref($@), 'Koha::Exceptions::MissingParameter',
214 'Missing parameter exception' );
215 is( $@->message, 'ItemHoldingBranch requires the item_holding_branch param',
216 'Exception message is correct' );
218 # Rollback transaction
219 $schema->storage->txn_rollback;
222 subtest 'Koha::RefundLostItemFeeRules::should_refund() tests' => sub {
227 $schema->storage->txn_begin;
229 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
231 $schema->resultset('RefundLostItemFeeRule')->search()->delete;
233 my $default_rule = $builder->build({
234 source => 'RefundLostItemFeeRule',
240 my $specific_rule_false = $builder->build({
241 source => 'RefundLostItemFeeRule',
246 my $specific_rule_true = $builder->build({
247 source => 'RefundLostItemFeeRule',
252 # Make sure we have an unused branchcode
253 my $specific_rule_dummy = $builder->build({
254 source => 'RefundLostItemFeeRule'
256 my $branch_without_rule = $specific_rule_dummy->{ branchcode };
257 Koha::RefundLostItemFeeRules
258 ->find({ branchcode => $branch_without_rule })
262 current_branch => $specific_rule_true->{ branchcode },
263 # patron_branch => $specific_rule_false->{ branchcode },
264 item_holding_branch => $branch_without_rule,
265 item_home_branch => $branch_without_rule
268 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
269 is( Koha::RefundLostItemFeeRules->should_refund( $params ),
270 1,'Specific rule is applied (true)');
272 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHomeBranch' );
273 is( Koha::RefundLostItemFeeRules->should_refund( $params ),
274 1,'No rule for branch, global rule applied (true)');
276 # Change the default value just to try
277 Koha::RefundLostItemFeeRules->find({ branchcode => '*' })->refund(0)->store;
278 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHoldingBranch' );
279 is( Koha::RefundLostItemFeeRules->should_refund( $params ),
280 0,'No rule for branch, global rule applied (false)');
282 # Rollback transaction
283 $schema->storage->txn_rollback;