View | Details | Raw Unified | Return to bug 14048
Collapse All | Expand All

(-)a/t/db_dependent/RefundLostItemFeeRule.t (-1 / +306 lines)
Line 0 Link Here
0
- 
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 => 12;
168
169
    # Start transaction
170
    $schema->storage->txn_begin;
171
172
    my $params = {
173
        current_branch => 'current_branch_code',
174
        patron_branch  => 'patron_branch_code',
175
        item_holding_branch => 'item_holding_branch_code',
176
        item_home_branch => 'item_home_branch_code'
177
    };
178
179
    my $syspref = Koha::Config::SysPrefs->find_or_create({
180
                        variable => 'RefundLostOnReturnControl',
181
                        value => 'CheckinLibrary' });
182
183
    is( Koha::RefundLostItemFeeRules->_choose_branch( $params ),
184
        'current_branch_code', 'CheckinLibrary is honoured');
185
186
    $syspref->value( 'PatronLibrary' )->store;
187
    is( Koha::RefundLostItemFeeRules->_choose_branch( $params ),
188
        'patron_branch_code', 'PatronLibrary is honoured');
189
190
    $syspref->value( 'ItemHomeBranch' )->store;
191
    is( Koha::RefundLostItemFeeRules->_choose_branch( $params ),
192
        'item_home_branch_code', 'ItemHomeBranch is honoured');
193
194
    $syspref->value( 'ItemHoldingBranch' )->store;
195
    is( Koha::RefundLostItemFeeRules->_choose_branch( $params ),
196
        'item_holding_branch_code', 'ItemHoldingBranch is honoured');
197
198
    $syspref->value( 'CheckinLibrary' )->store;
199
    eval {
200
        Koha::RefundLostItemFeeRules->_choose_branch();
201
    };
202
    is( ref($@), 'Koha::Exceptions::MissingParameter',
203
        'Missing parameter exception' );
204
    is( $@->message, 'CheckinLibrary requires the current_branch param',
205
        'Exception message is correct' );
206
207
    $syspref->value( 'PatronLibrary' )->store;
208
    eval {
209
        Koha::RefundLostItemFeeRules->_choose_branch();
210
    };
211
    is( ref($@), 'Koha::Exceptions::MissingParameter',
212
        'Missing parameter exception' );
213
    is( $@->message, 'PatronLibrary requires the patron_branch param',
214
        'Exception message is correct' );
215
216
    $syspref->value( 'ItemHomeBranch' )->store;
217
    eval {
218
        Koha::RefundLostItemFeeRules->_choose_branch();
219
    };
220
    is( ref($@), 'Koha::Exceptions::MissingParameter',
221
        'Missing parameter exception' );
222
    is( $@->message, 'ItemHomeBranch requires the item_home_branch param',
223
        'Exception message is correct' );
224
225
    $syspref->value( 'ItemHoldingBranch' )->store;
226
    eval {
227
        Koha::RefundLostItemFeeRules->_choose_branch();
228
    };
229
    is( ref($@), 'Koha::Exceptions::MissingParameter',
230
        'Missing parameter exception' );
231
    is( $@->message, 'ItemHoldingBranch requires the item_holding_branch param',
232
        'Exception message is correct' );
233
234
    # Rollback transaction
235
    $schema->storage->txn_rollback;
236
};
237
238
subtest 'Koha::RefundLostItemFeeRules::should_refund() tests' => sub {
239
240
    plan tests => 4;
241
242
    # Start transaction
243
    $schema->storage->txn_begin;
244
245
    my $syspref = Koha::Config::SysPrefs->find_or_create({
246
                        variable => 'RefundLostOnReturnControl',
247
                        value => 'CheckinLibrary' });
248
249
    $schema->resultset('RefundLostItemFeeRule')->search()->delete;
250
251
    my $default_rule = $builder->build({
252
        source => 'RefundLostItemFeeRule',
253
        value  => {
254
            branchcode => '*',
255
            refund     => 1
256
        }
257
    });
258
    my $specific_rule_false = $builder->build({
259
        source => 'RefundLostItemFeeRule',
260
        value  => {
261
            refund => 0
262
        }
263
    });
264
    my $specific_rule_true = $builder->build({
265
        source => 'RefundLostItemFeeRule',
266
        value  => {
267
            refund => 1
268
        }
269
    });
270
    # Make sure we have an unused branchcode
271
    my $specific_rule_dummy = $builder->build({
272
        source => 'RefundLostItemFeeRule'
273
    });
274
    my $branch_without_rule = $specific_rule_dummy->{ branchcode };
275
    Koha::RefundLostItemFeeRules->find({ branchcode => $branch_without_rule })->delete;
276
277
    my $params = {
278
        current_branch => $specific_rule_true->{ branchcode },
279
        patron_branch  => $specific_rule_false->{ branchcode },
280
        item_holding_branch => $branch_without_rule,
281
        item_home_branch => $branch_without_rule
282
    };
283
284
    $syspref->value( 'CheckinLibrary' )->store;
285
    is( Koha::RefundLostItemFeeRules->should_refund( $params ),
286
          1,'Specific rule is applied (true)');
287
288
    $syspref->value( 'PatronLibrary' )->store;
289
    is( Koha::RefundLostItemFeeRules->should_refund( $params ),
290
          0,'Specific rule is applied (false)');
291
292
    $syspref->value( 'ItemHomeBranch' )->store;
293
    is( Koha::RefundLostItemFeeRules->should_refund( $params ),
294
         1,'No rule for branch, global rule applied (true)');
295
296
    # Change the default value just to try
297
    Koha::RefundLostItemFeeRules->find({ branchcode => '*' })->refund(0)->store;
298
    $syspref->value( 'ItemHoldingBranch' )->store;
299
    is( Koha::RefundLostItemFeeRules->should_refund( $params ),
300
         0,'No rule for branch, global rule applied (false)');
301
302
    # Rollback transaction
303
    $schema->storage->txn_rollback;
304
};
305
306
1;

Return to bug 14048