|
Lines 24-35
use t::lib::Mocks;
Link Here
|
| 24 |
use t::lib::TestBuilder; |
24 |
use t::lib::TestBuilder; |
| 25 |
|
25 |
|
| 26 |
use DateTime; |
26 |
use DateTime; |
|
|
27 |
use DateTime::Duration; |
| 27 |
use Koha::Caches; |
28 |
use Koha::Caches; |
|
|
29 |
use Koha::Calendar; |
| 30 |
use Koha::Database; |
| 28 |
use Koha::DateUtils qw( dt_from_string ); |
31 |
use Koha::DateUtils qw( dt_from_string ); |
| 29 |
|
32 |
|
| 30 |
use_ok('Koha::Calendar'); |
|
|
| 31 |
|
| 32 |
my $schema = Koha::Database->new->schema; |
33 |
my $schema = Koha::Database->new->schema; |
|
|
34 |
my $builder = t::lib::TestBuilder->new; |
| 33 |
$schema->storage->txn_begin; |
35 |
$schema->storage->txn_begin; |
| 34 |
|
36 |
|
| 35 |
my $today = dt_from_string(); |
37 |
my $today = dt_from_string(); |
|
Lines 38-45
$holiday_dt->add(days => 3);
Link Here
|
| 38 |
|
40 |
|
| 39 |
Koha::Caches->get_instance()->flush_all(); |
41 |
Koha::Caches->get_instance()->flush_all(); |
| 40 |
|
42 |
|
| 41 |
my $builder = t::lib::TestBuilder->new(); |
43 |
subtest 'Original tests from t' => sub { |
|
|
44 |
# We need to mock the C4::Context->preference method for |
| 45 |
# simplicity and re-usability of the session definition. Any |
| 46 |
# syspref fits for syspref-agnostic tests. |
| 47 |
my $module_context = Test::MockModule->new('C4::Context'); |
| 48 |
$module_context->mock( |
| 49 |
'preference', |
| 50 |
sub { |
| 51 |
return 'Calendar'; |
| 52 |
} |
| 53 |
); |
| 54 |
|
| 55 |
my $mpl = $builder->build_object({ class => 'Koha::Libraries' })->branchcode; |
| 56 |
my $cpl = $builder->build_object({ class => 'Koha::Libraries' })->branchcode; |
| 57 |
my $rows = [ # add weekly holidays |
| 58 |
{ branchcode => $mpl, weekday => 0 }, # sundays |
| 59 |
{ branchcode => $mpl, weekday => 6 }, # saturdays |
| 60 |
{ branchcode => $mpl, day => 1, month => 1 }, # new year's day |
| 61 |
{ branchcode => $mpl, day => 25, month => 12 }, # chrismas |
| 62 |
]; |
| 63 |
$schema->resultset('RepeatableHoliday')->delete_all; |
| 64 |
$schema->resultset('RepeatableHoliday')->create({ %$_, description => q{} }) for @$rows; |
| 65 |
|
| 66 |
$rows = [ # exception holidays |
| 67 |
{ branchcode => $mpl, day => 11, month => 11, year => 2012, isexception => 1 }, # sunday exception |
| 68 |
{ branchcode => $mpl, day => 1, month => 6, year => 2011, isexception => 0 }, |
| 69 |
{ branchcode => $mpl, day => 4, month => 7, year => 2012, isexception => 0 }, |
| 70 |
{ branchcode => $cpl, day => 6, month => 8, year => 2012, isexception => 0 }, |
| 71 |
{ branchcode => $mpl, day => 7, month => 7, year => 2012, isexception => 1 }, # holiday exception |
| 72 |
{ branchcode => $mpl, day => 7, month => 7, year => 2012, isexception => 0 }, # holiday |
| 73 |
]; |
| 74 |
$schema->resultset('SpecialHoliday')->delete_all; |
| 75 |
$schema->resultset('SpecialHoliday')->create({ %$_, description => q{} }) for @$rows; |
| 76 |
|
| 77 |
my $cache = Koha::Caches->get_instance(); |
| 78 |
$cache->clear_from_cache( $mpl.'_holidays' ); |
| 79 |
$cache->clear_from_cache( $cpl.'_holidays' ); |
| 80 |
|
| 81 |
# $mpl branch is arbitrary, is not used at all but is needed for initialization |
| 82 |
my $cal = Koha::Calendar->new( branchcode => $mpl ); |
| 83 |
|
| 84 |
isa_ok( $cal, 'Koha::Calendar', 'Calendar class returned' ); |
| 85 |
|
| 86 |
my $saturday = DateTime->new( |
| 87 |
year => 2012, |
| 88 |
month => 11, |
| 89 |
day => 24, |
| 90 |
); |
| 91 |
|
| 92 |
my $sunday = DateTime->new( |
| 93 |
year => 2012, |
| 94 |
month => 11, |
| 95 |
day => 25, |
| 96 |
); |
| 97 |
|
| 98 |
my $monday = DateTime->new( |
| 99 |
year => 2012, |
| 100 |
month => 11, |
| 101 |
day => 26, |
| 102 |
); |
| 103 |
|
| 104 |
my $new_year = DateTime->new( |
| 105 |
year => 2013, |
| 106 |
month => 1, |
| 107 |
day => 1, |
| 108 |
); |
| 109 |
|
| 110 |
my $single_holiday = DateTime->new( |
| 111 |
year => 2011, |
| 112 |
month => 6, |
| 113 |
day => 1, |
| 114 |
); # should be a holiday |
| 115 |
|
| 116 |
my $notspecial = DateTime->new( |
| 117 |
year => 2011, |
| 118 |
month => 6, |
| 119 |
day => 2 |
| 120 |
); # should NOT be a holiday |
| 121 |
|
| 122 |
my $sunday_exception = DateTime->new( |
| 123 |
year => 2012, |
| 124 |
month => 11, |
| 125 |
day => 11 |
| 126 |
); |
| 127 |
|
| 128 |
my $day_after_christmas = DateTime->new( |
| 129 |
year => 2012, |
| 130 |
month => 12, |
| 131 |
day => 26 |
| 132 |
); # for testing negative addDuration |
| 133 |
|
| 134 |
my $holiday_for_another_branch = DateTime->new( |
| 135 |
year => 2012, |
| 136 |
month => 8, |
| 137 |
day => 6, # This is a monday |
| 138 |
); |
| 139 |
|
| 140 |
my $holiday_excepted = DateTime->new( |
| 141 |
year => 2012, |
| 142 |
month => 7, |
| 143 |
day => 7, # Both a holiday and exception |
| 144 |
); |
| 145 |
|
| 146 |
{ # Syspref-agnostic tests |
| 147 |
is ( $saturday->day_of_week, 6, '\'$saturday\' is actually a saturday (6th day of week)'); |
| 148 |
is ( $sunday->day_of_week, 7, '\'$sunday\' is actually a sunday (7th day of week)'); |
| 149 |
is ( $monday->day_of_week, 1, '\'$monday\' is actually a monday (1st day of week)'); |
| 150 |
is ( $cal->is_holiday($saturday), 1, 'Saturday is a closed day' ); |
| 151 |
is ( $cal->is_holiday($sunday), 1, 'Sunday is a closed day' ); |
| 152 |
is ( $cal->is_holiday($monday), 0, 'Monday is not a closed day' ); |
| 153 |
is ( $cal->is_holiday($new_year), 1, 'Month/Day closed day test (New year\'s day)' ); |
| 154 |
is ( $cal->is_holiday($single_holiday), 1, 'Single holiday closed day test' ); |
| 155 |
is ( $cal->is_holiday($notspecial), 0, 'Fixed single date that is not a holiday test' ); |
| 156 |
is ( $cal->is_holiday($sunday_exception), 0, 'Exception holiday is not a closed day test' ); |
| 157 |
is ( $cal->is_holiday($holiday_for_another_branch), 0, 'Holiday defined for another branch should not be defined as an holiday' ); |
| 158 |
is ( $cal->is_holiday($holiday_excepted), 0, 'Holiday defined and excepted should not be a holiday' ); |
| 159 |
} |
| 160 |
|
| 161 |
{ # Bugzilla #8966 - is_holiday truncates referenced date |
| 162 |
my $later_dt = DateTime->new( # Monday |
| 163 |
year => 2012, |
| 164 |
month => 9, |
| 165 |
day => 17, |
| 166 |
hour => 17, |
| 167 |
minute => 30, |
| 168 |
time_zone => 'Europe/London', |
| 169 |
); |
| 170 |
|
| 171 |
|
| 172 |
is( $cal->is_holiday($later_dt), 0, 'bz-8966 (1/2) Apply is_holiday for the next test' ); |
| 173 |
cmp_ok( $later_dt, 'eq', '2012-09-17T17:30:00', 'bz-8966 (2/2) Date should be the same after is_holiday' ); |
| 174 |
} |
| 175 |
|
| 176 |
{ # Bugzilla #8800 - is_holiday should use truncated date for 'contains' call |
| 177 |
my $single_holiday_time = DateTime->new( |
| 178 |
year => 2011, |
| 179 |
month => 6, |
| 180 |
day => 1, |
| 181 |
hour => 11, |
| 182 |
minute => 2 |
| 183 |
); |
| 184 |
|
| 185 |
is( $cal->is_holiday($single_holiday_time), |
| 186 |
$cal->is_holiday($single_holiday) , |
| 187 |
'bz-8800 is_holiday should truncate the date for holiday validation' ); |
| 188 |
} |
| 189 |
|
| 190 |
my $one_day_dur = DateTime::Duration->new( days => 1 ); |
| 191 |
my $two_day_dur = DateTime::Duration->new( days => 2 ); |
| 192 |
my $seven_day_dur = DateTime::Duration->new( days => 7 ); |
| 193 |
|
| 194 |
my $dt = dt_from_string( '2012-07-03','iso' ); #tuesday |
| 195 |
|
| 196 |
my $test_dt = DateTime->new( # Monday |
| 197 |
year => 2012, |
| 198 |
month => 7, |
| 199 |
day => 23, |
| 200 |
hour => 11, |
| 201 |
minute => 53, |
| 202 |
); |
| 203 |
|
| 204 |
my $later_dt = DateTime->new( # Monday |
| 205 |
year => 2012, |
| 206 |
month => 9, |
| 207 |
day => 17, |
| 208 |
hour => 17, |
| 209 |
minute => 30, |
| 210 |
time_zone => 'Europe/London', |
| 211 |
); |
| 212 |
|
| 213 |
{ ## 'Datedue' tests |
| 214 |
|
| 215 |
$cal = Koha::Calendar->new( branchcode => $mpl, days_mode => 'Datedue' ); |
| 216 |
|
| 217 |
is($cal->addDuration( $dt, $one_day_dur, 'days' ), # tuesday |
| 218 |
dt_from_string('2012-07-05','iso'), |
| 219 |
'Single day add (Datedue, matches holiday, shift)' ); |
| 220 |
|
| 221 |
is($cal->addDuration( $dt, $two_day_dur, 'days' ), |
| 222 |
dt_from_string('2012-07-05','iso'), |
| 223 |
'Two days add, skips holiday (Datedue)' ); |
| 224 |
|
| 225 |
cmp_ok($cal->addDuration( $test_dt, $seven_day_dur, 'days' ), 'eq', |
| 226 |
'2012-07-30T11:53:00', |
| 227 |
'Add 7 days (Datedue)' ); |
| 228 |
|
| 229 |
is( $cal->addDuration( $saturday, $one_day_dur, 'days' )->day_of_week, 1, |
| 230 |
'addDuration skips closed Sunday (Datedue)' ); |
| 231 |
|
| 232 |
is( $cal->addDuration($day_after_christmas, -1, 'days')->ymd(), '2012-12-24', |
| 233 |
'Negative call to addDuration (Datedue)' ); |
| 234 |
|
| 235 |
## Note that the days_between API says closed days are not considered. |
| 236 |
## This tests are here as an API test. |
| 237 |
cmp_ok( $cal->days_between( $test_dt, $later_dt )->in_units('days'), |
| 238 |
'==', 40, 'days_between calculates correctly (Days)' ); |
| 239 |
|
| 240 |
cmp_ok( $cal->days_between( $later_dt, $test_dt )->in_units('days'), |
| 241 |
'==', 40, 'Test parameter order not relevant (Days)' ); |
| 242 |
} |
| 243 |
|
| 244 |
{ ## 'Calendar' tests' |
| 245 |
|
| 246 |
$cal = Koha::Calendar->new( branchcode => $mpl, days_mode => 'Calendar' ); |
| 247 |
|
| 248 |
$dt = dt_from_string('2012-07-03','iso'); |
| 249 |
|
| 250 |
is($cal->addDuration( $dt, $one_day_dur, 'days' ), |
| 251 |
dt_from_string('2012-07-05','iso'), |
| 252 |
'Single day add (Calendar)' ); |
| 253 |
|
| 254 |
cmp_ok($cal->addDuration( $test_dt, $seven_day_dur, 'days' ), 'eq', |
| 255 |
'2012-08-01T11:53:00', |
| 256 |
'Add 7 days (Calendar)' ); |
| 257 |
|
| 258 |
is( $cal->addDuration( $saturday, $one_day_dur, 'days' )->day_of_week, 1, |
| 259 |
'addDuration skips closed Sunday (Calendar)' ); |
| 260 |
|
| 261 |
is( $cal->addDuration($day_after_christmas, -1, 'days')->ymd(), '2012-12-24', |
| 262 |
'Negative call to addDuration (Calendar)' ); |
| 263 |
|
| 264 |
cmp_ok( $cal->days_between( $test_dt, $later_dt )->in_units('days'), |
| 265 |
'==', 40, 'days_between calculates correctly (Calendar)' ); |
| 266 |
|
| 267 |
cmp_ok( $cal->days_between( $later_dt, $test_dt )->in_units('days'), |
| 268 |
'==', 40, 'Test parameter order not relevant (Calendar)' ); |
| 269 |
} |
| 270 |
|
| 271 |
|
| 272 |
{ ## 'Days' tests |
| 273 |
|
| 274 |
$cal = Koha::Calendar->new( branchcode => $mpl, days_mode => 'Days' ); |
| 275 |
|
| 276 |
$dt = dt_from_string('2012-07-03','iso'); |
| 277 |
|
| 278 |
is($cal->addDuration( $dt, $one_day_dur, 'days' ), |
| 279 |
dt_from_string('2012-07-04','iso'), |
| 280 |
'Single day add (Days)' ); |
| 281 |
|
| 282 |
cmp_ok($cal->addDuration( $test_dt, $seven_day_dur, 'days' ),'eq', |
| 283 |
'2012-07-30T11:53:00', |
| 284 |
'Add 7 days (Days)' ); |
| 285 |
|
| 286 |
is( $cal->addDuration( $saturday, $one_day_dur, 'days' )->day_of_week, 7, |
| 287 |
'addDuration doesn\'t skip closed Sunday (Days)' ); |
| 288 |
|
| 289 |
is( $cal->addDuration($day_after_christmas, -1, 'days')->ymd(), '2012-12-25', |
| 290 |
'Negative call to addDuration (Days)' ); |
| 291 |
|
| 292 |
## Note that the days_between API says closed days are not considered. |
| 293 |
## This tests are here as an API test. |
| 294 |
cmp_ok( $cal->days_between( $test_dt, $later_dt )->in_units('days'), |
| 295 |
'==', 40, 'days_between calculates correctly (Days)' ); |
| 296 |
|
| 297 |
cmp_ok( $cal->days_between( $later_dt, $test_dt )->in_units('days'), |
| 298 |
'==', 40, 'Test parameter order not relevant (Days)' ); |
| 299 |
|
| 300 |
} |
| 301 |
|
| 302 |
{ |
| 303 |
$cal = Koha::Calendar->new( branchcode => $cpl ); |
| 304 |
is ( $cal->is_holiday($single_holiday), 0, 'Single holiday for MPL, not CPL' ); |
| 305 |
is ( $cal->is_holiday($holiday_for_another_branch), 1, 'Holiday defined for CPL should be defined as an holiday' ); |
| 306 |
} |
| 307 |
|
| 308 |
subtest 'days_mode parameter' => sub { |
| 309 |
plan tests => 1; |
| 310 |
|
| 311 |
t::lib::Mocks::mock_preference('useDaysMode', 'Days'); |
| 312 |
|
| 313 |
$cal = Koha::Calendar->new( branchcode => $cpl, days_mode => 'Calendar' ); |
| 314 |
is( $cal->{days_mode}, 'Calendar', q|If set, days_mode is correctly set|); |
| 315 |
}; |
| 316 |
|
| 317 |
$cache->clear_from_cache( $mpl.'_holidays' ); |
| 318 |
$cache->clear_from_cache( $cpl.'_holidays' ); |
| 319 |
}; |
| 320 |
|
| 42 |
my $library = $builder->build_object({ class => 'Koha::Libraries' }); |
321 |
my $library = $builder->build_object({ class => 'Koha::Libraries' }); |
|
|
322 |
my $calendar = Koha::Calendar->new( branchcode => $library->branchcode, days_mode => 'Calendar' ); |
| 43 |
my $holiday = $builder->build( |
323 |
my $holiday = $builder->build( |
| 44 |
{ |
324 |
{ |
| 45 |
source => 'SpecialHoliday', |
325 |
source => 'SpecialHoliday', |
|
Lines 54-64
my $holiday = $builder->build(
Link Here
|
| 54 |
} |
334 |
} |
| 55 |
); |
335 |
); |
| 56 |
|
336 |
|
| 57 |
my $calendar = Koha::Calendar->new( branchcode => $library->branchcode, days_mode => 'Calendar' ); |
|
|
| 58 |
|
| 59 |
subtest 'days_forward' => sub { |
337 |
subtest 'days_forward' => sub { |
| 60 |
|
|
|
| 61 |
plan tests => 4; |
338 |
plan tests => 4; |
|
|
339 |
|
| 62 |
my $forwarded_dt = $calendar->days_forward( $today, 2 ); |
340 |
my $forwarded_dt = $calendar->days_forward( $today, 2 ); |
| 63 |
my $expected = $today->clone->add( days => 2 ); |
341 |
my $expected = $today->clone->add( days => 2 ); |
| 64 |
is( $forwarded_dt->ymd, $expected->ymd, 'With no holiday on the perioddays_forward should add 2 days' ); |
342 |
is( $forwarded_dt->ymd, $expected->ymd, 'With no holiday on the perioddays_forward should add 2 days' ); |
| 65 |
- |
|
|