Bug 18811: DBRev 17.05.00.006
[koha.git] / t / db_dependent / RefundLostItemFeeRule.t
blob84a373a008bf5215e2ec17b14c040cfcc2029278
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 => 8;
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::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 {
38 plan tests => 5;
40 # Start transaction
41 $schema->storage->txn_begin;
43 # Clean the table
44 $schema->resultset('RefundLostItemFeeRule')->search()->delete;
46 my $generated_default_rule = $builder->build({
47 source => 'RefundLostItemFeeRule',
48 value => {
49 branchcode => '*'
51 });
52 my $generated_other_rule = $builder->build({
53 source => 'RefundLostItemFeeRule'
54 });
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');
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');
67 # deleting the regular rule
68 $other_rule->delete;
69 ok( !$other_rule->in_storage, 'Other rule deleted from storage' );
71 # Rollback transaction
72 $schema->storage->txn_rollback;
75 subtest 'Koha::RefundLostItemFeeRules::_default_rule() tests' => sub {
77 plan tests => 6;
79 # Start transaction
80 $schema->storage->txn_begin;
82 # Clean the table
83 $schema->resultset('RefundLostItemFeeRule')->search()->delete;
85 my $generated_default_rule = $builder->build({
86 source => 'RefundLostItemFeeRule',
87 value => {
88 branchcode => '*',
89 refund => 1
91 });
92 my $generated_other_rule = $builder->build({
93 source => 'RefundLostItemFeeRule'
94 });
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' );
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' );
110 $default_rule->delete;
111 ok( !$default_rule->in_storage, 'Default rule effectively deleted from storage' );
113 ok( Koha::RefundLostItemFeeRules->_default_rule, 'Default rule is set to refund if no default rule is present' );
115 # Rollback transaction
116 $schema->storage->txn_rollback;
119 subtest 'Koha::RefundLostItemFeeRules::_effective_branch_rule() tests' => sub {
121 plan tests => 3;
123 # Start transaction
124 $schema->storage->txn_begin;
126 # Clean the table
127 $schema->resultset('RefundLostItemFeeRule')->search()->delete;
129 my $default_rule = $builder->build({
130 source => 'RefundLostItemFeeRule',
131 value => {
132 branchcode => '*',
133 refund => 1
136 my $specific_rule_false = $builder->build({
137 source => 'RefundLostItemFeeRule',
138 value => {
139 refund => 0
142 my $specific_rule_true = $builder->build({
143 source => 'RefundLostItemFeeRule',
144 value => {
145 refund => 1
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)');
158 # Rollback transaction
159 $schema->storage->txn_rollback;
162 subtest 'Koha::RefundLostItemFeeRules::_choose_branch() tests' => sub {
164 plan tests => 9;
166 # Start transaction
167 $schema->storage->txn_begin;
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'
175 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
177 is( Koha::RefundLostItemFeeRules->_choose_branch( $params ),
178 'current_branch_code', 'CheckinLibrary is honoured');
180 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHomeBranch' );
181 is( Koha::RefundLostItemFeeRules->_choose_branch( $params ),
182 'item_home_branch_code', 'ItemHomeBranch is honoured');
184 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHoldingBranch' );
185 is( Koha::RefundLostItemFeeRules->_choose_branch( $params ),
186 'item_holding_branch_code', 'ItemHoldingBranch is honoured');
188 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
189 eval {
190 Koha::RefundLostItemFeeRules->_choose_branch();
192 is( ref($@), 'Koha::Exceptions::MissingParameter',
193 'Missing parameter exception' );
194 is( $@->message, 'CheckinLibrary requires the current_branch param',
195 'Exception message is correct' );
197 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHomeBranch' );
198 eval {
199 Koha::RefundLostItemFeeRules->_choose_branch();
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' );
206 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHoldingBranch' );
207 eval {
208 Koha::RefundLostItemFeeRules->_choose_branch();
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' );
215 # Rollback transaction
216 $schema->storage->txn_rollback;
219 subtest 'Koha::RefundLostItemFeeRules::should_refund() tests' => sub {
221 plan tests => 3;
223 # Start transaction
224 $schema->storage->txn_begin;
226 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
228 $schema->resultset('RefundLostItemFeeRule')->search()->delete;
230 my $default_rule = $builder->build({
231 source => 'RefundLostItemFeeRule',
232 value => {
233 branchcode => '*',
234 refund => 1
237 my $specific_rule_false = $builder->build({
238 source => 'RefundLostItemFeeRule',
239 value => {
240 refund => 0
243 my $specific_rule_true = $builder->build({
244 source => 'RefundLostItemFeeRule',
245 value => {
246 refund => 1
249 # Make sure we have an unused branchcode
250 my $specific_rule_dummy = $builder->build({
251 source => 'RefundLostItemFeeRule'
253 my $branch_without_rule = $specific_rule_dummy->{ branchcode };
254 Koha::RefundLostItemFeeRules
255 ->find({ branchcode => $branch_without_rule })
256 ->delete;
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
265 t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
266 is( Koha::RefundLostItemFeeRules->should_refund( $params ),
267 1,'Specific rule is applied (true)');
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)');
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)');
279 # Rollback transaction
280 $schema->storage->txn_rollback;