TYPO3 CMS  TYPO3_6-2
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  * */
14 
19 
23  protected $backupLocales = array();
24 
28  protected $timezone;
29 
30  public function setUp() {
31  parent::setUp();
32  // Store all locale categories manipulated in tests for reconstruction in tearDown
33  $this->backupLocales = array(
34  'LC_COLLATE' => setlocale(LC_COLLATE, 0),
35  'LC_CTYPE' => setlocale(LC_CTYPE, 0),
36  'LC_MONETARY' => setlocale(LC_MONETARY, 0),
37  'LC_TIME' => setlocale(LC_TIME, 0),
38  );
39  $this->timezone = @date_default_timezone_get();
40  $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] = 'Y-m-d';
41  }
42 
43  public function tearDown() {
44  foreach ($this->backupLocales as $category => $locale) {
45  setlocale(constant($category), $locale);
46  }
47  date_default_timezone_set($this->timezone);
48  parent::tearDown();
49  }
50 
54  public function viewHelperFormatsDateCorrectly() {
55  $viewHelper = new DateViewHelper();
56  $actualResult = $viewHelper->render(new \DateTime('1980-12-13'));
57  $this->assertEquals('1980-12-13', $actualResult);
58  }
59 
64  $viewHelper = new DateViewHelper();
65  $actualResult = $viewHelper->render('1980-12-13');
66  $this->assertEquals('1980-12-13', $actualResult);
67  }
68 
72  public function viewHelperRespectsCustomFormat() {
73  $viewHelper = new DateViewHelper();
74  $actualResult = $viewHelper->render(new \DateTime('1980-02-01'), 'd.m.Y');
75  $this->assertEquals('01.02.1980', $actualResult);
76  }
77 
81  public function viewHelperReturnsEmptyStringIfNULLIsGiven() {
83  $viewHelper = $this->getMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Format\\DateViewHelper', array('renderChildren'));
84  $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue(NULL));
85  $actualResult = $viewHelper->render();
86  $this->assertEquals('', $actualResult);
87  }
88 
93  $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] = '';
94  $viewHelper = new DateViewHelper();
95  $actualResult = $viewHelper->render('@1391876733');
96  $this->assertEquals('2014-02-08', $actualResult);
97  }
98 
102  public function viewHelperUsesSystemFormat() {
103  $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] = 'l, j. M y';
104  $viewHelper = new DateViewHelper();
105  $actualResult = $viewHelper->render('@1391876733');
106  $this->assertEquals('Saturday, 8. Feb 14', $actualResult);
107  }
108 
114  $viewHelper = new DateViewHelper();
115  $viewHelper->render('foo');
116  }
117 
121  public function viewHelperUsesChildNodesIfDateAttributeIsNotSpecified() {
123  $viewHelper = $this->getMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Format\\DateViewHelper', array('renderChildren'));
124  $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue(new \DateTime('1980-12-13')));
125  $actualResult = $viewHelper->render();
126  $this->assertEquals('1980-12-13', $actualResult);
127  }
128 
132  public function viewHelperUsesChildNodesWithTimestamp() {
134  $viewHelper = $this->getMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Format\\DateViewHelper', array('renderChildren'));
135  $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue('1359891658'));
136  $actualResult = $viewHelper->render();
137  $this->assertEquals('2013-02-03', $actualResult);
138  }
139 
143  public function dateArgumentHasPriorityOverChildNodes() {
145  $viewHelper = $this->getMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Format\\DateViewHelper', array('renderChildren'));
146  $viewHelper->expects($this->never())->method('renderChildren');
147  $actualResult = $viewHelper->render('1980-12-12');
148  $this->assertEquals('1980-12-12', $actualResult);
149  }
150 
157  return array(
158  'Europe/Berlin' => array(
159  'Europe/Berlin',
160  '2013-02-03 12:40',
161  ),
162  'Asia/Riyadh' => array(
163  'Asia/Riyadh',
164  '2013-02-03 14:40',
165  ),
166  );
167  }
168 
173  public function viewHelperRespectsDefaultTimezoneForIntegerTimestamp($timezone, $expected) {
175  $viewHelper = $this->getMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Format\\DateViewHelper', array('renderChildren'));
176 
177  $date = 1359891658; // 2013-02-03 11:40 UTC
178  $format = 'Y-m-d H:i';
179 
180  date_default_timezone_set($timezone);
181  $this->assertEquals($expected, $viewHelper->render($date, $format));
182  }
183 
190  return array(
191  'Europe/Berlin UTC' => array(
192  'Europe/Berlin',
193  '@1359891658',
194  '2013-02-03 12:40'
195  ),
196  'Europe/Berlin Moscow' => array(
197  'Europe/Berlin',
198  '03/Oct/2000:14:55:36 +0400',
199  '2000-10-03 12:55'
200  ),
201  'Asia/Riyadh UTC' => array(
202  'Asia/Riyadh',
203  '@1359891658',
204  '2013-02-03 14:40'
205  ),
206  'Asia/Riyadh Moscow' => array(
207  'Asia/Riyadh',
208  '03/Oct/2000:14:55:36 +0400',
209  '2000-10-03 13:55'
210  ),
211  );
212  }
213 
219  public function viewHelperRespectsDefaultTimezoneForStringTimestamp($timeZone, $date, $expected) {
221  $viewHelper = $this->getMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Format\\DateViewHelper', array('renderChildren'));
222  $format = 'Y-m-d H:i';
223 
224  date_default_timezone_set($timeZone);
225  $this->assertEquals($expected, $viewHelper->render($date, $format));
226  }
227 
234  return array(
235  'de_DE.UTF-8' => array(
236  'de_DE.UTF-8',
237  '03. Februar 2013'
238  ),
239  'en_ZW.utf8' => array(
240  'en_ZW.utf8',
241  '03. February 2013'
242  )
243  );
244  }
245 
251  public function dateViewHelperFormatsDateLocalized($locale, $expected) {
253  $viewHelper = $this->getMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Format\\DateViewHelper', array('renderChildren'));
254  $format = '%d. %B %Y';
255  // 2013-02-03 11:40 UTC
256  $timestamp = '@1359891658';
257 
258  if (!setlocale(LC_COLLATE, $locale)) {
259  $this->markTestSkipped('Locale ' . $locale . ' is not available.');
260  }
261  $this->setCustomLocale($locale);
262  $this->assertEquals($expected, $viewHelper->render($timestamp, $format));
263  }
264 
268  protected function setCustomLocale($locale) {
269  setlocale(LC_CTYPE, $locale);
270  setlocale(LC_MONETARY, $locale);
271  setlocale(LC_TIME, $locale);
272  }
273 }
if(!defined('TYPO3_MODE')) $GLOBALS['TYPO3_CONF_VARS']['SC_OPTIONS']['t3lib/class.t3lib_userauth.php']['logoff_pre_processing'][]