TYPO3 CMS  TYPO3_7-6
DateViewHelperTest.php
Go to the documentation of this file.
1 <?php
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  * */
16 
21 {
25  protected $backupLocales = [];
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 = [
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, ['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  $actualResult = $this->subject->render(new \DateTimeImmutable('1980-02-01'), 'd.m.Y');
97  $this->assertEquals('01.02.1980', $actualResult);
98  }
99 
104  {
105  $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue(null));
106  $actualResult = $this->subject->render();
107  $this->assertEquals('', $actualResult);
108  }
109 
114  {
115  $actualResult = $this->subject->render('');
116  $expectedResult = (new \DateTime())->format('Y-m-d');
117  $this->assertEquals($expectedResult, $actualResult);
118  }
119 
124  {
125  $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue(''));
126  $actualResult = $this->subject->render();
127  $expectedResult = (new \DateTime())->format('Y-m-d');
128  $this->assertEquals($expectedResult, $actualResult);
129  }
130 
135  {
136  $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] = '';
137  $actualResult = $this->subject->render('@1391876733');
138  $this->assertEquals('2014-02-08', $actualResult);
139  }
140 
144  public function viewHelperUsesSystemFormat()
145  {
146  $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] = 'l, j. M y';
147  $actualResult = $this->subject->render('@1391876733');
148  $this->assertEquals('Saturday, 8. Feb 14', $actualResult);
149  }
150 
157  {
158  $this->subject->render('foo');
159  }
160 
165  {
166  $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue(new \DateTime('1980-12-13')));
167  $actualResult = $this->subject->render();
168  $this->assertEquals('1980-12-13', $actualResult);
169  }
170 
175  {
176  $this->subject->expects($this->once())->method('renderChildren')->will($this->returnValue('1359891658' . LF));
177  $actualResult = $this->subject->render();
178  $this->assertEquals('2013-02-03', $actualResult);
179  }
180 
185  {
186  $this->subject->expects($this->never())->method('renderChildren');
187  $actualResult = $this->subject->render('1980-12-12');
188  $this->assertEquals('1980-12-12', $actualResult);
189  }
190 
195  {
196  $this->subject->expects($this->never())->method('renderChildren');
197  $actualResult = $this->subject->render('now', 'Y');
198  $this->assertEquals(date('Y'), $actualResult);
199  }
200 
205  {
206  $this->subject->expects($this->never())->method('renderChildren');
207  $actualResult = $this->subject->render('-1 year', 'Y', '2017-01-01');
208  $this->assertEquals('2016', $actualResult);
209  }
210 
215  {
216  $this->subject->expects($this->never())->method('renderChildren');
217  $actualResult = $this->subject->render('-1 year', 'Y', new \DateTime('2017-01-01'));
218  $this->assertEquals('2016', $actualResult);
219  }
220 
225  {
226  $this->subject->expects($this->never())->method('renderChildren');
227  $actualResult = $this->subject->render('@1435784732', 'Y', 1485907200); // somewhere in 2017
228  $this->assertEquals('2015', $actualResult);
229  }
230 
237  {
238  return [
239  'Europe/Berlin' => [
240  'Europe/Berlin',
241  '2013-02-03 12:40',
242  ],
243  'Asia/Riyadh' => [
244  'Asia/Riyadh',
245  '2013-02-03 14:40',
246  ],
247  ];
248  }
249 
255  {
256  $date = 1359891658; // 2013-02-03 11:40 UTC
257  $format = 'Y-m-d H:i';
258 
259  date_default_timezone_set($timezone);
260  $this->assertEquals($expected, $this->subject->render($date, $format));
261  }
262 
269  {
270  return [
271  'Europe/Berlin UTC' => [
272  'Europe/Berlin',
273  '@1359891658',
274  '2013-02-03 12:40'
275  ],
276  'Europe/Berlin Moscow' => [
277  'Europe/Berlin',
278  '03/Oct/2000:14:55:36 +0400',
279  '2000-10-03 12:55'
280  ],
281  'Asia/Riyadh UTC' => [
282  'Asia/Riyadh',
283  '@1359891658',
284  '2013-02-03 14:40'
285  ],
286  'Asia/Riyadh Moscow' => [
287  'Asia/Riyadh',
288  '03/Oct/2000:14:55:36 +0400',
289  '2000-10-03 13:55'
290  ],
291  ];
292  }
293 
299  public function viewHelperRespectsDefaultTimezoneForStringTimestamp($timeZone, $date, $expected)
300  {
301  $format = 'Y-m-d H:i';
302 
303  date_default_timezone_set($timeZone);
304  $this->assertEquals($expected, $this->subject->render($date, $format));
305  }
306 
313  {
314  return [
315  'de_DE.UTF-8' => [
316  'de_DE.UTF-8',
317  '03. Februar 2013'
318  ],
319  'en_ZW.utf8' => [
320  'en_ZW.utf8',
321  '03. February 2013'
322  ]
323  ];
324  }
325 
331  public function dateViewHelperFormatsDateLocalized($locale, $expected)
332  {
333  $format = '%d. %B %Y';
334  // 2013-02-03 11:40 UTC
335  $timestamp = '@1359891658';
336 
337  if (!setlocale(LC_COLLATE, $locale)) {
338  $this->markTestSkipped('Locale ' . $locale . ' is not available.');
339  }
340  $this->setCustomLocale($locale);
341  $this->assertEquals($expected, $this->subject->render($timestamp, $format));
342  }
343 
347  protected function setCustomLocale($locale)
348  {
349  setlocale(LC_CTYPE, $locale);
350  setlocale(LC_MONETARY, $locale);
351  setlocale(LC_TIME, $locale);
352  }
353 }
getAccessibleMock( $originalClassName, $methods=[], array $arguments=[], $mockClassName='', $callOriginalConstructor=true, $callOriginalClone=true, $callAutoload=true)
if(TYPO3_MODE==='BE') $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_tsfebeuserauth.php']['frontendEditingController']['default']