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