diff -u b/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php b/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php --- b/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php +++ b/core/tests/Drupal/Tests/Core/Entity/EntityManagerTest.php @@ -1401,6 +1401,37 @@ return get_class($this->getMockForAbstractClass('Drupal\Core\Entity\EntityHandlerBase')); } + /** + * Tests the loadEntityByUuid() method. + * + * @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, + )); + + $new_entity = $this->entityManager->loadEntityByUuid($entity_type_id,$uuid); + $this->assertNull($new_entity); + } + + /** + * Provides test data for testloadEntityByUuid(). + * + * @return array + * Test data. + */ + public function providerTestLoadEntityByUuid() { + return array( + array('apple', 2415), + array('banana', NULL), + ); + } + } /* @@ -1545,37 +1576,6 @@ return get_class($this->getMockForAbstractClass('Drupal\Core\Entity\EntityHandlerBase')); } - /** - * Tests the loadEntityByUuid() method. - * - * @covers ::loadEntityByUuid() - * - * @dataProvider providerTestLoadEntityByUuid - */ - public function testloadEntityByUuid($entity_type_id, $uuid) { - $entity = $this->getMock('Drupal\Core\Entity\EntityManager'); - $this->setUpEntityManager(array( - 'apple' => $entity, - 'banana' => $entity, - )); - - $new_entity = $this->entityManager->loadEntityByUuid($entity_type_id,$uuid); - $this->assertNull($new_entity); - } - - /** - * Provides test data for testloadEntityByUuid(). - * - * @return array - * Test data. - */ - public function providerTestLoadEntityByUuid() { - return array( - array('apple', 2415), - array('banana', NULL), - ); - } - } /* only in patch2: unchanged: --- a/core/lib/Drupal/Core/Entity/EntityManager.php +++ b/core/lib/Drupal/Core/Entity/EntityManager.php @@ -9,6 +9,7 @@ use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException; use Drupal\Component\Plugin\Exception\PluginNotFoundException; +use Drupal\Component\Uuid\Uuid; use Drupal\Component\Utility\String; use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\CacheBackendInterface; @@ -968,6 +969,10 @@ protected function getDisplayModeOptions($display_type, $entity_type_id, $includ * {@inheritdoc} */ public function loadEntityByUuid($entity_type_id, $uuid) { + // Return NULL for invalid UUID's. + if (empty($uuid) || !Uuid::isValid($uuid)) { + return NULL; + } $entity_type = $this->getDefinition($entity_type_id); if (!$uuid_key = $entity_type->getKey('uuid')) { @@ -976,6 +981,10 @@ public function loadEntityByUuid($entity_type_id, $uuid) { $entities = $this->getStorage($entity_type_id)->loadByProperties(array($uuid_key => $uuid)); + if (empty($entities)) { + return NULL; + } + return reset($entities); }