Bug 14048: Use mock_preference in tests
[koha.git] / t / db_dependent / RefundLostItemFeeRule.t
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     t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
179
180     is( Koha::RefundLostItemFeeRules->_choose_branch( $params ),
181         'current_branch_code', 'CheckinLibrary is honoured');
182
183     t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHomeBranch' );
184     is( Koha::RefundLostItemFeeRules->_choose_branch( $params ),
185         'item_home_branch_code', 'ItemHomeBranch is honoured');
186
187     t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHoldingBranch' );
188     is( Koha::RefundLostItemFeeRules->_choose_branch( $params ),
189         'item_holding_branch_code', 'ItemHoldingBranch is honoured');
190
191     t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
192     eval {
193         Koha::RefundLostItemFeeRules->_choose_branch();
194     };
195     is( ref($@), 'Koha::Exceptions::MissingParameter',
196         'Missing parameter exception' );
197     is( $@->message, 'CheckinLibrary requires the current_branch param',
198         'Exception message is correct' );
199
200     t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHomeBranch' );
201     eval {
202         Koha::RefundLostItemFeeRules->_choose_branch();
203     };
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' );
208
209     t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'ItemHoldingBranch' );
210     eval {
211         Koha::RefundLostItemFeeRules->_choose_branch();
212     };
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' );
217
218     # Rollback transaction
219     $schema->storage->txn_rollback;
220 };
221
222 subtest 'Koha::RefundLostItemFeeRules::should_refund() tests' => sub {
223
224     plan tests => 3;
225
226     # Start transaction
227     $schema->storage->txn_begin;
228
229     t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
230
231     $schema->resultset('RefundLostItemFeeRule')->search()->delete;
232
233     my $default_rule = $builder->build({
234         source => 'RefundLostItemFeeRule',
235         value  => {
236             branchcode => '*',
237             refund     => 1
238         }
239     });
240     my $specific_rule_false = $builder->build({
241         source => 'RefundLostItemFeeRule',
242         value  => {
243             refund => 0
244         }
245     });
246     my $specific_rule_true = $builder->build({
247         source => 'RefundLostItemFeeRule',
248         value  => {
249             refund => 1
250         }
251     });
252     # Make sure we have an unused branchcode
253     my $specific_rule_dummy = $builder->build({
254         source => 'RefundLostItemFeeRule'
255     });
256     my $branch_without_rule = $specific_rule_dummy->{ branchcode };
257     Koha::RefundLostItemFeeRules
258         ->find({ branchcode => $branch_without_rule })
259         ->delete;
260
261     my $params = {
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
266     };
267
268     t::lib::Mocks::mock_preference( 'RefundLostOnReturnControl', 'CheckinLibrary' );
269     is( Koha::RefundLostItemFeeRules->should_refund( $params ),
270           1,'Specific rule is applied (true)');
271
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)');
275
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)');
281
282     # Rollback transaction
283     $schema->storage->txn_rollback;
284 };
285
286 1;