Lines 1-718
Link Here
|
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::NoWarnings; |
21 |
use Test::More tests => 7; |
22 |
use Time::Fake; |
23 |
|
24 |
use t::lib::Mocks; |
25 |
use t::lib::TestBuilder; |
26 |
|
27 |
use DateTime; |
28 |
use DateTime::Duration; |
29 |
use Koha::Caches; |
30 |
use Koha::Calendar; |
31 |
use Koha::Database; |
32 |
use Koha::DateUtils qw( dt_from_string ); |
33 |
|
34 |
my $schema = Koha::Database->new->schema; |
35 |
my $builder = t::lib::TestBuilder->new; |
36 |
$schema->storage->txn_begin; |
37 |
|
38 |
my $today = dt_from_string(); |
39 |
my $holiday_dt = $today->clone; |
40 |
$holiday_dt->add( days => 3 ); |
41 |
|
42 |
Koha::Caches->get_instance()->flush_all(); |
43 |
|
44 |
subtest 'Original tests from t' => sub { |
45 |
|
46 |
# We need to mock the C4::Context->preference method for |
47 |
# simplicity and re-usability of the session definition. Any |
48 |
# syspref fits for syspref-agnostic tests. |
49 |
my $module_context = Test::MockModule->new('C4::Context'); |
50 |
$module_context->mock( |
51 |
'preference', |
52 |
sub { |
53 |
return 'Calendar'; |
54 |
} |
55 |
); |
56 |
|
57 |
my $mpl = $builder->build_object( { class => 'Koha::Libraries' } )->branchcode; |
58 |
my $cpl = $builder->build_object( { class => 'Koha::Libraries' } )->branchcode; |
59 |
my $rows = [ # add weekly holidays |
60 |
{ branchcode => $mpl, weekday => 0 }, # sundays |
61 |
{ branchcode => $mpl, weekday => 6 }, # saturdays |
62 |
{ branchcode => $mpl, day => 1, month => 1 }, # new year's day |
63 |
{ branchcode => $mpl, day => 25, month => 12 }, # chrismas |
64 |
]; |
65 |
$schema->resultset('RepeatableHoliday')->delete_all; |
66 |
$schema->resultset('RepeatableHoliday')->create( { %$_, description => q{} } ) for @$rows; |
67 |
|
68 |
$rows = [ # exception holidays |
69 |
{ branchcode => $mpl, day => 11, month => 11, year => 2012, isexception => 1 }, # sunday exception |
70 |
{ branchcode => $mpl, day => 1, month => 6, year => 2011, isexception => 0 }, |
71 |
{ branchcode => $mpl, day => 4, month => 7, year => 2012, isexception => 0 }, |
72 |
{ branchcode => $cpl, day => 6, month => 8, year => 2012, isexception => 0 }, |
73 |
{ branchcode => $mpl, day => 7, month => 7, year => 2012, isexception => 1 }, # holiday exception |
74 |
{ branchcode => $mpl, day => 7, month => 7, year => 2012, isexception => 0 }, # holiday |
75 |
]; |
76 |
$schema->resultset('SpecialHoliday')->delete_all; |
77 |
$schema->resultset('SpecialHoliday')->create( { %$_, description => q{} } ) for @$rows; |
78 |
|
79 |
my $cache = Koha::Caches->get_instance(); |
80 |
$cache->clear_from_cache( $mpl . '_holidays' ); |
81 |
$cache->clear_from_cache( $cpl . '_holidays' ); |
82 |
|
83 |
# $mpl branch is arbitrary, is not used at all but is needed for initialization |
84 |
my $cal = Koha::Calendar->new( branchcode => $mpl ); |
85 |
|
86 |
isa_ok( $cal, 'Koha::Calendar', 'Calendar class returned' ); |
87 |
|
88 |
my $saturday = DateTime->new( |
89 |
year => 2012, |
90 |
month => 11, |
91 |
day => 24, |
92 |
); |
93 |
|
94 |
my $sunday = DateTime->new( |
95 |
year => 2012, |
96 |
month => 11, |
97 |
day => 25, |
98 |
); |
99 |
|
100 |
my $monday = DateTime->new( |
101 |
year => 2012, |
102 |
month => 11, |
103 |
day => 26, |
104 |
); |
105 |
|
106 |
my $new_year = DateTime->new( |
107 |
year => 2013, |
108 |
month => 1, |
109 |
day => 1, |
110 |
); |
111 |
|
112 |
my $single_holiday = DateTime->new( |
113 |
year => 2011, |
114 |
month => 6, |
115 |
day => 1, |
116 |
); # should be a holiday |
117 |
|
118 |
my $notspecial = DateTime->new( |
119 |
year => 2011, |
120 |
month => 6, |
121 |
day => 2 |
122 |
); # should NOT be a holiday |
123 |
|
124 |
my $sunday_exception = DateTime->new( |
125 |
year => 2012, |
126 |
month => 11, |
127 |
day => 11 |
128 |
); |
129 |
|
130 |
my $day_after_christmas = DateTime->new( |
131 |
year => 2012, |
132 |
month => 12, |
133 |
day => 26 |
134 |
); # for testing negative addDuration |
135 |
|
136 |
my $holiday_for_another_branch = DateTime->new( |
137 |
year => 2012, |
138 |
month => 8, |
139 |
day => 6, # This is a monday |
140 |
); |
141 |
|
142 |
my $holiday_excepted = DateTime->new( |
143 |
year => 2012, |
144 |
month => 7, |
145 |
day => 7, # Both a holiday and exception |
146 |
); |
147 |
|
148 |
{ # Syspref-agnostic tests |
149 |
is( $saturday->day_of_week, 6, '\'$saturday\' is actually a saturday (6th day of week)' ); |
150 |
is( $sunday->day_of_week, 7, '\'$sunday\' is actually a sunday (7th day of week)' ); |
151 |
is( $monday->day_of_week, 1, '\'$monday\' is actually a monday (1st day of week)' ); |
152 |
is( $cal->is_holiday($saturday), 1, 'Saturday is a closed day' ); |
153 |
is( $cal->is_holiday($sunday), 1, 'Sunday is a closed day' ); |
154 |
is( $cal->is_holiday($monday), 0, 'Monday is not a closed day' ); |
155 |
is( $cal->is_holiday($new_year), 1, 'Month/Day closed day test (New year\'s day)' ); |
156 |
is( $cal->is_holiday($single_holiday), 1, 'Single holiday closed day test' ); |
157 |
is( $cal->is_holiday($notspecial), 0, 'Fixed single date that is not a holiday test' ); |
158 |
is( $cal->is_holiday($sunday_exception), 0, 'Exception holiday is not a closed day test' ); |
159 |
is( |
160 |
$cal->is_holiday($holiday_for_another_branch), 0, |
161 |
'Holiday defined for another branch should not be defined as an holiday' |
162 |
); |
163 |
is( $cal->is_holiday($holiday_excepted), 0, 'Holiday defined and excepted should not be a holiday' ); |
164 |
} |
165 |
|
166 |
{ # Bugzilla #8966 - is_holiday truncates referenced date |
167 |
my $later_dt = DateTime->new( # Monday |
168 |
year => 2012, |
169 |
month => 9, |
170 |
day => 17, |
171 |
hour => 17, |
172 |
minute => 30, |
173 |
time_zone => 'Europe/London', |
174 |
); |
175 |
|
176 |
is( $cal->is_holiday($later_dt), 0, 'bz-8966 (1/2) Apply is_holiday for the next test' ); |
177 |
cmp_ok( $later_dt, 'eq', '2012-09-17T17:30:00', 'bz-8966 (2/2) Date should be the same after is_holiday' ); |
178 |
} |
179 |
|
180 |
{ # Bugzilla #8800 - is_holiday should use truncated date for 'contains' call |
181 |
my $single_holiday_time = DateTime->new( |
182 |
year => 2011, |
183 |
month => 6, |
184 |
day => 1, |
185 |
hour => 11, |
186 |
minute => 2 |
187 |
); |
188 |
|
189 |
is( |
190 |
$cal->is_holiday($single_holiday_time), |
191 |
$cal->is_holiday($single_holiday), |
192 |
'bz-8800 is_holiday should truncate the date for holiday validation' |
193 |
); |
194 |
} |
195 |
|
196 |
my $one_day_dur = DateTime::Duration->new( days => 1 ); |
197 |
my $two_day_dur = DateTime::Duration->new( days => 2 ); |
198 |
my $seven_day_dur = DateTime::Duration->new( days => 7 ); |
199 |
|
200 |
my $dt = dt_from_string( '2012-07-03', 'iso' ); #tuesday |
201 |
|
202 |
my $test_dt = DateTime->new( # Monday |
203 |
year => 2012, |
204 |
month => 7, |
205 |
day => 23, |
206 |
hour => 11, |
207 |
minute => 53, |
208 |
); |
209 |
|
210 |
my $later_dt = DateTime->new( # Monday |
211 |
year => 2012, |
212 |
month => 9, |
213 |
day => 17, |
214 |
hour => 17, |
215 |
minute => 30, |
216 |
time_zone => 'Europe/London', |
217 |
); |
218 |
|
219 |
{ ## 'Datedue' tests |
220 |
|
221 |
$cal = Koha::Calendar->new( branchcode => $mpl, days_mode => 'Datedue' ); |
222 |
|
223 |
is( |
224 |
$cal->addDuration( $dt, $one_day_dur, 'days' ), # tuesday |
225 |
dt_from_string( '2012-07-05', 'iso' ), |
226 |
'Single day add (Datedue, matches holiday, shift)' |
227 |
); |
228 |
|
229 |
is( |
230 |
$cal->addDuration( $dt, $two_day_dur, 'days' ), |
231 |
dt_from_string( '2012-07-05', 'iso' ), |
232 |
'Two days add, skips holiday (Datedue)' |
233 |
); |
234 |
|
235 |
cmp_ok( |
236 |
$cal->addDuration( $test_dt, $seven_day_dur, 'days' ), 'eq', |
237 |
'2012-07-30T11:53:00', |
238 |
'Add 7 days (Datedue)' |
239 |
); |
240 |
|
241 |
is( |
242 |
$cal->addDuration( $saturday, $one_day_dur, 'days' )->day_of_week, 1, |
243 |
'addDuration skips closed Sunday (Datedue)' |
244 |
); |
245 |
|
246 |
is( |
247 |
$cal->addDuration( $day_after_christmas, -1, 'days' )->ymd(), '2012-12-24', |
248 |
'Negative call to addDuration (Datedue)' |
249 |
); |
250 |
|
251 |
## Note that the days_between API says closed days are not considered. |
252 |
## This tests are here as an API test. |
253 |
cmp_ok( |
254 |
$cal->days_between( $test_dt, $later_dt )->in_units('days'), |
255 |
'==', 40, 'days_between calculates correctly (Days)' |
256 |
); |
257 |
|
258 |
cmp_ok( |
259 |
$cal->days_between( $later_dt, $test_dt )->in_units('days'), |
260 |
'==', 40, 'Test parameter order not relevant (Days)' |
261 |
); |
262 |
} |
263 |
|
264 |
{ ## 'Calendar' tests' |
265 |
|
266 |
$cal = Koha::Calendar->new( branchcode => $mpl, days_mode => 'Calendar' ); |
267 |
|
268 |
$dt = dt_from_string( '2012-07-03', 'iso' ); |
269 |
|
270 |
is( |
271 |
$cal->addDuration( $dt, $one_day_dur, 'days' ), |
272 |
dt_from_string( '2012-07-05', 'iso' ), |
273 |
'Single day add (Calendar)' |
274 |
); |
275 |
|
276 |
cmp_ok( |
277 |
$cal->addDuration( $test_dt, $seven_day_dur, 'days' ), 'eq', |
278 |
'2012-08-01T11:53:00', |
279 |
'Add 7 days (Calendar)' |
280 |
); |
281 |
|
282 |
is( |
283 |
$cal->addDuration( $saturday, $one_day_dur, 'days' )->day_of_week, 1, |
284 |
'addDuration skips closed Sunday (Calendar)' |
285 |
); |
286 |
|
287 |
is( |
288 |
$cal->addDuration( $day_after_christmas, -1, 'days' )->ymd(), '2012-12-24', |
289 |
'Negative call to addDuration (Calendar)' |
290 |
); |
291 |
|
292 |
cmp_ok( |
293 |
$cal->days_between( $test_dt, $later_dt )->in_units('days'), |
294 |
'==', 40, 'days_between calculates correctly (Calendar)' |
295 |
); |
296 |
|
297 |
cmp_ok( |
298 |
$cal->days_between( $later_dt, $test_dt )->in_units('days'), |
299 |
'==', 40, 'Test parameter order not relevant (Calendar)' |
300 |
); |
301 |
} |
302 |
|
303 |
{ ## 'Days' tests |
304 |
|
305 |
$cal = Koha::Calendar->new( branchcode => $mpl, days_mode => 'Days' ); |
306 |
|
307 |
$dt = dt_from_string( '2012-07-03', 'iso' ); |
308 |
|
309 |
is( |
310 |
$cal->addDuration( $dt, $one_day_dur, 'days' ), |
311 |
dt_from_string( '2012-07-04', 'iso' ), |
312 |
'Single day add (Days)' |
313 |
); |
314 |
|
315 |
cmp_ok( |
316 |
$cal->addDuration( $test_dt, $seven_day_dur, 'days' ), 'eq', |
317 |
'2012-07-30T11:53:00', |
318 |
'Add 7 days (Days)' |
319 |
); |
320 |
|
321 |
is( |
322 |
$cal->addDuration( $saturday, $one_day_dur, 'days' )->day_of_week, 7, |
323 |
'addDuration doesn\'t skip closed Sunday (Days)' |
324 |
); |
325 |
|
326 |
is( |
327 |
$cal->addDuration( $day_after_christmas, -1, 'days' )->ymd(), '2012-12-25', |
328 |
'Negative call to addDuration (Days)' |
329 |
); |
330 |
|
331 |
## Note that the days_between API says closed days are not considered. |
332 |
## This tests are here as an API test. |
333 |
cmp_ok( |
334 |
$cal->days_between( $test_dt, $later_dt )->in_units('days'), |
335 |
'==', 40, 'days_between calculates correctly (Days)' |
336 |
); |
337 |
|
338 |
cmp_ok( |
339 |
$cal->days_between( $later_dt, $test_dt )->in_units('days'), |
340 |
'==', 40, 'Test parameter order not relevant (Days)' |
341 |
); |
342 |
|
343 |
} |
344 |
|
345 |
{ |
346 |
$cal = Koha::Calendar->new( branchcode => $cpl ); |
347 |
is( $cal->is_holiday($single_holiday), 0, 'Single holiday for MPL, not CPL' ); |
348 |
is( |
349 |
$cal->is_holiday($holiday_for_another_branch), 1, |
350 |
'Holiday defined for CPL should be defined as an holiday' |
351 |
); |
352 |
} |
353 |
|
354 |
subtest 'days_mode parameter' => sub { |
355 |
plan tests => 1; |
356 |
|
357 |
t::lib::Mocks::mock_preference( 'useDaysMode', 'Days' ); |
358 |
|
359 |
$cal = Koha::Calendar->new( branchcode => $cpl, days_mode => 'Calendar' ); |
360 |
is( $cal->{days_mode}, 'Calendar', q|If set, days_mode is correctly set| ); |
361 |
}; |
362 |
|
363 |
$cache->clear_from_cache( $mpl . '_holidays' ); |
364 |
$cache->clear_from_cache( $cpl . '_holidays' ); |
365 |
}; |
366 |
|
367 |
my $library = $builder->build_object( { class => 'Koha::Libraries' } ); |
368 |
my $calendar = Koha::Calendar->new( branchcode => $library->branchcode, days_mode => 'Calendar' ); |
369 |
my $holiday = $builder->build( |
370 |
{ |
371 |
source => 'SpecialHoliday', |
372 |
value => { |
373 |
branchcode => $library->branchcode, |
374 |
day => $holiday_dt->day, |
375 |
month => $holiday_dt->month, |
376 |
year => $holiday_dt->year, |
377 |
title => 'My holiday', |
378 |
isexception => 0 |
379 |
}, |
380 |
} |
381 |
); |
382 |
|
383 |
subtest 'days_forward' => sub { |
384 |
plan tests => 4; |
385 |
|
386 |
my $forwarded_dt = $calendar->days_forward( $today, 2 ); |
387 |
my $expected = $today->clone->add( days => 2 ); |
388 |
is( $forwarded_dt->ymd, $expected->ymd, 'With no holiday on the perioddays_forward should add 2 days' ); |
389 |
|
390 |
$forwarded_dt = $calendar->days_forward( $today, 5 ); |
391 |
$expected = $today->clone->add( days => 6 ); |
392 |
is( |
393 |
$forwarded_dt->ymd, $expected->ymd, |
394 |
'With holiday on the perioddays_forward should add 5 days + 1 day for holiday' |
395 |
); |
396 |
|
397 |
$forwarded_dt = $calendar->days_forward( $today, 0 ); |
398 |
is( $forwarded_dt->ymd, $today->ymd, '0 day should return start dt' ); |
399 |
|
400 |
$forwarded_dt = $calendar->days_forward( $today, -2 ); |
401 |
is( $forwarded_dt->ymd, $today->ymd, 'negative day should return start dt' ); |
402 |
}; |
403 |
|
404 |
subtest 'crossing_DST' => sub { |
405 |
|
406 |
plan tests => 3; |
407 |
|
408 |
my $tz = DateTime::TimeZone->new( name => 'America/New_York' ); |
409 |
my $start_date = dt_from_string( "2016-03-09 02:29:00", undef, $tz ); |
410 |
my $end_date = dt_from_string( "2017-01-01 00:00:00", undef, $tz ); |
411 |
my $days_between = $calendar->days_between( $start_date, $end_date ); |
412 |
is( $days_between->delta_days, 298, "Days calculated correctly" ); |
413 |
$days_between = $calendar->days_between( $end_date, $start_date ); |
414 |
is( $days_between->delta_days, 298, "Swapping returns the same" ); |
415 |
my $hours_between = $calendar->hours_between( $start_date, $end_date ); |
416 |
is( |
417 |
$hours_between->delta_minutes, |
418 |
298 * 24 * 60 - 149, |
419 |
"Hours (in minutes) calculated correctly" |
420 |
); |
421 |
}; |
422 |
|
423 |
subtest 'hours_between | days_between' => sub { |
424 |
|
425 |
plan tests => 2; |
426 |
|
427 |
# November 2019 |
428 |
# Su Mo Tu We Th Fr Sa |
429 |
# 1 2 |
430 |
# 3 4 *5* 6 7 8 9 |
431 |
# 10 11 12 13 14 15 16 |
432 |
# 17 18 19 20 21 22 23 |
433 |
# 24 25 26 27 28 29 30 |
434 |
|
435 |
my $now = dt_from_string('2019-11-05 12:34:56'); # Today is 2019 Nov 5th |
436 |
my $nov_6 = dt_from_string('2019-11-06 12:34:56'); |
437 |
my $nov_7 = dt_from_string('2019-11-07 12:34:56'); |
438 |
my $nov_12 = dt_from_string('2019-11-12 12:34:56'); |
439 |
my $nov_13 = dt_from_string('2019-11-13 12:34:56'); |
440 |
my $nov_15 = dt_from_string('2019-11-15 12:34:56'); |
441 |
Time::Fake->offset( $now->epoch ); |
442 |
|
443 |
subtest 'No holiday' => sub { |
444 |
|
445 |
plan tests => 2; |
446 |
|
447 |
my $library = $builder->build_object( { class => 'Koha::Libraries' } ); |
448 |
my $calendar = Koha::Calendar->new( branchcode => $library->branchcode ); |
449 |
|
450 |
subtest 'Same hours' => sub { |
451 |
|
452 |
plan tests => 8; |
453 |
|
454 |
# Between 5th and 6th |
455 |
my $diff_hours = $calendar->hours_between( $now, $nov_6 )->hours; |
456 |
is( $diff_hours, 1 * 24, 'hours: 1 day, no holiday' ); |
457 |
my $diff_days = $calendar->days_between( $now, $nov_6 )->delta_days; |
458 |
is( $diff_days, 1, 'days: 1 day, no holiday' ); |
459 |
|
460 |
# Between 5th and 7th |
461 |
$diff_hours = $calendar->hours_between( $now, $nov_7 )->hours; |
462 |
is( $diff_hours, 2 * 24, 'hours: 2 days, no holiday' ); |
463 |
$diff_days = $calendar->days_between( $now, $nov_7 )->delta_days; |
464 |
is( $diff_days, 2, 'days: 2 days, no holiday' ); |
465 |
|
466 |
# Between 5th and 12th |
467 |
$diff_hours = $calendar->hours_between( $now, $nov_12 )->hours; |
468 |
is( $diff_hours, 7 * 24, 'hours: 7 days, no holiday' ); |
469 |
$diff_days = $calendar->days_between( $now, $nov_12 )->delta_days; |
470 |
is( $diff_days, 7, 'days: 7 days, no holiday' ); |
471 |
|
472 |
# Between 5th and 15th |
473 |
$diff_hours = $calendar->hours_between( $now, $nov_15 )->hours; |
474 |
is( $diff_hours, 10 * 24, 'hours: 10 days, no holiday' ); |
475 |
$diff_days = $calendar->days_between( $now, $nov_15 )->delta_days; |
476 |
is( $diff_days, 10, 'days: 10 days, no holiday' ); |
477 |
}; |
478 |
|
479 |
subtest 'Different hours' => sub { |
480 |
|
481 |
plan tests => 10; |
482 |
|
483 |
# Between 5th and 5th (Same day short hours loan) |
484 |
my $diff_hours = $calendar->hours_between( $now, $now->clone->add( hours => 3 ) )->hours; |
485 |
is( $diff_hours, 3, 'hours: 3 hours, no holidays' ); |
486 |
my $diff_days = $calendar->days_between( $now, $now->clone->add( hours => 3 ) )->delta_days; |
487 |
is( $diff_days, 0, 'days: 3 hours, no holidays' ); |
488 |
|
489 |
# Between 5th and 6th |
490 |
$diff_hours = $calendar->hours_between( $now, $nov_6->clone->subtract( hours => 3 ) )->hours; |
491 |
is( $diff_hours, 1 * 24 - 3, 'hours: 21 hours, no holidays' ); |
492 |
$diff_days = $calendar->days_between( $now, $nov_6->clone->subtract( hours => 3 ) )->delta_days; |
493 |
is( $diff_days, 1, 'days: 21 hours, no holidays' ); |
494 |
|
495 |
# Between 5th and 7th |
496 |
$diff_hours = $calendar->hours_between( $now, $nov_7->clone->subtract( hours => 3 ) )->hours; |
497 |
is( $diff_hours, 2 * 24 - 3, 'hours: 45 hours, no holidays' ); |
498 |
$diff_days = $calendar->days_between( $now, $nov_7->clone->subtract( hours => 3 ) )->delta_days; |
499 |
is( $diff_days, 2, 'days: 45 hours, no holidays' ); |
500 |
|
501 |
# Between 5th and 12th |
502 |
$diff_hours = $calendar->hours_between( $now, $nov_12->clone->subtract( hours => 3 ) )->hours; |
503 |
is( $diff_hours, 7 * 24 - 3, 'hours: 165 hours, no holidays' ); |
504 |
$diff_days = $calendar->days_between( $now, $nov_12->clone->subtract( hours => 3 ) )->delta_days; |
505 |
is( $diff_days, 7, 'days: 165 hours, no holidays' ); |
506 |
|
507 |
# Between 5th and 15th |
508 |
$diff_hours = $calendar->hours_between( $now, $nov_15->clone->subtract( hours => 3 ) )->hours; |
509 |
is( $diff_hours, 10 * 24 - 3, 'hours: 237 hours, no holidays' ); |
510 |
$diff_days = $calendar->days_between( $now, $nov_15->clone->subtract( hours => 3 ) )->delta_days; |
511 |
is( $diff_days, 10, 'days: 237 hours, no holidays' ); |
512 |
}; |
513 |
}; |
514 |
|
515 |
subtest 'With holiday' => sub { |
516 |
plan tests => 2; |
517 |
|
518 |
my $library = $builder->build_object( { class => 'Koha::Libraries' } ); |
519 |
|
520 |
# Wednesdays are closed |
521 |
my $dbh = C4::Context->dbh; |
522 |
$dbh->do( |
523 |
q| |
524 |
INSERT INTO repeatable_holidays (branchcode,weekday,day,month,title,description) |
525 |
VALUES ( ?, ?, NULL, NULL, ?, '' ) |
526 |
|, undef, $library->branchcode, 3, 'Closed on Wednesday' |
527 |
); |
528 |
|
529 |
my $calendar = Koha::Calendar->new( branchcode => $library->branchcode ); |
530 |
|
531 |
subtest 'Same hours' => sub { |
532 |
plan tests => 12; |
533 |
|
534 |
my ( $diff_hours, $diff_days ); |
535 |
|
536 |
# Between 5th and 6th (This case should never happen in real code, one cannot return on a closed day) |
537 |
$diff_hours = $calendar->hours_between( $now, $nov_6 )->hours; |
538 |
is( $diff_hours, 0 * 24, 'hours: 1 day, end_dt = holiday' ); # FIXME Is this really should be 0? |
539 |
$diff_days = $calendar->days_between( $now, $nov_6 )->delta_days; |
540 |
is( $diff_days, 0, 'days: 1 day, end_dt = holiday' ); # FIXME Is this really should be 0? |
541 |
|
542 |
# Between 6th and 7th (This case should never happen in real code, one cannot issue on a closed day) |
543 |
$diff_hours = $calendar->hours_between( $nov_6, $nov_7 )->hours; |
544 |
is( $diff_hours, 0 * 24, 'hours: 1 day, start_dt = holiday' ); # FIXME Is this really should be 0? |
545 |
$diff_days = $calendar->days_between( $nov_6, $nov_7 )->delta_days; |
546 |
is( $diff_days, 0, 'days: 1 day, start_dt = holiday' ); # FIXME Is this really should be 0? |
547 |
|
548 |
# Between 5th and 7th |
549 |
$diff_hours = $calendar->hours_between( $now, $nov_7 )->hours; |
550 |
is( $diff_hours, 2 * 24 - 1 * 24, 'hours: 2 days, 1 holiday' ); |
551 |
$diff_days = $calendar->days_between( $now, $nov_7 )->delta_days; |
552 |
is( $diff_days, 2 - 1, 'days: 2 days, 1 holiday' ); |
553 |
|
554 |
# Between 5th and 12th |
555 |
$diff_hours = $calendar->hours_between( $now, $nov_12 )->hours; |
556 |
is( $diff_hours, 7 * 24 - 1 * 24, 'hours: 7 days, 1 holiday' ); |
557 |
$diff_days = $calendar->days_between( $now, $nov_12 )->delta_days; |
558 |
is( $diff_days, 7 - 1, 'day: 7 days, 1 holiday' ); |
559 |
|
560 |
# Between 5th and 13th |
561 |
$diff_hours = $calendar->hours_between( $now, $nov_13 )->hours; |
562 |
is( $diff_hours, 8 * 24 - 2 * 24, 'hours: 8 days, 2 holidays' ); |
563 |
$diff_days = $calendar->days_between( $now, $nov_13 )->delta_days; |
564 |
is( $diff_days, 8 - 2, 'days: 8 days, 2 holidays' ); |
565 |
|
566 |
# Between 5th and 15th |
567 |
$diff_hours = $calendar->hours_between( $now, $nov_15 )->hours; |
568 |
is( $diff_hours, 10 * 24 - 2 * 24, 'hours: 10 days, 2 holidays' ); |
569 |
$diff_days = $calendar->days_between( $now, $nov_15 )->delta_days; |
570 |
is( $diff_days, 10 - 2, 'days: 10 days, 2 holidays' ); |
571 |
}; |
572 |
|
573 |
subtest 'Different hours' => sub { |
574 |
plan tests => 14; |
575 |
|
576 |
my ( $diff_hours, $diff_days ); |
577 |
|
578 |
# Between 5th and 5th (Same day short hours loan) |
579 |
# No test - Tested above as 5th is an open day |
580 |
|
581 |
# Between 5th and 6th (This case should never happen in real code, one cannot return on a closed day) |
582 |
my $duration = $calendar->hours_between( $now, $nov_6->clone->subtract( hours => 3 ) ); |
583 |
is( $duration->hours, abs( 0 * 24 - 3 ), 'hours: 21 hours, end_dt = holiday' ) |
584 |
; # FIXME $duration->hours always return a abs |
585 |
is( $duration->is_negative, 1, '? is negative ?' ) |
586 |
; # FIXME Do really test for that case in our calls to hours_between? |
587 |
$duration = $calendar->days_between( $now, $nov_6->clone->subtract( hours => 3 ) ); |
588 |
is( $duration->hours, abs(0), 'days: 21 hours, end_dt = holiday' ); # FIXME Is this correct? |
589 |
|
590 |
# Between 6th and 7th (This case should never happen in real code, one cannot issue on a closed day) |
591 |
$duration = $calendar->hours_between( $nov_6, $nov_7->clone->subtract( hours => 3 ) ); |
592 |
is( $duration->hours, abs( 0 * 24 - 3 ), 'hours: 21 hours, start_dt = holiday' ) |
593 |
; # FIXME $duration->hours always return a abs |
594 |
is( $duration->is_negative, 1, '? is negative ?' ) |
595 |
; # FIXME Do really test for that case in our calls to hours_between? |
596 |
$duration = $calendar->days_between( $nov_6, $nov_7->clone->subtract( hours => 3 ) ); |
597 |
is( $duration->hours, abs(0), 'days: 21 hours, start_dt = holiday' ); # FIXME Is this correct? |
598 |
|
599 |
# Between 5th and 7th |
600 |
$diff_hours = $calendar->hours_between( $now, $nov_7->clone->subtract( hours => 3 ) )->hours; |
601 |
is( $diff_hours, 2 * 24 - 1 * 24 - 3, 'hours: 45 hours, 1 holiday' ); |
602 |
$diff_days = $calendar->days_between( $now, $nov_7->clone->subtract( hours => 3 ) )->delta_days; |
603 |
is( $diff_days, 2 - 1, 'days: 45 hours, 1 holiday' ); |
604 |
|
605 |
# Between 5th and 12th |
606 |
$diff_hours = $calendar->hours_between( $now, $nov_12->clone->subtract( hours => 3 ) )->hours; |
607 |
is( $diff_hours, 7 * 24 - 1 * 24 - 3, 'hours: 165 hours, 1 holiday' ); |
608 |
$diff_days = $calendar->days_between( $now, $nov_12->clone->subtract( hours => 3 ) )->delta_days; |
609 |
is( $diff_days, 7 - 1, 'days: 165 hours, 1 holiday' ); |
610 |
|
611 |
# Between 5th and 13th |
612 |
$diff_hours = $calendar->hours_between( $now, $nov_13->clone->subtract( hours => 3 ) )->hours; |
613 |
is( $diff_hours, 8 * 24 - 2 * 24 - 3, 'hours: 289 hours, 2 holidays ' ); |
614 |
$diff_days = $calendar->days_between( $now, $nov_13->clone->subtract( hours => 3 ) )->delta_days; |
615 |
is( $diff_days, 8 - 1, 'days: 289 hours, 2 holidays' ); |
616 |
|
617 |
# Between 5th and 15th |
618 |
$diff_hours = $calendar->hours_between( $now, $nov_15->clone->subtract( hours => 3 ) )->hours; |
619 |
is( $diff_hours, 10 * 24 - 2 * 24 - 3, 'hours: 237 hours, 2 holidays' ); |
620 |
$diff_days = $calendar->days_between( $now, $nov_15->clone->subtract( hours => 3 ) )->delta_days; |
621 |
is( $diff_days, 10 - 2, 'days: 237 hours, 2 holidays' ); |
622 |
}; |
623 |
|
624 |
}; |
625 |
|
626 |
Time::Fake->reset; |
627 |
}; |
628 |
|
629 |
subtest 'is_holiday' => sub { |
630 |
plan tests => 1; |
631 |
|
632 |
subtest 'weekday holidays' => sub { |
633 |
plan tests => 7; |
634 |
|
635 |
my $library = $builder->build_object( { class => 'Koha::Libraries' } ); |
636 |
|
637 |
my $day = dt_from_string(); |
638 |
my $dow = scalar $day->day_of_week; |
639 |
$dow = 0 if $dow == 7; |
640 |
|
641 |
# Closed this day of the week |
642 |
my $dbh = C4::Context->dbh; |
643 |
$dbh->do( |
644 |
q| |
645 |
INSERT INTO repeatable_holidays (branchcode,weekday,day,month,title,description) |
646 |
VALUES ( ?, ?, NULL, NULL, ?, '' ) |
647 |
|, undef, $library->branchcode, $dow, "TEST" |
648 |
); |
649 |
|
650 |
# Iterate 7 days |
651 |
my $sth = $dbh->prepare("UPDATE repeatable_holidays SET weekday = ? WHERE branchcode = ? AND title = 'TEST'"); |
652 |
for my $i ( 0 .. 6 ) { |
653 |
my $calendar = Koha::Calendar->new( branchcode => $library->branchcode ); |
654 |
|
655 |
is( $calendar->is_holiday($day), 1, $day->day_name() . " works as a repeatable holiday" ); |
656 |
|
657 |
# Increment the date and holiday day |
658 |
$day->add( days => 1 ); |
659 |
$dow++; |
660 |
$dow = 0 if $dow == 7; |
661 |
$sth->execute( $dow, $library->branchcode ); |
662 |
} |
663 |
}; |
664 |
}; |
665 |
|
666 |
subtest 'get_push_amt' => sub { |
667 |
plan tests => 1; |
668 |
|
669 |
t::lib::Mocks::mock_preference( 'useDaysMode', 'Dayweek' ); |
670 |
|
671 |
subtest 'weekday holidays' => sub { |
672 |
plan tests => 7; |
673 |
|
674 |
my $library = $builder->build_object( { class => 'Koha::Libraries' } ); |
675 |
|
676 |
my $day = dt_from_string(); |
677 |
my $dow = scalar $day->day_of_week; |
678 |
$dow = 0 if $dow == 7; |
679 |
|
680 |
# Closed this day of the week |
681 |
my $dbh = C4::Context->dbh; |
682 |
$dbh->do( |
683 |
q| |
684 |
INSERT INTO repeatable_holidays (branchcode,weekday,day,month,title,description) |
685 |
VALUES ( ?, ?, NULL, NULL, ?, '' ) |
686 |
|, undef, $library->branchcode, $dow, "TEST" |
687 |
); |
688 |
|
689 |
# Iterate 7 days |
690 |
my $sth = $dbh->prepare("UPDATE repeatable_holidays SET weekday = ? WHERE branchcode = ? AND title = 'TEST'"); |
691 |
for my $i ( 0 .. 6 ) { |
692 |
my $calendar = Koha::Calendar->new( branchcode => $library->branchcode, days_mode => 'Dayweek' ); |
693 |
|
694 |
my $npa; |
695 |
eval { |
696 |
local $SIG{ALRM} = sub { die "alarm\n" }; # NB: \n required |
697 |
alarm 2; |
698 |
$npa = $calendar->next_open_days( $day, 0 ); |
699 |
alarm 0; |
700 |
}; |
701 |
if ($@) { |
702 |
die unless $@ eq "alarm\n"; # propagate unexpected errors |
703 |
# timed out |
704 |
ok( 0, "next_push_amt succeeded for " . $day->day_name() . " weekday holiday" ); |
705 |
} else { |
706 |
ok( $npa, "next_push_amt succeeded for " . $day->day_name() . " weekday holiday" ); |
707 |
} |
708 |
|
709 |
# Increment the date and holiday day |
710 |
$day->add( days => 1 ); |
711 |
$dow++; |
712 |
$dow = 0 if $dow == 7; |
713 |
$sth->execute( $dow, $library->branchcode ); |
714 |
} |
715 |
}; |
716 |
}; |
717 |
|
718 |
$schema->storage->txn_rollback(); |