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