TYPO3  7.6
DateViewHelperTest.php
Go to the documentation of this file.
1 <?php
2 namespace TYPO3\CMS\Fluid\Tests\Unit\ViewHelpers\Format;
3 
4 /* *
5  * This script is backported from the TYPO3 Flow package "TYPO3.Fluid". *
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 3 of the *
9  * License, or (at your option) any later version. *
10  * *
11  * The TYPO3 project - inspiring people to share! *
12  * */
13 use TYPO3\CMS\Core\Tests\UnitTestCase;
16 
20 class DateViewHelperTest extends UnitTestCase
21 {
25  protected $backupLocales = array();
26 
30  protected $subject;
31 
35  protected $timezone;
36 
37  protected function setUp()
38  {
39  parent::setUp();
40  // Store all locale categories manipulated in tests for reconstruction in tearDown
41  $this->backupLocales = array(
42  'LC_COLLATE' => setlocale(LC_COLLATE, 0),
43  'LC_CTYPE' => setlocale(LC_CTYPE, 0),
44  'LC_MONETARY' => setlocale(LC_MONETARY, 0),
45  'LC_TIME' => setlocale(LC_TIME, 0),
46  );
47  $this->timezone = @date_default_timezone_get();
48  $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] = 'Y-m-d';
49  $this->subject = $this->getAccessibleMock(DateViewHelper::class, array('renderChildren'));
51  $renderingContext = $this->getMock(RenderingContext::class);
52  $this->subject->_set('renderingContext', $renderingContext);
53  }
54 
55  protected function tearDown()
56  {
57  foreach ($this->backupLocales as $category => $locale) {
58  setlocale(constant($category), $locale);
59  }
60  date_default_timezone_set($this->timezone);
61  parent::tearDown();
62  }
63 
68  {
69  $actualResult = $this->subject->render(new \DateTime('1980-12-13'));
70  $this->assertEquals('1980-12-13', $actualResult);
71  }
72 
77  {
78  $actualResult = $this->subject->render('1980-12-13');
79  $this->assertEquals('1980-12-13', $actualResult);
80  }
81 
86  {
87  $actualResult = $this->subject->render(new \DateTime('1980-02-01'), 'd.m.Y');
88  $this->assertEquals('01.02.1980', $actualResult);
89  }
90 
95  {
96  $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue(null));
97  $actualResult = $this->subject->render();
98  $this->assertEquals('', $actualResult);
99  }
100 
105  {
106  $actualResult = $this->subject->render('');
107  $expectedResult = (new \DateTime())->format('Y-m-d');
108  $this->assertEquals($expectedResult, $actualResult);
109  }
110 
115  {
116  $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue(''));
117  $actualResult = $this->subject->render();
118  $expectedResult = (new \DateTime())->format('Y-m-d');
119  $this->assertEquals($expectedResult, $actualResult);
120  }
121 
126  {
127  $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] = '';
128  $actualResult = $this->subject->render('@1391876733');
129  $this->assertEquals('2014-02-08', $actualResult);
130  }
131 
135  public function viewHelperUsesSystemFormat()
136  {
137  $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] = 'l, j. M y';
138  $actualResult = $this->subject->render('@1391876733');
139  $this->assertEquals('Saturday, 8. Feb 14', $actualResult);
140  }
141 
148  {
149  $this->subject->render('foo');
150  }
151 
156  {
157  $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue(new \DateTime('1980-12-13')));
158  $actualResult = $this->subject->render();
159  $this->assertEquals('1980-12-13', $actualResult);
160  }
161 
166  {
167  $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue('1359891658'));
168  $actualResult = $this->subject->render();
169  $this->assertEquals('2013-02-03', $actualResult);
170  }
171 
176  {
177  $this->subject->expects($this->never())->method('renderChildren');
178  $actualResult = $this->subject->render('1980-12-12');
179  $this->assertEquals('1980-12-12', $actualResult);
180  }
181 
186  {
187  $this->subject->expects($this->never())->method('renderChildren');
188  $actualResult = $this->subject->render('now', 'Y');
189  $this->assertEquals(date('Y'), $actualResult);
190  }
191 
196  {
197  $this->subject->expects($this->never())->method('renderChildren');
198  $actualResult = $this->subject->render('-1 year', 'Y', '2017-01-01');
199  $this->assertEquals('2016', $actualResult);
200  }
201 
206  {
207  $this->subject->expects($this->never())->method('renderChildren');
208  $actualResult = $this->subject->render('-1 year', 'Y', new \DateTime('2017-01-01'));
209  $this->assertEquals('2016', $actualResult);
210  }
211 
216  {
217  $this->subject->expects($this->never())->method('renderChildren');
218  $actualResult = $this->subject->render('@1435784732', 'Y', 1485907200); // somewhere in 2017
219  $this->assertEquals('2015', $actualResult);
220  }
221 
228  {
229  return array(
230  'Europe/Berlin' => array(
231  'Europe/Berlin',
232  '2013-02-03 12:40',
233  ),
234  'Asia/Riyadh' => array(
235  'Asia/Riyadh',
236  '2013-02-03 14:40',
237  ),
238  );
239  }
240 
246  {
247  $date = 1359891658; // 2013-02-03 11:40 UTC
248  $format = 'Y-m-d H:i';
249 
250  date_default_timezone_set($timezone);
251  $this->assertEquals($expected, $this->subject->render($date, $format));
252  }
253 
260  {
261  return array(
262  'Europe/Berlin UTC' => array(
263  'Europe/Berlin',
264  '@1359891658',
265  '2013-02-03 12:40'
266  ),
267  'Europe/Berlin Moscow' => array(
268  'Europe/Berlin',
269  '03/Oct/2000:14:55:36 +0400',
270  '2000-10-03 12:55'
271  ),
272  'Asia/Riyadh UTC' => array(
273  'Asia/Riyadh',
274  '@1359891658',
275  '2013-02-03 14:40'
276  ),
277  'Asia/Riyadh Moscow' => array(
278  'Asia/Riyadh',
279  '03/Oct/2000:14:55:36 +0400',
280  '2000-10-03 13:55'
281  ),
282  );
283  }
284 
290  public function viewHelperRespectsDefaultTimezoneForStringTimestamp($timeZone, $date, $expected)
291  {
292  $format = 'Y-m-d H:i';
293 
294  date_default_timezone_set($timeZone);
295  $this->assertEquals($expected, $this->subject->render($date, $format));
296  }
297 
304  {
305  return array(
306  'de_DE.UTF-8' => array(
307  'de_DE.UTF-8',
308  '03. Februar 2013'
309  ),
310  'en_ZW.utf8' => array(
311  'en_ZW.utf8',
312  '03. February 2013'
313  )
314  );
315  }
316 
322  public function dateViewHelperFormatsDateLocalized($locale, $expected)
323  {
324  $format = '%d. %B %Y';
325  // 2013-02-03 11:40 UTC
326  $timestamp = '@1359891658';
327 
328  if (!setlocale(LC_COLLATE, $locale)) {
329  $this->markTestSkipped('Locale ' . $locale . ' is not available.');
330  }
331  $this->setCustomLocale($locale);
332  $this->assertEquals($expected, $this->subject->render($timestamp, $format));
333  }
334 
338  protected function setCustomLocale($locale)
339  {
340  setlocale(LC_CTYPE, $locale);
341  setlocale(LC_MONETARY, $locale);
342  setlocale(LC_TIME, $locale);
343  }
344 }