Commit c9b722d0 authored by alexpott's avatar alexpott

Issue #2119481 by damiankloip, xjm: Inject EntityManager dependency into UuidResolver.

parent fa0641ba
...@@ -141,8 +141,6 @@ function entity_revision_delete($entity_type, $revision_id) { ...@@ -141,8 +141,6 @@ function entity_revision_delete($entity_type, $revision_id) {
* The entity type to load; e.g., 'node' or 'user'. * The entity type to load; e.g., 'node' or 'user'.
* @param string $uuid * @param string $uuid
* The UUID of the entity to load. * The UUID of the entity to load.
* @param bool $reset
* Whether to reset the internal cache for the requested entity type.
* *
* @return EntityInterface|FALSE * @return EntityInterface|FALSE
* The entity object, or FALSE if there is no entity with the given UUID. * The entity object, or FALSE if there is no entity with the given UUID.
...@@ -151,20 +149,11 @@ function entity_revision_delete($entity_type, $revision_id) { ...@@ -151,20 +149,11 @@ function entity_revision_delete($entity_type, $revision_id) {
* Thrown in case the requested entity type does not support UUIDs. * Thrown in case the requested entity type does not support UUIDs.
* *
* @see \Drupal\Core\Entity\EntityManagerInterface * @see \Drupal\Core\Entity\EntityManagerInterface
*
* @deprecated Use \Drupal::entityManager()->loadEntityByUuid();
*/ */
function entity_load_by_uuid($entity_type_id, $uuid, $reset = FALSE) { function entity_load_by_uuid($entity_type_id, $uuid) {
$entity_type = \Drupal::entityManager()->getDefinition($entity_type_id); return \Drupal::entityManager()->loadEntityByUuid($entity_type_id, $uuid);
if (!$uuid_key = $entity_type->getKey('uuid')) {
throw new EntityStorageException("Entity type $entity_type_id does not support UUIDs.");
}
$controller = \Drupal::entityManager()->getStorage($entity_type_id);
if ($reset) {
$controller->resetCache();
}
$entities = $controller->loadByProperties(array($uuid_key => $uuid));
return reset($entities);
} }
/** /**
......
...@@ -876,4 +876,19 @@ protected function getDisplayModeOptions($display_type, $entity_type_id, $includ ...@@ -876,4 +876,19 @@ protected function getDisplayModeOptions($display_type, $entity_type_id, $includ
return $options; return $options;
} }
/**
* {@inheritdoc}
*/
public function loadEntityByUuid($entity_type_id, $uuid) {
$entity_type = $this->getDefinition($entity_type_id);
if (!$uuid_key = $entity_type->getKey('uuid')) {
throw new EntityStorageException("Entity type $entity_type_id does not support UUIDs.");
}
$entities = $this->getStorage($entity_type_id)->loadByProperties(array($uuid_key => $uuid));
return reset($entities);
}
} }
...@@ -356,4 +356,22 @@ public function getViewModeOptions($entity_type_id, $include_disabled = FALSE); ...@@ -356,4 +356,22 @@ public function getViewModeOptions($entity_type_id, $include_disabled = FALSE);
*/ */
public function getFormModeOptions($entity_type_id, $include_disabled = FALSE); public function getFormModeOptions($entity_type_id, $include_disabled = FALSE);
/**
* Loads an entity by UUID.
*
* Note that some entity types may not support UUIDs.
*
* @param string $entity_type_id
* The entity type ID to load from.
* @param string $uuid
* The UUID of the entity to load.
*
* @return \Drupal\Core\Entity\EntityInterface|FALSE
* The entity object, or FALSE if there is no entity with the given UUID.
*
* @throws \Drupal\Core\Entity\EntityStorageException
* Thrown in case the requested entity type does not support UUIDs.
*/
public function loadEntityByUuid($entity_type_id, $uuid);
} }
...@@ -122,13 +122,14 @@ function setUp() { ...@@ -122,13 +122,14 @@ function setUp() {
'bundle' => 'entity_test', 'bundle' => 'entity_test',
))->save(); ))->save();
$link_manager = new LinkManager(new TypeLinkManager(new MemoryBackend('default')), new RelationLinkManager(new MemoryBackend('default'), \Drupal::entityManager())); $entity_manager = \Drupal::entityManager();
$link_manager = new LinkManager(new TypeLinkManager(new MemoryBackend('default')), new RelationLinkManager(new MemoryBackend('default'), $entity_manager));
$chain_resolver = new ChainEntityResolver(array(new UuidResolver(), new TargetIdResolver())); $chain_resolver = new ChainEntityResolver(array(new UuidResolver($entity_manager), new TargetIdResolver()));
// Set up the mock serializer. // Set up the mock serializer.
$normalizers = array( $normalizers = array(
new ContentEntityNormalizer($link_manager, \Drupal::entityManager(), \Drupal::moduleHandler()), new ContentEntityNormalizer($link_manager, $entity_manager, \Drupal::moduleHandler()),
new EntityReferenceItemNormalizer($link_manager, $chain_resolver), new EntityReferenceItemNormalizer($link_manager, $chain_resolver),
new FieldItemNormalizer(), new FieldItemNormalizer(),
new FieldNormalizer(), new FieldNormalizer(),
......
...@@ -39,6 +39,7 @@ services: ...@@ -39,6 +39,7 @@ services:
class: Drupal\serialization\EntityResolver\UuidResolver class: Drupal\serialization\EntityResolver\UuidResolver
tags: tags:
- { name: entity_resolver} - { name: entity_resolver}
arguments: ['@entity.manager']
serialization.entity_resolver.target_id: serialization.entity_resolver.target_id:
class: Drupal\serialization\EntityResolver\TargetIdResolver class: Drupal\serialization\EntityResolver\TargetIdResolver
tags: tags:
......
...@@ -7,10 +7,12 @@ ...@@ -7,10 +7,12 @@
namespace Drupal\serialization\EntityResolver; namespace Drupal\serialization\EntityResolver;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/** /**
* Interface for extracting UUID from entity reference data when denormalizing. * Interface for extracting UUID from entity reference data when denormalizing.
*/ */
interface UuidReferenceInterface { interface UuidReferenceInterface extends NormalizerInterface {
/** /**
* Get the uuid from the data array. * Get the uuid from the data array.
......
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
namespace Drupal\serialization\EntityResolver; namespace Drupal\serialization\EntityResolver;
use Drupal\Core\Entity\EntityManagerInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
/** /**
...@@ -15,14 +16,31 @@ ...@@ -15,14 +16,31 @@
class UuidResolver implements EntityResolverInterface { class UuidResolver implements EntityResolverInterface {
/** /**
* Implements \Drupal\serialization\EntityResolver\EntityResolverInterface::resolve(). * The entity manager.
*
* @var \Drupal\Core\Entity\EntityManagerInterface
*/
protected $entityManager;
/**
* Constructs a UuidResolver object.
*
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* The entity manager.
*/
public function __construct(EntityManagerInterface $entity_manager) {
$this->entityManager = $entity_manager;
}
/**
* {@inheritdoc}
*/ */
public function resolve(NormalizerInterface $normalizer, $data, $entity_type) { public function resolve(NormalizerInterface $normalizer, $data, $entity_type) {
// The normalizer is what knows the specification of the data being // The normalizer is what knows the specification of the data being
// deserialized. If it can return a UUID from that data, and if there's an // deserialized. If it can return a UUID from that data, and if there's an
// entity with that UUID, then return its ID. // entity with that UUID, then return its ID.
if (($normalizer instanceof UuidReferenceInterface) && $uuid = $normalizer->getUuid($data)) { if (($normalizer instanceof UuidReferenceInterface) && ($uuid = $normalizer->getUuid($data))) {
if ($entity = entity_load_by_uuid($entity_type, $uuid)) { if ($entity = $this->entityManager->loadEntityByUuid($entity_type, $uuid)) {
return $entity->id(); return $entity->id();
} }
} }
......
<?php
/**
* @file
* Contains \Drupal\serialization\Tests\Normalizer\ListNormalizerTest.
*/
namespace Drupal\serialization\Tests\Normalizer;
use Drupal\Tests\UnitTestCase;
use Drupal\serialization\EntityResolver\UuidResolver;
/**
* Tests the UuidResolver class.
*
* @see \Drupal\serialization\EntityResolver\UuidResolver
*
* @group Drupal
* @group Serialization
*/
class UuidResolverTest extends UnitTestCase {
/**
* The UuidResolver instance.
*
* @var \Drupal\serialization\EntityResolver\UuidResolver
*/
protected $resolver;
/**
* The mock EntityManager instance.
*
* @var \Drupal\Core\Entity\EntityManager|\PHPUnit_Framework_MockObject_MockObject
*/
protected $entityManager;
/**
* {@inheritdoc}
*/
public static function getInfo() {
return array(
'name' => 'UuidResolver',
'description' => 'Tests the UuidResolver class.',
'group' => 'Serialization',
);
}
/**
* {@inheritdoc}
*/
public function setUp() {
$this->entityManager = $this->getMockBuilder('Drupal\Core\Entity\EntityManager')
->disableOriginalConstructor()
->getMock();
$this->resolver = new UuidResolver($this->entityManager);
}
/**
* Test resolve() with a class using the incorrect interface.
*/
public function testResolveNotInInterface() {
$this->entityManager->expects($this->never())
->method('loadEntityByUuid');
$normalizer = $this->getMock('Symfony\Component\Serializer\Normalizer\NormalizerInterface');
$this->assertNull($this->resolver->resolve($normalizer, array(), 'test_type'));
}
/**
* Test resolve() with a class using the correct interface but no UUID.
*/
public function testResolveNoUuid() {
$this->entityManager->expects($this->never())
->method('loadEntityByUuid');
$normalizer = $this->getMock('Drupal\serialization\EntityResolver\UuidReferenceInterface');
$normalizer->expects($this->once())
->method('getUuid')
->with(array())
->will($this->returnValue(NULL));
$this->assertNull($this->resolver->resolve($normalizer, array(), 'test_type'));
}
/**
* Test resolve() with correct interface but no matching entity for the UUID.
*/
public function testResolveNoEntity() {
$uuid = '392eab92-35c2-4625-872d-a9dab4da008e';
$this->entityManager->expects($this->once())
->method('loadEntityByUuid')
->with('test_type')
->will($this->returnValue(NULL));
$normalizer = $this->getMock('Drupal\serialization\EntityResolver\UuidReferenceInterface');
$normalizer->expects($this->once())
->method('getUuid')
->with(array())
->will($this->returnValue($uuid));
$this->assertNull($this->resolver->resolve($normalizer, array(), 'test_type'));
}
/**
* Test resolve() when a UUID corresponds to an entity.
*/
public function testResolveWithEntity() {
$uuid = '392eab92-35c2-4625-872d-a9dab4da008e';
$entity = $this->getMock('Drupal\Core\Entity\EntityInterface');
$entity->expects($this->once())
->method('id')
->will($this->returnValue(1));
$this->entityManager->expects($this->once())
->method('loadEntityByUuid')
->with('test_type', $uuid)
->will($this->returnValue($entity));
$normalizer = $this->getMock('Drupal\serialization\EntityResolver\UuidReferenceInterface');
$normalizer->expects($this->once())
->method('getUuid')
->with(array())
->will($this->returnValue($uuid));
$this->assertSame(1, $this->resolver->resolve($normalizer, array(), 'test_type'));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment