Line 0
Link Here
|
|
|
1 |
#!/usr/bin/perl |
2 |
|
3 |
# Copyright 2015 Koha Development team |
4 |
# |
5 |
# This file is part of Koha |
6 |
# |
7 |
# Koha is free software; you can redistribute it and/or modify it |
8 |
# under the terms of the GNU General Public License as published by |
9 |
# the Free Software Foundation; either version 3 of the License, or |
10 |
# (at your option) any later version. |
11 |
# |
12 |
# Koha is distributed in the hope that it will be useful, but |
13 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
14 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 |
# GNU General Public License for more details. |
16 |
# |
17 |
# You should have received a copy of the GNU General Public License |
18 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
19 |
|
20 |
use Modern::Perl; |
21 |
use Test::More tests => 50; |
22 |
use Test::MockModule; |
23 |
|
24 |
use C4::Context; |
25 |
use C4::Output; |
26 |
use Koha::DateUtils; |
27 |
use Koha::DiscreteCalendar; |
28 |
|
29 |
|
30 |
my $module_context = Test::MockModule->new('C4::Context'); |
31 |
my $today = DateTime->today; |
32 |
my $schema = Koha::Database->new->schema; |
33 |
$schema->storage->txn_begin; |
34 |
|
35 |
my $branch1 = "Library1"; |
36 |
my $branch2 = "Library2"; |
37 |
|
38 |
$schema->resultset('DiscreteCalendar')->search({ |
39 |
branchcode =>'' |
40 |
})->update_all({ |
41 |
is_opened => 1, |
42 |
holiday_type => '', |
43 |
note => '', |
44 |
open_hour => '08:00:00', |
45 |
close_hour => '16:00:00' |
46 |
}); |
47 |
|
48 |
#Added entries for the branches. |
49 |
Koha::DiscreteCalendar->add_new_branch('', $branch1); |
50 |
Koha::DiscreteCalendar->add_new_branch('', $branch2); |
51 |
|
52 |
isnt($branch1,'', "First branch to do tests. BranchCode => $branch1"); |
53 |
isnt($branch2,'', "Second branch to do tests. BranchCode => $branch2"); |
54 |
|
55 |
#2 Calendars to make sure branches are treated separately. |
56 |
my $calendar = Koha::DiscreteCalendar->new({branchcode => $branch1}); |
57 |
my $calendar2 = Koha::DiscreteCalendar->new({branchcode => $branch2}); |
58 |
|
59 |
my $unique_holiday = DateTime->today; |
60 |
$calendar->edit_holiday({ |
61 |
title => "Single holiday Today", |
62 |
holiday_type=> $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION}, |
63 |
start_date=>$unique_holiday, |
64 |
end_date=>$unique_holiday |
65 |
}); |
66 |
is($calendar->is_opened($unique_holiday), 0, "Branch closed today : $unique_holiday"); |
67 |
|
68 |
my @unique_holidays = $calendar->get_unique_holidays(); |
69 |
is(scalar @unique_holidays, 1, "Set of exception holidays at 1"); |
70 |
|
71 |
my $yesterday = DateTime->today->subtract(days => 1); |
72 |
$calendar->edit_holiday({ |
73 |
title => "Single holiday Today", |
74 |
holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION}, |
75 |
start_date => $yesterday, |
76 |
end_date => $yesterday |
77 |
}); |
78 |
is($calendar->is_opened($yesterday), 1, "Cannot edit dates in the past without override : $yesterday is not a holiday"); |
79 |
|
80 |
$calendar->edit_holiday({ |
81 |
title => "Single holiday Today", |
82 |
holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION}, |
83 |
start_date => $yesterday, |
84 |
end_date => $yesterday, |
85 |
override => 1 |
86 |
}); |
87 |
is($calendar->is_opened($yesterday), 0, "Can edit dates in the past without override : $yesterday is a holiday"); |
88 |
|
89 |
|
90 |
my $days_between_start = DateTime->today; |
91 |
my $days_between_end = DateTime->today->add(days => 6); |
92 |
my $days_between = $calendar->days_between($days_between_start, $days_between_end)->in_units('days'); |
93 |
is($days_between, 5, "Days between skips holiday"); |
94 |
|
95 |
my $repeatable_holiday = DateTime->today->add(days => 1); |
96 |
$calendar->edit_holiday({ |
97 |
title => "repeatable", |
98 |
holiday_type=> $Koha::DiscreteCalendar::HOLIDAYS->{REPEATABLE}, |
99 |
start_date=>$repeatable_holiday, |
100 |
end_date=>$repeatable_holiday |
101 |
}); |
102 |
is($calendar->is_opened($repeatable_holiday), 0, "Branch not opened on $repeatable_holiday"); |
103 |
|
104 |
my @repeatable_holidays = $calendar->get_repeatable_holidays(); |
105 |
is(scalar @repeatable_holidays, 1, "Set of repeatable holidays at 1"); |
106 |
|
107 |
# 1 being mysql DAYOFWEEK() for Sunday |
108 |
$calendar->edit_holiday({ |
109 |
title => "Weekly", |
110 |
weekday=>1, |
111 |
holiday_type=>$Koha::DiscreteCalendar::HOLIDAYS->{WEEKLY}, |
112 |
start_date=>$today, |
113 |
end_date=>$today |
114 |
}); |
115 |
my @weekly_holidays = $calendar->get_week_days_holidays(); |
116 |
is(scalar @weekly_holidays, 1, "Set of weekly holidays at 1"); |
117 |
|
118 |
my $sunday = DateTime->today->add(days =>( 7 - $today->day_of_week)); |
119 |
is($calendar->is_opened($sunday), 0, "Branch not opened on " . $sunday->day_name ."s"); |
120 |
|
121 |
my $unique_holiday_range_start = DateTime->today->add(days => 2); |
122 |
my $unique_holiday_range_end = DateTime->today->add(days => 7); |
123 |
$calendar->edit_holiday({ |
124 |
title => "Single holiday range", |
125 |
holiday_type=>"E", |
126 |
start_date=>$unique_holiday_range_start, |
127 |
end_date=>$unique_holiday_range_end |
128 |
}); |
129 |
@unique_holidays = $calendar->get_unique_holidays(); |
130 |
is(scalar @unique_holidays, 8, "Set of exception holidays at 7"); |
131 |
|
132 |
my $repeatable_holiday_range_start = DateTime->today->add(days => 8); |
133 |
my $repeatable_holiday_range_end = DateTime->today->add(days => 13); |
134 |
$calendar->edit_holiday({ |
135 |
title => "Repeatable holiday range", |
136 |
holiday_type=>$Koha::DiscreteCalendar::HOLIDAYS->{REPEATABLE}, |
137 |
start_date=>$repeatable_holiday_range_start, |
138 |
end_date=>$repeatable_holiday_range_end |
139 |
}); |
140 |
@repeatable_holidays = $calendar->get_repeatable_holidays(); |
141 |
is(scalar @repeatable_holidays, 7, "Set of repeatable holidays at 7"); |
142 |
|
143 |
#Hourly loan |
144 |
# ex : |
145 |
# item due : 2017-01-24 11:00:00 |
146 |
# item returned : 2017-01-26 10:00:00 |
147 |
# Branch closed : 2017-01-25 |
148 |
# Open/close hours : 08:00 to 16:00 (8h day) |
149 |
# Hours due : 5 hours on 2017-01-24 + 2 hours on 2017-01-26 = 7hours |
150 |
|
151 |
my $open_hours_since_start = dt_from_string->truncate(to => 'day')->add(days => 40, hours => 11); |
152 |
my $open_hours_since_end = dt_from_string->truncate(to => 'day')->add(days => 42, hours => 10); |
153 |
my $holiday_between = dt_from_string->truncate(to => 'day')->add(days => 41); |
154 |
$calendar->edit_holiday({ |
155 |
title => '', |
156 |
holiday_type=>$Koha::DiscreteCalendar::HOLIDAYS->{NONE}, |
157 |
start_date=>$open_hours_since_start, |
158 |
end_date=>$open_hours_since_end |
159 |
}); |
160 |
|
161 |
$calendar->edit_holiday({ |
162 |
title => "Single holiday", |
163 |
holiday_type=>$Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION}, |
164 |
start_date=> $holiday_between, |
165 |
end_date=>$holiday_between |
166 |
}); |
167 |
my $open_hours_between = $calendar->open_hours_between($open_hours_since_start, $open_hours_since_end); |
168 |
is($open_hours_between, 7, "7 Hours open between $open_hours_since_start and $open_hours_since_end"); |
169 |
|
170 |
my $christmas = DateTime->new( |
171 |
year => $today->year(), |
172 |
month => 12, |
173 |
day => 25, |
174 |
); |
175 |
$calendar->edit_holiday({ |
176 |
title => "Christmas", |
177 |
holiday_type=>$Koha::DiscreteCalendar::HOLIDAYS->{REPEATABLE}, |
178 |
start_date=>$christmas, |
179 |
end_date=>$christmas, |
180 |
override => 1, # necessary if unit tests are run between Christmas and the first of the year |
181 |
}); |
182 |
is($calendar->is_opened($christmas), 0, "Branch closed on Christmas : $christmas"); |
183 |
|
184 |
my $newyear = DateTime->new( |
185 |
year => $today->year() +1, |
186 |
month => 1, |
187 |
day => 1, |
188 |
); |
189 |
|
190 |
$calendar->edit_holiday({ |
191 |
title => "New Year", |
192 |
holiday_type=>$Koha::DiscreteCalendar::HOLIDAYS->{REPEATABLE}, |
193 |
start_date=>$newyear, |
194 |
end_date=>$newyear |
195 |
}); |
196 |
is($calendar->is_opened($newyear), 0, "Branch closed on New Year : $newyear"); |
197 |
|
198 |
#Hours between : |
199 |
my $date_due = DateTime->today->add(days =>1); |
200 |
my $date_returned = DateTime->today->add(hours => 8); |
201 |
my $hours_between = $calendar->hours_between($date_due, $date_returned); |
202 |
|
203 |
is($hours_between->in_units('hours'), 16, "Date due $date_due, returned $date_returned, 16 hours in advance"); |
204 |
|
205 |
$date_due = DateTime->today->add(days =>1); |
206 |
$date_returned = DateTime->today->add(days => 2, hours=> 8); |
207 |
$hours_between = $calendar->hours_between($date_due, $date_returned); |
208 |
|
209 |
is($hours_between->in_units('hours'), -16, "Date due $date_due, returned $date_returned, 16 hours late"); |
210 |
|
211 |
|
212 |
#delete holidays |
213 |
is($calendar->is_opened($today), 0, "Today is closed"); |
214 |
$calendar->edit_holiday({ |
215 |
title => '', |
216 |
holiday_type=>$Koha::DiscreteCalendar::HOLIDAYS->{NONE}, |
217 |
start_date=>$unique_holiday, |
218 |
end_date=>$unique_holiday |
219 |
}); |
220 |
is($calendar->is_opened($today), 1, "Today's holiday was removed"); |
221 |
|
222 |
$calendar->edit_holiday({ |
223 |
title => '', |
224 |
holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{NONE}, |
225 |
start_date => $yesterday, |
226 |
end_date => $yesterday, |
227 |
override => 1 |
228 |
}); |
229 |
is($calendar->is_opened($yesterday), 1, "Yesterdays's holiday was removed with override"); |
230 |
|
231 |
my $new_open_hours = '08:00'; |
232 |
$calendar->edit_holiday({ |
233 |
title => '', |
234 |
holiday_type=>$Koha::DiscreteCalendar::HOLIDAYS->{NONE}, |
235 |
open_hour=>$new_open_hours, |
236 |
close_hour=>'', |
237 |
start_date=>$today, |
238 |
end_date=>$today |
239 |
}); |
240 |
is($calendar->get_date_info($today, $branch1)->{open_hour}, '08:00:00', "Open hour changed to 08:00:00"); |
241 |
|
242 |
isnt($calendar->get_date_info($today, $branch1)->{close_hour}, '', "Close hour not removed"); |
243 |
|
244 |
my $delete_range_end = DateTime->today->add(days => 30); |
245 |
$calendar->edit_holiday({ |
246 |
title => '', |
247 |
holiday_type=>$Koha::DiscreteCalendar::HOLIDAYS->{NONE}, |
248 |
start_date=>$today, |
249 |
end_date=>$delete_range_end |
250 |
}); |
251 |
|
252 |
|
253 |
is($calendar->is_opened($today), 1, "Today's holiday was already removed"); |
254 |
is($calendar->is_opened(DateTime->today->add(days => 1)), 1, "Tomorrow's holiday was removed"); |
255 |
is($calendar->is_opened($delete_range_end), 1, "End of range holiday was removed"); |
256 |
|
257 |
#Check if other branch was affected |
258 |
my @unique_holidays_branch2 = $calendar2->get_unique_holidays(); |
259 |
is(scalar @unique_holidays_branch2, 0, "Set of exception holidays at 0 for branch => $branch2"); |
260 |
my @repeatable_holidays_branch2 = $calendar2->get_repeatable_holidays(); |
261 |
is(scalar @repeatable_holidays_branch2, 0, "Set of repeatable holidays at 0 for branch => $branch2"); |
262 |
my @weekly_holidays_branch2 = $calendar2->get_week_days_holidays(); |
263 |
is(scalar @weekly_holidays_branch2, 0, "Set of weekly holidays at 0 for branch => $branch2"); |
264 |
|
265 |
|
266 |
#Tests for addDate() |
267 |
|
268 |
my $one_day_dur = DateTime::Duration->new( days => 1 ); |
269 |
my $negative_one_day_dur = DateTime::Duration->new( days => - 1 ); |
270 |
my $two_day_dur = DateTime::Duration->new( days => 2 ); |
271 |
my $seven_day_dur = DateTime::Duration->new( days => 7 ); |
272 |
|
273 |
#Preference useDaysMode Calendar |
274 |
$calendar->{days_mode} = 'Calendar'; |
275 |
$unique_holiday->add(days => 1); |
276 |
my $tomorrow = DateTime->today->add(days => 1); |
277 |
$calendar->edit_holiday({ |
278 |
title => "Single holiday Today", |
279 |
holiday_type=>$Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION}, |
280 |
start_date=>$tomorrow, |
281 |
end_date=>$tomorrow |
282 |
}); |
283 |
|
284 |
is($calendar->addDate( $today, $one_day_dur, 'days' )->ymd(), |
285 |
$today->add(days => 2)->ymd(), |
286 |
'Single day add (Calendar)' ); |
287 |
|
288 |
is($calendar->addDate( $today, $two_day_dur, 'days' )->ymd(), |
289 |
$today->add(days => 2)->ymd(), |
290 |
'Two days add, skips holiday (Calendar)' ); |
291 |
|
292 |
cmp_ok($calendar->addDate( $today, $seven_day_dur, 'days' )->ymd(), 'eq', |
293 |
$today->add(days => 7)->ymd(), |
294 |
'Add 7 days (Calendar)' ); |
295 |
#Closed Sunday |
296 |
$calendar->edit_holiday({ |
297 |
title => "Weekly", |
298 |
weekday=>1, |
299 |
holiday_type=>$Koha::DiscreteCalendar::HOLIDAYS->{WEEKLY}, |
300 |
start_date=>$today, |
301 |
end_date=>$today |
302 |
}); |
303 |
is( $calendar->addDate( $sunday, $one_day_dur, 'days' )->day_of_week, 1, |
304 |
'addDate skips closed Sunday (Calendar)' ); |
305 |
#to remove the closed sundays |
306 |
$today = DateTime->today; |
307 |
$calendar->edit_holiday({ |
308 |
title => '', |
309 |
holiday_type=>$Koha::DiscreteCalendar::HOLIDAYS->{NONE}, |
310 |
start_date=>$today, |
311 |
end_date=>$delete_range_end |
312 |
}); |
313 |
|
314 |
is( $calendar->addDate($today, $negative_one_day_dur , 'days')->ymd(), |
315 |
$today->add(days => - 1)->ymd(), |
316 |
'Negative call to addDate (Calendar)' ); |
317 |
|
318 |
#Preference useDaysMode DateDue |
319 |
$calendar->{days_mode} = 'Datedue'; |
320 |
#clean everything |
321 |
$today = DateTime->today; |
322 |
$calendar->edit_holiday({ |
323 |
title => '', |
324 |
holiday_type=>$Koha::DiscreteCalendar::HOLIDAYS->{NONE}, |
325 |
start_date=>$today, |
326 |
end_date=>$delete_range_end |
327 |
}); |
328 |
$calendar->edit_holiday({ |
329 |
title => "Single holiday Today", |
330 |
holiday_type=>$Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION}, |
331 |
start_date=>$tomorrow, |
332 |
end_date=>$tomorrow |
333 |
}); |
334 |
|
335 |
is($calendar->addDate( $today, $one_day_dur, 'days' )->ymd(), |
336 |
$today->add(days => 2)->ymd(), |
337 |
'Single day add' ); |
338 |
|
339 |
is($calendar->addDate( $today, $two_day_dur, 'days' )->ymd(), |
340 |
$today->add(days => 2)->ymd(), |
341 |
'Two days add, skips holiday (Datedue)' ); |
342 |
|
343 |
cmp_ok($calendar->addDate( $today, $seven_day_dur, 'days' )->ymd(), 'eq', |
344 |
$today->add(days => 7)->ymd(), |
345 |
'Add 7 days (Datedue)' ); |
346 |
#Closed Sunday |
347 |
$calendar->edit_holiday({ |
348 |
title => "Weekly", |
349 |
weekday=>1, |
350 |
holiday_type=> $Koha::DiscreteCalendar::HOLIDAYS->{WEEKLY}, |
351 |
start_date=>$today, |
352 |
end_date=>$today |
353 |
}); |
354 |
is( $calendar->addDate( $sunday, $one_day_dur, 'days' )->day_of_week, 1, |
355 |
'addDate skips closed Sunday (Datedue)' ); |
356 |
#to remove the closed sundays |
357 |
$today = DateTime->today; |
358 |
$calendar->edit_holiday({ |
359 |
title => '', |
360 |
holiday_type=>$Koha::DiscreteCalendar::HOLIDAYS->{NONE}, |
361 |
start_date=>$today, |
362 |
end_date=>$delete_range_end |
363 |
}); |
364 |
|
365 |
is( $calendar->addDate($today, $negative_one_day_dur , 'days')->ymd(), |
366 |
$today->add(days => - 1)->ymd(), |
367 |
'Negative call to addDate (Datedue)' ); |
368 |
|
369 |
#Preference useDaysMode Days |
370 |
$calendar->{days_mode} = 'Days'; |
371 |
#clean everything |
372 |
$today = DateTime->today; |
373 |
$calendar->edit_holiday({ |
374 |
title => '', |
375 |
holiday_type=>$Koha::DiscreteCalendar::HOLIDAYS->{NONE}, |
376 |
start_date=>$today, |
377 |
end_date=>$delete_range_end |
378 |
}); |
379 |
$calendar->edit_holiday({ |
380 |
title => "Single holiday Today", |
381 |
holiday_type=>$Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION}, |
382 |
start_date=>$tomorrow, |
383 |
end_date=>$tomorrow |
384 |
}); |
385 |
is($calendar->addDate( $today, $one_day_dur, 'days' )->ymd(), |
386 |
$today->add(days => 1)->ymd(), |
387 |
'Single day add' ); |
388 |
|
389 |
is($calendar->addDate( $today, $two_day_dur, 'days' )->ymd(), |
390 |
$today->add(days => 2)->ymd(), |
391 |
'Two days add, skips holiday (Days)' ); |
392 |
|
393 |
cmp_ok($calendar->addDate( $today, $seven_day_dur, 'days' )->ymd(), 'eq', |
394 |
$today->add(days => 7)->ymd(), |
395 |
'Add 7 days (Days)' ); |
396 |
#Closed Sunday |
397 |
$calendar->edit_holiday({ |
398 |
title => "Weekly", |
399 |
weekday => 1, |
400 |
holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{WEEKLY}, |
401 |
start_date => $today, |
402 |
end_date => $today |
403 |
}); |
404 |
is( $calendar->addDate( $sunday, $one_day_dur, 'days' )->day_of_week, 1, |
405 |
'addDate skips closed Sunday (Days)' ); |
406 |
|
407 |
is( $calendar->addDate($today, $negative_one_day_dur , 'days')->ymd(), |
408 |
$today->add(days => - 1)->ymd(), |
409 |
'Negative call to addDate (Days)' ); |
410 |
|
411 |
#Days_forward |
412 |
|
413 |
#clean the range |
414 |
$today = DateTime->today; |
415 |
$calendar->edit_holiday({ |
416 |
title => '', |
417 |
holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{NONE}, |
418 |
start_date => $today, |
419 |
end_date => DateTime->today->add(days => 20) |
420 |
}); |
421 |
my $forwarded_dt = $calendar->days_forward($today, 10); |
422 |
|
423 |
my $expected = $today->clone; |
424 |
$expected->add(days => 10); |
425 |
is($forwarded_dt->ymd, $expected->ymd, 'With no holiday on the perioddays_forward should add 10 days'); |
426 |
|
427 |
#Added a holiday |
428 |
$unique_holiday = DateTime->today->add(days => 15); |
429 |
$calendar->edit_holiday({ |
430 |
title => "Single holiday Today", |
431 |
holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION}, |
432 |
start_date => $unique_holiday, |
433 |
end_date => $unique_holiday |
434 |
}); |
435 |
$forwarded_dt = $calendar->days_forward($today, 20); |
436 |
|
437 |
$expected->add(days => 11); |
438 |
is($forwarded_dt->ymd, $expected->ymd, 'With holiday on the perioddays_forward should add 20 days + 1 day for holiday'); |
439 |
|
440 |
$forwarded_dt = $calendar->days_forward($today, 0); |
441 |
is($forwarded_dt->ymd, $today->ymd, '0 day should return start dt'); |
442 |
|
443 |
$forwarded_dt = $calendar->days_forward($today, -2); |
444 |
is($forwarded_dt->ymd, $today->ymd, 'negative day should return start dt'); |
445 |
|
446 |
#copying a branch to an other |
447 |
is($calendar2->is_opened($tomorrow), 1, "$branch2 opened tomorrow"); |
448 |
#Added a goliday tomorrow for branch1 |
449 |
$calendar->edit_holiday({ |
450 |
title => "Single holiday Today", |
451 |
holiday_type => $Koha::DiscreteCalendar::HOLIDAYS->{EXCEPTION}, |
452 |
start_date => $tomorrow, |
453 |
end_date => $tomorrow |
454 |
}); |
455 |
#Copy dates from branch1 to branch2 |
456 |
$calendar->copy_to_branch($branch2); |
457 |
#Check if branch2 is also closed tomorrow after copying from branch1 |
458 |
is($calendar2->is_opened($tomorrow), 0, "$branch2 close tomorrow after copying from $branch1"); |
459 |
|
460 |
$schema->storage->txn_rollback; |
461 |
|
462 |
1; |