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>.
20 use Test::More tests => 4;
22 use t::lib::TestBuilder;
28 use_ok('Koha::Calendar');
30 my $schema = Koha::Database->new->schema;
31 $schema->storage->txn_begin;
33 my $today = dt_from_string();
34 my $holiday_dt = $today->clone;
35 $holiday_dt->add(days => 3);
37 Koha::Caches->get_instance()->flush_all();
39 my $builder = t::lib::TestBuilder->new();
40 my $library = $builder->build_object({ class => 'Koha::Libraries' });
41 my $holiday = $builder->build(
43 source => 'SpecialHoliday',
45 branchcode => $library->branchcode,
46 day => $holiday_dt->day,
47 month => $holiday_dt->month,
48 year => $holiday_dt->year,
49 title => 'My holiday',
55 my $calendar = Koha::Calendar->new( branchcode => $library->branchcode );
57 subtest 'days_forward' => sub {
60 my $forwarded_dt = $calendar->days_forward( $today, 2 );
61 my $expected = $today->clone->add( days => 2 );
62 is( $forwarded_dt->ymd, $expected->ymd, 'With no holiday on the perioddays_forward should add 2 days' );
64 $forwarded_dt = $calendar->days_forward( $today, 5 );
65 $expected = $today->clone->add( days => 6 );
66 is( $forwarded_dt->ymd, $expected->ymd, 'With holiday on the perioddays_forward should add 5 days + 1 day for holiday'
69 $forwarded_dt = $calendar->days_forward( $today, 0 );
70 is( $forwarded_dt->ymd, $today->ymd, '0 day should return start dt' );
72 $forwarded_dt = $calendar->days_forward( $today, -2 );
73 is( $forwarded_dt->ymd, $today->ymd, 'negative day should return start dt' );
76 subtest 'crossing_DST' => sub {
80 my $tz = DateTime::TimeZone->new( name => 'America/New_York' );
81 my $start_date = dt_from_string( "2016-03-09 02:29:00", undef, $tz );
82 my $end_date = dt_from_string( "2017-01-01 00:00:00", undef, $tz );
83 my $days_between = $calendar->days_between( $start_date, $end_date );
84 is( $days_between->delta_days, 298, "Days calculated correctly" );
85 $days_between = $calendar->days_between( $end_date, $start_date );
86 is( $days_between->delta_days, 298, "Swapping returns the same" );
87 my $hours_between = $calendar->hours_between( $start_date, $end_date );
89 $hours_between->delta_minutes,
91 "Hours (in minutes) calculated correctly"
95 subtest 'hours_between' => sub {
100 # Su Mo Tu We Th Fr Sa
103 # 10 11 12 13 14 15 16
104 # 17 18 19 20 21 22 23
105 # 24 25 26 27 28 29 30
107 my $now = dt_from_string('2019-11-05 12:34:56'); # Today is 2019 Nov 5th
108 my $nov_6 = dt_from_string('2019-11-06 12:34:56');
109 my $nov_7 = dt_from_string('2019-11-07 12:34:56');
110 my $nov_12 = dt_from_string('2019-11-12 12:34:56');
111 my $nov_13 = dt_from_string('2019-11-13 12:34:56');
112 my $nov_15 = dt_from_string('2019-11-15 12:34:56');
113 Time::Fake->offset($now->epoch);
115 subtest 'No holiday' => sub {
119 my $library = $builder->build_object({ class => 'Koha::Libraries' });
120 my $calendar = Koha::Calendar->new( branchcode => $library->branchcode );
122 subtest 'Same hours' => sub {
126 # Between 5th and 6th
127 my $diff_hours = $calendar->hours_between( $now, $nov_6 )->hours;
128 is( $diff_hours, 1 * 24, '' );
130 # Between 5th and 7th
131 $diff_hours = $calendar->hours_between( $now, $nov_7 )->hours;
132 is( $diff_hours, 2 * 24, '' );
134 # Between 5th and 12th
135 $diff_hours = $calendar->hours_between( $now, $nov_12 )->hours;
136 is( $diff_hours, 7 * 24, '' );
138 # Between 5th and 15th
139 $diff_hours = $calendar->hours_between( $now, $nov_15 )->hours;
140 is( $diff_hours, 10 * 24, '' );
143 subtest 'Different hours' => sub {
147 # Between 5th and 6th
148 my $diff_hours = $calendar->hours_between( $now, $nov_6->clone->subtract(hours => 3) )->hours;
149 is( $diff_hours, 1 * 24 - 3, '' );
151 # Between 5th and 7th
152 $diff_hours = $calendar->hours_between( $now, $nov_7->clone->subtract(hours => 3) )->hours;
153 is( $diff_hours, 2 * 24 - 3, '' );
155 # Between 5th and 12th
156 $diff_hours = $calendar->hours_between( $now, $nov_12->clone->subtract(hours => 3) )->hours;
157 is( $diff_hours, 7 * 24 - 3, '' );
159 # Between 5th and 15th
160 $diff_hours = $calendar->hours_between( $now, $nov_15->clone->subtract(hours => 3) )->hours;
161 is( $diff_hours, 10 * 24 - 3, '' );
165 subtest 'With holiday' => sub {
168 my $library = $builder->build_object({ class => 'Koha::Libraries' });
170 # Wednesdays are closed
171 my $dbh = C4::Context->dbh;
173 INSERT INTO repeatable_holidays (branchcode,weekday,day,month,title,description)
174 VALUES ( ?, ?, NULL, NULL, ?, '' )
175 |, undef, $library->branchcode, 3, 'Closed on Wednesday');
177 my $calendar = Koha::Calendar->new( branchcode => $library->branchcode );
179 subtest 'Same hours' => sub {
184 # Between 5th and 6th
185 $diff_hours = $calendar->hours_between( $now, $nov_6 )->hours;
186 is( $diff_hours, 0 * 24, '' ); # FIXME Is this really should be 0?
188 # Between 5th and 7th
189 $diff_hours = $calendar->hours_between( $now, $nov_7 )->hours;
190 is( $diff_hours, 2 * 24 - 1 * 24, '' );
192 # Between 5th and 12th
193 $diff_hours = $calendar->hours_between( $now, $nov_12 )->hours;
194 is( $diff_hours, 7 * 24 - 1 * 24, '' );
196 # Between 5th and 13th
197 $diff_hours = $calendar->hours_between( $now, $nov_13 )->hours;
198 is( $diff_hours, 8 * 24 - 2 * 24, '' );
200 # Between 5th and 15th
201 $diff_hours = $calendar->hours_between( $now, $nov_15 )->hours;
202 is( $diff_hours, 10 * 24 - 2 * 24, '' );
205 subtest 'Different hours' => sub {
210 # Between 5th and 6th
211 my $duration = $calendar->hours_between( $now, $nov_6->clone->subtract(hours => 3) );
212 is( $duration->hours, abs(0 * 24 - 3), '' ); # FIXME $duration->hours always return a abs
213 is( $duration->is_negative, 1, ); # FIXME Do really test for that case in our calls to hours_between?
215 # Between 5th and 7th
216 $diff_hours = $calendar->hours_between( $now, $nov_7->clone->subtract(hours => 3) )->hours;
217 is( $diff_hours, 2 * 24 - 1 * 24 - 3, '' );
219 # Between 5th and 12th
220 $diff_hours = $calendar->hours_between( $now, $nov_12->clone->subtract(hours => 3) )->hours;
221 is( $diff_hours, 7 * 24 - 1 * 24 - 3, '' );
223 # Between 5th and 13th
224 $diff_hours = $calendar->hours_between( $now, $nov_13->clone->subtract(hours => 3) )->hours;
225 is( $diff_hours, 8 * 24 - 2 * 24 - 3, '' );
227 # Between 5th and 15th
228 $diff_hours = $calendar->hours_between( $now, $nov_15->clone->subtract(hours => 3) )->hours;
229 is( $diff_hours, 10 * 24 - 2 * 24 - 3, '' );
236 $schema->storage->txn_rollback();