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

(-)a/t/Calendar.t (-324 lines)
Lines 1-324 Link Here
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;
21
use Test::MockModule;
22
23
use DateTime;
24
use DateTime::Duration;
25
use Koha::Caches;
26
use Koha::DateUtils qw( dt_from_string );
27
28
use t::lib::Mocks;
29
30
use Module::Load::Conditional qw/check_install/;
31
32
BEGIN {
33
    if ( check_install( module => 'Test::DBIx::Class' ) ) {
34
        plan tests => 40;
35
    } else {
36
        plan skip_all => "Need Test::DBIx::Class"
37
    }
38
}
39
40
use_ok('Koha::Calendar');
41
42
use Test::DBIx::Class;
43
44
my $db = Test::MockModule->new('Koha::Database');
45
$db->mock(
46
    _new_schema => sub { return Schema(); }
47
);
48
49
# We need to mock the C4::Context->preference method for
50
# simplicity and re-usability of the session definition. Any
51
# syspref fits for syspref-agnostic tests.
52
my $module_context = Test::MockModule->new('C4::Context');
53
$module_context->mock(
54
    'preference',
55
    sub {
56
        return 'Calendar';
57
    }
58
);
59
60
fixtures_ok [
61
    # weekly holidays
62
    RepeatableHoliday => [
63
        [ qw( branchcode day month weekday title description) ],
64
        [ 'MPL', undef, undef, 0, '', '' ], # sundays
65
        [ 'MPL', undef, undef, 6, '', '' ],# saturdays
66
        [ 'MPL', 1, 1, undef, '', ''], # new year's day
67
        [ 'MPL', 25, 12, undef, '', ''], # chrismas
68
    ],
69
    # exception holidays
70
    SpecialHoliday => [
71
        [qw( branchcode day month year title description isexception )],
72
        [ 'MPL', 11, 11, 2012, '', '', 1 ],    # sunday exception
73
        [ 'MPL', 1,  6,  2011, '', '', 0 ],
74
        [ 'MPL', 4,  7,  2012, '', '', 0 ],
75
        [ 'CPL', 6,  8,  2012, '', '', 0 ],
76
        [ 'MPL', 7,  7,  2012, '', '', 1 ], # holiday exception
77
        [ 'MPL', 7,  7,  2012, '', '', 0 ], # holiday
78
      ],
79
], "add fixtures";
80
81
my $cache = Koha::Caches->get_instance();
82
$cache->clear_from_cache('MPL_holidays');
83
$cache->clear_from_cache('CPL_holidays');
84
85
# 'MPL' branch is arbitrary, is not used at all but is needed for initialization
86
my $cal = Koha::Calendar->new( branchcode => 'MPL' );
87
88
isa_ok( $cal, 'Koha::Calendar', 'Calendar class returned' );
89
90
my $saturday = DateTime->new(
91
    year      => 2012,
92
    month     => 11,
93
    day       => 24,
94
);
95
96
my $sunday = DateTime->new(
97
    year      => 2012,
98
    month     => 11,
99
    day       => 25,
100
);
101
102
my $monday = DateTime->new(
103
    year      => 2012,
104
    month     => 11,
105
    day       => 26,
106
);
107
108
my $new_year = DateTime->new(
109
    year        => 2013,
110
    month       => 1,
111
    day         => 1,
112
);
113
114
my $single_holiday = DateTime->new(
115
    year      => 2011,
116
    month     => 6,
117
    day       => 1,
118
);    # should be a holiday
119
120
my $notspecial = DateTime->new(
121
    year      => 2011,
122
    month     => 6,
123
    day       => 2
124
);    # should NOT be a holiday
125
126
my $sunday_exception = DateTime->new(
127
    year      => 2012,
128
    month     => 11,
129
    day       => 11
130
);
131
132
my $day_after_christmas = DateTime->new(
133
    year    => 2012,
134
    month   => 12,
135
    day     => 26
136
);  # for testing negative addDuration
137
138
my $holiday_for_another_branch = DateTime->new(
139
    year => 2012,
140
    month => 8,
141
    day => 6, # This is a monday
142
);
143
144
my $holiday_excepted = DateTime->new(
145
    year => 2012,
146
    month => 7,
147
    day => 7, # Both a holiday and exception
148
);
149
150
{   # Syspref-agnostic tests
151
    is ( $saturday->day_of_week, 6, '\'$saturday\' is actually a saturday (6th day of week)');
152
    is ( $sunday->day_of_week, 7, '\'$sunday\' is actually a sunday (7th day of week)');
153
    is ( $monday->day_of_week, 1, '\'$monday\' is actually a monday (1st day of week)');
154
    is ( $cal->is_holiday($saturday), 1, 'Saturday is a closed day' );
155
    is ( $cal->is_holiday($sunday), 1, 'Sunday is a closed day' );
156
    is ( $cal->is_holiday($monday), 0, 'Monday is not a closed day' );
157
    is ( $cal->is_holiday($new_year), 1, 'Month/Day closed day test (New year\'s day)' );
158
    is ( $cal->is_holiday($single_holiday), 1, 'Single holiday closed day test' );
159
    is ( $cal->is_holiday($notspecial), 0, 'Fixed single date that is not a holiday test' );
160
    is ( $cal->is_holiday($sunday_exception), 0, 'Exception holiday is not a closed day test' );
161
    is ( $cal->is_holiday($holiday_for_another_branch), 0, 'Holiday defined for another branch should not be defined as an holiday' );
162
    is ( $cal->is_holiday($holiday_excepted), 0, 'Holiday defined and excepted should not be a holiday' );
163
}
164
165
{   # Bugzilla #8966 - is_holiday truncates referenced date
166
    my $later_dt = DateTime->new(    # Monday
167
        year      => 2012,
168
        month     => 9,
169
        day       => 17,
170
        hour      => 17,
171
        minute    => 30,
172
        time_zone => 'Europe/London',
173
    );
174
175
176
    is( $cal->is_holiday($later_dt), 0, 'bz-8966 (1/2) Apply is_holiday for the next test' );
177
    cmp_ok( $later_dt, 'eq', '2012-09-17T17:30:00', 'bz-8966 (2/2) Date should be the same after is_holiday' );
178
}
179
180
{   # Bugzilla #8800 - is_holiday should use truncated date for 'contains' call
181
    my $single_holiday_time = DateTime->new(
182
        year  => 2011,
183
        month => 6,
184
        day   => 1,
185
        hour  => 11,
186
        minute  => 2
187
    );
188
189
    is( $cal->is_holiday($single_holiday_time),
190
        $cal->is_holiday($single_holiday) ,
191
        'bz-8800 is_holiday should truncate the date for holiday validation' );
192
}
193
194
    my $one_day_dur = DateTime::Duration->new( days => 1 );
195
    my $two_day_dur = DateTime::Duration->new( days => 2 );
196
    my $seven_day_dur = DateTime::Duration->new( days => 7 );
197
198
    my $dt = dt_from_string( '2012-07-03','iso' ); #tuesday
199
200
    my $test_dt = DateTime->new(    # Monday
201
        year      => 2012,
202
        month     => 7,
203
        day       => 23,
204
        hour      => 11,
205
        minute    => 53,
206
    );
207
208
    my $later_dt = DateTime->new(    # Monday
209
        year      => 2012,
210
        month     => 9,
211
        day       => 17,
212
        hour      => 17,
213
        minute    => 30,
214
        time_zone => 'Europe/London',
215
    );
