fdf836e485927e1db55dc821bae30abbc1907c99
[Packages/TYPO3.CMS.git] / typo3 / sysext / extbase / Tests / Unit / Property / TypeConverter / DateTimeConverterTest.php
1 <?php
2 namespace TYPO3\CMS\Extbase\Tests\Unit\Property\TypeConverter;
3
4 /* *
5 * This script belongs to the Extbase framework. *
6 * *
7 * It is free software; you can redistribute it and/or modify it under *
8 * the terms of the GNU Lesser General Public License as published by the *
9 * Free Software Foundation, either version 3 of the License, or (at your *
10 * option) any later version. *
11 * *
12 * This script is distributed in the hope that it will be useful, but *
13 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHAN- *
14 * TABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser *
15 * General Public License for more details. *
16 * *
17 * You should have received a copy of the GNU Lesser General Public *
18 * License along with the script. *
19 * If not, see http://www.gnu.org/licenses/lgpl.html *
20 * *
21 * The TYPO3 project - inspiring people to share! *
22 * */
23 /**
24 * Testcase for the DateTime converter
25 *
26 * @license http://www.gnu.org/licenses/lgpl.html GNU Lesser General Public License, version 3 or later
27 * @covers \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter<extended>
28 */
29 class DateTimeConverterTest extends \TYPO3\CMS\Extbase\Tests\Unit\BaseTestCase {
30
31 /**
32 * @var \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter
33 */
34 protected $converter;
35
36 public function setUp() {
37 $this->converter = new \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter();
38 }
39
40 /**
41 * @test
42 * @author Bastian Waidelich <bastian@typo3.org>
43 */
44 public function checkMetadata() {
45 $this->assertEquals(array('string', 'integer', 'array'), $this->converter->getSupportedSourceTypes(), 'Source types do not match');
46 $this->assertEquals('DateTime', $this->converter->getSupportedTargetType(), 'Target type does not match');
47 $this->assertEquals(1, $this->converter->getPriority(), 'Priority does not match');
48 }
49
50 /**
51 * String to DateTime testcases *
52 */
53 /**
54 * @test
55 * @author Bastian Waidelich <bastian@typo3.org>
56 */
57 public function canConvertFromReturnsFalseIfTargetTypeIsNotDateTime() {
58 $this->assertFalse($this->converter->canConvertFrom('Foo', 'SomeOtherType'));
59 }
60
61 /**
62 * @test
63 * @author Bastian Waidelich <bastian@typo3.org>
64 */
65 public function canConvertFromReturnsTrueIfSourceTypeIsAString() {
66 $this->assertTrue($this->converter->canConvertFrom('Foo', 'DateTime'));
67 }
68
69 /**
70 * @test
71 * @author Bastian Waidelich <bastian@typo3.org>
72 */
73 public function canConvertFromReturnsTrueIfSourceTypeIsAnEmptyString() {
74 $this->assertTrue($this->converter->canConvertFrom('', 'DateTime'));
75 }
76
77 /**
78 * @test
79 * @author Bastian Waidelich <bastian@typo3.org>
80 */
81 public function convertFromReturnsErrorIfGivenStringCantBeConverted() {
82 $error = $this->converter->convertFrom('1980-12-13', 'DateTime');
83 $this->assertInstanceOf('TYPO3\\CMS\\Extbase\\Error\\Error', $error);
84 }
85
86 /**
87 * @test
88 * @author Bastian Waidelich <bastian@typo3.org>
89 */
90 public function convertFromProperlyConvertsStringWithDefaultDateFormat() {
91 $expectedResult = '1980-12-13T20:15:07+01:23';
92 $date = $this->converter->convertFrom($expectedResult, 'DateTime');
93 $actualResult = $date->format('Y-m-d\\TH:i:sP');
94 $this->assertSame($expectedResult, $actualResult);
95 }
96
97 /**
98 * @test
99 * @author Bastian Waidelich <bastian@typo3.org>
100 */
101 public function convertFromUsesDefaultDateFormatIfItIsNotConfigured() {
102 $expectedResult = '1980-12-13T20:15:07+01:23';
103 $mockMappingConfiguration = $this->getMock('TYPO3\\CMS\\Extbase\\Property\\PropertyMappingConfigurationInterface');
104 $mockMappingConfiguration->expects($this->atLeastOnce())->method('getConfigurationValue')->with('TYPO3\\CMS\\Extbase\\Property\\TypeConverter\\DateTimeConverter', \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT)->will($this->returnValue(NULL));
105 $date = $this->converter->convertFrom($expectedResult, 'DateTime', array(), $mockMappingConfiguration);
106 $actualResult = $date->format(\TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::DEFAULT_DATE_FORMAT);
107 $this->assertSame($expectedResult, $actualResult);
108 }
109
110 /**
111 * @test
112 */
113 public function convertFromEmptyStringReturnsNull() {
114 $date = $this->converter->convertFrom('', 'DateTime', array(), NULL);
115 $this->assertNull($date);
116 }
117
118 /**
119 * @return array
120 * @see convertFromStringTests()
121 * @author Bastian Waidelich <bastian@typo3.org>
122 */
123 public function convertFromStringDataProvider() {
124 return array(
125 array('1308174051', '', FALSE),
126 array('13-12-1980', 'd.m.Y', FALSE),
127 array('1308174051', 'Y-m-d', FALSE),
128 array('12:13', 'H:i', TRUE),
129 array('13.12.1980', 'd.m.Y', TRUE),
130 array('2005-08-15T15:52:01+00:00', NULL, TRUE),
131 array('2005-08-15T15:52:01+0000', \DateTime::ISO8601, TRUE),
132 array('1308174051', 'U', TRUE),
133 );
134 }
135
136 /**
137 * @param string $source the string to be converted
138 * @param string $dateFormat the expected date format
139 * @param boolean $isValid TRUE if the conversion is expected to be successful, otherwise FALSE
140 * @test
141 * @dataProvider convertFromStringDataProvider
142 * @author Bastian Waidelich <bastian@typo3.org>
143 */
144 public function convertFromStringTests($source, $dateFormat, $isValid) {
145 if ($dateFormat !== NULL) {
146 $mockMappingConfiguration = $this->getMock('TYPO3\\CMS\\Extbase\\Property\\PropertyMappingConfigurationInterface');
147 $mockMappingConfiguration->expects($this->atLeastOnce())->method('getConfigurationValue')->with('TYPO3\\CMS\\Extbase\\Property\\TypeConverter\\DateTimeConverter', \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT)->will($this->returnValue($dateFormat));
148 } else {
149 $mockMappingConfiguration = NULL;
150 }
151 $date = $this->converter->convertFrom($source, 'DateTime', array(), $mockMappingConfiguration);
152 if ($isValid !== TRUE) {
153 $this->assertInstanceOf('TYPO3\\CMS\\Extbase\\Error\\Error', $date);
154 return;
155 }
156 $this->assertInstanceOf('DateTime', $date);
157 if ($dateFormat === NULL) {
158 $dateFormat = \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::DEFAULT_DATE_FORMAT;
159 }
160 $this->assertSame($source, $date->format($dateFormat));
161 }
162
163 /**
164 * @return array
165 * @see convertFromIntegerOrDigitStringWithoutConfigurationTests()
166 * @see convertFromIntegerOrDigitStringInArrayWithoutConfigurationTests()
167 */
168 public function convertFromIntegerOrDigitStringsWithoutConfigurationDataProvider() {
169 return array(
170 array('1308174051'),
171 array(1308174051),
172 );
173 }
174
175 /**
176 * @test
177 * @param $source
178 * @dataProvider convertFromIntegerOrDigitStringsWithoutConfigurationDataProvider
179 */
180 public function convertFromIntegerOrDigitStringWithoutConfigurationTests($source) {
181 $date = $this->converter->convertFrom($source, 'DateTime', array(), NULL);
182 $this->assertInstanceOf('DateTime', $date);
183 $this->assertSame(strval($source), $date->format('U'));
184
185 }
186
187 /**
188 * Array to DateTime testcases *
189 */
190
191 /**
192 * @test
193 * @param $source
194 * @dataProvider convertFromIntegerOrDigitStringsWithoutConfigurationDataProvider
195 */
196 public function convertFromIntegerOrDigitStringInArrayWithoutConfigurationTests($source) {
197 $date = $this->converter->convertFrom(array('date' => $source), 'DateTime', array(), NULL);
198 $this->assertInstanceOf('DateTime', $date);
199 $this->assertSame(strval($source), $date->format('U'));
200
201 }
202
203 /**
204 * @test
205 * @author Bastian Waidelich <bastian@typo3.org>
206 */
207 public function canConvertFromReturnsTrueIfSourceTypeIsAnArray() {
208 $this->assertTrue($this->converter->canConvertFrom(array(), 'DateTime'));
209 }
210
211 /**
212 * @test
213 * @author Bastian Waidelich <bastian@typo3.org>
214 */
215 public function convertFromReturnsErrorIfGivenArrayCantBeConverted() {
216 $error = $this->converter->convertFrom(array('date' => '1980-12-13'), 'DateTime');
217 $this->assertInstanceOf('TYPO3\\CMS\\Extbase\\Error\\Error', $error);
218 }
219
220 /**
221 * @test
222 * @expectedException \TYPO3\CMS\Extbase\Property\Exception\TypeConverterException
223 * @author Bastian Waidelich <bastian@typo3.org>
224 */
225 public function convertFromThrowsExceptionIfGivenArrayDoesNotSpecifyTheDate() {
226 $this->converter->convertFrom(array('hour' => '12', 'minute' => '30'), 'DateTime');
227 }
228
229 /**
230 * @test
231 * @author Bastian Waidelich <bastian@typo3.org>
232 */
233 public function convertFromProperlyConvertsArrayWithDefaultDateFormat() {
234 $expectedResult = '1980-12-13T20:15:07+01:23';
235 $date = $this->converter->convertFrom(array('date' => $expectedResult), 'DateTime');
236 $actualResult = $date->format('Y-m-d\\TH:i:sP');
237 $this->assertSame($expectedResult, $actualResult);
238 }
239
240 /**
241 * @return array
242 * @see convertFromThrowsExceptionIfDatePartKeysHaveInvalidValuesSpecified
243 */
244 public function invalidDatePartKeyValuesDataProvider() {
245 return array(
246 array(array('day' => '13.0', 'month' => '10', 'year' => '2010')),
247 array(array('day' => '13', 'month' => '10.0', 'year' => '2010')),
248 array(array('day' => '13', 'month' => '10', 'year' => '2010.0')),
249 array(array('day' => '-13', 'month' => '10', 'year' => '2010')),
250 array(array('day' => '13', 'month' => '-10', 'year' => '2010')),
251 array(array('day' => '13', 'month' => '10', 'year' => '-2010')),
252 );
253 }
254
255 /**
256 * @test
257 * @expectedException \TYPO3\CMS\Extbase\Property\Exception\TypeConverterException
258 * @dataProvider invalidDatePartKeyValuesDataProvider
259 */
260 public function convertFromThrowsExceptionIfDatePartKeysHaveInvalidValuesSpecified($source) {
261 $this->converter->convertFrom($source, 'DateTime');
262 }
263
264 /**
265 * @test
266 * @author Bastian Waidelich <bastian@typo3.org>
267 */
268 public function convertFromAllowsToOverrideTheTime() {
269 $source = array(
270 'date' => '2011-06-16',
271 'dateFormat' => 'Y-m-d',
272 'hour' => '12',
273 'minute' => '30',
274 'second' => '59'
275 );
276 $date = $this->converter->convertFrom($source, 'DateTime');
277 $this->assertSame('2011-06-16', $date->format('Y-m-d'));
278 $this->assertSame('12', $date->format('H'));
279 $this->assertSame('30', $date->format('i'));
280 $this->assertSame('59', $date->format('s'));
281 }
282
283 /**
284 * @test
285 * @author Bastian Waidelich <bastian@typo3.org>
286 */
287 public function convertFromAllowsToOverrideTheTimezone() {
288 $source = array(
289 'date' => '2011-06-16 12:30:59',
290 'dateFormat' => 'Y-m-d H:i:s',
291 'timezone' => 'Atlantic/Reykjavik'
292 );
293 $date = $this->converter->convertFrom($source, 'DateTime');
294 $this->assertSame('2011-06-16', $date->format('Y-m-d'));
295 $this->assertSame('12', $date->format('H'));
296 $this->assertSame('30', $date->format('i'));
297 $this->assertSame('59', $date->format('s'));
298 $this->assertSame('Atlantic/Reykjavik', $date->getTimezone()->getName());
299 }
300
301 /**
302 * @test
303 * @expectedException \TYPO3\CMS\Extbase\Property\Exception\TypeConverterException
304 * @author Bastian Waidelich <bastian@typo3.org>
305 */
306 public function convertFromThrowsExceptionIfSpecifiedTimezoneIsInvalid() {
307 $source = array(
308 'date' => '2011-06-16',
309 'dateFormat' => 'Y-m-d',
310 'timezone' => 'Invalid/Timezone'
311 );
312 $this->converter->convertFrom($source, 'DateTime');
313 }
314
315 /**
316 * @test
317 * @expectedException \TYPO3\CMS\Extbase\Property\Exception\TypeConverterException
318 * @author Sebastian Kurfürst <sebastian@typo3.org>
319 */
320 public function convertFromArrayThrowsExceptionForEmptyArray() {
321 $this->converter->convertFrom(array(), 'DateTime', array(), NULL);
322 }
323
324 /**
325 * @test
326 * @author Sebastian Kurfürst <sebastian@typo3.org>
327 */
328 public function convertFromArrayReturnsNullForEmptyDate() {
329 $this->assertNull($this->converter->convertFrom(array('date' => ''), 'DateTime', array(), NULL));
330 }
331
332 /**
333 * @return array
334 * @see convertFromArrayTests()
335 * @author Bastian Waidelich <bastian@typo3.org>
336 */
337 public function convertFromArrayDataProvider() {
338 return array(
339 array(array('date' => '2005-08-15T15:52:01+01:00'), TRUE),
340 array(array('date' => '1308174051', 'dateFormat' => ''), FALSE),
341 array(array('date' => '13-12-1980', 'dateFormat' => 'd.m.Y'), FALSE),
342 array(array('date' => '1308174051', 'dateFormat' => 'Y-m-d'), FALSE),
343 array(array('date' => '12:13', 'dateFormat' => 'H:i'), TRUE),
344 array(array('date' => '13.12.1980', 'dateFormat' => 'd.m.Y'), TRUE),
345 array(array('date' => '2005-08-15T15:52:01+00:00', 'dateFormat' => ''), TRUE),
346 array(array('date' => '2005-08-15T15:52:01+0000', 'dateFormat' => \DateTime::ISO8601), TRUE),
347 array(array('date' => '1308174051', 'dateFormat' => 'U'), TRUE),
348 array(array('date' => 1308174051, 'dateFormat' => 'U'), TRUE),
349 );
350 }
351
352 /**
353 * @param array $source the array to be converted
354 * @param boolean $isValid TRUE if the conversion is expected to be successful, otherwise FALSE
355 * @test
356 * @dataProvider convertFromArrayDataProvider
357 * @author Bastian Waidelich <bastian@typo3.org>
358 */
359 public function convertFromArrayTests(array $source, $isValid) {
360 $dateFormat = isset($source['dateFormat']) && strlen($source['dateFormat']) > 0 ? $source['dateFormat'] : NULL;
361 if ($dateFormat !== NULL) {
362 $mockMappingConfiguration = $this->getMock('TYPO3\\CMS\\Extbase\\Property\\PropertyMappingConfigurationInterface');
363 $mockMappingConfiguration->expects($this->atLeastOnce())->method('getConfigurationValue')->with('TYPO3\\CMS\\Extbase\\Property\\TypeConverter\\DateTimeConverter', \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT)->will($this->returnValue($dateFormat));
364 } else {
365 $mockMappingConfiguration = NULL;
366 }
367 $date = $this->converter->convertFrom($source, 'DateTime', array(), $mockMappingConfiguration);
368 if ($isValid !== TRUE) {
369 $this->assertInstanceOf('TYPO3\\CMS\\Extbase\\Error\\Error', $date);
370 return;
371 }
372 $this->assertInstanceOf('DateTime', $date);
373 if ($dateFormat === NULL) {
374 $dateFormat = \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::DEFAULT_DATE_FORMAT;
375 }
376 $dateAsString = isset($source['date']) ? $source['date'] : '';
377 $this->assertSame(strval($dateAsString), $date->format($dateFormat));
378 }
379 }
380
381 ?>