[CLEANUP] Rework/simplify copyright header and remove @package
[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 file is part of the TYPO3 CMS project.
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 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16
17 /**
18 * Test case
19 */
20 class DateTimeConverterTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
21
22 /**
23 * @var \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter
24 */
25 protected $converter;
26
27 public function setUp() {
28 $this->converter = new \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter();
29 }
30
31 /**
32 * @test
33 */
34 public function checkMetadata() {
35 $this->assertEquals(array('string', 'integer', 'array'), $this->converter->getSupportedSourceTypes(), 'Source types do not match');
36 $this->assertEquals('DateTime', $this->converter->getSupportedTargetType(), 'Target type does not match');
37 $this->assertEquals(1, $this->converter->getPriority(), 'Priority does not match');
38 }
39
40
41 /** String to DateTime testcases **/
42
43 /**
44 * @test
45 */
46 public function canConvertFromReturnsFalseIfTargetTypeIsNotDateTime() {
47 $this->assertFalse($this->converter->canConvertFrom('Foo', 'SomeOtherType'));
48 }
49
50 /**
51 * @test
52 */
53 public function canConvertFromReturnsTrueIfSourceTypeIsAString() {
54 $this->assertTrue($this->converter->canConvertFrom('Foo', 'DateTime'));
55 }
56
57 /**
58 * @test
59 */
60 public function canConvertFromReturnsTrueIfSourceTypeIsAnEmptyString() {
61 $this->assertTrue($this->converter->canConvertFrom('', 'DateTime'));
62 }
63
64 /**
65 * @test
66 */
67 public function convertFromReturnsErrorIfGivenStringCantBeConverted() {
68 $error = $this->converter->convertFrom('1980-12-13', 'DateTime');
69 $this->assertInstanceOf('TYPO3\\CMS\\Extbase\\Error\\Error', $error);
70 }
71
72 /**
73 * @test
74 */
75 public function convertFromProperlyConvertsStringWithDefaultDateFormat() {
76 $expectedResult = '1980-12-13T20:15:07+01:23';
77 $date = $this->converter->convertFrom($expectedResult, 'DateTime');
78 $actualResult = $date->format('Y-m-d\\TH:i:sP');
79 $this->assertSame($expectedResult, $actualResult);
80 }
81
82 /**
83 * @test
84 */
85 public function convertFromUsesDefaultDateFormatIfItIsNotConfigured() {
86 $expectedResult = '1980-12-13T20:15:07+01:23';
87 $mockMappingConfiguration = $this->getMock('TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface');
88 $mockMappingConfiguration
89 ->expects($this->atLeastOnce())
90 ->method('getConfigurationValue')
91 ->with('TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter', \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT)
92 ->will($this->returnValue(NULL));
93
94 $date = $this->converter->convertFrom($expectedResult, 'DateTime', array(), $mockMappingConfiguration);
95 $actualResult = $date->format(\TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::DEFAULT_DATE_FORMAT);
96 $this->assertSame($expectedResult, $actualResult);
97 }
98
99 /**
100 * @test
101 */
102 public function convertFromEmptyStringReturnsNull() {
103 $date = $this->converter->convertFrom('', 'DateTime', array(), NULL);
104 $this->assertNull($date);
105 }
106
107 /**
108 * @return array
109 * @see convertFromStringTests()
110 */
111 public function convertFromStringDataProvider() {
112 return array(
113 array('1308174051', '', FALSE),
114 array('13-12-1980', 'd.m.Y', FALSE),
115 array('1308174051', 'Y-m-d', FALSE),
116 array('12:13', 'H:i', TRUE),
117 array('13.12.1980', 'd.m.Y', TRUE),
118 array('2005-08-15T15:52:01+00:00', NULL, TRUE),
119 array('2005-08-15T15:52:01+0000', \DateTime::ISO8601, TRUE),
120 array('1308174051', 'U', TRUE),
121 );
122 }
123
124 /**
125 * @param string $source the string to be converted
126 * @param string $dateFormat the expected date format
127 * @param boolean $isValid TRUE if the conversion is expected to be successful, otherwise FALSE
128 * @test
129 * @dataProvider convertFromStringDataProvider
130 */
131 public function convertFromStringTests($source, $dateFormat, $isValid) {
132 if ($dateFormat !== NULL) {
133 $mockMappingConfiguration = $this->getMock('TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface');
134 $mockMappingConfiguration
135 ->expects($this->atLeastOnce())
136 ->method('getConfigurationValue')
137 ->with('TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter', \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT)
138 ->will($this->returnValue($dateFormat));
139 } else {
140 $mockMappingConfiguration = NULL;
141 }
142 $date = $this->converter->convertFrom($source, 'DateTime', array(), $mockMappingConfiguration);
143 if ($isValid !== TRUE) {
144 $this->assertInstanceOf('TYPO3\\CMS\\Extbase\\Error\\Error', $date);
145 return;
146 }
147 $this->assertInstanceOf('DateTime', $date);
148
149 if ($dateFormat === NULL) {
150 $dateFormat = \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::DEFAULT_DATE_FORMAT;
151 }
152 $this->assertSame($source, $date->format($dateFormat));
153 }
154
155 /**
156 * @return array
157 * @see convertFromIntegerOrDigitStringWithoutConfigurationTests()
158 * @see convertFromIntegerOrDigitStringInArrayWithoutConfigurationTests()
159 */
160 public function convertFromIntegerOrDigitStringsWithoutConfigurationDataProvider() {
161 return array(
162 array('1308174051'),
163 array(1308174051),
164 );
165 }
166
167 /**
168 * @test
169 * @param $source
170 * @dataProvider convertFromIntegerOrDigitStringsWithoutConfigurationDataProvider
171 */
172 public function convertFromIntegerOrDigitStringWithoutConfigurationTests($source) {
173 $date = $this->converter->convertFrom($source, 'DateTime', array(), NULL);
174 $this->assertInstanceOf('DateTime', $date);
175 $this->assertSame(strval($source), $date->format('U'));
176
177 }
178
179 /** Array to DateTime testcases **/
180
181 /**
182 * @test
183 * @param $source
184 * @dataProvider convertFromIntegerOrDigitStringsWithoutConfigurationDataProvider
185 */
186 public function convertFromIntegerOrDigitStringInArrayWithoutConfigurationTests($source) {
187 $date = $this->converter->convertFrom(array('date' => $source), 'DateTime', array(), NULL);
188 $this->assertInstanceOf('DateTime', $date);
189 $this->assertSame(strval($source), $date->format('U'));
190
191 }
192
193 /**
194 * @test
195 */
196 public function canConvertFromReturnsTrueIfSourceTypeIsAnArray() {
197 $this->assertTrue($this->converter->canConvertFrom(array(), 'DateTime'));
198 }
199
200 /**
201 * @test
202 */
203 public function convertFromReturnsErrorIfGivenArrayCantBeConverted() {
204 $error = $this->converter->convertFrom(array('date' => '1980-12-13'), 'DateTime');
205 $this->assertInstanceOf('TYPO3\\CMS\\Extbase\\Error\\Error', $error);
206 }
207
208 /**
209 * @test
210 * @expectedException \TYPO3\CMS\Extbase\Property\Exception\TypeConverterException
211 */
212 public function convertFromThrowsExceptionIfGivenArrayDoesNotSpecifyTheDate() {
213 $this->converter->convertFrom(array('hour' => '12', 'minute' => '30'), 'DateTime');
214 }
215
216 /**
217 * @test
218 */
219 public function convertFromProperlyConvertsArrayWithDefaultDateFormat() {
220 $expectedResult = '1980-12-13T20:15:07+01:23';
221 $date = $this->converter->convertFrom(array('date' => $expectedResult), 'DateTime');
222 $actualResult = $date->format('Y-m-d\\TH:i:sP');
223 $this->assertSame($expectedResult, $actualResult);
224 }
225
226 /**
227 * @return array
228 * @see convertFromThrowsExceptionIfDatePartKeysHaveInvalidValuesSpecified
229 */
230 public function invalidDatePartKeyValuesDataProvider() {
231 return array(
232 array(array('day' => '13.0', 'month' => '10', 'year' => '2010')),
233 array(array('day' => '13', 'month' => '10.0', 'year' => '2010')),
234 array(array('day' => '13', 'month' => '10', 'year' => '2010.0')),
235 array(array('day' => '-13', 'month' => '10', 'year' => '2010')),
236 array(array('day' => '13', 'month' => '-10', 'year' => '2010')),
237 array(array('day' => '13', 'month' => '10', 'year' => '-2010')),
238 );
239 }
240
241 /**
242 * @test
243 * @expectedException \TYPO3\CMS\Extbase\Property\Exception\TypeConverterException
244 * @dataProvider invalidDatePartKeyValuesDataProvider
245 */
246 public function convertFromThrowsExceptionIfDatePartKeysHaveInvalidValuesSpecified($source) {
247 $this->converter->convertFrom($source, 'DateTime');
248 }
249
250 /**
251 * @test
252 */
253 public function convertFromProperlyConvertsArrayWithDateAsArray() {
254 $source = array('day' => '13', 'month' => '10', 'year' => '2010');
255 $mappingConfiguration = new \TYPO3\CMS\Extbase\Property\PropertyMappingConfiguration();
256 $mappingConfiguration->setTypeConverterOption(
257 'TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter',
258 \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT,
259 'Y-m-d'
260 );
261
262 $date = $this->converter->convertFrom($source, 'DateTime', array(), $mappingConfiguration);
263 $actualResult = $date->format('Y-m-d');
264 $this->assertSame('2010-10-13', $actualResult);
265 }
266
267 /**
268 * @test
269 */
270 public function convertFromAllowsToOverrideTheTime() {
271 $source = array(
272 'date' => '2011-06-16',
273 'dateFormat' => 'Y-m-d',
274 'hour' => '12',
275 'minute' => '30',
276 'second' => '59',
277 );
278 $date = $this->converter->convertFrom($source, 'DateTime');
279 $this->assertSame('2011-06-16', $date->format('Y-m-d'));
280 $this->assertSame('12', $date->format('H'));
281 $this->assertSame('30', $date->format('i'));
282 $this->assertSame('59', $date->format('s'));
283 }
284
285 /**
286 * @test
287 */
288 public function convertFromAllowsToOverrideTheTimezone() {
289 $source = array(
290 'date' => '2011-06-16 12:30:59',
291 'dateFormat' => 'Y-m-d H:i:s',
292 'timezone' => 'Atlantic/Reykjavik',
293 );
294 $date = $this->converter->convertFrom($source, 'DateTime');
295 $this->assertSame('2011-06-16', $date->format('Y-m-d'));
296 $this->assertSame('12', $date->format('H'));
297 $this->assertSame('30', $date->format('i'));
298 $this->assertSame('59', $date->format('s'));
299 $this->assertSame('Atlantic/Reykjavik', $date->getTimezone()->getName());
300 }
301
302 /**
303 * @test
304 * @expectedException \TYPO3\CMS\Extbase\Property\Exception\TypeConverterException
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 */
319 public function convertFromArrayThrowsExceptionForEmptyArray() {
320 $this->converter->convertFrom(array(), 'DateTime', array(), NULL);
321 }
322
323 /**
324 * @test
325 */
326 public function convertFromArrayReturnsNullForEmptyDate() {
327 $this->assertNull($this->converter->convertFrom(array('date' => ''), 'DateTime', array(), NULL));
328 }
329
330 /**
331 * @return array
332 * @see convertFromArrayTests()
333 */
334 public function convertFromArrayDataProvider() {
335 return array(
336 array(array('date' => '2005-08-15T15:52:01+01:00'), TRUE),
337 array(array('date' => '1308174051', 'dateFormat' => ''), FALSE),
338 array(array('date' => '13-12-1980', 'dateFormat' => 'd.m.Y'), FALSE),
339 array(array('date' => '1308174051', 'dateFormat' => 'Y-m-d'), FALSE),
340 array(array('date' => '12:13', 'dateFormat' => 'H:i'), TRUE),
341 array(array('date' => '13.12.1980', 'dateFormat' => 'd.m.Y'), TRUE),
342 array(array('date' => '2005-08-15T15:52:01+00:00', 'dateFormat' => ''), TRUE),
343 array(array('date' => '2005-08-15T15:52:01+0000', 'dateFormat' => \DateTime::ISO8601), TRUE),
344 array(array('date' => '1308174051', 'dateFormat' => 'U'), TRUE),
345 array(array('date' => 1308174051, 'dateFormat' => 'U'), TRUE),
346 );
347 }
348
349 /**
350 * @param array $source the array to be converted
351 * @param boolean $isValid TRUE if the conversion is expected to be successful, otherwise FALSE
352 * @test
353 * @dataProvider convertFromArrayDataProvider
354 */
355 public function convertFromArrayTests(array $source, $isValid) {
356 $dateFormat = isset($source['dateFormat']) && strlen($source['dateFormat']) > 0 ? $source['dateFormat'] : NULL;
357 if ($dateFormat !== NULL) {
358 $mockMappingConfiguration = $this->getMock('TYPO3\CMS\Extbase\Property\PropertyMappingConfigurationInterface');
359 $mockMappingConfiguration
360 ->expects($this->atLeastOnce())
361 ->method('getConfigurationValue')
362 ->with('TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter', \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::CONFIGURATION_DATE_FORMAT)
363 ->will($this->returnValue($dateFormat));
364 } else {
365 $mockMappingConfiguration = NULL;
366 }
367 $date = $this->converter->convertFrom($source, 'DateTime', array(), $mockMappingConfiguration);
368
369 if ($isValid !== TRUE) {
370 $this->assertInstanceOf('TYPO3\\CMS\\Extbase\\Error\\Error', $date);
371 return;
372 }
373
374 $this->assertInstanceOf('DateTime', $date);
375 if ($dateFormat === NULL) {
376 $dateFormat = \TYPO3\CMS\Extbase\Property\TypeConverter\DateTimeConverter::DEFAULT_DATE_FORMAT;
377 }
378 $dateAsString = isset($source['date']) ? strval($source['date']) : '';
379 $this->assertSame($dateAsString, $date->format($dateFormat));
380 }
381
382 /**
383 * @test
384 */
385 public function convertFromSupportsDateTimeSubClasses() {
386 $className = $this->getUniqueId('DateTimeSubClass');
387 eval('
388 class ' . $className . ' extends \\DateTime {
389 public static function createFromFormat($format, $time, $timezone = NULL) {
390 return new ' . $className . '();
391 }
392 public function foo() { return "Bar"; }
393 }
394 ');
395 $date = $this->converter->convertFrom('2005-08-15T15:52:01+00:00', $className);
396
397 $this->assertInstanceOf($className, $date);
398 $this->assertSame('Bar', $date->foo());
399 }
400
401 }