6 use DateTime
::Duration
;
7 use Test
::More tests
=> 34;
14 use_ok
('Koha::Calendar');
16 # This was the only test C4 had
17 # Remove when no longer used
18 #use_ok('C4::Calendar'); # not used anymore?
21 my $module_context = new Test
::MockModule
('C4::Context');
22 $module_context->mock(
25 my $dbh = DBI
->connect( 'DBI:Mock:', '', '' )
26 || die "Cannot create handle: $DBI::errstr\n";
31 # We need to mock the C4::Context->preference method for
32 # simplicity and re-usability of the session definition. Any
33 # syspref fits for syspref-agnostic tests.
34 $module_context->mock(
43 skip
"DBD::Mock is too old", 33
44 unless $DBD::Mock
::VERSION
>= 1.45;
46 my $holidays_session = DBD
::Mock
::Session
->new('holidays_session' => (
48 statement
=> "SELECT weekday FROM repeatable_holidays WHERE branchcode = ? AND weekday IS NOT NULL",
55 { # day and month repeatable holidays
56 statement
=> "SELECT day, month FROM repeatable_holidays WHERE branchcode = ? AND weekday IS NULL",
59 [ 1, 1 ], # new year's day
63 { # exception holidays
64 statement
=> "SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = 1",
66 [ 'day', 'month', 'year' ],
67 [ 11, 11, 2012 ] # sunday exception
72 statement
=> "SELECT distinct(branchcode) FROM special_holidays",
80 statement
=> "SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = 0",
82 [ 'day', 'month', 'year' ],
83 [ 1, 6, 2011 ], # single holiday
89 # Initialize the global $dbh variable
90 my $dbh = C4
::Context
->dbh();
91 # Apply the mock session
92 $dbh->{ mock_session
} = $holidays_session;
95 my $cache = Koha
::Cache
->get_instance();
96 $cache->clear_from_cache( 'single_holidays') ;
99 # 'MPL' branch is arbitrary, is not used at all but is needed for initialization
100 my $cal = Koha
::Calendar
->new( branchcode
=> 'MPL' );
102 isa_ok
( $cal, 'Koha::Calendar', 'Calendar class returned' );
105 my $saturday = DateTime
->new(
111 my $sunday = DateTime
->new(
117 my $monday = DateTime
->new(
123 my $new_year = DateTime
->new(
129 my $single_holiday = DateTime
->new(
133 ); # should be a holiday
135 my $notspecial = DateTime
->new(
139 ); # should NOT be a holiday
141 my $sunday_exception = DateTime
->new(
147 my $day_after_christmas = DateTime
->new(
151 ); # for testing negative addDate
153 { # Syspref-agnostic tests
154 is
( $saturday->day_of_week, 6, '\'$saturday\' is actually a saturday (6th day of week)');
155 is
( $sunday->day_of_week, 7, '\'$sunday\' is actually a sunday (7th day of week)');
156 is
( $monday->day_of_week, 1, '\'$monday\' is actually a monday (1st day of week)');
157 is
( $cal->is_holiday($saturday), 1, 'Saturday is a closed day' );
158 is
( $cal->is_holiday($sunday), 1, 'Sunday is a closed day' );
159 is
( $cal->is_holiday($monday), 0, 'Monday is not a closed day' );
160 is
( $cal->is_holiday($new_year), 1, 'Month/Day closed day test (New year\'s day)' );
161 is
( $cal->is_holiday($single_holiday), 1, 'Single holiday closed day test' );
162 is
( $cal->is_holiday($notspecial), 0, 'Fixed single date that is not a holiday test' );
163 is
( $cal->is_holiday($sunday_exception), 0, 'Exception holiday is not a closed day test' );
166 { # Bugzilla #8966 - is_holiday truncates referenced date
167 my $later_dt = DateTime
->new( # Monday
173 time_zone
=> 'Europe/London',
177 is
( $cal->is_holiday($later_dt), 0, 'bz-8966 (1/2) Apply is_holiday for the next test' );
178 cmp_ok
( $later_dt, 'eq', '2012-09-17T17:30:00', 'bz-8966 (2/2) Date should be the same after is_holiday' );
181 { # Bugzilla #8800 - is_holiday should use truncated date for 'contains' call
182 my $single_holiday_time = DateTime
->new(
190 is
( $cal->is_holiday($single_holiday_time),
191 $cal->is_holiday($single_holiday) ,
192 'bz-8800 is_holiday should truncate the date for holiday validation' );
195 my $one_day_dur = DateTime
::Duration
->new( days
=> 1 );
196 my $two_day_dur = DateTime
::Duration
->new( days
=> 2 );
197 my $seven_day_dur = DateTime
::Duration
->new( days
=> 7 );
199 my $dt = dt_from_string
( '2012-07-03','iso' ); #tuesday
201 my $test_dt = DateTime
->new( # Monday
209 my $later_dt = DateTime
->new( # Monday
215 time_zone
=> 'Europe/London',
220 $module_context->unmock('preference');
221 $module_context->mock(
228 $holidays_session->reset;
231 $cal = Koha
::Calendar
->new( branchcode
=> 'MPL' );
233 is
($cal->addDate( $dt, $one_day_dur, 'days' ), # tuesday
234 dt_from_string
('2012-07-05','iso'),
235 'Single day add (Datedue, matches holiday, shift)' );
237 is
($cal->addDate( $dt, $two_day_dur, 'days' ),
238 dt_from_string
('2012-07-05','iso'),
239 'Two days add, skips holiday (Datedue)' );
241 cmp_ok
($cal->addDate( $test_dt, $seven_day_dur, 'days' ), 'eq',
242 '2012-07-30T11:53:00',
243 'Add 7 days (Datedue)' );
245 is
( $cal->addDate( $saturday, $one_day_dur, 'days' )->day_of_week, 1,
246 'addDate skips closed Sunday (Datedue)' );
248 is
( $cal->addDate($day_after_christmas, -1, 'days')->ymd(), '2012-12-24',
249 'Negative call to addDate (Datedue)' );
251 ## Note that the days_between API says closed days are not considered.
252 ## This tests are here as an API test.
253 cmp_ok
( $cal->days_between( $test_dt, $later_dt )->in_units('days'),
254 '==', 40, 'days_between calculates correctly (Days)' );
256 cmp_ok
( $cal->days_between( $later_dt, $test_dt )->in_units('days'),
257 '==', 40, 'Test parameter order not relevant (Days)' );
260 { ## 'Calendar' tests'
262 $module_context->unmock('preference');
263 $module_context->mock(
270 $holidays_session->reset;
272 $cal = Koha
::Calendar
->new( branchcode
=> 'MPL' );
274 $dt = dt_from_string
('2012-07-03','iso');
276 is
($cal->addDate( $dt, $one_day_dur, 'days' ),
277 dt_from_string
('2012-07-05','iso'),
278 'Single day add (Calendar)' );
280 cmp_ok
($cal->addDate( $test_dt, $seven_day_dur, 'days' ), 'eq',
281 '2012-08-01T11:53:00',
282 'Add 7 days (Calendar)' );
284 is
( $cal->addDate( $saturday, $one_day_dur, 'days' )->day_of_week, 1,
285 'addDate skips closed Sunday (Calendar)' );
287 is
( $cal->addDate($day_after_christmas, -1, 'days')->ymd(), '2012-12-24',
288 'Negative call to addDate (Calendar)' );
290 cmp_ok
( $cal->days_between( $test_dt, $later_dt )->in_units('days'),
291 '==', 40, 'days_between calculates correctly (Calendar)' );
293 cmp_ok
( $cal->days_between( $later_dt, $test_dt )->in_units('days'),
294 '==', 40, 'Test parameter order not relevant (Calendar)' );
299 $module_context->unmock('preference');
300 $module_context->mock(
307 $holidays_session->reset;
309 $cal = Koha
::Calendar
->new( branchcode
=> 'MPL' );
311 $dt = dt_from_string
('2012-07-03','iso');
313 is
($cal->addDate( $dt, $one_day_dur, 'days' ),
314 dt_from_string
('2012-07-04','iso'),
315 'Single day add (Days)' );
317 cmp_ok
($cal->addDate( $test_dt, $seven_day_dur, 'days' ),'eq',
318 '2012-07-30T11:53:00',
319 'Add 7 days (Days)' );
321 is
( $cal->addDate( $saturday, $one_day_dur, 'days' )->day_of_week, 7,
322 'addDate doesn\'t skip closed Sunday (Days)' );
324 is
( $cal->addDate($day_after_christmas, -1, 'days')->ymd(), '2012-12-25',
325 'Negative call to addDate (Days)' );
327 ## Note that the days_between API says closed days are not considered.
328 ## This tests are here as an API test.
329 cmp_ok
( $cal->days_between( $test_dt, $later_dt )->in_units('days'),
330 '==', 40, 'days_between calculates correctly (Days)' );
332 cmp_ok
( $cal->days_between( $later_dt, $test_dt )->in_units('days'),
333 '==', 40, 'Test parameter order not relevant (Days)' );