Bug 23974: Improve readability
[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 => 3;
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 => 3);
35
36 Koha::Caches->get_instance()->flush_all();
37
38 my $builder = t::lib::TestBuilder->new();
39 my $library = $builder->build_object({ class => 'Koha::Libraries' });
40 my $holiday = $builder->build(
41     {
42         source => 'SpecialHoliday',
43         value  => {
44             branchcode  => $library->branchcode,
45             day         => $holiday_dt->day,
46             month       => $holiday_dt->month,
47             year        => $holiday_dt->year,
48             title       => 'My holiday',
49             isexception => 0
50         },
51     }
52 );
53
54 my $calendar = Koha::Calendar->new( branchcode => $library->branchcode );
55
56 subtest 'days_forward' => sub {
57
58     plan tests => 4;
59     my $forwarded_dt = $calendar->days_forward( $today, 2 );
60     my $expected = $today->clone->add( days => 2 );
61     is( $forwarded_dt->ymd, $expected->ymd, 'With no holiday on the perioddays_forward should add 2 days' );
62
63     $forwarded_dt = $calendar->days_forward( $today, 5 );
64     $expected = $today->clone->add( days => 6 );
65     is( $forwarded_dt->ymd, $expected->ymd, 'With holiday on the perioddays_forward should add 5 days + 1 day for holiday'
66     );
67
68     $forwarded_dt = $calendar->days_forward( $today, 0 );
69     is( $forwarded_dt->ymd, $today->ymd, '0 day should return start dt' );
70
71     $forwarded_dt = $calendar->days_forward( $today, -2 );
72     is( $forwarded_dt->ymd, $today->ymd, 'negative day should return start dt' );
73 };
74
75 subtest 'crossing_DST' => sub {
76
77     plan tests => 3;
78
79     my $tz = DateTime::TimeZone->new( name => 'America/New_York' );
80     my $start_date = dt_from_string( "2016-03-09 02:29:00", undef, $tz );
81     my $end_date   = dt_from_string( "2017-01-01 00:00:00", undef, $tz );
82     my $days_between = $calendar->days_between( $start_date, $end_date );
83     is( $days_between->delta_days, 298, "Days calculated correctly" );
84     $days_between = $calendar->days_between( $end_date, $start_date );
85     is( $days_between->delta_days, 298, "Swapping returns the same" );
86     my $hours_between = $calendar->hours_between( $start_date, $end_date );
87     is(
88         $hours_between->delta_minutes,
89         298 * 24 * 60 - 149,
90         "Hours (in minutes) calculated correctly"
91     );
92 };
93
94 $schema->storage->txn_rollback();