X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fserialization%2Ftests%2Fsrc%2FUnit%2FNormalizer%2FTimestampItemNormalizerTest.php;fp=web%2Fcore%2Fmodules%2Fserialization%2Ftests%2Fsrc%2FUnit%2FNormalizer%2FTimestampItemNormalizerTest.php;h=fd1fc9ce9f3b773df2efe384864202ed1af7fbfd;hb=9917807b03b64faf00f6a1f29dcb6eafc454efa5;hp=0000000000000000000000000000000000000000;hpb=aea91e65e895364e460983b890e295aa5d5540a5;p=yaffs-website diff --git a/web/core/modules/serialization/tests/src/Unit/Normalizer/TimestampItemNormalizerTest.php b/web/core/modules/serialization/tests/src/Unit/Normalizer/TimestampItemNormalizerTest.php new file mode 100644 index 000000000..fd1fc9ce9 --- /dev/null +++ b/web/core/modules/serialization/tests/src/Unit/Normalizer/TimestampItemNormalizerTest.php @@ -0,0 +1,156 @@ +normalizer = new TimestampItemNormalizer(); + } + + /** + * @covers ::supportsNormalization + */ + public function testSupportsNormalization() { + $timestamp_item = $this->createTimestampItemProphecy(); + $this->assertTrue($this->normalizer->supportsNormalization($timestamp_item->reveal())); + + $entity_ref_item = $this->prophesize(EntityReferenceItem::class); + $this->assertFalse($this->normalizer->supportsNormalization($entity_ref_item->reveal())); + } + + /** + * @covers ::supportsDenormalization + */ + public function testSupportsDenormalization() { + $timestamp_item = $this->createTimestampItemProphecy(); + $this->assertTrue($this->normalizer->supportsDenormalization($timestamp_item->reveal(), TimestampItem::class)); + + // CreatedItem extends regular TimestampItem. + $timestamp_item = $this->prophesize(CreatedItem::class); + $this->assertTrue($this->normalizer->supportsDenormalization($timestamp_item->reveal(), TimestampItem::class)); + + $entity_ref_item = $this->prophesize(EntityReferenceItem::class); + $this->assertFalse($this->normalizer->supportsNormalization($entity_ref_item->reveal(), TimestampItem::class)); + } + + /** + * Tests the normalize function. + * + * @covers ::normalize + */ + public function testNormalize() { + $expected = ['value' => '2016-11-06T09:02:00+00:00', 'format' => \DateTime::RFC3339]; + + $timestamp_item = $this->createTimestampItemProphecy(); + $timestamp_item->getIterator() + ->willReturn(new \ArrayIterator(['value' => 1478422920])); + + $serializer = new Serializer(); + $this->normalizer->setSerializer($serializer); + + $normalized = $this->normalizer->normalize($timestamp_item->reveal()); + $this->assertSame($expected, $normalized); + } + + /** + * Tests the denormalize function with good data. + * + * @covers ::denormalize + * @dataProvider providerTestDenormalizeValidFormats + */ + public function testDenormalizeValidFormats($value, $expected) { + $normalized = ['value' => $value]; + + $timestamp_item = $this->createTimestampItemProphecy(); + // The field item should be set with the expected timestamp. + $timestamp_item->setValue(['value' => $expected]) + ->shouldBeCalled(); + + $context = ['target_instance' => $timestamp_item->reveal()]; + + $denormalized = $this->normalizer->denormalize($normalized, TimestampItem::class, NULL, $context); + $this->assertTrue($denormalized instanceof TimestampItem); + } + + /** + * Data provider for testDenormalizeValidFormats. + * + * @return array + */ + public function providerTestDenormalizeValidFormats() { + $expected_stamp = 1478422920; + + $data = []; + + $data['U'] = [$expected_stamp, $expected_stamp]; + $data['RFC3339'] = ['2016-11-06T09:02:00+00:00', $expected_stamp]; + $data['RFC3339 +0100'] = ['2016-11-06T09:02:00+01:00', $expected_stamp - 1 * 3600]; + $data['RFC3339 -0600'] = ['2016-11-06T09:02:00-06:00', $expected_stamp + 6 * 3600]; + + $data['ISO8601'] = ['2016-11-06T09:02:00+0000', $expected_stamp]; + $data['ISO8601 +0100'] = ['2016-11-06T09:02:00+0100', $expected_stamp - 1 * 3600]; + $data['ISO8601 -0600'] = ['2016-11-06T09:02:00-0600', $expected_stamp + 6 * 3600]; + + return $data; + } + + /** + * Tests the denormalize function with bad data. + * + * @covers ::denormalize + */ + public function testDenormalizeException() { + $this->setExpectedException(UnexpectedValueException::class, 'The specified date "2016/11/06 09:02am GMT" is not in an accepted format: "U" (UNIX timestamp), "Y-m-d\TH:i:sO" (ISO 8601), "Y-m-d\TH:i:sP" (RFC 3339).'); + + $context = ['target_instance' => $this->createTimestampItemProphecy()->reveal()]; + + $normalized = ['value' => '2016/11/06 09:02am GMT']; + $this->normalizer->denormalize($normalized, TimestampItem::class, NULL, $context); + } + + /** + * Creates a TimestampItem prophecy. + * + * @return \Prophecy\Prophecy\ObjectProphecy|\Drupal\Core\Field\Plugin\Field\FieldType\TimestampItem + */ + protected function createTimestampItemProphecy() { + $timestamp_item = $this->prophesize(TimestampItem::class); + $timestamp_item->getParent() + ->willReturn(TRUE); + + return $timestamp_item; + } + +}