TYPO3 CMS  TYPO3_8-7
DateViewHelperTest.php
Go to the documentation of this file.
1 <?php
3 
4 /*
5  * This file is part of the TYPO3 CMS project.
6  *
7  * It is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU General Public License, either version 2
9  * of the License, or any later version.
10  *
11  * For the full copyright and license information, please read the
12  * LICENSE.txt file that was distributed with this source code.
13  *
14  * The TYPO3 project - inspiring people to share!
15  */
16 
20 
24 class DateViewHelperTest extends ViewHelperBaseTestcase
25 {
29  protected $subject;
30 
34  protected $timezone;
35 
39  protected $viewHelper;
40 
41  protected function setUp()
42  {
43  parent::setUp();
44  $this->timezone = @date_default_timezone_get();
45  $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] = 'Y-m-d';
46  $this->viewHelper = new DateViewHelper();
47  $this->injectDependenciesIntoViewHelper($this->viewHelper);
48  }
49 
50  protected function tearDown()
51  {
52  date_default_timezone_set($this->timezone);
53  parent::tearDown();
54  }
55 
60  {
61  $this->setArgumentsUnderTest(
62  $this->viewHelper,
63  [
64  'date' => new \DateTime('1980-12-13')
65  ]
66  );
67  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
68  $this->assertEquals('1980-12-13', $actualResult);
69  }
70 
75  {
76  $this->setArgumentsUnderTest(
77  $this->viewHelper,
78  [
79  'date' => '1980-12-13'
80  ]
81  );
82  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
83  $this->assertEquals('1980-12-13', $actualResult);
84  }
85 
90  {
91  $this->setArgumentsUnderTest(
92  $this->viewHelper,
93  [
94  'date' => new \DateTime('1980-02-01'),
95  'format' => 'd.m.Y'
96  ]
97  );
98  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
99  $this->assertEquals('01.02.1980', $actualResult);
100  }
101 
106  {
107  $this->setArgumentsUnderTest(
108  $this->viewHelper,
109  [
110  'date' => new \DateTimeImmutable('1980-02-01'),
111  'format' => 'd.m.Y'
112  ]
113  );
114  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
115  $this->assertEquals('01.02.1980', $actualResult);
116  }
117 
122  {
123  $this->viewHelper->setRenderChildrenClosure(
124  function () {
125  return null;
126  }
127  );
128  $this->setArgumentsUnderTest(
129  $this->viewHelper,
130  [
131  ]
132  );
133  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
134  $this->assertEquals('', $actualResult);
135  }
136 
141  {
142  $this->setArgumentsUnderTest(
143  $this->viewHelper,
144  [
145  'date' => ''
146  ]
147  );
148  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
149  $expectedResult = (new \DateTime())->format('Y-m-d');
150  $this->assertEquals($expectedResult, $actualResult);
151  }
152 
157  {
158  $this->viewHelper->setRenderChildrenClosure(
159  function () {
160  return '';
161  }
162  );
163  $this->setArgumentsUnderTest(
164  $this->viewHelper,
165  [
166  ]
167  );
168  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
169  $expectedResult = (new \DateTime())->format('Y-m-d');
170  $this->assertEquals($expectedResult, $actualResult);
171  }
172 
177  {
178  $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] = '';
179  $this->setArgumentsUnderTest(
180  $this->viewHelper,
181  [
182  'date' => '@1391876733'
183  ]
184  );
185  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
186  $this->assertEquals('2014-02-08', $actualResult);
187  }
188 
192  public function viewHelperUsesSystemFormat()
193  {
194  $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] = 'l, j. M y';
195  $this->setArgumentsUnderTest(
196  $this->viewHelper,
197  [
198  'date' => '@1391876733'
199  ]
200  );
201  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
202  $this->assertEquals('Saturday, 8. Feb 14', $actualResult);
203  }
204 
209  {
210  $this->expectException(Exception::class);
211  $this->expectExceptionCode(1241722579);
212 
213  $this->setArgumentsUnderTest(
214  $this->viewHelper,
215  [
216  'date' => 'foo'
217  ]
218  );
219  $this->viewHelper->initializeArgumentsAndRender();
220  }
221 
226  {
227  $this->viewHelper->setRenderChildrenClosure(
228  function () {
229  return new \DateTime('1980-12-13');
230  }
231  );
232  $this->setArgumentsUnderTest(
233  $this->viewHelper,
234  [
235  ]
236  );
237  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
238  $this->assertEquals('1980-12-13', $actualResult);
239  }
240 
245  {
246  $this->viewHelper->setRenderChildrenClosure(
247  function () {
248  return '1359891658' . LF;
249  }
250  );
251  $this->setArgumentsUnderTest(
252  $this->viewHelper,
253  [
254  ]
255  );
256  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
257  $this->assertEquals('2013-02-03', $actualResult);
258  }
259 
264  {
265  $this->setArgumentsUnderTest(
266  $this->viewHelper,
267  [
268  'date' => '1980-12-12'
269  ]
270  );
271  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
272  $this->assertEquals('1980-12-12', $actualResult);
273  }
274 
279  {
280  $this->setArgumentsUnderTest(
281  $this->viewHelper,
282  [
283  'date' => 'now',
284  'format' => 'Y',
285  ]
286  );
287  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
288  $this->assertEquals(date('Y'), $actualResult);
289  }
290 
295  {
296  $this->setArgumentsUnderTest(
297  $this->viewHelper,
298  [
299  'date' => '-1 year',
300  'format' => 'Y',
301  'base' => '2017-01-01'
302  ]
303  );
304  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
305  $this->assertEquals('2016', $actualResult);
306  }
307 
312  {
313  $this->setArgumentsUnderTest(
314  $this->viewHelper,
315  [
316  'date' => '-1 year',
317  'format' => 'Y',
318  'base' => new \DateTime('2017-01-01')
319  ]
320  );
321  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
322  $this->assertEquals('2016', $actualResult);
323  }
324 
329  {
330  $this->setArgumentsUnderTest(
331  $this->viewHelper,
332  [
333  'date' => '@1435784732',
334  'format' => 'Y',
335  'base' => 1485907200
336  ]
337  );
338  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
339  $this->assertEquals('2015', $actualResult);
340  }
341 
348  {
349  return [
350  'Europe/Berlin' => [
351  'Europe/Berlin',
352  '2013-02-03 12:40',
353  ],
354  'Asia/Riyadh' => [
355  'Asia/Riyadh',
356  '2013-02-03 14:40',
357  ],
358  ];
359  }
360 
366  {
367  $date = 1359891658; // 2013-02-03 11:40 UTC
368  $format = 'Y-m-d H:i';
369 
370  date_default_timezone_set($timezone);
371  $this->setArgumentsUnderTest(
372  $this->viewHelper,
373  [
374  'date' => $date,
375  'format' => $format
376  ]
377  );
378  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
379  $this->assertEquals($expected, $actualResult);
380  }
381 
388  {
389  return [
390  'Europe/Berlin UTC' => [
391  'Europe/Berlin',
392  '@1359891658',
393  '2013-02-03 12:40'
394  ],
395  'Europe/Berlin Moscow' => [
396  'Europe/Berlin',
397  '03/Oct/2000:14:55:36 +0400',
398  '2000-10-03 12:55'
399  ],
400  'Asia/Riyadh UTC' => [
401  'Asia/Riyadh',
402  '@1359891658',
403  '2013-02-03 14:40'
404  ],
405  'Asia/Riyadh Moscow' => [
406  'Asia/Riyadh',
407  '03/Oct/2000:14:55:36 +0400',
408  '2000-10-03 13:55'
409  ],
410  ];
411  }
412 
418  public function viewHelperRespectsDefaultTimezoneForStringTimestamp($timeZone, $date, $expected)
419  {
420  $format = 'Y-m-d H:i';
421 
422  date_default_timezone_set($timeZone);
423  $this->setArgumentsUnderTest(
424  $this->viewHelper,
425  [
426  'date' => $date,
427  'format' => $format
428  ]
429  );
430  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
431  $this->assertEquals($expected, $actualResult);
432  }
433 
440  {
441  return [
442  'de_DE.UTF-8' => [
443  'de_DE.UTF-8',
444  '03. Februar 2013'
445  ],
446  'en_ZW.utf8' => [
447  'en_ZW.utf8',
448  '03. February 2013'
449  ]
450  ];
451  }
452 
458  public function dateViewHelperFormatsDateLocalized($locale, $expected)
459  {
460  $format = '%d. %B %Y';
461  // 2013-02-03 11:40 UTC
462  $timestamp = '@1359891658';
463 
464  try {
465  $this->setLocale(LC_COLLATE, $locale);
466  $this->setLocale(LC_CTYPE, $locale);
467  $this->setLocale(LC_MONETARY, $locale);
468  $this->setLocale(LC_TIME, $locale);
469  } catch (\PHPUnit\Framework\Exception $e) {
470  $this->markTestSkipped('Locale ' . $locale . ' is not available.');
471  }
472  $this->setArgumentsUnderTest(
473  $this->viewHelper,
474  [
475  'date' => $timestamp,
476  'format' => $format
477  ]
478  );
479  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
480  $this->assertEquals($expected, $actualResult);
481  }
482 }
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']