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

(-)a/C4/Reserves.pm (-3 lines)
Lines 35-44 use C4::Members; Link Here
35
use Koha::Account::Lines;
35
use Koha::Account::Lines;
36
use Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue;
36
use Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue;
37
use Koha::Biblios;
37
use Koha::Biblios;
38
<<<<<<< HEAD
39
use Koha::Cache::Memory::Lite;
38
use Koha::Cache::Memory::Lite;
40
=======
41
>>>>>>> Bug 17015: DiscreteCalendar UI, Back-End and necessary scripts
42
use Koha::CirculationRules;
39
use Koha::CirculationRules;
43
use Koha::Database;
40
use Koha::Database;
44
use Koha::DateUtils qw( dt_from_string output_pref );
41
use Koha::DateUtils qw( dt_from_string output_pref );
(-)a/t/db_dependent/Calendar.t (-717 lines)
Lines 1-717 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 DateTime::Duration;
28
use Koha::Caches;
29
use Koha::Calendar;
30
use Koha::Database;
31
use Koha::DateUtils qw( dt_from_string );
32
33
my $schema  = Koha::Database->new->schema;
34
my $builder = t::lib::TestBuilder->new;
35
$schema->storage->txn_begin;
36
37
my $today      = dt_from_string();
38
my $holiday_dt = $today->clone;
39
$holiday_dt->add( days => 3 );
40
41
Koha::Caches->get_instance()->flush_all();
42
43
subtest 'Original tests from t' => sub {
44
45
    # We need to mock the C4::Context->preference method for
46
    # simplicity and re-usability of the session definition. Any
47
    # syspref fits for syspref-agnostic tests.
48
    my $module_context = Test::MockModule->new('C4::Context');
49
    $module_context->mock(
50
        'preference',
51
        sub {
52
            return 'Calendar';
53
        }
54
    );
55
56
    my $mpl = $builder->build_object( { class => 'Koha::Libraries' } )->branchcode;
57
    my $cpl = $builder->build_object( { class => 'Koha::Libraries' } )->branchcode;
58
    my $rows = [    # add weekly holidays
59
        { branchcode => $mpl, weekday => 0 },                  # sundays
60
        { branchcode => $mpl, weekday => 6 },                  # saturdays
61
        { branchcode => $mpl, day     => 1, month => 1 },      # new year's day
62
        { branchcode => $mpl, day     => 25, month => 12 },    # chrismas
63
    ];
64
    $schema->resultset('RepeatableHoliday')->delete_all;
65
    $schema->resultset('RepeatableHoliday')->create( { %$_, description => q{} } ) for @$rows;
66
67
    $rows = [                                                  # exception holidays
68
        { branchcode => $mpl, day => 11, month => 11, year => 2012, isexception => 1 },    # sunday exception
69
        { branchcode => $mpl, day => 1,  month => 6,  year => 2011, isexception => 0 },
70
        { branchcode => $mpl, day => 4,  month => 7,  year => 2012, isexception => 0 },
71
        { branchcode => $cpl, day => 6,  month => 8,  year => 2012, isexception => 0 },
72
        { branchcode => $mpl, day => 7,  month => 7,  year => 2012, isexception => 1 },    # holiday exception
73
        { branchcode => $mpl, day => 7,  month => 7,  year => 2012, isexception => 0 },    # holiday
74
    ];
75
    $schema->resultset('SpecialHoliday')->delete_all;
76
    $schema->resultset('SpecialHoliday')->create( { %$_, description => q{} } ) for @$rows;
77
78
    my $cache = Koha::Caches->get_instance();
79
    $cache->clear_from_cache( $mpl . '_holidays' );
80
    $cache->clear_from_cache( $cpl . '_holidays' );
81
82
    # $mpl branch is arbitrary, is not used at all but is needed for initialization
83
    my $cal = Koha::Calendar->new( branchcode => $mpl );
84
85
    isa_ok( $cal, 'Koha::Calendar', 'Calendar class returned' );
86
87
    my $saturday = DateTime->new(
88
        year  => 2012,
89
        month => 11,
90
        day   => 24,
91
    );
92
93
    my $sunday = DateTime->new(
94
        year  => 2012,
95
        month => 11,
96
        day   => 25,
97
    );
98
99
    my $monday = DateTime->new(
100
        year  => 2012,
101
        month => 11,
102
        day   => 26,
103
    );
104
105
    my $new_year = DateTime->new(
106
        year  => 2013,
107
        month => 1,
108
        day   => 1,
109
    );
110
111
    my $single_holiday = DateTime->new(
112
        year  => 2011,
113
        month => 6,
114
        day   => 1,
115
    );    # should be a holiday
116
117
    my $notspecial = DateTime->new(
118
        year  => 2011,
119
        month => 6,
120
        day   => 2
121
    );    # should NOT be a holiday
122
123
    my $sunday_exception = DateTime->new(
124
        year  => 2012,
125
        month => 11,
126
        day   => 11
127
    );
128
129
    my $day_after_christmas = DateTime->new(
130
        year  => 2012,
131
        month => 12,
132
        day   => 26
133
    );    # for testing negative addDuration
134
135
    my $holiday_for_another_branch = DateTime->new(
136
        year  => 2012,
137
        month => 8,
138
        day   => 6,      # This is a monday
139
    );
140
141
    my $holiday_excepted = DateTime->new(
142
        year  => 2012,
143
        month => 7,
144
        day   => 7,      # Both a holiday and exception
145
    );
146
147
    {                    # Syspref-agnostic tests
148
        is( $saturday->day_of_week,              6, '\'$saturday\' is actually a saturday (6th day of week)' );
149
        is( $sunday->day_of_week,                7, '\'$sunday\' is actually a sunday (7th day of week)' );
150
        is( $monday->day_of_week,                1, '\'$monday\' is actually a monday (1st day of week)' );
151
        is( $cal->is_holiday($saturday),         1, 'Saturday is a closed day' );
152
        is( $cal->is_holiday($sunday),           1, 'Sunday is a closed day' );
153
        is( $cal->is_holiday($monday),           0, 'Monday is not a closed day' );
154
        is( $cal->is_holiday($new_year),         1, 'Month/Day closed day test (New year\'s day)' );
155
        is( $cal->is_holiday($single_holiday),   1, 'Single holiday closed day test' );
156
        is( $cal->is_holiday($notspecial),       0, 'Fixed single date that is not a holiday test' );
157
        is( $cal->is_holiday($sunday_exception), 0, 'Exception holiday is not a closed day test' );
158
        is(
159
            $cal->is_holiday($holiday_for_another_branch), 0,
160
            'Holiday defined for another branch should not be defined as an holiday'
161
        );
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
        is( $cal->is_holiday($later_dt), 0, 'bz-8966 (1/2) Apply is_holiday for the next test' );
176
        cmp_ok( $later_dt, 'eq', '2012-09-17T17:30:00', 'bz-8966 (2/2) Date should be the same after is_holiday' );
177
    }
178
179
    {    # Bugzilla #8800 - is_holiday should use truncated date for 'contains' call
180
        my $single_holiday_time = DateTime->new(
181
            year   => 2011,
182
            month  => 6,
183
            day    => 1,
184
            hour   => 11,
185
            minute => 2
186
        );
187
188
        is(
189
            $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
195
    my $one_day_dur   = DateTime::Duration->new( days => 1 );
196
    my $two_day_dur   = DateTime::Duration->new( days => 2 );
197
    my $seven_day_dur = DateTime::Duration->new( days => 7 );
198
199
    my $dt = dt_from_string( '2012-07-03', 'iso' );    #tuesday
200
201
    my $test_dt = DateTime->new(                       # Monday
202
        year   => 2012,
203
        month  => 7,
204
        day    => 23,
205
        hour   => 11,
206
        minute => 53,
207
    );
208
209
    my $later_dt = DateTime->new(                      # Monday
210
        year      => 2012,
211
        month     => 9,
212
        day       => 17,
213
        hour      => 17,
214
        minute    => 30,
215
        time_zone => 'Europe/London',
216
    );
217
218
    {                                                  ## 'Datedue' tests
219
220
        $cal = Koha::Calendar->new( branchcode => $mpl, days_mode => 'Datedue' );
221
222
        is(
223
            $cal->addDuration( $dt, $one_day_dur, 'days' ),    # tuesday
224
            dt_from_string( '2012-07-05', 'iso' ),
225
            'Single day add (Datedue, matches holiday, shift)'
226
        );
227
228
        is(
229
            $cal->addDuration( $dt, $two_day_dur, 'days' ),
230
            dt_from_string( '2012-07-05', 'iso' ),
231
            'Two days add, skips holiday (Datedue)'
232
        );
233
234
        cmp_ok(
235
            $cal->addDuration( $test_dt, $seven_day_dur, 'days' ), 'eq',
236
            '2012-07-30T11:53:00',
237
            'Add 7 days (Datedue)'
238
        );
239
240
        is(
241
            $cal->addDuration( $saturday, $one_day_dur, 'days' )->day_of_week, 1,
242
            'addDuration skips closed Sunday (Datedue)'
243
        );
244
245
        is(
246
            $cal->addDuration( $day_after_christmas, -1, 'days' )->ymd(), '2012-12-24',
247
            'Negative call to addDuration (Datedue)'
248
        );
249
250
        ## Note that the days_between API says closed days are not considered.
251
        ## This tests are here as an API test.
252
        cmp_ok(
253
            $cal->days_between( $test_dt, $later_dt )->in_units('days'),
254
            '==', 40, 'days_between calculates correctly (Days)'
255
        );
256
257
        cmp_ok(
258
            $cal->days_between( $later_dt, $test_dt )->in_units('days'),
259
            '==', 40, 'Test parameter order not relevant (Days)'
260
        );
261
    }
262
263
    {    ## 'Calendar' tests'
264
265
        $cal = Koha::Calendar->new( branchcode => $mpl, days_mode => 'Calendar' );
266
267
        $dt = dt_from_string( '2012-07-03', 'iso' );
268
269
        is(
270
            $cal->addDuration( $dt, $one_day_dur, 'days' ),
271
            dt_from_string( '2012-07-05', 'iso' ),
272
            'Single day add (Calendar)'
273
        );
274
275
        cmp_ok(
276
            $cal->addDuration( $test_dt, $seven_day_dur, 'days' ), 'eq',
277
            '2012-08-01T11:53:00',
278
            'Add 7 days (Calendar)'
279
        );
280
281
        is(
282
            $cal->addDuration( $saturday, $one_day_dur, 'days' )->day_of_week, 1,
283
            'addDuration skips closed Sunday (Calendar)'
284
        );
285
286
        is(
287
            $cal->addDuration( $day_after_christmas, -1, 'days' )->ymd(), '2012-12-24',
288
            'Negative call to addDuration (Calendar)'
289
        );
290
291
        cmp_ok(
292
            $cal->days_between( $test_dt, $later_dt )->in_units('days'),
293
            '==', 40, 'days_between calculates correctly (Calendar)'
294
        );
295
296
        cmp_ok(
297
            $cal->days_between( $later_dt, $test_dt )->in_units('days'),
298
            '==', 40, 'Test parameter order not relevant (Calendar)'
299
        );
300
    }
301
302
    {    ## 'Days' tests
303
304
        $cal = Koha::Calendar->new( branchcode => $mpl, days_mode => 'Days' );
305
306
        $dt = dt_from_string( '2012-07-03', 'iso' );
307
308
        is(
309
            $cal->addDuration( $dt, $one_day_dur, 'days' ),
310
            dt_from_string( '2012-07-04', 'iso' ),
311
            'Single day add (Days)'
312
        );
313
314
        cmp_ok(
315
            $cal->addDuration( $test_dt, $seven_day_dur, 'days' ), 'eq',
316
            '2012-07-30T11:53:00',
317
            'Add 7 days (Days)'
318
        );
319
320
        is(
321
            $cal->addDuration( $saturday, $one_day_dur, 'days' )->day_of_week, 7,
322
            'addDuration doesn\'t skip closed Sunday (Days)'
323
        );
324
325
        is(
326
            $cal->addDuration( $day_after_christmas, -1, 'days' )->ymd(), '2012-12-25',
327
            'Negative call to addDuration (Days)'
328
        );
329
330
        ## Note that the days_between API says closed days are not considered.
331
        ## This tests are here as an API test.
332
        cmp_ok(
333
            $cal->days_between( $test_dt, $later_dt )->in_units('days'),
334
            '==', 40, 'days_between calculates correctly (Days)'
335
        );
336
337
        cmp_ok(
338
            $cal->days_between( $later_dt, $test_dt )->in_units('days'),
339
            '==', 40, 'Test parameter order not relevant (Days)'
340
        );
341
342
    }
343
344
    {
345
        $cal = Koha::Calendar->new( branchcode => $cpl );
346
        is( $cal->is_holiday($single_holiday), 0, 'Single holiday for MPL, not CPL' );
347
        is(
348
            $cal->is_holiday($holiday_for_another_branch), 1,
349
            'Holiday defined for CPL should be defined as an holiday'
350
        );
351
    }
352
353
    subtest 'days_mode parameter' => sub {
354
        plan tests => 1;
355
356
        t::lib::Mocks::mock_preference( 'useDaysMode', 'Days' );
357
358
        $cal = Koha::Calendar->new( branchcode => $cpl, days_mode => 'Calendar' );
359
        is( $cal->{days_mode}, 'Calendar', q|If set, days_mode is correctly set| );
360
    };
361
362
    $cache->clear_from_cache( $mpl . '_holidays' );
363
    $cache->clear_from_cache( $cpl . '_holidays' );
364
};
365
366
my $library  = $builder->build_object( { class => 'Koha::Libraries' } );
367
my $calendar = Koha::Calendar->new( branchcode => $library->branchcode, days_mode => 'Calendar' );
368
my $holiday  = $builder->build(
369
    {
370
        source => 'SpecialHoliday',
371
        value  => {
372
            branchcode  => $library->branchcode,
373
            day         => $holiday_dt->day,
374
            month       => $holiday_dt->month,
375
            year        => $holiday_dt->year,
376
            title       => 'My holiday',
377
            isexception => 0
378
        },
379
    }
380
);
381
382
subtest 'days_forward' => sub {
383
    plan tests => 4;
384
385
    my $forwarded_dt = $calendar->days_forward( $today, 2 );
386
    my $expected     = $today->clone->add( days => 2 );
387
    is( $forwarded_dt->ymd, $expected->ymd, 'With no holiday on the perioddays_forward should add 2 days' );
388
389
    $forwarded_dt = $calendar->days_forward( $today, 5 );
390
    $expected     = $today->clone->add( days => 6 );
391
    is(
392
        $forwarded_dt->ymd, $expected->ymd,
393
        'With holiday on the perioddays_forward should add 5 days + 1 day for holiday'
394
    );
395
396
    $forwarded_dt = $calendar->days_forward( $today, 0 );
397
    is( $forwarded_dt->ymd, $today->ymd, '0 day should return start dt' );
398
399
    $forwarded_dt = $calendar->days_forward( $today, -2 );
400
    is( $forwarded_dt->ymd, $today->ymd, 'negative day should return start dt' );
401
};
402
403
subtest 'crossing_DST' => sub {
404
405
    plan tests => 3;
406
407
    my $tz           = DateTime::TimeZone->new( name => 'America/New_York' );
408
    my $start_date   = dt_from_string( "2016-03-09 02:29:00", undef, $tz );
409
    my $end_date     = dt_from_string( "2017-01-01 00:00:00", undef, $tz );
410
    my $days_between = $calendar->days_between( $start_date, $end_date );
411
    is( $days_between->delta_days, 298, "Days calculated correctly" );
412
    $days_between = $calendar->days_between( $end_date, $start_date );
413
    is( $days_between->delta_days, 298, "Swapping returns the same" );
414
    my $hours_between = $calendar->hours_between( $start_date, $end_date );
415
    is(
416
        $hours_between->delta_minutes,
417
        298 * 24 * 60 - 149,
418
        "Hours (in minutes) calculated correctly"
419
    );
420
};
421
422
subtest 'hours_between | days_between' => sub {
423
424
    plan tests => 2;
425
426
    #    November 2019
427
    # Su Mo Tu We Th Fr Sa
428
    #                 1  2
429
    #  3  4 *5* 6  7  8  9
430
    # 10 11 12 13 14 15 16
431
    # 17 18 19 20 21 22 23
432
    # 24 25 26 27 28 29 30
433
434
    my $now    = dt_from_string('2019-11-05 12:34:56');    # Today is 2019 Nov 5th
435
    my $nov_6  = dt_from_string('2019-11-06 12:34:56');
436
    my $nov_7  = dt_from_string('2019-11-07 12:34:56');
437
    my $nov_12 = dt_from_string('2019-11-12 12:34:56');
438
    my $nov_13 = dt_from_string('2019-11-13 12:34:56');
439
    my $nov_15 = dt_from_string('2019-11-15 12:34:56');
440
    Time::Fake->offset( $now->epoch );
441
442
    subtest 'No holiday' => sub {
443
444
        plan tests => 2;
445
446
        my $library  = $builder->build_object( { class => 'Koha::Libraries' } );
447
        my $calendar = Koha::Calendar->new( branchcode => $library->branchcode );
448
449
        subtest 'Same hours' => sub {
450
451
            plan tests => 8;
452
453
            # Between 5th and 6th
454
            my $diff_hours = $calendar->hours_between( $now, $nov_6 )->hours;
455
            is( $diff_hours, 1 * 24, 'hours: 1 day, no holiday' );
456
            my $diff_days = $calendar->days_between( $now, $nov_6 )->delta_days;
457
            is( $diff_days, 1, 'days: 1 day, no holiday' );
458
459
            # Between 5th and 7th
460
            $diff_hours = $calendar->hours_between( $now, $nov_7 )->hours;
461
            is( $diff_hours, 2 * 24, 'hours: 2 days, no holiday' );
462
            $diff_days = $calendar->days_between( $now, $nov_7 )->delta_days;
463
            is( $diff_days, 2, 'days: 2 days, no holiday' );
464
465
            # Between 5th and 12th
466
            $diff_hours = $calendar->hours_between( $now, $nov_12 )->hours;
467
            is( $diff_hours, 7 * 24, 'hours: 7 days, no holiday' );
468
            $diff_days = $calendar->days_between( $now, $nov_12 )->delta_days;
469
            is( $diff_days, 7, 'days: 7 days, no holiday' );
470
471
            # Between 5th and 15th
472
            $diff_hours = $calendar->hours_between( $now, $nov_15 )->hours;
473
            is( $diff_hours, 10 * 24, 'hours: 10 days, no holiday' );
474
            $diff_days = $calendar->days_between( $now, $nov_15 )->delta_days;
475
            is( $diff_days, 10, 'days: 10 days, no holiday' );
476
        };
477
478
        subtest 'Different hours' => sub {
479
480
            plan tests => 10;
481
482
            # Between 5th and 5th (Same day short hours loan)
483
            my $diff_hours = $calendar->hours_between( $now, $now->clone->add( hours => 3 ) )->hours;
484
            is( $diff_hours, 3, 'hours: 3 hours, no holidays' );
485
            my $diff_days = $calendar->days_between( $now, $now->clone->add( hours => 3 ) )->delta_days;
486
            is( $diff_days, 0, 'days: 3 hours, no holidays' );
487
488
            # Between 5th and 6th
489
            $diff_hours = $calendar->hours_between( $now, $nov_6->clone->subtract( hours => 3 ) )->hours;
490
            is( $diff_hours, 1 * 24 - 3, 'hours: 21 hours, no holidays' );
491
            $diff_days = $calendar->days_between( $now, $nov_6->clone->subtract( hours => 3 ) )->delta_days;
492
            is( $diff_days, 1, 'days: 21 hours, no holidays' );
493
494
            # Between 5th and 7th
495
            $diff_hours = $calendar->hours_between( $now, $nov_7->clone->subtract( hours => 3 ) )->hours;
496
            is( $diff_hours, 2 * 24 - 3, 'hours: 45 hours, no holidays' );
497
            $diff_days = $calendar->days_between( $now, $nov_7->clone->subtract( hours => 3 ) )->delta_days;
498
            is( $diff_days, 2, 'days: 45 hours, no holidays' );
499
500
            # Between 5th and 12th
501
            $diff_hours = $calendar->hours_between( $now, $nov_12->clone->subtract( hours => 3 ) )->hours;
502
            is( $diff_hours, 7 * 24 - 3, 'hours: 165 hours, no holidays' );
503
            $diff_days = $calendar->days_between( $now, $nov_12->clone->subtract( hours => 3 ) )->delta_days;
504
            is( $diff_days, 7, 'days: 165 hours, no holidays' );
505
506
            # Between 5th and 15th
507
            $diff_hours = $calendar->hours_between( $now, $nov_15->clone->subtract( hours => 3 ) )->hours;
508
            is( $diff_hours, 10 * 24 - 3, 'hours: 237 hours, no holidays' );
509
            $diff_days = $calendar->days_between( $now, $nov_15->clone->subtract( hours => 3 ) )->delta_days;
510
            is( $diff_days, 10, 'days: 237 hours, no holidays' );
511
        };
512
    };
513
514
    subtest 'With holiday' => sub {
515
        plan tests => 2;
516
517
        my $library = $builder->build_object( { class => 'Koha::Libraries' } );
518
519
        # Wednesdays are closed
520
        my $dbh = C4::Context->dbh;
521
        $dbh->do(
522
            q|
523
            INSERT INTO repeatable_holidays (branchcode,weekday,day,month,title,description)
524
            VALUES ( ?, ?, NULL, NULL, ?, '' )
525
        |, undef, $library->branchcode, 3, 'Closed on Wednesday'
526
        );
527
528
        my $calendar = Koha::Calendar->new( branchcode => $library->branchcode );
529
530
        subtest 'Same hours' => sub {
531
            plan tests => 12;
532
533
            my ( $diff_hours, $diff_days );
534
535
            # Between 5th and 6th (This case should never happen in real code, one cannot return on a closed day)
536
            $diff_hours = $calendar->hours_between( $now, $nov_6 )->hours;
537
            is( $diff_hours, 0 * 24, 'hours: 1 day, end_dt = holiday' );    # FIXME Is this really should be 0?
538
            $diff_days = $calendar->days_between( $now, $nov_6 )->delta_days;
539
            is( $diff_days, 0, 'days: 1 day, end_dt = holiday' );           # FIXME Is this really should be 0?
540
541
            # Between 6th and 7th (This case should never happen in real code, one cannot issue on a closed day)
542
            $diff_hours = $calendar->hours_between( $nov_6, $nov_7 )->hours;
543
            is( $diff_hours, 0 * 24, 'hours: 1 day, start_dt = holiday' );    # FIXME Is this really should be 0?
544
            $diff_days = $calendar->days_between( $nov_6, $nov_7 )->delta_days;
545
            is( $diff_days, 0, 'days: 1 day, start_dt = holiday' );           # FIXME Is this really should be 0?
546
547
            # Between 5th and 7th
548
            $diff_hours = $calendar->hours_between( $now, $nov_7 )->hours;
549
            is( $diff_hours, 2 * 24 - 1 * 24, 'hours: 2 days, 1 holiday' );
550
            $diff_days = $calendar->days_between( $now, $nov_7 )->delta_days;
551
            is( $diff_days, 2 - 1, 'days: 2 days, 1 holiday' );
552
553
            # Between 5th and 12th
554
            $diff_hours = $calendar->hours_between( $now, $nov_12 )->hours;
555
            is( $diff_hours, 7 * 24 - 1 * 24, 'hours: 7 days, 1 holiday' );
556
            $diff_days = $calendar->days_between( $now, $nov_12 )->delta_days;
557
            is( $diff_days, 7 - 1, 'day: 7 days, 1 holiday' );
558
559
            # Between 5th and 13th
560
            $diff_hours = $calendar->hours_between( $now, $nov_13 )->hours;
561
            is( $diff_hours, 8 * 24 - 2 * 24, 'hours: 8 days, 2 holidays' );
562
            $diff_days = $calendar->days_between( $now, $nov_13 )->delta_days;
563
            is( $diff_days, 8 - 2, 'days: 8 days, 2 holidays' );
564
565
            # Between 5th and 15th
566
            $diff_hours = $calendar->hours_between( $now, $nov_15 )->hours;
567
            is( $diff_hours, 10 * 24 - 2 * 24, 'hours: 10 days, 2 holidays' );
568
            $diff_days = $calendar->days_between( $now, $nov_15 )->delta_days;
569
            is( $diff_days, 10 - 2, 'days: 10 days, 2 holidays' );
570
        };
571
572
        subtest 'Different hours' => sub {
573
            plan tests => 14;
574
575
            my ( $diff_hours, $diff_days );
576
577
            # Between 5th and 5th (Same day short hours loan)
578
            # No test - Tested above as 5th is an open day
579
580
            # Between 5th and 6th (This case should never happen in real code, one cannot return on a closed day)
581
            my $duration = $calendar->hours_between( $now, $nov_6->clone->subtract( hours => 3 ) );
582
            is( $duration->hours, abs( 0 * 24 - 3 ), 'hours: 21 hours, end_dt = holiday' )
583
                ;    # FIXME $duration->hours always return a abs
584
            is( $duration->is_negative, 1, '? is negative ?' )
585
                ;    # FIXME Do really test for that case in our calls to hours_between?
586
            $duration = $calendar->days_between( $now, $nov_6->clone->subtract( hours => 3 ) );
587
            is( $duration->hours, abs(0), 'days: 21 hours, end_dt = holiday' );    # FIXME Is this correct?
588
589
            # Between 6th and 7th (This case should never happen in real code, one cannot issue on a closed day)
590
            $duration = $calendar->hours_between( $nov_6, $nov_7->clone->subtract( hours => 3 ) );
591
            is( $duration->hours, abs( 0 * 24 - 3 ), 'hours: 21 hours, start_dt = holiday' )
592
                ;    # FIXME $duration->hours always return a abs
593
            is( $duration->is_negative, 1, '? is negative ?' )
594
                ;    # FIXME Do really test for that case in our calls to hours_between?
595
            $duration = $calendar->days_between( $nov_6, $nov_7->clone->subtract( hours => 3 ) );
596
            is( $duration->hours, abs(0), 'days: 21 hours, start_dt = holiday' );    # FIXME Is this correct?
597
598
            # Between 5th and 7th
599
            $diff_hours = $calendar->hours_between( $now, $nov_7->clone->subtract( hours => 3 ) )->hours;
600
            is( $diff_hours, 2 * 24 - 1 * 24 - 3, 'hours: 45 hours, 1 holiday' );
601
            $diff_days = $calendar->days_between( $now, $nov_7->clone->subtract( hours => 3 ) )->delta_days;
602
            is( $diff_days, 2 - 1, 'days: 45 hours, 1 holiday' );
603
604
            # Between 5th and 12th
605
            $diff_hours = $calendar->hours_between( $now, $nov_12->clone->subtract( hours => 3 ) )->hours;
606
            is( $diff_hours, 7 * 24 - 1 * 24 - 3, 'hours: 165 hours, 1 holiday' );
607
            $diff_days = $calendar->days_between( $now, $nov_12->clone->subtract( hours => 3 ) )->delta_days;
608
            is( $diff_days, 7 - 1, 'days: 165 hours, 1 holiday' );
609
610
            # Between 5th and 13th
611
            $diff_hours = $calendar->hours_between( $now, $nov_13->clone->subtract( hours => 3 ) )->hours;
612
            is( $diff_hours, 8 * 24 - 2 * 24 - 3, 'hours: 289 hours, 2 holidays ' );
613
            $diff_days = $calendar->days_between( $now, $nov_13->clone->subtract( hours => 3 ) )->delta_days;
614
            is( $diff_days, 8 - 1, 'days: 289 hours, 2 holidays' );
615
616
            # Between 5th and 15th
617
            $diff_hours = $calendar->hours_between( $now, $nov_15->clone->subtract( hours => 3 ) )->hours;
618
            is( $diff_hours, 10 * 24 - 2 * 24 - 3, 'hours: 237 hours, 2 holidays' );
619
            $diff_days = $calendar->days_between( $now, $nov_15->clone->subtract( hours => 3 ) )->delta_days;
620
            is( $diff_days, 10 - 2, 'days: 237 hours, 2 holidays' );
621
        };
622
623
    };
624
625
    Time::Fake->reset;
626
};
627
628
subtest 'is_holiday' => sub {
629
    plan tests => 1;
630
631
    subtest 'weekday holidays' => sub {
632
        plan tests => 7;
633
634
        my $library = $builder->build_object( { class => 'Koha::Libraries' } );
635
636
        my $day = dt_from_string();
637
        my $dow = scalar $day->day_of_week;
638
        $dow = 0 if $dow == 7;
639
640
        # Closed this day of the week
641
        my $dbh = C4::Context->dbh;
642
        $dbh->do(
643
            q|
644
            INSERT INTO repeatable_holidays (branchcode,weekday,day,month,title,description)
645
            VALUES ( ?, ?, NULL, NULL, ?, '' )
646
        |, undef, $library->branchcode, $dow, "TEST"
647
        );
648
649
        # Iterate 7 days
650
        my $sth = $dbh->prepare("UPDATE repeatable_holidays SET weekday = ? WHERE branchcode = ? AND title = 'TEST'");
651
        for my $i ( 0 .. 6 ) {
652
            my $calendar = Koha::Calendar->new( branchcode => $library->branchcode );
653
654
            is( $calendar->is_holiday($day), 1, $day->day_name() . " works as a repeatable holiday" );
655
656
            # Increment the date and holiday day
657
            $day->add( days => 1 );
658
            $dow++;
659
            $dow = 0 if $dow == 7;
660
            $sth->execute( $dow, $library->branchcode );
661
        }
662
    };
663
};
664
665
subtest 'get_push_amt' => sub {
666
    plan tests => 1;
667
668
    t::lib::Mocks::mock_preference( 'useDaysMode', 'Dayweek' );
669
670
    subtest 'weekday holidays' => sub {
671
        plan tests => 7;
672
673
        my $library = $builder->build_object( { class => 'Koha::Libraries' } );
674
675
        my $day = dt_from_string();
676
        my $dow = scalar $day->day_of_week;
677
        $dow = 0 if $dow == 7;
678
679
        # Closed this day of the week
680
        my $dbh = C4::Context->dbh;
681
        $dbh->do(
682
            q|
683
            INSERT INTO repeatable_holidays (branchcode,weekday,day,month,title,description)
684
            VALUES ( ?, ?, NULL, NULL, ?, '' )
685
        |, undef, $library->branchcode, $dow, "TEST"
686
        );
687
688
        # Iterate 7 days
689
        my $sth = $dbh->prepare("UPDATE repeatable_holidays SET weekday = ? WHERE branchcode = ? AND title = 'TEST'");
690
        for my $i ( 0 .. 6 ) {
691
            my $calendar = Koha::Calendar->new( branchcode => $library->branchcode, days_mode => 'Dayweek' );
692
693
            my $npa;
694
            eval {
695
                local $SIG{ALRM} = sub { die "alarm\n" };    # NB: \n required
696
                alarm 2;
697
                $npa = $calendar->next_open_days( $day, 0 );
698
                alarm 0;
699
            };
700
            if ($@) {
701
                die unless $@ eq "alarm\n";                  # propagate unexpected errors
702
                                                             # timed out
703
                ok( 0, "next_push_amt succeeded for " . $day->day_name() . " weekday holiday" );
704
            } else {
705
                ok( $npa, "next_push_amt succeeded for " . $day->day_name() . " weekday holiday" );
706
            }
707
708
            # Increment the date and holiday day
709
            $day->add( days => 1 );
710
            $dow++;
711
            $dow = 0 if $dow == 7;
712
            $sth->execute( $dow, $library->branchcode );
713
        }
714
    };
715
};
716
717
$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 2867-2881 subtest 'AddReturn + suspension_chargeperiod' => sub { Link Here
2867
    t::lib::Mocks::mock_preference('SuspensionsCalendar', 'noSuspensionsWhenClosed');
2866
    t::lib::Mocks::mock_preference('SuspensionsCalendar', 'noSuspensionsWhenClosed');
2868
2867
2869
    # Adding a holiday 2 days ago
2868
    # Adding a holiday 2 days ago
2870
    my $calendar = C4::Calendar->new(branchcode => $library->{branchcode});
2869
    my $calendar = Koha::DiscreteCalendar->new({ branchcode => $library->{branchcode} });
2871
    my $two_days_ago = $now->clone->subtract( days => 2 );
2870
    my $two_days_ago = $now->clone->subtract( days => 2 );
2872
    $calendar->insert_single_holiday(
2871
    $calendar->edit_holiday({
2873
        day             => $two_days_ago->day,
2872
        holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
2874
        month           => $two_days_ago->month,
2873
        override        => 1,
2875
        year            => $two_days_ago->year,
2874
        start_date      => $two_days_ago,
2875
        end_date        => $two_days_ago,
2876
        title           => 'holidayTest-2d',
2876
        title           => 'holidayTest-2d',
2877
        description     => 'holidayDesc 2 days ago'
2877
        description     => 'holidayDesc 2 days ago'
2878
    );
2878
    });
2879
    # With 5 days of overdue, only 4 (x finedays=2) days must charged (one was an holiday)
2879
    # With 5 days of overdue, only 4 (x finedays=2) days must charged (one was an holiday)
2880
    $expected_expiration = $now->clone->add( days => floor( ( ( 5 - 0 - 1 ) / 1 ) * 2 ) );
2880
    $expected_expiration = $now->clone->add( days => floor( ( ( 5 - 0 - 1 ) / 1 ) * 2 ) );
2881
    test_debarment_on_checkout(
2881
    test_debarment_on_checkout(
Lines 2890-2902 subtest 'AddReturn + suspension_chargeperiod' => sub { Link Here
2890
2890
2891
    # Adding a holiday 2 days ahead, with finesCalendar=noFinesWhenClosed it should be skipped
2891
    # Adding a holiday 2 days ahead, with finesCalendar=noFinesWhenClosed it should be skipped
2892
    my $two_days_ahead = $now->clone->add( days => 2 );
2892
    my $two_days_ahead = $now->clone->add( days => 2 );
2893
    $calendar->insert_single_holiday(
2893
    $calendar->edit_holiday({
2894
        day             => $two_days_ahead->day,
2894
        holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
2895
        month           => $two_days_ahead->month,
2895
        start_date      => $two_days_ahead,
2896
        year            => $two_days_ahead->year,
2896
        end_date        => $two_days_ahead,
2897
        title           => 'holidayTest+2d',
2897
        title           => 'holidayTest+2d',
2898
        description     => 'holidayDesc 2 days ahead'
2898
        description     => 'holidayDesc 2 days ahead'
2899
    );
2899
    });
2900
2900
2901
    # Same as above, but we should skip D+2
2901
    # Same as above, but we should skip D+2
2902
    $expected_expiration = $now->clone->add( days => floor( ( ( 5 - 0 - 1 ) / 1 ) * 2 ) + 1 );
2902
    $expected_expiration = $now->clone->add( days => floor( ( ( 5 - 0 - 1 ) / 1 ) * 2 ) + 1 );
Lines 2912-2924 subtest 'AddReturn + suspension_chargeperiod' => sub { Link Here
2912
2912
2913
    # Adding another holiday, day of expiration date
2913
    # Adding another holiday, day of expiration date
2914
    my $expected_expiration_dt = dt_from_string($expected_expiration);
2914
    my $expected_expiration_dt = dt_from_string($expected_expiration);
2915
    $calendar->insert_single_holiday(
2915
    $calendar->edit_holiday({
2916
        day             => $expected_expiration_dt->day,
2916
        holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
2917
        month           => $expected_expiration_dt->month,
2917
        start_date      => $expected_expiration_dt,
2918
        year            => $expected_expiration_dt->year,
2918
        end_date        => $expected_expiration_dt,
2919
        title           => 'holidayTest_exp',
2919
        title           => 'holidayTest_exp',
2920
        description     => 'holidayDesc on expiration date'
2920
        description     => 'holidayDesc on expiration date'
2921
    );
2921
    });
2922
    # Expiration date will be the day after
2922
    # Expiration date will be the day after
2923
    test_debarment_on_checkout(
2923
    test_debarment_on_checkout(
2924
        {
2924
        {
Lines 2993-2998 subtest 'AddReturn + suspension_chargeperiod' => sub { Link Here
2993
    AddIssue( $patron, $item_1->barcode, $now->clone->subtract( days => 1 ) );
2993
    AddIssue( $patron, $item_1->barcode, $now->clone->subtract( days => 1 ) );
2994
    (undef, $message) = AddReturn( $item_1->barcode, $library->{branchcode}, undef, $now );
2994
    (undef, $message) = AddReturn( $item_1->barcode, $library->{branchcode}, undef, $now );
2995
    is( $message->{WasReturned} && exists $message->{PrevDebarred}, 1, 'Previously debarred message for Addreturn when overdue');
2995
    is( $message->{WasReturned} && exists $message->{PrevDebarred}, 1, 'Previously debarred message for Addreturn when overdue');
2996
2997
    # delete holidays
2998
    $calendar->edit_holiday({
2999
        holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{NONE},
3000
        start_date      => $two_days_ago,
3001
        end_date        => $expected_expiration_dt,
3002
        title           => '',
3003
        description     => ''
3004
    });
2996
};
3005
};
2997
3006
2998
subtest 'CanBookBeIssued + AutoReturnCheckedOutItems' => sub {
3007
subtest 'CanBookBeIssued + AutoReturnCheckedOutItems' => sub {
Lines 4901-4918 subtest 'Incremented fee tests' => sub { Link Here
4901
    $accountline->delete();
4910
    $accountline->delete();
4902
    $issue->delete();
4911
    $issue->delete();
4903
4912
4904
    my $calendar = C4::Calendar->new( branchcode => $library->id );
4913
    my $calendar = Koha::DiscreteCalendar->new({ branchcode => $library->id });
4905
    # DateTime 1..7 (Mon..Sun), C4::Calender 0..6 (Sun..Sat)
4914
    # DateTime 1..7 (Mon..Sun), Koha::DiscreteCalender 1..7 (Sun..Sat)
4906
    my $closed_day =
4915
    my $closed_day =
4907
        ( $dt_from->day_of_week == 6 ) ? 0
4916
        ( $dt_from->day_of_week == 7 ) ? 1
4908
      : ( $dt_from->day_of_week == 7 ) ? 1
4917
      : $dt_from->day_of_week + 1;
4909
      :                                  $dt_from->day_of_week + 1;
4910
    my $closed_day_name = $dt_from->clone->add(days => 1)->day_name;
4918
    my $closed_day_name = $dt_from->clone->add(days => 1)->day_name;
4911
    $calendar->insert_week_day_holiday(
4919
    $calendar->edit_holiday({
4920
        holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{WEEKLY},
4912
        weekday     => $closed_day,
4921
        weekday     => $closed_day,
4922
        start_date   => $dt_from,
4923
        end_date     => $dt_from,
4913
        title       => 'Test holiday',
4924
        title       => 'Test holiday',
4914
        description => 'Test holiday'
4925
        description => 'Test holiday'
4915
    );
4926
    });
4916
    $issue =
4927
    $issue =
4917
      AddIssue( $patron, $item->barcode, $dt_to, undef, $dt_from );
4928
      AddIssue( $patron, $item->barcode, $dt_to, undef, $dt_from );
4918
    $accountline = Koha::Account::Lines->find( { itemnumber => $item->id } );
4929
    $accountline = Koha::Account::Lines->find( { itemnumber => $item->id } );
Lines 5037-5043 subtest 'Incremented fee tests' => sub { Link Here
5037
    $accountline->delete();
5048
    $accountline->delete();
5038
    $issue->delete();
5049
    $issue->delete();
5039
5050
5040
    $calendar->delete_holiday( weekday => $closed_day );
5051
    $calendar->edit_holiday({
5052
        holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{NONE},
5053
        delete_type  => 1,
5054
        override     => 1,
5055
        weekday      => $closed_day,
5056
        start_date   => $dt_from,
5057
        end_date     => $dt_from,
5058
        title       => '',
5059
        description => '',
5060
    });
5061
5041
    $issue =
5062
    $issue =
5042
      AddIssue( $patron, $item->barcode, $dt_to, undef, $dt_from );
5063
      AddIssue( $patron, $item->barcode, $dt_to, undef, $dt_from );
5043
    $accountline = Koha::Account::Lines->find( { itemnumber => $item->id } );
5064
    $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 77-99 is($date, $dateexpiry . 'T23:59:00', 'date expiry with useDaysMode to noDays'); Link Here
77
78
78
# Let's add a special holiday on 2013-01-01. With ReturnBeforeExpiry and
79
# Let's add a special holiday on 2013-01-01. With ReturnBeforeExpiry and
79
# useDaysMode different from 'Days', return should forward the dateexpiry.
80
# useDaysMode different from 'Days', return should forward the dateexpiry.
80
my $calendar = C4::Calendar->new(branchcode => $branchcode);
81
my $calendar = Koha::DiscreteCalendar->new({ branchcode => $branchcode });
81
$calendar->insert_single_holiday(
82
$calendar->edit_holiday({
82
    day             => 1,
83
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
83
    month           => 1,
84
    override        => 1,
84
    year            => 2013,
85
    start_date      => dt_from_string($dateexpiry, 'iso'),
86
    end_date        => dt_from_string($dateexpiry, 'iso'),
85
    title           =>'holidayTest',
87
    title           =>'holidayTest',
86
    description     => 'holidayDesc'
88
    description     => 'holidayDesc'
87
);
89
});
88
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower );
90
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower );
89
is($date, '2012-12-31T23:59:00', 'date expiry should be 2013-01-01 -1 day');
91
is($date, '2012-12-31T23:59:00', 'date expiry should be 2013-01-01 -1 day');
90
$calendar->insert_single_holiday(
92
$calendar->edit_holiday({
91
    day             => 31,
93
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
92
    month           => 12,
94
    override        => 1,
93
    year            => 2012,
95
    start_date      => dt_from_string('2012-12-31', 'iso'),
96
    end_date        => dt_from_string('2012-12-31', 'iso'),
94
    title           =>'holidayTest',
97
    title           =>'holidayTest',
95
    description     => 'holidayDesc'
98
    description     => 'holidayDesc'
96
);
99
});
97
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower );
100
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower );
98
is($date, '2012-12-30T23:59:00', 'date expiry should be 2013-01-01 -2 day');
101
is($date, '2012-12-30T23:59:00', 'date expiry should be 2013-01-01 -2 day');
99
102
Lines 132-169 is($date, '2013-02-' . (9 + $renewalperiod) . 'T23:59:00', "useDaysMode = Daywee Link Here
132
# Now let's add a closed day on the expected renewal date, it should
135
# Now let's add a closed day on the expected renewal date, it should
133
# roll forward as per Datedue (i.e. one day at a time)
136
# roll forward as per Datedue (i.e. one day at a time)
134
# For issues...
137
# For issues...
135
$calendar->insert_single_holiday(
138
my $issue_date = dt_from_string('2013-02-' . (9 + $issuelength), 'iso');
136
    day             => 9 + $issuelength,
139
$calendar->edit_holiday({
137
    month           => 2,
140
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
138
    year            => 2013,
141
    override        => 1,
142
    start_date      => $issue_date,
143
    end_date        => $issue_date,
139
    title           =>'DayweekTest1',
144
    title           =>'DayweekTest1',
140
    description     => 'DayweekTest1'
145
    description     => 'DayweekTest1'
141
);
146
});
147
142
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower );
148
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower );
143
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 )");
149
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 )");
144
# Remove the holiday we just created
150
# Remove the holiday we just created
145
$calendar->delete_holiday(
151
$calendar->edit_holiday({
146
    day             => 9 + $issuelength,
152
    holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{NONE},
147
    month           => 2,
153
    override     => 1,
148
    year            => 2013
154
    start_date   => $issue_date,
149
);
155
    end_date     => $issue_date,
156
    title        => '',
157
    description  => '',
158
});
150
159
151
# ...and for renewals...
160
# ...and for renewals...
152
$calendar->insert_single_holiday(
161
my $renewal_date = dt_from_string('2013-02-' . (9 + $renewalperiod), 'iso');
153
    day             => 9 + $renewalperiod,
162
$calendar->edit_holiday({
154
    month           => 2,
163
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
155
    year            => 2013,
164
    override        => 1,
165
    start_date      => $renewal_date,
166
    end_date        => $renewal_date,
156
    title           =>'DayweekTest2',
167
    title           =>'DayweekTest2',
157
    description     => 'DayweekTest2'
168
    description     => 'DayweekTest2'
158
);
169
});
159
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower, 1 );
170
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower, 1 );
160
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 )");
171
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 )");
161
# Remove the holiday we just created
172
# Remove the holiday we just created
162
$calendar->delete_holiday(
173
$calendar->edit_holiday({
163
    day             => 9 + $renewalperiod,
174
    title        => '',
164
    month           => 2,
175
    description  => '',
165
    year            => 2013,
176
    holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{NONE},
166
);
177
    start_date   => $renewal_date,
178
    end_date     => $renewal_date,
179
    override     => 1,
180
});
167
181
168
# Now we test it does the right thing if the loan and renewal periods
182
# Now we test it does the right thing if the loan and renewal periods
169
# are a multiple of 7 days
183
# are a multiple of 7 days
Lines 198-240 my $dayweek_borrower = $builder->build_object( Link Here
198
212
199
# For issues...
213
# For issues...
200
$start_date = DateTime->new({year => 2013, month => 2, day => 9});
214
$start_date = DateTime->new({year => 2013, month => 2, day => 9});
201
$calendar->insert_single_holiday(
215
my $holiday_date = $start_date->clone();
202
    day             => 9 + $dayweek_issuelength,
216
$holiday_date->add(days => $dayweek_issuelength);
203
    month           => 2,
217
$calendar->edit_holiday({
204
    year            => 2013,
218
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
219
    override        => 1,
220
    start_date      => $holiday_date,
221
    end_date        => $holiday_date,
205
    title           =>'DayweekTest3',
222
    title           =>'DayweekTest3',
206
    description     => 'DayweekTest3'
223
    description     => 'DayweekTest3'
207
);
224
});
208
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $dayweek_borrower );
225
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $dayweek_borrower );
209
my $issue_should_add = $dayweek_issuelength + 7;
226
my $issue_should_add = $dayweek_issuelength + 7;
210
my $dayweek_issue_expected = $start_date->add( days => $issue_should_add );
227
my $dayweek_issue_expected = $start_date->add( days => $issue_should_add );
211
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 )");
228
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
# Remove the holiday we just created
229
# Remove the holiday we just created
213
$calendar->delete_holiday(
230
$calendar->edit_holiday({
214
    day             => 9 + $dayweek_issuelength,
231
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{NONE},
215
    month           => 2,
232
    override        => 1,
216
    year            => 2013,
233
    start_date      => $holiday_date,
217
);
234
    end_date        => $holiday_date,
235
    title           => '',
236
    description     => '',
237
});
218
238
219
# ...and for renewals...
239
# ...and for renewals...
220
$start_date = DateTime->new({year => 2013, month => 2, day => 9});
240
$start_date = DateTime->new({year => 2013, month => 2, day => 9});
221
$calendar->insert_single_holiday(
241
$holiday_date = $start_date->clone();
222
    day             => 9 + $dayweek_renewalperiod,
242
$holiday_date->add(days => $dayweek_renewalperiod);
223
    month           => 2,
243
$calendar->edit_holiday({
224
    year            => 2013,
244
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
245
    override        => 1,
246
    start_date      => $holiday_date,
247
    end_date        => $holiday_date,
225
    title           => 'DayweekTest4',
248
    title           => 'DayweekTest4',
226
    description     => 'DayweekTest4'
249
    description     => 'DayweekTest4'
227
);
250
});
228
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $dayweek_borrower, 1 );
251
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $dayweek_borrower, 1 );
229
my $renewal_should_add = $dayweek_renewalperiod + 7;
252
my $renewal_should_add = $dayweek_renewalperiod + 7;
230
my $dayweek_renewal_expected = $start_date->add( days => $renewal_should_add );
253
my $dayweek_renewal_expected = $start_date->add( days => $renewal_should_add );
231
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 )");
254
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 )");
232
# Remove the holiday we just created
255
# Remove the holiday we just created
233
$calendar->delete_holiday(
256
$calendar->edit_holiday({
234
    day             => 9 + $dayweek_renewalperiod,
257
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{NONE},
235
    month           => 2,
258
    override        => 1,
236
    year            => 2013,
259
    start_date      => $holiday_date,
237
);
260
    end_date        => $holiday_date,
261
    title           => '',
262
    description     => '',
263
});
238
264
239
# Now test it continues to roll forward by 7 days until it finds
265
# Now test it continues to roll forward by 7 days until it finds
240
# an open day, so we create a 3 week period of closed Saturdays
266
# an open day, so we create a 3 week period of closed Saturdays
Lines 242-270 $start_date = DateTime->new({year => 2013, month => 2, day => 9}); Link Here
242
my $expected_rolled_date = DateTime->new({year => 2013, month => 3, day => 9});
268
my $expected_rolled_date = DateTime->new({year => 2013, month => 3, day => 9});
243
my $holiday = $start_date->clone();
269
my $holiday = $start_date->clone();
244
$holiday->add(days => 7);
270
$holiday->add(days => 7);
245
$calendar->insert_single_holiday(
271
$calendar->edit_holiday({
246
    day             => $holiday->day,
272
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
247
    month           => $holiday->month,
273
    override        => 1,
248
    year            => 2013,
274
    start_date      => $holiday,
275
    end_date        => $holiday,
249
    title           =>'DayweekTest5',
276
    title           =>'DayweekTest5',
250
    description     => 'DayweekTest5'
277
    description     => 'DayweekTest5'
251
);
278
});
252
$holiday->add(days => 7);
279
$holiday->add(days => 7);
253
$calendar->insert_single_holiday(
280
$calendar->edit_holiday({
254
    day             => $holiday->day,
281
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
255
    month           => $holiday->month,
282
    override        => 1,
256
    year            => 2013,
283
    start_date      => $holiday,
284
    end_date        => $holiday,
257
    title           =>'DayweekTest6',
285
    title           =>'DayweekTest6',
258
    description     => 'DayweekTest6'
286
    description     => 'DayweekTest6'
259
);
287
});
260
$holiday->add(days => 7);
288
$holiday->add(days => 7);
261
$calendar->insert_single_holiday(
289
$calendar->edit_holiday({
262
    day             => $holiday->day,
290
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
263
    month           => $holiday->month,
291
    override        => 1,
264
    year            => 2013,
292
    start_date      => $holiday,
293
    end_date        => $holiday,
265
    title           =>'DayweekTest7',
294
    title           =>'DayweekTest7',
266
    description     => 'DayweekTest7'
295
    description     => 'DayweekTest7'
267
);
296
});
268
# For issues...
297
# For issues...
269
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $dayweek_borrower );
298
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $dayweek_borrower );
270
$dayweek_issue_expected = $start_date->add( days => $issue_should_add );
299
$dayweek_issue_expected = $start_date->add( days => $issue_should_add );
Lines 278-310 is($date, $expected_rolled_date->strftime('%F') . 'T23:59:00', "useDaysMode = Da Link Here
278
$start_date = DateTime->new({year => 2013, month => 2, day => 9});
307
$start_date = DateTime->new({year => 2013, month => 2, day => 9});
279
my $del_holiday = $start_date->clone();
308
my $del_holiday = $start_date->clone();
280
$del_holiday->add(days => 7);
309
$del_holiday->add(days => 7);
281
$calendar->delete_holiday(
310
$calendar->edit_holiday({
282
    day             => $del_holiday->day,
311
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{NONE},
283
    month           => $del_holiday->month,
312
    override        => 1,
284
    year            => 2013
313
    start_date      => $del_holiday,
285
);
314
    end_date        => $del_holiday,
315
    title           => '',
316
    description     => '',
317
});
286
$del_holiday->add(days => 7);
318
$del_holiday->add(days => 7);
287
$calendar->delete_holiday(
319
$calendar->edit_holiday({
288
    day             => $del_holiday->day,
320
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{NONE},
289
    month           => $del_holiday->month,
321
    override        => 1,
290
    year            => 2013
322
    start_date      => $del_holiday,
291
);
323
    end_date        => $del_holiday,
324
    title           => '',
325
    description     => '',
326
});
292
$del_holiday->add(days => 7);
327
$del_holiday->add(days => 7);
293
$calendar->delete_holiday(
328
$calendar->edit_holiday({
294
    day             => $del_holiday->day,
329
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{NONE},
295
    month           => $del_holiday->month,
330
    override        => 1,
296
    year            => 2013
331
    start_date      => $del_holiday,
297
);
332
    end_date        => $del_holiday,
333
    title           => '',
334
    description     => '',
335
});
298
336
299
# Now test that useDaysMode "Dayweek" doesn't try to roll forward onto
337
# Now test that useDaysMode "Dayweek" doesn't try to roll forward onto
300
# a permanently closed day and instead rolls forward just one day
338
# a permanently closed day and instead rolls forward just one day
301
$start_date = DateTime->new({year => 2013, month => 2, day => 9});
339
$start_date = DateTime->new({year => 2013, month => 2, day => 9});
302
# Our tests are concerned with Saturdays, so let's close on Saturdays
340
# Our tests are concerned with Saturdays, so let's close on Saturdays
303
$calendar->insert_week_day_holiday(
341
$calendar->edit_holiday({
304
    weekday => 6,
342
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{WEEKLY},
343
    weekday         => 7,
344
    override        => 1,
345
    start_date      => $start_date,
346
    end_date        => $start_date,
305
    title => "Saturday closure",
347
    title => "Saturday closure",
306
    description => "Closed on Saturdays"
348
    description => "Closed on Saturdays"
307
);
349
});
308
# For issues...
350
# For issues...
309
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $dayweek_borrower );
351
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $dayweek_borrower );
310
$dayweek_issue_expected = $start_date->add( days => $dayweek_issuelength + 1 );
352
$dayweek_issue_expected = $start_date->add( days => $dayweek_issuelength + 1 );
Lines 315-323 $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $dayw Link Here
315
$dayweek_renewal_expected = $start_date->add( days => $dayweek_renewalperiod + 1 );
357
$dayweek_renewal_expected = $start_date->add( days => $dayweek_renewalperiod + 1 );
316
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 )");
358
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 )");
317
# Remove the holiday we just created
359
# Remove the holiday we just created
318
$calendar->delete_holiday(
360
$calendar->edit_holiday({
319
    weekday => 6
361
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{NONE},
320
);
362
    weekday         => 7,
363
    override        => 1,
364
    delete_type     => 1,
365
    start_date      => $start_date,
366
    end_date        => $start_date,
367
    title           => "",
368
    description     => ""
369
});
321
370
322
# Renewal period of 0 is valid
371
# Renewal period of 0 is valid
323
Koha::CirculationRules->search()->delete();
372
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 65-75 my $itemnumber = Koha::Item->new({ Link Here
65
        itype => $itemtype
69
        itype => $itemtype
66
    })->store->itemnumber;
