Bug 14778: Make Barcodes_ValueBuilder.t db dependent
[koha.git] / t / Calendar.t
1 #!/usr/bin/env perl
2
3 use Modern::Perl;
4 use DateTime;
5 use DateTime::Duration;
6 use Test::More tests => 35;
7 use Test::MockModule;
8 use Koha::Cache;
9 use Koha::DateUtils;
10
11 BEGIN {
12     use_ok('Koha::Calendar');
13
14     # This was the only test C4 had
15     # Remove when no longer used
16     #use_ok('C4::Calendar'); # not used anymore?
17 }
18 use Test::DBIx::Class {
19     schema_class => 'Koha::Schema',
20     connect_info => ['dbi:SQLite:dbname=:memory:','',''],
21     connect_opts => { name_sep => '.', quote_char => '`', },
22     fixture_class => '::Populate',
23 }, 'Biblio' ;
24
25 sub fixtures {
26     my ( $data ) = @_;
27     fixtures_ok [
28         Biblio => [
29             [ qw/ biblionumber datecreated timestamp  / ],
30             @$data,
31         ],
32     ], 'add fixtures';
33 }
34
35 my $db = Test::MockModule->new('Koha::Database');
36 $db->mock(
37     _new_schema => sub { return Schema(); }
38 );
39
40 # We need to mock the C4::Context->preference method for
41 # simplicity and re-usability of the session definition. Any
42 # syspref fits for syspref-agnostic tests.
43 my $module_context = new Test::MockModule('C4::Context');
44 $module_context->mock(
45     'preference',
46     sub {
47         return 'Calendar';
48     }
49 );
50
51 fixtures_ok [
52     # weekly holidays
53     RepeatableHoliday => [
54         [ qw( branchcode day month weekday title description) ],
55         [ 'MPL', undef, undef, 0, '', '' ], # sundays
56         [ 'MPL', undef, undef, 6, '', '' ],# saturdays
57         [ 'MPL', 1, 1, undef, '', ''], # new year's day
58         [ 'MPL', 25, 12, undef, '', ''], # chrismas
59     ],
60     # exception holidays
61     SpecialHoliday => [
62         [qw( branchcode day month year title description isexception )],
63         [ 'MPL', 11, 11, 2012, '', '', 1 ],    # sunday exception
64         [ 'MPL', 1,  6,  2011, '', '', 0 ],
65         [ 'MPL', 4,  7,  2012, '', '', 0 ],
66       ],
67 ], "add fixtures";
68
69 my $cache = Koha::Cache->get_instance();
70 $cache->clear_from_cache( 'single_holidays') ;
71
72 # 'MPL' branch is arbitrary, is not used at all but is needed for initialization
73 my $cal = Koha::Calendar->new( branchcode => 'MPL' );
74
75 isa_ok( $cal, 'Koha::Calendar', 'Calendar class returned' );
76
77 my $saturday = DateTime->new(
78     year      => 2012,
79     month     => 11,
80     day       => 24,
81 );
82
83 my $sunday = DateTime->new(
84     year      => 2012,
85     month     => 11,
86     day       => 25,
87 );
88
89 my $monday = DateTime->new(
90     year      => 2012,
91     month     => 11,
92     day       => 26,
93 );
94
95 my $new_year = DateTime->new(
96     year        => 2013,
97     month       => 1,
98     day         => 1,
99 );
100
101 my $single_holiday = DateTime->new(
102     year      => 2011,
103     month     => 6,
104     day       => 1,
105 );    # should be a holiday
106
107 my $notspecial = DateTime->new(
108     year      => 2011,
109     month     => 6,
110     day       => 2
111 );    # should NOT be a holiday
112
113 my $sunday_exception = DateTime->new(
114     year      => 2012,
115     month     => 11,
116     day       => 11
117 );
118
119 my $day_after_christmas = DateTime->new(
120     year    => 2012,
121     month   => 12,
122     day     => 26
123 );  # for testing negative addDate
124
125 {   # Syspref-agnostic tests
126     is ( $saturday->day_of_week, 6, '\'$saturday\' is actually a saturday (6th day of week)');
127     is ( $sunday->day_of_week, 7, '\'$sunday\' is actually a sunday (7th day of week)');
128     is ( $monday->day_of_week, 1, '\'$monday\' is actually a monday (1st day of week)');
129     is ( $cal->is_holiday($saturday), 1, 'Saturday is a closed day' );
130     is ( $cal->is_holiday($sunday), 1, 'Sunday is a closed day' );
131     is ( $cal->is_holiday($monday), 0, 'Monday is not a closed day' );
132     is ( $cal->is_holiday($new_year), 1, 'Month/Day closed day test (New year\'s day)' );
133     is ( $cal->is_holiday($single_holiday), 1, 'Single holiday closed day test' );
134     is ( $cal->is_holiday($notspecial), 0, 'Fixed single date that is not a holiday test' );
135     is ( $cal->is_holiday($sunday_exception), 0, 'Exception holiday is not a closed day test' );
136 }
137
138 {   # Bugzilla #8966 - is_holiday truncates referenced date
139     my $later_dt = DateTime->new(    # Monday
140         year      => 2012,
141         month     => 9,
142         day       => 17,
143         hour      => 17,
144         minute    => 30,
145         time_zone => 'Europe/London',
146     );
147
148
149     is( $cal->is_holiday($later_dt), 0, 'bz-8966 (1/2) Apply is_holiday for the next test' );
150     cmp_ok( $later_dt, 'eq', '2012-09-17T17:30:00', 'bz-8966 (2/2) Date should be the same after is_holiday' );
151 }
152
153 {   # Bugzilla #8800 - is_holiday should use truncated date for 'contains' call
154     my $single_holiday_time = DateTime->new(
155         year  => 2011,
156         month => 6,
157         day   => 1,
158         hour  => 11,
159         minute  => 2
160     );
161
162     is( $cal->is_holiday($single_holiday_time),
163         $cal->is_holiday($single_holiday) ,
164         'bz-8800 is_holiday should truncate the date for holiday validation' );
165 }
166
167     my $one_day_dur = DateTime::Duration->new( days => 1 );
168     my $two_day_dur = DateTime::Duration->new( days => 2 );
169     my $seven_day_dur = DateTime::Duration->new( days => 7 );
170
171     my $dt = dt_from_string( '2012-07-03','iso' ); #tuesday
172
173     my $test_dt = DateTime->new(    # Monday
174         year      => 2012,
175         month     => 7,
176         day       => 23,
177         hour      => 11,
178         minute    => 53,
179     );
180
181     my $later_dt = DateTime->new(    # Monday
182         year      => 2012,
183         month     => 9,
184         day       => 17,
185         hour      => 17,
186         minute    => 30,
187         time_zone => 'Europe/London',
188     );
189
190 {    ## 'Datedue' tests
191
192     $module_context->unmock('preference');
193     $module_context->mock(
194         'preference',
195         sub {
196             return 'Datedue';
197         }
198     );
199
200     $cal = Koha::Calendar->new( branchcode => 'MPL' );
201
202     is($cal->addDate( $dt, $one_day_dur, 'days' ), # tuesday
203         dt_from_string('2012-07-05','iso'),
204         'Single day add (Datedue, matches holiday, shift)' );
205
206     is($cal->addDate( $dt, $two_day_dur, 'days' ),
207         dt_from_string('2012-07-05','iso'),
208         'Two days add, skips holiday (Datedue)' );
209
210     cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ), 'eq',
211         '2012-07-30T11:53:00',
212         'Add 7 days (Datedue)' );
213
214     is( $cal->addDate( $saturday, $one_day_dur, 'days' )->day_of_week, 1,
215         'addDate skips closed Sunday (Datedue)' );
216
217     is( $cal->addDate($day_after_christmas, -1, 'days')->ymd(), '2012-12-24',
218         'Negative call to addDate (Datedue)' );
219
220     ## Note that the days_between API says closed days are not considered.
221     ## This tests are here as an API test.
222     cmp_ok( $cal->days_between( $test_dt, $later_dt )->in_units('days'),
223                 '==', 40, 'days_between calculates correctly (Days)' );
224
225     cmp_ok( $cal->days_between( $later_dt, $test_dt )->in_units('days'),
226                 '==', 40, 'Test parameter order not relevant (Days)' );
227 }
228
229 {   ## 'Calendar' tests'
230
231     $module_context->unmock('preference');
232     $module_context->mock(
233         'preference',
234         sub {
235             return 'Calendar';
236         }
237     );
238
239     $cal = Koha::Calendar->new( branchcode => 'MPL' );
240
241     $dt = dt_from_string('2012-07-03','iso');
242
243     is($cal->addDate( $dt, $one_day_dur, 'days' ),
244         dt_from_string('2012-07-05','iso'),
245         'Single day add (Calendar)' );
246
247     cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ), 'eq',
248        '2012-08-01T11:53:00',
249        'Add 7 days (Calendar)' );
250
251     is( $cal->addDate( $saturday, $one_day_dur, 'days' )->day_of_week, 1,
252             'addDate skips closed Sunday (Calendar)' );
253
254     is( $cal->addDate($day_after_christmas, -1, 'days')->ymd(), '2012-12-24',
255             'Negative call to addDate (Calendar)' );
256
257     cmp_ok( $cal->days_between( $test_dt, $later_dt )->in_units('days'),
258                 '==', 40, 'days_between calculates correctly (Calendar)' );
259
260     cmp_ok( $cal->days_between( $later_dt, $test_dt )->in_units('days'),
261                 '==', 40, 'Test parameter order not relevant (Calendar)' );
262 }
263
264
265 {   ## 'Days' tests
266     $module_context->unmock('preference');
267     $module_context->mock(
268         'preference',
269         sub {
270             return 'Days';
271         }
272     );
273
274     $cal = Koha::Calendar->new( branchcode => 'MPL' );
275
276     $dt = dt_from_string('2012-07-03','iso');
277
278     is($cal->addDate( $dt, $one_day_dur, 'days' ),
279         dt_from_string('2012-07-04','iso'),
280         'Single day add (Days)' );
281
282     cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ),'eq',
283         '2012-07-30T11:53:00',
284         'Add 7 days (Days)' );
285
286     is( $cal->addDate( $saturday, $one_day_dur, 'days' )->day_of_week, 7,
287         'addDate doesn\'t skip closed Sunday (Days)' );
288
289     is( $cal->addDate($day_after_christmas, -1, 'days')->ymd(), '2012-12-25',
290         'Negative call to addDate (Days)' );
291
292     ## Note that the days_between API says closed days are not considered.
293     ## This tests are here as an API test.
294     cmp_ok( $cal->days_between( $test_dt, $later_dt )->in_units('days'),
295                 '==', 40, 'days_between calculates correctly (Days)' );
296
297     cmp_ok( $cal->days_between( $later_dt, $test_dt )->in_units('days'),
298                 '==', 40, 'Test parameter order not relevant (Days)' );
299
300 }