diff --git a/core/lib/Drupal/Core/Entity/EntityManager.php b/core/lib/Drupal/Core/Entity/EntityManager.php index b4fc8ef..d10ee23 100644 --- a/core/lib/Drupal/Core/Entity/EntityManager.php +++ b/core/lib/Drupal/Core/Entity/EntityManager.php @@ -14,6 +14,7 @@ use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Config\Entity\ConfigEntityType; +use Drupal\Core\Database\InvalidQueryException; use Drupal\Core\DependencyInjection\ClassResolverInterface; use Drupal\Core\Entity\Exception\AmbiguousEntityClassException; use Drupal\Core\Entity\Exception\NoCorrespondingEntityClassException; @@ -1121,17 +1122,17 @@ protected function getDisplayModeOptions($display_type, $entity_type_id, $includ * {@inheritdoc} */ public function loadEntityByUuid($entity_type_id, $uuid) { - // Return NULL for invalid UUIDs. - if (empty($uuid) || !Uuid::isValid($uuid)) { - return NULL; - } $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)); + try { + $entities = $this->getStorage($entity_type_id)->loadByProperties(array($uuid_key => $uuid)); + } catch (InvalidQueryException $ex) { + return NULL; + } if (empty($entities)) { return NULL; diff --git a/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php index 0bc2d50..45cd7cf 100644 --- a/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php @@ -10,6 +10,7 @@ use Drupal\Component\Plugin\Discovery\DiscoveryInterface; use Drupal\Component\Plugin\Exception\PluginNotFoundException; use Drupal\Core\Config\Entity\ConfigEntityStorage; +use Drupal\Core\Database\InvalidQueryException; use Drupal\Core\DependencyInjection\ContainerInjectionInterface; use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\EntityHandlerBase; @@ -1819,34 +1820,189 @@ protected function getTestHandlerClass() { } /** - * Tests the loadEntityByUuid() method. + * Provides test data for testloadEntityByUuid() and its friends. + * + * @return array + * - An entity type ID. + * - A UUID. + */ + public function providerTestLoadEntityByUuid() { + return array( + array('apple', '6d286553-59ae-409a-887d-ee75df67b834'), + ); + } + + /** + * Tests loadEntityByUuid() for UUIDs which are supported and exist. * * @covers ::loadEntityByUuid * * @dataProvider providerTestLoadEntityByUuid */ public function testloadEntityByUuid($entity_type_id, $uuid) { - $entity = $this->getMock('Drupal\Core\Entity\EntityTypeInterface'); - $this->setUpEntityManager(array( - 'apple' => $entity, - 'banana' => $entity, - )); + // Mock the entity type. + $entity_type = $this->getMockBuilder('Drupal\Core\Entity\EntityTypeInterface') + ->setMethods(['getKey']) + ->getMockForAbstractClass(); + $entity_type->expects($this->once()) + ->method('getKey') + ->with('uuid') + ->willReturn('uuid'); + + // Mock the entity storage. + $entity_storage = $this->getMockBuilder('Drupal\Core\Entity\EntityStorageInterface') + ->setMethods(['loadByProperties']) + ->getMockForAbstractClass(); + $entity_storage->expects($this->once()) + ->method('loadByProperties') + ->willReturn(['this entity contains uuid: ' . $uuid]); - $new_entity = $this->entityManager->loadEntityByUuid($entity_type_id, $uuid); - $this->assertNull($new_entity); + // Mock the manager. + $manager = $this->getMockBuilder('Drupal\Core\Entity\EntityManager') + ->setMethods(['getDefinition', 'getStorage']) + ->disableOriginalConstructor() + ->getMock(); + $manager->expects($this->once()) + ->method('getDefinition') + ->willReturn($entity_type); + $manager->expects($this->once()) + ->method('getStorage') + ->with($entity_type_id) + ->willReturn($entity_storage); + + $this->assertEquals( + 'this entity contains uuid: ' . $uuid, + $manager->loadEntityByUuid($entity_type_id, $uuid) + ); } /** - * Provides test data for testloadEntityByUuid(). + * Tests loadEntityByUuid() for UUIDs which don't exist in the database. * - * @return array - * Test data. + * Verifes that when + * Drupal\Core\Entity\EntityStorageInterface::loadByProperties() returns an + * empty array, loadEntityByUuid() returns NULL. + * + * @covers ::loadEntityByUuid + * + * @dataProvider providerTestLoadEntityByUuid */ - public function providerTestLoadEntityByUuid() { - return array( - array('apple', 2415), - array('banana', NULL), - ); + public function testloadEntityByUuidDoesNotExist($entity_type_id, $uuid) { + // Mock the entity type. + $entity_type = $this->getMockBuilder('Drupal\Core\Entity\EntityTypeInterface') + ->setMethods(['getKey']) + ->getMockForAbstractClass(); + $entity_type->expects($this->once()) + ->method('getKey') + ->with('uuid') + ->willReturn('uuid'); + + // Mock the entity storage. + $entity_storage = $this->getMockBuilder('Drupal\Core\Entity\EntityStorageInterface') + ->setMethods(['loadByProperties']) + ->getMockForAbstractClass(); + // Make sure we return an empty array of entities. + $entity_storage->expects($this->once()) + ->method('loadByProperties') + ->willReturn([]); + + // Mock the manager. + $manager = $this->getMockBuilder('Drupal\Core\Entity\EntityManager') + ->setMethods(['getDefinition', 'getStorage']) + ->disableOriginalConstructor() + ->getMock(); + $manager->expects($this->once()) + ->method('getDefinition') + ->willReturn($entity_type); + $manager->expects($this->once()) + ->method('getStorage') + ->with($entity_type_id) + ->willReturn($entity_storage); + + $this->assertNull($manager->loadEntityByUuid($entity_type_id, $uuid)); + } + + /** + * Tests loadEntityByUuid() where UUIDs are not supported. + * + * Verify that loadEntityByUuid() throws a + * Drupal\Core\Entity\EntityStorageException if the entity type does not + * support UUIDs. + * + * @covers ::loadEntityByUuid + * + * @expectedException \Drupal\Core\Entity\EntityStorageException + * + * @dataProvider providerTestLoadEntityByUuid + */ + public function testloadEntityByUuidNotSupported($entity_type_id, $uuid) { + // Mock the entity type. + $entity_type = $this->getMockBuilder('Drupal\Core\Entity\EntityTypeInterface') + ->setMethods(['getKey']) + ->getMockForAbstractClass(); + // Make sure that the UUID key does not exist. + $entity_type->expects($this->once()) + ->method('getKey') + ->with('uuid') + ->willReturn(FALSE); + + // Mock the manager. + $manager = $this->getMockBuilder('Drupal\Core\Entity\EntityManager') + ->setMethods(['getDefinition', 'getStorage']) + ->disableOriginalConstructor() + ->getMock(); + $manager->expects($this->once()) + ->method('getDefinition') + ->willReturn($entity_type); + $manager->expects($this->never()) + ->method('getStorage'); + + $manager->loadEntityByUuid($entity_type_id, $uuid); + } + + /** + * Tests the loadEntityByUuid() method querying for an empty value. + * + * We verify that loadEntityByUuid() returns NULL when + * Drupal\Core\Entity\EntityStorageInterface::loadByProperties() throws + * Drupal\Core\Database\InvalidQueryException. + * + * @covers ::loadEntityByUuid + */ + public function testloadEntityByUuidNullQuery() { + // Mock the entity type. + $entity_type = $this->getMockBuilder('Drupal\Core\Entity\EntityTypeInterface') + ->setMethods(['getKey']) + ->getMockForAbstractClass(); + $entity_type->expects($this->once()) + ->method('getKey') + ->with('uuid') + ->willReturn('uuid'); + + // Mock the entity storage. + $entity_storage = $this->getMockBuilder('Drupal\Core\Entity\EntityStorageInterface') + ->setMethods(['loadByProperties']) + ->getMockForAbstractClass(); + // Have loadByProperites() throw an exception. Since we're forcing this to + // happen, we can't use a data provider with this test method. + $entity_storage->expects($this->once()) + ->method('loadByProperties') + ->willThrowException(new InvalidQueryException()); + + // Mock the manager. + $manager = $this->getMockBuilder('Drupal\Core\Entity\EntityManager') + ->setMethods(['getDefinition', 'getStorage']) + ->disableOriginalConstructor() + ->getMock(); + $manager->expects($this->once()) + ->method('getDefinition') + ->willReturn($entity_type); + $manager->expects($this->once()) + ->method('getStorage') + ->with('entity_type_id') + ->willReturn($entity_storage); + + $this->assertNull($manager->loadEntityByUuid('entity_type_id', NULL)); } }