Fix use of non-existent variable $borrower
[koha.git] / t / Calendar.t
blob4d48c02f4f298f5ec45c06a115f3952d3740c0fa
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 => 38;
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 ],
88 [ 'CPL', 6, 8, 2012, '', '', 0 ],
90 ], "add fixtures";
92 my $cache = Koha::Cache->get_instance();
93 $cache->clear_from_cache( 'single_holidays') ;
95 # 'MPL' branch is arbitrary, is not used at all but is needed for initialization
96 my $cal = Koha::Calendar->new( branchcode => 'MPL' );
98 isa_ok( $cal, 'Koha::Calendar', 'Calendar class returned' );
100 my $saturday = DateTime->new(
101 year => 2012,
102 month => 11,
103 day => 24,
106 my $sunday = DateTime->new(
107 year => 2012,
108 month => 11,
109 day => 25,
112 my $monday = DateTime->new(
113 year => 2012,
114 month => 11,
115 day => 26,
118 my $new_year = DateTime->new(
119 year => 2013,
120 month => 1,
121 day => 1,
124 my $single_holiday = DateTime->new(
125 year => 2011,
126 month => 6,
127 day => 1,
128 ); # should be a holiday
130 my $notspecial = DateTime->new(
131 year => 2011,
132 month => 6,
133 day => 2
134 ); # should NOT be a holiday
136 my $sunday_exception = DateTime->new(
137 year => 2012,
138 month => 11,
139 day => 11
142 my $day_after_christmas = DateTime->new(
143 year => 2012,
144 month => 12,
145 day => 26
146 ); # for testing negative addDate
148 my $holiday_for_another_branch = DateTime->new(
149 year => 2012,
150 month => 8,
151 day => 6, # This is a monday
154 { # Syspref-agnostic tests
155 is ( $saturday->day_of_week, 6, '\'$saturday\' is actually a saturday (6th day of week)');
156 is ( $sunday->day_of_week, 7, '\'$sunday\' is actually a sunday (7th day of week)');
157 is ( $monday->day_of_week, 1, '\'$monday\' is actually a monday (1st day of week)');
158 is ( $cal->is_holiday($saturday), 1, 'Saturday is a closed day' );
159 is ( $cal->is_holiday($sunday), 1, 'Sunday is a closed day' );
160 is ( $cal->is_holiday($monday), 0, 'Monday is not a closed day' );
161 is ( $cal->is_holiday($new_year), 1, 'Month/Day closed day test (New year\'s day)' );
162 is ( $cal->is_holiday($single_holiday), 1, 'Single holiday closed day test' );
163 is ( $cal->is_holiday($notspecial), 0, 'Fixed single date that is not a holiday test' );
164 is ( $cal->is_holiday($sunday_exception), 0, 'Exception holiday is not a closed day test' );
165 is ( $cal->is_holiday($holiday_for_another_branch), 0, 'Holiday defined for another branch should not be defined as an holiday' );
168 { # Bugzilla #8966 - is_holiday truncates referenced date
169 my $later_dt = DateTime->new( # Monday
170 year => 2012,
171 month => 9,
172 day => 17,
173 hour => 17,
174 minute => 30,
175 time_zone => 'Europe/London',
179 is( $cal->is_holiday($later_dt), 0, 'bz-8966 (1/2) Apply is_holiday for the next test' );
180 cmp_ok( $later_dt, 'eq', '2012-09-17T17:30:00', 'bz-8966 (2/2) Date should be the same after is_holiday' );
183 { # Bugzilla #8800 - is_holiday should use truncated date for 'contains' call
184 my $single_holiday_time = DateTime->new(
185 year => 2011,
186 month => 6,
187 day => 1,
188 hour => 11,
189 minute => 2
192 is( $cal->is_holiday($single_holiday_time),
193 $cal->is_holiday($single_holiday) ,
194 'bz-8800 is_holiday should truncate the date for holiday validation' );
197 my $one_day_dur = DateTime::Duration->new( days => 1 );
198 my $two_day_dur = DateTime::Duration->new( days => 2 );
199 my $seven_day_dur = DateTime::Duration->new( days => 7 );
201 my $dt = dt_from_string( '2012-07-03','iso' ); #tuesday
203 my $test_dt = DateTime->new( # Monday
204 year => 2012,
205 month => 7,
206 day => 23,
207 hour => 11,
208 minute => 53,
211 my $later_dt = DateTime->new( # Monday
212 year => 2012,
213 month => 9,
214 day => 17,
215 hour => 17,
216 minute => 30,
217 time_zone => 'Europe/London',
220 { ## 'Datedue' tests
222 $module_context->unmock('preference');
223 $module_context->mock(
224 'preference',
225 sub {
226 return 'Datedue';
230 $cal = Koha::Calendar->new( branchcode => 'MPL' );
232 is($cal->addDate( $dt, $one_day_dur, 'days' ), # tuesday
233 dt_from_string('2012-07-05','iso'),
234 'Single day add (Datedue, matches holiday, shift)' );
236 is($cal->addDate( $dt, $two_day_dur, 'days' ),
237 dt_from_string('2012-07-05','iso'),
238 'Two days add, skips holiday (Datedue)' );
240 cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ), 'eq',
241 '2012-07-30T11:53:00',
242 'Add 7 days (Datedue)' );
244 is( $cal->addDate( $saturday, $one_day_dur, 'days' )->day_of_week, 1,
245 'addDate skips closed Sunday (Datedue)' );
247 is( $cal->addDate($day_after_christmas, -1, 'days')->ymd(), '2012-12-24',
248 'Negative call to addDate (Datedue)' );
250 ## Note that the days_between API says closed days are not considered.
251 ## This tests are here as an API test.
252 cmp_ok( $cal->days_between( $test_dt, $later_dt )->in_units('days'),
253 '==', 40, 'days_between calculates correctly (Days)' );
255 cmp_ok( $cal->days_between( $later_dt, $test_dt )->in_units('days'),
256 '==', 40, 'Test parameter order not relevant (Days)' );
259 { ## 'Calendar' tests'
261 $module_context->unmock('preference');
262 $module_context->mock(
263 'preference',
264 sub {
265 return 'Calendar';
269 $cal = Koha::Calendar->new( branchcode => 'MPL' );
271 $dt = dt_from_string('2012-07-03','iso');
273 is($cal->addDate( $dt, $one_day_dur, 'days' ),
274 dt_from_string('2012-07-05','iso'),
275 'Single day add (Calendar)' );
277 cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ), 'eq',
278 '2012-08-01T11:53:00',
279 'Add 7 days (Calendar)' );
281 is( $cal->addDate( $saturday, $one_day_dur, 'days' )->day_of_week, 1,
282 'addDate skips closed Sunday (Calendar)' );
284 is( $cal->addDate($day_after_christmas, -1, 'days')->ymd(), '2012-12-24',
285 'Negative call to addDate (Calendar)' );
287 cmp_ok( $cal->days_between( $test_dt, $later_dt )->in_units('days'),
288 '==', 40, 'days_between calculates correctly (Calendar)' );
290 cmp_ok( $cal->days_between( $later_dt, $test_dt )->in_units('days'),
291 '==', 40, 'Test parameter order not relevant (Calendar)' );
295 { ## 'Days' tests
296 $module_context->unmock('preference');
297 $module_context->mock(
298 'preference',
299 sub {
300 return 'Days';
304 $cal = Koha::Calendar->new( branchcode => 'MPL' );
306 $dt = dt_from_string('2012-07-03','iso');
308 is($cal->addDate( $dt, $one_day_dur, 'days' ),
309 dt_from_string('2012-07-04','iso'),
310 'Single day add (Days)' );
312 cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ),'eq',
313 '2012-07-30T11:53:00',
314 'Add 7 days (Days)' );
316 is( $cal->addDate( $saturday, $one_day_dur, 'days' )->day_of_week, 7,
317 'addDate doesn\'t skip closed Sunday (Days)' );
319 is( $cal->addDate($day_after_christmas, -1, 'days')->ymd(), '2012-12-25',
320 'Negative call to addDate (Days)' );
322 ## Note that the days_between API says closed days are not considered.
323 ## This tests are here as an API test.
324 cmp_ok( $cal->days_between( $test_dt, $later_dt )->in_units('days'),
325 '==', 40, 'days_between calculates correctly (Days)' );
327 cmp_ok( $cal->days_between( $later_dt, $test_dt )->in_units('days'),
328 '==', 40, 'Test parameter order not relevant (Days)' );
333 $cal = Koha::Calendar->new( branchcode => 'CPL' );
334 is ( $cal->is_holiday($single_holiday), 0, 'Single holiday for MPL, not CPL' );
335 is ( $cal->is_holiday($holiday_for_another_branch), 1, 'Holiday defined for CPL should be defined as an holiday' );