[BUGFIX] Date ViewHelper not using configured Timezones
[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 FLOW3 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 class DateViewHelperTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
14
15 /**
16 * @var array Backup of current locale, it is manipulated in tests
17 */
18 protected $backupLocales = array();
19
20 /**
21 * @var string Backup of current timezone, it is manipulated in tests
22 */
23 protected $timezone;
24
25 public function setUp() {
26 parent::setUp();
27 // Store all locale categories manipulated in tests for reconstruction in tearDown
28 $this->backupLocales = array(
29 'LC_COLLATE' => setlocale(LC_COLLATE, 0),
30 'LC_CTYPE' => setlocale(LC_CTYPE, 0),
31 'LC_MONETARY' => setlocale(LC_MONETARY, 0),
32 'LC_TIME' => setlocale(LC_TIME, 0),
33 );
34 $this->timezone = @date_default_timezone_get();
35 }
36
37 public function tearDown() {
38 foreach ($this->backupLocales as $category => $locale) {
39 setlocale(constant($category), $locale);
40 }
41 date_default_timezone_set($this->timezone);
42 }
43
44 /**
45 * @test
46 */
47 public function viewHelperFormatsDateCorrectly() {
48 $viewHelper = new \TYPO3\CMS\Fluid\ViewHelpers\Format\DateViewHelper();
49 $actualResult = $viewHelper->render(new \DateTime('1980-12-13'));
50 $this->assertEquals('1980-12-13', $actualResult);
51 }
52
53 /**
54 * @test
55 */
56 public function viewHelperFormatsDateStringCorrectly() {
57 $viewHelper = new \TYPO3\CMS\Fluid\ViewHelpers\Format\DateViewHelper();
58 $actualResult = $viewHelper->render('1980-12-13');
59 $this->assertEquals('1980-12-13', $actualResult);
60 }
61
62 /**
63 * @test
64 */
65 public function viewHelperRespectsCustomFormat() {
66 $viewHelper = new \TYPO3\CMS\Fluid\ViewHelpers\Format\DateViewHelper();
67 $actualResult = $viewHelper->render(new \DateTime('1980-02-01'), 'd.m.Y');
68 $this->assertEquals('01.02.1980', $actualResult);
69 }
70
71 /**
72 * @test
73 */
74 public function viewHelperReturnsEmptyStringIfNULLIsGiven() {
75 $viewHelper = $this->getMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Format\\DateViewHelper', array('renderChildren'));
76 $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue(NULL));
77 $actualResult = $viewHelper->render();
78 $this->assertEquals('', $actualResult);
79 }
80
81 /**
82 * @test
83 * @expectedException \TYPO3\CMS\Fluid\Core\ViewHelper\Exception
84 */
85 public function viewHelperThrowsExceptionIfDateStringCantBeParsed() {
86 $viewHelper = new \TYPO3\CMS\Fluid\ViewHelpers\Format\DateViewHelper();
87 $viewHelper->render('foo');
88 }
89
90 /**
91 * @test
92 */
93 public function viewHelperUsesChildNodesIfDateAttributeIsNotSpecified() {
94 $viewHelper = $this->getMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Format\\DateViewHelper', array('renderChildren'));
95 $viewHelper->expects($this->once())->method('renderChildren')->will($this->returnValue(new \DateTime('1980-12-13')));
96 $actualResult = $viewHelper->render();
97 $this->assertEquals('1980-12-13', $actualResult);
98 }
99
100 /**
101 * @test
102 */
103 public function dateArgumentHasPriorityOverChildNodes() {
104 $viewHelper = $this->getMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Format\\DateViewHelper', array('renderChildren'));
105 $viewHelper->expects($this->never())->method('renderChildren');
106 $actualResult = $viewHelper->render('1980-12-12');
107 $this->assertEquals('1980-12-12', $actualResult);
108 }
109
110 /**
111 * @test
112 */
113 public function viewHelperRespectsDefaultTimezoneForIntegerTimestamp() {
114 $viewHelper = $this->getMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Format\\DateViewHelper', array('renderChildren'));
115
116 $date = 1359891658; // 2013-02-03 11:40 UTC
117 $format = 'Y-m-d H:i';
118
119 date_default_timezone_set('Europa/Berlin');
120 $expected = '2013-02-03 12:40';
121 $this->assertEquals($expected, $viewHelper->render($date, $format));
122
123 date_default_timezone_set('Asia/Riyadh');
124 $expected = '2013-02-03 14:40';
125 $this->assertEquals($expected, $viewHelper->render($date, $format));
126 }
127
128 /**
129 * @test
130 */
131 public function viewHelperRespectsDefaultTimezoneForStringTimestamp() {
132 $viewHelper = $this->getMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Format\\DateViewHelper', array('renderChildren'));
133
134 $format = 'Y-m-d H:i';
135
136 date_default_timezone_set('Europa/Berlin');
137 $date = '@1359891658'; // 2013-02-03 11:40 UTC
138 $expected = '2013-02-03 12:40';
139 $this->assertEquals($expected, $viewHelper->render($date, $format));
140
141 $date = '03/Oct/2000:14:55:36 +0400'; // Moscow
142 $expected = '2000-10-03 12:55';
143 $this->assertEquals($expected, $viewHelper->render($date, $format));
144
145 date_default_timezone_set('Asia/Riyadh');
146 $date = '@1359891658'; // 2013-02-03 11:40 UTC
147 $expected = '2013-02-03 14:40';
148 $this->assertEquals($expected, $viewHelper->render($date, $format));
149
150 $date = '03/Oct/2000:14:55:36 +0400'; // Moscow
151 $expected = '2000-10-03 13:55';
152 $this->assertEquals($expected, $viewHelper->render($date, $format));
153 }
154
155 /**
156 * @test
157 */
158 public function dateViewHelperFormatsDateLocalized() {
159 $viewHelper = $this->getMock('TYPO3\\CMS\\Fluid\\ViewHelpers\\Format\\DateViewHelper', array('renderChildren'));
160 $format = '%d. %B %Y';
161 $timestamp = '@1359891658'; // 2013-02-03 11:40 UTC
162
163 $locale = 'de_DE.UTF-8';
164 if (!setlocale(LC_COLLATE, $locale)) {
165 $this->markTestSkipped('Locale ' . $locale . ' is not available.');
166 }
167 $this->setLocale($locale);
168 $expected = '03. Februar 2013';
169 $this->assertEquals($expected, $viewHelper->render($timestamp, $format));
170
171 $locale = 'en_ZW.utf8';
172 if (!setlocale(LC_COLLATE, $locale)) {
173 $this->markTestSkipped('Locale ' . $locale . ' is not available.');
174 }
175 $this->setLocale($locale);
176 $expected = '03. February 2013';
177 $this->assertEquals($expected, $viewHelper->render($timestamp, $format));
178 }
179
180 protected function setLocale($locale) {
181 setlocale(LC_CTYPE, $locale);
182 setlocale(LC_MONETARY, $locale);
183 setlocale(LC_TIME, $locale);
184 }
185 }
186
187 ?>