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

(-)a/t/db_dependent/Circulation/IssuingRules/maxsuspensiondays.t (-133 lines)
Lines 1-133 Link Here
1
use Modern::Perl;
2
use Test::More tests => 3;
3
4
use MARC::Record;
5
use MARC::Field;
6
use C4::Context;
7
8
use C4::Circulation qw( AddIssue AddReturn );
9
use C4::Items qw( AddItem );
10
use C4::Biblio qw( AddBiblio );
11
use Koha::Database;
12
use Koha::DateUtils;
13
use Koha::Patron::Debarments qw( GetDebarments DelDebarment );
14
use Koha::Patrons;
15
16
use t::lib::TestBuilder;
17
use t::lib::Mocks;
18
19
my $schema = Koha::Database->schema;
20
$schema->storage->txn_begin;
21
my $builder = t::lib::TestBuilder->new;
22
my $dbh = C4::Context->dbh;
23
$dbh->{RaiseError} = 1;
24
25
my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
26
my $itemtype   = $builder->build({ source => 'Itemtype' })->{itemtype};
27
my $patron_category = $builder->build({ source => 'Category' });
28
29
t::lib::Mocks::mock_userenv({ branchcode => $branchcode });
30
31
# Test without maxsuspensiondays set
32
Koha::IssuingRules->search->delete;
33
$builder->build(
34
    {
35
        source => 'Issuingrule',
36
        value  => {
37
            categorycode => '*',
38
            itemtype     => '*',
39
            branchcode   => '*',
40
            firstremind  => 0,
41
            finedays     => 2,
42
            lengthunit   => 'days',
43
            suspension_chargeperiod => 1,
44
        }
45
    }
46
);
47
48
my $borrowernumber = Koha::Patron->new({
49
    firstname =>  'my firstname',
50
    surname => 'my surname',
51
    categorycode => $patron_category->{categorycode},
52
    branchcode => $branchcode,
53
})->store->borrowernumber;
54
my $borrower = Koha::Patrons->find( $borrowernumber )->unblessed;
55
56
my $record = MARC::Record->new();
57
$record->append_fields(
58
    MARC::Field->new('100', ' ', ' ', a => 'My author'),
59
    MARC::Field->new('245', ' ', ' ', a => 'My title'),
60
);
61
62
my $barcode = 'bc_maxsuspensiondays';
63
my ($biblionumber, $biblioitemnumber) = AddBiblio($record, '');
64
my (undef, undef, $itemnumber) = AddItem({
65
        homebranch => $branchcode,
66
        holdingbranch => $branchcode,
67
        barcode => $barcode,
68
        itype => $itemtype
69
    } , $biblionumber);
