Bug 23004: Unit test
[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 => 39;
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       ],
77 ], "add fixtures";
78
79 my $cache = Koha::Caches->get_instance();
80 $cache->clear_from_cache( 'single_holidays' ) ;
81 $cache->clear_from_cache( 'exception_holidays' ) ;
82
83 # 'MPL' branch is arbitrary, is not used at all but is needed for initialization
84 my $cal = Koha::Calendar->new( branchcode => 'MPL' );
85
86 isa_ok( $cal, 'Koha::Calendar', 'Calendar class returned' );
87
88 my $saturday = DateTime->new(
89     year      => 2012,
90     month     => 11,
91     day       => 24,
92 );
93
94 my $sunday = DateTime->new(
95     year      => 2012,
96     month     => 11,
97     day       => 25,
98 );
99
100 my $monday = DateTime->new(
101     year      => 2012,
102     month     => 11,
103     day       => 26,
104 );
105
106 my $new_year = DateTime->new(
107     year        => 2013,
108     month       => 1,
109     day         => 1,
110 );
111
112 my $single_holiday = DateTime->new(
113     year      => 2011,
114     month     => 6,
115     day       => 1,
116 );    # should be a holiday
117
118 my $notspecial = DateTime->new(
119     year      => 2011,
120     month     => 6,
121     day       => 2
122 );    # should NOT be a holiday
123
124 my $sunday_exception = DateTime->new(
125     year      => 2012,
126     month     => 11,
127     day       => 11
128 );
129
130 my $day_after_christmas = DateTime->new(
131     year    => 2012,
132     month   => 12,
133     day     => 26
134 );  # for testing negative addDate
135
136 my $holiday_for_another_branch = DateTime->new(
137     year => 2012,
138     month => 8,
139     day => 6, # This is a monday
140 );
141
142 {   # Syspref-agnostic tests
143     is ( $saturday->day_of_week, 6, '\'$saturday\' is actually a saturday (6th day of week)');
144     is ( $sunday->day_of_week, 7, '\'$sunday\' is actually a sunday (7th day of week)');
145     is ( $monday->day_of_week, 1, '\'$monday\' is actually a monday (1st day of week)');
146     is ( $cal->is_holiday($saturday), 1, 'Saturday is a closed day' );
147     is ( $cal->is_holiday($sunday), 1, 'Sunday is a closed day' );
148     is ( $cal->is_holiday($monday), 0, 'Monday is not a closed day' );
149     is ( $cal->is_holiday($new_year), 1, 'Month/Day closed day test (New year\'s day)' );
150     is ( $cal->is_holiday($single_holiday), 1, 'Single holiday closed day test' );
151     is ( $cal->is_holiday($notspecial), 0, 'Fixed single date that is not a holiday test' );
152     is ( $cal->is_holiday($sunday_exception), 0, 'Exception holiday is not a closed day test' );
153     is ( $cal->is_holiday($holiday_for_another_branch), 0, 'Holiday defined for another branch should not be defined as an holiday' );
154 }
155
156 {   # Bugzilla #8966 - is_holiday truncates referenced date
157     my $later_dt = DateTime->new(    # Monday
158         year      => 2012,
159         month     => 9,
160         day       => 17,
161         hour      => 17,
162         minute    => 30,
163         time_zone => 'Europe/London',
164     );
165
166
167     is( $cal->is_holiday($later_dt), 0, 'bz-8966 (1/2) Apply is_holiday for the next test' );
168     cmp_ok( $later_dt, 'eq', '2012-09-17T17:30:00', 'bz-8966 (2/2) Date should be the same after is_holiday' );
169 }
170
171 {   # Bugzilla #8800 - is_holiday should use truncated date for 'contains' call
172     my $single_holiday_time = DateTime->new(
173         year  => 2011,
174         month => 6,
175         day   => 1,
176         hour  => 11,
177         minute  => 2
178     );
179
180     is( $cal->is_holiday($single_holiday_time),
181         $cal->is_holiday($single_holiday) ,
182         'bz-8800 is_holiday should truncate the date for holiday validation' );
183 }
184
185     my $one_day_dur = DateTime::Duration->new( days => 1 );
186     my $two_day_dur = DateTime::Duration->new( days => 2 );
187     my $seven_day_dur = DateTime::Duration->new( days => 7 );
188
189     my $dt = dt_from_string( '2012-07-03','iso' ); #tuesday
190
191     my $test_dt = DateTime->new(    # Monday
192         year      => 2012,
193         month     => 7,
194         day       => 23,
195         hour      => 11,
196         minute    => 53,
197     );
198
199     my $later_dt = DateTime->new(    # Monday
200         year      => 2012,
201         month     => 9,
202         day       => 17,
203         hour      => 17,
204         minute    => 30,
205         time_zone => 'Europe/London',
206     );
207
208 {    ## 'Datedue' tests
209
210     $module_context->unmock('preference');
211     $module_context->mock(
212         'preference',
213         sub {
214             return 'Datedue';
215         }
216     );
217
218     $cal = Koha::Calendar->new( branchcode => 'MPL' );
219
220     is($cal->addDate( $dt, $one_day_dur, 'days' ), # tuesday
221         dt_from_string('2012-07-05','iso'),
222         'Single day add (Datedue, matches holiday, shift)' );
223
224     is($cal->addDate( $dt, $two_day_dur, 'days' ),
225         dt_from_string('2012-07-05','iso'),
226         'Two days add, skips holiday (Datedue)' );
227
228     cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ), 'eq',
229         '2012-07-30T11:53:00',
230         'Add 7 days (Datedue)' );
231
232     is( $cal->addDate( $saturday, $one_day_dur, 'days' )->day_of_week, 1,
233         'addDate skips closed Sunday (Datedue)' );
234
235     is( $cal->addDate($day_after_christmas, -1, 'days')->ymd(), '2012-12-24',
236         'Negative call to addDate (Datedue)' );
237
238     ## Note that the days_between API says closed days are not considered.
239     ## This tests are here as an API test.
240     cmp_ok( $cal->days_between( $test_dt, $later_dt )->in_units('days'),
241                 '==', 40, 'days_between calculates correctly (Days)' );
242
243     cmp_ok( $cal->days_between( $later_dt, $test_dt )->in_units('days'),
244                 '==', 40, 'Test parameter order not relevant (Days)' );
245 }
246
247 {   ## 'Calendar' tests'
248
249     $module_context->unmock('preference');
250     $module_context->mock(
251         'preference',
252         sub {
253             return 'Calendar';
254         }
255     );
256
257     $cal = Koha::Calendar->new( branchcode => 'MPL' );
258
259     $dt = dt_from_string('2012-07-03','iso');
260
261     is($cal->addDate( $dt, $one_day_dur, 'days' ),
262         dt_from_string('2012-07-05','iso'),
263         'Single day add (Calendar)' );
264
265     cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ), 'eq',
266        '2012-08-01T11:53:00',
267        'Add 7 days (Calendar)' );
268
269     is( $cal->addDate( $saturday, $one_day_dur, 'days' )->day_of_week, 1,
270             'addDate skips closed Sunday (Calendar)' );
271
272     is( $cal->addDate($day_after_christmas, -1, 'days')->ymd(), '2012-12-24',
273             'Negative call to addDate (Calendar)' );
274
275     cmp_ok( $cal->days_between( $test_dt, $later_dt )->in_units('days'),
276                 '==', 40, 'days_between calculates correctly (Calendar)' );
277
278     cmp_ok( $cal->days_between( $later_dt, $test_dt )->in_units('days'),
279                 '==', 40, 'Test parameter order not relevant (Calendar)' );
280 }
281
282
283 {   ## 'Days' tests
284     $module_context->unmock('preference');
285     $module_context->mock(
286         'preference',
287         sub {
288             return 'Days';
289         }
290     );
291
292     $cal = Koha::Calendar->new( branchcode => 'MPL' );
293
294     $dt = dt_from_string('2012-07-03','iso');
295
296     is($cal->addDate( $dt, $one_day_dur, 'days' ),
297         dt_from_string('2012-07-04','iso'),
298         'Single day add (Days)' );
299
300     cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ),'eq',
301         '2012-07-30T11:53:00',
302         'Add 7 days (Days)' );
303
304     is( $cal->addDate( $saturday, $one_day_dur, 'days' )->day_of_week, 7,
305         'addDate doesn\'t skip closed Sunday (Days)' );
306
307     is( $cal->addDate($day_after_christmas, -1, 'days')->ymd(), '2012-12-25',
308         'Negative call to addDate (Days)' );
309
310     ## Note that the days_between API says closed days are not considered.
311     ## This tests are here as an API test.
312     cmp_ok( $cal->days_between( $test_dt, $later_dt )->in_units('days'),
313                 '==', 40, 'days_between calculates correctly (Days)' );
314
315     cmp_ok( $cal->days_between( $later_dt, $test_dt )->in_units('days'),
316                 '==', 40, 'Test parameter order not relevant (Days)' );
317
318 }
319
320 {
321     $cal = Koha::Calendar->new( branchcode => 'CPL' );
322     is ( $cal->is_holiday($single_holiday), 0, 'Single holiday for MPL, not CPL' );
323     is ( $cal->is_holiday($holiday_for_another_branch), 1, 'Holiday defined for CPL should be defined as an holiday' );
324 }
325
326 subtest 'days_mode parameter' => sub {
327     plan tests => 2;
328
329     t::lib::Mocks::mock_preference('useDaysMode', 'Days');
330     my $cal = Koha::Calendar->new( branchcode => 'CPL' );
331     is( $cal->{days_mode}, 'Days', q|If not set, days_mode defaults to syspref's value|);
332
333     $cal = Koha::Calendar->new( branchcode => 'CPL', days_mode => 'Calendar' );
334     is( $cal->{days_mode}, 'Calendar', q|If set, days_mode is correctly set|);
335 };
336
337 END {
338     $cache->clear_from_cache( 'single_holidays' ) ;
339     $cache->clear_from_cache( 'exception_holidays' ) ;
340 };