Bug 15012: use sudo instead of su in koha-shell
[koha.git] / t / Calendar.t
blob758f60bfc43cdb5621b3bcf4e168e8cb57fd5a41
1 #!/usr/bin/env perl
3 use strict;
4 use warnings;
5 use DateTime;
6 use DateTime::Duration;
7 use Test::More tests => 34;
8 use Test::MockModule;
9 use DBD::Mock;
10 use Koha::Cache;
11 use Koha::DateUtils;
13 BEGIN {
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(
23 '_new_dbh',
24 sub {
25 my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
26 || die "Cannot create handle: $DBI::errstr\n";
27 return $dbh;
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(
35 'preference',
36 sub {
37 return 'Calendar';
41 SKIP: {
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' => (
47 { # weekly holidays
48 statement => "SELECT weekday FROM repeatable_holidays WHERE branchcode = ? AND weekday IS NOT NULL",
49 results => [
50 ['weekday'],
51 [0], # sundays
52 [6] # saturdays
55 { # day and month repeatable holidays
56 statement => "SELECT day, month FROM repeatable_holidays WHERE branchcode = ? AND weekday IS NULL",
57 results => [
58 [ 'month', 'day' ],
59 [ 1, 1 ], # new year's day
60 [12,25] # christmas
63 { # exception holidays
64 statement => "SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = 1",
65 results => [
66 [ 'day', 'month', 'year' ],
67 [ 11, 11, 2012 ] # sunday exception
71 { # single holidays1
72 statement => "SELECT distinct(branchcode) FROM special_holidays",
73 results => [
74 [ 'branchcode' ],
75 [ 'MPL']
79 { # single holidays2
80 statement => "SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = 0",
81 results => [
82 [ 'day', 'month', 'year' ],
83 [ 1, 6, 2011 ], # single holiday
84 [ 4, 7, 2012 ]
87 ));
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(
106 year => 2012,
107 month => 11,
108 day => 24,
111 my $sunday = DateTime->new(
112 year => 2012,
113 month => 11,
114 day => 25,
117 my $monday = DateTime->new(
118 year => 2012,
119 month => 11,
120 day => 26,
123 my $new_year = DateTime->new(
124 year => 2013,
125 month => 1,
126 day => 1,
129 my $single_holiday = DateTime->new(
130 year => 2011,
131 month => 6,
132 day => 1,
133 ); # should be a holiday
135 my $notspecial = DateTime->new(
136 year => 2011,
137 month => 6,
138 day => 2
139 ); # should NOT be a holiday
141 my $sunday_exception = DateTime->new(
142 year => 2012,
143 month => 11,
144 day => 11
147 my $day_after_christmas = DateTime->new(
148 year => 2012,
149 month => 12,
150 day => 26
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
168 year => 2012,
169 month => 9,
170 day => 17,
171 hour => 17,
172 minute => 30,
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(
183 year => 2011,
184 month => 6,
185 day => 1,
186 hour => 11,
187 minute => 2
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
202 year => 2012,
203 month => 7,
204 day => 23,
205 hour => 11,
206 minute => 53,
209 my $later_dt = DateTime->new( # Monday
210 year => 2012,
211 month => 9,
212 day => 17,
213 hour => 17,
214 minute => 30,
215 time_zone => 'Europe/London',
218 { ## 'Datedue' tests
220 $module_context->unmock('preference');
221 $module_context->mock(
222 'preference',
223 sub {
224 return 'Datedue';
227 # rewind dbh session
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(
264 'preference',
265 sub {
266 return 'Calendar';
269 # rewind dbh session
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)' );
298 { ## 'Days' tests
299 $module_context->unmock('preference');
300 $module_context->mock(
301 'preference',
302 sub {
303 return 'Days';
306 # rewind dbh session
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)' );
337 } # End SKIP block