.../Normalizer/DateTimeIso8601NormalizerTest.php | 48 +++++++++++++++++----- 1 file changed, 38 insertions(+), 10 deletions(-) diff --git a/core/modules/serialization/tests/src/Unit/Normalizer/DateTimeIso8601NormalizerTest.php b/core/modules/serialization/tests/src/Unit/Normalizer/DateTimeIso8601NormalizerTest.php index b3cf2ae..ffd3f23 100644 --- a/core/modules/serialization/tests/src/Unit/Normalizer/DateTimeIso8601NormalizerTest.php +++ b/core/modules/serialization/tests/src/Unit/Normalizer/DateTimeIso8601NormalizerTest.php @@ -11,6 +11,7 @@ use Drupal\Core\TypedData\Plugin\DataType\IntegerData; use Drupal\Core\TypedData\Type\DateTimeInterface; use Drupal\datetime\Plugin\Field\FieldType\DateTimeItem; +use Drupal\datetime\Plugin\Field\FieldType\DateTimeItemInterface; use Drupal\serialization\Normalizer\DateTimeIso8601Normalizer; use Drupal\Tests\UnitTestCase; use Prophecy\Argument; @@ -187,12 +188,13 @@ public function providerTestNormalize() { * @covers ::denormalize * @dataProvider providerTestDenormalizeValidFormats */ - public function testDenormalizeValidFormats($normalized, $expected) { - $denormalized = $this->normalizer->denormalize($normalized, DateTimeIso8601::class, NULL, []); - $this->assertInstanceOf(\DateTime::class, $denormalized); - $this->assertEquals('UTC', $denormalized->getTimezone()->getName()); - $this->assertEquals('12:00:00', $denormalized->format('H:i:s')); - $this->assertEquals($expected->format(\DateTime::RFC3339), $denormalized->format(\DateTime::RFC3339)); + public function testDenormalizeValidFormats($type, $normalized, $expected) { + $field_definition = $this->prophesize(FieldDefinitionInterface::class); + $field_definition->getSetting('datetime_type')->willReturn($type === 'date-only' ? DateTimeItem::DATETIME_TYPE_DATE : DateTimeItem::DATETIME_TYPE_DATETIME); + $denormalized = $this->normalizer->denormalize($normalized, DateTimeIso8601::class, NULL, [ + 'field_definition' => $field_definition->reveal(), + ]); + $this->assertSame($expected, $denormalized); } /** @@ -202,21 +204,47 @@ public function testDenormalizeValidFormats($normalized, $expected) { */ public function providerTestDenormalizeValidFormats() { $data = []; - $data['denormalized dates have the UTC timezone'] = ['2016-11-06', new \DateTimeImmutable('2016-11-06T12:00:00', new \DateTimeZone('UTC'))]; + $data['just a date'] = ['date-only', '2016-11-06', '2016-11-06']; + + $data['RFC3339'] = ['date+time', '2016-11-06T09:02:00+00:00', '2016-11-06T09:02:00']; + $data['RFC3339 +0100'] = ['date+time', '2016-11-06T09:02:00+01:00', '2016-11-06T08:02:00']; + $data['RFC3339 -0600'] = ['date+time', '2016-11-06T09:02:00-06:00', '2016-11-06T15:02:00']; + + $data['ISO8601'] = ['date+time', '2016-11-06T09:02:00+0000', '2016-11-06T09:02:00']; + $data['ISO8601 +0100'] = ['date+time', '2016-11-06T09:02:00+0100', '2016-11-06T08:02:00']; + $data['ISO8601 -0600'] = ['date+time', '2016-11-06T09:02:00-0600', '2016-11-06T15:02:00']; + return $data; } /** - * Tests the denormalize function with bad data. + * Tests the denormalize function with bad data for the date-only case. * * @covers ::denormalize */ - public function testDenormalizeException() { + public function testDenormalizeDateOnlyException() { $this->setExpectedException(UnexpectedValueException::class, 'The specified date "2016/11/06" is not in an accepted format: "Y-m-d" (date-only).'); $normalized = '2016/11/06'; - $this->normalizer->denormalize($normalized, DateTimeIso8601::class, NULL, []); + $field_definition = $this->prophesize(FieldDefinitionInterface::class); + $field_definition->getSetting('datetime_type')->willReturn(DateTimeItem::DATETIME_TYPE_DATE); + $this->normalizer->denormalize($normalized, DateTimeIso8601::class, NULL, ['field_definition' => $field_definition->reveal()]); + } + + /** + * Tests the denormalize function with bad data for the date+time case. + * + * @covers ::denormalize + */ + public function testDenormalizeDateAndTimeException() { + $this->setExpectedException(UnexpectedValueException::class, 'The specified date "2016-11-06T08:00:00" is not in an accepted format: "Y-m-d\TH:i:sP" (RFC 3339), "Y-m-d\TH:i:sO" (ISO 8601).'); + + $normalized = '2016-11-06T08:00:00'; + + $field_definition = $this->prophesize(FieldDefinitionInterface::class); + $field_definition->getSetting('datetime_type')->willReturn(DateTimeItem::DATETIME_TYPE_DATETIME); + $this->normalizer->denormalize($normalized, DateTimeIso8601::class, NULL, ['field_definition' => $field_definition->reveal()]); } }