3 # This file is part of Koha.
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
24 use DateTime::Duration;
26 use Koha::DateUtils qw( dt_from_string );
30 use Module::Load::Conditional qw/check_install/;
33 if ( check_install( module => 'Test::DBIx::Class' ) ) {
36 plan skip_all => "Need Test::DBIx::Class"
40 use_ok('Koha::Calendar');
42 use Test::DBIx::Class;
44 my $db = Test::MockModule->new('Koha::Database');
46 _new_schema => sub { return Schema(); }
49 # We need to mock the C4::Context->preference method for
50 # simplicity and re-usability of the session definition. Any
51 # syspref fits for syspref-agnostic tests.
52 my $module_context = Test::MockModule->new('C4::Context');
53 $module_context->mock(
62 RepeatableHoliday => [
63 [ qw( branchcode day month weekday title description) ],
64 [ 'MPL', undef, undef, 0, '', '' ], # sundays
65 [ 'MPL', undef, undef, 6, '', '' ],# saturdays
66 [ 'MPL', 1, 1, undef, '', ''], # new year's day
67 [ 'MPL', 25, 12, undef, '', ''], # chrismas
71 [qw( branchcode day month year title description isexception )],
72 [ 'MPL', 11, 11, 2012, '', '', 1 ], # sunday exception
73 [ 'MPL', 1, 6, 2011, '', '', 0 ],
74 [ 'MPL', 4, 7, 2012, '', '', 0 ],
75 [ 'CPL', 6, 8, 2012, '', '', 0 ],
76 [ 'MPL', 7, 7, 2012, '', '', 1 ], # holiday exception
77 [ 'MPL', 7, 7, 2012, '', '', 0 ], # holiday
81 my $cache = Koha::Caches->get_instance();
82 $cache->clear_from_cache('MPL_holidays');
83 $cache->clear_from_cache('CPL_holidays');
85 # 'MPL' branch is arbitrary, is not used at all but is needed for initialization
86 my $cal = Koha::Calendar->new( branchcode => 'MPL' );
88 isa_ok( $cal, 'Koha::Calendar', 'Calendar class returned' );
90 my $saturday = DateTime->new(
96 my $sunday = DateTime->new(
102 my $monday = DateTime->new(
108 my $new_year = DateTime->new(
114 my $single_holiday = DateTime->new(
118 ); # should be a holiday
120 my $notspecial = DateTime->new(
124 ); # should NOT be a holiday
126 my $sunday_exception = DateTime->new(
132 my $day_after_christmas = DateTime->new(
136 ); # for testing negative addDuration
138 my $holiday_for_another_branch = DateTime->new(
141 day => 6, # This is a monday
144 my $holiday_excepted = DateTime->new(
147 day => 7, # Both a holiday and exception
150 { # Syspref-agnostic tests
151 is ( $saturday->day_of_week, 6, '\'$saturday\' is actually a saturday (6th day of week)');
152 is ( $sunday->day_of_week, 7, '\'$sunday\' is actually a sunday (7th day of week)');
153 is ( $monday->day_of_week, 1, '\'$monday\' is actually a monday (1st day of week)');
154 is ( $cal->is_holiday($saturday), 1, 'Saturday is a closed day' );
155 is ( $cal->is_holiday($sunday), 1, 'Sunday is a closed day' );
156 is ( $cal->is_holiday($monday), 0, 'Monday is not a closed day' );
157 is ( $cal->is_holiday($new_year), 1, 'Month/Day closed day test (New year\'s day)' );
158 is ( $cal->is_holiday($single_holiday), 1, 'Single holiday closed day test' );
159 is ( $cal->is_holiday($notspecial), 0, 'Fixed single date that is not a holiday test' );
160 is ( $cal->is_holiday($sunday_exception), 0, 'Exception holiday is not a closed day test' );
161 is ( $cal->is_holiday($holiday_for_another_branch), 0, 'Holiday defined for another branch should not be defined as an holiday' );
162 is ( $cal->is_holiday($holiday_excepted), 0, 'Holiday defined and excepted should not be a holiday' );
165 { # Bugzilla #8966 - is_holiday truncates referenced date
166 my $later_dt = DateTime->new( # Monday
172 time_zone => 'Europe/London',
176 is( $cal->is_holiday($later_dt), 0, 'bz-8966 (1/2) Apply is_holiday for the next test' );
177 cmp_ok( $later_dt, 'eq', '2012-09-17T17:30:00', 'bz-8966 (2/2) Date should be the same after is_holiday' );
180 { # Bugzilla #8800 - is_holiday should use truncated date for 'contains' call
181 my $single_holiday_time = DateTime->new(
189 is( $cal->is_holiday($single_holiday_time),
190 $cal->is_holiday($single_holiday) ,
191 'bz-8800 is_holiday should truncate the date for holiday validation' );
194 my $one_day_dur = DateTime::Duration->new( days => 1 );
195 my $two_day_dur = DateTime::Duration->new( days => 2 );
196 my $seven_day_dur = DateTime::Duration->new( days => 7 );
198 my $dt = dt_from_string( '2012-07-03','iso' ); #tuesday
200 my $test_dt = DateTime->new( # Monday
208 my $later_dt = DateTime->new( # Monday
214 time_zone => 'Europe/London',
219 $cal = Koha::Calendar->new( branchcode => 'MPL', days_mode => 'Datedue' );
221 is($cal->addDuration( $dt, $one_day_dur, 'days' ), # tuesday
222 dt_from_string('2012-07-05','iso'),
223 'Single day add (Datedue, matches holiday, shift)' );
225 is($cal->addDuration( $dt, $two_day_dur, 'days' ),
226 dt_from_string('2012-07-05','iso'),
227 'Two days add, skips holiday (Datedue)' );
229 cmp_ok($cal->addDuration( $test_dt, $seven_day_dur, 'days' ), 'eq',
230 '2012-07-30T11:53:00',
231 'Add 7 days (Datedue)' );
233 is( $cal->addDuration( $saturday, $one_day_dur, 'days' )->day_of_week, 1,
234 'addDuration skips closed Sunday (Datedue)' );
236 is( $cal->addDuration($day_after_christmas, -1, 'days')->ymd(), '2012-12-24',
237 'Negative call to addDuration (Datedue)' );
239 ## Note that the days_between API says closed days are not considered.
240 ## This tests are here as an API test.
241 cmp_ok( $cal->days_between( $test_dt, $later_dt )->in_units('days'),
242 '==', 40, 'days_between calculates correctly (Days)' );
244 cmp_ok( $cal->days_between( $later_dt, $test_dt )->in_units('days'),
245 '==', 40, 'Test parameter order not relevant (Days)' );
248 { ## 'Calendar' tests'
250 $cal = Koha::Calendar->new( branchcode => 'MPL', days_mode => 'Calendar' );
252 $dt = dt_from_string('2012-07-03','iso');
254 is($cal->addDuration( $dt, $one_day_dur, 'days' ),
255 dt_from_string('2012-07-05','iso'),
256 'Single day add (Calendar)' );
258 cmp_ok($cal->addDuration( $test_dt, $seven_day_dur, 'days' ), 'eq',
259 '2012-08-01T11:53:00',
260 'Add 7 days (Calendar)' );
262 is( $cal->addDuration( $saturday, $one_day_dur, 'days' )->day_of_week, 1,
263 'addDuration skips closed Sunday (Calendar)' );
265 is( $cal->addDuration($day_after_christmas, -1, 'days')->ymd(), '2012-12-24',
266 'Negative call to addDuration (Calendar)' );
268 cmp_ok( $cal->days_between( $test_dt, $later_dt )->in_units('days'),
269 '==', 40, 'days_between calculates correctly (Calendar)' );
271 cmp_ok( $cal->days_between( $later_dt, $test_dt )->in_units('days'),
272 '==', 40, 'Test parameter order not relevant (Calendar)' );
278 $cal = Koha::Calendar->new( branchcode => 'MPL', days_mode => 'Days' );
280 $dt = dt_from_string('2012-07-03','iso');
282 is($cal->addDuration( $dt, $one_day_dur, 'days' ),
283 dt_from_string('2012-07-04','iso'),
284 'Single day add (Days)' );
286 cmp_ok($cal->addDuration( $test_dt, $seven_day_dur, 'days' ),'eq',
287 '2012-07-30T11:53:00',
288 'Add 7 days (Days)' );
290 is( $cal->addDuration( $saturday, $one_day_dur, 'days' )->day_of_week, 7,
291 'addDuration doesn\'t skip closed Sunday (Days)' );
293 is( $cal->addDuration($day_after_christmas, -1, 'days')->ymd(), '2012-12-25',
294 'Negative call to addDuration (Days)' );
296 ## Note that the days_between API says closed days are not considered.
297 ## This tests are here as an API test.
298 cmp_ok( $cal->days_between( $test_dt, $later_dt )->in_units('days'),
299 '==', 40, 'days_between calculates correctly (Days)' );
301 cmp_ok( $cal->days_between( $later_dt, $test_dt )->in_units('days'),
302 '==', 40, 'Test parameter order not relevant (Days)' );
307 $cal = Koha::Calendar->new( branchcode => 'CPL' );
308 is ( $cal->is_holiday($single_holiday), 0, 'Single holiday for MPL, not CPL' );
309 is ( $cal->is_holiday($holiday_for_another_branch), 1, 'Holiday defined for CPL should be defined as an holiday' );
312 subtest 'days_mode parameter' => sub {
315 t::lib::Mocks::mock_preference('useDaysMode', 'Days');
317 $cal = Koha::Calendar->new( branchcode => 'CPL', days_mode => 'Calendar' );
318 is( $cal->{days_mode}, 'Calendar', q|If set, days_mode is correctly set|);
322 $cache->clear_from_cache('MPL_holidays');
323 $cache->clear_from_cache('CPL_holidays');