TYPO3CMS  8
 All Classes Namespaces Files Functions Variables Pages
DateViewHelperTest.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format;
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  */
19 
24 {
28  protected $subject;
29 
33  protected $timezone;
34 
38  protected $viewHelper;
39 
40  protected function setUp()
41  {
42  parent::setUp();
43  $this->timezone = @date_default_timezone_get();
44  $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] = 'Y-m-d';
45  $this->viewHelper = new DateViewHelper();
46  $this->injectDependenciesIntoViewHelper($this->viewHelper);
47  }
48 
49  protected function tearDown()
50  {
51  date_default_timezone_set($this->timezone);
52  parent::tearDown();
53  }
54 
59  {
60  $this->setArgumentsUnderTest(
61  $this->viewHelper,
62  [
63  'date' => new \DateTime('1980-12-13')
64  ]
65  );
66  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
67  $this->assertEquals('1980-12-13', $actualResult);
68  }
69 
74  {
75  $this->setArgumentsUnderTest(
76  $this->viewHelper,
77  [
78  'date' => '1980-12-13'
79  ]
80  );
81  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
82  $this->assertEquals('1980-12-13', $actualResult);
83  }
84 
89  {
90  $this->setArgumentsUnderTest(
91  $this->viewHelper,
92  [
93  'date' => new \DateTime('1980-02-01'),
94  'format' => 'd.m.Y'
95  ]
96  );
97  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
98  $this->assertEquals('01.02.1980', $actualResult);
99  }
100 
105  {
106  $this->setArgumentsUnderTest(
107  $this->viewHelper,
108  [
109  'date' => new \DateTimeImmutable('1980-02-01'),
110  'format' => 'd.m.Y'
111  ]
112  );
113  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
114  $this->assertEquals('01.02.1980', $actualResult);
115  }
116 
121  {
122  $this->viewHelper->setRenderChildrenClosure(
123  function () {
124  return null;
125  }
126  );
127  $this->setArgumentsUnderTest(
128  $this->viewHelper,
129  [
130  ]
131  );
132  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
133  $this->assertEquals('', $actualResult);
134  }
135 
140  {
141  $this->setArgumentsUnderTest(
142  $this->viewHelper,
143  [
144  'date' => ''
145  ]
146  );
147  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
148  $expectedResult = (new \DateTime())->format('Y-m-d');
149  $this->assertEquals($expectedResult, $actualResult);
150  }
151 
156  {
157  $this->viewHelper->setRenderChildrenClosure(
158  function () {
159  return '';
160  }
161  );
162  $this->setArgumentsUnderTest(
163  $this->viewHelper,
164  [
165  ]
166  );
167  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
168  $expectedResult = (new \DateTime())->format('Y-m-d');
169  $this->assertEquals($expectedResult, $actualResult);
170  }
171 
176  {
177  $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] = '';
178  $this->setArgumentsUnderTest(
179  $this->viewHelper,
180  [
181  'date' => '@1391876733'
182  ]
183  );
184  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
185  $this->assertEquals('2014-02-08', $actualResult);
186  }
187 
191  public function viewHelperUsesSystemFormat()
192  {
193  $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] = 'l, j. M y';
194  $this->setArgumentsUnderTest(
195  $this->viewHelper,
196  [
197  'date' => '@1391876733'
198  ]
199  );
200  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
201  $this->assertEquals('Saturday, 8. Feb 14', $actualResult);
202  }
203 
208  {
209  $this->expectException(Exception::class);
210  $this->expectExceptionCode(1241722579);
211 
212  $this->setArgumentsUnderTest(
213  $this->viewHelper,
214  [
215  'date' => 'foo'
216  ]
217  );
218  $this->viewHelper->initializeArgumentsAndRender();
219  }
220 
225  {
226  $this->viewHelper->setRenderChildrenClosure(
227  function () {
228  return new \DateTime('1980-12-13');
229  }
230  );
231  $this->setArgumentsUnderTest(
232  $this->viewHelper,
233  [
234  ]
235  );
236  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
237  $this->assertEquals('1980-12-13', $actualResult);
238  }
239 
244  {
245  $this->viewHelper->setRenderChildrenClosure(
246  function () {
247  return '1359891658' . LF;
248  }
249  );
250  $this->setArgumentsUnderTest(
251  $this->viewHelper,
252  [
253  ]
254  );
255  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
256  $this->assertEquals('2013-02-03', $actualResult);
257  }
258 
263  {
264  $this->setArgumentsUnderTest(
265  $this->viewHelper,
266  [
267  'date' => '1980-12-12'
268  ]
269  );
270  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
271  $this->assertEquals('1980-12-12', $actualResult);
272  }
273 
278  {
279  $this->setArgumentsUnderTest(
280  $this->viewHelper,
281  [
282  'date' => 'now',
283  'format' => 'Y',
284  ]
285  );
286  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
287  $this->assertEquals(date('Y'), $actualResult);
288  }
289 
294  {
295  $this->setArgumentsUnderTest(
296  $this->viewHelper,
297  [
298  'date' => '-1 year',
299  'format' => 'Y',
300  'base' => '2017-01-01'
301  ]
302  );
303  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
304  $this->assertEquals('2016', $actualResult);
305  }
306 
311  {
312  $this->setArgumentsUnderTest(
313  $this->viewHelper,
314  [
315  'date' => '-1 year',
316  'format' => 'Y',
317  'base' => new \DateTime('2017-01-01')
318  ]
319  );
320  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
321  $this->assertEquals('2016', $actualResult);
322  }
323 
328  {
329  $this->setArgumentsUnderTest(
330  $this->viewHelper,
331  [
332  'date' => '@1435784732',
333  'format' => 'Y',
334  'base' => 1485907200
335  ]
336  );
337  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
338  $this->assertEquals('2015', $actualResult);
339  }
340 
347  {
348  return [
349  'Europe/Berlin' => [
350  'Europe/Berlin',
351  '2013-02-03 12:40',
352  ],
353  'Asia/Riyadh' => [
354  'Asia/Riyadh',
355  '2013-02-03 14:40',
356  ],
357  ];
358  }
359 
365  {
366  $date = 1359891658; // 2013-02-03 11:40 UTC
367  $format = 'Y-m-d H:i';
368 
369  date_default_timezone_set($timezone);
370  $this->setArgumentsUnderTest(
371  $this->viewHelper,
372  [
373  'date' => $date,
374  'format' => $format
375  ]
376  );
377  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
378  $this->assertEquals($expected, $actualResult);
379  }
380 
387  {
388  return [
389  'Europe/Berlin UTC' => [
390  'Europe/Berlin',
391  '@1359891658',
392  '2013-02-03 12:40'
393  ],
394  'Europe/Berlin Moscow' => [
395  'Europe/Berlin',
396  '03/Oct/2000:14:55:36 +0400',
397  '2000-10-03 12:55'
398  ],
399  'Asia/Riyadh UTC' => [
400  'Asia/Riyadh',
401  '@1359891658',
402  '2013-02-03 14:40'
403  ],
404  'Asia/Riyadh Moscow' => [
405  'Asia/Riyadh',
406  '03/Oct/2000:14:55:36 +0400',
407  '2000-10-03 13:55'
408  ],
409  ];
410  }
411 
417  public function viewHelperRespectsDefaultTimezoneForStringTimestamp($timeZone, $date, $expected)
418  {
419  $format = 'Y-m-d H:i';
420 
421  date_default_timezone_set($timeZone);
422  $this->setArgumentsUnderTest(
423  $this->viewHelper,
424  [
425  'date' => $date,
426  'format' => $format
427  ]
428  );
429  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
430  $this->assertEquals($expected, $actualResult);
431  }
432 
439  {
440  return [
441  'de_DE.UTF-8' => [
442  'de_DE.UTF-8',
443  '03. Februar 2013'
444  ],
445  'en_ZW.utf8' => [
446  'en_ZW.utf8',
447  '03. February 2013'
448  ]
449  ];
450  }
451 
457  public function dateViewHelperFormatsDateLocalized($locale, $expected)
458  {
459  $format = '%d. %B %Y';
460  // 2013-02-03 11:40 UTC
461  $timestamp = '@1359891658';
462 
463  try {
464  $this->setLocale(LC_COLLATE, $locale);
465  $this->setLocale(LC_CTYPE, $locale);
466  $this->setLocale(LC_MONETARY, $locale);
467  $this->setLocale(LC_TIME, $locale);
468  } catch (\PHPUnit_Framework_Exception $e) {
469  $this->markTestSkipped('Locale ' . $locale . ' is not available.');
470  }
471  $this->setArgumentsUnderTest(
472  $this->viewHelper,
473  [
474  'date' => $timestamp,
475  'format' => $format
476  ]
477  );
478  $actualResult = $this->viewHelper->initializeArgumentsAndRender();
479  $this->assertEquals($expected, $actualResult);
480  }
481 }
setArgumentsUnderTest(ViewHelperInterface $viewHelper, array $arguments=[])
if(TYPO3_MODE=== 'BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']