216
217
{    ## 'Datedue' tests
218
219
    $cal = Koha::Calendar->new( branchcode => 'MPL', days_mode => 'Datedue' );
220
221
    is($cal->addDuration( $dt, $one_day_dur, 'days' ), # tuesday
222
        dt_from_string('2012-07-05','iso'),
223
        'Single day add (Datedue, matches holiday, shift)' );
224
225
    is($cal->addDuration( $dt, $two_day_dur, 'days' ),
226
        dt_from_string('2012-07-05','iso'),
227
        'Two days add, skips holiday (Datedue)' );
228
229
    cmp_ok($cal->addDuration( $test_dt, $seven_day_dur, 'days' ), 'eq',
230
        '2012-07-30T11:53:00',
231
        'Add 7 days (Datedue)' );
232
233
    is( $cal->addDuration( $saturday, $one_day_dur, 'days' )->day_of_week, 1,
234
        'addDuration skips closed Sunday (Datedue)' );
235
236
    is( $cal->addDuration($day_after_christmas, -1, 'days')->ymd(), '2012-12-24',
237
        'Negative call to addDuration (Datedue)' );
238
239
    ## Note that the days_between API says closed days are not considered.
240
    ## This tests are here as an API test.
241
    cmp_ok( $cal->days_between( $test_dt, $later_dt )->in_units('days'),
242
                '==', 40, 'days_between calculates correctly (Days)' );
243
244
    cmp_ok( $cal->days_between( $later_dt, $test_dt )->in_units('days'),
245
                '==', 40, 'Test parameter order not relevant (Days)' );
246
}
247
248
{   ## 'Calendar' tests'
249
250
    $cal = Koha::Calendar->new( branchcode => 'MPL', days_mode => 'Calendar' );
251
252
    $dt = dt_from_string('2012-07-03','iso');
253
254
    is($cal->addDuration( $dt, $one_day_dur, 'days' ),
255
        dt_from_string('2012-07-05','iso'),
256
        'Single day add (Calendar)' );
257
258
    cmp_ok($cal->addDuration( $test_dt, $seven_day_dur, 'days' ), 'eq',
259
       '2012-08-01T11:53:00',
260
       'Add 7 days (Calendar)' );
261
262
    is( $cal->addDuration( $saturday, $one_day_dur, 'days' )->day_of_week, 1,
263
            'addDuration skips closed Sunday (Calendar)' );
264
265
    is( $cal->addDuration($day_after_christmas, -1, 'days')->ymd(), '2012-12-24',
266
            'Negative call to addDuration (Calendar)' );
267
268
    cmp_ok( $cal->days_between( $test_dt, $later_dt )->in_units('days'),
269
                '==', 40, 'days_between calculates correctly (Calendar)' );
270
271
    cmp_ok( $cal->days_between( $later_dt, $test_dt )->in_units('days'),
272
                '==', 40, 'Test parameter order not relevant (Calendar)' );
273
}
274
275
276
{   ## 'Days' tests
277
278
    $cal = Koha::Calendar->new( branchcode => 'MPL', days_mode => 'Days' );
279
280
    $dt = dt_from_string('2012-07-03','iso');
281
282
    is($cal->addDuration( $dt, $one_day_dur, 'days' ),
283
        dt_from_string('2012-07-04','iso'),
284
        'Single day add (Days)' );
285
286
    cmp_ok($cal->addDuration( $test_dt, $seven_day_dur, 'days' ),'eq',
287
        '2012-07-30T11:53:00',
288
        'Add 7 days (Days)' );
289
290
    is( $cal->addDuration( $saturday, $one_day_dur, 'days' )->day_of_week, 7,
291
        'addDuration doesn\'t skip closed Sunday (Days)' );
292
293
    is( $cal->addDuration($day_after_christmas, -1, 'days')->ymd(), '2012-12-25',
294
        'Negative call to addDuration (Days)' );
295
296
    ## Note that the days_between API says closed days are not considered.
297
    ## This tests are here as an API test.
298
    cmp_ok( $cal->days_between( $test_dt, $later_dt )->in_units('days'),
299
                '==', 40, 'days_between calculates correctly (Days)' );
300
301
    cmp_ok( $cal->days_between( $later_dt, $test_dt )->in_units('days'),
302
                '==', 40, 'Test parameter order not relevant (Days)' );
303
304
}
305
306
{
307
    $cal = Koha::Calendar->new( branchcode => 'CPL' );
308
    is ( $cal->is_holiday($single_holiday), 0, 'Single holiday for MPL, not CPL' );
309
    is ( $cal->is_holiday($holiday_for_another_branch), 1, 'Holiday defined for CPL should be defined as an holiday' );
310
}
311
312
subtest 'days_mode parameter' => sub {
313
    plan tests => 1;
314
315
    t::lib::Mocks::mock_preference('useDaysMode', 'Days');
316
317
    $cal = Koha::Calendar->new( branchcode => 'CPL', days_mode => 'Calendar' );
318
    is( $cal->{days_mode}, 'Calendar', q|If set, days_mode is correctly set|);
319
};
320
321
END {
322
    $cache->clear_from_cache('MPL_holidays');
323
    $cache->clear_from_cache('CPL_holidays');
324
};
(-)a/t/db_dependent/Calendar.t (-393 lines)
Lines 1-393 Link Here
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 => 6;
21
use Time::Fake;
22
23
use t::lib::Mocks;
24
use t::lib::TestBuilder;
25
26
use DateTime;
27
use Koha::Caches;
28
use Koha::DateUtils qw( dt_from_string );
29
30
use_ok('Koha::Calendar');
31
32
my $schema  = Koha::Database->new->schema;
33
$schema->storage->txn_begin;
34
35
my $today = dt_from_string();
36
my $holiday_dt = $today->clone;
37
$holiday_dt->add(days => 3);
38
39
Koha::Caches->get_instance()->flush_all();
40
41
my $builder = t::lib::TestBuilder->new();
42
my $library = $builder->build_object({ class => 'Koha::Libraries' });
43
my $holiday = $builder->build(
44
    {
45
        source => 'SpecialHoliday',
46
        value  => {
47
            branchcode  => $library->branchcode,
48
            day         => $holiday_dt->day,
49
            month       => $holiday_dt->month,
50
            year        => $holiday_dt->year,
51
            title       => 'My holiday',
52
            isexception => 0
53
        },
54
    }
55
);
56
57
my $calendar = Koha::Calendar->new( branchcode => $library->branchcode, days_mode => 'Calendar' );
58
59
subtest 'days_forward' => sub {
60
61
    plan tests => 4;
62
    my $forwarded_dt = $calendar->days_forward( $today, 2 );
63
    my $expected = $today->clone->add( days => 2 );
64
    is( $forwarded_dt->ymd, $expected->ymd, 'With no holiday on the perioddays_forward should add 2 days' );
65
66
    $forwarded_dt = $calendar->days_forward( $today, 5 );
67
    $expected = $today->clone->add( days => 6 );
68
    is( $forwarded_dt->ymd, $expected->ymd, 'With holiday on the perioddays_forward should add 5 days + 1 day for holiday'
69
    );
70
71
    $forwarded_dt = $calendar->days_forward( $today, 0 );
72
    is( $forwarded_dt->ymd, $today->ymd, '0 day should return start dt' );
73
74
    $forwarded_dt = $calendar->days_forward( $today, -2 );
75
    is( $forwarded_dt->ymd, $today->ymd, 'negative day should return start dt' );
76
};
77
78
subtest 'crossing_DST' => sub {
79
80
    plan tests => 3;
81
82
    my $tz = DateTime::TimeZone->new( name => 'America/New_York' );
83
    my $start_date = dt_from_string( "2016-03-09 02:29:00", undef, $tz );
84
    my $end_date   = dt_from_string( "2017-01-01 00:00:00", undef, $tz );
85
    my $days_between = $calendar->days_between( $start_date, $end_date );
86
    is( $days_between->delta_days, 298, "Days calculated correctly" );
87
    $days_between = $calendar->days_between( $end_date, $start_date );
88
    is( $days_between->delta_days, 298, "Swapping returns the same" );
89
    my $hours_between = $calendar->hours_between( $start_date, $end_date );
90
    is(
91
        $hours_between->delta_minutes,
92
        298 * 24 * 60 - 149,
93
        "Hours (in minutes) calculated correctly"
94
    );
95
};
96
97
subtest 'hours_between | days_between' => sub {
98
99
    plan tests => 2;
100
101
    #    November 2019
102
    # Su Mo Tu We Th Fr Sa
103
    #                 1  2
104
    #  3  4 *5* 6  7  8  9
105
    # 10 11 12 13 14 15 16
106
    # 17 18 19 20 21 22 23
107
    # 24 25 26 27 28 29 30
108
109
    my $now    = dt_from_string('2019-11-05 12:34:56'); # Today is 2019 Nov 5th
110
    my $nov_6  = dt_from_string('2019-11-06 12:34:56');
111
    my $nov_7  = dt_from_string('2019-11-07 12:34:56');
112
    my $nov_12 = dt_from_string('2019-11-12 12:34:56');
113
    my $nov_13 = dt_from_string('2019-11-13 12:34:56');
114
    my $nov_15 = dt_from_string('2019-11-15 12:34:56');
115
    Time::Fake->offset($now->epoch);
116
117
    subtest 'No holiday' => sub {
118
119
        plan tests => 2;
120
121
        my $library = $builder->build_object({ class => 'Koha::Libraries' });
122
        my $calendar = Koha::Calendar->new( branchcode => $library->branchcode );
123
124
        subtest 'Same hours' => sub {
125
126
            plan tests => 8;
127
128
            # Between 5th and 6th
129
            my $diff_hours = $calendar->hours_between( $now, $nov_6 )->hours;
130
            is( $diff_hours, 1 * 24, 'hours: 1 day, no holiday' );
131
            my $diff_days = $calendar->days_between( $now, $nov_6 )->delta_days;
132
            is( $diff_days, 1, 'days: 1 day, no holiday' );
133
134
            # Between 5th and 7th
135
            $diff_hours = $calendar->hours_between( $now, $nov_7 )->hours;
136
            is( $diff_hours, 2 * 24, 'hours: 2 days, no holiday' );
137
            $diff_days = $calendar->days_between( $now, $nov_7 )->delta_days;
138
            is( $diff_days, 2, 'days: 2 days, no holiday' );
139
140
            # Between 5th and 12th
141
            $diff_hours = $calendar->hours_between( $now, $nov_12 )->hours;
142
            is( $diff_hours, 7 * 24, 'hours: 7 days, no holiday' );
143
            $diff_days = $calendar->days_between( $now, $nov_12 )->delta_days;
144
            is( $diff_days, 7, 'days: 7 days, no holiday' );
145
146
            # Between 5th and 15th
147
            $diff_hours = $calendar->hours_between( $now, $nov_15 )->hours;
148
            is( $diff_hours, 10 * 24, 'hours: 10 days, no holiday' );
149
            $diff_days = $calendar->days_between( $now, $nov_15 )->delta_days;
150
            is( $diff_days, 10, 'days: 10 days, no holiday' );
151
        };
152
153
        subtest 'Different hours' => sub {
154
155
            plan tests => 10;
156
157
            # Between 5th and 5th (Same day short hours loan)
158
            my $diff_hours = $calendar->hours_between( $now, $now->clone->add(hours => 3) )->hours;
159
            is( $diff_hours, 3, 'hours: 3 hours, no holidays' );
160
            my $diff_days = $calendar->days_between( $now, $now->clone->add(hours => 3) )->delta_days;
161
            is( $diff_days, 0, 'days: 3 hours, no holidays' );
162
163
            # Between 5th and 6th
164
            $diff_hours = $calendar->hours_between( $now, $nov_6->clone->subtract(hours => 3) )->hours;
165
            is( $diff_hours, 1 * 24 - 3, 'hours: 21 hours, no holidays' );
166
            $diff_days = $calendar->days_between( $now, $nov_6->clone->subtract(hours => 3) )->delta_days;
167
            is( $diff_days, 1, 'days: 21 hours, no holidays' );
168
169
            # Between 5th and 7th
170
            $diff_hours = $calendar->hours_between( $now, $nov_7->clone->subtract(hours => 3) )->hours;
171
            is( $diff_hours, 2 * 24 - 3, 'hours: 45 hours, no holidays' );
172
            $diff_days = $calendar->days_between( $now, $nov_7->clone->subtract(hours => 3) )->delta_days;
173
            is( $diff_days, 2, 'days: 45 hours, no holidays' );
174
175
            # Between 5th and 12th
176
            $diff_hours = $calendar->hours_between( $now, $nov_12->clone->subtract(hours => 3) )->hours;
177
            is( $diff_hours, 7 * 24 - 3, 'hours: 165 hours, no holidays' );
178
            $diff_days = $calendar->days_between( $now, $nov_12->clone->subtract(hours => 3) )->delta_days;
179
            is( $diff_days, 7, 'days: 165 hours, no holidays' );
180
181
            # Between 5th and 15th
182
            $diff_hours = $calendar->hours_between( $now, $nov_15->clone->subtract(hours => 3) )->hours;
183
            is( $diff_hours, 10 * 24 - 3, 'hours: 237 hours, no holidays' );
184
            $diff_days = $calendar->days_between( $now, $nov_15->clone->subtract(hours => 3) )->delta_days;
185
            is( $diff_days, 10, 'days: 237 hours, no holidays' );
186
        };
187
    };
188
189
    subtest 'With holiday' => sub {
190
        plan tests => 2;
191
192
        my $library = $builder->build_object({ class => 'Koha::Libraries' });
193
194
        # Wednesdays are closed
195
        my $dbh = C4::Context->dbh;
196
        $dbh->do(q|
197
            INSERT INTO repeatable_holidays (branchcode,weekday,day,month,title,description)
198
            VALUES ( ?, ?, NULL, NULL, ?, '' )
199
        |, undef, $library->branchcode, 3, 'Closed on Wednesday');
200
201
        my $calendar = Koha::Calendar->new( branchcode => $library->branchcode );
202
203
        subtest 'Same hours' => sub {
204
            plan tests => 12;
205
206
            my ( $diff_hours, $diff_days );
207
208
            # Between 5th and 6th (This case should never happen in real code, one cannot return on a closed day)
209
            $diff_hours = $calendar->hours_between( $now, $nov_6 )->hours;
210
            is( $diff_hours, 0 * 24, 'hours: 1 day, end_dt = holiday' ); # FIXME Is this really should be 0?
211
            $diff_days = $calendar->days_between( $now, $nov_6)->delta_days;
212
            is( $diff_days, 0, 'days: 1 day, end_dt = holiday' ); # FIXME Is this really should be 0?
213
214
            # Between 6th and 7th (This case should never happen in real code, one cannot issue on a closed day)
215
            $diff_hours = $calendar->hours_between( $nov_6, $nov_7 )->hours;
216
            is( $diff_hours, 0 * 24, 'hours: 1 day, start_dt = holiday' ); # FIXME Is this really should be 0?
217
            $diff_days = $calendar->days_between( $nov_6, $nov_7 )->delta_days;
218
            is( $diff_days, 0, 'days: 1 day, start_dt = holiday' ); # FIXME Is this really should be 0?
219
220
            # Between 5th and 7th
221
            $diff_hours = $calendar->hours_between( $now, $nov_7 )->hours;
222
            is( $diff_hours, 2 * 24 - 1 * 24, 'hours: 2 days, 1 holiday' );
223
            $diff_days = $calendar->days_between( $now, $nov_7 )->delta_days;
224
            is( $diff_days, 2 - 1, 'days: 2 days, 1 holiday' );
225
226
            # Between 5th and 12th
227
            $diff_hours = $calendar->hours_between( $now, $nov_12 )->hours;
228
            is( $diff_hours, 7 * 24 - 1 * 24, 'hours: 7 days, 1 holiday' );
229
            $diff_days = $calendar->days_between( $now, $nov_12)->delta_days;
230
            is( $diff_days, 7 - 1, 'day: 7 days, 1 holiday' );
231
232
            # Between 5th and 13th
233
            $diff_hours = $calendar->hours_between( $now, $nov_13 )->hours;
234
            is( $diff_hours, 8 * 24 - 2 * 24, 'hours: 8 days, 2 holidays' );
235
            $diff_days = $calendar->days_between( $now, $nov_13)->delta_days;
236
            is( $diff_days, 8 - 2, 'days: 8 days, 2 holidays' );
237
238
            # Between 5th and 15th
239
            $diff_hours = $calendar->hours_between( $now, $nov_15 )->hours;
240
            is( $diff_hours, 10 * 24 - 2 * 24, 'hours: 10 days, 2 holidays' );
241
            $diff_days = $calendar->days_between( $now, $nov_15)->delta_days;
242
            is( $diff_days, 10 - 2, 'days: 10 days, 2 holidays' );
243
        };
244
245
        subtest 'Different hours' => sub {
246
            plan tests => 14;
247
248
            my ( $diff_hours, $diff_days );
249
250
            # Between 5th and 5th (Same day short hours loan)
251
            # No test - Tested above as 5th is an open day
252
253
            # Between 5th and 6th (This case should never happen in real code, one cannot return on a closed day)
254
            my $duration = $calendar->hours_between( $now, $nov_6->clone->subtract(hours => 3) );
255
            is( $duration->hours, abs(0 * 24 - 3), 'hours: 21 hours, end_dt = holiday' ); # FIXME $duration->hours always return a abs
256
            is( $duration->is_negative, 1, '? is negative ?' ); # FIXME Do really test for that case in our calls to hours_between?
257
            $duration = $calendar->days_between( $now, $nov_6->clone->subtract(hours => 3) );
258
            is( $duration->hours, abs(0), 'days: 21 hours, end_dt = holiday' ); # FIXME Is this correct?
259
260
            # Between 6th and 7th (This case should never happen in real code, one cannot issue on a closed day)
261
            $duration = $calendar->hours_between( $nov_6, $nov_7->clone->subtract(hours => 3) );
262
            is( $duration->hours, abs(0 * 24 - 3), 'hours: 21 hours, start_dt = holiday' ); # FIXME $duration->hours always return a abs
263
            is( $duration->is_negative, 1, '? is negative ?' ); # FIXME Do really test for that case in our calls to hours_between?
264
            $duration = $calendar->days_between( $nov_6, $nov_7->clone->subtract(hours => 3) );
265
            is( $duration->hours, abs(0), 'days: 21 hours, start_dt = holiday' ); # FIXME Is this correct?
266
267
            # Between 5th and 7th
268
            $diff_hours = $calendar->hours_between( $now, $nov_7->clone->subtract(hours => 3) )->hours;
269
            is( $diff_hours, 2 * 24 - 1 * 24 - 3, 'hours: 45 hours, 1 holiday' );
270
            $diff_days = $calendar->days_between( $now, $nov_7->clone->subtract(hours => 3) )->delta_days;
271
            is( $diff_days, 2 - 1, 'days: 45 hours, 1 holiday' );
272
273
            # Between 5th and 12th
274
            $diff_hours = $calendar->hours_between( $now, $nov_12->clone->subtract(hours => 3) )->hours;
275
            is( $diff_hours, 7 * 24 - 1 * 24 - 3, 'hours: 165 hours, 1 holiday' );
276
            $diff_days = $calendar->days_between( $now, $nov_12->clone->subtract(hours => 3) )->delta_days;
277
            is( $diff_days, 7 - 1, 'days: 165 hours, 1 holiday' );
278
279
            # Between 5th and 13th
280
            $diff_hours = $calendar->hours_between( $now, $nov_13->clone->subtract(hours => 3) )->hours;
281
            is( $diff_hours, 8 * 24 - 2 * 24 - 3, 'hours: 289 hours, 2 holidays ' );
282
            $diff_days = $calendar->days_between( $now, $nov_13->clone->subtract(hours => 3) )->delta_days;
283
            is( $diff_days, 8 - 1, 'days: 289 hours, 2 holidays' );
284
285
            # Between 5th and 15th
286
            $diff_hours = $calendar->hours_between( $now, $nov_15->clone->subtract(hours => 3) )->hours;
287
            is( $diff_hours, 10 * 24 - 2 * 24 - 3, 'hours: 237 hours, 2 holidays' );
288
            $diff_days = $calendar->days_between( $now, $nov_15->clone->subtract(hours => 3) )->delta_days;
289
            is( $diff_days, 10 - 2, 'days: 237 hours, 2 holidays' );
290
        };
291
292
    };
293
294
    Time::Fake->reset;
295
};
296
297
subtest 'is_holiday' => sub {
298
    plan tests => 1;
299
300
    subtest 'weekday holidays' => sub {
301
        plan tests => 7;
302
303
        my $library = $builder->build_object( { class => 'Koha::Libraries' } );
304
305
        my $day = dt_from_string();
306
        my $dow = scalar $day->day_of_week;
307
        $dow = 0 if $dow == 7;
308
309
        # Closed this day of the week
310
        my $dbh = C4::Context->dbh;
311
        $dbh->do(
312
            q|
313
            INSERT INTO repeatable_holidays (branchcode,weekday,day,month,title,description)
314
            VALUES ( ?, ?, NULL, NULL, ?, '' )
315
        |, undef, $library->branchcode, $dow, "TEST"
316
        );
317
318
        # Iterate 7 days
319
        my $sth = $dbh->prepare(
320
"UPDATE repeatable_holidays SET weekday = ? WHERE branchcode = ? AND title = 'TEST'"
321
        );
322
        for my $i ( 0 .. 6 ) {
323
            my $calendar =
324
              Koha::Calendar->new( branchcode => $library->branchcode );
325
326
            is( $calendar->is_holiday($day), 1, $day->day_name() ." works as a repeatable holiday");
327
328
            # Increment the date and holiday day
329
            $day->add( days => 1 );
330
            $dow++;
331
            $dow = 0 if $dow == 7;
332
            $sth->execute($dow, $library->branchcode);
333
        }
334
    };
335
};
336
337
subtest 'get_push_amt' => sub {
338
    plan tests => 1;
339
340
    t::lib::Mocks::mock_preference('useDaysMode', 'Dayweek');
341
342
    subtest 'weekday holidays' => sub {
343
        plan tests => 7;
344
345
        my $library = $builder->build_object( { class => 'Koha::Libraries' } );
346
347
        my $day = dt_from_string();
348
        my $dow = scalar $day->day_of_week;
349
        $dow = 0 if $dow == 7;
350
351
        # Closed this day of the week
352
        my $dbh = C4::Context->dbh;
353
        $dbh->do(
354
            q|
355
            INSERT INTO repeatable_holidays (branchcode,weekday,day,month,title,description)
356
            VALUES ( ?, ?, NULL, NULL, ?, '' )
357
        |, undef, $library->branchcode, $dow, "TEST"
358
        );
359
360
        # Iterate 7 days
361
        my $sth = $dbh->prepare(
362
"UPDATE repeatable_holidays SET weekday = ? WHERE branchcode = ? AND title = 'TEST'"
363
        );
364
        for my $i ( 0 .. 6 ) {
365
            my $calendar =
366
              Koha::Calendar->new( branchcode => $library->branchcode, days_mode => 'Dayweek' );
367
368
            my $npa;
369
            eval {
370
                local $SIG{ALRM} = sub { die "alarm\n" };    # NB: \n required
371
                alarm 2;
372
                $npa = $calendar->next_open_days( $day, 0 );
373
                alarm 0;
374
            };
375
            if ($@) {
376
                die unless $@ eq "alarm\n";    # propagate unexpected errors
377
                # timed out
378
                ok(0, "next_push_amt succeeded for ".$day->day_name()." weekday holiday");
379
            }
380
            else {
381
                ok($npa, "next_push_amt succeeded for ".$day->day_name()." weekday holiday");
382
            }
383
384
            # Increment the date and holiday day
385
            $day->add( days => 1 );
386
            $dow++;
387
            $dow = 0 if $dow == 7;
388
            $sth->execute( $dow, $library->branchcode );
389
        }
390
    };
391
};
392
393
$schema->storage->txn_rollback();
(-)a/t/db_dependent/Circulation.t (-27 / +48 lines)
Lines 32-38 use t::lib::Mocks; Link Here
32
use t::lib::TestBuilder;
32
use t::lib::TestBuilder;
33
33
34
use C4::Accounts;
34
use C4::Accounts;
35
use C4::Calendar qw( new insert_single_holiday insert_week_day_holiday delete_holiday );
36
use C4::Circulation qw( AddIssue AddReturn CanBookBeRenewed GetIssuingCharges AddRenewal GetSoonestRenewDate GetLatestAutoRenewDate LostItem GetUpcomingDueIssues CanBookBeIssued AddIssuingCharge MarkIssueReturned ProcessOfflinePayment transferbook updateWrongTransfer );
35
use C4::Circulation qw( AddIssue AddReturn CanBookBeRenewed GetIssuingCharges AddRenewal GetSoonestRenewDate GetLatestAutoRenewDate LostItem GetUpcomingDueIssues CanBookBeIssued AddIssuingCharge MarkIssueReturned ProcessOfflinePayment transferbook updateWrongTransfer );
37
use C4::Biblio;
36
use C4::Biblio;
38
use C4::Items qw( ModItemTransfer );
37
use C4::Items qw( ModItemTransfer );
Lines 42-47 use C4::Overdues qw( CalcFine UpdateFine get_chargeable_units ); Link Here
42
use C4::Members::Messaging qw( SetMessagingPreference );
41
use C4::Members::Messaging qw( SetMessagingPreference );
43
use Koha::DateUtils qw( dt_from_string output_pref );
42
use Koha::DateUtils qw( dt_from_string output_pref );
44
use Koha::Database;
43
use Koha::Database;
44
use Koha::DiscreteCalendar;
45
use Koha::Items;
45
use Koha::Items;
46
use Koha::Item::Transfers;
46
use Koha::Item::Transfers;
47
use Koha::Checkouts;
47
use Koha::Checkouts;
Lines 115-122 my $mocked_datetime = Test::MockModule->new('DateTime'); Link Here
115
$mocked_datetime->mock( 'now', sub { return $now_value->clone; } );
115
$mocked_datetime->mock( 'now', sub { return $now_value->clone; } );
116
116
117
my $cache = Koha::Caches->get_instance();
117
my $cache = Koha::Caches->get_instance();
118
$dbh->do(q|DELETE FROM special_holidays|);
118
$dbh->do(q|DELETE FROM discrete_calendar|);
119
$dbh->do(q|DELETE FROM repeatable_holidays|);
120
my $branches = Koha::Libraries->search();
119
my $branches = Koha::Libraries->search();
121
for my $branch ( $branches->next ) {
120
for my $branch ( $branches->next ) {
122
    my $key = $branch->branchcode . "_holidays";
121
    my $key = $branch->branchcode . "_holidays";
Lines 2775-2789 subtest 'AddReturn + suspension_chargeperiod' => sub { Link Here
2775
    t::lib::Mocks::mock_preference('SuspensionsCalendar', 'noSuspensionsWhenClosed');
2774
    t::lib::Mocks::mock_preference('SuspensionsCalendar', 'noSuspensionsWhenClosed');
2776
2775
2777
    # Adding a holiday 2 days ago
2776
    # Adding a holiday 2 days ago
2778
    my $calendar = C4::Calendar->new(branchcode => $library->{branchcode});
2777
    my $calendar = Koha::DiscreteCalendar->new({ branchcode => $library->{branchcode} });
2779
    my $two_days_ago = $now->clone->subtract( days => 2 );
2778
    my $two_days_ago = $now->clone->subtract( days => 2 );
2780
    $calendar->insert_single_holiday(
2779
    $calendar->edit_holiday({
2781
        day             => $two_days_ago->day,
2780
        holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
2782
        month           => $two_days_ago->month,
2781
        override        => 1,
2783
        year            => $two_days_ago->year,
2782
        start_date      => $two_days_ago,
2783
        end_date        => $two_days_ago,
2784
        title           => 'holidayTest-2d',
2784
        title           => 'holidayTest-2d',
2785
        description     => 'holidayDesc 2 days ago'
2785
        description     => 'holidayDesc 2 days ago'
2786
    );
2786
    });
2787
    # With 5 days of overdue, only 4 (x finedays=2) days must charged (one was an holiday)
2787
    # With 5 days of overdue, only 4 (x finedays=2) days must charged (one was an holiday)
2788
    $expected_expiration = $now->clone->add( days => floor( ( ( 5 - 0 - 1 ) / 1 ) * 2 ) );
2788
    $expected_expiration = $now->clone->add( days => floor( ( ( 5 - 0 - 1 ) / 1 ) * 2 ) );
2789
    test_debarment_on_checkout(
2789
    test_debarment_on_checkout(
Lines 2798-2810 subtest 'AddReturn + suspension_chargeperiod' => sub { Link Here
2798
2798
2799
    # Adding a holiday 2 days ahead, with finesCalendar=noFinesWhenClosed it should be skipped
2799
    # Adding a holiday 2 days ahead, with finesCalendar=noFinesWhenClosed it should be skipped
2800
    my $two_days_ahead = $now->clone->add( days => 2 );
2800
    my $two_days_ahead = $now->clone->add( days => 2 );
2801
    $calendar->insert_single_holiday(
2801
    $calendar->edit_holiday({
2802
        day             => $two_days_ahead->day,
2802
        holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
2803
        month           => $two_days_ahead->month,
2803
        start_date      => $two_days_ahead,
2804
        year            => $two_days_ahead->year,
2804
        end_date        => $two_days_ahead,
2805
        title           => 'holidayTest+2d',
2805
        title           => 'holidayTest+2d',
2806
        description     => 'holidayDesc 2 days ahead'
2806
        description     => 'holidayDesc 2 days ahead'
2807
    );
2807
    });
2808
2808
2809
    # Same as above, but we should skip D+2
2809
    # Same as above, but we should skip D+2
2810
    $expected_expiration = $now->clone->add( days => floor( ( ( 5 - 0 - 1 ) / 1 ) * 2 ) + 1 );
2810
    $expected_expiration = $now->clone->add( days => floor( ( ( 5 - 0 - 1 ) / 1 ) * 2 ) + 1 );
Lines 2820-2832 subtest 'AddReturn + suspension_chargeperiod' => sub { Link Here
2820
2820
2821
    # Adding another holiday, day of expiration date
2821
    # Adding another holiday, day of expiration date
2822
    my $expected_expiration_dt = dt_from_string($expected_expiration);
2822
    my $expected_expiration_dt = dt_from_string($expected_expiration);
2823
    $calendar->insert_single_holiday(
2823
    $calendar->edit_holiday({
2824
        day             => $expected_expiration_dt->day,
2824
        holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
2825
        month           => $expected_expiration_dt->month,
2825
        start_date      => $expected_expiration_dt,
2826
        year            => $expected_expiration_dt->year,
2826
        end_date        => $expected_expiration_dt,
2827
        title           => 'holidayTest_exp',
2827
        title           => 'holidayTest_exp',
2828
        description     => 'holidayDesc on expiration date'
2828
        description     => 'holidayDesc on expiration date'
2829
    );
2829
    });
2830
    # Expiration date will be the day after
2830
    # Expiration date will be the day after
2831
    test_debarment_on_checkout(
2831
    test_debarment_on_checkout(
2832
        {
2832
        {
Lines 2901-2906 subtest 'AddReturn + suspension_chargeperiod' => sub { Link Here
2901
    AddIssue( $patron->unblessed, $item_1->barcode, $now->clone->subtract( days => 1 ) );
2901
    AddIssue( $patron->unblessed, $item_1->barcode, $now->clone->subtract( days => 1 ) );
2902
    (undef, $message) = AddReturn( $item_1->barcode, $library->{branchcode}, undef, $now );
2902
    (undef, $message) = AddReturn( $item_1->barcode, $library->{branchcode}, undef, $now );
2903
    is( $message->{WasReturned} && exists $message->{PrevDebarred}, 1, 'Previously debarred message for Addreturn when overdue');
2903
    is( $message->{WasReturned} && exists $message->{PrevDebarred}, 1, 'Previously debarred message for Addreturn when overdue');
2904
2905
    # delete holidays
2906
    $calendar->edit_holiday({
2907
        holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{NONE},
2908
        start_date      => $two_days_ago,
2909
        end_date        => $expected_expiration_dt,
2910
        title           => '',
2911
        description     => ''
2912
    });
2904
};
2913
};
2905
2914
2906
subtest 'CanBookBeIssued + AutoReturnCheckedOutItems' => sub {
2915
subtest 'CanBookBeIssued + AutoReturnCheckedOutItems' => sub {
Lines 4760-4777 subtest 'Incremented fee tests' => sub { Link Here
4760
    $accountline->delete();
4769
    $accountline->delete();
4761
    $issue->delete();
4770
    $issue->delete();
4762
4771
4763
    my $calendar = C4::Calendar->new( branchcode => $library->id );
4772
    my $calendar = Koha::DiscreteCalendar->new({ branchcode => $library->id });
4764
    # DateTime 1..7 (Mon..Sun), C4::Calender 0..6 (Sun..Sat)
4773
    # DateTime 1..7 (Mon..Sun), Koha::DiscreteCalender 1..7 (Sun..Sat)
4765
    my $closed_day =
4774
    my $closed_day =
4766
        ( $dt_from->day_of_week == 6 ) ? 0
4775
        ( $dt_from->day_of_week == 7 ) ? 1
4767
      : ( $dt_from->day_of_week == 7 ) ? 1
4776
      : $dt_from->day_of_week + 1;
4768
      :                                  $dt_from->day_of_week + 1;
4769
    my $closed_day_name = $dt_from->clone->add(days => 1)->day_name;
4777
    my $closed_day_name = $dt_from->clone->add(days => 1)->day_name;
4770
    $calendar->insert_week_day_holiday(
4778
    $calendar->edit_holiday({
4779
        holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{WEEKLY},
4771
        weekday     => $closed_day,
4780
        weekday     => $closed_day,
4781
        start_date   => $dt_from,
4782
        end_date     => $dt_from,
4772
        title       => 'Test holiday',
4783
        title       => 'Test holiday',
4773
        description => 'Test holiday'
4784
        description => 'Test holiday'
4774
    );
4785
    });
4775
    $issue =
4786
    $issue =
4776
      AddIssue( $patron->unblessed, $item->barcode, $dt_to, undef, $dt_from );
4787
      AddIssue( $patron->unblessed, $item->barcode, $dt_to, undef, $dt_from );
4777
    $accountline = Koha::Account::Lines->find( { itemnumber => $item->id } );
4788
    $accountline = Koha::Account::Lines->find( { itemnumber => $item->id } );
Lines 4864-4870 subtest 'Incremented fee tests' => sub { Link Here
4864
    $accountline->delete();
4875
    $accountline->delete();
4865
    $issue->delete();
4876
    $issue->delete();
4866
4877
4867
    $calendar->delete_holiday( weekday => $closed_day );
4878
    $calendar->edit_holiday({
4879
        holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{NONE},
4880
        delete_type  => 1,
4881
        override     => 1,
4882
        weekday      => $closed_day,
4883
        start_date   => $dt_from,
4884
        end_date     => $dt_from,
4885
        title       => '',
4886
        description => '',
4887
    });
4888
4868
    $issue =
4889
    $issue =
4869
      AddIssue( $patron->unblessed, $item->barcode, $dt_to, undef, $dt_from );
4890
      AddIssue( $patron->unblessed, $item->barcode, $dt_to, undef, $dt_from );
4870
    $accountline = Koha::Account::Lines->find( { itemnumber => $item->id } );
4891
    $accountline = Koha::Account::Lines->find( { itemnumber => $item->id } );
(-)a/t/db_dependent/Circulation/CalcDateDue.t (-88 / +137 lines)
Lines 8-14 use DBI; Link Here
8
use DateTime;
8
use DateTime;
9
use t::lib::Mocks;
9
use t::lib::Mocks;
10
use t::lib::TestBuilder;
10
use t::lib::TestBuilder;
11
use C4::Calendar qw( new insert_single_holiday delete_holiday insert_week_day_holiday );
11
use Koha::DateUtils qw(dt_from_string);
12
use Koha::DiscreteCalendar;
12
13
13
use Koha::CirculationRules;
14
use Koha::CirculationRules;
14
15
Lines 67-89 is($date, $dateexpiry . 'T23:59:00', 'date expiry with useDaysMode to noDays'); Link Here
67
68
68
# Let's add a special holiday on 2013-01-01. With ReturnBeforeExpiry and
69
# Let's add a special holiday on 2013-01-01. With ReturnBeforeExpiry and
69
# useDaysMode different from 'Days', return should forward the dateexpiry.
70
# useDaysMode different from 'Days', return should forward the dateexpiry.
70
my $calendar = C4::Calendar->new(branchcode => $branchcode);
71
my $calendar = Koha::DiscreteCalendar->new({ branchcode => $branchcode });
71
$calendar->insert_single_holiday(
72
$calendar->edit_holiday({
72
    day             => 1,
73
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
73
    month           => 1,
74
    override        => 1,
74
    year            => 2013,
75
    start_date      => dt_from_string($dateexpiry, 'iso'),
76
    end_date        => dt_from_string($dateexpiry, 'iso'),
75
    title           =>'holidayTest',
77
    title           =>'holidayTest',
76
    description     => 'holidayDesc'
78
    description     => 'holidayDesc'
77
);
79
});
78
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower );
80
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower );
79
is($date, '2012-12-31T23:59:00', 'date expiry should be 2013-01-01 -1 day');
81
is($date, '2012-12-31T23:59:00', 'date expiry should be 2013-01-01 -1 day');
80
$calendar->insert_single_holiday(
82
$calendar->edit_holiday({
81
    day             => 31,
83
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
82
    month           => 12,
84
    override        => 1,
83
    year            => 2012,
85
    start_date      => dt_from_string('2012-12-31', 'iso'),
86
    end_date        => dt_from_string('2012-12-31', 'iso'),
84
    title           =>'holidayTest',
87
    title           =>'holidayTest',
85
    description     => 'holidayDesc'
88
    description     => 'holidayDesc'
86
);
89
});
87
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower );
90
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower );
88
is($date, '2012-12-30T23:59:00', 'date expiry should be 2013-01-01 -2 day');
91
is($date, '2012-12-30T23:59:00', 'date expiry should be 2013-01-01 -2 day');
89
92
Lines 123-160 is($date, '2013-02-' . (9 + $renewalperiod) . 'T23:59:00', "useDaysMode = Daywee Link Here
123
# Now let's add a closed day on the expected renewal date, it should
126
# Now let's add a closed day on the expected renewal date, it should
124
# roll forward as per Datedue (i.e. one day at a time)
127
# roll forward as per Datedue (i.e. one day at a time)
125
# For issues...
128
# For issues...
126
$calendar->insert_single_holiday(
129
my $issue_date = dt_from_string('2013-02-' . (9 + $issuelength), 'iso');
127
    day             => 9 + $issuelength,
130
$calendar->edit_holiday({
128
    month           => 2,
131
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
129
    year            => 2013,
132
    override        => 1,
133
    start_date      => $issue_date,
134
    end_date        => $issue_date,
130
    title           =>'DayweekTest1',
135
    title           =>'DayweekTest1',
131
    description     => 'DayweekTest1'
136
    description     => 'DayweekTest1'
132
);
137
});
138
133
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower );
139
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower );
134
is($date, '2013-02-' . (9 + $issuelength + 1) . 'T23:59:00', "useDaysMode = Dayweek, closed on due date, 10 day loan (should not trigger 7 day roll forward), issue date expiry ( start + $issuelength  + 1 )");
140
is($date, '2013-02-' . (9 + $issuelength + 1) . 'T23:59:00', "useDaysMode = Dayweek, closed on due date, 10 day loan (should not trigger 7 day roll forward), issue date expiry ( start + $issuelength  + 1 )");
135
# Remove the holiday we just created
141
# Remove the holiday we just created
136
$calendar->delete_holiday(
142
$calendar->edit_holiday({
137
    day             => 9 + $issuelength,
143
    holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{NONE},
138
    month           => 2,
144
    override     => 1,
139
    year            => 2013
145
    start_date   => $issue_date,
140
);
146
    end_date     => $issue_date,
147
    title        => '',
148
    description  => '',
149
});
141
150
142
# ...and for renewals...
151
# ...and for renewals...
143
$calendar->insert_single_holiday(
152
my $renewal_date = dt_from_string('2013-02-' . (9 + $renewalperiod), 'iso');
144
    day             => 9 + $renewalperiod,
153
$calendar->edit_holiday({
145
    month           => 2,
154
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
146
    year            => 2013,
155
    override        => 1,
156
    start_date      => $renewal_date,
157
    end_date        => $renewal_date,
147
    title           =>'DayweekTest2',
158
    title           =>'DayweekTest2',
148
    description     => 'DayweekTest2'
159
    description     => 'DayweekTest2'
149
);
160
});
150
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower, 1 );
161
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower, 1 );
151
is($date, '2013-02-' . (9 + $renewalperiod + 1) . 'T23:59:00', "useDaysMode = Dayweek, closed on due date, 5 day renewal (should not trigger 7 day roll forward), renewal date expiry ( start + $renewalperiod  + 1 )");
162
is($date, '2013-02-' . (9 + $renewalperiod + 1) . 'T23:59:00', "useDaysMode = Dayweek, closed on due date, 5 day renewal (should not trigger 7 day roll forward), renewal date expiry ( start + $renewalperiod  + 1 )");
152
# Remove the holiday we just created
163
# Remove the holiday we just created
153
$calendar->delete_holiday(
164
$calendar->edit_holiday({
154
    day             => 9 + $renewalperiod,
165
    title        => '',
155
    month           => 2,
166
    description  => '',
156
    year            => 2013,
167
    holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{NONE},
157
);
168
    start_date   => $renewal_date,
169
    end_date     => $renewal_date,
170
    override     => 1,
171
});
158
172
159
# Now we test it does the right thing if the loan and renewal periods
173
# Now we test it does the right thing if the loan and renewal periods
160
# are a multiple of 7 days
174
# are a multiple of 7 days
Lines 182-224 my $dayweek_borrower = {categorycode => 'K', dateexpiry => $dateexpiry}; Link Here
182
196
183
# For issues...
197
# For issues...
184
$start_date = DateTime->new({year => 2013, month => 2, day => 9});
198
$start_date = DateTime->new({year => 2013, month => 2, day => 9});
185
$calendar->insert_single_holiday(
199
my $holiday_date = $start_date->clone();
186
    day             => 9 + $dayweek_issuelength,
200
$holiday_date->add(days => $dayweek_issuelength);
187
    month           => 2,
201
$calendar->edit_holiday({
188
    year            => 2013,
202
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
203
    override        => 1,
204
    start_date      => $holiday_date,
205
    end_date        => $holiday_date,
189
    title           =>'DayweekTest3',
206
    title           =>'DayweekTest3',
190
    description     => 'DayweekTest3'
207
    description     => 'DayweekTest3'
191
);
208
});
192
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $dayweek_borrower );
209
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $dayweek_borrower );
193
my $issue_should_add = $dayweek_issuelength + 7;
210
my $issue_should_add = $dayweek_issuelength + 7;
194
my $dayweek_issue_expected = $start_date->add( days => $issue_should_add );
211
my $dayweek_issue_expected = $start_date->add( days => $issue_should_add );
195
is($date, $dayweek_issue_expected->strftime('%F') . 'T23:59:00', "useDaysMode = Dayweek, closed on due date, 14 day loan (should trigger 7 day roll forward), issue date expiry ( start + $issue_should_add )");
212
is($date, $dayweek_issue_expected->strftime('%F') . 'T23:59:00', "useDaysMode = Dayweek, closed on due date, 14 day loan (should trigger 7 day roll forward), issue date expiry ( start + $issue_should_add )");
196
# Remove the holiday we just created
213
# Remove the holiday we just created
197
$calendar->delete_holiday(
214
$calendar->edit_holiday({
198
    day             => 9 + $dayweek_issuelength,
215
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{NONE},
199
    month           => 2,
216
    override        => 1,
200
    year            => 2013,
217
    start_date      => $holiday_date,
201
);
218
    end_date        => $holiday_date,
219
    title           => '',
220
    description     => '',
221
});
202
222
203
# ...and for renewals...
223
# ...and for renewals...
204
$start_date = DateTime->new({year => 2013, month => 2, day => 9});
224
$start_date = DateTime->new({year => 2013, month => 2, day => 9});
205
$calendar->insert_single_holiday(
225
$holiday_date = $start_date->clone();
206
    day             => 9 + $dayweek_renewalperiod,
226
$holiday_date->add(days => $dayweek_renewalperiod);
207
    month           => 2,
227
$calendar->edit_holiday({
208
    year            => 2013,
228
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
229
    override        => 1,
230
    start_date      => $holiday_date,
231
    end_date        => $holiday_date,
209
    title           => 'DayweekTest4',
232
    title           => 'DayweekTest4',
210
    description     => 'DayweekTest4'
233
    description     => 'DayweekTest4'
211
);
234
});
212
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $dayweek_borrower, 1 );
235
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $dayweek_borrower, 1 );
213
my $renewal_should_add = $dayweek_renewalperiod + 7;
236
my $renewal_should_add = $dayweek_renewalperiod + 7;
214
my $dayweek_renewal_expected = $start_date->add( days => $renewal_should_add );
237
my $dayweek_renewal_expected = $start_date->add( days => $renewal_should_add );
215
is($date, $dayweek_renewal_expected->strftime('%F') . 'T23:59:00', "useDaysMode = Dayweek, closed on due date, 7 day renewal (should trigger 7 day roll forward), renewal date expiry ( start + $renewal_should_add )");
238
is($date, $dayweek_renewal_expected->strftime('%F') . 'T23:59:00', "useDaysMode = Dayweek, closed on due date, 7 day renewal (should trigger 7 day roll forward), renewal date expiry ( start + $renewal_should_add )");
216
# Remove the holiday we just created
239
# Remove the holiday we just created
217
$calendar->delete_holiday(
240
$calendar->edit_holiday({
218
    day             => 9 + $dayweek_renewalperiod,
241
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{NONE},
219
    month           => 2,
242
    override        => 1,
220
    year            => 2013,
243
    start_date      => $holiday_date,
221
);
244
    end_date        => $holiday_date,
245
    title           => '',
246
    description     => '',
247
});
222
248
223
# Now test it continues to roll forward by 7 days until it finds
249
# Now test it continues to roll forward by 7 days until it finds
224
# an open day, so we create a 3 week period of closed Saturdays
250
# an open day, so we create a 3 week period of closed Saturdays
Lines 226-254 $start_date = DateTime->new({year => 2013, month => 2, day => 9}); Link Here
226
my $expected_rolled_date = DateTime->new({year => 2013, month => 3, day => 9});
252
my $expected_rolled_date = DateTime->new({year => 2013, month => 3, day => 9});
227
my $holiday = $start_date->clone();
253
my $holiday = $start_date->clone();
228
$holiday->add(days => 7);
254
$holiday->add(days => 7);
229
$calendar->insert_single_holiday(
255
$calendar->edit_holiday({
230
    day             => $holiday->day,
256
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
231
    month           => $holiday->month,
257
    override        => 1,
232
    year            => 2013,
258
    start_date      => $holiday,
259
    end_date        => $holiday,
233
    title           =>'DayweekTest5',
260
    title           =>'DayweekTest5',
234
    description     => 'DayweekTest5'
261
    description     => 'DayweekTest5'
235
);
262
});
236
$holiday->add(days => 7);
263
$holiday->add(days => 7);
237
$calendar->insert_single_holiday(
264
$calendar->edit_holiday({
238
    day             => $holiday->day,
265
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
239
    month           => $holiday->month,
266
    override        => 1,
240
    year            => 2013,
267
    start_date      => $holiday,
268
    end_date        => $holiday,
241
    title           =>'DayweekTest6',
269
    title           =>'DayweekTest6',
242
    description     => 'DayweekTest6'
270
    description     => 'DayweekTest6'
243
);
271
});
244
$holiday->add(days => 7);
272
$holiday->add(days => 7);
245
$calendar->insert_single_holiday(
273
$calendar->edit_holiday({
246
    day             => $holiday->day,
274
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
247
    month           => $holiday->month,
275
    override        => 1,
248
    year            => 2013,
276
    start_date      => $holiday,
277
    end_date        => $holiday,
249
    title           =>'DayweekTest7',
278
    title           =>'DayweekTest7',
250
    description     => 'DayweekTest7'
279
    description     => 'DayweekTest7'
251
);
280
});
252
# For issues...
281
# For issues...
253
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $dayweek_borrower );
282
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $dayweek_borrower );
254
$dayweek_issue_expected = $start_date->add( days => $issue_should_add );
283
$dayweek_issue_expected = $start_date->add( days => $issue_should_add );
Lines 262-294 is($date, $expected_rolled_date->strftime('%F') . 'T23:59:00', "useDaysMode = Da Link Here
262
$start_date = DateTime->new({year => 2013, month => 2, day => 9});
291
$start_date = DateTime->new({year => 2013, month => 2, day => 9});
263
my $del_holiday = $start_date->clone();
292
my $del_holiday = $start_date->clone();
264
$del_holiday->add(days => 7);
293
$del_holiday->add(days => 7);
265
$calendar->delete_holiday(
294
$calendar->edit_holiday({
266
    day             => $del_holiday->day,
295
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{NONE},
267
    month           => $del_holiday->month,
296
    override        => 1,
268
    year            => 2013
297
    start_date      => $del_holiday,
269
);
298
    end_date        => $del_holiday,
299
    title           => '',
300
    description     => '',
301
});
270
$del_holiday->add(days => 7);
302
$del_holiday->add(days => 7);
271
$calendar->delete_holiday(
303
$calendar->edit_holiday({
272
    day             => $del_holiday->day,
304
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{NONE},
273
    month           => $del_holiday->month,
305
    override        => 1,
274
    year            => 2013
306
    start_date      => $del_holiday,
275
);
307
    end_date        => $del_holiday,
308
    title           => '',
309
    description     => '',
310
});
276
$del_holiday->add(days => 7);
311
$del_holiday->add(days => 7);
277
$calendar->delete_holiday(
312
$calendar->edit_holiday({
278
    day             => $del_holiday->day,
313
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{NONE},
279
    month           => $del_holiday->month,
314
    override        => 1,
280
    year            => 2013
315
    start_date      => $del_holiday,
281
);
316
    end_date        => $del_holiday,
317
    title           => '',
318
    description     => '',
319
});
282
320
283
# Now test that useDaysMode "Dayweek" doesn't try to roll forward onto
321
# Now test that useDaysMode "Dayweek" doesn't try to roll forward onto
284
# a permanently closed day and instead rolls forward just one day
322
# a permanently closed day and instead rolls forward just one day
285
$start_date = DateTime->new({year => 2013, month => 2, day => 9});
323
$start_date = DateTime->new({year => 2013, month => 2, day => 9});
286
# Our tests are concerned with Saturdays, so let's close on Saturdays
324
# Our tests are concerned with Saturdays, so let's close on Saturdays
287
$calendar->insert_week_day_holiday(
325
$calendar->edit_holiday({
288
    weekday => 6,
326
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{WEEKLY},
327
    weekday         => 7,
328
    override        => 1,
329
    start_date      => $start_date,
330
    end_date        => $start_date,
289
    title => "Saturday closure",
331
    title => "Saturday closure",
290
    description => "Closed on Saturdays"
332
    description => "Closed on Saturdays"
291
);
333
});
292
# For issues...
334
# For issues...
293
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $dayweek_borrower );
335
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $dayweek_borrower );
294
$dayweek_issue_expected = $start_date->add( days => $dayweek_issuelength + 1 );
336
$dayweek_issue_expected = $start_date->add( days => $dayweek_issuelength + 1 );
Lines 299-307 $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $dayw Link Here
299
$dayweek_renewal_expected = $start_date->add( days => $dayweek_renewalperiod + 1 );
341
$dayweek_renewal_expected = $start_date->add( days => $dayweek_renewalperiod + 1 );
300
is($date, $dayweek_renewal_expected->strftime('%F') . 'T23:59:00', "useDaysMode = Dayweek, due on Saturday, closed on Saturdays, 7 day renewal (should trigger 1 day roll forward), issue date expiry ( start + 8 )");
342
is($date, $dayweek_renewal_expected->strftime('%F') . 'T23:59:00', "useDaysMode = Dayweek, due on Saturday, closed on Saturdays, 7 day renewal (should trigger 1 day roll forward), issue date expiry ( start + 8 )");
301
# Remove the holiday we just created
343
# Remove the holiday we just created
302
$calendar->delete_holiday(
344
$calendar->edit_holiday({
303
    weekday => 6
345
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{NONE},
304
);
346
    weekday         => 7,
347
    override        => 1,
348
    delete_type     => 1,
349
    start_date      => $start_date,
350
    end_date        => $start_date,
351
    title           => "",
352
    description     => ""
353
});
305
354
306
# Renewal period of 0 is valid
355
# Renewal period of 0 is valid
307
Koha::CirculationRules->search()->delete();
356
Koha::CirculationRules->search()->delete();
(-)a/t/db_dependent/Circulation/maxsuspensiondays.t (-5 / +4 lines)
Lines 20-25 $schema->storage->txn_begin; Link Here
20
my $builder = t::lib::TestBuilder->new;
20
my $builder = t::lib::TestBuilder->new;
21
my $dbh = C4::Context->dbh;
21
my $dbh = C4::Context->dbh;
22
22
23
# clear any holidays to avoid throwing off the suspension day
24
# calculations
25
$dbh->do('DELETE FROM discrete_calendar');
26
23
my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
27
my $branchcode = $builder->build({ source => 'Branch' })->{branchcode};
24
my $itemtype   = $builder->build({ source => 'Itemtype' })->{itemtype};
28
my $itemtype   = $builder->build({ source => 'Itemtype' })->{itemtype};
25
my $patron_category = $builder->build({ source => 'Category' });
29
my $patron_category = $builder->build({ source => 'Category' });
Lines 67-77 my $itemnumber = Koha::Item->new({ Link Here
67
        itype => $itemtype
71
        itype => $itemtype
68
    })->store->itemnumber;
