|
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 =>'DFLT' |
| 40 |
})->update_all({ |
| 41 |
isopened => 1, |
| 42 |
holidaytype => '', |
| 43 |
note => '', |
| 44 |
openhour => '08:00:00', |
| 45 |
closehour => '16:00:00' |
| 46 |
}); |
| 47 |
|
| 48 |
#Added entries for the branches. |
| 49 |
Koha::DiscreteCalendar->new( branchcode => 'DFLT' )->add_new_branch('DFLT', $branch1); |
| 50 |
Koha::DiscreteCalendar->new( branchcode => 'DFLT' )->add_new_branch('DFLT', $branch2); |
| 51 |
|
| 52 |
isnt($branch1,'', "First branch to do tests. BranchCode => $branch1"); |
| 53 |
isnt($branch1,'', "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("Single holiday Today", '', "E", '', '', $unique_holiday, $unique_holiday); |
| 61 |
is($calendar->isOpened($unique_holiday), 0, "Branch closed today : $unique_holiday"); |
| 62 |
|
| 63 |
my @unique_holidays = $calendar->get_unique_holidays(); |
| 64 |
is(scalar @unique_holidays, 1, "Set of exception holidays at 1"); |
| 65 |
|
| 66 |
my $days_between_start = DateTime->today; |
| 67 |
my $days_between_end = DateTime->today->add(days => 6); |
| 68 |
my $days_between = $calendar->days_between($days_between_start, $days_between_end)->in_units('days'); |
| 69 |
is($days_between, 5, "Days between skips holiday"); |
| 70 |
|
| 71 |
my $repeatable_holiday = DateTime->today->add(days => 1); |
| 72 |
$calendar->edit_holiday("repeatable", '', "R", '', '', $repeatable_holiday, $repeatable_holiday); |
| 73 |
is($calendar->isOpened($repeatable_holiday), 0, "Branch not opened on $repeatable_holiday"); |
| 74 |
|
| 75 |
my @repeatable_holidays = $calendar->get_repeatable_holidays(); |
| 76 |
is(scalar @repeatable_holidays, 1, "Set of repeatable holidays at 1"); |
| 77 |
|
| 78 |
# 1 being mysql DAYOFWEEK() for Sunday |
| 79 |
$calendar->edit_holiday("Weekly",1, "W", '', '', $today, $today); |
| 80 |
my @weekly_holidays = $calendar->get_week_days_holidays(); |
| 81 |
is(scalar @weekly_holidays, 1, "Set of weekly holidays at 1"); |
| 82 |
|
| 83 |
my $sunday = DateTime->today->add(days =>( 7 - $today->day_of_week)); |
| 84 |
is($calendar->isOpened($sunday), 0, "Branch not opened on " . $sunday->day_name ."s"); |
| 85 |
|
| 86 |
my $unique_holiday_range_start = DateTime->today->add(days => 2); |
| 87 |
my $unique_holiday_range_end = DateTime->today->add(days => 7); |
| 88 |
$calendar->edit_holiday("Single holiday range", '', "E", '', '', $unique_holiday_range_start, $unique_holiday_range_end); |
| 89 |
@unique_holidays = $calendar->get_unique_holidays(); |
| 90 |
is(scalar @unique_holidays, 7, "Set of exception holidays at 7"); |
| 91 |
|
| 92 |
my $repeatable_holiday_range_start = DateTime->today->add(days => 8); |
| 93 |
my $repeatable_holiday_range_end = DateTime->today->add(days => 13); |
| 94 |
$calendar->edit_holiday("Repeatable holiday range", '', "R", '', '', $repeatable_holiday_range_start, $repeatable_holiday_range_end); |
| 95 |
@repeatable_holidays = $calendar->get_repeatable_holidays(); |
| 96 |
is(scalar @repeatable_holidays, 7, "Set of repeatable holidays at 7"); |
| 97 |
|
| 98 |
#Hourly loan |
| 99 |
# ex : |
| 100 |
# item due : 2017-01-24 11:00:00 |
| 101 |
# item returned : 2017-01-26 10:00:00 |
| 102 |
# Branch closed : 2017-01-25 |
| 103 |
# Open/close hours : 8AM to 4PM (8h day) |
| 104 |
# Hours due : 5 hours on 2017-01-24 + 2 hours on 2017-01-26 = 7hours |
| 105 |
|
| 106 |
my $open_hours_since_start = DateTime->today->add(days => 40, hours => 11); |
| 107 |
my $open_hours_since_end = DateTime->today->add(days => 42, hours => 10); |
| 108 |
my $holiday_between = DateTime->today->add(days => 41); |
| 109 |
$calendar->edit_holiday('', '', 'none', '', '', $open_hours_since_start, $open_hours_since_end); |
| 110 |
|
| 111 |
$calendar->edit_holiday("Single holiday", '', "E", '', '', $holiday_between,$holiday_between); |
| 112 |
my $open_hours_between = $calendar->open_hours_between($open_hours_since_start, $open_hours_since_end); |
| 113 |
is($open_hours_between, 7, "7 Hours open between $open_hours_since_start and $open_hours_since_end"); |
| 114 |
|
| 115 |
my $christmas = DateTime->new( |
| 116 |
year => $today->year(), |
| 117 |
month => 12, |
| 118 |
day => 25, |
| 119 |
); |
| 120 |
$calendar->edit_holiday("Christmas", '', "R", '', '', $christmas, $christmas); |
| 121 |
is($calendar->isOpened($christmas), 0, "Branch closed on Christmas : $christmas"); |
| 122 |
|
| 123 |
my $newyear = DateTime->new( |
| 124 |
year => $today->year() +1, |
| 125 |
month => 1, |
| 126 |
day => 1, |
| 127 |
); |
| 128 |
|
| 129 |
$calendar->edit_holiday("New Year", '', "R", '', '', $newyear, $newyear); |
| 130 |
is($calendar->isOpened($newyear), 0, "Branch closed on New Year : $newyear"); |
| 131 |
|
| 132 |
#Hours between : |
| 133 |
my $date_due = DateTime->today->add(days =>1); |
| 134 |
my $date_returned = DateTime->today->add(hours => 8); |
| 135 |
my $hours_between = $calendar->hours_between($date_due, $date_returned); |
| 136 |
|
| 137 |
is($hours_between->in_units('hours'), 16, "Date due $date_due, returned $date_returned, 16 hours in advance"); |
| 138 |
|
| 139 |
$date_due = DateTime->today->add(days =>1); |
| 140 |
$date_returned = DateTime->today->add(days => 2, hours=> 8); |
| 141 |
$hours_between = $calendar->hours_between($date_due, $date_returned); |
| 142 |
|
| 143 |
is($hours_between->in_units('hours'), -16, "Date due $date_due, returned $date_returned, 16 hours late"); |
| 144 |
|
| 145 |
|
| 146 |
#delete holidays |
| 147 |
is($calendar->isOpened($today), 0, "Today is closed"); |
| 148 |
$calendar->edit_holiday('', '', 'none', '', '', $unique_holiday, $unique_holiday); |
| 149 |
is($calendar->isOpened($today), 1, "Today's holiday was removed"); |
| 150 |
|
| 151 |
my $new_open_hours = '08:00'; |
| 152 |
$calendar->edit_holiday('', '', '', $new_open_hours, '', $today, $today); |
| 153 |
is($calendar->get_date_info($today, $branch1)->{openhour}, '08:00:00', "Open hour changed to 08:00:00"); |
| 154 |
|
| 155 |
isnt($calendar->get_date_info($today, $branch1)->{closehour}, '', "Close hour not removed"); |
| 156 |
|
| 157 |
my $delete_range_end = DateTime->today->add(days => 30); |
| 158 |
$calendar->edit_holiday('', '', 'none', '', '', $today, $delete_range_end); |
| 159 |
|
| 160 |
|
| 161 |
is($calendar->isOpened($today), 1, "Today's holiday was already removed"); |
| 162 |
is($calendar->isOpened(DateTime->today->add(days => 1)), 1, "Tomorrow's holiday was removed"); |
| 163 |
is($calendar->isOpened($delete_range_end), 1, "End of range holiday was removed"); |
| 164 |
|
| 165 |
#Check if other branch was affected |
| 166 |
my @unique_holidays_branch2 = $calendar2->get_unique_holidays(); |
| 167 |
is(scalar @unique_holidays_branch2, 0, "Set of exception holidays at 0 for branch => $branch2"); |
| 168 |
my @repeatable_holidays_branch2 = $calendar2->get_repeatable_holidays(); |
| 169 |
is(scalar @repeatable_holidays_branch2, 0, "Set of repeatable holidays at 0 for branch => $branch2"); |
| 170 |
my @weekly_holidays_branch2 = $calendar2->get_week_days_holidays(); |
| 171 |
is(scalar @weekly_holidays_branch2, 0, "Set of weekly holidays at 0 for branch => $branch2"); |
| 172 |
|
| 173 |
|
| 174 |
#Tests for addDate() |
| 175 |
|
| 176 |
my $one_day_dur = DateTime::Duration->new( days => 1 ); |
| 177 |
my $negative_one_day_dur = DateTime::Duration->new( days => - 1 ); |
| 178 |
my $two_day_dur = DateTime::Duration->new( days => 2 ); |
| 179 |
my $seven_day_dur = DateTime::Duration->new( days => 7 ); |
| 180 |
|
| 181 |
#Preference useDaysMode Calendar |
| 182 |
$calendar->{days_mode} = 'Calendar'; |
| 183 |
$unique_holiday->add(days => 1); |
| 184 |
my $tomorrow = DateTime->today->add(days => 1); |
| 185 |
$calendar->edit_holiday("Single holiday Today", '', "E", '', '', $tomorrow, $tomorrow); |
| 186 |
|
| 187 |
is($calendar->addDate( $today, $one_day_dur, 'days' )->ymd(), |
| 188 |
$today->add(days => 2)->ymd(), |
| 189 |
'Single day add (Calendar)' ); |
| 190 |
|
| 191 |
is($calendar->addDate( $today, $two_day_dur, 'days' )->ymd(), |
| 192 |
$today->add(days => 2)->ymd(), |
| 193 |
'Two days add, skips holiday (Calendar)' ); |
| 194 |
|
| 195 |
cmp_ok($calendar->addDate( $today, $seven_day_dur, 'days' )->ymd(), 'eq', |
| 196 |
$today->add(days => 7)->ymd(), |
| 197 |
'Add 7 days (Calendar)' ); |
| 198 |
#Closed Sunday |
| 199 |
$calendar->edit_holiday("Weekly",1, "W", '', '', $today, $today); |
| 200 |
is( $calendar->addDate( $sunday, $one_day_dur, 'days' )->day_of_week, 1, |
| 201 |
'addDate skips closed Sunday (Calendar)' ); |
| 202 |
#to remove the closed sundays |
| 203 |
$today = DateTime->today; |
| 204 |
$calendar->edit_holiday('', '', 'none', '', '', $today, $delete_range_end); |
| 205 |
|
| 206 |
is( $calendar->addDate($today, $negative_one_day_dur , 'days')->ymd(), |
| 207 |
$today->add(days => - 1)->ymd(), |
| 208 |
'Negative call to addDate (Calendar)' ); |
| 209 |
|
| 210 |
#Preference useDaysMode DateDue |
| 211 |
$calendar->{days_mode} = 'Datedue'; |
| 212 |
#clean everything |
| 213 |
$today = DateTime->today; |
| 214 |
$calendar->edit_holiday('', '', 'none', '', '', $today, $delete_range_end); |
| 215 |
$calendar->edit_holiday("Single holiday Today", '', "E", '', '', $tomorrow, $tomorrow); |
| 216 |
|
| 217 |
is($calendar->addDate( $today, $one_day_dur, 'days' )->ymd(), |
| 218 |
$today->add(days => 2)->ymd(), |
| 219 |
'Single day add' ); |
| 220 |
|
| 221 |
is($calendar->addDate( $today, $two_day_dur, 'days' )->ymd(), |
| 222 |
$today->add(days => 2)->ymd(), |
| 223 |
'Two days add, skips holiday (Datedue)' ); |
| 224 |
|
| 225 |
cmp_ok($calendar->addDate( $today, $seven_day_dur, 'days' )->ymd(), 'eq', |
| 226 |
$today->add(days => 7)->ymd(), |
| 227 |
'Add 7 days (Datedue)' ); |
| 228 |
#Closed Sunday |
| 229 |
$calendar->edit_holiday("Weekly",1, "W", '', '', $today, $today); |
| 230 |
is( $calendar->addDate( $sunday, $one_day_dur, 'days' )->day_of_week, 1, |
| 231 |
'addDate skips closed Sunday (Datedue)' ); |
| 232 |
#to remove the closed sundays |
| 233 |
$today = DateTime->today; |
| 234 |
$calendar->edit_holiday('', '', 'none', '', '', $today, $delete_range_end); |
| 235 |
|
| 236 |
is( $calendar->addDate($today, $negative_one_day_dur , 'days')->ymd(), |
| 237 |
$today->add(days => - 1)->ymd(), |
| 238 |
'Negative call to addDate (Datedue)' ); |
| 239 |
|
| 240 |
#Preference useDaysMode Days |
| 241 |
$calendar->{days_mode} = 'Days'; |
| 242 |
#clean everything |
| 243 |
$today = DateTime->today; |
| 244 |
$calendar->edit_holiday('', '', 'none', '', '', $today, $delete_range_end); |
| 245 |
$calendar->edit_holiday("Single holiday Today", '', "E", '', '', $tomorrow, $tomorrow); |
| 246 |
is($calendar->addDate( $today, $one_day_dur, 'days' )->ymd(), |
| 247 |
$today->add(days => 1)->ymd(), |
| 248 |
'Single day add' ); |
| 249 |
|
| 250 |
is($calendar->addDate( $today, $two_day_dur, 'days' )->ymd(), |
| 251 |
$today->add(days => 2)->ymd(), |
| 252 |
'Two days add, skips holiday (Days)' ); |
| 253 |
|
| 254 |
cmp_ok($calendar->addDate( $today, $seven_day_dur, 'days' )->ymd(), 'eq', |
| 255 |
$today->add(days => 7)->ymd(), |
| 256 |
'Add 7 days (Days)' ); |
| 257 |
#Closed Sunday |
| 258 |
$calendar->edit_holiday("Weekly",1, "W", '', '', $today, $today); |
| 259 |
is( $calendar->addDate( $sunday, $one_day_dur, 'days' )->day_of_week, 1, |
| 260 |
'addDate skips closed Sunday (Days)' ); |
| 261 |
|
| 262 |
is( $calendar->addDate($today, $negative_one_day_dur , 'days')->ymd(), |
| 263 |
$today->add(days => - 1)->ymd(), |
| 264 |
'Negative call to addDate (Days)' ); |
| 265 |
|
| 266 |
#Days_forward |
| 267 |
|
| 268 |
#clean the range |
| 269 |
$today = DateTime->today; |
| 270 |
$calendar->edit_holiday('', '', 'none', '', '', $today, DateTime->today->add(days => 20)); |
| 271 |
my $forwarded_dt = $calendar->days_forward($today, 10); |
| 272 |
|
| 273 |
my $expected = $today->clone; |
| 274 |
$expected->add(days => 10); |
| 275 |
is($forwarded_dt->ymd, $expected->ymd, 'With no holiday on the perioddays_forward should add 10 days'); |
| 276 |
|
| 277 |
#Added a holiday |
| 278 |
$unique_holiday = DateTime->today->add(days => 15); |
| 279 |
$calendar->edit_holiday("Single holiday Today", '', "E", '', '',$unique_holiday, $unique_holiday); |
| 280 |
$forwarded_dt = $calendar->days_forward($today, 20); |
| 281 |
|
| 282 |
$expected->add(days => 11); |
| 283 |
is($forwarded_dt->ymd, $expected->ymd, 'With holiday on the perioddays_forward should add 20 days + 1 day for holiday'); |
| 284 |
|
| 285 |
$forwarded_dt = $calendar->days_forward($today, 0); |
| 286 |
is($forwarded_dt->ymd, $today->ymd, '0 day should return start dt'); |
| 287 |
|
| 288 |
$forwarded_dt = $calendar->days_forward($today, -2); |
| 289 |
is($forwarded_dt->ymd, $today->ymd, 'negative day should return start dt'); |
| 290 |
|
| 291 |
#copying a branch to an other |
| 292 |
is($calendar2->isOpened($tomorrow), 1, "$branch2 opened tomorrow"); |
| 293 |
#Added a goliday tomorrow for branch1 |
| 294 |
$calendar->edit_holiday("Single holiday Today", '', "E", '', '', $tomorrow, $tomorrow); |
| 295 |
#Copy dates from branch1 to branch2 |
| 296 |
$calendar->copyToBranch($branch2); |
| 297 |
#Check if branch2 is also closed tomorrow after copying from branch1 |
| 298 |
is($calendar2->isOpened($tomorrow), 0, "$branch2 close tomorrow after copying from $branch1"); |
| 299 |
|
| 300 |
$schema->storage->txn_rollback; |
| 301 |
|
| 302 |
1; |