70
    })->store->itemnumber;
67
71
68
# clear any holidays to avoid throwing off the suspension day
69
# calculations
70
$dbh->do('DELETE FROM special_holidays');
71
$dbh->do('DELETE FROM repeatable_holidays');
72
73
my $daysago20 = dt_from_string->add_duration(DateTime::Duration->new(days => -20));
72
my $daysago20 = dt_from_string->add_duration(DateTime::Duration->new(days => -20));
74
my $daysafter40 = dt_from_string->add_duration(DateTime::Duration->new(days => 40));
73
my $daysafter40 = dt_from_string->add_duration(DateTime::Duration->new(days => 40));
75
74
(-)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 (-340 lines)
Lines 1-340 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 => 14;
21
use Test::Exception;
22
23
use DateTime;
24
use DateTime::TimeZone;
25
26
use t::lib::TestBuilder;
27
use C4::Context;
28
use Koha::Database;
29
use Koha::DateUtils qw( dt_from_string );
30
31
BEGIN {
32
    use_ok('Koha::Calendar');
33
    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 ));
34
}
35
36
my $schema = Koha::Database->new->schema;
37
my $dbh = C4::Context->dbh;
38
my $builder = t::lib::TestBuilder->new;
39
40
subtest 'is_holiday timezone tests' => sub {
41
42
    plan tests => 1;
43
44
    $schema->storage->txn_begin;
45
46
    $dbh->do("DELETE FROM special_holidays");
47
    # Clear cache
48
    Koha::Caches->get_instance->flush_all;
49
50
    # Artificially set timezone
51
    my $timezone = 'America/Santiago';
52
    $ENV{TZ} = $timezone;
53
    use POSIX qw(tzset);
54
    tzset;
55
56
    my $branch = $builder->build( { source => 'Branch' } )->{branchcode};
57
    my $calendar = Koha::Calendar->new( branchcode => $branch );
58
59
    C4::Calendar->new( branchcode => $branch )->insert_exception_holiday(
60
        day         => 6,
61
        month       => 9,
62
        year        => 2015,
63
        title       => 'Invalid date',
64
        description => 'Invalid date description',
65
    );
66
67
    my $exception_holiday = DateTime->new( day => 6, month => 9, year => 2015 );
68
    my $now_dt            = DateTime->now;
69
70
    my $diff;
71
    eval { $diff = $calendar->days_between( $now_dt, $exception_holiday ) };
72
    unlike(
73
        $@,
74
        qr/Invalid local time for date in time zone: America\/Santiago/,
75
        'Avoid invalid datetime due to DST'
76
    );
77
78
    $schema->storage->txn_rollback;
79
};
80
81
$schema->storage->txn_begin;
82
83
# Create two fresh branches for the tests
84
my $branch_1 = $builder->build({ source => 'Branch' })->{ branchcode };
85
my $branch_2 = $builder->build({ source => 'Branch' })->{ branchcode };
86
87
C4::Calendar->new( branchcode => $branch_1 )->insert_week_day_holiday(
88
    weekday     => 0,
89
    title       => '',
90
    description => 'Sundays',
91
);
92
93
my $holiday2add = dt_from_string("2015-01-01");
94
C4::Calendar->new( branchcode => $branch_1 )->insert_day_month_holiday(
95
    day         => $holiday2add->day(),
96
    month       => $holiday2add->month(),
97
    year        => $holiday2add->year(),
98
    title       => '',
99
    description => "New Year's Day",
100
);
101
$holiday2add = dt_from_string("2014-12-25");
102
C4::Calendar->new( branchcode => $branch_1 )->insert_day_month_holiday(
103
    day         => $holiday2add->day(),
104
    month       => $holiday2add->month(),
105
    year        => $holiday2add->year(),
106
    title       => '',
107
    description => 'Christmas',
108
);
109
110
my $koha_calendar = Koha::Calendar->new( branchcode => $branch_1 );
111
my $c4_calendar = C4::Calendar->new( branchcode => $branch_1 );
112
113
isa_ok( $koha_calendar, 'Koha::Calendar', 'Koha::Calendar class returned' );
114
isa_ok( $c4_calendar,   'C4::Calendar',   'C4::Calendar class returned' );
115
116
my $sunday = DateTime->new(
117
    year  => 2011,
118
    month => 6,
119
    day   => 26,
120
);
121
my $monday = DateTime->new(
122
    year  => 2011,
123
    month => 6,
124
    day   => 27,
125
);
126
my $christmas = DateTime->new(
127
    year  => 2032,
128
    month => 12,
129
    day   => 25,
130
);
131
my $newyear = DateTime->new(
132
    year  => 2035,
133
    month => 1,
134
    day   => 1,
135
);
136
137
is( $koha_calendar->is_holiday($sunday),    1, 'Sunday is a closed day' );
138
is( $koha_calendar->is_holiday($monday),    0, 'Monday is not a closed day' );
139
is( $koha_calendar->is_holiday($christmas), 1, 'Christmas is a closed day' );
140
is( $koha_calendar->is_holiday($newyear),   1, 'New Years day is a closed day' );
141
142
$dbh->do("DELETE FROM repeatable_holidays");
143
$dbh->do("DELETE FROM special_holidays");
144
145
my $custom_holiday = DateTime->new(
146
    year  => 2013,
147
    month => 11,
148
    day   => 12,
149
);
150
151
my $today = dt_from_string();
152
C4::Calendar->new( branchcode => $branch_2 )->insert_single_holiday(
153
    day         => $today->day(),
154
    month       => $today->month(),
155
    year        => $today->year(),
156
    title       => "$today",
157
    description => "$today",
158
);
159
160
is( Koha::Calendar->new( branchcode => $branch_2 )->is_holiday( $today ), 1, "Today is a holiday for $branch_2" );
161
is( Koha::Calendar->new( branchcode => $branch_1 )->is_holiday( $today ), 0, "Today is not a holiday for $branch_1");
162
163
$schema->storage->txn_rollback;
164
165
subtest 'copy_to_branch' => sub {
166
167
    plan tests => 8;
168
169
    $schema->storage->txn_begin;
170
171
    my $branch1 = $builder->build( { source => 'Branch' } )->{ branchcode };
172
    my $calendar1 = C4::Calendar->new( branchcode => $branch1 );
173
    my $sunday = dt_from_string("2020-03-15");
174
    $calendar1->insert_week_day_holiday(
175
        weekday     => 0,
176
        title       => '',
177
        description => 'Sundays',
178
    );
179
180
    my $day_month = dt_from_string("2020-03-17");
181
    $calendar1->insert_day_month_holiday(
182
        day         => $day_month->day(),
183
        month       => $day_month->month(),
184
        year        => $day_month->year(),
185
        title       => '',
186
        description => "",
187
    );
188
189
    my $future_date = dt_from_string("9999-12-31");
190
    $calendar1->insert_single_holiday(
191
        day         => $future_date->day(),
192
        month       => $future_date->month(),
193
        year        => $future_date->year(),
194
        title       => "",
195
        description => "",
196
    );
197
198
    my $future_exception = dt_from_string("9999-12-30");
199
    $calendar1->insert_exception_holiday(
200
        day         => $future_exception->day(),
201
        month       => $future_exception->month(),
202
        year        => $future_exception->year(),
203
        title       => "",
204
        description => "",
205
    );
206
207
    my $past_date = dt_from_string("2019-11-20");
208
    $calendar1->insert_single_holiday(
209
        day         => $past_date->day(),
210
        month       => $past_date->month(),
211
        year        => $past_date->year(),
212
        title       => "",
213
        description => "",
214
    );
215
216
    my $past_exception = dt_from_string("2020-03-09");
217
    $calendar1->insert_exception_holiday(
218
        day         => $past_exception->day(),
219
        month       => $past_exception->month(),
220
        year        => $past_exception->year(),
221
        title       => "",
222
        description => "",
223
    );
224
225
    my $branch2 = $builder->build( { source => 'Branch' } )->{branchcode};
226
227
    C4::Calendar->new( branchcode => $branch1 )->copy_to_branch( $branch2 );
228
229
    my $calendar2 = C4::Calendar->new( branchcode => $branch2 );
230
    my $exceptions = $calendar2->get_exception_holidays;
231
232
    is( $calendar2->isHoliday( $sunday->day, $sunday->month, $sunday->year ), 1, "Weekday holiday copied to branch 2" );
233
    is( $calendar2->isHoliday( $day_month->day, $day_month->month, $day_month->year ), 1, "Day/month holiday copied to branch 2" );
234
    is( $calendar2->isHoliday( $future_date->day, $future_date->month, $future_date->year ), 1, "Single holiday copied to branch 2" );
235
    is( ( grep { $_->{date} eq "9999-12-30"} values %$exceptions ), 1, "Exception holiday copied to branch 2" );
236
    is( $calendar2->isHoliday( $past_date->day, $past_date->month, $past_date->year ), 0, "Don't copy past single holidays" );
237
    is( ( grep { $_->{date} eq "2020-03-09"} values %$exceptions ), 0, "Don't copy past exception holidays " );
238
239
    C4::Calendar->new( branchcode => $branch1 )->copy_to_branch( $branch2 );
240
241
    #Select all rows with same values from database
242
    my $dbh = C4::Context->dbh;
243
    my $get_repeatable_holidays = "SELECT a.* FROM repeatable_holidays a
244
        JOIN (SELECT branchcode, weekday, day, month, COUNT(*)
245
        FROM repeatable_holidays
246
        GROUP BY branchcode, weekday, day, month HAVING count(*) > 1) b
247
        ON a.branchcode = b.branchcode
248
        AND ( a.weekday = b.weekday OR (a.day = b.day AND a.month = b.month))
249
        ORDER BY a.branchcode;";
250
    my $sth  = $dbh->prepare($get_repeatable_holidays);
251
    $sth->execute;
252
253
    my @repeatable_holidays;
254
    while(my $row = $sth->fetchrow_hashref){
255
        push @repeatable_holidays, $row
256
    }
257
258
    is( scalar(@repeatable_holidays), 0, "None of the repeatable holidays were doubled");
259
260
    my $get_special_holidays = "SELECT a.* FROM special_holidays a
261
    JOIN (SELECT branchcode, day, month, year, isexception, COUNT(*)
262
    FROM special_holidays
263
    GROUP BY branchcode, day, month, year, isexception HAVING count(*) > 1) b
264
    ON a.branchcode = b.branchcode
265
    AND a.day = b.day AND a.month = b.month AND a.year = b.year AND a.isexception = b.isexception
266
    ORDER BY a.branchcode;";
267
    $sth  = $dbh->prepare($get_special_holidays);
268
    $sth->execute;
269
270
    my @special_holidays;
271
    while(my $row = $sth->fetchrow_hashref){
272
        push @special_holidays, $row
273
    }
274
275
    is( scalar(@special_holidays), 0, "None of the special holidays were doubled");
276
277
    $schema->storage->txn_rollback;
278
279
};
280
281
subtest 'with a library that is never open' => sub {
282
    plan tests => 2;
283
284
    $schema->storage->txn_begin;
285
286
    my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
287
    my $calendar   = C4::Calendar->new( branchcode => $branchcode );
288
    foreach my $weekday ( 0 .. 6 ) {
289
        $calendar->insert_week_day_holiday( weekday => $weekday, title => '', description => '' );
290
    }
291
292
    my $now = dt_from_string;
293
294
    subtest 'next_open_days should throw an exception' => sub {
295
        my $kcalendar = Koha::Calendar->new( branchcode => $branchcode, days_mode => 'Calendar' );
296
        throws_ok { $kcalendar->next_open_days( $now, 1 ) } 'Koha::Exceptions::Calendar::NoOpenDays';
297
    };
298
299
    subtest 'prev_open_days should throw an exception' => sub {
300
        my $kcalendar = Koha::Calendar->new( branchcode => $branchcode, days_mode => 'Calendar' );
301
        throws_ok { $kcalendar->prev_open_days( $now, 1 ) } 'Koha::Exceptions::Calendar::NoOpenDays';
302
    };
303
304
    $schema->storage->txn_rollback;
305
};
306
307
subtest 'with a library that is *almost* never open' => sub {
308
    plan tests => 2;
309
310
    $schema->storage->txn_begin;
311
312
    my $branchcode = $builder->build( { source => 'Branch' } )->{branchcode};
313
    my $calendar   = C4::Calendar->new( branchcode => $branchcode );
314
    foreach my $weekday ( 0 .. 6 ) {
315
        $calendar->insert_week_day_holiday( weekday => $weekday, title => '', description => '' );
316
    }
317
318
    my $now                    = dt_from_string;
319
    my $open_day_in_the_future = $now->clone()->add( years => 1 );
320
    my $open_day_in_the_past   = $now->clone()->subtract( years => 1 );
321
    $calendar->insert_exception_holiday( date => $open_day_in_the_future->ymd, title => '', description => '' );
322
    $calendar->insert_exception_holiday( date => $open_day_in_the_past->ymd,   title => '', description => '' );
323
324
    subtest 'next_open_days should find the open day' => sub {
325
        my $kcalendar     = Koha::Calendar->new( branchcode => $branchcode, days_mode => 'Calendar' );
326
        my $next_open_day = $kcalendar->next_open_days( $now, 1 );
327
        is( $next_open_day->ymd, $open_day_in_the_future->ymd );
328
    };
329
330
    subtest 'prev_open_days should find the open day' => sub {
331
        my $kcalendar     = Koha::Calendar->new( branchcode => $branchcode, days_mode => 'Calendar' );
332
        my $prev_open_day = $kcalendar->prev_open_days( $now, 1 );
333
        is( $prev_open_day->ymd, $open_day_in_the_past->ymd );
334
    };
335
336
    $schema->storage->txn_rollback;
337
};
338
339
# Clear cache
340
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 24-30 use Test::More tests => 15; Link Here
24
use t::lib::Mocks;
24
use t::lib::Mocks;
25
use t::lib::TestBuilder;
25
use t::lib::TestBuilder;
26
26
27
use C4::Calendar qw( new );
28
use Koha::Biblioitems;
27
use Koha::Biblioitems;
29
use Koha::Libraries;
28
use Koha::Libraries;
30
use Koha::Database;
29
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