Bug 19751: Holds awaiting pickup report should not be fixed-width
[koha.git] / t / Calendar.t
blob1c3f194973ac54e613ff6d80d5866f33c8f157dc
1 #!/usr/bin/perl
3 # This file is part of Koha.
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.
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.
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>.
18 use Modern::Perl;
20 use Test::More;
21 use Test::MockModule;
23 use DateTime;
24 use DateTime::Duration;
25 use Koha::Caches;
26 use Koha::DateUtils;
28 use Module::Load::Conditional qw/check_install/;
30 BEGIN {
31 if ( check_install( module => 'Test::DBIx::Class' ) ) {
32 plan tests => 38;
33 } else {
34 plan skip_all => "Need Test::DBIx::Class"
38 use_ok('Koha::Calendar');
40 use Test::DBIx::Class;
42 my $db = Test::MockModule->new('Koha::Database');
43 $db->mock(
44 _new_schema => sub { return Schema(); }
47 # We need to mock the C4::Context->preference method for
48 # simplicity and re-usability of the session definition. Any
49 # syspref fits for syspref-agnostic tests.
50 my $module_context = new Test::MockModule('C4::Context');
51 $module_context->mock(
52 'preference',
53 sub {
54 return 'Calendar';
58 fixtures_ok [
59 # weekly holidays
60 RepeatableHoliday => [
61 [ qw( branchcode day month weekday title description) ],
62 [ 'MPL', undef, undef, 0, '', '' ], # sundays
63 [ 'MPL', undef, undef, 6, '', '' ],# saturdays
64 [ 'MPL', 1, 1, undef, '', ''], # new year's day
65 [ 'MPL', 25, 12, undef, '', ''], # chrismas
67 # exception holidays
68 SpecialHoliday => [
69 [qw( branchcode day month year title description isexception )],
70 [ 'MPL', 11, 11, 2012, '', '', 1 ], # sunday exception
71 [ 'MPL', 1, 6, 2011, '', '', 0 ],
72 [ 'MPL', 4, 7, 2012, '', '', 0 ],
73 [ 'CPL', 6, 8, 2012, '', '', 0 ],
75 ], "add fixtures";
77 my $cache = Koha::Caches->get_instance();
78 $cache->clear_from_cache( 'single_holidays' ) ;
79 $cache->clear_from_cache( 'exception_holidays' ) ;
81 # 'MPL' branch is arbitrary, is not used at all but is needed for initialization
82 my $cal = Koha::Calendar->new( branchcode => 'MPL' );
84 isa_ok( $cal, 'Koha::Calendar', 'Calendar class returned' );
86 my $saturday = DateTime->new(
87 year => 2012,
88 month => 11,
89 day => 24,
92 my $sunday = DateTime->new(
93 year => 2012,
94 month => 11,
95 day => 25,
98 my $monday = DateTime->new(
99 year => 2012,
100 month => 11,
101 day => 26,
104 my $new_year = DateTime->new(
105 year => 2013,
106 month => 1,
107 day => 1,
110 my $single_holiday = DateTime->new(
111 year => 2011,
112 month => 6,
113 day => 1,
114 ); # should be a holiday
116 my $notspecial = DateTime->new(
117 year => 2011,
118 month => 6,
119 day => 2
120 ); # should NOT be a holiday
122 my $sunday_exception = DateTime->new(
123 year => 2012,
124 month => 11,
125 day => 11
128 my $day_after_christmas = DateTime->new(
129 year => 2012,
130 month => 12,
131 day => 26
132 ); # for testing negative addDate
134 my $holiday_for_another_branch = DateTime->new(
135 year => 2012,
136 month => 8,
137 day => 6, # This is a monday
140 { # Syspref-agnostic tests
141 is ( $saturday->day_of_week, 6, '\'$saturday\' is actually a saturday (6th day of week)');
142 is ( $sunday->day_of_week, 7, '\'$sunday\' is actually a sunday (7th day of week)');
143 is ( $monday->day_of_week, 1, '\'$monday\' is actually a monday (1st day of week)');
144 is ( $cal->is_holiday($saturday), 1, 'Saturday is a closed day' );
145 is ( $cal->is_holiday($sunday), 1, 'Sunday is a closed day' );
146 is ( $cal->is_holiday($monday), 0, 'Monday is not a closed day' );
147 is ( $cal->is_holiday($new_year), 1, 'Month/Day closed day test (New year\'s day)' );
148 is ( $cal->is_holiday($single_holiday), 1, 'Single holiday closed day test' );
149 is ( $cal->is_holiday($notspecial), 0, 'Fixed single date that is not a holiday test' );
150 is ( $cal->is_holiday($sunday_exception), 0, 'Exception holiday is not a closed day test' );
151 is ( $cal->is_holiday($holiday_for_another_branch), 0, 'Holiday defined for another branch should not be defined as an holiday' );
154 { # Bugzilla #8966 - is_holiday truncates referenced date
155 my $later_dt = DateTime->new( # Monday
156 year => 2012,
157 month => 9,
158 day => 17,
159 hour => 17,
160 minute => 30,
161 time_zone => 'Europe/London',
165 is( $cal->is_holiday($later_dt), 0, 'bz-8966 (1/2) Apply is_holiday for the next test' );
166 cmp_ok( $later_dt, 'eq', '2012-09-17T17:30:00', 'bz-8966 (2/2) Date should be the same after is_holiday' );
169 { # Bugzilla #8800 - is_holiday should use truncated date for 'contains' call
170 my $single_holiday_time = DateTime->new(
171 year => 2011,
172 month => 6,
173 day => 1,
174 hour => 11,
175 minute => 2
178 is( $cal->is_holiday($single_holiday_time),
179 $cal->is_holiday($single_holiday) ,
180 'bz-8800 is_holiday should truncate the date for holiday validation' );
183 my $one_day_dur = DateTime::Duration->new( days => 1 );
184 my $two_day_dur = DateTime::Duration->new( days => 2 );
185 my $seven_day_dur = DateTime::Duration->new( days => 7 );
187 my $dt = dt_from_string( '2012-07-03','iso' ); #tuesday
189 my $test_dt = DateTime->new( # Monday
190 year => 2012,
191 month => 7,
192 day => 23,
193 hour => 11,
194 minute => 53,
197 my $later_dt = DateTime->new( # Monday
198 year => 2012,
199 month => 9,
200 day => 17,
201 hour => 17,
202 minute => 30,
203 time_zone => 'Europe/London',
206 { ## 'Datedue' tests
208 $module_context->unmock('preference');
209 $module_context->mock(
210 'preference',
211 sub {
212 return 'Datedue';
216 $cal = Koha::Calendar->new( branchcode => 'MPL' );
218 is($cal->addDate( $dt, $one_day_dur, 'days' ), # tuesday
219 dt_from_string('2012-07-05','iso'),
220 'Single day add (Datedue, matches holiday, shift)' );
222 is($cal->addDate( $dt, $two_day_dur, 'days' ),
223 dt_from_string('2012-07-05','iso'),
224 'Two days add, skips holiday (Datedue)' );
226 cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ), 'eq',
227 '2012-07-30T11:53:00',
228 'Add 7 days (Datedue)' );
230 is( $cal->addDate( $saturday, $one_day_dur, 'days' )->day_of_week, 1,
231 'addDate skips closed Sunday (Datedue)' );
233 is( $cal->addDate($day_after_christmas, -1, 'days')->ymd(), '2012-12-24',
234 'Negative call to addDate (Datedue)' );
236 ## Note that the days_between API says closed days are not considered.
237 ## This tests are here as an API test.
238 cmp_ok( $cal->days_between( $test_dt, $later_dt )->in_units('days'),
239 '==', 40, 'days_between calculates correctly (Days)' );
241 cmp_ok( $cal->days_between( $later_dt, $test_dt )->in_units('days'),
242 '==', 40, 'Test parameter order not relevant (Days)' );
245 { ## 'Calendar' tests'
247 $module_context->unmock('preference');
248 $module_context->mock(
249 'preference',
250 sub {
251 return 'Calendar';
255 $cal = Koha::Calendar->new( branchcode => 'MPL' );
257 $dt = dt_from_string('2012-07-03','iso');
259 is($cal->addDate( $dt, $one_day_dur, 'days' ),
260 dt_from_string('2012-07-05','iso'),
261 'Single day add (Calendar)' );
263 cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ), 'eq',
264 '2012-08-01T11:53:00',
265 'Add 7 days (Calendar)' );
267 is( $cal->addDate( $saturday, $one_day_dur, 'days' )->day_of_week, 1,
268 'addDate skips closed Sunday (Calendar)' );
270 is( $cal->addDate($day_after_christmas, -1, 'days')->ymd(), '2012-12-24',
271 'Negative call to addDate (Calendar)' );
273 cmp_ok( $cal->days_between( $test_dt, $later_dt )->in_units('days'),
274 '==', 40, 'days_between calculates correctly (Calendar)' );
276 cmp_ok( $cal->days_between( $later_dt, $test_dt )->in_units('days'),
277 '==', 40, 'Test parameter order not relevant (Calendar)' );
281 { ## 'Days' tests
282 $module_context->unmock('preference');
283 $module_context->mock(
284 'preference',
285 sub {
286 return 'Days';
290 $cal = Koha::Calendar->new( branchcode => 'MPL' );
292 $dt = dt_from_string('2012-07-03','iso');
294 is($cal->addDate( $dt, $one_day_dur, 'days' ),
295 dt_from_string('2012-07-04','iso'),
296 'Single day add (Days)' );
298 cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ),'eq',
299 '2012-07-30T11:53:00',
300 'Add 7 days (Days)' );
302 is( $cal->addDate( $saturday, $one_day_dur, 'days' )->day_of_week, 7,
303 'addDate doesn\'t skip closed Sunday (Days)' );
305 is( $cal->addDate($day_after_christmas, -1, 'days')->ymd(), '2012-12-25',
306 'Negative call to addDate (Days)' );
308 ## Note that the days_between API says closed days are not considered.
309 ## This tests are here as an API test.
310 cmp_ok( $cal->days_between( $test_dt, $later_dt )->in_units('days'),
311 '==', 40, 'days_between calculates correctly (Days)' );
313 cmp_ok( $cal->days_between( $later_dt, $test_dt )->in_units('days'),
314 '==', 40, 'Test parameter order not relevant (Days)' );
319 $cal = Koha::Calendar->new( branchcode => 'CPL' );
320 is ( $cal->is_holiday($single_holiday), 0, 'Single holiday for MPL, not CPL' );
321 is ( $cal->is_holiday($holiday_for_another_branch), 1, 'Holiday defined for CPL should be defined as an holiday' );
324 END {
325 $cache->clear_from_cache( 'single_holidays' ) ;
326 $cache->clear_from_cache( 'exception_holidays' ) ;