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