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 | days_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, 'hours: 1 day, no holiday' );
129 my $diff_days = $calendar->days_between( $now, $nov_6 )->delta_days;
130 is
( $diff_days, 1, 'days: 1 day, no holiday' );
132 # Between 5th and 7th
133 $diff_hours = $calendar->hours_between( $now, $nov_7 )->hours;
134 is
( $diff_hours, 2 * 24, 'hours: 2 days, no holiday' );
135 $diff_days = $calendar->days_between( $now, $nov_7 )->delta_days;
136 is
( $diff_days, 2, 'days: 2 days, no holiday' );
138 # Between 5th and 12th
139 $diff_hours = $calendar->hours_between( $now, $nov_12 )->hours;
140 is
( $diff_hours, 7 * 24, 'hours: 7 days, no holiday' );
141 $diff_days = $calendar->days_between( $now, $nov_12 )->delta_days;
142 is
( $diff_days, 7, 'days: 7 days, no holiday' );
144 # Between 5th and 15th
145 $diff_hours = $calendar->hours_between( $now, $nov_15 )->hours;
146 is
( $diff_hours, 10 * 24, 'hours: 10 days, no holiday' );
147 $diff_days = $calendar->days_between( $now, $nov_15 )->delta_days;
148 is
( $diff_days, 10, 'days: 10 days, no holiday' );
151 subtest
'Different hours' => sub {
155 # Between 5th and 5th (Same day short hours loan)
156 my $diff_hours = $calendar->hours_between( $now, $now->clone->add(hours
=> 3) )->hours;
157 is
( $diff_hours, 3, 'hours: 3 hours, no holidays' );
158 my $diff_days = $calendar->days_between( $now, $now->clone->add(hours
=> 3) )->delta_days;
159 is
( $diff_days, 0, 'days: 3 hours, no holidays' );
161 # Between 5th and 6th
162 $diff_hours = $calendar->hours_between( $now, $nov_6->clone->subtract(hours
=> 3) )->hours;
163 is
( $diff_hours, 1 * 24 - 3, 'hours: 21 hours, no holidays' );
164 $diff_days = $calendar->days_between( $now, $nov_6->clone->subtract(hours
=> 3) )->delta_days;
165 is
( $diff_days, 1, 'days: 21 hours, no holidays' );
167 # Between 5th and 7th
168 $diff_hours = $calendar->hours_between( $now, $nov_7->clone->subtract(hours
=> 3) )->hours;
169 is
( $diff_hours, 2 * 24 - 3, 'hours: 45 hours, no holidays' );
170 $diff_days = $calendar->days_between( $now, $nov_7->clone->subtract(hours
=> 3) )->delta_days;
171 is
( $diff_days, 2, 'days: 45 hours, no holidays' );
173 # Between 5th and 12th
174 $diff_hours = $calendar->hours_between( $now, $nov_12->clone->subtract(hours
=> 3) )->hours;
175 is
( $diff_hours, 7 * 24 - 3, 'hours: 165 hours, no holidays' );
176 $diff_days = $calendar->days_between( $now, $nov_12->clone->subtract(hours
=> 3) )->delta_days;
177 is
( $diff_days, 7, 'days: 165 hours, no holidays' );
179 # Between 5th and 15th
180 $diff_hours = $calendar->hours_between( $now, $nov_15->clone->subtract(hours
=> 3) )->hours;
181 is
( $diff_hours, 10 * 24 - 3, 'hours: 237 hours, no holidays' );
182 $diff_days = $calendar->days_between( $now, $nov_15->clone->subtract(hours
=> 3) )->delta_days;
183 is
( $diff_days, 10, 'days: 237 hours, no holidays' );
187 subtest
'With holiday' => sub {
190 my $library = $builder->build_object({ class => 'Koha::Libraries' });
192 # Wednesdays are closed
193 my $dbh = C4
::Context
->dbh;
195 INSERT INTO repeatable_holidays
(branchcode
,weekday
,day
,month
,title
,description
)
196 VALUES
( ?
, ?
, NULL
, NULL
, ?
, '' )
197 |, undef, $library->branchcode, 3, 'Closed on Wednesday');
199 my $calendar = Koha
::Calendar
->new( branchcode
=> $library->branchcode );
201 subtest
'Same hours' => sub {
204 my ( $diff_hours, $diff_days );
206 # Between 5th and 6th (This case should never happen in real code, one cannot return on a closed day)
207 $diff_hours = $calendar->hours_between( $now, $nov_6 )->hours;
208 is
( $diff_hours, 0 * 24, 'hours: 1 day, end_dt = holiday' ); # FIXME Is this really should be 0?
209 $diff_days = $calendar->days_between( $now, $nov_6)->delta_days;
210 is
( $diff_days, 0, 'days: 1 day, end_dt = holiday' ); # FIXME Is this really should be 0?
212 # Between 6th and 7th (This case should never happen in real code, one cannot issue on a closed day)
213 $diff_hours = $calendar->hours_between( $nov_6, $nov_7 )->hours;
214 is
( $diff_hours, 0 * 24, 'hours: 1 day, start_dt = holiday' ); # FIXME Is this really should be 0?
215 $diff_days = $calendar->days_between( $nov_6, $nov_7 )->delta_days;
216 is
( $diff_days, 0, 'days: 1 day, start_dt = holiday' ); # FIXME Is this really should be 0?
218 # Between 5th and 7th
219 $diff_hours = $calendar->hours_between( $now, $nov_7 )->hours;
220 is
( $diff_hours, 2 * 24 - 1 * 24, 'hours: 2 days, 1 holiday' );
221 $diff_days = $calendar->days_between( $now, $nov_7 )->delta_days;
222 is
( $diff_days, 2 - 1, 'days: 2 days, 1 holiday' );
224 # Between 5th and 12th
225 $diff_hours = $calendar->hours_between( $now, $nov_12 )->hours;
226 is
( $diff_hours, 7 * 24 - 1 * 24, 'hours: 7 days, 1 holiday' );
227 $diff_days = $calendar->days_between( $now, $nov_12)->delta_days;
228 is
( $diff_days, 7 - 1, 'day: 7 days, 1 holiday' );
230 # Between 5th and 13th
231 $diff_hours = $calendar->hours_between( $now, $nov_13 )->hours;
232 is
( $diff_hours, 8 * 24 - 2 * 24, 'hours: 8 days, 2 holidays' );
233 $diff_days = $calendar->days_between( $now, $nov_13)->delta_days;
234 is
( $diff_days, 8 - 2, 'days: 8 days, 2 holidays' );
236 # Between 5th and 15th
237 $diff_hours = $calendar->hours_between( $now, $nov_15 )->hours;
238 is
( $diff_hours, 10 * 24 - 2 * 24, 'hours: 10 days, 2 holidays' );
239 $diff_days = $calendar->days_between( $now, $nov_15)->delta_days;
240 is
( $diff_days, 10 - 2, 'days: 10 days, 2 holidays' );
243 subtest
'Different hours' => sub {
246 my ( $diff_hours, $diff_days );
248 # Between 5th and 5th (Same day short hours loan)
249 # No test - Tested above as 5th is an open day
251 # Between 5th and 6th (This case should never happen in real code, one cannot return on a closed day)
252 my $duration = $calendar->hours_between( $now, $nov_6->clone->subtract(hours
=> 3) );
253 is
( $duration->hours, abs(0 * 24 - 3), 'hours: 21 hours, end_dt = holiday' ); # FIXME $duration->hours always return a abs
254 is
( $duration->is_negative, 1, '? is negative ?' ); # FIXME Do really test for that case in our calls to hours_between?
255 $duration = $calendar->days_between( $now, $nov_6->clone->subtract(hours
=> 3) );
256 is
( $duration->hours, abs(0), 'days: 21 hours, end_dt = holiday' ); # FIXME Is this correct?
258 # Between 6th and 7th (This case should never happen in real code, one cannot issue on a closed day)
259 $duration = $calendar->hours_between( $nov_6, $nov_7->clone->subtract(hours
=> 3) );
260 is
( $duration->hours, abs(0 * 24 - 3), 'hours: 21 hours, start_dt = holiday' ); # FIXME $duration->hours always return a abs
261 is
( $duration->is_negative, 1, '? is negative ?' ); # FIXME Do really test for that case in our calls to hours_between?
262 $duration = $calendar->days_between( $nov_6, $nov_7->clone->subtract(hours
=> 3) );
263 is
( $duration->hours, abs(0), 'days: 21 hours, start_dt = holiday' ); # FIXME Is this correct?
265 # Between 5th and 7th
266 $diff_hours = $calendar->hours_between( $now, $nov_7->clone->subtract(hours
=> 3) )->hours;
267 is
( $diff_hours, 2 * 24 - 1 * 24 - 3, 'hours: 45 hours, 1 holiday' );
268 $diff_days = $calendar->days_between( $now, $nov_7->clone->subtract(hours
=> 3) )->delta_days;
269 is
( $diff_days, 2 - 1, 'days: 45 hours, 1 holiday' );
271 # Between 5th and 12th
272 $diff_hours = $calendar->hours_between( $now, $nov_12->clone->subtract(hours
=> 3) )->hours;
273 is
( $diff_hours, 7 * 24 - 1 * 24 - 3, 'hours: 165 hours, 1 holiday' );
274 $diff_days = $calendar->days_between( $now, $nov_12->clone->subtract(hours
=> 3) )->delta_days;
275 is
( $diff_days, 7 - 1, 'days: 165 hours, 1 holiday' );
277 # Between 5th and 13th
278 $diff_hours = $calendar->hours_between( $now, $nov_13->clone->subtract(hours
=> 3) )->hours;
279 is
( $diff_hours, 8 * 24 - 2 * 24 - 3, 'hours: 289 hours, 2 holidays ' );
280 $diff_days = $calendar->days_between( $now, $nov_13->clone->subtract(hours
=> 3) )->delta_days;
281 is
( $diff_days, 8 - 1, 'days: 289 hours, 2 holidays' );
283 # Between 5th and 15th
284 $diff_hours = $calendar->hours_between( $now, $nov_15->clone->subtract(hours
=> 3) )->hours;
285 is
( $diff_hours, 10 * 24 - 2 * 24 - 3, 'hours: 237 hours, 2 holidays' );
286 $diff_days = $calendar->days_between( $now, $nov_15->clone->subtract(hours
=> 3) )->delta_days;
287 is
( $diff_days, 10 - 2, 'days: 237 hours, 2 holidays' );
294 $schema->storage->txn_rollback();