‪TYPO3CMS  9.5
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 
18 use TYPO3\TestingFramework\Fluid\Unit\ViewHelpers\ViewHelperBaseTestcase;
19 use TYPO3Fluid\Fluid\Core\ViewHelper\Exception;
20 
24 class ‪DateViewHelperTest extends ViewHelperBaseTestcase
25 {
29  protected ‪$subject;
30 
34  protected ‪$timezone;
35 
39  protected ‪$viewHelper;
40 
41  protected ‪$resetSingletonInstances = true;
42 
43  protected function ‪setUp()
44  {
45  parent::setUp();
46  $this->timezone = @date_default_timezone_get();
47  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] = 'Y-m-d';
48  $this->viewHelper = new ‪DateViewHelper();
49  $this->injectDependenciesIntoViewHelper($this->viewHelper);
50  }
51 
52  protected function ‪tearDown()
53  {
54  date_default_timezone_set($this->timezone);
55  parent::tearDown();
56  }
57 
61  public function ‪viewHelperFormatsDateCorrectly()
62  {
63  $this->setArgumentsUnderTest(
64  $this->viewHelper,
65  [
66  'date' => new \DateTime('1980-12-13')
67  ]
68  );
69  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
70  $this->assertEquals('1980-12-13', $actualResult);
71  }
72 
77  {
78  $this->setArgumentsUnderTest(
79  $this->viewHelper,
80  [
81  'date' => '1980-12-13'
82  ]
83  );
84  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
85  $this->assertEquals('1980-12-13', $actualResult);
86  }
87 
91  public function ‪viewHelperRespectsCustomFormat()
92  {
93  $this->setArgumentsUnderTest(
94  $this->viewHelper,
95  [
96  'date' => new \DateTime('1980-02-01'),
97  'format' => 'd.m.Y'
98  ]
99  );
100  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
101  $this->assertEquals('01.02.1980', $actualResult);
102  }
103 
108  {
109  $this->setArgumentsUnderTest(
110  $this->viewHelper,
111  [
112  'date' => new \DateTimeImmutable('1980-02-01'),
113  'format' => 'd.m.Y'
114  ]
115  );
116  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
117  $this->assertEquals('01.02.1980', $actualResult);
118  }
119 
124  {
125  $this->viewHelper->setRenderChildrenClosure(
126  function () {
127  return null;
128  }
129  );
130  $this->setArgumentsUnderTest(
131  $this->viewHelper,
132  [
133  ]
134  );
135  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
136  $this->assertEquals('', $actualResult);
137  }
138 
143  {
144  $this->setArgumentsUnderTest(
145  $this->viewHelper,
146  [
147  'date' => ''
148  ]
149  );
150  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
151  $expectedResult = date('Y-m-d', ‪$GLOBALS['EXEC_TIME']);
152  self::assertEquals($expectedResult, $actualResult);
153  }
154 
159  {
160  $this->viewHelper->setRenderChildrenClosure(
161  function () {
162  return '';
163  }
164  );
165  $this->setArgumentsUnderTest(
166  $this->viewHelper,
167  [
168  ]
169  );
170  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
171  $expectedResult = date('Y-m-d', ‪$GLOBALS['EXEC_TIME']);
172  self::assertEquals($expectedResult, $actualResult);
173  }
174 
179  {
180  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] = '';
181  $this->setArgumentsUnderTest(
182  $this->viewHelper,
183  [
184  'date' => '@1391876733'
185  ]
186  );
187  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
188  $this->assertEquals('2014-02-08', $actualResult);
189  }
190 
194  public function ‪viewHelperUsesSystemFormat()
195  {
196  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] = 'l, j. M y';
197  $this->setArgumentsUnderTest(
198  $this->viewHelper,
199  [
200  'date' => '@1391876733'
201  ]
202  );
203  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
204  $this->assertEquals('Saturday, 8. Feb 14', $actualResult);
205  }
206 
211  {
212  $this->expectException(Exception::class);
213  $this->expectExceptionCode(1241722579);
214 
215  $this->setArgumentsUnderTest(
216  $this->viewHelper,
217  [
218  'date' => 'foo'
219  ]
220  );
221  $this->viewHelper->initializeArgumentsAndRender();
222  }
223 
228  {
229  $this->viewHelper->setRenderChildrenClosure(
230  function () {
231  return new \DateTime('1980-12-13');
232  }
233  );
234  $this->setArgumentsUnderTest(
235  $this->viewHelper,
236  [
237  ]
238  );
239  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
240  $this->assertEquals('1980-12-13', $actualResult);
241  }
242 
247  {
248  $this->viewHelper->setRenderChildrenClosure(
249  function () {
250  return '1359891658' . LF;
251  }
252  );
253  $this->setArgumentsUnderTest(
254  $this->viewHelper,
255  [
256  ]
257  );
258  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
259  $this->assertEquals('2013-02-03', $actualResult);
260  }
261 
266  {
267  $this->setArgumentsUnderTest(
268  $this->viewHelper,
269  [
270  'date' => '1980-12-12'
271  ]
272  );
273  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
274  $this->assertEquals('1980-12-12', $actualResult);
275  }
276 
281  {
282  $this->setArgumentsUnderTest(
283  $this->viewHelper,
284  [
285  'date' => 'now',
286  'format' => 'Y',
287  ]
288  );
289  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
290  $this->assertEquals(date('Y'), $actualResult);
291  }
292 
297  {
298  $this->setArgumentsUnderTest(
299  $this->viewHelper,
300  [
301  'date' => '-1 year',
302  'format' => 'Y',
303  'base' => '2017-01-01'
304  ]
305  );
306  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
307  $this->assertEquals('2016', $actualResult);
308  }
309 
314  {
315  $this->setArgumentsUnderTest(
316  $this->viewHelper,
317  [
318  'date' => '-1 year',
319  'format' => 'Y',
320  'base' => new \DateTime('2017-01-01')
321  ]
322  );
323  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
324  $this->assertEquals('2016', $actualResult);
325  }
326 
331  {
332  $this->setArgumentsUnderTest(
333  $this->viewHelper,
334  [
335  'date' => '@1435784732',
336  'format' => 'Y',
337  'base' => 1485907200
338  ]
339  );
340  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
341  $this->assertEquals('2015', $actualResult);
342  }
343 
350  {
351  return [
352  'Europe/Berlin' => [
353  'Europe/Berlin',
354  '2013-02-03 12:40',
355  ],
356  'Asia/Riyadh' => [
357  'Asia/Riyadh',
358  '2013-02-03 14:40',
359  ],
360  ];
361  }
362 
368  {
369  $date = 1359891658; // 2013-02-03 11:40 UTC
370  $format = 'Y-m-d H:i';
371 
372  date_default_timezone_set(‪$timezone);
373  $this->setArgumentsUnderTest(
374  $this->viewHelper,
375  [
376  'date' => $date,
377  'format' => $format
378  ]
379  );
380  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
381  $this->assertEquals($expected, $actualResult);
382  }
383 
390  {
391  return [
392  'Europe/Berlin UTC' => [
393  'Europe/Berlin',
394  '@1359891658',
395  '2013-02-03 12:40'
396  ],
397  'Europe/Berlin Moscow' => [
398  'Europe/Berlin',
399  '03/Oct/2000:14:55:36 +0400',
400  '2000-10-03 12:55'
401  ],
402  'Asia/Riyadh UTC' => [
403  'Asia/Riyadh',
404  '@1359891658',
405  '2013-02-03 14:40'
406  ],
407  'Asia/Riyadh Moscow' => [
408  'Asia/Riyadh',
409  '03/Oct/2000:14:55:36 +0400',
410  '2000-10-03 13:55'
411  ],
412  ];
413  }
414 
420  public function ‪viewHelperRespectsDefaultTimezoneForStringTimestamp($timeZone, $date, $expected)
421  {
422  $format = 'Y-m-d H:i';
423 
424  date_default_timezone_set($timeZone);
425  $this->setArgumentsUnderTest(
426  $this->viewHelper,
427  [
428  'date' => $date,
429  'format' => $format
430  ]
431  );
432  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
433  $this->assertEquals($expected, $actualResult);
434  }
435 }
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest
Definition: DateViewHelperTest.php:25
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\viewHelperRespectsDefaultTimezoneForIntegerTimestamp
‪viewHelperRespectsDefaultTimezoneForIntegerTimestamp($timezone, $expected)
Definition: DateViewHelperTest.php:364
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\viewHelperReturnsCurrentDateIfChildrenIsEmptyString
‪viewHelperReturnsCurrentDateIfChildrenIsEmptyString()
Definition: DateViewHelperTest.php:155
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\viewHelperReturnsEmptyStringIfChildrenIsNULL
‪viewHelperReturnsEmptyStringIfChildrenIsNULL()
Definition: DateViewHelperTest.php:120
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\setUp
‪setUp()
Definition: DateViewHelperTest.php:40
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\tearDown
‪tearDown()
Definition: DateViewHelperTest.php:49
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\viewHelperUsesChildNodesWithTimestamp
‪viewHelperUsesChildNodesWithTimestamp()
Definition: DateViewHelperTest.php:243
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\$resetSingletonInstances
‪$resetSingletonInstances
Definition: DateViewHelperTest.php:38
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\viewHelperUsesDefaultIfNoSystemFormatIsAvailable
‪viewHelperUsesDefaultIfNoSystemFormatIsAvailable()
Definition: DateViewHelperTest.php:175
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\viewHelperRespectsDefaultTimezoneForIntegerTimestampDataProvider
‪array viewHelperRespectsDefaultTimezoneForIntegerTimestampDataProvider()
Definition: DateViewHelperTest.php:346
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\viewHelperThrowsExceptionWithOriginalMessageIfDateStringCantBeParsed
‪viewHelperThrowsExceptionWithOriginalMessageIfDateStringCantBeParsed()
Definition: DateViewHelperTest.php:207
‪TYPO3\CMS\Fluid\ViewHelpers\Format\DateViewHelper
Definition: DateViewHelper.php:102
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\viewHelperRespectsCustomFormat
‪viewHelperRespectsCustomFormat()
Definition: DateViewHelperTest.php:88
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format
Definition: BytesViewHelperTest.php:2
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\$subject
‪DateViewHelper PHPUnit_Framework_MockObject_MockObject TYPO3 TestingFramework Core AccessibleObjectInterface $subject
Definition: DateViewHelperTest.php:28
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\viewHelperFormatsDateCorrectly
‪viewHelperFormatsDateCorrectly()
Definition: DateViewHelperTest.php:58
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\viewHelperFormatsDateStringCorrectly
‪viewHelperFormatsDateStringCorrectly()
Definition: DateViewHelperTest.php:73
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\viewHelperReturnsCurrentDateIfEmptyStringIsGiven
‪viewHelperReturnsCurrentDateIfEmptyStringIsGiven()
Definition: DateViewHelperTest.php:139
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\viewHelperRespectsDefaultTimezoneForStringTimestampDataProvider
‪array viewHelperRespectsDefaultTimezoneForStringTimestampDataProvider()
Definition: DateViewHelperTest.php:386
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\viewHelperUsesSystemFormat
‪viewHelperUsesSystemFormat()
Definition: DateViewHelperTest.php:191
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\$viewHelper
‪DateViewHelper $viewHelper
Definition: DateViewHelperTest.php:36
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\dateArgumentHasPriorityOverChildNodes
‪dateArgumentHasPriorityOverChildNodes()
Definition: DateViewHelperTest.php:262
‪$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:32
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\baseArgumentDoesNotAffectAbsoluteTime
‪baseArgumentDoesNotAffectAbsoluteTime()
Definition: DateViewHelperTest.php:327
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\viewHelperRespectsDefaultTimezoneForStringTimestamp
‪viewHelperRespectsDefaultTimezoneForStringTimestamp($timeZone, $date, $expected)
Definition: DateViewHelperTest.php:417
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\baseArgumentAsDateTimeIsConsideredForRelativeDate
‪baseArgumentAsDateTimeIsConsideredForRelativeDate()
Definition: DateViewHelperTest.php:310
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\relativeDateCalculationWorksWithoutBase
‪relativeDateCalculationWorksWithoutBase()
Definition: DateViewHelperTest.php:277
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\baseArgumentIsConsideredForRelativeDate
‪baseArgumentIsConsideredForRelativeDate()
Definition: DateViewHelperTest.php:293
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\viewHelperSupportsDateTimeImmutable
‪viewHelperSupportsDateTimeImmutable()
Definition: DateViewHelperTest.php:104
‪TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format\DateViewHelperTest\viewHelperUsesChildNodesIfDateAttributeIsNotSpecified
‪viewHelperUsesChildNodesIfDateAttributeIsNotSpecified()
Definition: DateViewHelperTest.php:224