72
    })->store->itemnumber;
69
73
70
# clear any holidays to avoid throwing off the suspension day
71
# calculations
72
$dbh->do('DELETE FROM special_holidays');
73
$dbh->do('DELETE FROM repeatable_holidays');
74
75
my $daysago20 = dt_from_string->add_duration(DateTime::Duration->new(days => -20));
74
my $daysago20 = dt_from_string->add_duration(DateTime::Duration->new(days => -20));
76
my $daysafter40 = dt_from_string->add_duration(DateTime::Duration->new(days => 40));
75
my $daysafter40 = dt_from_string->add_duration(DateTime::Duration->new(days => 40));
77
76
(-)a/t/db_dependent/DiscreteCalendar.t (+464 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# Copyright 2015 Koha Development team
4
#
5
# This file is part of Koha
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
use Test::More tests => 50;
22
use Test::MockModule;
23
24
use t::lib::TestBuilder;
25
26
use C4::Context;
27
use C4::Output;
28
use Koha::DateUtils qw ( dt_from_string );
29
use Koha::DiscreteCalendar;
30
31
32
my $module_context = Test::MockModule->new('C4::Context');
33
my $today = DateTime->today;
34
my $schema = Koha::Database->new->schema;
35
$schema->storage->txn_begin;
36
37
my $builder = t::lib::TestBuilder->new;
38
39
my $branch1 = $builder->build( { source => 'Branch' } )->{branchcode};
40
my $branch2 = $builder->build( { source => 'Branch' } )->{branchcode};
41
42
$builder->fill_discrete_calendar({ branchcode => $branch1, days => 365 });
43
44
$schema->resultset('DiscreteCalendar')->search({
45
    branchcode  => $branch1
46
})->update_all({
47
    is_opened    => 1,
48
    holiday_type => '',
49
    note         => '',
50
    open_hour    => '08:00:00',
51
    close_hour   => '16:00:00'
52
});
53
54
isnt($branch1,'', "First branch to do tests. BranchCode => $branch1");
55
isnt($branch2,'', "Second branch to do tests. BranchCode => $branch2");
56
57
#2 Calendars to make sure branches are treated separately.
58
my $calendar = Koha::DiscreteCalendar->new({branchcode => $branch1});
59
my $calendar2 = Koha::DiscreteCalendar->new({branchcode => $branch2});
60
61
my $unique_holiday = DateTime->today;
62
$calendar->edit_holiday({
63
    title => "Single holiday Today",
64
    holiday_type=> $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
65
    start_date=>$unique_holiday,
66
    end_date=>$unique_holiday
67
});
68
is($calendar->is_opened($unique_holiday), 0, "Branch closed today : $unique_holiday");
69
70
my @unique_holidays = $calendar->get_unique_holidays();
71
is(scalar @unique_holidays, 1, "Set of exception holidays at 1");
72
73
my $yesterday = DateTime->today->subtract(days => 1);
74
$calendar->edit_holiday({
75
    title => "Single holiday Today",
76
    holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
77
    start_date => $yesterday,
78
    end_date => $yesterday
79
});
80
is($calendar->is_opened($yesterday), 1, "Cannot edit dates in the past without override : $yesterday is not a holiday");
81
82
$calendar->edit_holiday({
83
    title => "Single holiday Today",
84
    holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
85
    start_date => $yesterday,
86
    end_date => $yesterday,
87
    override => 1
88
});
89
is($calendar->is_opened($yesterday), 0, "Can edit dates in the past without override : $yesterday is a holiday");
90
91
92
my $days_between_start = DateTime->today;
93
my $days_between_end = DateTime->today->add(days => 6);
94
my $days_between = $calendar->days_between($days_between_start, $days_between_end)->in_units('days');
95
is($days_between, 5, "Days between skips holiday");
96
97
my $repeatable_holiday = DateTime->today->add(days => 1);
98
$calendar->edit_holiday({
99
    title => "repeatable",
100
    holiday_type=> $Koha::DiscreteCalendar::HOLIDAYS->{REPEATABLE},
101
    start_date=>$repeatable_holiday,
102
    end_date=>$repeatable_holiday
103
});
104
is($calendar->is_opened($repeatable_holiday), 0, "Branch not opened on $repeatable_holiday");
105
106
my @repeatable_holidays = $calendar->get_repeatable_holidays();
107
is(scalar @repeatable_holidays, 1, "Set of repeatable holidays at 1");
108
109
# 1 being mysql DAYOFWEEK() for Sunday
110
$calendar->edit_holiday({
111
    title => "Weekly",
112
    weekday=>1,
113
    holiday_type=>$Koha::DiscreteCalendar::HOLIDAYS->{WEEKLY},
114
    start_date=>$today,
115
    end_date=>$today
116
});
117
my @weekly_holidays = $calendar->get_week_days_holidays();
118
is(scalar @weekly_holidays, 1, "Set of weekly holidays at 1");
119
120
my $sunday = DateTime->today->add(days =>( 7 - $today->day_of_week));
121
is($calendar->is_opened($sunday), 0, "Branch not opened on " . $sunday->day_name ."s");
122
123
my $unique_holiday_range_start = DateTime->today->add(days => 2);
124
my $unique_holiday_range_end = DateTime->today->add(days => 7);
125
$calendar->edit_holiday({
126
    title => "Single holiday range",
127
    holiday_type=>"E",
128
    start_date=>$unique_holiday_range_start,
129
    end_date=>$unique_holiday_range_end
130
});
131
@unique_holidays = $calendar->get_unique_holidays();
132
is(scalar @unique_holidays, 7, "Set of exception holidays at 7");
133
134
my $repeatable_holiday_range_start = DateTime->today->add(days => 8);
135
my $repeatable_holiday_range_end = DateTime->today->add(days => 13);
136
$calendar->edit_holiday({
137
    title => "Repeatable holiday range",
138
    holiday_type=>$Koha::DiscreteCalendar::HOLIDAYS->{REPEATABLE},
139
    start_date=>$repeatable_holiday_range_start,
140
    end_date=>$repeatable_holiday_range_end
141
});
142
@repeatable_holidays = $calendar->get_repeatable_holidays();
143
is(scalar @repeatable_holidays, 7, "Set of repeatable holidays at 7");
144
145
#Hourly loan
146
# ex :
147
# item due      : 2017-01-24 11:00:00
148
# item returned : 2017-01-26 10:00:00
149
# Branch closed : 2017-01-25
150
# Open/close hours : 08:00 to 16:00 (8h day)
151
# Hours due : 5 hours on 2017-01-24 + 2 hours on 2017-01-26 = 7hours
152
153
my $open_hours_since_start = dt_from_string->truncate(to => 'day')->add(days => 40, hours => 11);
154
my $open_hours_since_end = dt_from_string->truncate(to => 'day')->add(days => 42, hours => 10);
155
my $holiday_between =  dt_from_string->truncate(to => 'day')->add(days => 41);
156
$calendar->edit_holiday({
157
    title => '',
158
    holiday_type=>$Koha::DiscreteCalendar::HOLIDAYS->{NONE},
159
    start_date=>$open_hours_since_start,
160
    end_date=>$open_hours_since_end
161
});
162
163
$calendar->edit_holiday({
164
    title => "Single holiday",
165
    holiday_type=>$Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
166
    start_date=> $holiday_between,
167
    end_date=>$holiday_between
168
});
169
my $open_hours_between = $calendar->open_hours_between($open_hours_since_start, $open_hours_since_end);
170
is($open_hours_between, 7, "7 Hours open between $open_hours_since_start and $open_hours_since_end");
171
172
my $christmas = DateTime->new(
173
    year  => $today->year(),
174
    month => 12,
175
    day   => 25,
176
);
177
$calendar->edit_holiday({
178
    title => "Christmas",
179
    holiday_type=>$Koha::DiscreteCalendar::HOLIDAYS->{REPEATABLE},
180
    start_date=>$christmas,
181
    end_date=>$christmas,
182
    override => 1, # necessary if unit tests are run between Christmas and the first of the year
183
});
184
is($calendar->is_opened($christmas), 0, "Branch closed on Christmas : $christmas");
185
186
my $newyear = DateTime->new(
187
    year  => $today->year() +1,
188
    month => 1,
189
    day   => 1,
190
);
191
192
$calendar->edit_holiday({
193
    title => "New Year",
194
    holiday_type=>$Koha::DiscreteCalendar::HOLIDAYS->{REPEATABLE},
195
    start_date=>$newyear,
196
    end_date=>$newyear
197
});
198
is($calendar->is_opened($newyear), 0, "Branch closed on New Year : $newyear");
199
200
#Hours between :
201
my $date_due = DateTime->today->add(days => 1);
202
my $date_returned = DateTime->today->add(hours => 8);
203
my $hours_between = $calendar->hours_between($date_due, $date_returned);
204
205
is($hours_between->in_units('hours'), 16, "Date due $date_due, returned $date_returned, 16 hours in advance");
206
207
$date_due = DateTime->today->add(days => 1);
208
$date_returned = DateTime->today->add(days => 1, hours => 16);
209
$hours_between = $calendar->hours_between($date_due, $date_returned);
210
211
is($hours_between->in_units('hours'), 16, "Date due $date_due, returned $date_returned, 16 hours late");
212
213
214
#delete holidays
215
is($calendar->is_opened($today), 0, "Today is closed");
216
$calendar->edit_holiday({
217
    title => '',
218
    holiday_type=>$Koha::DiscreteCalendar::HOLIDAYS->{NONE},
219
    start_date=>$unique_holiday,
220
    end_date=>$unique_holiday
221
});
222
is($calendar->is_opened($today), 1, "Today's holiday was removed");
223
224
$calendar->edit_holiday({
225
    title => '',
226
    holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{NONE},
227
    start_date => $yesterday,
228
    end_date => $yesterday,
229
    override => 1
230
});
231
is($calendar->is_opened($yesterday), 1, "Yesterdays's holiday was removed with override");
232
233
my $new_open_hours = '08:00';
234
$calendar->edit_holiday({
235
    title => '',
236
    holiday_type=>$Koha::DiscreteCalendar::HOLIDAYS->{NONE},
237
    open_hour=>$new_open_hours,
238
    close_hour=>'',
239
    start_date=>$today,
240
    end_date=>$today
241
});
242
is($calendar->get_date_info($today, $branch1)->{open_hour}, '08:00:00', "Open hour changed to 08:00:00");
243
244
isnt($calendar->get_date_info($today, $branch1)->{close_hour}, '', "Close hour not removed");
245
246
my $delete_range_end = DateTime->today->add(days => 30);
247
$calendar->edit_holiday({
248
    title => '',
249
    holiday_type=>$Koha::DiscreteCalendar::HOLIDAYS->{NONE},
250
    start_date=>$today,
251
    end_date=>$delete_range_end
252
});
253
254
255
is($calendar->is_opened($today), 1, "Today's holiday was already removed");
256
is($calendar->is_opened(DateTime->today->add(days => 1)), 1, "Tomorrow's holiday was removed");
257
is($calendar->is_opened($delete_range_end), 1, "End of range holiday was removed");
258
259
#Check if other branch was affected
260
my @unique_holidays_branch2 = $calendar2->get_unique_holidays();
261
is(scalar @unique_holidays_branch2, 0, "Set of exception holidays at 0 for branch => $branch2");
262
my @repeatable_holidays_branch2 = $calendar2->get_repeatable_holidays();
263
is(scalar @repeatable_holidays_branch2, 0, "Set of repeatable holidays at 0 for branch => $branch2");
264
my @weekly_holidays_branch2 = $calendar2->get_week_days_holidays();
265
is(scalar @weekly_holidays_branch2, 0, "Set of weekly holidays at 0 for branch => $branch2");
266
267
268
#Tests for addDate()
269
270
my $one_day_dur = DateTime::Duration->new( days => 1 );
271
my $negative_one_day_dur = DateTime::Duration->new( days => - 1 );
272
my $two_day_dur = DateTime::Duration->new( days => 2 );
273
my $seven_day_dur = DateTime::Duration->new( days => 7 );
274
275
#Preference useDaysMode Calendar
276
$calendar->{days_mode} = 'Calendar';
277
$unique_holiday->add(days => 1);
278
my $tomorrow = DateTime->today->add(days => 1);
279
$calendar->edit_holiday({
280
    title => "Single holiday Today",
281
    holiday_type=>$Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
282
    start_date=>$tomorrow,
283
    end_date=>$tomorrow
284
});
285
286
is($calendar->addDuration( $today, $one_day_dur, 'days' )->ymd(),
287
    $today->add(days => 2)->ymd(),
288
    'Single day add (Calendar)' );
289
290
is($calendar->addDuration( $today, $two_day_dur, 'days' )->ymd(),
291
    $today->add(days => 2)->ymd(),
292
    'Two days add, skips holiday (Calendar)' );
293
294
cmp_ok($calendar->addDuration( $today, $seven_day_dur, 'days' )->ymd(), 'eq',
295
    $today->add(days => 7)->ymd(),
296
    'Add 7 days (Calendar)' );
297
#Closed Sunday
298
$calendar->edit_holiday({
299
    title => "Weekly",
300
    weekday=>1,
301
    holiday_type=>$Koha::DiscreteCalendar::HOLIDAYS->{WEEKLY},
302
    start_date=>$today,
303
    end_date=>$today
304
});
305
is( $calendar->addDuration( $sunday, $one_day_dur, 'days' )->day_of_week, 1,
306
    'addDate skips closed Sunday (Calendar)' );
307
#to remove the closed sundays
308
$today = DateTime->today;
309
$calendar->edit_holiday({
310
    title => '',
311
    holiday_type=>$Koha::DiscreteCalendar::HOLIDAYS->{NONE},
312
    start_date=>$today,
313
    end_date=>$delete_range_end
314
});
315
316
is( $calendar->addDuration($today, $negative_one_day_dur , 'days')->ymd(),
317
    $today->add(days => - 1)->ymd(),
318
    'Negative call to addDate (Calendar)' );
319
320
#Preference useDaysMode DateDue
321
$calendar->{days_mode} = 'Datedue';
322
#clean everything
323
$today = DateTime->today;
324
$calendar->edit_holiday({
325
    title => '',
326
    holiday_type=>$Koha::DiscreteCalendar::HOLIDAYS->{NONE},
327
    start_date=>$today,
328
    end_date=>$delete_range_end
329
});
330
$calendar->edit_holiday({
331
    title => "Single holiday Today",
332
    holiday_type=>$Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
333
    start_date=>$tomorrow,
334
    end_date=>$tomorrow
335
});
336
337
is($calendar->addDuration( $today, $one_day_dur, 'days' )->ymd(),
338
    $today->add(days => 2)->ymd(),
339
    'Single day add' );
340
341
is($calendar->addDuration( $today, $two_day_dur, 'days' )->ymd(),
342
    $today->add(days => 2)->ymd(),
343
    'Two days add, skips holiday (Datedue)' );
344
345
cmp_ok($calendar->addDuration( $today, $seven_day_dur, 'days' )->ymd(), 'eq',
346
    $today->add(days => 7)->ymd(),
347
    'Add 7 days (Datedue)' );
348
#Closed Sunday
349
$calendar->edit_holiday({
350
    title => "Weekly",
351
    weekday=>1,
352
    holiday_type=> $Koha::DiscreteCalendar::HOLIDAYS->{WEEKLY},
353
    start_date=>$today,
354
    end_date=>$today
355
});
356
is( $calendar->addDuration( $sunday, $one_day_dur, 'days' )->day_of_week, 1,
357
    'addDate skips closed Sunday (Datedue)' );
358
#to remove the closed sundays
359
$today = DateTime->today;
360
$calendar->edit_holiday({
361
    title => '',
362
    holiday_type=>$Koha::DiscreteCalendar::HOLIDAYS->{NONE},
363
    start_date=>$today,
364
    end_date=>$delete_range_end
365
});
366
367
is( $calendar->addDuration($today, $negative_one_day_dur , 'days')->ymd(),
368
    $today->add(days => - 1)->ymd(),
369
    'Negative call to addDate (Datedue)' );
370
371
#Preference useDaysMode Days
372
$calendar->{days_mode} = 'Days';
373
#clean everything
374
$today = DateTime->today;
375
$calendar->edit_holiday({
376
    title => '',
377
    holiday_type=>$Koha::DiscreteCalendar::HOLIDAYS->{NONE},
378
    start_date=>$today,
379
    end_date=>$delete_range_end
380
});
381
$calendar->edit_holiday({
382
    title => "Single holiday Today",
383
    holiday_type=>$Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
384
    start_date=>$tomorrow,
385
    end_date=>$tomorrow
386
});
387
is($calendar->addDuration( $today, $one_day_dur, 'days' )->ymd(),
388
    $today->add(days => 1)->ymd(),
389
    'Single day add' );
390
391
is($calendar->addDuration( $today, $two_day_dur, 'days' )->ymd(),
392
    $today->add(days => 2)->ymd(),
393
    'Two days add, skips holiday (Days)' );
394
395
cmp_ok($calendar->addDuration( $today, $seven_day_dur, 'days' )->ymd(), 'eq',
396
    $today->add(days => 7)->ymd(),
397
    'Add 7 days (Days)' );
398
#Closed Sunday
399
$calendar->edit_holiday({
400
    title => "Weekly",
401
    weekday => 1,
402
    holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{WEEKLY},
403
    start_date => $today,
404
    end_date => $today
405
});
406
is( $calendar->addDuration( $sunday, $one_day_dur, 'days' )->day_of_week, 1,
407
    'addDate skips closed Sunday (Days)' );
408
409
is( $calendar->addDuration($today, $negative_one_day_dur , 'days')->ymd(),
410
    $today->add(days => - 1)->ymd(),
411
    'Negative call to addDate (Days)' );
412
413
#Days_forward
414
415
#clean the range
416
$today = DateTime->today;
417
$calendar->edit_holiday({
418
    title => '',
419
    holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{NONE},
420
    start_date => $today,
421
    end_date => DateTime->today->add(days => 20)
422
});
423
my $forwarded_dt = $calendar->days_forward($today, 10);
424
425
my $expected = $today->clone;
426
$expected->add(days => 10);
427
is($forwarded_dt->ymd, $expected->ymd, 'With no holiday on the perioddays_forward should add 10 days');
428
429
#Added a holiday
430
$unique_holiday = DateTime->today->add(days => 15);
431
$calendar->edit_holiday({
432
    title => "Single holiday Today",
433
    holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
434
    start_date => $unique_holiday,
435
    end_date => $unique_holiday
436
});
437
$forwarded_dt = $calendar->days_forward($today, 20);
438
439
$expected->add(days => 11);
440
is($forwarded_dt->ymd, $expected->ymd, 'With holiday on the perioddays_forward should add 20 days + 1 day for holiday');
441
442
$forwarded_dt = $calendar->days_forward($today, 0);
443
is($forwarded_dt->ymd, $today->ymd, '0 day should return start dt');
444
445
$forwarded_dt = $calendar->days_forward($today, -2);
446
is($forwarded_dt->ymd, $today->ymd, 'negative day should return start dt');
447
448
#copying a branch to an other
449
is($calendar2->is_opened($tomorrow), 1, "$branch2 opened tomorrow");
450
#Added a goliday tomorrow for branch1
451
$calendar->edit_holiday({
452
    title => "Single holiday Today",
453
    holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
454
    start_date => $tomorrow,
455
    end_date => $tomorrow
456
});
457
#Copy dates from branch1 to branch2
458
$calendar->copy_to_branch($branch2);
459
#Check if branch2 is also closed tomorrow after copying from branch1
460
is($calendar2->is_opened($tomorrow), 0, "$branch2 close tomorrow after copying from $branch1");
461
462
$schema->storage->txn_rollback;
463
464
1;
(-)a/t/db_dependent/Hold.t (-3 / +11 lines)
Lines 22-28 use C4::Context; Link Here
22
use C4::Biblio qw( AddBiblio );
22
use C4::Biblio qw( AddBiblio );
23
use Koha::Database;
23
use Koha::Database;
24
use Koha::Libraries;
24
use Koha::Libraries;
25
use C4::Calendar qw( new insert_single_holiday );
25
use Koha::DiscreteCalendar;
26
use Koha::Patrons;
26
use Koha::Patrons;
27
use Koha::Holds;
27
use Koha::Holds;
28
use Koha::Item;
28
use Koha::Item;
Lines 77-84 my $hold = Koha::Hold->new( Link Here
77
);
77
);
78
$hold->store();
78
$hold->store();
79
79
80
my $b1_cal = C4::Calendar->new( branchcode => $branches[1]->{branchcode} );
80
my $b1_cal = Koha::DiscreteCalendar->new({ branchcode => $branches[1]->{branchcode} });
81
$b1_cal->insert_single_holiday( day => 2, month => 1, year => 2017, title => "Morty Day", description => "Rick" ); #Add a holiday
81
my $holiday = dt_from_string('2017-01-02', 'iso');
82
$b1_cal->edit_holiday({
83
    holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
84
    override     => 1,
85
    start_date   => $holiday,
86
    end_date     => $holiday,
87
    title => "Morty Day",
88
    description => "Rick",
89
}); #Add a holiday
82
my $today = dt_from_string;
90
my $today = dt_from_string;
83
is( $hold->age(), $today->delta_days( dt_from_string( '2017-01-01' ) )->in_units( 'days')  , "Age of hold is days from reservedate to now if calendar ignored");
91
is( $hold->age(), $today->delta_days( dt_from_string( '2017-01-01' ) )->in_units( 'days')  , "Age of hold is days from reservedate to now if calendar ignored");
84
is( $hold->age(1), $today->delta_days( dt_from_string( '2017-01-01' ) )->in_units( 'days' ) - 1 , "Age of hold is days from reservedate to now minus 1 if calendar used");
92
is( $hold->age(1), $today->delta_days( dt_from_string( '2017-01-01' ) )->in_units( 'days' ) - 1 , "Age of hold is days from reservedate to now minus 1 if calendar used");
(-)a/t/db_dependent/Holds.t (-1 lines)
Lines 13-19 use Test::Exception; Link Here
13
use MARC::Record;
13
use MARC::Record;
14
14
15
use C4::Biblio;
15
use C4::Biblio;
16
use C4::Calendar;
17
use C4::Items;
16
use C4::Items;
18
use C4::Reserves qw( AddReserve CalculatePriority ModReserve ToggleSuspend AutoUnsuspendReserves SuspendAll ModReserveMinusPriority AlterPriority CanItemBeReserved CheckReserves );
17
use C4::Reserves qw( AddReserve CalculatePriority ModReserve ToggleSuspend AutoUnsuspendReserves SuspendAll ModReserveMinusPriority AlterPriority CanItemBeReserved CheckReserves );
19
use C4::Circulation qw( CanBookBeRenewed );
18
use C4::Circulation qw( CanBookBeRenewed );
(-)a/t/db_dependent/Holds/WaitingReserves.t (-22 / +13 lines)
Lines 16-23 my $schema = Koha::Database->new->schema; Link Here
16
$schema->storage->txn_begin;
16
$schema->storage->txn_begin;
17
17
18
my $dbh = C4::Context->dbh;
18
my $dbh = C4::Context->dbh;
19
$dbh->do(q{DELETE FROM special_holidays});
19
$dbh->do(q{DELETE FROM discrete_calendar});
20
$dbh->do(q{DELETE FROM repeatable_holidays});
21
$dbh->do("DELETE FROM reserves");
20
$dbh->do("DELETE FROM reserves");
22
21
23
my $builder = t::lib::TestBuilder->new();
22
my $builder = t::lib::TestBuilder->new();
Lines 36-41 $builder->build({ Link Here
36
    },
35
    },
