forked from Koha-Community/Koha
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCalendar.t
executable file
·328 lines (259 loc) · 9.52 KB
/
Calendar.t
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#!/usr/bin/env perl
use strict;
use warnings;
use DateTime;
use DateTime::Duration;
use Test::More tests => 35;
use Test::MockModule;
use DBD::Mock;
use Koha::DateUtils;
BEGIN {
use_ok('Koha::Calendar');
# This was the only test C4 had
# Remove when no longer used
use_ok('C4::Calendar');
}
my $module_context = new Test::MockModule('C4::Context');
$module_context->mock(
'_new_dbh',
sub {
my $dbh = DBI->connect( 'DBI:Mock:', '', '' )
|| die "Cannot create handle: $DBI::errstr\n";
return $dbh;
}
);
# We need to mock the C4::Context->preference method for
# simplicity and re-usability of the session definition. Any
# syspref fits for syspref-agnostic tests.
$module_context->mock(
'preference',
sub {
return 'Calendar';
}
);
SKIP: {
skip "DBD::Mock is too old", 33
unless $DBD::Mock::VERSION >= 1.45;
my $holidays_session = DBD::Mock::Session->new('holidays_session' => (
{ # weekly holidays
statement => "SELECT weekday FROM repeatable_holidays WHERE branchcode = ? AND weekday IS NOT NULL",
results => [
['weekday'],
[0], # sundays
[6] # saturdays
]
},
{ # day and month repeatable holidays
statement => "SELECT day, month FROM repeatable_holidays WHERE branchcode = ? AND weekday IS NULL",
results => [
[ 'month', 'day' ],
[ 1, 1 ], # new year's day
[12,25] # christmas
]
},
{ # exception holidays
statement => "SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = 1",
results => [
[ 'day', 'month', 'year' ],
[ 11, 11, 2012 ] # sunday exception
]
},
{ # single holidays
statement => "SELECT day, month, year FROM special_holidays WHERE branchcode = ? AND isexception = 0",
results => [
[ 'day', 'month', 'year' ],
[ 1, 6, 2011 ], # single holiday
[ 4, 7, 2012 ]
]
}
));
# Initialize the global $dbh variable
my $dbh = C4::Context->dbh();
# Apply the mock session
$dbh->{ mock_session } = $holidays_session;
# 'MPL' branch is arbitrary, is not used at all but is needed for initialization
my $cal = Koha::Calendar->new( branchcode => 'MPL' );
isa_ok( $cal, 'Koha::Calendar', 'Calendar class returned' );
my $saturday = DateTime->new(
year => 2012,
month => 11,
day => 24,
);
my $sunday = DateTime->new(
year => 2012,
month => 11,
day => 25,
);
my $monday = DateTime->new(
year => 2012,
month => 11,
day => 26,
);
my $new_year = DateTime->new(
year => 2013,
month => 1,
day => 1,
);
my $single_holiday = DateTime->new(
year => 2011,
month => 6,
day => 1,
); # should be a holiday
my $notspecial = DateTime->new(
year => 2011,
month => 6,
day => 2
); # should NOT be a holiday
my $sunday_exception = DateTime->new(
year => 2012,
month => 11,
day => 11
);
my $day_after_christmas = DateTime->new(
year => 2012,
month => 12,
day => 26
); # for testing negative addDate
{ # Syspref-agnostic tests
is ( $saturday->day_of_week, 6, '\'$saturday\' is actually a saturday (6th day of week)');
is ( $sunday->day_of_week, 7, '\'$sunday\' is actually a sunday (7th day of week)');
is ( $monday->day_of_week, 1, '\'$monday\' is actually a monday (1st day of week)');
is ( $cal->is_holiday($saturday), 1, 'Saturday is a closed day' );
is ( $cal->is_holiday($sunday), 1, 'Sunday is a closed day' );
is ( $cal->is_holiday($monday), 0, 'Monday is not a closed day' );
is ( $cal->is_holiday($new_year), 1, 'Month/Day closed day test (New year\'s day)' );
is ( $cal->is_holiday($single_holiday), 1, 'Single holiday closed day test' );
is ( $cal->is_holiday($notspecial), 0, 'Fixed single date that is not a holiday test' );
is ( $cal->is_holiday($sunday_exception), 0, 'Exception holiday is not a closed day test' );
}
{ # Bugzilla #8966 - is_holiday truncates referenced date
my $later_dt = DateTime->new( # Monday
year => 2012,
month => 9,
day => 17,
hour => 17,
minute => 30,
time_zone => 'Europe/London',
);
is( $cal->is_holiday($later_dt), 0, 'bz-8966 (1/2) Apply is_holiday for the next test' );
cmp_ok( $later_dt, 'eq', '2012-09-17T17:30:00', 'bz-8966 (2/2) Date should be the same after is_holiday' );
}
{ # Bugzilla #8800 - is_holiday should use truncated date for 'contains' call
my $single_holiday_time = DateTime->new(
year => 2011,
month => 6,
day => 1,
hour => 11,
minute => 2
);
is( $cal->is_holiday($single_holiday_time),
$cal->is_holiday($single_holiday) ,
'bz-8800 is_holiday should truncate the date for holiday validation' );
}
my $one_day_dur = DateTime::Duration->new( days => 1 );
my $two_day_dur = DateTime::Duration->new( days => 2 );
my $seven_day_dur = DateTime::Duration->new( days => 7 );
my $dt = dt_from_string( '2012-07-03','iso' );
my $test_dt = DateTime->new( # Monday
year => 2012,
month => 7,
day => 23,
hour => 11,
minute => 53,
);
my $later_dt = DateTime->new( # Monday
year => 2012,
month => 9,
day => 17,
hour => 17,
minute => 30,
time_zone => 'Europe/London',
);
{ ## 'Datedue' tests
$module_context->unmock('preference');
$module_context->mock(
'preference',
sub {
return 'Datedue';
}
);
# rewind dbh session
$holidays_session->reset;
$cal = Koha::Calendar->new( branchcode => 'MPL' );
is($cal->addDate( $dt, $one_day_dur, 'days' ),
dt_from_string('2012-07-05','iso'),
'Single day add (Datedue, matches holiday, shift)' );
is($cal->addDate( $dt, $two_day_dur, 'days' ),
dt_from_string('2012-07-05','iso'),
'Two days add, skips holiday (Datedue)' );
cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ), 'eq',
'2012-07-30T11:53:00',
'Add 7 days (Datedue)' );
is( $cal->addDate( $saturday, $one_day_dur, 'days' )->day_of_week, 1,
'addDate skips closed Sunday (Datedue)' );
is( $cal->addDate($day_after_christmas, -1, 'days')->ymd(), '2012-12-24',
'Negative call to addDate (Datedue)' );
## Note that the days_between API says closed days are not considered.
## This tests are here as an API test.
cmp_ok( $cal->days_between( $test_dt, $later_dt )->in_units('days'),
'==', 40, 'days_between calculates correctly (Days)' );
cmp_ok( $cal->days_between( $later_dt, $test_dt )->in_units('days'),
'==', 40, 'Test parameter order not relevant (Days)' );
}
{ ## 'Calendar' tests'
$module_context->unmock('preference');
$module_context->mock(
'preference',
sub {
return 'Calendar';
}
);
# rewind dbh session
$holidays_session->reset;
$cal = Koha::Calendar->new( branchcode => 'MPL' );
$dt = dt_from_string('2012-07-03','iso');
is($cal->addDate( $dt, $one_day_dur, 'days' ),
dt_from_string('2012-07-05','iso'),
'Single day add (Calendar)' );
cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ), 'eq',
'2012-08-01T11:53:00',
'Add 7 days (Calendar)' );
is( $cal->addDate( $saturday, $one_day_dur, 'days' )->day_of_week, 1,
'addDate skips closed Sunday (Calendar)' );
is( $cal->addDate($day_after_christmas, -1, 'days')->ymd(), '2012-12-24',
'Negative call to addDate (Calendar)' );
cmp_ok( $cal->days_between( $test_dt, $later_dt )->in_units('days'),
'==', 40, 'days_between calculates correctly (Calendar)' );
cmp_ok( $cal->days_between( $later_dt, $test_dt )->in_units('days'),
'==', 40, 'Test parameter order not relevant (Calendar)' );
}
{ ## 'Days' tests
$module_context->unmock('preference');
$module_context->mock(
'preference',
sub {
return 'Days';
}
);
# rewind dbh session
$holidays_session->reset;
$cal = Koha::Calendar->new( branchcode => 'MPL' );
$dt = dt_from_string('2012-07-03','iso');
is($cal->addDate( $dt, $one_day_dur, 'days' ),
dt_from_string('2012-07-04','iso'),
'Single day add (Days)' );
cmp_ok($cal->addDate( $test_dt, $seven_day_dur, 'days' ),'eq',
'2012-07-30T11:53:00',
'Add 7 days (Days)' );
is( $cal->addDate( $saturday, $one_day_dur, 'days' )->day_of_week, 7,
'addDate doesn\'t skip closed Sunday (Days)' );
is( $cal->addDate($day_after_christmas, -1, 'days')->ymd(), '2012-12-25',
'Negative call to addDate (Days)' );
## Note that the days_between API says closed days are not considered.
## This tests are here as an API test.
cmp_ok( $cal->days_between( $test_dt, $later_dt )->in_units('days'),
'==', 40, 'days_between calculates correctly (Days)' );
cmp_ok( $cal->days_between( $later_dt, $test_dt )->in_units('days'),
'==', 40, 'Test parameter order not relevant (Days)' );
}
} # End SKIP block