2c2 < index 98a190b..4118774 100644 --- > index 3e47b00..d47308e 100644 5,8c5,8 < @@ -99,3 +99,11 @@ services: < serialization.link_manager.relation: < class: Drupal\serialization\LinkManager\RelationLinkManager < arguments: ['@cache.default', '@entity.manager', '@module_handler', '@config.factory', '@request_stack'] --- > @@ -64,6 +64,14 @@ services: > class: Drupal\serialization\Normalizer\TypedDataNormalizer > tags: > - { name: normalizer } 16a17,19 > serializer.encoder.json: > class: Drupal\serialization\Encoder\JsonEncoder > tags: 19c22 < index 0000000..955a32d --- > index 0000000..fb136db 22c25 < @@ -0,0 +1,33 @@ --- > @@ -0,0 +1,34 @@ 44c47 < + public function normalize($object, $format = NULL, array $context = array()) { --- > + public function normalize($object, $format = NULL, array $context = []) { 51a55 > + 54a59,180 > +} > diff --git a/core/modules/serialization/tests/src/Unit/Normalizer/UriNormalizerTest.php b/core/modules/serialization/tests/src/Unit/Normalizer/UriNormalizerTest.php > new file mode 100644 > index 0000000..0986796 > --- /dev/null > +++ b/core/modules/serialization/tests/src/Unit/Normalizer/UriNormalizerTest.php > @@ -0,0 +1,116 @@ > + + > +namespace Drupal\Tests\serialization\Unit\Normalizer; > + > +use Drupal\Core\Field\Plugin\Field\FieldType\UriItem; > +use Drupal\Core\TypedData\Plugin\DataType\Uri; > +use Drupal\serialization\Normalizer\UriItemNormalizer; > +use Drupal\Tests\UnitTestCase; > +use Symfony\Component\Serializer\Serializer; > + > +/** > + * @coversDefaultClass \Drupal\serialization\Normalizer\UriItemNormalizer > + * > + * > + * > + * Drupal\Core\TypedData\Plugin\DataType\Uri > + * > + * Drupal\Core\Field\Plugin\Field\FieldType\UriItem > + * @group serialization > + */ > +class UriNormalizerTest extends UnitTestCase { > + > + /** > + * The mock serializer. > + * > + * @var \Symfony\Component\Serializer\SerializerInterface|\Prophecy\Prophecy\ObjectProphecy > + */ > + protected $serializer; > + > + /** > + * The TypedDataNormalizer instance. > + * > + * @var \Drupal\serialization\Normalizer\TypedDataNormalizer > + */ > + protected $normalizer; > + > + /** > + * {@inheritdoc} > + */ > + protected function setUp() { > + $this->normalizer = new UriItemNormalizer(); > + > + $this->serializer = $this->prophesize(Serializer::class); > + $this->normalizer->setSerializer($this->serializer->reveal()); > + } > + > + /** > + * @covers ::supportsNormalization > + * @dataProvider dataProviderUriData > + */ > + public function testSupportsNormalization($uri_data, $expected) { > + $this->assertTrue($this->normalizer->supportsNormalization($uri_data)); > + } > + > + /** > + * @covers ::normalize > + * @dataProvider dataProviderUriData > + */ > + public function testNormalize($uri_data, $expected) { > + $this->assertSame($expected, $this->normalizer->normalize($uri_data)); > + } > + > + /** > + * Data provider for testNormalize(). > + */ > + public function dataProviderUriData() { > + $data = []; > + > + $data['uri'] = [ > + $this->prophesizeDefinition('public://some-file.pdf')->reveal(), > + [ > + 'value' => 'public://some-file.pdf', > + 'url' => 'file_create_url:public://some-file.pdf', > + ], > + ]; > + > + $data['uri-null'] = [ > + $this->prophesizeDefinition(NULL)->reveal(), > + [ > + 'value' => NULL, > + ], > + ]; > + return $data; > + } > + > + /** > + * Helper to define prophecies. > + * > + * @param string $file_uri > + * > + * @return \Prophecy\Prophecy\ObjectProphecy > + */ > + protected function prophesizeDefinition($file_uri) { > + $uri_prophecy = $this->prophesize(Uri::class); > + $uri_prophecy->getValue()->willReturn($file_uri); > + $uri = $uri_prophecy->reveal(); > + > + $uri_item_prophecy = $this->prophesize(UriItem::class); > + $uri_item_prophecy->get('value')->willReturn($uri); > + $uri_item_prophecy->getIterator()->willReturn(new \ArrayIterator(['value' => $uri])); > + > + return $uri_item_prophecy; > + } > +} > + > +namespace Drupal\serialization\Normalizer; > + > +/** > + * Temporary mock for file_create_url(), until that is moved into > + * Component/Utility. > + */ > +if (!function_exists('Drupal\serialization\Normalizer\file_create_url')) { > + function file_create_url($uri) { > + return $uri ? "file_create_url:$uri" : $uri; > + }