Bug 25723: (QA follow-up) Handle holiday and exception on same day
[koha.git] / t / Calendar.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
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.
9 #
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.
14 #
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>.
17
18 use Modern::Perl;
19
20 use Test::More;
21 use Test::MockModule;
22
23 use DateTime;
24 use DateTime::Duration;
25 use Koha::Caches;
26 use Koha::DateUtils;
27
28 use t::lib::Mocks;
29
30 use Module::Load::Conditional qw/check_install/;
31
32 BEGIN {
33     if ( check_install( module => 'Test::DBIx::Class' ) ) {
34         plan tests => 40;
35     } else {
36         plan skip_all => "Need Test::DBIx::Class"
37     }
38 }
39
40 use_ok('Koha::Calendar');
41
42 use Test::DBIx::Class;
43
44 my $db = Test::MockModule->new('Koha::Database');
45 $db->mock(
46     _new_schema => sub { return Schema(); }
47 );
48
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 = new Test::MockModule('C4::Context');
53 $module_context->mock(
54     'preference',
55     sub {
56         return 'Calendar';
57     }
58 );
59
60 fixtures_ok [
61     # weekly holidays
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
68     ],
69     # exception holidays
70     SpecialHoliday => [
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
78       ],
79 ], "add fixtures";
80
81 my $cache = Koha::Caches->get_instance();
82 $cache->clear_from_cache('MPL_holidays');
83 $cache->clear_from_cache('CPL_holidays');
84
85 # 'MPL' branch is arbitrary, is not used at all but is needed for initialization
86 my $cal = Koha::Calendar->new( branchcode => 'MPL' );
87
88 isa_ok( $cal, 'Koha::Calendar', 'Calendar class returned' );
89
90 my $saturday = DateTime->new(
91     year      => 2012,
92     month     => 11,
93     day       => 24,
94 );
95
96 my $sunday = DateTime->new(
97     year      => 2012,
98     month     => 11,
99     day       => 25,
100 );
101
102 my $monday = DateTime->new(
103     year      => 2012,
104     month     => 11,
105     day       => 26,
106 );
107
108 my $new_year = DateTime->new(
109     year        => 2013,
110     month       => 1,
111     day         => 1,
112 );
113
114 my $single_holiday = DateTime->new(
115     year      => 2011,
116     month     => 6,
117     day       => 1,
118 );    # should be a holiday
119
120 my $notspecial = DateTime->new(
121     year      => 2011,
122     month     => 6,
123     day       => 2
124 );    # should NOT be a holiday
125
126 my $sunday_exception = DateTime->new(
127     year      => 2012,
128     month     => 11,
129     day       => 11
130 );
131
132 my $day_after_christmas = DateTime->new(
133     year    => 2012,
134     month   => 12,
135     day     => 26
136 );  # for testing negative addDate
137
138 my $holiday_for_another_branch = DateTime->new(
139     year => 2012,
140     month => 8,
141     day => 6, # This is a monday
142 );
143
144 my $holiday_excepted = DateTime->new(
145     year => 2012,
146     month => 7,
147     day => 7, # Both a holiday and exception
148 );
149
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' );
163 }
164
165 {   # Bugzilla #8966 - is_holiday truncates referenced date
166     my $later_dt = DateTime->new(    # Monday
167         year      => 2012,
168         month     => 9,
169         day       => 17,
170         hour      => 17,
171         minute    => 30,
172         time_zone => 'Europe/London',
173     );
174
175
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' );
178 }
179
180 {   # Bugzilla #8800 - is_holiday should use truncated date for 'contains' call
181     my $single_holiday_time = DateTime->new(
182         year  => 2011,
183         month => 6,
184         day   => 1,
185         hour  => 11,
186         minute  => 2
187     );
188
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' );
192 }
193
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 );
197
198     my $dt = dt_from_string( '2012-07-03','iso' ); #tuesday
199
200     my $test_dt = DateTime->new(    # Monday
201         year      => 2012,
202         month     => 7,
203         day       => 23,
204         hour      => 11,
205         minute    => 53,
206     );
207
208     my $later_dt = DateTime->new(    # Monday
209         year      => 2012,
210         month     => 9,
211         day       => 17,
212         hour      => 17,
213         minute    => 30,
214         time_zone => 'Europe/London',
215     );
216
217 {    ## 'Datedue' tests
218
219     $module_context->unmock('preference');
220     $module_context->mock(
221         'preference',
222         sub {
223             return 'Datedue';
224         }
225     );
226
227     $cal = Koha::Calendar->new( branchcode => 'MPL' );
228
229     is($cal->addDate( $dt, $one_day_dur, 'days' ), # tuesday
230         dt_from_string('2012-07-05','iso'),
231         'Single day add (Datedue, matches holiday, shift)' );
232
233     is($cal->addDate( $dt, $two_day_dur, 'days' ),
234         dt_from_string('2012-07-05','iso'),
235         'Two days add, skips holiday (Datedue)' );
236
237     cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ), 'eq',
238         '2012-07-30T11:53:00',
239         'Add 7 days (Datedue)' );
240
241     is( $cal->addDate( $saturday, $one_day_dur, 'days' )->day_of_week, 1,
242         'addDate skips closed Sunday (Datedue)' );
243
244     is( $cal->addDate($day_after_christmas, -1, 'days')->ymd(), '2012-12-24',
245         'Negative call to addDate (Datedue)' );
246
247     ## Note that the days_between API says closed days are not considered.
248     ## This tests are here as an API test.
249     cmp_ok( $cal->days_between( $test_dt, $later_dt )->in_units('days'),
250                 '==', 40, 'days_between calculates correctly (Days)' );
251
252     cmp_ok( $cal->days_between( $later_dt, $test_dt )->in_units('days'),
253                 '==', 40, 'Test parameter order not relevant (Days)' );
254 }
255
256 {   ## 'Calendar' tests'
257
258     $module_context->unmock('preference');
259     $module_context->mock(
260         'preference',
261         sub {
262             return 'Calendar';
263         }
264     );
265
266     $cal = Koha::Calendar->new( branchcode => 'MPL' );
267
268     $dt = dt_from_string('2012-07-03','iso');
269
270     is($cal->addDate( $dt, $one_day_dur, 'days' ),
271         dt_from_string('2012-07-05','iso'),
272         'Single day add (Calendar)' );
273
274     cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ), 'eq',
275        '2012-08-01T11:53:00',
276        'Add 7 days (Calendar)' );
277
278     is( $cal->addDate( $saturday, $one_day_dur, 'days' )->day_of_week, 1,
279             'addDate skips closed Sunday (Calendar)' );
280
281     is( $cal->addDate($day_after_christmas, -1, 'days')->ymd(), '2012-12-24',
282             'Negative call to addDate (Calendar)' );
283
284     cmp_ok( $cal->days_between( $test_dt, $later_dt )->in_units('days'),
285                 '==', 40, 'days_between calculates correctly (Calendar)' );
286
287     cmp_ok( $cal->days_between( $later_dt, $test_dt )->in_units('days'),
288                 '==', 40, 'Test parameter order not relevant (Calendar)' );
289 }
290
291
292 {   ## 'Days' tests
293     $module_context->unmock('preference');
294     $module_context->mock(
295         'preference',
296         sub {
297             return 'Days';
298         }
299     );
300
301     $cal = Koha::Calendar->new( branchcode => 'MPL' );
302
303     $dt = dt_from_string('2012-07-03','iso');
304
305     is($cal->addDate( $dt, $one_day_dur, 'days' ),
306         dt_from_string('2012-07-04','iso'),
307         'Single day add (Days)' );
308
309     cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ),'eq',
310         '2012-07-30T11:53:00',
311         'Add 7 days (Days)' );
312
313     is( $cal->addDate( $saturday, $one_day_dur, 'days' )->day_of_week, 7,
314         'addDate doesn\'t skip closed Sunday (Days)' );
315
316     is( $cal->addDate($day_after_christmas, -1, 'days')->ymd(), '2012-12-25',
317         'Negative call to addDate (Days)' );
318
319     ## Note that the days_between API says closed days are not considered.
320     ## This tests are here as an API test.
321     cmp_ok( $cal->days_between( $test_dt, $later_dt )->in_units('days'),
322                 '==', 40, 'days_between calculates correctly (Days)' );
323
324     cmp_ok( $cal->days_between( $later_dt, $test_dt )->in_units('days'),
325                 '==', 40, 'Test parameter order not relevant (Days)' );
326
327 }
328
329 {
330     $cal = Koha::Calendar->new( branchcode => 'CPL' );
331     is ( $cal->is_holiday($single_holiday), 0, 'Single holiday for MPL, not CPL' );
332     is ( $cal->is_holiday($holiday_for_another_branch), 1, 'Holiday defined for CPL should be defined as an holiday' );
333 }
334
335 subtest 'days_mode parameter' => sub {
336     plan tests => 2;
337
338     t::lib::Mocks::mock_preference('useDaysMode', 'Days');
339     my $cal = Koha::Calendar->new( branchcode => 'CPL' );
340     is( $cal->{days_mode}, 'Days', q|If not set, days_mode defaults to syspref's value|);
341
342     $cal = Koha::Calendar->new( branchcode => 'CPL', days_mode => 'Calendar' );
343     is( $cal->{days_mode}, 'Calendar', q|If set, days_mode is correctly set|);
344 };
345
346 END {
347     $cache->clear_from_cache('MPL_holidays');
348     $cache->clear_from_cache('CPL_holidays');
349 };