1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Unit tests for Date API functions.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* Unit tests for Date API functions.
|
10
|
*/
|
11
|
class DateApiUnitTestCase extends DrupalUnitTestCase {
|
12
|
|
13
|
/**
|
14
|
* @todo.
|
15
|
*/
|
16
|
public static function getInfo() {
|
17
|
return array(
|
18
|
'name' => t('Date API unit tests'),
|
19
|
'description' => t('Unit test coverage for Date API functions.') ,
|
20
|
'group' => t('Date'),
|
21
|
);
|
22
|
}
|
23
|
|
24
|
/**
|
25
|
* {@inheritdoc}
|
26
|
*/
|
27
|
public function setUp() {
|
28
|
drupal_load('module', 'date_api');
|
29
|
parent::setUp();
|
30
|
}
|
31
|
|
32
|
/**
|
33
|
* Tests for date_is_all_day().
|
34
|
*/
|
35
|
public function testDateIsAllDay() {
|
36
|
// Two empty strings, this should always return FALSE.
|
37
|
$string1 = '';
|
38
|
$string2 = '';
|
39
|
$response = date_is_all_day($string1, $string2);
|
40
|
$this->assertFalse($response, 'Two empty strings cannot represent an all-day date pair.');
|
41
|
|
42
|
// Two random date strings do not make an all-day pair.
|
43
|
$string1 = '2021-02-25 01:01:01';
|
44
|
$string2 = '2021-02-25 23:00:00';
|
45
|
$response = date_is_all_day($string1, $string2);
|
46
|
$this->assertFalse($response, 'Two random date strings do not make an "all day" pair.');
|
47
|
|
48
|
// It is not a valid "all day" date pair to start one second after midnight
|
49
|
// and end on midnight the next day.
|
50
|
$string1 = '2021-02-25 00:00:01';
|
51
|
$string2 = '2021-02-26 00:00:00';
|
52
|
$response = date_is_all_day($string1, $string2);
|
53
|
$this->assertFalse($response, 'Does not mark dates as "all day" if the first is 1 second after midnight on day 1 and the other is midnight on day 2.');
|
54
|
|
55
|
// A valid "all day" date pair when the granularity is 1, regardless of the
|
56
|
// unit.
|
57
|
$string1 = '2021-02-25 00:00:00';
|
58
|
$string2 = '2021-02-25 23:59:59';
|
59
|
|
60
|
// The granularity option must be one of a limited set of strings.
|
61
|
$response = date_is_all_day($string1, $string2, 'mango');
|
62
|
$this->assertFalse($response, 'The granularity argument must be one of a limited set of strings, so "mango" will not work.');
|
63
|
|
64
|
// A valid "all day" date pair starts at midnight and ends one second before
|
65
|
// midnight, both on the same day.
|
66
|
$response = date_is_all_day($string1, $string2);
|
67
|
$this->assertTrue($response, 'An "all day" date pair must start at midnight and end one time unit before midnight of the next day.');
|
68
|
|
69
|
// Confirm the granularity option works with seconds.
|
70
|
$response = date_is_all_day($string1, $string2, 'second');
|
71
|
$this->assertTrue($response, 'End time is 23:59:59 and granularity is set to "second".');
|
72
|
|
73
|
// Confirm the granularity option works with minutes.
|
74
|
$response = date_is_all_day($string1, $string2, 'minute');
|
75
|
$this->assertTrue($response, 'End time is 23:59:59 and granularity is set to "minute".');
|
76
|
|
77
|
// Confirm the granularity option works with hours.
|
78
|
$response = date_is_all_day($string1, $string2, 'hour');
|
79
|
$this->assertTrue($response, 'End time is 23:59:59 and granularity is set to "hour".');
|
80
|
|
81
|
// Test the "increment" argument with a unit of 1.
|
82
|
$response = date_is_all_day($string1, $string2, 'second', 1);
|
83
|
$this->assertTrue($response, 'End time is 23:59:59 and granularity is set to 1 "second".');
|
84
|
$response = date_is_all_day($string1, $string2, 'minute', 1);
|
85
|
$this->assertTrue($response, 'End time is 23:59:59 and granularity is set to 1 "minute".');
|
86
|
$response = date_is_all_day($string1, $string2, 'hour', 1);
|
87
|
$this->assertTrue($response, 'End time is 23:59:59 and granularity is set to 1 "hour".');
|
88
|
|
89
|
// Test the "increment" argument with a unit of 15.
|
90
|
$response = date_is_all_day($string1, $string2, 'second', 15);
|
91
|
$this->assertTrue($response, 'End time is 23:59:59 and granularity is set to 15 "second".');
|
92
|
$response = date_is_all_day($string1, $string2, 'minute', 15);
|
93
|
$this->assertTrue($response, 'End time is 23:59:59 and granularity is set to 15 "minute".');
|
94
|
$response = date_is_all_day($string1, $string2, 'hour', 15);
|
95
|
$this->assertTrue($response, 'End time is 23:59:59 and granularity is set to 15 "hour".');
|
96
|
|
97
|
// A datetime pair ending at 23:59:00.
|
98
|
$string1 = '2021-02-25 00:00:00';
|
99
|
$string2 = '2021-02-25 23:59:00';
|
100
|
|
101
|
// This pair will only be valid when the granularity is "minute" or "hour".
|
102
|
$response = date_is_all_day($string1, $string2, 'second');
|
103
|
$this->assertFalse($response, 'Not valid with an end time of 23:59:00 and granularity is set to "second".');
|
104
|
$response = date_is_all_day($string1, $string2, 'minute');
|
105
|
$this->assertTrue($response, '23:59:00 is a valid end time if the granularity is set to "minute".');
|
106
|
$response = date_is_all_day($string1, $string2, 'minute');
|
107
|
$this->assertTrue($response, '23:59:00 is a valid end time if the granularity is set to "hour".');
|
108
|
|
109
|
// A datetime pair ending at 23:59:24. This is to confirm that segments of
|
110
|
// the time, after the granularity option, are ignored.
|
111
|
$string1 = '2021-02-25 00:00:00';
|
112
|
$string2 = '2021-02-25 23:59:24';
|
113
|
|
114
|
// This pair will only be valid when the granularity is "minute" or "hour".
|
115
|
$response = date_is_all_day($string1, $string2, 'second');
|
116
|
$this->assertFalse($response, 'Not valid with an end time of 23:59:24 and granularity is set to "second".');
|
117
|
$response = date_is_all_day($string1, $string2, 'minute');
|
118
|
$this->assertTrue($response, '23:59:24 is a valid end time if the granularity is set to "minute".');
|
119
|
$response = date_is_all_day($string1, $string2, 'minute');
|
120
|
$this->assertTrue($response, '23:59:24 is a valid end time if the granularity is set to "hour".');
|
121
|
|
122
|
// A datetime pair ending at 23:00:00.
|
123
|
$string1 = '2021-02-25 00:00:00';
|
124
|
$string2 = '2021-02-25 23:00:00';
|
125
|
|
126
|
// This pair will only be valid when the granularity is "minute" or "hour".
|
127
|
$response = date_is_all_day($string1, $string2, 'second');
|
128
|
$this->assertFalse($response, 'Not valid with an end time of 23:00:00 and granularity is set to "second".');
|
129
|
$response = date_is_all_day($string1, $string2, 'minute');
|
130
|
$this->assertFalse($response, 'Not valid with an end time of 23:00:00 and granularity is set to "minute".');
|
131
|
$response = date_is_all_day($string1, $string2, 'hour');
|
132
|
$this->assertTrue($response, '23:00:00 is a valid end time if the granularity is set to "hour".');
|
133
|
|
134
|
// A datetime pair ending at 23:13:00.
|
135
|
$string1 = '2021-02-25 00:00:00';
|
136
|
$string2 = '2021-02-25 23:13:00';
|
137
|
|
138
|
// This pair will only be valid when the granularity is "minute" or "hour".
|
139
|
$response = date_is_all_day($string1, $string2, 'second');
|
140
|
$this->assertFalse($response, 'Not valid with an end time of 23:13:00 and granularity is set to "second".');
|
141
|
$response = date_is_all_day($string1, $string2, 'minute');
|
142
|
$this->assertFalse($response, 'Not valid with an end time of 23:13:00 and granularity is set to "minute".');
|
143
|
$response = date_is_all_day($string1, $string2, 'hour');
|
144
|
$this->assertTrue($response, '23:00:00 is a valid end time if the granularity is set to "hour".');
|
145
|
|
146
|
// A datetime pair ending at 23:59:55.
|
147
|
$string1 = '2021-02-25 00:00:00';
|
148
|
$string2 = '2021-02-25 23:59:55';
|
149
|
|
150
|
// Test the "increment" argument with 5 seconds.
|
151
|
$response = date_is_all_day($string1, $string2, 'second');
|
152
|
$this->assertFalse($response, '23:59:55 is not a valid end time when the increment is set to 1 second.');
|
153
|
$response = date_is_all_day($string1, $string2, 'second', 5);
|
154
|
$this->assertTrue($response, '23:59:55 is a valid end time when the increment is set to 5 seconds.');
|
155
|
$response = date_is_all_day($string1, $string2, 'minute', 5);
|
156
|
$this->assertTrue($response, '23:59:55 is a valid end time when the increment is set to 5 minutes.');
|
157
|
$response = date_is_all_day($string1, $string2, 'hour', 5);
|
158
|
$this->assertTrue($response, '23:59:55 is a valid end time when the increment is set to 5 hours.');
|
159
|
|
160
|
// A datetime pair ending at 23:55:00.
|
161
|
$string1 = '2021-02-25 00:00:00';
|
162
|
$string2 = '2021-02-25 23:55:00';
|
163
|
|
164
|
// Test the "increment" argument with 5 seconds.
|
165
|
$response = date_is_all_day($string1, $string2, 'second');
|
166
|
$this->assertFalse($response, '23:55:00 is not a valid end time when the increment is set to 1 second.');
|
167
|
$response = date_is_all_day($string1, $string2, 'second', 5);
|
168
|
$this->assertFalse($response, '23:55:00 is not a valid end time when the increment is set to 5 seconds.');
|
169
|
$response = date_is_all_day($string1, $string2, 'minute');
|
170
|
$this->assertFalse($response, '23:55:00 is not a valid end time when the increment is set to 1 minute.');
|
171
|
$response = date_is_all_day($string1, $string2, 'minute', 5);
|
172
|
$this->assertTrue($response, '23:55:00 is a valid end time when the increment is set to 5 minutes.');
|
173
|
$response = date_is_all_day($string1, $string2, 'hour', 5);
|
174
|
$this->assertTrue($response, '23:55:00 is a valid end time when the increment is set to 5 hours.');
|
175
|
|
176
|
// A datetime pair ending at 23:59:50.
|
177
|
$string1 = '2021-02-25 00:00:00';
|
178
|
$string2 = '2021-02-25 23:59:50';
|
179
|
|
180
|
// Test the "increment" argument with 5 seconds.
|
181
|
$response = date_is_all_day($string1, $string2, 'second');
|
182
|
$this->assertFalse($response, '23:59:50 is not a valid end time when the increment is set to 1 second.');
|
183
|
$response = date_is_all_day($string1, $string2, 'second', 10);
|
184
|
$this->assertTrue($response, '23:59:50 is a valid end time when the increment is set to 10 seconds.');
|
185
|
$response = date_is_all_day($string1, $string2, 'minute', 10);
|
186
|
$this->assertTrue($response, '23:59:50 is a valid end time when the increment is set to 10 minutes.');
|
187
|
$response = date_is_all_day($string1, $string2, 'hour', 10);
|
188
|
$this->assertTrue($response, '23:59:50 is a valid end time when the increment is set to 10 hours.');
|
189
|
|
190
|
// A datetime pair ending at 23:50:00.
|
191
|
$string1 = '2021-02-25 00:00:00';
|
192
|
$string2 = '2021-02-25 23:50:00';
|
193
|
|
194
|
// Test the "increment" argument with 5 seconds.
|
195
|
$response = date_is_all_day($string1, $string2, 'second');
|
196
|
$this->assertFalse($response, '23:50:00 is not a valid end time when the increment is set to 1 second.');
|
197
|
$response = date_is_all_day($string1, $string2, 'second', 10);
|
198
|
$this->assertFalse($response, '23:50:00 is not a valid end time when the increment is set to 10 seconds.');
|
199
|
$response = date_is_all_day($string1, $string2, 'minute');
|
200
|
$this->assertFalse($response, '23:50:00 is not a valid end time when the increment is set to 1 minute.');
|
201
|
$response = date_is_all_day($string1, $string2, 'minute', 10);
|
202
|
$this->assertTrue($response, '23:50:00 is a valid end time when the increment is set to 10 minutes.');
|
203
|
$response = date_is_all_day($string1, $string2, 'hour', 10);
|
204
|
$this->assertTrue($response, '23:50:00 is a valid end time when the increment is set to 10 hours.');
|
205
|
|
206
|
// A datetime pair ending at 23:59:45.
|
207
|
$string1 = '2021-02-25 00:00:00';
|
208
|
$string2 = '2021-02-25 23:59:45';
|
209
|
|
210
|
// Test the "increment" argument with 5 seconds.
|
211
|
$response = date_is_all_day($string1, $string2, 'second');
|
212
|
$this->assertFalse($response, '23:59:45 is not a valid end time when the increment is set to 1 second.');
|
213
|
$response = date_is_all_day($string1, $string2, 'second', 15);
|
214
|
$this->assertTrue($response, '23:59:45 is a valid end time when the increment is set to 15 seconds.');
|
215
|
$response = date_is_all_day($string1, $string2, 'minute', 15);
|
216
|
$this->assertTrue($response, '23:59:45 is a valid end time when the increment is set to 15 minutes.');
|
217
|
$response = date_is_all_day($string1, $string2, 'hour', 15);
|
218
|
$this->assertTrue($response, '23:59:45 is a valid end time when the increment is set to 15 hours.');
|
219
|
|
220
|
// A datetime pair ending at 23:45:00.
|
221
|
$string1 = '2021-02-25 00:00:00';
|
222
|
$string2 = '2021-02-25 23:45:00';
|
223
|
|
224
|
// Test the "increment" argument with 5 seconds.
|
225
|
$response = date_is_all_day($string1, $string2, 'second');
|
226
|
$this->assertFalse($response, '23:45:00 is not a valid end time when the increment is set to 1 second.');
|
227
|
$response = date_is_all_day($string1, $string2, 'second', 15);
|
228
|
$this->assertFalse($response, '23:45:00 is not a valid end time when the increment is set to 15 seconds.');
|
229
|
$response = date_is_all_day($string1, $string2, 'minute');
|
230
|
$this->assertFalse($response, '23:45:00 is not a valid end time when the increment is set to 1 minute.');
|
231
|
$response = date_is_all_day($string1, $string2, 'minute', 15);
|
232
|
$this->assertTrue($response, '23:45:00 is a valid end time when the increment is set to 15 minutes.');
|
233
|
$response = date_is_all_day($string1, $string2, 'hour', 15);
|
234
|
$this->assertTrue($response, '23:45:00 is a valid end time when the increment is set to 15 hours.');
|
235
|
|
236
|
// A datetime pair ending at 23:59:30.
|
237
|
$string1 = '2021-02-25 00:00:00';
|
238
|
$string2 = '2021-02-25 23:59:30';
|
239
|
|
240
|
// Test the "increment" argument with 5 seconds.
|
241
|
$response = date_is_all_day($string1, $string2, 'second');
|
242
|
$this->assertFalse($response, '23:59:30 is not a valid end time when the increment is set to 1 second.');
|
243
|
$response = date_is_all_day($string1, $string2, 'second', 30);
|
244
|
$this->assertTrue($response, '23:59:30 is a valid end time when the increment is set to 30 seconds.');
|
245
|
$response = date_is_all_day($string1, $string2, 'minute', 30);
|
246
|
$this->assertTrue($response, '23:59:30 is a valid end time when the increment is set to 30 minutes.');
|
247
|
$response = date_is_all_day($string1, $string2, 'hour', 30);
|
248
|
$this->assertTrue($response, '23:59:30 is a valid end time when the increment is set to 30 hours.');
|
249
|
|
250
|
// A datetime pair ending at 23:30:00.
|
251
|
$string1 = '2021-02-25 00:00:00';
|
252
|
$string2 = '2021-02-25 23:30:00';
|
253
|
|
254
|
// Test the "increment" argument with 5 seconds.
|
255
|
$response = date_is_all_day($string1, $string2, 'second');
|
256
|
$this->assertFalse($response, '23:30:00 is not a valid end time when the increment is set to 1 second.');
|
257
|
$response = date_is_all_day($string1, $string2, 'second', 30);
|
258
|
$this->assertFalse($response, '23:30:00 is not a valid end time when the increment is set to 30 seconds.');
|
259
|
$response = date_is_all_day($string1, $string2, 'minute');
|
260
|
$this->assertFalse($response, '23:30:00 is not a valid end time when the increment is set to 1 minute.');
|
261
|
$response = date_is_all_day($string1, $string2, 'minute', 30);
|
262
|
$this->assertTrue($response, '23:30:00 is a valid end time when the increment is set to 30 minutes.');
|
263
|
$response = date_is_all_day($string1, $string2, 'hour', 30);
|
264
|
$this->assertTrue($response, '23:30:00 is a valid end time when the increment is set to 30 hours.');
|
265
|
}
|
266
|
|
267
|
}
|