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 => 6;
24 use t::lib::TestBuilder;
30 use_ok('Koha::Calendar');
32 my $schema = Koha::Database->new->schema;
33 $schema->storage->txn_begin;
35 my $today = dt_from_string();
36 my $holiday_dt = $today->clone;
37 $holiday_dt->add(days => 3);
39 Koha::Caches->get_instance()->flush_all();
41 my $builder = t::lib::TestBuilder->new();
42 my $library = $builder->build_object({ class => 'Koha::Libraries' });
43 my $holiday = $builder->build(
45 source => 'SpecialHoliday',
47 branchcode => $library->branchcode,
48 day => $holiday_dt->day,
49 month => $holiday_dt->month,
50 year => $holiday_dt->year,
51 title => 'My holiday',
57 t::lib::Mocks::mock_preference('useDaysMode', 'Calendar');
58 my $calendar = Koha::Calendar->new( branchcode => $library->branchcode );
60 subtest 'days_forward' => sub {
63 my $forwarded_dt = $calendar->days_forward( $today, 2 );
64 my $expected = $today->clone->add( days => 2 );
65 is( $forwarded_dt->ymd, $expected->ymd, 'With no holiday on the perioddays_forward should add 2 days' );
67 $forwarded_dt = $calendar->days_forward( $today, 5 );
68 $expected = $today->clone->add( days => 6 );
69 is( $forwarded_dt->ymd, $expected->ymd, 'With holiday on the perioddays_forward should add 5 days + 1 day for holiday'
72 $forwarded_dt = $calendar->days_forward( $today, 0 );
73 is( $forwarded_dt->ymd, $today->ymd, '0 day should return start dt' );
75 $forwarded_dt = $calendar->days_forward( $today, -2 );
76 is( $forwarded_dt->ymd, $today->ymd, 'negative day should return start dt' );
79 subtest 'crossing_DST' => sub {
83 my $tz = DateTime::TimeZone->new( name => 'America/New_York' );
84 my $start_date = dt_from_string( "2016-03-09 02:29:00", undef, $tz );
85 my $end_date = dt_from_string( "2017-01-01 00:00:00", undef, $tz );
86 my $days_between = $calendar->days_between( $start_date, $end_date );
87 is( $days_between->delta_days, 298, "Days calculated correctly" );
88 $days_between = $calendar->days_between( $end_date, $start_date );
89 is( $days_between->delta_days, 298, "Swapping returns the same" );
90 my $hours_between = $calendar->hours_between( $start_date, $end_date );
92 $hours_between->delta_minutes,
94 "Hours (in minutes) calculated correctly"
98 subtest 'hours_between | days_between' => sub {
103 # Su Mo Tu We Th Fr Sa
106 # 10 11 12 13 14 15 16
107 # 17 18 19 20 21 22 23
108 # 24 25 26 27 28 29 30
110 my $now = dt_from_string('2019-11-05 12:34:56'); # Today is 2019 Nov 5th
111 my $nov_6 = dt_from_string('2019-11-06 12:34:56');
112 my $nov_7 = dt_from_string('2019-11-07 12:34:56');
113 my $nov_12 = dt_from_string('2019-11-12 12:34:56');
114 my $nov_13 = dt_from_string('2019-11-13 12:34:56');
115 my $nov_15 = dt_from_string('2019-11-15 12:34:56');
116 Time::Fake->offset($now->epoch);
118 subtest 'No holiday' => sub {
122 my $library = $builder->build_object({ class => 'Koha::Libraries' });
123 my $calendar = Koha::Calendar->new( branchcode => $library->branchcode );
125 subtest 'Same hours' => sub {
129 # Between 5th and 6th
130 my $diff_hours = $calendar->hours_between( $now, $nov_6 )->hours;
131 is( $diff_hours, 1 * 24, 'hours: 1 day, no holiday' );
132 my $diff_days = $calendar->days_between( $now, $nov_6 )->delta_days;
133 is( $diff_days, 1, 'days: 1 day, no holiday' );
135 # Between 5th and 7th
136 $diff_hours = $calendar->hours_between( $now, $nov_7 )->hours;
137 is( $diff_hours, 2 * 24, 'hours: 2 days, no holiday' );
138 $diff_days = $calendar->days_between( $now, $nov_7 )->delta_days;
139 is( $diff_days, 2, 'days: 2 days, no holiday' );
141 # Between 5th and 12th
142 $diff_hours = $calendar->hours_between( $now, $nov_12 )->hours;
143 is( $diff_hours, 7 * 24, 'hours: 7 days, no holiday' );
144 $diff_days = $calendar->days_between( $now, $nov_12 )->delta_days;
145 is( $diff_days, 7, 'days: 7 days, no holiday' );
147 # Between 5th and 15th
148 $diff_hours = $calendar->hours_between( $now, $nov_15 )->hours;
149 is( $diff_hours, 10 * 24, 'hours: 10 days, no holiday' );
150 $diff_days = $calendar->days_between( $now, $nov_15 )->delta_days;
151 is( $diff_days, 10, 'days: 10 days, no holiday' );
154 subtest 'Different hours' => sub {
158 # Between 5th and 5th (Same day short hours loan)
159 my $diff_hours = $calendar->hours_between( $now, $now->clone->add(hours => 3) )->hours;
160 is( $diff_hours, 3, 'hours: 3 hours, no holidays' );
161 my $diff_days = $calendar->days_between( $now, $now->clone->add(hours => 3) )->delta_days;
162 is( $diff_days, 0, 'days: 3 hours, no holidays' );
164 # Between 5th and 6th
165 $diff_hours = $calendar->hours_between( $now, $nov_6->clone->subtract(hours => 3) )->hours;
166 is( $diff_hours, 1 * 24 - 3, 'hours: 21 hours, no holidays' );
167 $diff_days = $calendar->days_between( $now, $nov_6->clone->subtract(hours => 3) )->delta_days;
168 is( $diff_days, 1, 'days: 21 hours, no holidays' );
170 # Between 5th and 7th
171 $diff_hours = $calendar->hours_between( $now, $nov_7->clone->subtract(hours => 3) )->hours;
172 is( $diff_hours, 2 * 24 - 3, 'hours: 45 hours, no holidays' );
173 $diff_days = $calendar->days_between( $now, $nov_7->clone->subtract(hours => 3) )->delta_days;
174 is( $diff_days, 2, 'days: 45 hours, no holidays' );
176 # Between 5th and 12th
177 $diff_hours = $calendar->hours_between( $now, $nov_12->clone->subtract(hours => 3) )->hours;
178 is( $diff_hours, 7 * 24 - 3, 'hours: 165 hours, no holidays' );
179 $diff_days = $calendar->days_between( $now, $nov_12->clone->subtract(hours => 3) )->delta_days;
180 is( $diff_days, 7, 'days: 165 hours, no holidays' );
182 # Between 5th and 15th
183 $diff_hours = $calendar->hours_between( $now, $nov_15->clone->subtract(hours => 3) )->hours;
184 is( $diff_hours, 10 * 24 - 3, 'hours: 237 hours, no holidays' );
185 $diff_days = $calendar->days_between( $now, $nov_15->clone->subtract(hours => 3) )->delta_days;
186 is( $diff_days, 10, 'days: 237 hours, no holidays' );
190 subtest 'With holiday' => sub {
193 my $library = $builder->build_object({ class => 'Koha::Libraries' });
195 # Wednesdays are closed
196 my $dbh = C4::Context->dbh;
198 INSERT INTO repeatable_holidays (branchcode,weekday,day,month,title,description)
199 VALUES ( ?, ?, NULL, NULL, ?, '' )
200 |, undef, $library->branchcode, 3, 'Closed on Wednesday');
202 my $calendar = Koha::Calendar->new( branchcode => $library->branchcode );
204 subtest 'Same hours' => sub {
207 my ( $diff_hours, $diff_days );
209 # Between 5th and 6th (This case should never happen in real code, one cannot return on a closed day)
210 $diff_hours = $calendar->hours_between( $now, $nov_6 )->hours;
211 is( $diff_hours, 0 * 24, 'hours: 1 day, end_dt = holiday' ); # FIXME Is this really should be 0?
212 $diff_days = $calendar->days_between( $now, $nov_6)->delta_days;
213 is( $diff_days, 0, 'days: 1 day, end_dt = holiday' ); # FIXME Is this really should be 0?
215 # Between 6th and 7th (This case should never happen in real code, one cannot issue on a closed day)
216 $diff_hours = $calendar->hours_between( $nov_6, $nov_7 )->hours;
217 is( $diff_hours, 0 * 24, 'hours: 1 day, start_dt = holiday' ); # FIXME Is this really should be 0?
218 $diff_days = $calendar->days_between( $nov_6, $nov_7 )->delta_days;
219 is( $diff_days, 0, 'days: 1 day, start_dt = holiday' ); # FIXME Is this really should be 0?
221 # Between 5th and 7th
222 $diff_hours = $calendar->hours_between( $now, $nov_7 )->hours;
223 is( $diff_hours, 2 * 24 - 1 * 24, 'hours: 2 days, 1 holiday' );
224 $diff_days = $calendar->days_between( $now, $nov_7 )->delta_days;
225 is( $diff_days, 2 - 1, 'days: 2 days, 1 holiday' );
227 # Between 5th and 12th
228 $diff_hours = $calendar->hours_between( $now, $nov_12 )->hours;
229 is( $diff_hours, 7 * 24 - 1 * 24, 'hours: 7 days, 1 holiday' );
230 $diff_days = $calendar->days_between( $now, $nov_12)->delta_days;
231 is( $diff_days, 7 - 1, 'day: 7 days, 1 holiday' );
233 # Between 5th and 13th
234 $diff_hours = $calendar->hours_between( $now, $nov_13 )->hours;
235 is( $diff_hours, 8 * 24 - 2 * 24, 'hours: 8 days, 2 holidays' );
236 $diff_days = $calendar->days_between( $now, $nov_13)->delta_days;
237 is( $diff_days, 8 - 2, 'days: 8 days, 2 holidays' );
239 # Between 5th and 15th
240 $diff_hours = $calendar->hours_between( $now, $nov_15 )->hours;
241 is( $diff_hours, 10 * 24 - 2 * 24, 'hours: 10 days, 2 holidays' );
242 $diff_days = $calendar->days_between( $now, $nov_15)->delta_days;
243 is( $diff_days, 10 - 2, 'days: 10 days, 2 holidays' );
246 subtest 'Different hours' => sub {
249 my ( $diff_hours, $diff_days );
251 # Between 5th and 5th (Same day short hours loan)
252 # No test - Tested above as 5th is an open day
254 # Between 5th and 6th (This case should never happen in real code, one cannot return on a closed day)
255 my $duration = $calendar->hours_between( $now, $nov_6->clone->subtract(hours => 3) );
256 is( $duration->hours, abs(0 * 24 - 3), 'hours: 21 hours, end_dt = holiday' ); # FIXME $duration->hours always return a abs
257 is( $duration->is_negative, 1, '? is negative ?' ); # FIXME Do really test for that case in our calls to hours_between?
258 $duration = $calendar->days_between( $now, $nov_6->clone->subtract(hours => 3) );
259 is( $duration->hours, abs(0), 'days: 21 hours, end_dt = holiday' ); # FIXME Is this correct?
261 # Between 6th and 7th (This case should never happen in real code, one cannot issue on a closed day)
262 $duration = $calendar->hours_between( $nov_6, $nov_7->clone->subtract(hours => 3) );
263 is( $duration->hours, abs(0 * 24 - 3), 'hours: 21 hours, start_dt = holiday' ); # FIXME $duration->hours always return a abs
264 is( $duration->is_negative, 1, '? is negative ?' ); # FIXME Do really test for that case in our calls to hours_between?
265 $duration = $calendar->days_between( $nov_6, $nov_7->clone->subtract(hours => 3) );
266 is( $duration->hours, abs(0), 'days: 21 hours, start_dt = holiday' ); # FIXME Is this correct?
268 # Between 5th and 7th
269 $diff_hours = $calendar->hours_between( $now, $nov_7->clone->subtract(hours => 3) )->hours;
270 is( $diff_hours, 2 * 24 - 1 * 24 - 3, 'hours: 45 hours, 1 holiday' );
271 $diff_days = $calendar->days_between( $now, $nov_7->clone->subtract(hours => 3) )->delta_days;
272 is( $diff_days, 2 - 1, 'days: 45 hours, 1 holiday' );
274 # Between 5th and 12th
275 $diff_hours = $calendar->hours_between( $now, $nov_12->clone->subtract(hours => 3) )->hours;
276 is( $diff_hours, 7 * 24 - 1 * 24 - 3, 'hours: 165 hours, 1 holiday' );
277 $diff_days = $calendar->days_between( $now, $nov_12->clone->subtract(hours => 3) )->delta_days;
278 is( $diff_days, 7 - 1, 'days: 165 hours, 1 holiday' );
280 # Between 5th and 13th
281 $diff_hours = $calendar->hours_between( $now, $nov_13->clone->subtract(hours => 3) )->hours;
282 is( $diff_hours, 8 * 24 - 2 * 24 - 3, 'hours: 289 hours, 2 holidays ' );
283 $diff_days = $calendar->days_between( $now, $nov_13->clone->subtract(hours => 3) )->delta_days;
284 is( $diff_days, 8 - 1, 'days: 289 hours, 2 holidays' );
286 # Between 5th and 15th
287 $diff_hours = $calendar->hours_between( $now, $nov_15->clone->subtract(hours => 3) )->hours;
288 is( $diff_hours, 10 * 24 - 2 * 24 - 3, 'hours: 237 hours, 2 holidays' );
289 $diff_days = $calendar->days_between( $now, $nov_15->clone->subtract(hours => 3) )->delta_days;
290 is( $diff_days, 10 - 2, 'days: 237 hours, 2 holidays' );
298 subtest 'is_holiday' => sub {
301 subtest 'weekday holidays' => sub {
304 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
306 my $day = DateTime->now();
307 my $dow = scalar $day->day_of_week;
308 $dow = 0 if $dow == 7;
310 # Closed this day of the week
311 my $dbh = C4::Context->dbh;
314 INSERT INTO repeatable_holidays (branchcode,weekday,day,month,title,description)
315 VALUES ( ?, ?, NULL, NULL, ?, '' )
316 |, undef, $library->branchcode, $dow, "TEST"
320 my $sth = $dbh->prepare(
321 "UPDATE repeatable_holidays SET weekday = ? WHERE branchcode = ? AND title = 'TEST'"
323 for my $i ( 0 .. 6 ) {
325 Koha::Calendar->new( branchcode => $library->branchcode );
327 is( $calendar->is_holiday($day), 1, $day->day_name() ." works as a repeatable holiday");
329 # Increment the date and holiday day
330 $day->add( days => 1 );
332 $dow = 0 if $dow == 7;
333 $sth->execute($dow, $library->branchcode);
338 subtest 'get_push_amt' => sub {
341 t::lib::Mocks::mock_preference('useDaysMode', 'Dayweek');
343 subtest 'weekday holidays' => sub {
346 my $library = $builder->build_object( { class => 'Koha::Libraries' } );
348 my $day = dt_from_string();
349 my $dow = scalar $day->day_of_week;
350 $dow = 0 if $dow == 7;
352 # Closed this day of the week
353 my $dbh = C4::Context->dbh;
356 INSERT INTO repeatable_holidays (branchcode,weekday,day,month,title,description)
357 VALUES ( ?, ?, NULL, NULL, ?, '' )
358 |, undef, $library->branchcode, $dow, "TEST"
362 my $sth = $dbh->prepare(
363 "UPDATE repeatable_holidays SET weekday = ? WHERE branchcode = ? AND title = 'TEST'"
365 for my $i ( 0 .. 6 ) {
367 Koha::Calendar->new( branchcode => $library->branchcode, days_mode => 'Dayweek' );
371 local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required
373 $npa = $calendar->next_open_days( $day, 0 );
377 die unless $@ eq "alarm\n"; # propagate unexpected errors
379 ok(0, "next_push_amt succeeded for ".$day->day_name()." weekday holiday");
382 ok($npa, "next_push_amt succeeded for ".$day->day_name()." weekday holiday");
385 # Increment the date and holiday day
386 $day->add( days => 1 );
388 $dow = 0 if $dow == 7;
389 $sth->execute( $dow, $library->branchcode );
394 $schema->storage->txn_rollback();