37
});
36
});
38
37
38
my $calendar = Koha::DiscreteCalendar->new({ branchcode => 'LIB1' });
39
39
$builder->build({
40
$builder->build({
40
    source => 'Branch',
41
    source => 'Branch',
41
    value  => {
42
    value  => {
Lines 138-168 my $reserve3 = $builder->build({ Link Here
138
my $special_holiday1_dt = $today->clone;
139
my $special_holiday1_dt = $today->clone;
139
$special_holiday1_dt->add(days => 2);
140
$special_holiday1_dt->add(days => 2);
140
141
141
my $holiday = $builder->build({
142
$calendar->edit_holiday({
142
    source => 'SpecialHoliday',
143
    title => "My special holiday",
143
    value => {
144
    holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
144
        branchcode => 'LIB1',
145
    start_date => $special_holiday1_dt,
145
        day => $special_holiday1_dt->day,
146
    end_date => $special_holiday1_dt,
146
        month => $special_holiday1_dt->month,
147
        year => $special_holiday1_dt->year,
148
        title => 'My special holiday',
149
        isexception => 0
150
    },
151
});
147
});
152
148
153
my $special_holiday2_dt = $today->clone;
149
my $special_holiday2_dt = $today->clone;
154
$special_holiday2_dt->add(days => 4);
150
$special_holiday2_dt->add(days => 4);
155
151
156
my $holiday2 = $builder->build({
152
$calendar->edit_holiday({
157
    source => 'SpecialHoliday',
153
    title => "My special holiday 2",
158
    value => {
154
    holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
159
        branchcode => 'LIB1',
155
    start_date => $special_holiday2_dt,
160
        day => $special_holiday2_dt->day,
156
    end_date => $special_holiday2_dt,
161
        month => $special_holiday2_dt->month,
162
        year => $special_holiday2_dt->year,
163
        title => 'My special holiday 2',
164
        isexception => 0
165
    },
166
});
157
});
167
158
168
Koha::Caches->get_instance->flush_all;
159
Koha::Caches->get_instance->flush_all;
(-)a/t/db_dependent/HoldsQueue.t (-7 / +8 lines)
Lines 11-17 use Modern::Perl; Link Here
11
use Test::More tests => 61;
11
use Test::More tests => 61;
12
use Data::Dumper;
12
use Data::Dumper;
13
13
14
use C4::Calendar qw( new insert_single_holiday );
14
use Koha::DiscreteCalendar;
15
use C4::Context;
15
use C4::Context;
16
use C4::Members;
16
use C4::Members;
17
use C4::Circulation qw( AddIssue AddReturn );
17
use C4::Circulation qw( AddIssue AddReturn );
Lines 327-342 is( $holds_queue->[1]->{cardnumber}, $borrower2->{cardnumber}, "Holds queue fill Link Here
327
# have 1 row in the holds queue
327
# have 1 row in the holds queue
328
t::lib::Mocks::mock_preference('HoldsQueueSkipClosed', 1);
328
t::lib::Mocks::mock_preference('HoldsQueueSkipClosed', 1);
329
my $today = dt_from_string();
329
my $today = dt_from_string();
330
C4::Calendar->new( branchcode => $branchcodes[0] )->insert_single_holiday(
330
my $calendar = Koha::DiscreteCalendar->new({ branchcode => $branchcodes[0] });
331
    day         => $today->day(),
331
$calendar->edit_holiday({
332
    month       => $today->month(),
332
    holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
333
    year        => $today->year(),
333
    start_date   => $today,
334
    end_date     => $today,
334
    title       => "$today",
335
    title       => "$today",
335
    description => "$today",
336
    description => "$today",
336
);
337
});
337
# If the test below is removed, aother tests using the holiday will fail. For some reason if we call is_holiday now
338
# If the test below is removed, aother tests using the holiday will fail. For some reason if we call is_holiday now
338
# the holiday will get set in cache correctly, but not if we let C4::HoldsQueue call is_holiday instead.
339
# the holiday will get set in cache correctly, but not if we let C4::HoldsQueue call is_holiday instead.
339
is( Koha::Calendar->new( branchcode => $branchcodes[0] )->is_holiday( $today ), 1, 'Is today a holiday for pickup branch' );
340
is( $calendar->is_holiday( $today ), 1, 'Is today a holiday for pickup branch' );
340
C4::HoldsQueue::CreateQueue();
341
C4::HoldsQueue::CreateQueue();
341
$holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
342
$holds_queue = $dbh->selectall_arrayref("SELECT * FROM tmp_holdsqueue", { Slice => {} });
342
is( scalar( @$holds_queue ), 1, "Holds not filled with items from closed libraries" );
343
is( scalar( @$holds_queue ), 1, "Holds not filled with items from closed libraries" );
(-)a/t/db_dependent/Holidays.t (-281 lines)
Lines 1-281 Link Here
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 => 12;
21
22
use DateTime;
23
use DateTime::TimeZone;
24
25
use t::lib::TestBuilder;
26
use C4::Context;
27
use Koha::Database;
28
use Koha::DateUtils qw( dt_from_string );
29
30
BEGIN {
31
    use_ok('Koha::Calendar');
32
    use_ok('C4::Calendar', qw( insert_exception_holiday insert_week_day_holiday insert_day_month_holiday insert_single_holiday copy_to_branch get_exception_holidays isHoliday ));
33
}
34
35
my $schema = Koha::Database->new->schema;
36
my $dbh = C4::Context->dbh;
37
my $builder = t::lib::TestBuilder->new;
38
39
subtest 'is_holiday timezone tests' => sub {
40
41
    plan tests => 1;
42
43
    $schema->storage->txn_begin;
44
45
    $dbh->do("DELETE FROM special_holidays");
46
    # Clear cache
47
    Koha::Caches->get_instance->flush_all;
48
49
    # Artificially set timezone
50
    my $timezone = 'America/Santiago';
51
    $ENV{TZ} = $timezone;
52
    use POSIX qw(tzset);
53
    tzset;
54
55
    my $branch = $builder->build( { source => 'Branch' } )->{branchcode};
56
    my $calendar = Koha::Calendar->new( branchcode => $branch );
57
58
    C4::Calendar->new( branchcode => $branch )->insert_exception_holiday(
59
        day         => 6,
60
        month       => 9,
61
        year        => 2015,
62
        title       => 'Invalid date',
63
        description => 'Invalid date description',
64
    );
65
66
    my $exception_holiday = DateTime->new( day => 6, month => 9, year => 2015 );
67
    my $now_dt            = DateTime->now;
68
69
    my $diff;
70
    eval { $diff = $calendar->days_between( $now_dt, $exception_holiday ) };
71
    unlike(
72
        $@,
73
        qr/Invalid local time for date in time zone: America\/Santiago/,
74
        'Avoid invalid datetime due to DST'
75
    );
76
77
    $schema->storage->txn_rollback;
78
};
79
80
$schema->storage->txn_begin;
81
82
# Create two fresh branches for the tests
83
my $branch_1 = $builder->build({ source => 'Branch' })->{ branchcode };
84
my $branch_2 = $builder->build({ source => 'Branch' })->{ branchcode };
85
86
C4::Calendar->new( branchcode => $branch_1 )->insert_week_day_holiday(
87
    weekday     => 0,
88
    title       => '',
89
    description => 'Sundays',
90
);
91
92
my $holiday2add = dt_from_string("2015-01-01");
93
C4::Calendar->new( branchcode => $branch_1 )->insert_day_month_holiday(
94
    day         => $holiday2add->day(),
95
    month       => $holiday2add->month(),
96
    year        => $holiday2add->year(),
97
    title       => '',
98
    description => "New Year's Day",
99
);
100
$holiday2add = dt_from_string("2014-12-25");
101
C4::Calendar->new( branchcode => $branch_1 )->insert_day_month_holiday(
102
    day         => $holiday2add->day(),
103
    month       => $holiday2add->month(),
104
    year        => $holiday2add->year(),
105
    title       => '',
106
    description => 'Christmas',
107
);
108
109
my $koha_calendar = Koha::Calendar->new( branchcode => $branch_1 );
110
my $c4_calendar = C4::Calendar->new( branchcode => $branch_1 );
111
112
isa_ok( $koha_calendar, 'Koha::Calendar', 'Koha::Calendar class returned' );
113
isa_ok( $c4_calendar,   'C4::Calendar',   'C4::Calendar class returned' );
114
115
my $sunday = DateTime->new(
116
    year  => 2011,
117
    month => 6,
118
    day   => 26,
119
);
120
my $monday = DateTime->new(
121
    year  => 2011,
122
    month => 6,
123
    day   => 27,
124
);
125
my $christmas = DateTime->new(
126
    year  => 2032,
127
    month => 12,
128
    day   => 25,
129
);
130
my $newyear = DateTime->new(
131
    year  => 2035,
132
    month => 1,
133
    day   => 1,
134
);
135
136
is( $koha_calendar->is_holiday($sunday),    1, 'Sunday is a closed day' );
137
is( $koha_calendar->is_holiday($monday),    0, 'Monday is not a closed day' );
138
is( $koha_calendar->is_holiday($christmas), 1, 'Christmas is a closed day' );
139
is( $koha_calendar->is_holiday($newyear),   1, 'New Years day is a closed day' );
140
141
$dbh->do("DELETE FROM repeatable_holidays");
142
$dbh->do("DELETE FROM special_holidays");
143
144
my $custom_holiday = DateTime->new(
145
    year  => 2013,
146
    month => 11,
147
    day   => 12,
148
);
149
150
my $today = dt_from_string();
151
C4::Calendar->new( branchcode => $branch_2 )->insert_single_holiday(
152
    day         => $today->day(),
153
    month       => $today->month(),
154
    year        => $today->year(),
155
    title       => "$today",
156
    description => "$today",
157
);
158
159
is( Koha::Calendar->new( branchcode => $branch_2 )->is_holiday( $today ), 1, "Today is a holiday for $branch_2" );
160
is( Koha::Calendar->new( branchcode => $branch_1 )->is_holiday( $today ), 0, "Today is not a holiday for $branch_1");
161
162
$schema->storage->txn_rollback;
163
164
subtest 'copy_to_branch' => sub {
165
166
    plan tests => 8;
167
168
    $schema->storage->txn_begin;
169
170
    my $branch1 = $builder->build( { source => 'Branch' } )->{ branchcode };
171
    my $calendar1 = C4::Calendar->new( branchcode => $branch1 );
172
    my $sunday = dt_from_string("2020-03-15");
173
    $calendar1->insert_week_day_holiday(
174
        weekday     => 0,
175
        title       => '',
176
        description => 'Sundays',
177
    );
178
179
    my $day_month = dt_from_string("2020-03-17");
180
    $calendar1->insert_day_month_holiday(
181
        day         => $day_month->day(),
182
        month       => $day_month->month(),
183
        year        => $day_month->year(),
184
        title       => '',
185
        description => "",
186
    );
187
188
    my $future_date = dt_from_string("9999-12-31");
189
    $calendar1->insert_single_holiday(
190
        day         => $future_date->day(),
191
        month       => $future_date->month(),
192
        year        => $future_date->year(),
193
        title       => "",
194
        description => "",
195
    );
196
197
    my $future_exception = dt_from_string("9999-12-30");
198
    $calendar1->insert_exception_holiday(
199
        day         => $future_exception->day(),
200
        month       => $future_exception->month(),
201
        year        => $future_exception->year(),
202
        title       => "",
203
        description => "",
204
    );
205
206
    my $past_date = dt_from_string("2019-11-20");
207
    $calendar1->insert_single_holiday(
208
        day         => $past_date->day(),
209
        month       => $past_date->month(),
210
        year        => $past_date->year(),
211
        title       => "",
212
        description => "",
213
    );
214
215
    my $past_exception = dt_from_string("2020-03-09");
216
    $calendar1->insert_exception_holiday(
217
        day         => $past_exception->day(),
218
        month       => $past_exception->month(),
219
        year        => $past_exception->year(),
220
        title       => "",
221
        description => "",
222
    );
223
224
    my $branch2 = $builder->build( { source => 'Branch' } )->{branchcode};
225
226
    C4::Calendar->new( branchcode => $branch1 )->copy_to_branch( $branch2 );
227
228
    my $calendar2 = C4::Calendar->new( branchcode => $branch2 );
229
    my $exceptions = $calendar2->get_exception_holidays;
230
231
    is( $calendar2->isHoliday( $sunday->day, $sunday->month, $sunday->year ), 1, "Weekday holiday copied to branch 2" );
232
    is( $calendar2->isHoliday( $day_month->day, $day_month->month, $day_month->year ), 1, "Day/month holiday copied to branch 2" );
233
    is( $calendar2->isHoliday( $future_date->day, $future_date->month, $future_date->year ), 1, "Single holiday copied to branch 2" );
234
    is( ( grep { $_->{date} eq "9999-12-30"} values %$exceptions ), 1, "Exception holiday copied to branch 2" );
235
    is( $calendar2->isHoliday( $past_date->day, $past_date->month, $past_date->year ), 0, "Don't copy past single holidays" );
236
    is( ( grep { $_->{date} eq "2020-03-09"} values %$exceptions ), 0, "Don't copy past exception holidays " );
237
238
    C4::Calendar->new( branchcode => $branch1 )->copy_to_branch( $branch2 );
239
240
    #Select all rows with same values from database
241
    my $dbh = C4::Context->dbh;
242
    my $get_repeatable_holidays = "SELECT a.* FROM repeatable_holidays a
243
        JOIN (SELECT branchcode, weekday, day, month, COUNT(*)
244
        FROM repeatable_holidays
245
        GROUP BY branchcode, weekday, day, month HAVING count(*) > 1) b
246
        ON a.branchcode = b.branchcode
247
        AND ( a.weekday = b.weekday OR (a.day = b.day AND a.month = b.month))
248
        ORDER BY a.branchcode;";
249
    my $sth  = $dbh->prepare($get_repeatable_holidays);
250
    $sth->execute;
251
252
    my @repeatable_holidays;
253
    while(my $row = $sth->fetchrow_hashref){
254
        push @repeatable_holidays, $row
255
    }
256
257
    is( scalar(@repeatable_holidays), 0, "None of the repeatable holidays were doubled");
258
259
    my $get_special_holidays = "SELECT a.* FROM special_holidays a
260
    JOIN (SELECT branchcode, day, month, year, isexception, COUNT(*)
261
    FROM special_holidays
262
    GROUP BY branchcode, day, month, year, isexception HAVING count(*) > 1) b
263
    ON a.branchcode = b.branchcode
264
    AND a.day = b.day AND a.month = b.month AND a.year = b.year AND a.isexception = b.isexception
265
    ORDER BY a.branchcode;";
266
    $sth  = $dbh->prepare($get_special_holidays);
267
    $sth->execute;
268
269
    my @special_holidays;
270
    while(my $row = $sth->fetchrow_hashref){
271
        push @special_holidays, $row
272
    }
273
274
    is( scalar(@special_holidays), 0, "None of the special holidays were doubled");
275
276
    $schema->storage->txn_rollback;
277
278
};
279
280
# Clear cache
281
Koha::Caches->get_instance->flush_all;
(-)a/t/db_dependent/Koha/Charges/Fees.t (-17 / +29 lines)
Lines 28-34 use t::lib::TestBuilder; Link Here
28
use t::lib::Dates;
28
use t::lib::Dates;
29
29
30
use Time::Fake;
30
use Time::Fake;
31
use C4::Calendar qw( new insert_week_day_holiday delete_holiday );
31
use Koha::DiscreteCalendar;
32
use Koha::DateUtils qw(dt_from_string);
32
use Koha::DateUtils qw(dt_from_string);
33
33
34
BEGIN {
34
BEGIN {
Lines 357-382 subtest 'accumulate_rentalcharge tests' => sub { Link Here
357
    );
357
    );
358
    $itemtype->rentalcharge_daily_calendar(1)->store();
358
    $itemtype->rentalcharge_daily_calendar(1)->store();
359
359
360
    my $calendar = C4::Calendar->new( branchcode => $library->id );
360
    my $calendar = Koha::DiscreteCalendar->new({ branchcode => $library->id });
361
    # DateTime 1..7 (Mon..Sun), C4::Calender 0..6 (Sun..Sat)
361
    # DateTime 1..7 (Mon..Sun), Koha::DiscreteCalendar 1..7 (Sun..Sat)
362
    my $closed_day =
362
    my $closed_day =
363
        ( $dt_from->day_of_week == 6 ) ? 0
363
        ( $dt_from->day_of_week == 7 ) ? 1
364
      : ( $dt_from->day_of_week == 7 ) ? 1
364
      : $dt_from->day_of_week + 1;
365
      :                                  $dt_from->day_of_week + 1;
365
    $calendar->edit_holiday({
366
    $calendar->insert_week_day_holiday(
366
        holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{WEEKLY},
367
        weekday     => $closed_day,
367
        weekday      => $closed_day,
368
        override     => 1,
369
        start_date   => $dt_from,
370
        end_date     => $dt_from,
368
        title       => 'Test holiday',
371
        title       => 'Test holiday',
369
        description => 'Test holiday'
372
        description => 'Test holiday'
370
    );
373
    });
371
    $charge = $fees->accumulate_rentalcharge();
374
    $charge = $fees->accumulate_rentalcharge();
372
    my $day_names = {
375
    my $day_names = {
373
        0 => 'Sunday',
376
        1 => 'Sunday',
374
        1 => 'Monday',
377
        2 => 'Monday',
375
        2 => 'Tuesday',
378
        3 => 'Tuesday',
376
        3 => 'Wednesday',
379
        4 => 'Wednesday',
377
        4 => 'Thursday',
380
        5 => 'Thursday',
378
        5 => 'Friday',
381
        6 => 'Friday',
379
        6 => 'Saturday'
382
        7 => 'Saturday'
380
    };
383
    };
381
    my $dayname = $day_names->{$closed_day};
384
    my $dayname = $day_names->{$closed_day};
382
    is( $charge, 5.00,
385
    is( $charge, 5.00,
Lines 426-432 subtest 'accumulate_rentalcharge tests' => sub { Link Here
426
    );
429
    );
427
430
428
    $itemtype->rentalcharge_hourly_calendar(1)->store();
431
    $itemtype->rentalcharge_hourly_calendar(1)->store();
429
    $calendar->delete_holiday( weekday => $closed_day );
432
    $calendar->edit_holiday({
433
        holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{NONE},
434
        weekday      => $closed_day,
435
        override     => 1,
436
        delete_type  => 1,
437
        start_date   => $dt_from,
438
        end_date     => $dt_from,
439
        title        => '',
440
        description  => '',
441
    });
430
    $charge = $fees->accumulate_rentalcharge();
442
    $charge = $fees->accumulate_rentalcharge();
431
    is( $charge, 24.00, 'Hourly rental charge calculated correctly with finesCalendar = noFinesWhenClosed (96h - 0h * 0.25u)' );
443
    is( $charge, 24.00, 'Hourly rental charge calculated correctly with finesCalendar = noFinesWhenClosed (96h - 0h * 0.25u)' );
432
};
444
};
(-)a/t/db_dependent/Koha/CurbsidePickups.t (-7 / +17 lines)
Lines 20-29 use Modern::Perl; Link Here
20
use Test::More tests => 4;
20
use Test::More tests => 4;
21
use Test::Exception;
21
use Test::Exception;
22
22
23
use C4::Calendar;
24
use Koha::CurbsidePickups;
23
use Koha::CurbsidePickups;
25
use Koha::CurbsidePickupPolicies;
24
use Koha::CurbsidePickupPolicies;
26
use Koha::Calendar;
25
use Koha::DiscreteCalendar;
27
use Koha::Database;
26
use Koha::Database;
28
use Koha::DateUtils qw( dt_from_string );
27
use Koha::DateUtils qw( dt_from_string );
29
28
Lines 154-172 subtest 'Create a pickup' => sub { Link Here
154
153
155
    # Day is a holiday
154
    # Day is a holiday
156
    Koha::Caches->get_instance->flush_all;
155
    Koha::Caches->get_instance->flush_all;
157
    C4::Calendar->new( branchcode => $library->branchcode )->insert_week_day_holiday(
156
    my $calendar = Koha::DiscreteCalendar->new({ branchcode => $library->branchcode });
158
        weekday     => 1,
157
    $calendar->edit_holiday({
158
        holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{WEEKLY},
159
        weekday      => 2,
160
        start_date   => $schedule_dt,
161
        end_date     => $schedule_dt,
159
        title       => '',
162
        title       => '',
160
        description => 'Mondays',
163
        description => 'Mondays',
161
    );
164
    });
162
    my $calendar = Koha::Calendar->new( branchcode => $library->branchcode );
163
    throws_ok {
165
    throws_ok {
164
        Koha::CurbsidePickup->new({%$params, scheduled_pickup_datetime => $schedule_dt})->store;
166
        Koha::CurbsidePickup->new({%$params, scheduled_pickup_datetime => $schedule_dt})->store;
165
    }
167
    }
166
    'Koha::Exceptions::CurbsidePickup::LibraryIsClosed',
168
    'Koha::Exceptions::CurbsidePickup::LibraryIsClosed',
167
      'Cannot create a pickup on a holiday';
169
      'Cannot create a pickup on a holiday';
168
170
169
    C4::Context->dbh->do(q{DELETE FROM repeatable_holidays});
171
    $calendar->edit_holiday({
172
        holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{NONE},
173
        weekday      => 2,
174
        delete_type  => 1,
175
        start_date   => $schedule_dt,
176
        end_date     => $schedule_dt,
177
        description  => '',
178
    });
179
170
    Koha::Caches->get_instance->flush_all;
180
    Koha::Caches->get_instance->flush_all;
171
};
181
};
172
182
(-)a/t/db_dependent/Koha/ItemTypes.t (-1 lines)
Lines 25-31 use Test::More tests => 14; Link Here
25
use t::lib::Mocks;
25
use t::lib::Mocks;
26
use t::lib::TestBuilder;
26
use t::lib::TestBuilder;
27
27
28
use C4::Calendar qw( new );
29
use Koha::Biblioitems;
28
use Koha::Biblioitems;
30
use Koha::Libraries;
29
use Koha::Libraries;
31
use Koha::Database;
30
use Koha::Database;
(-)a/t/db_dependent/Reserves/CancelExpiredReserves.t (-10 / +5 lines)
Lines 81-96 subtest 'CancelExpiredReserves tests incl. holidays' => sub { Link Here
81
    });
81
    });
82
82
83
    Koha::Caches->get_instance()->flush_all();
83
    Koha::Caches->get_instance()->flush_all();
84
    my $holiday = $builder->build({
84
    my $calendar = Koha::DiscreteCalendar->new({ branchcode => 'LIB1' })->edit_holiday({
85
        source => 'SpecialHoliday',
85
        title => "My holiday",
86
        value => {
86
        holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
87
            branchcode => 'LIB1',
87
        start_date => $today,
88
            day => $today->day,
88
        end_date => $today,
89
            month => $today->month,
90
            year => $today->year,
91
            title => 'My holiday',
92
            isexception => 0
93
        },
94
    });
89
    });
95
90
96
    CancelExpiredReserves();
91
    CancelExpiredReserves();
(-)a/t/lib/TestBuilder.pm (-1 / +6 lines)
Lines 467-472 sub _gen_type { Link Here
467
        decimal          => \&_gen_real,
467
        decimal          => \&_gen_real,
468
        double_precision => \&_gen_real,
468
        double_precision => \&_gen_real,
469
469
470
        time      => \&_gen_time,
470
        timestamp => \&_gen_datetime,
471
        timestamp => \&_gen_datetime,
471
        datetime  => \&_gen_datetime,
472
        datetime  => \&_gen_datetime,
472
        date      => \&_gen_date,
473
        date      => \&_gen_date,
Lines 536-541 sub _gen_datetime { Link Here
536
    return $self->schema->storage->datetime_parser->format_datetime(dt_from_string);
537
    return $self->schema->storage->datetime_parser->format_datetime(dt_from_string);
537
}
538
}
538
539
540
sub _gen_time {
541
    my ($self, $params) = @_;
542
    return $self->schema->storage->datetime_parser->format_time(dt_from_string);
543
}
544
539
sub _gen_text {
545
sub _gen_text {
540
    my ($self, $params) = @_;
546
    my ($self, $params) = @_;
541
    # From perldoc String::Random
547
    # From perldoc String::Random
542
- 

Return to bug 17015