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