Bug 9031: Unit tests for DST crossing in (days|hours)_between
[koha.git] / t / db_dependent / Calendar.t
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 t::lib::TestBuilder;
22
23 use DateTime;
24 use Koha::Caches;
25 use Koha::DateUtils;
26 use POSIX qw(tzset);
27
28 use_ok('Koha::Calendar');
29
30 my $schema  = Koha::Database->new->schema;
31 $schema->storage->txn_begin;
32
33 my $today = dt_from_string();
34 my $holiday_dt = $today->clone;
35 $holiday_dt->add(days => 15);
36
37 Koha::Caches->get_instance()->flush_all();
38
39 my $builder = t::lib::TestBuilder->new();
40 my $holiday = $builder->build({
41     source => 'SpecialHoliday',
42     value => {
43         branchcode => 'LIB1',
44         day => $holiday_dt->day,
45         month => $holiday_dt->month,
46         year => $holiday_dt->year,
47         title => 'My holiday',
48         isexception => 0
49     },
50 });
51
52 my $calendar = Koha::Calendar->new( branchcode => 'LIB1');
53 my $forwarded_dt = $calendar->days_forward($today, 10);
54
55 my $expected = $today->clone;
56 $expected->add(days => 10);
57 is($forwarded_dt->ymd, $expected->ymd, 'With no holiday on the perioddays_forward should add 10 days');
58
59 $forwarded_dt = $calendar->days_forward($today, 20);
60
61 $expected->add(days => 11);
62 is($forwarded_dt->ymd, $expected->ymd, 'With holiday on the perioddays_forward should add 20 days + 1 day for holiday');
63
64 $forwarded_dt = $calendar->days_forward($today, 0);
65 is($forwarded_dt->ymd, $today->ymd, '0 day should return start dt');
66
67 $forwarded_dt = $calendar->days_forward($today, -2);
68 is($forwarded_dt->ymd, $today->ymd, 'negative day should return start dt');
69
70 subtest 'crossing_DST' => sub {
71
72     plan tests => 2;
73
74     $ENV{TZ} = 'America/New_York';
75     tzset;
76     my $tz = DateTime::TimeZone->new( name => 'local' );
77     my $start_date = dt_from_string( "2016-03-09 02:29:00",undef,$tz );
78     my $end_date = dt_from_string("2017-01-01");
79     my $days_between = $calendar->days_between($start_date,$end_date);
80     is($days_between->{days}, 298, "Days calculated correctly");
81     my $hours_between = $calendar->hours_between($start_date,$end_date);
82     is($hours_between->{minutes}, 428671, "Hours (in minutes) calculated correctly");
83
84 };
85
86 $schema->storage->txn_rollback();