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