‪TYPO3CMS  10.4
DateViewHelperTest.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the TYPO3 CMS project.
5  *
6  * It is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU General Public License, either version 2
8  * of the License, or any later version.
9  *
10  * For the full copyright and license information, please read the
11  * LICENSE.txt file that was distributed with this source code.
12  *
13  * The TYPO3 project - inspiring people to share!
14  */
15 
17 
19 use TYPO3\TestingFramework\Fluid\Unit\ViewHelpers\ViewHelperBaseTestcase;
20 use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
21 
25 class ‪DateViewHelperTest extends ViewHelperBaseTestcase
26 {
30  protected ‪$subject;
31 
35  protected ‪$timezone;
36 
40  protected ‪$viewHelper;
41 
42  protected ‪$resetSingletonInstances = true;
43 
44  protected function ‪setUp(): void
45  {
46  parent::setUp();
47  $this->timezone = @date_default_timezone_get();
48  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] = 'Y-m-d';
49  $this->viewHelper = new ‪DateViewHelper();
50  $this->injectDependenciesIntoViewHelper($this->viewHelper);
51  }
52 
53  protected function ‪tearDown(): void
54  {
55  date_default_timezone_set($this->timezone);
56  parent::tearDown();
57  }
58 
62  public function ‪viewHelperFormatsDateCorrectly()
63  {
64  $this->setArgumentsUnderTest(
65  $this->viewHelper,
66  [
67  'date' => new \DateTime('1980-12-13')
68  ]
69  );
70  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
71  self::assertEquals('1980-12-13', $actualResult);
72  }
73 
78  {
79  $this->setArgumentsUnderTest(
80  $this->viewHelper,
81  [
82  'date' => '1980-12-13'
83  ]
84  );
85  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
86  self::assertEquals('1980-12-13', $actualResult);
87  }
88 
92  public function ‪viewHelperRespectsCustomFormat()
93  {
94  $this->setArgumentsUnderTest(
95  $this->viewHelper,
96  [
97  'date' => new \DateTime('1980-02-01'),
98  'format' => 'd.m.Y'
99  ]
100  );
101  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
102  self::assertEquals('01.02.1980', $actualResult);
103  }
104 
109  {
110  $this->setArgumentsUnderTest(
111  $this->viewHelper,
112  [
113  'date' => new \DateTimeImmutable('1980-02-01'),
114  'format' => 'd.m.Y'
115  ]
116  );
117  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
118  self::assertEquals('01.02.1980', $actualResult);
119  }
120 
125  {
126  $this->viewHelper->setRenderChildrenClosure(
127  function () {
128  return null;
129  }
130  );
131  $this->setArgumentsUnderTest(
132  $this->viewHelper,
133  [
134  ]
135  );
136  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
137  self::assertEquals('', $actualResult);
138  }
139 
144  {
145  $this->setArgumentsUnderTest(
146  $this->viewHelper,
147  [
148  'date' => ''
149  ]
150  );
151  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
152  $expectedResult = date('Y-m-d', ‪$GLOBALS['EXEC_TIME']);
153  self::assertEquals($expectedResult, $actualResult);
154  }
155 
160  {
161  $this->viewHelper->setRenderChildrenClosure(
162  function () {
163  return '';
164  }
165  );
166  $this->setArgumentsUnderTest(
167  $this->viewHelper,
168  [
169  ]
170  );
171  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
172  $expectedResult = date('Y-m-d', ‪$GLOBALS['EXEC_TIME']);
173  self::assertEquals($expectedResult, $actualResult);
174  }
175 
180  {
181  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] = '';
182  $this->setArgumentsUnderTest(
183  $this->viewHelper,
184  [
185  'date' => '@1391876733'
186  ]
187  );
188  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
189  self::assertEquals('2014-02-08', $actualResult);
190  }
191 
195  public function ‪viewHelperUsesSystemFormat()
196  {
197  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] = 'l, j. M y';
198  $this->setArgumentsUnderTest(
199  $this->viewHelper,
200  [
201  'date' => '@1391876733'
202  ]
203  );
204  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
205  self::assertEquals('Saturday, 8. Feb 14', $actualResult);
206  }
207 
212  {
213  $this->expectException(Exception::class);
214  $this->expectExceptionCode(1241722579);
215 
216  $this->setArgumentsUnderTest(
217  $this->viewHelper,
218  [
219  'date' => 'foo'
220  ]
221  );
222  $this->viewHelper->initializeArgumentsAndRender();
223  }
224 
229  {
230  $this->viewHelper->setRenderChildrenClosure(
231  function () {
232  return new \DateTime('1980-12-13');
233  }
234  );
235  $this->setArgumentsUnderTest(
236  $this->viewHelper,
237  [
238  ]
239  );
240  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
241  self::assertEquals('1980-12-13', $actualResult);
242  }
243 
248  {
249  $this->viewHelper->setRenderChildrenClosure(
250  function () {
251  return '1359891658' . LF;
252  }
253  );
254  $this->setArgumentsUnderTest(
255  $this->viewHelper,
256  [
257  ]
258  );
259  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
260  self::assertEquals('2013-02-03', $actualResult);
261  }
262 
267  {
268  $this->setArgumentsUnderTest(
269  $this->viewHelper,
270  [
271  'date' => '1980-12-12'
272  ]
273  );
274  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
275  self::assertEquals('1980-12-12', $actualResult);
276  }
277 
282  {
283  $this->setArgumentsUnderTest(
284  $this->viewHelper,
285  [
286  'date' => 'now',
287  'format' => 'Y',
288  ]
289  );
290  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
291  self::assertEquals(date('Y'), $actualResult);
292  }
293 
298  {
299  $this->setArgumentsUnderTest(
300  $this->viewHelper,
301  [
302  'date' => '-1 year',
303  'format' => 'Y',
304  'base' => '2017-01-01'
305  ]
306  );
307  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
308  self::assertEquals('2016', $actualResult);
309  }
310 
315  {
316  $this->setArgumentsUnderTest(
317  $this->viewHelper,
318  [
319  'date' => '-1 year',
320  'format' => 'Y',
321  'base' => new \DateTime('2017-01-01')
322  ]
323  );
324  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
325  self::assertEquals('2016', $actualResult);
326  }
327 
332  {
333  $this->setArgumentsUnderTest(
334  $this->viewHelper,
335  [
336  'date' => '@1435784732',
337  'format' => 'Y',
338  'base' => 1485907200
339  ]
340  );
341  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
342  self::assertEquals('2015', $actualResult);
343  }
344 
351  {
352  return [
353  'Europe/Berlin' => [
354  'Europe/Berlin',
355  '2013-02-03 12:40',
356  ],
357  'Asia/Riyadh' => [
358  'Asia/Riyadh',
359  '2013-02-03 14:40',
360  ],
361  ];
362  }
363 
369  {
370  $date = 1359891658; // 2013-02-03 11:40 UTC
371  $format = 'Y-m-d H:i';
372 
373  date_default_timezone_set(‪$timezone);
374  $this->setArgumentsUnderTest(
375  $this->viewHelper,
376  [
377  'date' => $date,
378  'format' => $format
379  ]
380  );
381  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
382  self::assertEquals($expected, $actualResult);
383  }
384 
391  {
392  return [
393  'Europe/Berlin UTC' => [
394  'Europe/Berlin',
395  '@1359891658',
396  '2013-02-03 12:40'
397  ],
398  'Europe/Berlin Moscow' => [
399  'Europe/Berlin',
400  '03/Oct/2000:14:55:36 +0400',
401  '2000-10-03 12:55'
402  ],
403  'Asia/Riyadh UTC' => [
404  'Asia/Riyadh',
405  '@1359891658',
406  '2013-02-03 14:40'
407  ],
408  'Asia/Riyadh Moscow' => [
409  'Asia/Riyadh',
410  '03/Oct/2000:14:55:36 +0400',
411  '2000-10-03 13:55'
412  ],
413  ];
414  }
415 
421  public function ‪viewHelperRespectsDefaultTimezoneForStringTimestamp($timeZone, $date, $expected)
422  {
423  $format = 'Y-m-d H:i';
424 
425  date_default_timezone_set($timeZone);
426  $this->setArgumentsUnderTest(
427  $this->viewHelper,
428  [
429  'date' => $date,
430  'format' => $format
431  ]
432  );
433  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
434  self::assertEquals($expected, $actualResult);
435  }
436 }
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest
Definition: DateViewHelperTest.php:26
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\viewHelperRespectsDefaultTimezoneForIntegerTimestamp
‪viewHelperRespectsDefaultTimezoneForIntegerTimestamp($timezone, $expected)
Definition: DateViewHelperTest.php:365
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\viewHelperReturnsCurrentDateIfChildrenIsEmptyString
‪viewHelperReturnsCurrentDateIfChildrenIsEmptyString()
Definition: DateViewHelperTest.php:156
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\viewHelperReturnsEmptyStringIfChildrenIsNULL
‪viewHelperReturnsEmptyStringIfChildrenIsNULL()
Definition: DateViewHelperTest.php:121
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\setUp
‪setUp()
Definition: DateViewHelperTest.php:41
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\tearDown
‪tearDown()
Definition: DateViewHelperTest.php:50
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\viewHelperUsesChildNodesWithTimestamp
‪viewHelperUsesChildNodesWithTimestamp()
Definition: DateViewHelperTest.php:244
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\$resetSingletonInstances
‪$resetSingletonInstances
Definition: DateViewHelperTest.php:39
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\viewHelperUsesDefaultIfNoSystemFormatIsAvailable
‪viewHelperUsesDefaultIfNoSystemFormatIsAvailable()
Definition: DateViewHelperTest.php:176
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\viewHelperRespectsDefaultTimezoneForIntegerTimestampDataProvider
‪array viewHelperRespectsDefaultTimezoneForIntegerTimestampDataProvider()
Definition: DateViewHelperTest.php:347
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\viewHelperThrowsExceptionWithOriginalMessageIfDateStringCantBeParsed
‪viewHelperThrowsExceptionWithOriginalMessageIfDateStringCantBeParsed()
Definition: DateViewHelperTest.php:208
‪TYPO3\CMS\Fluid\ViewHelpers\Format\DateViewHelper
Definition: DateViewHelper.php:103
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\viewHelperRespectsCustomFormat
‪viewHelperRespectsCustomFormat()
Definition: DateViewHelperTest.php:89
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format
Definition: ScriptViewHelperTest.php:18
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\viewHelperFormatsDateCorrectly
‪viewHelperFormatsDateCorrectly()
Definition: DateViewHelperTest.php:59
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\$subject
‪DateViewHelper PHPUnit Framework MockObject MockObject TYPO3 TestingFramework Core AccessibleObjectInterface $subject
Definition: DateViewHelperTest.php:29
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\viewHelperFormatsDateStringCorrectly
‪viewHelperFormatsDateStringCorrectly()
Definition: DateViewHelperTest.php:74
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\viewHelperReturnsCurrentDateIfEmptyStringIsGiven
‪viewHelperReturnsCurrentDateIfEmptyStringIsGiven()
Definition: DateViewHelperTest.php:140
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\viewHelperRespectsDefaultTimezoneForStringTimestampDataProvider
‪array viewHelperRespectsDefaultTimezoneForStringTimestampDataProvider()
Definition: DateViewHelperTest.php:387
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\viewHelperUsesSystemFormat
‪viewHelperUsesSystemFormat()
Definition: DateViewHelperTest.php:192
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\$viewHelper
‪DateViewHelper $viewHelper
Definition: DateViewHelperTest.php:37
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\dateArgumentHasPriorityOverChildNodes
‪dateArgumentHasPriorityOverChildNodes()
Definition: DateViewHelperTest.php:263
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:5
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\$timezone
‪string $timezone
Definition: DateViewHelperTest.php:33
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\baseArgumentDoesNotAffectAbsoluteTime
‪baseArgumentDoesNotAffectAbsoluteTime()
Definition: DateViewHelperTest.php:328
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\viewHelperRespectsDefaultTimezoneForStringTimestamp
‪viewHelperRespectsDefaultTimezoneForStringTimestamp($timeZone, $date, $expected)
Definition: DateViewHelperTest.php:418
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\baseArgumentAsDateTimeIsConsideredForRelativeDate
‪baseArgumentAsDateTimeIsConsideredForRelativeDate()
Definition: DateViewHelperTest.php:311
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\relativeDateCalculationWorksWithoutBase
‪relativeDateCalculationWorksWithoutBase()
Definition: DateViewHelperTest.php:278
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\baseArgumentIsConsideredForRelativeDate
‪baseArgumentIsConsideredForRelativeDate()
Definition: DateViewHelperTest.php:294
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\viewHelperSupportsDateTimeImmutable
‪viewHelperSupportsDateTimeImmutable()
Definition: DateViewHelperTest.php:105
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\viewHelperUsesChildNodesIfDateAttributeIsNotSpecified
‪viewHelperUsesChildNodesIfDateAttributeIsNotSpecified()
Definition: DateViewHelperTest.php:225