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