70
71
# clear any holidays to avoid throwing off the suspension day
72
# calculations
73
$dbh->do('DELETE FROM special_holidays');
74
$dbh->do('DELETE FROM repeatable_holidays');
75
76
my $daysago20 = dt_from_string->add_duration(DateTime::Duration->new(days => -20));
77
my $daysafter40 = dt_from_string->add_duration(DateTime::Duration->new(days => 40));
78
79
AddIssue( $borrower, $barcode, $daysago20 );
80
AddReturn( $barcode, $branchcode );
81
my $debarments = GetDebarments({borrowernumber => $borrower->{borrowernumber}});
82
is(
83
    $debarments->[0]->{expiration},
84
    output_pref({ dt => $daysafter40, dateformat => 'iso', dateonly => 1 }),
85
    'calculate suspension with no maximum set'
86
);
87
DelDebarment( $debarments->[0]->{borrower_debarment_id} );
88
89
# Test with maxsuspensiondays = 10 days
90
my $issuing_rule = Koha::IssuingRules->search->next;
91
$issuing_rule->maxsuspensiondays( 10 )->store;
92
93
my $daysafter10 = dt_from_string->add_duration(DateTime::Duration->new(days => 10));
94
AddIssue( $borrower, $barcode, $daysago20 );
95
AddReturn( $barcode, $branchcode );
96
$debarments = GetDebarments({borrowernumber => $borrower->{borrowernumber}});
97
is(
98
    $debarments->[0]->{expiration},
99
    output_pref({ dt => $daysafter10, dateformat => 'iso', dateonly => 1 }),
100
    'calculate suspension with a maximum set'
101
);
102
DelDebarment( $debarments->[0]->{borrower_debarment_id} );
103
104
# Test with lengthunit => 'hours'
105
Koha::IssuingRules->search->delete;
106
$builder->build(
107
    {
108
        source => 'Issuingrule',
109
        value  => {
110
            categorycode => '*',
111
            itemtype     => '*',
112
            branchcode   => '*',
113
            firstremind  => 0,
114
            finedays     => 2,
115
            lengthunit   => 'hours',
116
            suspension_chargeperiod => 1,
117
            maxsuspensiondays => 100,
118
        }
119
    }
120
);
121
122
$daysafter10 = dt_from_string->add_duration(DateTime::Duration->new(days => 10));
123
AddIssue( $borrower, $barcode, $daysago20 );
124
AddReturn( $barcode, $branchcode );
125
$debarments = GetDebarments({borrowernumber => $borrower->{borrowernumber}});
126
is(
127
    $debarments->[0]->{expiration},
128
    output_pref({ dt => $daysafter40, dateformat => 'iso', dateonly => 1 }),
129
    'calculate suspension with lengthunit hours.'
130
);
131
DelDebarment( $debarments->[0]->{borrower_debarment_id} );
132
133
$schema->storage->txn_rollback;
(-)a/t/db_dependent/Circulation/maxsuspensiondays.t (-5 / +114 lines)
Lines 15-20 use Koha::Patrons; Link Here
15
use t::lib::TestBuilder;
15
use t::lib::TestBuilder;
16
use t::lib::Mocks;
16
use t::lib::Mocks;
17
17
18
use POSIX qw( ceil );
19
20
18
my $schema = Koha::Database->schema;
21
my $schema = Koha::Database->schema;
19
$schema->storage->txn_begin;
22
$schema->storage->txn_begin;
20
my $builder = t::lib::TestBuilder->new;
23
my $builder = t::lib::TestBuilder->new;
Lines 130-137 subtest "suspension_chargeperiod" => sub { Link Here
130
    my $today = dt_from_string;
133
    my $today = dt_from_string;
131
    my $new_debar_dt = C4::Circulation::_calculate_new_debar_dt( $patron, $item, $last_year, $today );
134
    my $new_debar_dt = C4::Circulation::_calculate_new_debar_dt( $patron, $item, $last_year, $today );
132
    is( $new_debar_dt->truncate( to => 'day' ),
135
    is( $new_debar_dt->truncate( to => 'day' ),
133
        $today->clone->add( days => 365 / 15 * 7 )->truncate( to => 'day' ) );
136
        $today->clone->add( days => ceil(365 / 15 * 7) )->truncate( to => 'day' )),
134
137
        'the truncated dates are equal.'
135
};
138
};
136
139
137
subtest "maxsuspensiondays" => sub {
140
subtest "maxsuspensiondays" => sub {
Lines 156-162 subtest "maxsuspensiondays" => sub { Link Here
156
    my $today = dt_from_string;
159
    my $today = dt_from_string;
157
    my $new_debar_dt = C4::Circulation::_calculate_new_debar_dt( $patron, $item, $last_year, $today );
160
    my $new_debar_dt = C4::Circulation::_calculate_new_debar_dt( $patron, $item, $last_year, $today );
158
    is( $new_debar_dt->truncate( to => 'day' ),
161
    is( $new_debar_dt->truncate( to => 'day' ),
159
        $today->clone->add( days => 333 )->truncate( to => 'day' ) );
162
        $today->clone->add( days => 333 )->truncate( to => 'day' ));
160
};
163
164
165
    Koha::CirculationRules->set_rules(
166
        {
167
            categorycode => undef,
168
            itemtype     => undef,
169
            branchcode   => undef,
170
            rules        => {
171
                firstremind  => 0,
172
                finedays     => 2,
173
                lengthunit   => 'hours',
174
                suspension_chargeperiod => 1,
175
                maxsuspensiondays => 100,
176
            }
177
        }
178
    );
179
180
    my $yesterday = dt_from_string->clone->subtract( days => 1 );
181
    my $daysafter2 = dt_from_string->clone->add( days => 2 );
182
    AddIssue( $patron, $barcode, $yesterday);
183
    AddReturn( $barcode, $branchcode );
184
    $debarments = $patron->restrictions;
185
    $THE_debarment = $debarments->next;
186
    is(
187
        $THE_debarment->expiration,
188
        output_pref({ dt => $daysafter2, dateformat => 'iso', dateonly => 1 }),
189
        'calculate suspension with lengthunit hours.'
190
    );
191
192
    DelDebarment( $THE_debarment->borrower_debarment_id );
193
194
    my $hoursago2 = dt_from_string->clone->subtract( hours => 2 );
195
    AddIssue( $patron, $barcode, $hoursago2);
196
    AddReturn( $barcode, $branchcode );
197
    $debarments = $patron->restrictions;
198
    $THE_debarment = $debarments->next;
199
    is(
200
        $THE_debarment->expiration,
201
        output_pref({ dt => $daysafter2, dateformat => 'iso', dateonly => 1 }),
202
        'calculate suspension with lengthunit hours.'
203
    );
204
205
    DelDebarment( $THE_debarment->borrower_debarment_id );
161
206
207
    my $hoursafter2 = dt_from_string->clone->add( hours => 2 );
208
    AddIssue( $patron, $barcode, $hoursafter2);
209
    AddReturn( $barcode, $branchcode );
210
    $debarments = $patron->restrictions;
211
    $THE_debarment = $debarments->next;
212
    is_deeply(
213
        $THE_debarment,
214
        undef,
215
        'No debarment if in time'
216
    );
217
218
    # Add 1 day every 2 days
219
    Koha::CirculationRules->set_rules(
220
        {
221
            categorycode => undef,
222
            itemtype     => undef,
223
            branchcode   => undef,
224
            rules        => {
225
                firstremind  => 0,
226
                finedays     => 1,
227
                lengthunit   => 'hours',
228
                suspension_chargeperiod => 2,
229
                maxsuspensiondays => 100,
230
            }
231
        }
232
    );
233
234
    # 20 days late => 20/2 * 1 => 10 days of suspension
235
    AddIssue( $patron, $barcode, $daysago20 );
236
    AddReturn( $barcode, $branchcode );
237
    $debarments = $patron->restrictions;
238
    $THE_debarment = $debarments->next;
239
    is(
240
        $THE_debarment->expiration,
241
        output_pref({ dt => $daysafter10, dateformat => 'iso', dateonly => 1 }),
242
        'calculate suspension with lengthunit hours.'
243
    );
244
245
    DelDebarment( $THE_debarment->borrower_debarment_id );
246
247
    # 1 day late => ceil(1/2) * 1 => 1 day of suspension
248
    my $tomorrow = dt_from_string->clone->add( days => 1 );
249
    AddIssue( $patron, $barcode, $yesterday);
250
    AddReturn( $barcode, $branchcode );
251
    $debarments = $patron->restrictions;
252
    $THE_debarment = $debarments->next;
253
    is(
254
        $THE_debarment->expiration,
255
        output_pref({ dt => $tomorrow, dateformat => 'iso', dateonly => 1 }),
256
        'calculate suspension with lengthunit hours.'
257
    );
258
259
    DelDebarment( $THE_debarment->borrower_debarment_id );
260
261
    # 2 hours late => ceil(.x/2) * 1 => 1 day of suspension
262
    AddIssue( $patron, $barcode, $hoursafter2);
263
    AddReturn( $barcode, $branchcode );
264
    $debarments = $patron->restrictions;
265
    $THE_debarment = $debarments->next;
266
    is_deeply(
267
        $THE_debarment,
268
        undef,
269
        'No debarment if in time'
270
    );
271
};
162
$schema->storage->txn_rollback;
272
$schema->storage->txn_rollback;
163
- 

Return to bug 14293