6 use DateTime
::Duration
;
7 use Test
::More tests
=> 35;
13 use_ok
('Koha::Calendar');
15 # This was the only test C4 had
16 # Remove when no longer used
17 use_ok
('C4::Calendar');
20 my $module_context = new Test
::MockModule
('C4::Context');
21 $module_context->mock(
24 my $dbh = DBI
->connect( 'DBI:Mock:', '', '' )
25 || die "Cannot create handle: $DBI::errstr\n";
30 # We need to mock the C4::Context->preference method for
31 # simplicity and re-usability of the session definition. Any
32 # syspref fits for syspref-agnostic tests.
33 $module_context->mock(
42 skip
"DBD::Mock is too old", 33
43 unless $DBD::Mock
::VERSION
>= 1.45;
45 my $holidays_session = DBD
::Mock
::Session
->new('holidays_session' => (
47 statement
=> "SELECT weekday FROM repeatable_holidays WHERE branchcode = ? AND weekday IS NOT NULL",
54 { # day and month repeatable holidays
55 statement
=> "SELECT day, month FROM repeatable_holidays WHERE branchcode = ? AND weekday IS NULL",
58 [ 1, 1 ], # new year's day
62 { # exception holidays
63 statement
=> "SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = 1",
65 [ 'day', 'month', 'year' ],
66 [ 11, 11, 2012 ] # sunday exception
70 statement
=> "SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = 0",
72 [ 'day', 'month', 'year' ],
73 [ 1, 6, 2011 ], # single holiday
79 # Initialize the global $dbh variable
80 my $dbh = C4
::Context
->dbh();
81 # Apply the mock session
82 $dbh->{ mock_session
} = $holidays_session;
83 # 'MPL' branch is arbitrary, is not used at all but is needed for initialization
84 my $cal = Koha
::Calendar
->new( branchcode
=> 'MPL' );
86 isa_ok
( $cal, 'Koha::Calendar', 'Calendar class returned' );
89 my $saturday = DateTime
->new(
95 my $sunday = DateTime
->new(
101 my $monday = DateTime
->new(
107 my $new_year = DateTime
->new(
113 my $single_holiday = DateTime
->new(
117 ); # should be a holiday
119 my $notspecial = DateTime
->new(
123 ); # should NOT be a holiday
125 my $sunday_exception = DateTime
->new(
131 my $day_after_christmas = DateTime
->new(
135 ); # for testing negative addDate
138 { # Syspref-agnostic tests
139 is
( $saturday->day_of_week, 6, '\'$saturday\' is actually a saturday (6th day of week)');
140 is
( $sunday->day_of_week, 7, '\'$sunday\' is actually a sunday (7th day of week)');
141 is
( $monday->day_of_week, 1, '\'$monday\' is actually a monday (1st day of week)');
142 is
( $cal->is_holiday($saturday), 1, 'Saturday is a closed day' );
143 is
( $cal->is_holiday($sunday), 1, 'Sunday is a closed day' );
144 is
( $cal->is_holiday($monday), 0, 'Monday is not a closed day' );
145 is
( $cal->is_holiday($new_year), 1, 'Month/Day closed day test (New year\'s day)' );
146 is
( $cal->is_holiday($single_holiday), 1, 'Single holiday closed day test' );
147 is
( $cal->is_holiday($notspecial), 0, 'Fixed single date that is not a holiday test' );
148 is
( $cal->is_holiday($sunday_exception), 0, 'Exception holiday is not a closed day test' );
152 { # Bugzilla #8966 - is_holiday truncates referenced date
153 my $later_dt = DateTime
->new( # Monday
159 time_zone
=> 'Europe/London',
163 is
( $cal->is_holiday($later_dt), 0, 'bz-8966 (1/2) Apply is_holiday for the next test' );
164 cmp_ok
( $later_dt, 'eq', '2012-09-17T17:30:00', 'bz-8966 (2/2) Date should be the same after is_holiday' );
168 { # Bugzilla #8800 - is_holiday should use truncated date for 'contains' call
169 my $single_holiday_time = DateTime
->new(
177 is
( $cal->is_holiday($single_holiday_time),
178 $cal->is_holiday($single_holiday) ,
179 'bz-8800 is_holiday should truncate the date for holiday validation' );
183 my $one_day_dur = DateTime
::Duration
->new( days
=> 1 );
184 my $two_day_dur = DateTime
::Duration
->new( days
=> 2 );
185 my $seven_day_dur = DateTime
::Duration
->new( days
=> 7 );
187 my $dt = dt_from_string
( '2012-07-03','iso' );
188 my $test_dt = DateTime
->new( # Monday
196 my $later_dt = DateTime
->new( # Monday
202 time_zone
=> 'Europe/London',
208 $module_context->unmock('preference');
209 $module_context->mock(
216 $holidays_session->reset;
219 $cal = Koha
::Calendar
->new( branchcode
=> 'MPL' );
221 is
($cal->addDate( $dt, $one_day_dur, 'days' ),
222 dt_from_string
('2012-07-05','iso'),
223 'Single day add (Datedue, matches holiday, shift)' );
225 is
($cal->addDate( $dt, $two_day_dur, 'days' ),
226 dt_from_string
('2012-07-05','iso'),
227 'Two days add, skips holiday (Datedue)' );
229 cmp_ok
($cal->addDate( $test_dt, $seven_day_dur, 'days' ), 'eq',
230 '2012-07-30T11:53:00',
231 'Add 7 days (Datedue)' );
233 is
( $cal->addDate( $saturday, $one_day_dur, 'days' )->day_of_week, 1,
234 'addDate skips closed Sunday (Datedue)' );
236 is
( $cal->addDate($day_after_christmas, -1, 'days')->ymd(), '2012-12-24',
237 'Negative call to addDate (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)' );
251 { ## 'Calendar' tests'
253 $module_context->unmock('preference');
254 $module_context->mock(
261 $holidays_session->reset;
263 $cal = Koha
::Calendar
->new( branchcode
=> 'MPL' );
265 $dt = dt_from_string
('2012-07-03','iso');
267 is
($cal->addDate( $dt, $one_day_dur, 'days' ),
268 dt_from_string
('2012-07-05','iso'),
269 'Single day add (Calendar)' );
271 cmp_ok
($cal->addDate( $test_dt, $seven_day_dur, 'days' ), 'eq',
272 '2012-08-01T11:53:00',
273 'Add 7 days (Calendar)' );
275 is
( $cal->addDate( $saturday, $one_day_dur, 'days' )->day_of_week, 1,
276 'addDate skips closed Sunday (Calendar)' );
278 is
( $cal->addDate($day_after_christmas, -1, 'days')->ymd(), '2012-12-24',
279 'Negative call to addDate (Calendar)' );
281 cmp_ok
( $cal->days_between( $test_dt, $later_dt )->in_units('days'),
282 '==', 40, 'days_between calculates correctly (Calendar)' );
284 cmp_ok
( $cal->days_between( $later_dt, $test_dt )->in_units('days'),
285 '==', 40, 'Test parameter order not relevant (Calendar)' );
290 $module_context->unmock('preference');
291 $module_context->mock(
298 $holidays_session->reset;
300 $cal = Koha
::Calendar
->new( branchcode
=> 'MPL' );
302 $dt = dt_from_string
('2012-07-03','iso');
304 is
($cal->addDate( $dt, $one_day_dur, 'days' ),
305 dt_from_string
('2012-07-04','iso'),
306 'Single day add (Days)' );
308 cmp_ok
($cal->addDate( $test_dt, $seven_day_dur, 'days' ),'eq',
309 '2012-07-30T11:53:00',
310 'Add 7 days (Days)' );
312 is
( $cal->addDate( $saturday, $one_day_dur, 'days' )->day_of_week, 7,
313 'addDate doesn\'t skip closed Sunday (Days)' );
315 is
( $cal->addDate($day_after_christmas, -1, 'days')->ymd(), '2012-12-25',
316 'Negative call to addDate (Days)' );
318 ## Note that the days_between API says closed days are not considered.
319 ## This tests are here as an API test.
320 cmp_ok
( $cal->days_between( $test_dt, $later_dt )->in_units('days'),
321 '==', 40, 'days_between calculates correctly (Days)' );
323 cmp_ok
( $cal->days_between( $later_dt, $test_dt )->in_units('days'),
324 '==', 40, 'Test parameter order not relevant (Days)' );