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