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