Bug 16011 reintroduced VERSION variable needs declaration
[koha.git] / t / Calendar.t
blob37f11f489412e3fce4c24c6ae77616ea1f58aaf7
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::Cache;
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 => 35;
33 } else {
34 plan skip_all => "Need Test::DBIx::Class"
38 use_ok('Koha::Calendar');
40 use Test::DBIx::Class {
41 schema_class => 'Koha::Schema',
42 connect_info => ['dbi:SQLite:dbname=:memory:','',''],
43 connect_opts => { name_sep => '.', quote_char => '`', },
44 fixture_class => '::Populate',
45 }, 'Biblio' ;
47 sub fixtures {
48 my ( $data ) = @_;
49 fixtures_ok [
50 Biblio => [
51 [ qw/ biblionumber datecreated timestamp / ],
52 @$data,
54 ], 'add fixtures';
57 my $db = Test::MockModule->new('Koha::Database');
58 $db->mock(
59 _new_schema => sub { return Schema(); }
62 # We need to mock the C4::Context->preference method for
63 # simplicity and re-usability of the session definition. Any
64 # syspref fits for syspref-agnostic tests.
65 my $module_context = new Test::MockModule('C4::Context');
66 $module_context->mock(
67 'preference',
68 sub {
69 return 'Calendar';
73 fixtures_ok [
74 # weekly holidays
75 RepeatableHoliday => [
76 [ qw( branchcode day month weekday title description) ],
77 [ 'MPL', undef, undef, 0, '', '' ], # sundays
78 [ 'MPL', undef, undef, 6, '', '' ],# saturdays
79 [ 'MPL', 1, 1, undef, '', ''], # new year's day
80 [ 'MPL', 25, 12, undef, '', ''], # chrismas
82 # exception holidays
83 SpecialHoliday => [
84 [qw( branchcode day month year title description isexception )],
85 [ 'MPL', 11, 11, 2012, '', '', 1 ], # sunday exception
86 [ 'MPL', 1, 6, 2011, '', '', 0 ],
87 [ 'MPL', 4, 7, 2012, '', '', 0 ],
89 ], "add fixtures";
91 my $cache = Koha::Cache->get_instance();
92 $cache->clear_from_cache( 'single_holidays') ;
94 # 'MPL' branch is arbitrary, is not used at all but is needed for initialization
95 my $cal = Koha::Calendar->new( branchcode => 'MPL' );
97 isa_ok( $cal, 'Koha::Calendar', 'Calendar class returned' );
99 my $saturday = DateTime->new(
100 year => 2012,
101 month => 11,
102 day => 24,
105 my $sunday = DateTime->new(
106 year => 2012,
107 month => 11,
108 day => 25,
111 my $monday = DateTime->new(
112 year => 2012,
113 month => 11,
114 day => 26,
117 my $new_year = DateTime->new(
118 year => 2013,
119 month => 1,
120 day => 1,
123 my $single_holiday = DateTime->new(
124 year => 2011,
125 month => 6,
126 day => 1,
127 ); # should be a holiday
129 my $notspecial = DateTime->new(
130 year => 2011,
131 month => 6,
132 day => 2
133 ); # should NOT be a holiday
135 my $sunday_exception = DateTime->new(
136 year => 2012,
137 month => 11,
138 day => 11
141 my $day_after_christmas = DateTime->new(
142 year => 2012,
143 month => 12,
144 day => 26
145 ); # for testing negative addDate
147 { # Syspref-agnostic tests
148 is ( $saturday->day_of_week, 6, '\'$saturday\' is actually a saturday (6th day of week)');
149 is ( $sunday->day_of_week, 7, '\'$sunday\' is actually a sunday (7th day of week)');
150 is ( $monday->day_of_week, 1, '\'$monday\' is actually a monday (1st day of week)');
151 is ( $cal->is_holiday($saturday), 1, 'Saturday is a closed day' );
152 is ( $cal->is_holiday($sunday), 1, 'Sunday is a closed day' );
153 is ( $cal->is_holiday($monday), 0, 'Monday is not a closed day' );
154 is ( $cal->is_holiday($new_year), 1, 'Month/Day closed day test (New year\'s day)' );
155 is ( $cal->is_holiday($single_holiday), 1, 'Single holiday closed day test' );
156 is ( $cal->is_holiday($notspecial), 0, 'Fixed single date that is not a holiday test' );
157 is ( $cal->is_holiday($sunday_exception), 0, 'Exception holiday is not a closed day test' );
160 { # Bugzilla #8966 - is_holiday truncates referenced date
161 my $later_dt = DateTime->new( # Monday
162 year => 2012,
163 month => 9,
164 day => 17,
165 hour => 17,
166 minute => 30,
167 time_zone => 'Europe/London',
171 is( $cal->is_holiday($later_dt), 0, 'bz-8966 (1/2) Apply is_holiday for the next test' );
172 cmp_ok( $later_dt, 'eq', '2012-09-17T17:30:00', 'bz-8966 (2/2) Date should be the same after is_holiday' );
175 { # Bugzilla #8800 - is_holiday should use truncated date for 'contains' call
176 my $single_holiday_time = DateTime->new(
177 year => 2011,
178 month => 6,
179 day => 1,
180 hour => 11,
181 minute => 2
184 is( $cal->is_holiday($single_holiday_time),
185 $cal->is_holiday($single_holiday) ,
186 'bz-8800 is_holiday should truncate the date for holiday validation' );
189 my $one_day_dur = DateTime::Duration->new( days => 1 );
190 my $two_day_dur = DateTime::Duration->new( days => 2 );
191 my $seven_day_dur = DateTime::Duration->new( days => 7 );
193 my $dt = dt_from_string( '2012-07-03','iso' ); #tuesday
195 my $test_dt = DateTime->new( # Monday
196 year => 2012,
197 month => 7,
198 day => 23,
199 hour => 11,
200 minute => 53,
203 my $later_dt = DateTime->new( # Monday
204 year => 2012,
205 month => 9,
206 day => 17,
207 hour => 17,
208 minute => 30,
209 time_zone => 'Europe/London',
212 { ## 'Datedue' tests
214 $module_context->unmock('preference');
215 $module_context->mock(
216 'preference',
217 sub {
218 return 'Datedue';
222 $cal = Koha::Calendar->new( branchcode => 'MPL' );
224 is($cal->addDate( $dt, $one_day_dur, 'days' ), # tuesday
225 dt_from_string('2012-07-05','iso'),
226 'Single day add (Datedue, matches holiday, shift)' );
228 is($cal->addDate( $dt, $two_day_dur, 'days' ),
229 dt_from_string('2012-07-05','iso'),
230 'Two days add, skips holiday (Datedue)' );
232 cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ), 'eq',
233 '2012-07-30T11:53:00',
234 'Add 7 days (Datedue)' );
236 is( $cal->addDate( $saturday, $one_day_dur, 'days' )->day_of_week, 1,
237 'addDate skips closed Sunday (Datedue)' );
239 is( $cal->addDate($day_after_christmas, -1, 'days')->ymd(), '2012-12-24',
240 'Negative call to addDate (Datedue)' );
242 ## Note that the days_between API says closed days are not considered.
243 ## This tests are here as an API test.
244 cmp_ok( $cal->days_between( $test_dt, $later_dt )->in_units('days'),
245 '==', 40, 'days_between calculates correctly (Days)' );
247 cmp_ok( $cal->days_between( $later_dt, $test_dt )->in_units('days'),
248 '==', 40, 'Test parameter order not relevant (Days)' );
251 { ## 'Calendar' tests'
253 $module_context->unmock('preference');
254 $module_context->mock(
255 'preference',
256 sub {
257 return 'Calendar';
261 $cal = Koha::Calendar->new( branchcode => 'MPL' );
263 $dt = dt_from_string('2012-07-03','iso');
265 is($cal->addDate( $dt, $one_day_dur, 'days' ),
266 dt_from_string('2012-07-05','iso'),
267 'Single day add (Calendar)' );
269 cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ), 'eq',
270 '2012-08-01T11:53:00',
271 'Add 7 days (Calendar)' );
273 is( $cal->addDate( $saturday, $one_day_dur, 'days' )->day_of_week, 1,
274 'addDate skips closed Sunday (Calendar)' );
276 is( $cal->addDate($day_after_christmas, -1, 'days')->ymd(), '2012-12-24',
277 'Negative call to addDate (Calendar)' );
279 cmp_ok( $cal->days_between( $test_dt, $later_dt )->in_units('days'),
280 '==', 40, 'days_between calculates correctly (Calendar)' );
282 cmp_ok( $cal->days_between( $later_dt, $test_dt )->in_units('days'),
283 '==', 40, 'Test parameter order not relevant (Calendar)' );
287 { ## 'Days' tests
288 $module_context->unmock('preference');
289 $module_context->mock(
290 'preference',
291 sub {
292 return 'Days';
296 $cal = Koha::Calendar->new( branchcode => 'MPL' );
298 $dt = dt_from_string('2012-07-03','iso');
300 is($cal->addDate( $dt, $one_day_dur, 'days' ),
301 dt_from_string('2012-07-04','iso'),
302 'Single day add (Days)' );
304 cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ),'eq',
305 '2012-07-30T11:53:00',
306 'Add 7 days (Days)' );
308 is( $cal->addDate( $saturday, $one_day_dur, 'days' )->day_of_week, 7,
309 'addDate doesn\'t skip closed Sunday (Days)' );
311 is( $cal->addDate($day_after_christmas, -1, 'days')->ymd(), '2012-12-25',
312 'Negative call to addDate (Days)' );
314 ## Note that the days_between API says closed days are not considered.
315 ## This tests are here as an API test.
316 cmp_ok( $cal->days_between( $test_dt, $later_dt )->in_units('days'),
317 '==', 40, 'days_between calculates correctly (Days)' );
319 cmp_ok( $cal->days_between( $later_dt, $test_dt )->in_units('days'),
320 '==', 40, 'Test parameter order not relevant (Days)' );