From ba447d77abc1c75cf0c2c7e42e5c17faa7ce677f Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Sat, 26 Sep 2015 15:33:59 +0200 Subject: [PATCH] Issue #2124677 by damiankloip, juampynr, chr.fritsch, klausi: Expose URI in file fields when serializing an object --- .../serialization/serialization.services.yml | 4 + .../EntityReferenceFieldItemNormalizer.php | 41 ++++++ .../src/Tests/EntitySerializationTest.php | 36 ++++-- ...EntityReferenceFieldItemNormalizerTest.php | 121 ++++++++++++++++++ 4 files changed, 190 insertions(+), 12 deletions(-) create mode 100644 core/modules/serialization/src/Normalizer/EntityReferenceFieldItemNormalizer.php create mode 100644 core/modules/serialization/tests/src/Unit/Normalizer/EntityReferenceFieldItemNormalizerTest.php diff --git a/core/modules/serialization/serialization.services.yml b/core/modules/serialization/serialization.services.yml index 318a90cf577..070a530a29e 100644 --- a/core/modules/serialization/serialization.services.yml +++ b/core/modules/serialization/serialization.services.yml @@ -21,6 +21,10 @@ services: class: Drupal\serialization\Normalizer\ComplexDataNormalizer tags: - { name: normalizer } + serializer.normalizer.entity_reference_field_item: + class: Drupal\serialization\Normalizer\EntityReferenceFieldItemNormalizer + tags: + - { name: normalizer, priority: 10 } serializer.normalizer.list: class: Drupal\serialization\Normalizer\ListNormalizer tags: diff --git a/core/modules/serialization/src/Normalizer/EntityReferenceFieldItemNormalizer.php b/core/modules/serialization/src/Normalizer/EntityReferenceFieldItemNormalizer.php new file mode 100644 index 00000000000..846d8837e84 --- /dev/null +++ b/core/modules/serialization/src/Normalizer/EntityReferenceFieldItemNormalizer.php @@ -0,0 +1,41 @@ +get('entity')->getValue()) && ($url = $entity->url('canonical'))) { + $values['url'] = $url; + } + + return $values; + } + +} diff --git a/core/modules/serialization/src/Tests/EntitySerializationTest.php b/core/modules/serialization/src/Tests/EntitySerializationTest.php index bb305a7bd2a..3a9daf84aee 100644 --- a/core/modules/serialization/src/Tests/EntitySerializationTest.php +++ b/core/modules/serialization/src/Tests/EntitySerializationTest.php @@ -39,6 +39,13 @@ class EntitySerializationTest extends NormalizerTestBase { */ protected $entity; + /** + * The test user. + * + * @var \Drupal\user\Entity\User + */ + protected $user; + /** * The serializer service. * @@ -58,10 +65,19 @@ class EntitySerializationTest extends NormalizerTestBase { // User create needs sequence table. $this->installSchema('system', array('sequences')); + + // Create a test user to use as the entity owner. + $this->user = \Drupal::entityManager()->getStorage('user')->create([ + 'name' => 'serialization_test_user', + 'mail' => 'foo@example.com', + 'pass' => '123456', + ]); + $this->user->save(); + // Create a test entity to serialize. $this->values = array( 'name' => $this->randomMachineName(), - 'user_id' => \Drupal::currentUser()->id(), + 'user_id' => $this->user->id(), 'field_test_text' => array( 'value' => $this->randomMachineName(), 'format' => 'full_html', @@ -99,7 +115,10 @@ class EntitySerializationTest extends NormalizerTestBase { array('value' => $this->entity->created->value), ), 'user_id' => array( - array('target_id' => $this->values['user_id']), + array( + 'target_id' => $this->user->id(), + 'url' => $this->user->url(), + ), ), 'revision_id' => array( array('value' => 1), @@ -128,22 +147,15 @@ class EntitySerializationTest extends NormalizerTestBase { * override some default access controls. */ public function testUserNormalize() { - $account = User::create([ - 'name' => 'serialization_test_user', - 'mail' => 'foo@example.com', - 'pass' => '123456', - ]); - $account->save(); - // Test password isn't available. - $normalized = $this->serializer->normalize($account); + $normalized = $this->serializer->normalize($this->user); $this->assertFalse(array_key_exists('pass', $normalized), '"pass" key does not exist in normalized user'); $this->assertFalse(array_key_exists('mail', $normalized), '"mail" key does not exist in normalized user'); // Test again using our test user, so that our access control override will // allow password viewing. - $normalized = $this->serializer->normalize($account, NULL, ['account' => $account]); + $normalized = $this->serializer->normalize($this->user, NULL, ['account' => $this->user]); // The key 'pass' will now exist, but the password value should be // normalized to NULL. @@ -179,7 +191,7 @@ class EntitySerializationTest extends NormalizerTestBase { 'name' => '' . $this->values['name'] . '', 'type' => 'entity_test_mulrev', 'created' => '' . $this->entity->created->value . '', - 'user_id' => '' . $this->values['user_id'] . '', + 'user_id' => '' . $this->user->id() . '' . $this->user->url() . '', 'revision_id' => '' . $this->entity->getRevisionId() . '', 'default_langcode' => '1', 'field_test_text' => '' . $this->values['field_test_text']['value'] . '' . $this->values['field_test_text']['format'] . '', diff --git a/core/modules/serialization/tests/src/Unit/Normalizer/EntityReferenceFieldItemNormalizerTest.php b/core/modules/serialization/tests/src/Unit/Normalizer/EntityReferenceFieldItemNormalizerTest.php new file mode 100644 index 00000000000..c0c2e9f9216 --- /dev/null +++ b/core/modules/serialization/tests/src/Unit/Normalizer/EntityReferenceFieldItemNormalizerTest.php @@ -0,0 +1,121 @@ +normalizer = new EntityReferenceFieldItemNormalizer(); + + $this->serializer = $this->prophesize(Serializer::class); + // Set up the serializer to return an entity property. + $this->serializer->normalize(Argument::cetera()) + ->willReturn(['value' => 'test']); + + $this->normalizer->setSerializer($this->serializer->reveal()); + + $this->fieldItem = $this->prophesize(EntityReferenceItem::class); + $this->fieldItem->getIterator() + ->willReturn(new \ArrayIterator(['target_id' => []])); + } + + /** + * @covers ::supportsNormalization + */ + public function testSupportsNormalization() { + $this->assertTrue($this->normalizer->supportsNormalization($this->fieldItem->reveal())); + $this->assertFalse($this->normalizer->supportsNormalization(new \stdClass())); + } + + /** + * @covers ::normalize + */ + public function testNormalize() { + $test_url = '/test/100'; + + $entity = $this->prophesize(EntityInterface::class); + $entity->url('canonical') + ->willReturn($test_url) + ->shouldBeCalled(); + + $entity_reference = $this->prophesize(TypedDataInterface::class); + $entity_reference->getValue() + ->willReturn($entity->reveal()) + ->shouldBeCalled(); + + $this->fieldItem->get('entity') + ->willReturn($entity_reference) + ->shouldBeCalled(); + + $normalized = $this->normalizer->normalize($this->fieldItem->reveal()); + + $expected = [ + 'target_id' => ['value' => 'test'], + 'url' => $test_url, + ]; + $this->assertSame($expected, $normalized); + } + + /** + * @covers ::normalize + */ + public function testNormalizeWithNoEntity() { + $entity_reference = $this->prophesize(TypedDataInterface::class); + $entity_reference->getValue() + ->willReturn(NULL) + ->shouldBeCalled(); + + $this->fieldItem->get('entity') + ->willReturn($entity_reference->reveal()) + ->shouldBeCalled(); + + $normalized = $this->normalizer->normalize($this->fieldItem->reveal()); + + $expected = [ + 'target_id' => ['value' => 'test'], + ]; + $this->assertSame($expected, $normalized); + } + +}