Bug 23974: Improve readability
[koha.git] / t / db_dependent / Calendar.t
blob9788cf6d5ec3982f6015cc07b4790d223eab2d46
1 #!/usr/bin/perl
3 # This file is part of Koha.
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.
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.
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>.
18 use Modern::Perl;
20 use Test::More tests => 3;
21 use t::lib::TestBuilder;
23 use DateTime;
24 use Koha::Caches;
25 use Koha::DateUtils;
27 use_ok('Koha::Calendar');
29 my $schema = Koha::Database->new->schema;
30 $schema->storage->txn_begin;
32 my $today = dt_from_string();
33 my $holiday_dt = $today->clone;
34 $holiday_dt->add(days => 3);
36 Koha::Caches->get_instance()->flush_all();
38 my $builder = t::lib::TestBuilder->new();
39 my $library = $builder->build_object({ class => 'Koha::Libraries' });
40 my $holiday = $builder->build(
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
54 my $calendar = Koha::Calendar->new( branchcode => $library->branchcode );
56 subtest 'days_forward' => sub {
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' );
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'
68 $forwarded_dt = $calendar->days_forward( $today, 0 );
69 is( $forwarded_dt->ymd, $today->ymd, '0 day should return start dt' );
71 $forwarded_dt = $calendar->days_forward( $today, -2 );
72 is( $forwarded_dt->ymd, $today->ymd, 'negative day should return start dt' );
75 subtest 'crossing_DST' => sub {
77 plan tests => 3;
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"
94 $schema->storage->txn_rollback();