[TASK] Rebuild the calcAge functionality
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Tests / Unit / Utility / DateTimeUtilityTest.php
1 <?php
2 namespace TYPO3\CMS\Core\Tests\Unit\Utility;
3
4 /***************************************************************
5 * Copyright notice
6 *
7 * (c) 2013 Alexander Opitz <opitz.alexander@googlemail.com>
8 * All rights reserved
9 *
10 * This script is part of the TYPO3 project. The TYPO3 project is
11 * free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * The GNU General Public License can be found at
17 * http://www.gnu.org/copyleft/gpl.html.
18 *
19 * This script is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * This copyright notice MUST APPEAR in all copies of the script!
25 ***************************************************************/
26
27 use TYPO3\CMS\Core\Tests\Unit\Utility\Fixtures\DateTimeUtilityFixture;
28 use TYPO3\CMS\Core\Utility\DateTimeUtility;
29
30 /**
31 * Test case
32 *
33 * @author Alexander Opitz <opitz@pluspol.info>
34 */
35 class DateTimeUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
36
37 /**
38 * Test the converting the string output of microtime into an float/double
39 * @test
40 */
41 public function convertMicrotimeProperlyConvertsAMicrotime() {
42 $this->assertSame(
43 1381567522451,
44 DateTimeUtility::convertMicrotime('0.45148500 1381567522')
45 );
46 }
47
48 /**
49 * Test the converting the string output of microtime into an float/double
50 * @test
51 */
52 public function getTimeDiffStringUnixCreatesCorrectStringResult() {
53 DateTimeUtilityFixture::$mockGetTimeDiffString = '1 foo';
54
55 $this->assertSame(
56 '1 foo',
57 DateTimeUtilityFixture::getTimeDiffStringUnix(500, time(), 'foo|bar')
58 );
59 DateTimeUtilityFixture::$mockGetTimeDiffString = '';
60 }
61
62 ///////////////////////////////////////
63 // Tests concerning getTimeDiffString
64 ///////////////////////////////////////
65 /**
66 * Data provider for getTimeDiffString function
67 *
68 * @return array
69 */
70 public function getTimeDiffStringProvider() {
71 $baseTime = new \DateTime();
72 return array(
73 'Single year' => array(
74 'time1' => new \DateTime('-1year'),
75 'time2' => $baseTime,
76 'expectedLabel' => '1 testYear'
77 ),
78 'Plural years' => array(
79 'time1' => new \DateTime('-2years'),
80 'time2' => $baseTime,
81 'expectedLabel' => '2 testYears'
82 ),
83 'Single negative year' => array(
84 'time1' => new \DateTime('+1year'),
85 'time2' => $baseTime,
86 'expectedLabel' => '-1 testYear'
87 ),
88 'Plural negative years' => array(
89 'time1' => new \DateTime('+2years'),
90 'time2' => $baseTime,
91 'expectedLabel' => '-2 testYears'
92 ),
93 'Single month' => array(
94 'time1' => new \DateTime('-1month'),
95 'time2' => $baseTime,
96 'expectedLabel' => '1 testMonth'
97 ),
98 'Plural months' => array(
99 'time1' => new \DateTime('-2months'),
100 'time2' => $baseTime,
101 'expectedLabel' => '2 testMonths'
102 ),
103 'Single negative month' => array(
104 'time1' => new \DateTime('+1month'),
105 'time2' => $baseTime,
106 'expectedLabel' => '-1 testMonth'
107 ),
108 'Plural negative months' => array(
109 'time1' => new \DateTime('+2months'),
110 'time2' => $baseTime,
111 'expectedLabel' => '-2 testMonths'
112 ),
113 'Single day' => array(
114 'time1' => new \DateTime('-1day'),
115 'time2' => $baseTime,
116 'expectedLabel' => '1 testDay'
117 ),
118 'Plural days' => array(
119 'time1' => new \DateTime('-2days'),
120 'time2' => $baseTime,
121 'expectedLabel' => '2 testDays'
122 ),
123 'Single negative day' => array(
124 'time1' => new \DateTime('+1day'),
125 'time2' => $baseTime,
126 'expectedLabel' => '-1 testDay'
127 ),
128 'Plural negative days' => array(
129 'time1' => new \DateTime('+2days'),
130 'time2' => $baseTime,
131 'expectedLabel' => '-2 testDays'
132 ),
133 'Single hour' => array(
134 'time1' => new \DateTime('-1hour'),
135 'time2' => $baseTime,
136 'expectedLabel' => '1 testHour'
137 ),
138 'Plural hours' => array(
139 'time1' => new \DateTime('-2hours'),
140 'time2' => $baseTime,
141 'expectedLabel' => '2 testHours'
142 ),
143 'Single negative hour' => array(
144 'time1' => new \DateTime('+1hour'),
145 'time2' => $baseTime,
146 'expectedLabel' => '-1 testHour'
147 ),
148 'Plural negative hours' => array(
149 'time1' => new \DateTime('+2hours'),
150 'time2' => $baseTime,
151 'expectedLabel' => '-2 testHours'
152 ),
153 'Single minute' => array(
154 'time1' => new \DateTime('-1min'),
155 'time2' => $baseTime,
156 'expectedLabel' => '1 testMinute'
157 ),
158 'Plural minutes' => array(
159 'time1' => new \DateTime('-2min'),
160 'time2' => $baseTime,
161 'expectedLabel' => '2 testMinutes'
162 ),
163 'Single negative minute' => array(
164 'time1' => new \DateTime('+1min'),
165 'time2' => $baseTime,
166 'expectedLabel' => '-1 testMinute'
167 ),
168 'Plural negative minutes' => array(
169 'time1' => new \DateTime('+2min'),
170 'time2' => $baseTime,
171 'expectedLabel' => '-2 testMinutes'
172 ),
173 'Zero seconds' => array(
174 'time1' => new \DateTime(),
175 'time2' => $baseTime,
176 'expectedLabel' => '0 testMinutes'
177 )
178 );
179 }
180
181 /**
182 * Test the converting the string output of microtime into an float/double
183 * @test
184 * @dataProvider getTimeDiffStringProvider
185 */
186 public function getTimeDiffStringCreatesCorrectStringResult($time1, $time2, $result) {
187 $this->assertSame(
188 $result,
189 DateTimeUtility::getTimeDiffString(
190 $time1,
191 $time2,
192 array(
193 'min' => ' testMinute',
194 'mins' => ' testMinutes',
195 'hour' => ' testHour',
196 'hours' => ' testHours',
197 'day' => ' testDay',
198 'days' => ' testDays',
199 'month' => ' testMonth',
200 'months' => ' testMonths',
201 'year' => ' testYear',
202 'years' => ' testYears',
203 )
204 )
205 );
206 }
207
208 /**
209 * Data provider for getTimeDiffString function
210 * For the backward compatible version without month
211 *
212 * @return array
213 */
214 public function getTimeDiffStringWithoutMonthProvider() {
215 $baseTime = new \DateTime();
216 return array(
217 'Single year' => array(
218 'time1' => new \DateTime('-1year'),
219 'time2' => $baseTime,
220 'expectedLabel' => '1 testYear'
221 ),
222 'Single negative year' => array(
223 'time1' => new \DateTime('+1year'),
224 'time2' => $baseTime,
225 'expectedLabel' => '-1 testYear'
226 ),
227 'Single month' => array(
228 'time1' => new \DateTime('-30days'),
229 'time2' => $baseTime,
230 'expectedLabel' => '30 testDays'
231 ),
232 'Plural months' => array(
233 'time1' => new \DateTime('-90days'),
234 'time2' => $baseTime,
235 'expectedLabel' => '90 testDays'
236 ),
237 'Single negative month' => array(
238 'time1' => new \DateTime('+30days'),
239 'time2' => $baseTime,
240 'expectedLabel' => '-30 testDays'
241 ),
242 'Plural negative months' => array(
243 'time1' => new \DateTime('+90days'),
244 'time2' => $baseTime,
245 'expectedLabel' => '-90 testDays'
246 ),
247 );
248 }
249
250 /**
251 * Test the converting the string output of microtime into an float/double
252 * @test
253 * @dataProvider getTimeDiffStringWithoutMonthProvider
254 */
255 public function getTimeDiffStringWithoutMonthCreatesCorrectStringResult($time1, $time2, $result) {
256 $this->assertSame(
257 $result,
258 DateTimeUtility::getTimeDiffString(
259 $time1,
260 $time2,
261 array(
262 'min' => ' testMinute',
263 'mins' => ' testMinutes',
264 'hour' => ' testHour',
265 'hours' => ' testHours',
266 'day' => ' testDay',
267 'days' => ' testDays',
268 'year' => ' testYear',
269 'years' => ' testYears',
270 )
271 )
272 );
273 }
274
275 ///////////////////////////////////////
276 // Tests concerning splitTimeUnitsFromLabel
277 ///////////////////////////////////////
278 /**
279 * Test the converting of the pipe split time units into an array
280 * @test
281 */
282 public function splitTimeUnitsFromLabelOldPlural() {
283 $this->assertSame(
284 array(
285 'min' => ' mins',
286 'mins' => ' mins',
287 'hour' => ' hrs',
288 'hours' => ' hrs',
289 'day' => ' days',
290 'days' => ' days',
291 'year' => ' yrs',
292 'years' => ' yrs',
293 ),
294 DateTimeUtility::splitTimeUnitsFromLabel('" mins| hrs| days| yrs"')
295 );
296 }
297
298 /**
299 * Test the converting of the pipe split time units into an array
300 * @test
301 */
302 public function splitTimeUnitsFromLabelOldSingularPlural() {
303 $this->assertSame(
304 array(
305 'min' => ' min',
306 'mins' => ' min',
307 'hour' => ' hour',
308 'hours' => ' hrs',
309 'day' => ' day',
310 'days' => ' days',
311 'year' => ' year',
312 'years' => ' yrs',
313 ),
314 DateTimeUtility::splitTimeUnitsFromLabel(' min| hrs|" days"| yrs| min| hour| day| year')
315 );
316 }
317
318 /**
319 * Test the converting of the pipe split time units into an array
320 * @test
321 */
322 public function splitTimeUnitsFromLabelNewSingularPlural() {
323 $this->assertSame(
324 array(
325 'min' => ' min',
326 'mins' => ' mins',
327 'hour' => ' hour',
328 'hours' => ' hours',
329 'day' => ' day',
330 'days' => ' days',
331 'month' => ' month',
332 'months' => ' months',
333 'year' => ' year',
334 'years' => ' years',
335 ),
336 DateTimeUtility::splitTimeUnitsFromLabel(' mins| hours| days| months| years| min| hour| day| month| year')
337 );
338 }
339
340 /**
341 * Test the converting of the pipe split time units into an array
342 * @test
343 */
344 public function splitTimeUnitsFromLabelDefaultLabels() {
345 $this->assertSame(
346 array(
347 'min' => ' min',
348 'mins' => ' min',
349 'hour' => ' hour',
350 'hours' => ' hrs',
351 'day' => ' day',
352 'days' => ' days',
353 'month' => ' month',
354 'months' => ' months',
355 'year' => ' year',
356 'years' => ' yrs',
357 ),
358 DateTimeUtility::splitTimeUnitsFromLabel()
359 );
360 }
361
362 /**
363 * Data provider for getSimpleAgeStringReturnsExpectedValues
364 * Adding one day to the years to not collide with leap years. (366)
365 *
366 * @return array
367 */
368 public function getSimpleAgeStringReturnsExpectedValuesDataProvider() {
369 return array(
370 'Single year' => array(
371 'seconds' => 60 * 60 * 24 * 366,
372 'expectedLabel' => '1 year'
373 ),
374 'Plural years' => array(
375 'seconds' => 60 * 60 * 24 * 366 * 2,
376 'expectedLabel' => '2 yrs'
377 ),
378 'Single negative year' => array(
379 'seconds' => 60 * 60 * 24 * 366 * -1,
380 'expectedLabel' => '-1 year'
381 ),
382 'Plural negative years' => array(
383 'seconds' => 60 * 60 * 24 * 366 * 2 * -1,
384 'expectedLabel' => '-2 yrs'
385 ),
386 'Single month' => array(
387 'seconds' => 60 * 60 * 24 * 31,
388 'expectedLabel' => '1 month'
389 ),
390 'Plural months' => array(
391 'seconds' => 60 * 60 * 24 * 31 * 2,
392 'expectedLabel' => '2 months'
393 ),
394 'Single negative month' => array(
395 'seconds' => 60 * 60 * 24 * 31 * -1,
396 'expectedLabel' => '-1 month'
397 ),
398 'Plural negative months' => array(
399 'seconds' => 60 * 60 * 24 * 31 * 2 * -1,
400 'expectedLabel' => '-2 months'
401 ),
402 'Single day' => array(
403 'seconds' => 60 * 60 * 24,
404 'expectedLabel' => '1 day'
405 ),
406 'Plural days' => array(
407 'seconds' => 60 * 60 * 24 * 2,
408 'expectedLabel' => '2 days'
409 ),
410 'Single negative day' => array(
411 'seconds' => 60 * 60 * 24 * -1,
412 'expectedLabel' => '-1 day'
413 ),
414 'Plural negative days' => array(
415 'seconds' => 60 * 60 * 24 * 2 * -1,
416 'expectedLabel' => '-2 days'
417 ),
418 'Single hour' => array(
419 'seconds' => 60 * 60,
420 'expectedLabel' => '1 hour'
421 ),
422 'Plural hours' => array(
423 'seconds' => 60 * 60 * 2,
424 'expectedLabel' => '2 hrs'
425 ),
426 'Single negative hour' => array(
427 'seconds' => 60 * 60 * -1,
428 'expectedLabel' => '-1 hour'
429 ),
430 'Plural negative hours' => array(
431 'seconds' => 60 * 60 * 2 * -1,
432 'expectedLabel' => '-2 hrs'
433 ),
434 'Single minute' => array(
435 'seconds' => 60,
436 'expectedLabel' => '1 min'
437 ),
438 'Plural minutes' => array(
439 'seconds' => 60 * 2,
440 'expectedLabel' => '2 min'
441 ),
442 'Single negative minute' => array(
443 'seconds' => 60 * -1,
444 'expectedLabel' => '-1 min'
445 ),
446 'Plural negative minutes' => array(
447 'seconds' => 60 * 2 * -1,
448 'expectedLabel' => '-2 min'
449 ),
450 'Zero seconds' => array(
451 'seconds' => 0,
452 'expectedLabel' => '0 min'
453 )
454 );
455 }
456
457 /**
458 * @test
459 * @dataProvider getSimpleAgeStringReturnsExpectedValuesDataProvider
460 */
461 public function getSimpleAgeStringReturnsExpectedValues($seconds, $expectedLabel) {
462 $this->assertSame($expectedLabel, DateTimeUtility::getSimpleAgeString($seconds));
463 }
464
465 /**
466 * Data provider for getSimpleAgeStringReturnsExpectedValues
467 * Adding one day to the years to not collide with leap years. (366)
468 * For the backward compatible version without month
469 *
470 * @return array
471 */
472 public function getSimpleAgeStringWithoutMonthReturnsExpectedValuesDataProvider() {
473 return array(
474 'Single year' => array(
475 'seconds' => 60 * 60 * 24 * 366,
476 'expectedLabel' => '1 testYear'
477 ),
478 'Single negative year' => array(
479 'seconds' => 60 * 60 * 24 * 366 * -1,
480 'expectedLabel' => '-1 testYear'
481 ),
482 'Single month' => array(
483 'seconds' => 60 * 60 * 24 * 31,
484 'expectedLabel' => '31 testDays'
485 ),
486 'Plural months' => array(
487 'seconds' => 60 * 60 * 24 * 31 * 2,
488 'expectedLabel' => '62 testDays'
489 ),
490 'Single negative month' => array(
491 'seconds' => 60 * 60 * 24 * 31 * -1,
492 'expectedLabel' => '-31 testDays'
493 ),
494 'Plural negative months' => array(
495 'seconds' => 60 * 60 * 24 * 31 * 2 * -1,
496 'expectedLabel' => '-62 testDays'
497 ),
498 );
499 }
500
501 /**
502 * @test
503 * @dataProvider getSimpleAgeStringWithoutMonthReturnsExpectedValuesDataProvider
504 */
505 public function getSimpleAgeStringWithoutMonthReturnsExpectedValues($seconds, $expectedLabel) {
506 $this->assertSame(
507 $expectedLabel,
508 DateTimeUtility::getSimpleAgeString(
509 $seconds,
510 array(
511 'min' => ' testMinute',
512 'mins' => ' testMinutes',
513 'hour' => ' testHour',
514 'hours' => ' testHours',
515 'day' => ' testDay',
516 'days' => ' testDays',
517 'year' => ' testYear',
518 'years' => ' testYears',
519 )
520 )
521 );
522 }
523 }