X-Git-Url: http://www.aleph1.co.uk/gitweb/?a=blobdiff_plain;f=web%2Fcore%2Ftests%2FDrupal%2FTests%2FCore%2FEntity%2FEntityUnitTest.php;h=141668def57562bc1b95f0005e1a3ed46047f62f;hb=0bf8d09d2542548982e81a441b1f16e75873a04f;hp=ad9e49c61675d236ace39748177cb80184cb16b6;hpb=a2bd1bf0c2c1f1a17d188f4dc0726a45494cefae;p=yaffs-website diff --git a/web/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php b/web/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php index ad9e49c61..141668def 100644 --- a/web/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php +++ b/web/core/tests/Drupal/Tests/Core/Entity/EntityUnitTest.php @@ -5,7 +5,11 @@ namespace Drupal\Tests\Core\Entity; use Drupal\Core\Access\AccessResult; use Drupal\Core\Cache\Cache; use Drupal\Core\DependencyInjection\ContainerBuilder; +use Drupal\Core\Entity\EntityStorageInterface; +use Drupal\Core\Entity\EntityTypeManagerInterface; +use Drupal\Core\Entity\EntityTypeRepositoryInterface; use Drupal\Core\Language\Language; +use Drupal\entity_test\Entity\EntityTestMul; use Drupal\Tests\UnitTestCase; /** @@ -30,11 +34,11 @@ class EntityUnitTest extends UnitTestCase { protected $entityType; /** - * The entity manager used for testing. + * The entity type manager used for testing. * - * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject + * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject */ - protected $entityManager; + protected $entityTypeManager; /** * The ID of the type of the entity under test. @@ -94,8 +98,8 @@ class EntityUnitTest extends UnitTestCase { ->method('getListCacheTags') ->willReturn([$this->entityTypeId . '_list']); - $this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface'); - $this->entityManager->expects($this->any()) + $this->entityTypeManager = $this->getMockForAbstractClass(EntityTypeManagerInterface::class); + $this->entityTypeManager->expects($this->any()) ->method('getDefinition') ->with($this->entityTypeId) ->will($this->returnValue($this->entityType)); @@ -111,7 +115,9 @@ class EntityUnitTest extends UnitTestCase { $this->cacheTagsInvalidator = $this->getMock('Drupal\Core\Cache\CacheTagsInvalidator'); $container = new ContainerBuilder(); - $container->set('entity.manager', $this->entityManager); + // Ensure that Entity doesn't use the deprecated entity.manager service. + $container->set('entity.manager', NULL); + $container->set('entity_type.manager', $this->entityTypeManager); $container->set('uuid', $this->uuid); $container->set('language_manager', $this->languageManager); $container->set('cache_tags.invalidator', $this->cacheTagsInvalidator); @@ -162,6 +168,7 @@ class EntityUnitTest extends UnitTestCase { /** * @covers ::label + * @group legacy */ public function testLabel() { // Make a mock with one method that we use as the entity's uri_callback. We @@ -186,7 +193,7 @@ class EntityUnitTest extends UnitTestCase { // Set a dummy property on the entity under test to test that the label can // be returned form a property if there is no callback. - $this->entityManager->expects($this->at(1)) + $this->entityTypeManager->expects($this->at(1)) ->method('getDefinition') ->with($this->entityTypeId) ->will($this->returnValue([ @@ -213,9 +220,10 @@ class EntityUnitTest extends UnitTestCase { $access->expects($this->at(1)) ->method('createAccess') ->will($this->returnValue(AccessResult::allowed())); - $this->entityManager->expects($this->exactly(2)) + $this->entityTypeManager->expects($this->exactly(2)) ->method('getAccessControlHandler') ->will($this->returnValue($access)); + $this->assertEquals(AccessResult::allowed(), $this->entity->access($operation)); $this->assertEquals(AccessResult::allowed(), $this->entity->access('create')); } @@ -239,11 +247,11 @@ class EntityUnitTest extends UnitTestCase { // Base our mocked entity on a real entity class so we can test if calling // Entity::load() on the base class will bubble up to an actual entity. $this->entityTypeId = 'entity_test_mul'; - $methods = get_class_methods('Drupal\entity_test\Entity\EntityTestMul'); + $methods = get_class_methods(EntityTestMul::class); unset($methods[array_search('load', $methods)]); unset($methods[array_search('loadMultiple', $methods)]); unset($methods[array_search('create', $methods)]); - $this->entity = $this->getMockBuilder('Drupal\entity_test\Entity\EntityTestMul') + $this->entity = $this->getMockBuilder(EntityTestMul::class) ->disableOriginalConstructor() ->setMethods($methods) ->getMock(); @@ -260,21 +268,25 @@ class EntityUnitTest extends UnitTestCase { $class_name = get_class($this->entity); - $this->entityManager->expects($this->once()) + $entity_type_repository = $this->getMockForAbstractClass(EntityTypeRepositoryInterface::class); + $entity_type_repository->expects($this->once()) ->method('getEntityTypeFromClass') ->with($class_name) ->willReturn($this->entityTypeId); - $storage = $this->getMock('\Drupal\Core\Entity\EntityStorageInterface'); + $storage = $this->getMock(EntityStorageInterface::class); $storage->expects($this->once()) ->method('load') ->with(1) ->will($this->returnValue($this->entity)); - $this->entityManager->expects($this->once()) + + $this->entityTypeManager->expects($this->once()) ->method('getStorage') ->with($this->entityTypeId) ->will($this->returnValue($storage)); + \Drupal::getContainer()->set('entity_type.repository', $entity_type_repository); + // Call Entity::load statically and check that it returns the mock entity. $this->assertSame($this->entity, $class_name::load(1)); } @@ -290,21 +302,25 @@ class EntityUnitTest extends UnitTestCase { $class_name = get_class($this->entity); - $this->entityManager->expects($this->once()) + $entity_type_repository = $this->getMockForAbstractClass(EntityTypeRepositoryInterface::class); + $entity_type_repository->expects($this->once()) ->method('getEntityTypeFromClass') ->with($class_name) ->willReturn($this->entityTypeId); - $storage = $this->getMock('\Drupal\Core\Entity\EntityStorageInterface'); + $storage = $this->getMock(EntityStorageInterface::class); $storage->expects($this->once()) ->method('loadMultiple') ->with([1]) ->will($this->returnValue([1 => $this->entity])); - $this->entityManager->expects($this->once()) + + $this->entityTypeManager->expects($this->once()) ->method('getStorage') ->with($this->entityTypeId) ->will($this->returnValue($storage)); + \Drupal::getContainer()->set('entity_type.repository', $entity_type_repository); + // Call Entity::loadMultiple statically and check that it returns the mock // entity. $this->assertSame([1 => $this->entity], $class_name::loadMultiple([1])); @@ -317,21 +333,26 @@ class EntityUnitTest extends UnitTestCase { $this->setupTestLoad(); $class_name = get_class($this->entity); - $this->entityManager->expects($this->once()) + + $entity_type_repository = $this->getMockForAbstractClass(EntityTypeRepositoryInterface::class); + $entity_type_repository->expects($this->once()) ->method('getEntityTypeFromClass') ->with($class_name) ->willReturn($this->entityTypeId); - $storage = $this->getMock('\Drupal\Core\Entity\EntityStorageInterface'); + $storage = $this->getMock(EntityStorageInterface::class); $storage->expects($this->once()) ->method('create') ->with([]) ->will($this->returnValue($this->entity)); - $this->entityManager->expects($this->once()) + + $this->entityTypeManager->expects($this->once()) ->method('getStorage') ->with($this->entityTypeId) ->will($this->returnValue($storage)); + \Drupal::getContainer()->set('entity_type.repository', $entity_type_repository); + // Call Entity::create() statically and check that it returns the mock // entity. $this->assertSame($this->entity, $class_name::create([])); @@ -345,10 +366,12 @@ class EntityUnitTest extends UnitTestCase { $storage->expects($this->once()) ->method('save') ->with($this->entity); - $this->entityManager->expects($this->once()) + + $this->entityTypeManager->expects($this->once()) ->method('getStorage') ->with($this->entityTypeId) ->will($this->returnValue($storage)); + $this->entity->save(); } @@ -361,10 +384,12 @@ class EntityUnitTest extends UnitTestCase { // Testing the argument of the delete() method consumes too much memory. $storage->expects($this->once()) ->method('delete'); - $this->entityManager->expects($this->once()) + + $this->entityTypeManager->expects($this->once()) ->method('getStorage') ->with($this->entityTypeId) ->will($this->returnValue($storage)); + $this->entity->delete(); } @@ -392,13 +417,16 @@ class EntityUnitTest extends UnitTestCase { $this->cacheTagsInvalidator->expects($this->at(0)) ->method('invalidateTags') ->with([ - $this->entityTypeId . '_list', // List cache tag. + // List cache tag. + $this->entityTypeId . '_list', ]); $this->cacheTagsInvalidator->expects($this->at(1)) ->method('invalidateTags') ->with([ - $this->entityTypeId . ':' . $this->values['id'], // Own cache tag. - $this->entityTypeId . '_list', // List cache tag. + // Own cache tag. + $this->entityTypeId . ':' . $this->values['id'], + // List cache tag. + $this->entityTypeId . '_list', ]); // This method is internal, so check for errors on calling it only.