Bug 19231: Add number of attached items to confirm message when deleting course
[koha.git] / t / Calendar.t
blobe0128b76f43256997128e5b451784732cbf8f7d3
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 sub fixtures {
43 my ( $data ) = @_;
44 fixtures_ok [
45 Biblio => [
46 [ qw/ biblionumber datecreated timestamp / ],
47 @$data,
49 ], 'add fixtures';
52 my $db = Test::MockModule->new('Koha::Database');
53 $db->mock(
54 _new_schema => sub { return Schema(); }
57 # We need to mock the C4::Context->preference method for
58 # simplicity and re-usability of the session definition. Any
59 # syspref fits for syspref-agnostic tests.
60 my $module_context = new Test::MockModule('C4::Context');
61 $module_context->mock(
62 'preference',
63 sub {
64 return 'Calendar';
68 fixtures_ok [
69 # weekly holidays
70 RepeatableHoliday => [
71 [ qw( branchcode day month weekday title description) ],
72 [ 'MPL', undef, undef, 0, '', '' ], # sundays
73 [ 'MPL', undef, undef, 6, '', '' ],# saturdays
74 [ 'MPL', 1, 1, undef, '', ''], # new year's day
75 [ 'MPL', 25, 12, undef, '', ''], # chrismas
77 # exception holidays
78 SpecialHoliday => [
79 [qw( branchcode day month year title description isexception )],
80 [ 'MPL', 11, 11, 2012, '', '', 1 ], # sunday exception
81 [ 'MPL', 1, 6, 2011, '', '', 0 ],
82 [ 'MPL', 4, 7, 2012, '', '', 0 ],
83 [ 'CPL', 6, 8, 2012, '', '', 0 ],
85 ], "add fixtures";
87 my $cache = Koha::Caches->get_instance();
88 $cache->clear_from_cache( 'single_holidays' ) ;
89 $cache->clear_from_cache( 'exception_holidays' ) ;
91 # 'MPL' branch is arbitrary, is not used at all but is needed for initialization
92 my $cal = Koha::Calendar->new( branchcode => 'MPL' );
94 isa_ok( $cal, 'Koha::Calendar', 'Calendar class returned' );
96 my $saturday = DateTime->new(
97 year => 2012,
98 month => 11,
99 day => 24,
102 my $sunday = DateTime->new(
103 year => 2012,
104 month => 11,
105 day => 25,
108 my $monday = DateTime->new(
109 year => 2012,
110 month => 11,
111 day => 26,
114 my $new_year = DateTime->new(
115 year => 2013,
116 month => 1,
117 day => 1,
120 my $single_holiday = DateTime->new(
121 year => 2011,
122 month => 6,
123 day => 1,
124 ); # should be a holiday
126 my $notspecial = DateTime->new(
127 year => 2011,
128 month => 6,
129 day => 2
130 ); # should NOT be a holiday
132 my $sunday_exception = DateTime->new(
133 year => 2012,
134 month => 11,
135 day => 11
138 my $day_after_christmas = DateTime->new(
139 year => 2012,
140 month => 12,
141 day => 26
142 ); # for testing negative addDate
144 my $holiday_for_another_branch = DateTime->new(
145 year => 2012,
146 month => 8,
147 day => 6, # This is a monday
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' );
164 { # Bugzilla #8966 - is_holiday truncates referenced date
165 my $later_dt = DateTime->new( # Monday
166 year => 2012,
167 month => 9,
168 day => 17,
169 hour => 17,
170 minute => 30,
171 time_zone => 'Europe/London',
175 is( $cal->is_holiday($later_dt), 0, 'bz-8966 (1/2) Apply is_holiday for the next test' );
176 cmp_ok( $later_dt, 'eq', '2012-09-17T17:30:00', 'bz-8966 (2/2) Date should be the same after is_holiday' );
179 { # Bugzilla #8800 - is_holiday should use truncated date for 'contains' call
180 my $single_holiday_time = DateTime->new(
181 year => 2011,
182 month => 6,
183 day => 1,
184 hour => 11,
185 minute => 2
188 is( $cal->is_holiday($single_holiday_time),
189 $cal->is_holiday($single_holiday) ,
190 'bz-8800 is_holiday should truncate the date for holiday validation' );
193 my $one_day_dur = DateTime::Duration->new( days => 1 );
194 my $two_day_dur = DateTime::Duration->new( days => 2 );
195 my $seven_day_dur = DateTime::Duration->new( days => 7 );
197 my $dt = dt_from_string( '2012-07-03','iso' ); #tuesday
199 my $test_dt = DateTime->new( # Monday
200 year => 2012,
201 month => 7,
202 day => 23,
203 hour => 11,
204 minute => 53,
207 my $later_dt = DateTime->new( # Monday
208 year => 2012,
209 month => 9,
210 day => 17,
211 hour => 17,
212 minute => 30,
213 time_zone => 'Europe/London',
216 { ## 'Datedue' tests
218 $module_context->unmock('preference');
219 $module_context->mock(
220 'preference',
221 sub {
222 return 'Datedue';
226 $cal = Koha::Calendar->new( branchcode => 'MPL' );
228 is($cal->addDate( $dt, $one_day_dur, 'days' ), # tuesday
229 dt_from_string('2012-07-05','iso'),
230 'Single day add (Datedue, matches holiday, shift)' );
232 is($cal->addDate( $dt, $two_day_dur, 'days' ),
233 dt_from_string('2012-07-05','iso'),
234 'Two days add, skips holiday (Datedue)' );
236 cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ), 'eq',
237 '2012-07-30T11:53:00',
238 'Add 7 days (Datedue)' );
240 is( $cal->addDate( $saturday, $one_day_dur, 'days' )->day_of_week, 1,
241 'addDate skips closed Sunday (Datedue)' );
243 is( $cal->addDate($day_after_christmas, -1, 'days')->ymd(), '2012-12-24',
244 'Negative call to addDate (Datedue)' );
246 ## Note that the days_between API says closed days are not considered.
247 ## This tests are here as an API test.
248 cmp_ok( $cal->days_between( $test_dt, $later_dt )->in_units('days'),
249 '==', 40, 'days_between calculates correctly (Days)' );
251 cmp_ok( $cal->days_between( $later_dt, $test_dt )->in_units('days'),
252 '==', 40, 'Test parameter order not relevant (Days)' );
255 { ## 'Calendar' tests'
257 $module_context->unmock('preference');
258 $module_context->mock(
259 'preference',
260 sub {
261 return 'Calendar';
265 $cal = Koha::Calendar->new( branchcode => 'MPL' );
267 $dt = dt_from_string('2012-07-03','iso');
269 is($cal->addDate( $dt, $one_day_dur, 'days' ),
270 dt_from_string('2012-07-05','iso'),
271 'Single day add (Calendar)' );
273 cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ), 'eq',
274 '2012-08-01T11:53:00',
275 'Add 7 days (Calendar)' );
277 is( $cal->addDate( $saturday, $one_day_dur, 'days' )->day_of_week, 1,
278 'addDate skips closed Sunday (Calendar)' );
280 is( $cal->addDate($day_after_christmas, -1, 'days')->ymd(), '2012-12-24',
281 'Negative call to addDate (Calendar)' );
283 cmp_ok( $cal->days_between( $test_dt, $later_dt )->in_units('days'),
284 '==', 40, 'days_between calculates correctly (Calendar)' );
286 cmp_ok( $cal->days_between( $later_dt, $test_dt )->in_units('days'),
287 '==', 40, 'Test parameter order not relevant (Calendar)' );
291 { ## 'Days' tests
292 $module_context->unmock('preference');
293 $module_context->mock(
294 'preference',
295 sub {
296 return 'Days';
300 $cal = Koha::Calendar->new( branchcode => 'MPL' );
302 $dt = dt_from_string('2012-07-03','iso');
304 is($cal->addDate( $dt, $one_day_dur, 'days' ),
305 dt_from_string('2012-07-04','iso'),
306 'Single day add (Days)' );
308 cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ),'eq',
309 '2012-07-30T11:53:00',
310 'Add 7 days (Days)' );
312 is( $cal->addDate( $saturday, $one_day_dur, 'days' )->day_of_week, 7,
313 'addDate doesn\'t skip closed Sunday (Days)' );
315 is( $cal->addDate($day_after_christmas, -1, 'days')->ymd(), '2012-12-25',
316 'Negative call to addDate (Days)' );
318 ## Note that the days_between API says closed days are not considered.
319 ## This tests are here as an API test.
320 cmp_ok( $cal->days_between( $test_dt, $later_dt )->in_units('days'),
321 '==', 40, 'days_between calculates correctly (Days)' );
323 cmp_ok( $cal->days_between( $later_dt, $test_dt )->in_units('days'),
324 '==', 40, 'Test parameter order not relevant (Days)' );
329 $cal = Koha::Calendar->new( branchcode => 'CPL' );
330 is ( $cal->is_holiday($single_holiday), 0, 'Single holiday for MPL, not CPL' );
331 is ( $cal->is_holiday($holiday_for_another_branch), 1, 'Holiday defined for CPL should be defined as an holiday' );
334 END {
335 $cache->clear_from_cache( 'single_holidays' ) ;
336 $cache->clear_from_cache( 'exception_holidays' ) ;