[!!!][TASK] Set f:format.date format to system default
[Packages/TYPO3.CMS.git] / typo3 / sysext / fluid / Tests / Unit / ViewHelpers / Format / DateViewHelperTest.php
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
14 class DateViewHelperTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
15
16 /**
17 * @var array Backup of current locale, it is manipulated in tests
18 */
19 protected $backupLocales = array();
20
21 /**
22 * @var string Backup of current timezone, it is manipulated in tests
23 */
24 protected $timezone;
25
26 public function setUp() {
27 parent::setUp();
28 // Store all locale categories manipulated in tests for reconstruction in tearDown
29 $this->backupLocales = array(
30 'LC_COLLATE' => setlocale(LC_COLLATE, 0),
31 'LC_CTYPE' => setlocale(LC_CTYPE, 0),
32 'LC_MONETARY' => setlocale(LC_MONETARY, 0),
33 'LC_TIME' => setlocale(LC_TIME, 0),
34 );
35 $this->timezone = @date_default_timezone_get();
36 $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] = 'Y-m-d';
37 }
38
39 public function tearDown() {
40 foreach ($this->backupLocales as $category => $locale) {
41 setlocale(constant($category), $locale);
42 }
43 date_default_timezone_set($this->timezone);
44 }
45
46 /**
47 * @test
48 */
49 public function viewHelperFormatsDateCorrectly() {
50 $viewHelper = new \TYPO3\CMS\Fluid\ViewHelpers\Format\DateViewHelper();
51 $actualResult = $viewHelper->render(new \DateTime('1980-12-13'));
52 $this->assertEquals('1980-12-13', $actualResult);
53 }
54
55 /**
56 * @test
57 */
58 public function viewHelperFormatsDateStringCorrectly() {
59 $viewHelper = new \TYPO3\CMS\Fluid\ViewHelpers\Format\DateViewHelper();
60 $actualResult = $viewHelper->render('1980-12-13');
61 $this->assertEquals('1980-12-13', $actualResult);
62 }
63
64 /**
65 * @test
66 */
67 public function viewHelperRespectsCustomFormat() {
68 $viewHelper = new \TYPO3\CMS\Fluid\ViewHelpers\Format\DateViewHelper();
69 $actualResult = $viewHelper->render(new \DateTime('1980-02-01'), 'd.m.Y');
70 $this->assertEquals('01.02.1980', $actualResult);
71 }
72
73 /**
74 * @test
75 */
76 public function viewHelperReturnsEmptyStringIfNULLIsGiven() {
77 $viewHelper = $this->getMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Format\\DateViewHelper', array('renderChildren'));
78 $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue(NULL));
79 $actualResult = $viewHelper->render();
80 $this->assertEquals('', $actualResult);
81 }
82
83 /**
84 * @test
85 */
86 public function viewHelperUsesDefaultIfNoSystemFormatIsAvailable() {
87 $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] = '';
88 $viewHelper = new \TYPO3\CMS\Fluid\ViewHelpers\Format\DateViewHelper();
89 $actualResult = $viewHelper->render('@1391876733');
90 $this->assertEquals('2014-02-08', $actualResult);
91 }
92
93 /**
94 * @test
95 */
96 public function viewHelperUsesSystemFormat() {
97 $GLOBALS['TYPO3_CONF_VARS']['SYS']['ddmmyy'] = 'l, j. M y';
98 $viewHelper = new \TYPO3\CMS\Fluid\ViewHelpers\Format\DateViewHelper();
99 $actualResult = $viewHelper->render('@1391876733');
100 $this->assertEquals('Saturday, 8. Feb 14', $actualResult);
101 }
102
103 /**
104 * @test
105 * @expectedException \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
106 */
107 public function viewHelperThrowsExceptionIfDateStringCantBeParsed() {
108 $viewHelper = new \TYPO3\CMS\Fluid\ViewHelpers\Format\DateViewHelper();
109 $viewHelper->render('foo');
110 }
111
112 /**
113 * @test
114 */
115 public function viewHelperUsesChildNodesIfDateAttributeIsNotSpecified() {
116 $viewHelper = $this->getMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Format\\DateViewHelper', array('renderChildren'));
117 $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue(new \DateTime('1980-12-13')));
118 $actualResult = $viewHelper->render();
119 $this->assertEquals('1980-12-13', $actualResult);
120 }
121
122 /**
123 * @test
124 */
125 public function dateArgumentHasPriorityOverChildNodes() {
126 $viewHelper = $this->getMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Format\\DateViewHelper', array('renderChildren'));
127 $viewHelper->expects($this->never())->method('renderChildren');
128 $actualResult = $viewHelper->render('1980-12-12');
129 $this->assertEquals('1980-12-12', $actualResult);
130 }
131
132 /**
133 * Data provider for viewHelperRespectsDefaultTimezoneForIntegerTimestamp
134 *
135 * @return array
136 */
137 public function viewHelperRespectsDefaultTimezoneForIntegerTimestampDataProvider() {
138 return array(
139 'Europe/Berlin' => array(
140 'Europe/Berlin',
141 '2013-02-03 12:40',
142 ),
143 'Asia/Riyadh' => array(
144 'Asia/Riyadh',
145 '2013-02-03 14:40',
146 ),
147 );
148 }
149
150 /**
151 * @test
152 * @dataProvider viewHelperRespectsDefaultTimezoneForIntegerTimestampDataProvider
153 */
154 public function viewHelperRespectsDefaultTimezoneForIntegerTimestamp($timezone, $expected) {
155 $viewHelper = $this->getMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Format\\DateViewHelper', array('renderChildren'));
156
157 $date = 1359891658; // 2013-02-03 11:40 UTC
158 $format = 'Y-m-d H:i';
159
160 date_default_timezone_set($timezone);
161 $this->assertEquals($expected, $viewHelper->render($date, $format));
162 }
163
164 /**
165 * Data provider for viewHelperRespectsDefaultTimezoneForStringTimestamp
166 *
167 * @return array
168 */
169 public function viewHelperRespectsDefaultTimezoneForStringTimestampDataProvider() {
170 return array(
171 'Europe/Berlin UTC' => array(
172 'Europe/Berlin',
173 '@1359891658',
174 '2013-02-03 12:40'
175 ),
176 'Europe/Berlin Moscow' => array(
177 'Europe/Berlin',
178 '03/Oct/2000:14:55:36 +0400',
179 '2000-10-03 12:55'
180 ),
181 'Asia/Riyadh UTC' => array(
182 'Asia/Riyadh',
183 '@1359891658',
184 '2013-02-03 14:40'
185 ),
186 'Asia/Riyadh Moscow' => array(
187 'Asia/Riyadh',
188 '03/Oct/2000:14:55:36 +0400',
189 '2000-10-03 13:55'
190 ),
191 );
192 }
193
194 /**
195 * @dataProvider viewHelperRespectsDefaultTimezoneForStringTimestampDataProvider
196 *
197 * @test
198 */
199 public function viewHelperRespectsDefaultTimezoneForStringTimestamp($timeZone, $date, $expected) {
200 $viewHelper = $this->getMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Format\\DateViewHelper', array('renderChildren'));
201 $format = 'Y-m-d H:i';
202
203 date_default_timezone_set($timeZone);
204 $this->assertEquals($expected, $viewHelper->render($date, $format));
205 }
206
207 /**
208 * Data provider for dateViewHelperFormatsDateLocalizedDataProvider
209 *
210 * @return array
211 */
212 public function dateViewHelperFormatsDateLocalizedDataProvider() {
213 return array(
214 'de_DE.UTF-8' => array(
215 'de_DE.UTF-8',
216 '03. Februar 2013'
217 ),
218 'en_ZW.utf8' => array(
219 'en_ZW.utf8',
220 '03. February 2013'
221 )
222 );
223 }
224
225 /**
226 * @dataProvider dateViewHelperFormatsDateLocalizedDataProvider
227 *
228 * @test
229 */
230 public function dateViewHelperFormatsDateLocalized($locale, $expected) {
231 $viewHelper = $this->getMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Format\\DateViewHelper', array('renderChildren'));
232 $format = '%d. %B %Y';
233 // 2013-02-03 11:40 UTC
234 $timestamp = '@1359891658';
235
236 if (!setlocale(LC_COLLATE, $locale)) {
237 $this->markTestSkipped('Locale ' . $locale . ' is not available.');
238 }
239 $this->setLocale($locale);
240 $this->assertEquals($expected, $viewHelper->render($timestamp, $format));
241 }
242
243 /**
244 * @param string $locale
245 */
246 protected function setLocale($locale) {
247 setlocale(LC_CTYPE, $locale);
248 setlocale(LC_MONETARY, $locale);
249 setlocale(LC_TIME, $locale);
250 }
251 }