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

(-)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 );
35
use Koha::DiscreteCalendar;
36
use C4::Circulation qw( AddIssue AddReturn CanBookBeRenewed GetIssuingCharges AddRenewal GetSoonestRenewDate GetLatestAutoRenewDate LostItem GetUpcomingDueIssues CanBookBeIssued AddIssuingCharge MarkIssueReturned ProcessOfflinePayment transferbook updateWrongTransfer );
36
use C4::Circulation qw( AddIssue AddReturn CanBookBeRenewed GetIssuingCharges AddRenewal GetSoonestRenewDate GetLatestAutoRenewDate LostItem GetUpcomingDueIssues CanBookBeIssued AddIssuingCharge MarkIssueReturned ProcessOfflinePayment transferbook updateWrongTransfer );
37
use C4::Biblio;
37
use C4::Biblio;
38
use C4::Items qw( ModItemTransfer );
38
use C4::Items qw( ModItemTransfer );
Lines 116-123 my $mocked_datetime = Test::MockModule->new('DateTime'); Link Here
116
$mocked_datetime->mock( 'now', sub { return $now_value->clone; } );
116
$mocked_datetime->mock( 'now', sub { return $now_value->clone; } );
117
117
118
my $cache = Koha::Caches->get_instance();
118
my $cache = Koha::Caches->get_instance();
119
$dbh->do(q|DELETE FROM special_holidays|);
119
$dbh->do(q|DELETE FROM discrete_calendar|);
120
$dbh->do(q|DELETE FROM repeatable_holidays|);
121
my $branches = Koha::Libraries->search();
120
my $branches = Koha::Libraries->search();
122
for my $branch ( $branches->next ) {
121
for my $branch ( $branches->next ) {
123
    my $key = $branch->branchcode . "_holidays";
122
    my $key = $branch->branchcode . "_holidays";
Lines 3167-3181 subtest 'AddReturn + suspension_chargeperiod' => sub { Link Here
3167
    t::lib::Mocks::mock_preference('SuspensionsCalendar', 'noSuspensionsWhenClosed');
3166
    t::lib::Mocks::mock_preference('SuspensionsCalendar', 'noSuspensionsWhenClosed');
3168
3167
3169
    # Adding a holiday 2 days ago
3168
    # Adding a holiday 2 days ago
3170
    my $calendar = C4::Calendar->new(branchcode => $library->{branchcode});
3169
    my $calendar = Koha::DiscreteCalendar->new({ branchcode => $library->{branchcode} });
3171
    my $two_days_ago = $now->clone->subtract( days => 2 );
3170
    my $two_days_ago = $now->clone->subtract( days => 2 );
3172
    $calendar->insert_single_holiday(
3171
    $calendar->edit_holiday({
3173
        day             => $two_days_ago->day,
3172
        holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
3174
        month           => $two_days_ago->month,
3173
        override        => 1,
3175
        year            => $two_days_ago->year,
3174
        start_date      => $two_days_ago,
3175
        end_date        => $two_days_ago,
3176
        title           => 'holidayTest-2d',
3176
        title           => 'holidayTest-2d',
3177
        description     => 'holidayDesc 2 days ago'
3177
        description     => 'holidayDesc 2 days ago'
3178
    );
3178
    });
3179
    # With 5 days of overdue, only 4 (x finedays=2) days must charged (one was an holiday)
3179
    # With 5 days of overdue, only 4 (x finedays=2) days must charged (one was an holiday)
3180
    $expected_expiration = $now->clone->add( days => floor( ( ( 5 - 0 - 1 ) / 1 ) * 2 ) );
3180
    $expected_expiration = $now->clone->add( days => floor( ( ( 5 - 0 - 1 ) / 1 ) * 2 ) );
3181
    test_debarment_on_checkout(
3181
    test_debarment_on_checkout(
Lines 3190-3202 subtest 'AddReturn + suspension_chargeperiod' => sub { Link Here
3190
3190
3191
    # Adding a holiday 2 days ahead, with finesCalendar=noFinesWhenClosed it should be skipped
3191
    # Adding a holiday 2 days ahead, with finesCalendar=noFinesWhenClosed it should be skipped
3192
    my $two_days_ahead = $now->clone->add( days => 2 );
3192
    my $two_days_ahead = $now->clone->add( days => 2 );
3193
    $calendar->insert_single_holiday(
3193
    $calendar->edit_holiday({
3194
        day             => $two_days_ahead->day,
3194
        holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
3195
        month           => $two_days_ahead->month,
3195
        start_date      => $two_days_ahead,
3196
        year            => $two_days_ahead->year,
3196
        end_date        => $two_days_ahead,
3197
        title           => 'holidayTest+2d',
3197
        title           => 'holidayTest+2d',
3198
        description     => 'holidayDesc 2 days ahead'
3198
        description     => 'holidayDesc 2 days ahead'
3199
    );
3199
    });
3200
3200
3201
    # Same as above, but we should skip D+2
3201
    # Same as above, but we should skip D+2
3202
    $expected_expiration = $now->clone->add( days => floor( ( ( 5 - 0 - 1 ) / 1 ) * 2 ) + 1 );
3202
    $expected_expiration = $now->clone->add( days => floor( ( ( 5 - 0 - 1 ) / 1 ) * 2 ) + 1 );
Lines 3212-3224 subtest 'AddReturn + suspension_chargeperiod' => sub { Link Here
3212
3212
3213
    # Adding another holiday, day of expiration date
3213
    # Adding another holiday, day of expiration date
3214
    my $expected_expiration_dt = dt_from_string($expected_expiration);
3214
    my $expected_expiration_dt = dt_from_string($expected_expiration);
3215
    $calendar->insert_single_holiday(
3215
    $calendar->edit_holiday({
3216
        day             => $expected_expiration_dt->day,
3216
        holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
3217
        month           => $expected_expiration_dt->month,
3217
        start_date      => $expected_expiration_dt,
3218
        year            => $expected_expiration_dt->year,
3218
        end_date        => $expected_expiration_dt,
3219
        title           => 'holidayTest_exp',
3219
        title           => 'holidayTest_exp',
3220
        description     => 'holidayDesc on expiration date'
3220
        description     => 'holidayDesc on expiration date'
3221
    );
3221
    });
3222
    # Expiration date will be the day after
3222
    # Expiration date will be the day after
3223
    test_debarment_on_checkout(
3223
    test_debarment_on_checkout(
3224
        {
3224
        {
Lines 3293-3298 subtest 'AddReturn + suspension_chargeperiod' => sub { Link Here
3293
    AddIssue( $patron, $item_1->barcode, $now->clone->subtract( days => 1 ) );
3293
    AddIssue( $patron, $item_1->barcode, $now->clone->subtract( days => 1 ) );
3294
    (undef, $message) = AddReturn( $item_1->barcode, $library->{branchcode}, undef, $now );
3294
    (undef, $message) = AddReturn( $item_1->barcode, $library->{branchcode}, undef, $now );
3295
    is( $message->{WasReturned} && exists $message->{PrevDebarred}, 1, 'Previously debarred message for Addreturn when overdue');
3295
    is( $message->{WasReturned} && exists $message->{PrevDebarred}, 1, 'Previously debarred message for Addreturn when overdue');
3296
3297
    # delete holidays
3298
    $calendar->edit_holiday({
3299
        holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{NONE},
3300
        start_date      => $two_days_ago,
3301
        end_date        => $expected_expiration_dt,
3302
        title           => '',
3303
        description     => ''
3304
    });
3296
};
3305
};
3297
3306
3298
subtest 'CanBookBeIssued + AutoReturnCheckedOutItems' => sub {
3307
subtest 'CanBookBeIssued + AutoReturnCheckedOutItems' => sub {
Lines 5271-5288 subtest 'Incremented fee tests' => sub { Link Here
5271
    $accountline->delete();
5280
    $accountline->delete();
5272
    $issue->delete();
5281
    $issue->delete();
5273
5282
5274
    my $calendar = C4::Calendar->new( branchcode => $library->id );
5283
    my $calendar = Koha::DiscreteCalendar->new({ branchcode => $library->id });
5275
    # DateTime 1..7 (Mon..Sun), C4::Calender 0..6 (Sun..Sat)
5284
    # DateTime 1..7 (Mon..Sun), Koha::DiscreteCalender 1..7 (Sun..Sat)
5276
    my $closed_day =
5285
    my $closed_day =
5277
        ( $dt_from->day_of_week == 6 ) ? 0
5286
        ( $dt_from->day_of_week == 7 ) ? 1
5278
      : ( $dt_from->day_of_week == 7 ) ? 1
5287
      : $dt_from->day_of_week + 1;
5279
      :                                  $dt_from->day_of_week + 1;
5280
    my $closed_day_name = $dt_from->clone->add(days => 1)->day_name;
5288
    my $closed_day_name = $dt_from->clone->add(days => 1)->day_name;
5281
    $calendar->insert_week_day_holiday(
5289
    $calendar->edit_holiday({
5290
        holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{WEEKLY},
5282
        weekday     => $closed_day,
5291
        weekday     => $closed_day,
5292
        start_date   => $dt_from,
5293
        end_date     => $dt_from,
5283
        title       => 'Test holiday',
5294
        title       => 'Test holiday',
5284
        description => 'Test holiday'
5295
        description => 'Test holiday'
5285
    );
5296
    });
5286
    $issue =
5297
    $issue =
5287
      AddIssue( $patron, $item->barcode, $dt_to, undef, $dt_from );
5298
      AddIssue( $patron, $item->barcode, $dt_to, undef, $dt_from );
5288
    $accountline = Koha::Account::Lines->find( { itemnumber => $item->id } );
5299
    $accountline = Koha::Account::Lines->find( { itemnumber => $item->id } );
Lines 5407-5413 subtest 'Incremented fee tests' => sub { Link Here
5407
    $accountline->delete();
5418
    $accountline->delete();
5408
    $issue->delete();
5419
    $issue->delete();
5409
5420
5410
    $calendar->delete_holiday( weekday => $closed_day );
5421
    $calendar->edit_holiday({
5422
        holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{NONE},
5423
        delete_type  => 1,
5424
        override     => 1,
5425
        weekday      => $closed_day,
5426
        start_date   => $dt_from,
5427
        end_date     => $dt_from,
5428
        title       => '',
5429
        description => '',
5430
    });
5431
5411
    $issue =
5432
    $issue =
5412
      AddIssue( $patron, $item->barcode, $dt_to, undef, $dt_from );
5433
      AddIssue( $patron, $item->barcode, $dt_to, undef, $dt_from );
5413
    $accountline = Koha::Account::Lines->find( { itemnumber => $item->id } );
5434
    $accountline = Koha::Account::Lines->find( { itemnumber => $item->id } );
(-)a/t/db_dependent/Circulation/CalcDateDue.t (-95 / +148 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::DiscreteCalendar;
12
use Koha::DateUtils qw( dt_from_string );
12
use Koha::DateUtils qw( dt_from_string );
13
use Koha::Library::Hours;
13
use Koha::Library::Hours;
14
use Koha::CirculationRules;
14
use Koha::CirculationRules;
Lines 38-43 my $issuelength = 10; Link Here
38
my $renewalperiod = 5;
38
my $renewalperiod = 5;
39
my $lengthunit = 'days';
39
my $lengthunit = 'days';
40
40
41
$builder->fill_discrete_calendar({ branchcode => $library->branchcode, start_date => $dateexpiry, days => 365 });
42
41
Koha::CirculationRules->search()->delete();
43
Koha::CirculationRules->search()->delete();
42
Koha::CirculationRules->set_rules(
44
Koha::CirculationRules->set_rules(
43
    {
45
    {
Lines 78-100 is($date, $dateexpiry . 'T23:59:00', 'date expiry with useDaysMode to noDays'); Link Here
78
80
79
# Let's add a special holiday on 2013-01-01. With ReturnBeforeExpiry and
81
# Let's add a special holiday on 2013-01-01. With ReturnBeforeExpiry and
80
# useDaysMode different from 'Days', return should forward the dateexpiry.
82
# useDaysMode different from 'Days', return should forward the dateexpiry.
81
my $calendar = C4::Calendar->new(branchcode => $branchcode);
83
my $calendar = Koha::DiscreteCalendar->new({ branchcode => $branchcode });
82
$calendar->insert_single_holiday(
84
$calendar->edit_holiday({
83
    day             => 1,
85
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
84
    month           => 1,
86
    override        => 1,
85
    year            => 2013,
87
    start_date      => dt_from_string($dateexpiry, 'iso'),
88
    end_date        => dt_from_string($dateexpiry, 'iso'),
86
    title           =>'holidayTest',
89
    title           =>'holidayTest',
87
    description     => 'holidayDesc'
90
    description     => 'holidayDesc'
88
);
91
});
89
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower );
92
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower );
90
is($date, '2012-12-31T23:59:00', 'date expiry should be 2013-01-01 -1 day');
93
is($date, '2012-12-31T23:59:00', 'date expiry should be 2013-01-01 -1 day');
91
$calendar->insert_single_holiday(
94
$calendar->edit_holiday({
92
    day             => 31,
95
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
93
    month           => 12,
96
    override        => 1,
94
    year            => 2012,
97
    start_date      => dt_from_string('2012-12-31', 'iso'),
98
    end_date        => dt_from_string('2012-12-31', 'iso'),
95
    title           =>'holidayTest',
99
    title           =>'holidayTest',
96
    description     => 'holidayDesc'
100
    description     => 'holidayDesc'
97
);
101
});
98
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower );
102
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower );
99
is($date, '2012-12-30T23:59:00', 'date expiry should be 2013-01-01 -2 day');
103
is($date, '2012-12-30T23:59:00', 'date expiry should be 2013-01-01 -2 day');
100
104
Lines 133-170 is($date, '2013-02-' . (9 + $renewalperiod) . 'T23:59:00', "useDaysMode = Daywee Link Here
133
# Now let's add a closed day on the expected renewal date, it should
137
# Now let's add a closed day on the expected renewal date, it should
134
# roll forward as per Datedue (i.e. one day at a time)
138
# roll forward as per Datedue (i.e. one day at a time)
135
# For issues...
139
# For issues...
136
$calendar->insert_single_holiday(
140
my $issue_date = dt_from_string('2013-02-' . (9 + $issuelength), 'iso');
137
    day             => 9 + $issuelength,
141
$calendar->edit_holiday({
138
    month           => 2,
142
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
139
    year            => 2013,
143
    override        => 1,
144
    start_date      => $issue_date,
145
    end_date        => $issue_date,
140
    title           =>'DayweekTest1',
146
    title           =>'DayweekTest1',
141
    description     => 'DayweekTest1'
147
    description     => 'DayweekTest1'
142
);
148
});
149
143
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower );
150
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower );
144
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 )");
151
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 )");
145
# Remove the holiday we just created
152
# Remove the holiday we just created
146
$calendar->delete_holiday(
153
$calendar->edit_holiday({
147
    day             => 9 + $issuelength,
154
    holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{NONE},
148
    month           => 2,
155
    override     => 1,
149
    year            => 2013
156
    start_date   => $issue_date,
150
);
157
    end_date     => $issue_date,
158
    title        => '',
159
    description  => '',
160
});
151
161
152
# ...and for renewals...
162
# ...and for renewals...
153
$calendar->insert_single_holiday(
163
my $renewal_date = dt_from_string('2013-02-' . (9 + $renewalperiod), 'iso');
154
    day             => 9 + $renewalperiod,
164
$calendar->edit_holiday({
155
    month           => 2,
165
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
156
    year            => 2013,
166
    override        => 1,
167
    start_date      => $renewal_date,
168
    end_date        => $renewal_date,
157
    title           =>'DayweekTest2',
169
    title           =>'DayweekTest2',
158
    description     => 'DayweekTest2'
170
    description     => 'DayweekTest2'
159
);
171
});
160
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower, 1 );
172
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $borrower, 1 );
161
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 )");
173
is($date, '2013-02-' . (9 + $renewalperiod + 1) . 'T23:59:00', "useDaysMode = Dayweek, closed on due date, 5 day renewal (should not trigger 7 day roll forward), renewal date expiry ( start + $renewalperiod  + 1 )");
162
# Remove the holiday we just created
174
# Remove the holiday we just created
163
$calendar->delete_holiday(
175
$calendar->edit_holiday({
164
    day             => 9 + $renewalperiod,
176
    title        => '',
165
    month           => 2,
177
    description  => '',
166
    year            => 2013,
178
    holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{NONE},
167
);
179
    start_date   => $renewal_date,
180
    end_date     => $renewal_date,
181
    override     => 1,
182
});
168
183
169
# Now we test it does the right thing if the loan and renewal periods
184
# Now we test it does the right thing if the loan and renewal periods
170
# are a multiple of 7 days
185
# are a multiple of 7 days
Lines 199-241 my $dayweek_borrower = $builder->build_object( Link Here
199
214
200
# For issues...
215
# For issues...
201
$start_date = DateTime->new({year => 2013, month => 2, day => 9});
216
$start_date = DateTime->new({year => 2013, month => 2, day => 9});
202
$calendar->insert_single_holiday(
217
my $holiday_date = $start_date->clone();
203
    day             => 9 + $dayweek_issuelength,
218
$holiday_date->add(days => $dayweek_issuelength);
204
    month           => 2,
219
$calendar->edit_holiday({
205
    year            => 2013,
220
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
221
    override        => 1,
222
    start_date      => $holiday_date,
223
    end_date        => $holiday_date,
206
    title           =>'DayweekTest3',
224
    title           =>'DayweekTest3',
207
    description     => 'DayweekTest3'
225
    description     => 'DayweekTest3'
208
);
226
});
209
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $dayweek_borrower );
227
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $dayweek_borrower );
210
my $issue_should_add = $dayweek_issuelength + 7;
228
my $issue_should_add = $dayweek_issuelength + 7;
211
my $dayweek_issue_expected = $start_date->add( days => $issue_should_add );
229
my $dayweek_issue_expected = $start_date->add( days => $issue_should_add );
212
is($date, $dayweek_issue_expected->strftime('%F') . 'T23:59:00', "useDaysMode = Dayweek, closed on due date, 14 day loan (should trigger 7 day roll forward), issue date expiry ( start + $issue_should_add )");
230
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 )");
213
# Remove the holiday we just created
231
# Remove the holiday we just created
214
$calendar->delete_holiday(
232
$calendar->edit_holiday({
215
    day             => 9 + $dayweek_issuelength,
233
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{NONE},
216
    month           => 2,
234
    override        => 1,
217
    year            => 2013,
235
    start_date      => $holiday_date,
218
);
236
    end_date        => $holiday_date,
237
    title           => '',
238
    description     => '',
239
});
219
240
220
# ...and for renewals...
241
# ...and for renewals...
221
$start_date = DateTime->new({year => 2013, month => 2, day => 9});
242
$start_date = DateTime->new({year => 2013, month => 2, day => 9});
222
$calendar->insert_single_holiday(
243
$holiday_date = $start_date->clone();
223
    day             => 9 + $dayweek_renewalperiod,
244
$holiday_date->add(days => $dayweek_renewalperiod);
224
    month           => 2,
245
$calendar->edit_holiday({
225
    year            => 2013,
246
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
247
    override        => 1,
248
    start_date      => $holiday_date,
249
    end_date        => $holiday_date,
226
    title           => 'DayweekTest4',
250
    title           => 'DayweekTest4',
227
    description     => 'DayweekTest4'
251
    description     => 'DayweekTest4'
228
);
252
});
229
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $dayweek_borrower, 1 );
253
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $dayweek_borrower, 1 );
230
my $renewal_should_add = $dayweek_renewalperiod + 7;
254
my $renewal_should_add = $dayweek_renewalperiod + 7;
231
my $dayweek_renewal_expected = $start_date->add( days => $renewal_should_add );
255
my $dayweek_renewal_expected = $start_date->add( days => $renewal_should_add );
232
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 )");
256
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 )");
233
# Remove the holiday we just created
257
# Remove the holiday we just created
234
$calendar->delete_holiday(
258
$calendar->edit_holiday({
235
    day             => 9 + $dayweek_renewalperiod,
259
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{NONE},
236
    month           => 2,
260
    override        => 1,
237
    year            => 2013,
261
    start_date      => $holiday_date,
238
);
262
    end_date        => $holiday_date,
263
    title           => '',
264
    description     => '',
265
});
239
266
240
# Now test it continues to roll forward by 7 days until it finds
267
# Now test it continues to roll forward by 7 days until it finds
241
# an open day, so we create a 3 week period of closed Saturdays
268
# an open day, so we create a 3 week period of closed Saturdays
Lines 243-271 $start_date = DateTime->new({year => 2013, month => 2, day => 9}); Link Here
243
my $expected_rolled_date = DateTime->new({year => 2013, month => 3, day => 9});
270
my $expected_rolled_date = DateTime->new({year => 2013, month => 3, day => 9});
244
my $holiday = $start_date->clone();
271
my $holiday = $start_date->clone();
245
$holiday->add(days => 7);
272
$holiday->add(days => 7);
246
$calendar->insert_single_holiday(
273
$calendar->edit_holiday({
247
    day             => $holiday->day,
274
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
248
    month           => $holiday->month,
275
    override        => 1,
249
    year            => 2013,
276
    start_date      => $holiday,
277
    end_date        => $holiday,
250
    title           =>'DayweekTest5',
278
    title           =>'DayweekTest5',
251
    description     => 'DayweekTest5'
279
    description     => 'DayweekTest5'
252
);
280
});
253
$holiday->add(days => 7);
281
$holiday->add(days => 7);
254
$calendar->insert_single_holiday(
282
$calendar->edit_holiday({
255
    day             => $holiday->day,
283
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
256
    month           => $holiday->month,
284
    override        => 1,
257
    year            => 2013,
285
    start_date      => $holiday,
286
    end_date        => $holiday,
258
    title           =>'DayweekTest6',
287
    title           =>'DayweekTest6',
259
    description     => 'DayweekTest6'
288
    description     => 'DayweekTest6'
260
);
289
});
261
$holiday->add(days => 7);
290
$holiday->add(days => 7);
262
$calendar->insert_single_holiday(
291
$calendar->edit_holiday({
263
    day             => $holiday->day,
292
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
264
    month           => $holiday->month,
293
    override        => 1,
265
    year            => 2013,
294
    start_date      => $holiday,
295
    end_date        => $holiday,
266
    title           =>'DayweekTest7',
296
    title           =>'DayweekTest7',
267
    description     => 'DayweekTest7'
297
    description     => 'DayweekTest7'
268
);
298
});
269
# For issues...
299
# For issues...
270
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $dayweek_borrower );
300
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $dayweek_borrower );
271
$dayweek_issue_expected = $start_date->add( days => $issue_should_add );
301
$dayweek_issue_expected = $start_date->add( days => $issue_should_add );
Lines 279-311 is($date, $expected_rolled_date->strftime('%F') . 'T23:59:00', "useDaysMode = Da Link Here
279
$start_date = DateTime->new({year => 2013, month => 2, day => 9});
309
$start_date = DateTime->new({year => 2013, month => 2, day => 9});
280
my $del_holiday = $start_date->clone();
310
my $del_holiday = $start_date->clone();
281
$del_holiday->add(days => 7);
311
$del_holiday->add(days => 7);
282
$calendar->delete_holiday(
312
$calendar->edit_holiday({
283
    day             => $del_holiday->day,
313
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{NONE},
284
    month           => $del_holiday->month,
314
    override        => 1,
285
    year            => 2013
315
    start_date      => $del_holiday,
286
);
316
    end_date        => $del_holiday,
317
    title           => '',
318
    description     => '',
319
});
287
$del_holiday->add(days => 7);
320
$del_holiday->add(days => 7);
288
$calendar->delete_holiday(
321
$calendar->edit_holiday({
289
    day             => $del_holiday->day,
322
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{NONE},
290
    month           => $del_holiday->month,
323
    override        => 1,
291
    year            => 2013
324
    start_date      => $del_holiday,
292
);
325
    end_date        => $del_holiday,
326
    title           => '',
327
    description     => '',
328
});
293
$del_holiday->add(days => 7);
329
$del_holiday->add(days => 7);
294
$calendar->delete_holiday(
330
$calendar->edit_holiday({
295
    day             => $del_holiday->day,
331
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{NONE},
296
    month           => $del_holiday->month,
332
    override        => 1,
297
    year            => 2013
333
    start_date      => $del_holiday,
298
);
334
    end_date        => $del_holiday,
335
    title           => '',
336
    description     => '',
337
});
299
338
300
# Now test that useDaysMode "Dayweek" doesn't try to roll forward onto
339
# Now test that useDaysMode "Dayweek" doesn't try to roll forward onto
301
# a permanently closed day and instead rolls forward just one day
340
# a permanently closed day and instead rolls forward just one day
302
$start_date = DateTime->new({year => 2013, month => 2, day => 9});
341
$start_date = DateTime->new({year => 2013, month => 2, day => 9});
303
# Our tests are concerned with Saturdays, so let's close on Saturdays
342
# Our tests are concerned with Saturdays, so let's close on Saturdays
304
$calendar->insert_week_day_holiday(
343
$calendar->edit_holiday({
305
    weekday => 6,
344
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{WEEKLY},
345
    weekday         => 7,
346
    override        => 1,
347
    start_date      => $start_date,
348
    end_date        => $start_date,
306
    title => "Saturday closure",
349
    title => "Saturday closure",
307
    description => "Closed on Saturdays"
350
    description => "Closed on Saturdays"
308
);
351
});
309
# For issues...
352
# For issues...
310
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $dayweek_borrower );
353
$date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $dayweek_borrower );
311
$dayweek_issue_expected = $start_date->add( days => $dayweek_issuelength + 1 );
354
$dayweek_issue_expected = $start_date->add( days => $dayweek_issuelength + 1 );
Lines 316-324 $date = C4::Circulation::CalcDateDue( $start_date, $itemtype, $branchcode, $dayw Link Here
316
$dayweek_renewal_expected = $start_date->add( days => $dayweek_renewalperiod + 1 );
359
$dayweek_renewal_expected = $start_date->add( days => $dayweek_renewalperiod + 1 );
317
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 )");
360
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 )");
318
# Remove the holiday we just created
361
# Remove the holiday we just created
319
$calendar->delete_holiday(
362
$calendar->edit_holiday({
320
    weekday => 6
363
    holiday_type    => $Koha::DiscreteCalendar::HOLIDAYS->{NONE},
321
);
364
    weekday         => 7,
365
    override        => 1,
366
    delete_type     => 1,
367
    start_date      => $start_date,
368
    end_date        => $start_date,
369
    title           => "",
370
    description     => ""
371
});
322
372
323
# Renewal period of 0 is valid
373
# Renewal period of 0 is valid
324
Koha::CirculationRules->search()->delete();
374
Koha::CirculationRules->search()->delete();
Lines 377-382 my $open = DateTime->new( year => 2023, month => 5, day => 1, hour => 10 )->hms Link Here
377
my $close = DateTime->new( year => 2023, month => 5, day => 1, hour => 16 )->hms;
427
my $close = DateTime->new( year => 2023, month => 5, day => 1, hour => 16 )->hms;
378
my $now   = DateTime->new( year => 2023, month => 5, day => 1, hour => 14 );
428
my $now   = DateTime->new( year => 2023, month => 5, day => 1, hour => 14 );
379
429
430
$builder->fill_discrete_calendar({ branchcode => $library1->{branchcode}, start_date => $now });
431
380
foreach ( 0 .. 6 ) {
432
foreach ( 0 .. 6 ) {
381
433
382
    # library opened 4 hours ago and closes in 2 hours.
434
    # library opened 4 hours ago and closes in 2 hours.
Lines 411-424 is( Link Here
411
my $holiday_tomorrow = $now->clone->add( days => 1 );
463
my $holiday_tomorrow = $now->clone->add( days => 1 );
412
464
413
# consider calendar
465
# consider calendar
414
my $library1_calendar = C4::Calendar->new( branchcode => $library1->{branchcode} );
466
my $library1_calendar = Koha::DiscreteCalendar->new({ branchcode => $library1->{branchcode} });
415
$library1_calendar->insert_single_holiday(
467
$library1_calendar->edit_holiday({
416
    day         => $holiday_tomorrow->day,
468
    holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
417
    month       => $holiday_tomorrow->month,
469
    start_date  => $holiday_tomorrow,
418
    year        => $holiday_tomorrow->year,
470
    end_date    => $holiday_tomorrow,
419
    title       => 'testholiday',
471
    title       => 'testholiday',
420
    description => 'testholiday'
472
    description => 'testholiday',
421
);
473
    override    => 1,
474
});
422
Koha::CirculationRules->set_rules(
475
Koha::CirculationRules->set_rules(
423
    {
476
    {
424
        categorycode => $patron_category->categorycode,
477
        categorycode => $patron_category->categorycode,
(-)a/t/db_dependent/Circulation/CalcFine.t (+1 lines)
Lines 28-33 my $branch = $builder->build( Link Here
28
        source => 'Branch',
28
        source => 'Branch',
29
    }
29
    }
30
);
30
);
31
$builder->fill_discrete_calendar({ branchcode => $branch->{branchcode}, start_date => '2000-01-01' });
31
32
32
my $category = $builder->build(
33
my $category = $builder->build(
33
    {
34
    {
(-)a/t/db_dependent/Circulation/maxsuspensiondays.t (-5 / +10 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
Lines 126-131 subtest "suspension_chargeperiod" => sub { Link Here
126
    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
125
    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
127
    my $item = $builder->build_sample_item;
126
    my $item = $builder->build_sample_item;
128
127
128
    $builder->fill_discrete_calendar({ branchcode => $patron->{branchcode}, days => 365 });
129
    $builder->fill_discrete_calendar({ branchcode => $item->homebranch, days => 365 });
130
129
    my $last_year = dt_from_string->clone->subtract( years => 1 );
131
    my $last_year = dt_from_string->clone->subtract( years => 1 );
130
    my $today = dt_from_string;
132
    my $today = dt_from_string;
131
    my $new_debar_dt = C4::Circulation::_calculate_new_debar_dt( $patron, $item, $last_year, $today );
133
    my $new_debar_dt = C4::Circulation::_calculate_new_debar_dt( $patron, $item, $last_year, $today );
Lines 152-157 subtest "maxsuspensiondays" => sub { Link Here
152
    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
154
    my $patron = $builder->build_object({ class => 'Koha::Patrons' });
153
    my $item = $builder->build_sample_item;
155
    my $item = $builder->build_sample_item;
154
156
157
    $builder->fill_discrete_calendar({ branchcode => $patron->{branchcode}, days => 365 });
158
    $builder->fill_discrete_calendar({ branchcode => $item->homebranch, days => 365 });
159
155
    my $last_year = dt_from_string->clone->subtract( years => 1 );
160
    my $last_year = dt_from_string->clone->subtract( years => 1 );
156
    my $today = dt_from_string;
161
    my $today = dt_from_string;
157
    my $new_debar_dt = C4::Circulation::_calculate_new_debar_dt( $patron, $item, $last_year, $today );
162
    my $new_debar_dt = C4::Circulation::_calculate_new_debar_dt( $patron, $item, $last_year, $today );
(-)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/Fines.t (+5 lines)
Lines 7-12 use C4::Overdues qw( CalcFine ); Link Here
7
use Koha::Database;
7
use Koha::Database;
8
use Koha::DateUtils qw( dt_from_string );
8
use Koha::DateUtils qw( dt_from_string );
9
9
10
use t::lib::TestBuilder;
10
use Test::More tests => 5;
11
use Test::More tests => 5;
11
12
12
my $schema = Koha::Database->new->schema;
13
my $schema = Koha::Database->new->schema;
Lines 31-36 my $issuingrule = Koha::CirculationRules->set_rules( Link Here
31
    }
32
    }
32
);
33
);
33
34
35
my $builder = t::lib::TestBuilder->new();
36
$builder->fill_discrete_calendar({ branchcode => '' });
37
$builder->fill_discrete_calendar({ branchcode => '', start_date => '2000-01-01' });
38
34
ok( $issuingrule, 'Issuing rule created' );
39
ok( $issuingrule, 'Issuing rule created' );
35
40
36
my $period_start = dt_from_string('2000-01-01');
41
my $period_start = dt_from_string('2000-01-01');
(-)a/t/db_dependent/Hold.t (-3 / +12 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
$builder->fill_discrete_calendar({ branchcode => $branches[1]->{branchcode}, start_date => '2017-01-01', days => dt_from_string->delta_days( dt_from_string( '2017-01-01' ) )->in_units( 'days') });
81
$b1_cal->insert_single_holiday( day => 2, month => 1, year => 2017, title => "Morty Day", description => "Rick" ); #Add a holiday
81
my $b1_cal = Koha::DiscreteCalendar->new({ branchcode => $branches[1]->{branchcode} });
82
my $holiday = dt_from_string('2017-01-02', 'iso');
83
$b1_cal->edit_holiday({
84
    holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
85
    override     => 1,
86
    start_date   => $holiday,
87
    end_date     => $holiday,
88
    title => "Morty Day",
89
    description => "Rick",
90
}); #Add a holiday
82
my $today = dt_from_string;
91
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");
92
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");
93
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 MoveReserve );
17
use C4::Reserves qw( AddReserve CalculatePriority ModReserve ToggleSuspend AutoUnsuspendReserves SuspendAll ModReserveMinusPriority AlterPriority CanItemBeReserved CheckReserves MoveReserve );
19
use C4::Circulation qw( CanBookBeRenewed );
18
use C4::Circulation qw( CanBookBeRenewed );
(-)a/t/db_dependent/Holds/WaitingReserves.t (-22 / +14 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
$builder->fill_discrete_calendar({ branchcode => 'LIB1' });
39
my $calendar = Koha::DiscreteCalendar->new({ branchcode => 'LIB1' });
40
39
$builder->build({
41
$builder->build({
40
    source => 'Branch',
42
    source => 'Branch',
41
    value  => {
43
    value  => {
Lines 153-183 my $reserve3 = $builder->build({ Link Here
153
my $special_holiday1_dt = $today->clone;
155
my $special_holiday1_dt = $today->clone;
154
$special_holiday1_dt->add(days => 2);
156
$special_holiday1_dt->add(days => 2);
155
157
156
my $holiday = $builder->build({
158
$calendar->edit_holiday({
157
    source => 'SpecialHoliday',
159
    title => "My special holiday",
158
    value => {
160
    holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
159
        branchcode => 'LIB1',
161
    start_date => $special_holiday1_dt,
160
        day => $special_holiday1_dt->day,
162
    end_date => $special_holiday1_dt,
161
        month => $special_holiday1_dt->month,
162
        year => $special_holiday1_dt->year,
163
        title => 'My special holiday',
164
        isexception => 0
165
    },
166
});
163
});
167
164
168
my $special_holiday2_dt = $today->clone;
165
my $special_holiday2_dt = $today->clone;
169
$special_holiday2_dt->add(days => 4);
166
$special_holiday2_dt->add(days => 4);
170
167
171
my $holiday2 = $builder->build({
168
$calendar->edit_holiday({
172
    source => 'SpecialHoliday',
169
    title => "My special holiday 2",
173
    value => {
170
    holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
174
        branchcode => 'LIB1',
171
    start_date => $special_holiday2_dt,
175
        day => $special_holiday2_dt->day,
172
    end_date => $special_holiday2_dt,
176
        month => $special_holiday2_dt->month,
177
        year => $special_holiday2_dt->year,
178
        title => 'My special holiday 2',
179
        isexception => 0
180
    },
181
});
173
});
182
174
183
Koha::Caches->get_instance->flush_all;
175
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 => 64;
11
use Test::More tests => 64;
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 / +30 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
        today        => $dt_from,           # To override past date when deleting holiday type
437
        delete_type  => 1,
438
        start_date   => $dt_from,
439
        end_date     => $dt_from,
440
        title        => '',
441
        description  => '',
442
    });
430
    $charge = $fees->accumulate_rentalcharge();
443
    $charge = $fees->accumulate_rentalcharge();
431
    is( $charge, 24.00, 'Hourly rental charge calculated correctly with finesCalendar = noFinesWhenClosed (96h - 0h * 0.25u)' );
444
    is( $charge, 24.00, 'Hourly rental charge calculated correctly with finesCalendar = noFinesWhenClosed (96h - 0h * 0.25u)' );
432
};
445
};
(-)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 / +6 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
    $builder->fill_discrete_calendar({ branchcode => 'LIB1 '});
85
        source => 'SpecialHoliday',
85
    my $calendar = Koha::DiscreteCalendar->new({ branchcode => 'LIB1' })->edit_holiday({
86
        value => {
86
        title => "My holiday",
87
            branchcode => 'LIB1',
87
        holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION},
88
            day => $today->day,
88
        start_date => $today,
89
            month => $today->month,
89
        end_date => $today,
90
            year => $today->year,
91
            title => 'My holiday',
92
            isexception => 0
93
        },
94
    });
90
    });
95
91
96
    CancelExpiredReserves();
92
    CancelExpiredReserves();
(-)a/t/lib/TestBuilder.pm (-2 / +31 lines)
Lines 6-12 use Koha::Database qw( schema ); Link Here
6
use C4::Biblio qw( AddBiblio );
6
use C4::Biblio qw( AddBiblio );
7
use Koha::Biblios qw( _type );
7
use Koha::Biblios qw( _type );
8
use Koha::Items qw( _type );
8
use Koha::Items qw( _type );
9
use Koha::DateUtils qw( dt_from_string );
9
use Koha::DateUtils qw( dt_from_string output_pref );
10
10
11
use Bytes::Random::Secure;
11
use Bytes::Random::Secure;
12
use Carp qw( carp );
12
use Carp qw( carp );
Lines 146-151 sub build { Link Here
146
          if scalar @{ $fk->{keys} } == 1
146
          if scalar @{ $fk->{keys} } == 1
147
    }
147
    }
148
148
149
    if ($source eq 'Branch') {
150
        $self->fill_discrete_calendar({ branchcode => $col_values->{branchcode} });
151
    }
152
149
    # store this record and return hashref
153
    # store this record and return hashref
150
    return $self->_storeColumnValues({
154
    return $self->_storeColumnValues({
151
        source => $source,
155
        source => $source,
Lines 210-215 sub build_sample_item { Link Here
210
    )->store->get_from_storage;
214
    )->store->get_from_storage;
211
}
215
}
212
216
217
sub fill_discrete_calendar {
218
    my ( $self, $args ) = @_;
219
220
    my $branchcode = $args->{branchcode} || '';
221
    my $start_date = ($args->{start_date}) ? dt_from_string($args->{start_date}) : DateTime->today();
222
    my $days = $args->{days} || 60;
223
224
    my $end_date = $start_date->clone();
225
    $start_date->add(days => 0 - $days);
226
    $end_date->add(days => $days);
227
228
    for (1; $start_date <= $end_date; $start_date->add(days => 1)) {
229
        my $data = {
230
            date       => output_pref( { dt => $start_date, dateformat => 'iso', dateonly => 1 }),
231
            branchcode => $branchcode,
232
            is_opened  => 1,
233
            open_hour  => "08:00:00",
234
            close_hour => "17:00:00",
235
        };
236
237
        $self->schema->resultset( "DiscreteCalendar" )->update_or_create( $data );
238
    }
239
240
    return
241
}
242
213
# ------------------------------------------------------------------------------
243
# ------------------------------------------------------------------------------
214
# Internal helper routines
244
# Internal helper routines
215
245
216
- 

Return to bug 17015