Bug 14522: Use Koha::Cache for accessing single_holidays()
[koha.git] / t / Calendar.t
1 #!/usr/bin/env perl
2
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;
12
13 BEGIN {
14     use_ok('Koha::Calendar');
15
16     # This was the only test C4 had
17     # Remove when no longer used
18     #use_ok('C4::Calendar'); # not used anymore?
19 }
20
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;
28     }
29 );
30
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';
38     }
39 );
40
41 SKIP: {
42
43 skip "DBD::Mock is too old", 33
44   unless $DBD::Mock::VERSION >= 1.45;
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
53                      ]
54     },
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
61                      ]
62     },
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
68                      ]
69     },
70
71     { # single holidays1
72         statement => "SELECT distinct(branchcode) FROM special_holidays",
73         results   => [
74                         [ 'branchcode' ],
75                         [ 'MPL']
76                      ]
77     },
78
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 ]
85                      ]
86     },
87 ));
88
89 # Initialize the global $dbh variable
90 my $dbh = C4::Context->dbh();
91 # Apply the mock session
92 $dbh->{ mock_session } = $holidays_session;
93
94
95 my $cache = Koha::Cache->get_instance();
96 $cache->clear_from_cache( 'single_holidays') ;
97
98
99 # 'MPL' branch is arbitrary, is not used at all but is needed for initialization
100 my $cal = Koha::Calendar->new( branchcode => 'MPL' );
101
102 isa_ok( $cal, 'Koha::Calendar', 'Calendar class returned' );
103
104
105 my $saturday = DateTime->new(
106     year      => 2012,
107     month     => 11,
108     day       => 24,
109 );
110
111 my $sunday = DateTime->new(
112     year      => 2012,
113     month     => 11,
114     day       => 25,
115 );
116
117 my $monday = DateTime->new(
118     year      => 2012,
119     month     => 11,
120     day       => 26,
121 );
122
123 my $new_year = DateTime->new(
124     year        => 2013,
125     month       => 1,
126     day         => 1,
127 );
128
129 my $single_holiday = DateTime->new(
130     year      => 2011,
131     month     => 6,
132     day       => 1,
133 );    # should be a holiday
134
135 my $notspecial = DateTime->new(
136     year      => 2011,
137     month     => 6,
138     day       => 2
139 );    # should NOT be a holiday
140
141 my $sunday_exception = DateTime->new(
142     year      => 2012,
143     month     => 11,
144     day       => 11
145 );
146
147 my $day_after_christmas = DateTime->new(
148     year    => 2012,
149     month   => 12,
150     day     => 26
151 );  # for testing negative addDate
152
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' );
164 }
165
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',
174     );
175
176
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' );
179 }
180
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
188     );
189
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' );
193 }
194
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 );
198
199     my $dt = dt_from_string( '2012-07-03','iso' ); #tuesday
200
201     my $test_dt = DateTime->new(    # Monday
202         year      => 2012,
203         month     => 7,
204         day       => 23,
205         hour      => 11,
206         minute    => 53,
207     );
208
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',
216     );
217
218 {    ## 'Datedue' tests
219
220     $module_context->unmock('preference');
221     $module_context->mock(
222         'preference',
223         sub {
224             return 'Datedue';
225         }
226     );
227     # rewind dbh session
228     $holidays_session->reset;
229
230
231     $cal = Koha::Calendar->new( branchcode => 'MPL' );
232
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)' );
236
237     is($cal->addDate( $dt, $two_day_dur, 'days' ),
238         dt_from_string('2012-07-05','iso'),
239         'Two days add, skips holiday (Datedue)' );
240
241     cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ), 'eq',
242         '2012-07-30T11:53:00',
243         'Add 7 days (Datedue)' );
244
245     is( $cal->addDate( $saturday, $one_day_dur, 'days' )->day_of_week, 1,
246         'addDate skips closed Sunday (Datedue)' );
247
248     is( $cal->addDate($day_after_christmas, -1, 'days')->ymd(), '2012-12-24',
249         'Negative call to addDate (Datedue)' );
250
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)' );
255
256     cmp_ok( $cal->days_between( $later_dt, $test_dt )->in_units('days'),
257                 '==', 40, 'Test parameter order not relevant (Days)' );
258 }
259
260 {   ## 'Calendar' tests'
261
262     $module_context->unmock('preference');
263     $module_context->mock(
264         'preference',
265         sub {
266             return 'Calendar';
267         }
268     );
269     # rewind dbh session
270     $holidays_session->reset;
271
272     $cal = Koha::Calendar->new( branchcode => 'MPL' );
273
274     $dt = dt_from_string('2012-07-03','iso');
275
276     is($cal->addDate( $dt, $one_day_dur, 'days' ),
277         dt_from_string('2012-07-05','iso'),
278         'Single day add (Calendar)' );
279
280     cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ), 'eq',
281        '2012-08-01T11:53:00',
282        'Add 7 days (Calendar)' );
283
284     is( $cal->addDate( $saturday, $one_day_dur, 'days' )->day_of_week, 1,
285             'addDate skips closed Sunday (Calendar)' );
286
287     is( $cal->addDate($day_after_christmas, -1, 'days')->ymd(), '2012-12-24',
288             'Negative call to addDate (Calendar)' );
289
290     cmp_ok( $cal->days_between( $test_dt, $later_dt )->in_units('days'),
291                 '==', 40, 'days_between calculates correctly (Calendar)' );
292
293     cmp_ok( $cal->days_between( $later_dt, $test_dt )->in_units('days'),
294                 '==', 40, 'Test parameter order not relevant (Calendar)' );
295 }
296
297
298 {   ## 'Days' tests
299     $module_context->unmock('preference');
300     $module_context->mock(
301         'preference',
302         sub {
303             return 'Days';
304         }
305     );
306     # rewind dbh session
307     $holidays_session->reset;
308
309     $cal = Koha::Calendar->new( branchcode => 'MPL' );
310
311     $dt = dt_from_string('2012-07-03','iso');
312
313     is($cal->addDate( $dt, $one_day_dur, 'days' ),
314         dt_from_string('2012-07-04','iso'),
315         'Single day add (Days)' );
316
317     cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ),'eq',
318         '2012-07-30T11:53:00',
319         'Add 7 days (Days)' );
320
321     is( $cal->addDate( $saturday, $one_day_dur, 'days' )->day_of_week, 7,
322         'addDate doesn\'t skip closed Sunday (Days)' );
323
324     is( $cal->addDate($day_after_christmas, -1, 'days')->ymd(), '2012-12-25',
325         'Negative call to addDate (Days)' );
326
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)' );
331
332     cmp_ok( $cal->days_between( $later_dt, $test_dt )->in_units('days'),
333                 '==', 40, 'Test parameter order not relevant (Days)' );
334
335 }
336
337 } # End SKIP block