Issue #2782833 by Mile23, martin107, larowlan, effulgentsia: Remove usages of deprecated EntityManager from Entity class

8.5.x
Nathaniel Catchpole 2017-09-25 13:21:07 +01:00
parent 5f3b66ed0d
commit 3bc858ecf5
20 changed files with 412 additions and 148 deletions

View File

@ -88,6 +88,15 @@ abstract class Entity implements EntityInterface {
return \Drupal::entityTypeManager(); return \Drupal::entityTypeManager();
} }
/**
* Gets the entity type bundle info service.
*
* @return \Drupal\Core\Entity\EntityTypeBundleInfoInterface
*/
protected function entityTypeBundleInfo() {
return \Drupal::service('entity_type.bundle.info');
}
/** /**
* Gets the language manager. * Gets the language manager.
* *
@ -198,7 +207,7 @@ abstract class Entity implements EntityInterface {
$bundle = $this->bundle(); $bundle = $this->bundle();
// A bundle-specific callback takes precedence over the generic one for // A bundle-specific callback takes precedence over the generic one for
// the entity type. // the entity type.
$bundles = $this->entityManager()->getBundleInfo($this->getEntityTypeId()); $bundles = $this->entityTypeBundleInfo()->getBundleInfo($this->getEntityTypeId());
if (isset($bundles[$bundle]['uri_callback'])) { if (isset($bundles[$bundle]['uri_callback'])) {
$uri_callback = $bundles[$bundle]['uri_callback']; $uri_callback = $bundles[$bundle]['uri_callback'];
} }
@ -344,11 +353,11 @@ abstract class Entity implements EntityInterface {
*/ */
public function access($operation, AccountInterface $account = NULL, $return_as_object = FALSE) { public function access($operation, AccountInterface $account = NULL, $return_as_object = FALSE) {
if ($operation == 'create') { if ($operation == 'create') {
return $this->entityManager() return $this->entityTypeManager()
->getAccessControlHandler($this->entityTypeId) ->getAccessControlHandler($this->entityTypeId)
->createAccess($this->bundle(), $account, [], $return_as_object); ->createAccess($this->bundle(), $account, [], $return_as_object);
} }
return $this->entityManager() return $this->entityTypeManager()
->getAccessControlHandler($this->entityTypeId) ->getAccessControlHandler($this->entityTypeId)
->access($this, $operation, $account, $return_as_object); ->access($this, $operation, $account, $return_as_object);
} }
@ -374,7 +383,8 @@ abstract class Entity implements EntityInterface {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function save() { public function save() {
return $this->entityManager()->getStorage($this->entityTypeId)->save($this); $storage = $this->entityTypeManager()->getStorage($this->entityTypeId);
return $storage->save($this);
} }
/** /**
@ -382,7 +392,7 @@ abstract class Entity implements EntityInterface {
*/ */
public function delete() { public function delete() {
if (!$this->isNew()) { if (!$this->isNew()) {
$this->entityManager()->getStorage($this->entityTypeId)->delete([$this->id() => $this]); $this->entityTypeManager()->getStorage($this->entityTypeId)->delete([$this->id() => $this]);
} }
} }
@ -407,7 +417,7 @@ abstract class Entity implements EntityInterface {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function getEntityType() { public function getEntityType() {
return $this->entityManager()->getDefinition($this->getEntityTypeId()); return $this->entityTypeManager()->getDefinition($this->getEntityTypeId());
} }
/** /**
@ -508,24 +518,30 @@ abstract class Entity implements EntityInterface {
* {@inheritdoc} * {@inheritdoc}
*/ */
public static function load($id) { public static function load($id) {
$entity_manager = \Drupal::entityManager(); $entity_type_repository = \Drupal::service('entity_type.repository');
return $entity_manager->getStorage($entity_manager->getEntityTypeFromClass(get_called_class()))->load($id); $entity_type_manager = \Drupal::entityTypeManager();
$storage = $entity_type_manager->getStorage($entity_type_repository->getEntityTypeFromClass(get_called_class()));
return $storage->load($id);
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public static function loadMultiple(array $ids = NULL) { public static function loadMultiple(array $ids = NULL) {
$entity_manager = \Drupal::entityManager(); $entity_type_repository = \Drupal::service('entity_type.repository');
return $entity_manager->getStorage($entity_manager->getEntityTypeFromClass(get_called_class()))->loadMultiple($ids); $entity_type_manager = \Drupal::entityTypeManager();
$storage = $entity_type_manager->getStorage($entity_type_repository->getEntityTypeFromClass(get_called_class()));
return $storage->loadMultiple($ids);
} }
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
public static function create(array $values = []) { public static function create(array $values = []) {
$entity_manager = \Drupal::entityManager(); $entity_type_repository = \Drupal::service('entity_type.repository');
return $entity_manager->getStorage($entity_manager->getEntityTypeFromClass(get_called_class()))->create($values); $entity_type_manager = \Drupal::entityTypeManager();
$storage = $entity_type_manager->getStorage($entity_type_repository->getEntityTypeFromClass(get_called_class()));
return $storage->create($values);
} }
/** /**

View File

@ -3,6 +3,7 @@
namespace Drupal\Tests\block\Unit; namespace Drupal\Tests\block\Unit;
use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Tests\Core\Plugin\Fixtures\TestConfigurablePlugin; use Drupal\Tests\Core\Plugin\Fixtures\TestConfigurablePlugin;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
@ -20,11 +21,11 @@ class BlockConfigEntityUnitTest extends UnitTestCase {
protected $entityType; 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. * The ID of the type of the entity under test.
@ -51,8 +52,8 @@ class BlockConfigEntityUnitTest extends UnitTestCase {
->method('getProvider') ->method('getProvider')
->will($this->returnValue('block')); ->will($this->returnValue('block'));
$this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface'); $this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
$this->entityManager->expects($this->any()) $this->entityTypeManager->expects($this->any())
->method('getDefinition') ->method('getDefinition')
->with($this->entityTypeId) ->with($this->entityTypeId)
->will($this->returnValue($this->entityType)); ->will($this->returnValue($this->entityType));
@ -60,7 +61,7 @@ class BlockConfigEntityUnitTest extends UnitTestCase {
$this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface'); $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
$container = new ContainerBuilder(); $container = new ContainerBuilder();
$container->set('entity.manager', $this->entityManager); $container->set('entity_type.manager', $this->entityTypeManager);
$container->set('uuid', $this->uuid); $container->set('uuid', $this->uuid);
\Drupal::setContainer($container); \Drupal::setContainer($container);
} }

View File

@ -3,6 +3,8 @@
namespace Drupal\Tests\editor\Unit; namespace Drupal\Tests\editor\Unit;
use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\editor\Entity\Editor; use Drupal\editor\Entity\Editor;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
@ -22,9 +24,9 @@ class EditorConfigEntityUnitTest extends UnitTestCase {
/** /**
* The entity manager used for testing. * The entity 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. * The ID of the type of the entity under test.
@ -66,8 +68,8 @@ class EditorConfigEntityUnitTest extends UnitTestCase {
->method('getProvider') ->method('getProvider')
->will($this->returnValue('editor')); ->will($this->returnValue('editor'));
$this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface'); $this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
$this->entityManager->expects($this->any()) $this->entityTypeManager->expects($this->any())
->method('getDefinition') ->method('getDefinition')
->with($this->entityTypeId) ->with($this->entityTypeId)
->will($this->returnValue($this->entityType)); ->will($this->returnValue($this->entityType));
@ -78,10 +80,16 @@ class EditorConfigEntityUnitTest extends UnitTestCase {
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$entity_manager = new EntityManager();
$container = new ContainerBuilder(); $container = new ContainerBuilder();
$container->set('entity.manager', $this->entityManager); $container->set('entity.manager', $entity_manager);
$container->set('entity_type.manager', $this->entityTypeManager);
$container->set('uuid', $this->uuid); $container->set('uuid', $this->uuid);
$container->set('plugin.manager.editor', $this->editorPluginManager); $container->set('plugin.manager.editor', $this->editorPluginManager);
// Inject the container into entity.manager so it can defer to
// entity_type.manager.
$entity_manager->setContainer($container);
\Drupal::setContainer($container); \Drupal::setContainer($container);
} }
@ -120,7 +128,7 @@ class EditorConfigEntityUnitTest extends UnitTestCase {
->with($format_id) ->with($format_id)
->will($this->returnValue($filter_format)); ->will($this->returnValue($filter_format));
$this->entityManager->expects($this->once()) $this->entityTypeManager->expects($this->once())
->method('getStorage') ->method('getStorage')
->with('filter_format') ->with('filter_format')
->will($this->returnValue($storage)); ->will($this->returnValue($storage));

View File

@ -10,6 +10,9 @@ namespace Drupal\Tests\field\Unit;
use Drupal\Core\Entity\EntityType; use Drupal\Core\Entity\EntityType;
use Drupal\Core\Field\FieldDefinitionInterface; use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
@ -33,6 +36,20 @@ class FieldConfigEntityUnitTest extends UnitTestCase {
*/ */
protected $entityManager; protected $entityManager;
/**
* The entity type manager used for testing.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $entityTypeManager;
/**
* The entity field manager used for testing.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $entityFieldManager;
/** /**
* The ID of the type of the entity under test. * The ID of the type of the entity under test.
* *
@ -75,7 +92,9 @@ class FieldConfigEntityUnitTest extends UnitTestCase {
$this->entityTypeId = $this->randomMachineName(); $this->entityTypeId = $this->randomMachineName();
$this->entityType = $this->getMock('\Drupal\Core\Config\Entity\ConfigEntityTypeInterface'); $this->entityType = $this->getMock('\Drupal\Core\Config\Entity\ConfigEntityTypeInterface');
$this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface'); $this->entityManager = new EntityManager();
$this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
$this->entityFieldManager = $this->getMock(EntityFieldManagerInterface::class);
$this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface'); $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
@ -85,9 +104,14 @@ class FieldConfigEntityUnitTest extends UnitTestCase {
$container = new ContainerBuilder(); $container = new ContainerBuilder();
$container->set('entity.manager', $this->entityManager); $container->set('entity.manager', $this->entityManager);
$container->set('entity_field.manager', $this->entityFieldManager);
$container->set('entity_type.manager', $this->entityTypeManager);
$container->set('uuid', $this->uuid); $container->set('uuid', $this->uuid);
$container->set('config.typed', $this->typedConfigManager); $container->set('config.typed', $this->typedConfigManager);
$container->set('plugin.manager.field.field_type', $this->fieldTypePluginManager); $container->set('plugin.manager.field.field_type', $this->fieldTypePluginManager);
// Inject the container into entity.manager so it can defer to
// entity_type.manager, etc.
$this->entityManager->setContainer($container);
\Drupal::setContainer($container); \Drupal::setContainer($container);
// Create a mock FieldStorageConfig object. // Create a mock FieldStorageConfig object.
@ -102,7 +126,7 @@ class FieldConfigEntityUnitTest extends UnitTestCase {
->method('getSettings') ->method('getSettings')
->willReturn([]); ->willReturn([]);
// Place the field in the mocked entity manager's field registry. // Place the field in the mocked entity manager's field registry.
$this->entityManager->expects($this->any()) $this->entityFieldManager->expects($this->any())
->method('getFieldStorageDefinitions') ->method('getFieldStorageDefinitions')
->with('test_entity_type') ->with('test_entity_type')
->will($this->returnValue([ ->will($this->returnValue([
@ -120,19 +144,19 @@ class FieldConfigEntityUnitTest extends UnitTestCase {
->method('getBundleConfigDependency') ->method('getBundleConfigDependency')
->will($this->returnValue(['type' => 'config', 'name' => 'test.test_entity_type.id'])); ->will($this->returnValue(['type' => 'config', 'name' => 'test.test_entity_type.id']));
$this->entityManager->expects($this->at(0)) $this->entityTypeManager->expects($this->at(0))
->method('getDefinition') ->method('getDefinition')
->with($this->entityTypeId) ->with($this->entityTypeId)
->willReturn($this->entityType); ->willReturn($this->entityType);
$this->entityManager->expects($this->at(1)) $this->entityTypeManager->expects($this->at(1))
->method('getDefinition') ->method('getDefinition')
->with($this->entityTypeId) ->with($this->entityTypeId)
->willReturn($this->entityType); ->willReturn($this->entityType);
$this->entityManager->expects($this->at(2)) $this->entityTypeManager->expects($this->at(2))
->method('getDefinition') ->method('getDefinition')
->with($this->entityTypeId) ->with($this->entityTypeId)
->willReturn($this->entityType); ->willReturn($this->entityType);
$this->entityManager->expects($this->at(3)) $this->entityTypeManager->expects($this->at(3))
->method('getDefinition') ->method('getDefinition')
->with('test_entity_type') ->with('test_entity_type')
->willReturn($target_entity_type); ->willReturn($target_entity_type);
@ -168,7 +192,7 @@ class FieldConfigEntityUnitTest extends UnitTestCase {
->with('test_bundle_not_exists') ->with('test_bundle_not_exists')
->will($this->returnValue(NULL)); ->will($this->returnValue(NULL));
$this->entityManager->expects($this->any()) $this->entityTypeManager->expects($this->any())
->method('getStorage') ->method('getStorage')
->with('bundle_entity_type') ->with('bundle_entity_type')
->will($this->returnValue($storage)); ->will($this->returnValue($storage));
@ -178,19 +202,19 @@ class FieldConfigEntityUnitTest extends UnitTestCase {
'bundle_entity_type' => 'bundle_entity_type', 'bundle_entity_type' => 'bundle_entity_type',
]); ]);
$this->entityManager->expects($this->at(0)) $this->entityTypeManager->expects($this->at(0))
->method('getDefinition') ->method('getDefinition')
->with($this->entityTypeId) ->with($this->entityTypeId)
->willReturn($this->entityType); ->willReturn($this->entityType);
$this->entityManager->expects($this->at(1)) $this->entityTypeManager->expects($this->at(1))
->method('getDefinition') ->method('getDefinition')
->with($this->entityTypeId) ->with($this->entityTypeId)
->willReturn($this->entityType); ->willReturn($this->entityType);
$this->entityManager->expects($this->at(2)) $this->entityTypeManager->expects($this->at(2))
->method('getDefinition') ->method('getDefinition')
->with($this->entityTypeId) ->with($this->entityTypeId)
->willReturn($this->entityType); ->willReturn($this->entityType);
$this->entityManager->expects($this->at(3)) $this->entityTypeManager->expects($this->at(3))
->method('getDefinition') ->method('getDefinition')
->with('test_entity_type') ->with('test_entity_type')
->willReturn($target_entity_type); ->willReturn($target_entity_type);
@ -267,7 +291,7 @@ class FieldConfigEntityUnitTest extends UnitTestCase {
'dependencies' => [], 'dependencies' => [],
'field_type' => 'test_field', 'field_type' => 'test_field',
]; ];
$this->entityManager->expects($this->any()) $this->entityTypeManager->expects($this->any())
->method('getDefinition') ->method('getDefinition')
->with($this->entityTypeId) ->with($this->entityTypeId)
->will($this->returnValue($this->entityType)); ->will($this->returnValue($this->entityType));
@ -289,7 +313,7 @@ class FieldConfigEntityUnitTest extends UnitTestCase {
public function testGetType() { public function testGetType() {
// Ensure that FieldConfig::getType() is not delegated to // Ensure that FieldConfig::getType() is not delegated to
// FieldStorage. // FieldStorage.
$this->entityManager->expects($this->never()) $this->entityFieldManager->expects($this->never())
->method('getFieldStorageDefinitions'); ->method('getFieldStorageDefinitions');
$this->fieldStorage->expects($this->never()) $this->fieldStorage->expects($this->never())
->method('getType'); ->method('getType');

View File

@ -6,8 +6,9 @@ use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\Cache\Context\CacheContextsManager; use Drupal\Core\Cache\Context\CacheContextsManager;
use Drupal\Core\Config\Entity\ConfigEntityTypeInterface; use Drupal\Core\Config\Entity\ConfigEntityTypeInterface;
use Drupal\Core\DependencyInjection\Container; use Drupal\Core\DependencyInjection\Container;
use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Session\AccountInterface; use Drupal\Core\Session\AccountInterface;
use Drupal\field\Entity\FieldStorageConfig; use Drupal\field\Entity\FieldStorageConfig;
@ -126,31 +127,37 @@ class FieldStorageConfigAccessControlHandlerTest extends UnitTestCase {
$storage_access_control_handler = new FieldStorageConfigAccessControlHandler($storageType); $storage_access_control_handler = new FieldStorageConfigAccessControlHandler($storageType);
$storage_access_control_handler->setModuleHandler($this->moduleHandler); $storage_access_control_handler->setModuleHandler($this->moduleHandler);
$entityManager = $this->getMock(EntityManagerInterface::class); $entity_type_manager = $this->getMock(EntityTypeManagerInterface::class);
$entityManager $entity_type_manager
->expects($this->any()) ->expects($this->any())
->method('getDefinition') ->method('getDefinition')
->willReturnMap([ ->willReturnMap([
['field_storage_config', TRUE, $storageType], ['field_storage_config', TRUE, $storageType],
['node', TRUE, $entityType], ['node', TRUE, $entityType],
]); ]);
$entityManager $entity_type_manager
->expects($this->any()) ->expects($this->any())
->method('getStorage') ->method('getStorage')
->willReturnMap([ ->willReturnMap([
['field_storage_config', $this->getMock(EntityStorageInterface::class)], ['field_storage_config', $this->getMock(EntityStorageInterface::class)],
]); ]);
$entityManager $entity_type_manager
->expects($this->any()) ->expects($this->any())
->method('getAccessControlHandler') ->method('getAccessControlHandler')
->willReturnMap([ ->willReturnMap([
['field_storage_config', $storage_access_control_handler], ['field_storage_config', $storage_access_control_handler],
]); ]);
$entity_manager = new EntityManager();
$container = new Container(); $container = new Container();
$container->set('entity.manager', $entityManager); $container->set('entity.manager', $entity_manager);
$container->set('entity_type.manager', $entity_type_manager);
$container->set('uuid', $this->getMock(UuidInterface::class)); $container->set('uuid', $this->getMock(UuidInterface::class));
$container->set('cache_contexts_manager', $this->prophesize(CacheContextsManager::class)); $container->set('cache_contexts_manager', $this->prophesize(CacheContextsManager::class));
// Inject the container into entity.manager so it can defer to
// entity_type.manager.
$entity_manager->setContainer($container);
\Drupal::setContainer($container); \Drupal::setContainer($container);
$this->fieldStorage = new FieldStorageConfig([ $this->fieldStorage = new FieldStorageConfig([

View File

@ -8,6 +8,8 @@
namespace Drupal\Tests\field\Unit; namespace Drupal\Tests\field\Unit;
use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldException; use Drupal\Core\Field\FieldException;
use Drupal\Core\Field\FieldStorageDefinitionInterface; use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface; use Drupal\Core\Field\FieldTypePluginManagerInterface;
@ -22,11 +24,11 @@ use Drupal\Tests\UnitTestCase;
class FieldStorageConfigEntityUnitTest extends UnitTestCase { class FieldStorageConfigEntityUnitTest extends UnitTestCase {
/** /**
* 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. * The ID of the type of the entity under test.
@ -53,14 +55,19 @@ class FieldStorageConfigEntityUnitTest extends UnitTestCase {
* {@inheritdoc} * {@inheritdoc}
*/ */
protected function setUp() { protected function setUp() {
$this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface'); $entity_manager = new EntityManager();
$this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
$this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface'); $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
$this->fieldTypeManager = $this->getMock(FieldTypePluginManagerInterface::class); $this->fieldTypeManager = $this->getMock(FieldTypePluginManagerInterface::class);
$container = new ContainerBuilder(); $container = new ContainerBuilder();
$container->set('entity.manager', $this->entityManager); $container->set('entity.manager', $entity_manager);
$container->set('entity_type.manager', $this->entityTypeManager);
$container->set('uuid', $this->uuid); $container->set('uuid', $this->uuid);
$container->set('plugin.manager.field.field_type', $this->fieldTypeManager); $container->set('plugin.manager.field.field_type', $this->fieldTypeManager);
// Inject the container into entity.manager so it can defer to
// entity_type.manager.
$entity_manager->setContainer($container);
\Drupal::setContainer($container); \Drupal::setContainer($container);
} }
@ -85,7 +92,7 @@ class FieldStorageConfigEntityUnitTest extends UnitTestCase {
// ConfigEntityBase::addDependency() to get the provider of the field config // ConfigEntityBase::addDependency() to get the provider of the field config
// entity type and once in FieldStorageConfig::calculateDependencies() to // entity type and once in FieldStorageConfig::calculateDependencies() to
// get the provider of the entity type that field is attached to. // get the provider of the entity type that field is attached to.
$this->entityManager->expects($this->any()) $this->entityTypeManager->expects($this->any())
->method('getDefinition') ->method('getDefinition')
->willReturnMap([ ->willReturnMap([
['field_storage_config', TRUE, $fieldStorageConfigentityType], ['field_storage_config', TRUE, $fieldStorageConfigentityType],

View File

@ -4,6 +4,9 @@ namespace Drupal\Tests\language\Unit;
use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeRepositoryInterface;
use Drupal\language\Entity\ContentLanguageSettings; use Drupal\language\Entity\ContentLanguageSettings;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
@ -27,6 +30,13 @@ class ContentLanguageSettingsUnitTest extends UnitTestCase {
*/ */
protected $entityManager; protected $entityManager;
/**
* The entity type manager used for testing.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $entityTypeManager;
/** /**
* The ID of the type of the entity under test. * The ID of the type of the entity under test.
* *
@ -62,7 +72,8 @@ class ContentLanguageSettingsUnitTest extends UnitTestCase {
$this->entityTypeId = $this->randomMachineName(); $this->entityTypeId = $this->randomMachineName();
$this->entityType = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface'); $this->entityType = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
$this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface'); $this->entityManager = new EntityManager();
$this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
$this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface'); $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
@ -72,9 +83,13 @@ class ContentLanguageSettingsUnitTest extends UnitTestCase {
$container = new ContainerBuilder(); $container = new ContainerBuilder();
$container->set('entity.manager', $this->entityManager); $container->set('entity.manager', $this->entityManager);
$container->set('entity_type.manager', $this->entityTypeManager);
$container->set('uuid', $this->uuid); $container->set('uuid', $this->uuid);
$container->set('config.typed', $this->typedConfigManager); $container->set('config.typed', $this->typedConfigManager);
$container->set('config.storage', $this->configEntityStorageInterface); $container->set('config.storage', $this->configEntityStorageInterface);
// Inject the container into entity.manager so it can defer to other entity
// services.
$this->entityManager->setContainer($container);
\Drupal::setContainer($container); \Drupal::setContainer($container);
} }
@ -88,7 +103,7 @@ class ContentLanguageSettingsUnitTest extends UnitTestCase {
->method('getBundleConfigDependency') ->method('getBundleConfigDependency')
->will($this->returnValue(['type' => 'config', 'name' => 'test.test_entity_type.id'])); ->will($this->returnValue(['type' => 'config', 'name' => 'test.test_entity_type.id']));
$this->entityManager->expects($this->any()) $this->entityTypeManager->expects($this->any())
->method('getDefinition') ->method('getDefinition')
->with('test_entity_type') ->with('test_entity_type')
->will($this->returnValue($target_entity_type)); ->will($this->returnValue($target_entity_type));
@ -254,16 +269,20 @@ class ContentLanguageSettingsUnitTest extends UnitTestCase {
->method('create') ->method('create')
->will($this->returnValue($nullConfig)); ->will($this->returnValue($nullConfig));
$this->entityManager $this->entityTypeManager
->expects($this->any()) ->expects($this->any())
->method('getStorage') ->method('getStorage')
->with('language_content_settings') ->with('language_content_settings')
->will($this->returnValue($this->configEntityStorageInterface)); ->will($this->returnValue($this->configEntityStorageInterface));
$this->entityManager->expects($this->any())
$entity_type_repository = $this->getMockForAbstractClass(EntityTypeRepositoryInterface::class);
$entity_type_repository->expects($this->any())
->method('getEntityTypeFromClass') ->method('getEntityTypeFromClass')
->with('Drupal\language\Entity\ContentLanguageSettings') ->with(ContentLanguageSettings::class)
->willReturn('language_content_settings'); ->willReturn('language_content_settings');
\Drupal::getContainer()->set('entity_type.repository', $entity_type_repository);
$config = ContentLanguageSettings::loadByEntityTypeBundle($type, $bundle); $config = ContentLanguageSettings::loadByEntityTypeBundle($type, $bundle);
$this->assertSame($expected_langcode, $config->getDefaultLangcode()); $this->assertSame($expected_langcode, $config->getDefaultLangcode());

View File

@ -3,6 +3,8 @@
namespace Drupal\Tests\rdf\Unit; namespace Drupal\Tests\rdf\Unit;
use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
use Drupal\rdf\Entity\RdfMapping; use Drupal\rdf\Entity\RdfMapping;
@ -26,6 +28,13 @@ class RdfMappingConfigEntityUnitTest extends UnitTestCase {
*/ */
protected $entityManager; protected $entityManager;
/**
* The entity type manager used for testing.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $entityTypeManager;
/** /**
* The ID of the type of the entity under test. * The ID of the type of the entity under test.
* *
@ -51,13 +60,16 @@ class RdfMappingConfigEntityUnitTest extends UnitTestCase {
->method('getProvider') ->method('getProvider')
->will($this->returnValue('entity')); ->will($this->returnValue('entity'));
$this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface'); $this->entityManager = new EntityManager();
$this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
$this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface'); $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
$container = new ContainerBuilder(); $container = new ContainerBuilder();
$container->set('entity.manager', $this->entityManager); $container->set('entity.manager', $this->entityManager);
$container->set('entity_type.manager', $this->entityTypeManager);
$container->set('uuid', $this->uuid); $container->set('uuid', $this->uuid);
$this->entityManager->setContainer($container);
\Drupal::setContainer($container); \Drupal::setContainer($container);
} }
@ -77,11 +89,11 @@ class RdfMappingConfigEntityUnitTest extends UnitTestCase {
->method('getBundleEntityType') ->method('getBundleEntityType')
->will($this->returnValue(NULL)); ->will($this->returnValue(NULL));
$this->entityManager->expects($this->at(0)) $this->entityTypeManager->expects($this->at(0))
->method('getDefinition') ->method('getDefinition')
->with($target_entity_type_id) ->with($target_entity_type_id)
->will($this->returnValue($target_entity_type)); ->will($this->returnValue($target_entity_type));
$this->entityManager->expects($this->at(1)) $this->entityTypeManager->expects($this->at(1))
->method('getDefinition') ->method('getDefinition')
->with($this->entityTypeId) ->with($this->entityTypeId)
->will($this->returnValue($this->entityType)); ->will($this->returnValue($this->entityType));
@ -108,11 +120,11 @@ class RdfMappingConfigEntityUnitTest extends UnitTestCase {
->method('getBundleConfigDependency') ->method('getBundleConfigDependency')
->will($this->returnValue(['type' => 'config', 'name' => 'test_module.type.' . $bundle_id])); ->will($this->returnValue(['type' => 'config', 'name' => 'test_module.type.' . $bundle_id]));
$this->entityManager->expects($this->at(0)) $this->entityTypeManager->expects($this->at(0))
->method('getDefinition') ->method('getDefinition')
->with($target_entity_type_id) ->with($target_entity_type_id)
->will($this->returnValue($target_entity_type)); ->will($this->returnValue($target_entity_type));
$this->entityManager->expects($this->at(1)) $this->entityTypeManager->expects($this->at(1))
->method('getDefinition') ->method('getDefinition')
->with($this->entityTypeId) ->with($this->entityTypeId)
->will($this->returnValue($this->entityType)); ->will($this->returnValue($this->entityType));

View File

@ -3,6 +3,8 @@
namespace Drupal\Tests\responsive_image\Unit; namespace Drupal\Tests\responsive_image\Unit;
use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\EntityTypeRepositoryInterface;
use Drupal\responsive_image\Entity\ResponsiveImageStyle; use Drupal\responsive_image\Entity\ResponsiveImageStyle;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
@ -20,11 +22,11 @@ class ResponsiveImageStyleConfigEntityUnitTest extends UnitTestCase {
protected $entityType; 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 breakpoint manager used for testing. * The breakpoint manager used for testing.
@ -42,8 +44,8 @@ class ResponsiveImageStyleConfigEntityUnitTest extends UnitTestCase {
->method('getProvider') ->method('getProvider')
->will($this->returnValue('responsive_image')); ->will($this->returnValue('responsive_image'));
$this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface'); $this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
$this->entityManager->expects($this->any()) $this->entityTypeManager->expects($this->any())
->method('getDefinition') ->method('getDefinition')
->with('responsive_image_style') ->with('responsive_image_style')
->will($this->returnValue($this->entityType)); ->will($this->returnValue($this->entityType));
@ -51,7 +53,7 @@ class ResponsiveImageStyleConfigEntityUnitTest extends UnitTestCase {
$this->breakpointManager = $this->getMock('\Drupal\breakpoint\BreakpointManagerInterface'); $this->breakpointManager = $this->getMock('\Drupal\breakpoint\BreakpointManagerInterface');
$container = new ContainerBuilder(); $container = new ContainerBuilder();
$container->set('entity.manager', $this->entityManager); $container->set('entity_type.manager', $this->entityTypeManager);
$container->set('breakpoint.manager', $this->breakpointManager); $container->set('breakpoint.manager', $this->breakpointManager);
\Drupal::setContainer($container); \Drupal::setContainer($container);
} }
@ -74,11 +76,14 @@ class ResponsiveImageStyleConfigEntityUnitTest extends UnitTestCase {
->method('loadMultiple') ->method('loadMultiple')
->with(array_keys($styles)) ->with(array_keys($styles))
->willReturn($styles); ->willReturn($styles);
$this->entityManager->expects($this->any())
$this->entityTypeManager->expects($this->any())
->method('getStorage') ->method('getStorage')
->with('image_style') ->with('image_style')
->willReturn($storage); ->willReturn($storage);
$this->entityManager->expects($this->any())
$entity_type_repository = $this->getMockForAbstractClass(EntityTypeRepositoryInterface::class);
$entity_type_repository->expects($this->any())
->method('getEntityTypeFromClass') ->method('getEntityTypeFromClass')
->with('Drupal\image\Entity\ImageStyle') ->with('Drupal\image\Entity\ImageStyle')
->willReturn('image_style'); ->willReturn('image_style');
@ -103,6 +108,8 @@ class ResponsiveImageStyleConfigEntityUnitTest extends UnitTestCase {
->with('test_group') ->with('test_group')
->willReturn(['bartik' => 'theme', 'toolbar' => 'module']); ->willReturn(['bartik' => 'theme', 'toolbar' => 'module']);
\Drupal::getContainer()->set('entity_type.repository', $entity_type_repository);
$dependencies = $entity->calculateDependencies()->getDependencies(); $dependencies = $entity->calculateDependencies()->getDependencies();
$this->assertEquals(['toolbar'], $dependencies['module']); $this->assertEquals(['toolbar'], $dependencies['module']);
$this->assertEquals(['bartik'], $dependencies['theme']); $this->assertEquals(['bartik'], $dependencies['theme']);

View File

@ -3,6 +3,8 @@
namespace Drupal\Tests\user\Unit\Views\Argument; namespace Drupal\Tests\user\Unit\Views\Argument;
use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
use Drupal\user\Entity\Role; use Drupal\user\Entity\Role;
use Drupal\user\Plugin\views\argument\RolesRid; use Drupal\user\Plugin\views\argument\RolesRid;
@ -44,23 +46,27 @@ class RolesRidTest extends UnitTestCase {
->with('label') ->with('label')
->will($this->returnValue('label')); ->will($this->returnValue('label'));
$entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface'); $entity_manager = new EntityManager();
$entity_manager->expects($this->any()) $entity_type_manager = $this->getMock(EntityTypeManagerInterface::class);
$entity_type_manager->expects($this->any())
->method('getDefinition') ->method('getDefinition')
->with($this->equalTo('user_role')) ->with($this->equalTo('user_role'))
->will($this->returnValue($entity_type)); ->will($this->returnValue($entity_type));
$entity_manager $entity_type_manager
->expects($this->once()) ->expects($this->once())
->method('getStorage') ->method('getStorage')
->with($this->equalTo('user_role')) ->with($this->equalTo('user_role'))
->will($this->returnValue($role_storage)); ->will($this->returnValue($role_storage));
// @todo \Drupal\Core\Entity\Entity::entityType() uses a global call to // Set up a minimal container to satisfy Drupal\Core\Entity\Entity's
// entity_get_info(), which in turn wraps \Drupal::entityManager(). Set // dependency on it.
// the entity manager until this is fixed.
$container = new ContainerBuilder(); $container = new ContainerBuilder();
$container->set('entity.manager', $entity_manager); $container->set('entity.manager', $entity_manager);
$container->set('entity_type.manager', $entity_type_manager);
// Inject the container into entity.manager so it can defer to
// entity_type.manager.
$entity_manager->setContainer($container);
\Drupal::setContainer($container); \Drupal::setContainer($container);
$roles_rid_argument = new RolesRid([], 'user__roles_rid', [], $entity_manager); $roles_rid_argument = new RolesRid([], 'user__roles_rid', [], $entity_manager);

View File

@ -10,6 +10,7 @@ namespace Drupal\Tests\Core\Config\Entity;
use Drupal\Component\Plugin\PluginManagerInterface; use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Config\Schema\SchemaIncompleteException; use Drupal\Core\Config\Schema\SchemaIncompleteException;
use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\Language; use Drupal\Core\Language\Language;
use Drupal\Core\Plugin\DefaultLazyPluginCollection; use Drupal\Core\Plugin\DefaultLazyPluginCollection;
use Drupal\Tests\Core\Config\Entity\Fixtures\ConfigEntityBaseWithPluginCollections; use Drupal\Tests\Core\Config\Entity\Fixtures\ConfigEntityBaseWithPluginCollections;
@ -37,11 +38,11 @@ class ConfigEntityBaseUnitTest extends UnitTestCase {
protected $entityType; 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. * The ID of the type of the entity under test.
@ -112,8 +113,8 @@ class ConfigEntityBaseUnitTest extends UnitTestCase {
->method('getConfigPrefix') ->method('getConfigPrefix')
->willReturn('test_provider.' . $this->entityTypeId); ->willReturn('test_provider.' . $this->entityTypeId);
$this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface'); $this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
$this->entityManager->expects($this->any()) $this->entityTypeManager->expects($this->any())
->method('getDefinition') ->method('getDefinition')
->with($this->entityTypeId) ->with($this->entityTypeId)
->will($this->returnValue($this->entityType)); ->will($this->returnValue($this->entityType));
@ -131,7 +132,7 @@ class ConfigEntityBaseUnitTest extends UnitTestCase {
$this->typedConfigManager = $this->getMock('Drupal\Core\Config\TypedConfigManagerInterface'); $this->typedConfigManager = $this->getMock('Drupal\Core\Config\TypedConfigManagerInterface');
$container = new ContainerBuilder(); $container = new ContainerBuilder();
$container->set('entity.manager', $this->entityManager); $container->set('entity_type.manager', $this->entityTypeManager);
$container->set('uuid', $this->uuid); $container->set('uuid', $this->uuid);
$container->set('language_manager', $this->languageManager); $container->set('language_manager', $this->languageManager);
$container->set('cache_tags.invalidator', $this->cacheTagsInvalidator); $container->set('cache_tags.invalidator', $this->cacheTagsInvalidator);
@ -468,7 +469,7 @@ class ConfigEntityBaseUnitTest extends UnitTestCase {
* @covers ::sort * @covers ::sort
*/ */
public function testSort() { public function testSort() {
$this->entityManager->expects($this->any()) $this->entityTypeManager->expects($this->any())
->method('getDefinition') ->method('getDefinition')
->with($this->entityTypeId) ->with($this->entityTypeId)
->will($this->returnValue([ ->will($this->returnValue([

View File

@ -17,8 +17,8 @@ use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Config\TypedConfigManagerInterface; use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityMalformedException; use Drupal\Core\Entity\EntityMalformedException;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityStorageException; use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Query\QueryFactoryInterface; use Drupal\Core\Entity\Query\QueryFactoryInterface;
use Drupal\Core\Entity\Query\QueryInterface; use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\Core\Extension\ModuleHandlerInterface; use Drupal\Core\Extension\ModuleHandlerInterface;
@ -136,8 +136,8 @@ class ConfigEntityStorageTest extends UnitTestCase {
$this->entityStorage = new ConfigEntityStorage($entity_type, $this->configFactory->reveal(), $this->uuidService->reveal(), $this->languageManager->reveal()); $this->entityStorage = new ConfigEntityStorage($entity_type, $this->configFactory->reveal(), $this->uuidService->reveal(), $this->languageManager->reveal());
$this->entityStorage->setModuleHandler($this->moduleHandler->reveal()); $this->entityStorage->setModuleHandler($this->moduleHandler->reveal());
$entity_manager = $this->prophesize(EntityManagerInterface::class); $entity_type_manager = $this->prophesize(EntityTypeManagerInterface::class);
$entity_manager->getDefinition('test_entity_type')->willReturn($entity_type); $entity_type_manager->getDefinition('test_entity_type')->willReturn($entity_type);
$this->cacheTagsInvalidator = $this->prophesize(CacheTagsInvalidatorInterface::class); $this->cacheTagsInvalidator = $this->prophesize(CacheTagsInvalidatorInterface::class);
@ -149,7 +149,7 @@ class ConfigEntityStorageTest extends UnitTestCase {
$this->configManager = $this->prophesize(ConfigManagerInterface::class); $this->configManager = $this->prophesize(ConfigManagerInterface::class);
$container = new ContainerBuilder(); $container = new ContainerBuilder();
$container->set('entity.manager', $entity_manager->reveal()); $container->set('entity_type.manager', $entity_type_manager->reveal());
$container->set('entity.query.config', $entity_query_factory->reveal()); $container->set('entity.query.config', $entity_query_factory->reveal());
$container->set('config.typed', $typed_config_manager->reveal()); $container->set('config.typed', $typed_config_manager->reveal());
$container->set('cache_tags.invalidator', $this->cacheTagsInvalidator->reveal()); $container->set('cache_tags.invalidator', $this->cacheTagsInvalidator->reveal());

View File

@ -3,6 +3,8 @@
namespace Drupal\Tests\Core\Config\Entity; namespace Drupal\Tests\Core\Config\Entity;
use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
/** /**
@ -32,6 +34,13 @@ class EntityDisplayModeBaseUnitTest extends UnitTestCase {
*/ */
protected $entityManager; protected $entityManager;
/**
* The entity type manager used for testing.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $entityTypeManager;
/** /**
* The ID of the type of the entity under test. * The ID of the type of the entity under test.
* *
@ -57,13 +66,21 @@ class EntityDisplayModeBaseUnitTest extends UnitTestCase {
->method('getProvider') ->method('getProvider')
->will($this->returnValue('entity')); ->will($this->returnValue('entity'));
$this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface'); $this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
$this->entityManager = new EntityManager();
$this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface'); $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
$container = new ContainerBuilder(); $container = new ContainerBuilder();
$container->set('entity.manager', $this->entityManager); $container->set('entity.manager', $this->entityManager);
$container->set('entity_type.manager', $this->entityTypeManager);
$container->set('uuid', $this->uuid); $container->set('uuid', $this->uuid);
// Inject the container into entity.manager so it can defer to
// entity_type.manager.
$this->entityManager->setContainer($container);
\Drupal::setContainer($container); \Drupal::setContainer($container);
} }
@ -79,11 +96,11 @@ class EntityDisplayModeBaseUnitTest extends UnitTestCase {
->will($this->returnValue('test_module')); ->will($this->returnValue('test_module'));
$values = ['targetEntityType' => $target_entity_type_id]; $values = ['targetEntityType' => $target_entity_type_id];
$this->entityManager->expects($this->at(0)) $this->entityTypeManager->expects($this->at(0))
->method('getDefinition') ->method('getDefinition')
->with($target_entity_type_id) ->with($target_entity_type_id)
->will($this->returnValue($target_entity_type)); ->will($this->returnValue($target_entity_type));
$this->entityManager->expects($this->at(1)) $this->entityTypeManager->expects($this->at(1))
->method('getDefinition') ->method('getDefinition')
->with($this->entityType) ->with($this->entityType)
->will($this->returnValue($this->entityInfo)); ->will($this->returnValue($this->entityInfo));

View File

@ -4,7 +4,12 @@ namespace Drupal\Tests\Core\Entity;
use Drupal\Core\Access\AccessResult; use Drupal\Core\Access\AccessResult;
use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Language\LanguageInterface;
use Drupal\Core\TypedData\TypedDataManagerInterface; use Drupal\Core\TypedData\TypedDataManagerInterface;
@ -54,6 +59,27 @@ class ContentEntityBaseUnitTest extends UnitTestCase {
*/ */
protected $entityManager; protected $entityManager;
/**
* The entity field manager used for testing.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $entityFieldManager;
/**
* The entity type bundle manager used for testing.
*
* @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $entityTypeBundleInfo;
/**
* The entity type manager used for testing.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $entityTypeManager;
/** /**
* The type ID of the entity under test. * The type ID of the entity under test.
* *
@ -124,12 +150,18 @@ class ContentEntityBaseUnitTest extends UnitTestCase {
'uuid' => 'uuid', 'uuid' => 'uuid',
])); ]));
$this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface'); $this->entityManager = new EntityManager();
$this->entityManager->expects($this->any())
$this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
$this->entityTypeManager->expects($this->any())
->method('getDefinition') ->method('getDefinition')
->with($this->entityTypeId) ->with($this->entityTypeId)
->will($this->returnValue($this->entityType)); ->will($this->returnValue($this->entityType));
$this->entityFieldManager = $this->getMock(EntityFieldManagerInterface::class);
$this->entityTypeBundleInfo = $this->getMock(EntityTypeBundleInfoInterface::class);
$this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface'); $this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
$this->typedDataManager = $this->getMock(TypedDataManagerInterface::class); $this->typedDataManager = $this->getMock(TypedDataManagerInterface::class);
@ -168,10 +200,16 @@ class ContentEntityBaseUnitTest extends UnitTestCase {
$container = new ContainerBuilder(); $container = new ContainerBuilder();
$container->set('entity.manager', $this->entityManager); $container->set('entity.manager', $this->entityManager);
$container->set('entity_field.manager', $this->entityFieldManager);
$container->set('entity_type.bundle.info', $this->entityTypeBundleInfo);
$container->set('entity_type.manager', $this->entityTypeManager);
$container->set('uuid', $this->uuid); $container->set('uuid', $this->uuid);
$container->set('typed_data_manager', $this->typedDataManager); $container->set('typed_data_manager', $this->typedDataManager);
$container->set('language_manager', $this->languageManager); $container->set('language_manager', $this->languageManager);
$container->set('plugin.manager.field.field_type', $this->fieldTypePluginManager); $container->set('plugin.manager.field.field_type', $this->fieldTypePluginManager);
// Inject the container into entity.manager so it can defer to
// entity_type.manager and other services.
$this->entityManager->setContainer($container);
\Drupal::setContainer($container); \Drupal::setContainer($container);
$this->fieldDefinitions = [ $this->fieldDefinitions = [
@ -179,14 +217,14 @@ class ContentEntityBaseUnitTest extends UnitTestCase {
'revision_id' => BaseFieldDefinition::create('integer'), 'revision_id' => BaseFieldDefinition::create('integer'),
]; ];
$this->entityManager->expects($this->any()) $this->entityFieldManager->expects($this->any())
->method('getFieldDefinitions') ->method('getFieldDefinitions')
->with($this->entityTypeId, $this->bundle) ->with($this->entityTypeId, $this->bundle)
->will($this->returnValue($this->fieldDefinitions)); ->will($this->returnValue($this->fieldDefinitions));
$this->entity = $this->getMockForAbstractClass('\Drupal\Core\Entity\ContentEntityBase', [$values, $this->entityTypeId, $this->bundle], '', TRUE, TRUE, TRUE, ['isNew']); $this->entity = $this->getMockForAbstractClass(ContentEntityBase::class, [$values, $this->entityTypeId, $this->bundle], '', TRUE, TRUE, TRUE, ['isNew']);
$values['defaultLangcode'] = [LanguageInterface::LANGCODE_DEFAULT => LanguageInterface::LANGCODE_NOT_SPECIFIED]; $values['defaultLangcode'] = [LanguageInterface::LANGCODE_DEFAULT => LanguageInterface::LANGCODE_NOT_SPECIFIED];
$this->entityUnd = $this->getMockForAbstractClass('\Drupal\Core\Entity\ContentEntityBase', [$values, $this->entityTypeId, $this->bundle]); $this->entityUnd = $this->getMockForAbstractClass(ContentEntityBase::class, [$values, $this->entityTypeId, $this->bundle]);
} }
/** /**
@ -283,7 +321,7 @@ class ContentEntityBaseUnitTest extends UnitTestCase {
* @covers ::isTranslatable * @covers ::isTranslatable
*/ */
public function testIsTranslatable() { public function testIsTranslatable() {
$this->entityManager->expects($this->any()) $this->entityTypeBundleInfo->expects($this->any())
->method('getBundleInfo') ->method('getBundleInfo')
->with($this->entityTypeId) ->with($this->entityTypeId)
->will($this->returnValue([ ->will($this->returnValue([
@ -382,7 +420,7 @@ class ContentEntityBaseUnitTest extends UnitTestCase {
$entity->preSave($storage); $entity->preSave($storage);
}); });
$this->entityManager->expects($this->any()) $this->entityTypeManager->expects($this->any())
->method('getStorage') ->method('getStorage')
->with($this->entityTypeId) ->with($this->entityTypeId)
->will($this->returnValue($storage)); ->will($this->returnValue($storage));
@ -434,7 +472,7 @@ class ContentEntityBaseUnitTest extends UnitTestCase {
$access->expects($this->at(3)) $access->expects($this->at(3))
->method('createAccess') ->method('createAccess')
->will($this->returnValue(AccessResult::allowed())); ->will($this->returnValue(AccessResult::allowed()));
$this->entityManager->expects($this->exactly(4)) $this->entityTypeManager->expects($this->exactly(4))
->method('getAccessControlHandler') ->method('getAccessControlHandler')
->will($this->returnValue($access)); ->will($this->returnValue($access));
$this->assertTrue($this->entity->access($operation)); $this->assertTrue($this->entity->access($operation));

View File

@ -3,6 +3,7 @@
namespace Drupal\Tests\Core\Entity; namespace Drupal\Tests\Core\Entity;
use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\Language; use Drupal\Core\Language\Language;
use Drupal\Core\Link; use Drupal\Core\Link;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
@ -14,11 +15,11 @@ use Drupal\Tests\UnitTestCase;
class EntityLinkTest extends UnitTestCase { class EntityLinkTest extends UnitTestCase {
/** /**
* The mocked entity manager. * The mocked entity type manager.
* *
* @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject * @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/ */
protected $entityManager; protected $entityTypeManager;
/** /**
* The tested link generator. * The tested link generator.
@ -40,12 +41,12 @@ class EntityLinkTest extends UnitTestCase {
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
$this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface'); $this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
$this->linkGenerator = $this->getMock('Drupal\Core\Utility\LinkGeneratorInterface'); $this->linkGenerator = $this->getMock('Drupal\Core\Utility\LinkGeneratorInterface');
$this->languageManager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface'); $this->languageManager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
$container = new ContainerBuilder(); $container = new ContainerBuilder();
$container->set('entity.manager', $this->entityManager); $container->set('entity_type.manager', $this->entityTypeManager);
$container->set('link_generator', $this->linkGenerator); $container->set('link_generator', $this->linkGenerator);
$container->set('language_manager', $this->languageManager); $container->set('language_manager', $this->languageManager);
\Drupal::setContainer($container); \Drupal::setContainer($container);
@ -86,7 +87,7 @@ class EntityLinkTest extends UnitTestCase {
['langcode', 'langcode'], ['langcode', 'langcode'],
]); ]);
$this->entityManager $this->entityTypeManager
->expects($this->any()) ->expects($this->any())
->method('getDefinition') ->method('getDefinition')
->with($entity_type_id) ->with($entity_type_id)
@ -148,7 +149,7 @@ class EntityLinkTest extends UnitTestCase {
['langcode', 'langcode'], ['langcode', 'langcode'],
]); ]);
$this->entityManager $this->entityTypeManager
->expects($this->any()) ->expects($this->any())
->method('getDefinition') ->method('getDefinition')
->with($entity_type_id) ->with($entity_type_id)

View File

@ -5,7 +5,11 @@ namespace Drupal\Tests\Core\Entity;
use Drupal\Core\Access\AccessResult; use Drupal\Core\Access\AccessResult;
use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\Cache;
use Drupal\Core\DependencyInjection\ContainerBuilder; 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\Core\Language\Language;
use Drupal\entity_test\Entity\EntityTestMul;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
/** /**
@ -30,11 +34,11 @@ class EntityUnitTest extends UnitTestCase {
protected $entityType; 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. * The ID of the type of the entity under test.
@ -94,8 +98,8 @@ class EntityUnitTest extends UnitTestCase {
->method('getListCacheTags') ->method('getListCacheTags')
->willReturn([$this->entityTypeId . '_list']); ->willReturn([$this->entityTypeId . '_list']);
$this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface'); $this->entityTypeManager = $this->getMockForAbstractClass(EntityTypeManagerInterface::class);
$this->entityManager->expects($this->any()) $this->entityTypeManager->expects($this->any())
->method('getDefinition') ->method('getDefinition')
->with($this->entityTypeId) ->with($this->entityTypeId)
->will($this->returnValue($this->entityType)); ->will($this->returnValue($this->entityType));
@ -111,7 +115,9 @@ class EntityUnitTest extends UnitTestCase {
$this->cacheTagsInvalidator = $this->getMock('Drupal\Core\Cache\CacheTagsInvalidator'); $this->cacheTagsInvalidator = $this->getMock('Drupal\Core\Cache\CacheTagsInvalidator');
$container = new ContainerBuilder(); $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('uuid', $this->uuid);
$container->set('language_manager', $this->languageManager); $container->set('language_manager', $this->languageManager);
$container->set('cache_tags.invalidator', $this->cacheTagsInvalidator); $container->set('cache_tags.invalidator', $this->cacheTagsInvalidator);
@ -186,7 +192,7 @@ class EntityUnitTest extends UnitTestCase {
// Set a dummy property on the entity under test to test that the label can // 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. // be returned form a property if there is no callback.
$this->entityManager->expects($this->at(1)) $this->entityTypeManager->expects($this->at(1))
->method('getDefinition') ->method('getDefinition')
->with($this->entityTypeId) ->with($this->entityTypeId)
->will($this->returnValue([ ->will($this->returnValue([
@ -213,9 +219,10 @@ class EntityUnitTest extends UnitTestCase {
$access->expects($this->at(1)) $access->expects($this->at(1))
->method('createAccess') ->method('createAccess')
->will($this->returnValue(AccessResult::allowed())); ->will($this->returnValue(AccessResult::allowed()));
$this->entityManager->expects($this->exactly(2)) $this->entityTypeManager->expects($this->exactly(2))
->method('getAccessControlHandler') ->method('getAccessControlHandler')
->will($this->returnValue($access)); ->will($this->returnValue($access));
$this->assertEquals(AccessResult::allowed(), $this->entity->access($operation)); $this->assertEquals(AccessResult::allowed(), $this->entity->access($operation));
$this->assertEquals(AccessResult::allowed(), $this->entity->access('create')); $this->assertEquals(AccessResult::allowed(), $this->entity->access('create'));
} }
@ -239,11 +246,11 @@ class EntityUnitTest extends UnitTestCase {
// Base our mocked entity on a real entity class so we can test if calling // 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. // Entity::load() on the base class will bubble up to an actual entity.
$this->entityTypeId = 'entity_test_mul'; $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('load', $methods)]);
unset($methods[array_search('loadMultiple', $methods)]); unset($methods[array_search('loadMultiple', $methods)]);
unset($methods[array_search('create', $methods)]); unset($methods[array_search('create', $methods)]);
$this->entity = $this->getMockBuilder('Drupal\entity_test\Entity\EntityTestMul') $this->entity = $this->getMockBuilder(EntityTestMul::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->setMethods($methods) ->setMethods($methods)
->getMock(); ->getMock();
@ -260,21 +267,25 @@ class EntityUnitTest extends UnitTestCase {
$class_name = get_class($this->entity); $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') ->method('getEntityTypeFromClass')
->with($class_name) ->with($class_name)
->willReturn($this->entityTypeId); ->willReturn($this->entityTypeId);
$storage = $this->getMock('\Drupal\Core\Entity\EntityStorageInterface'); $storage = $this->getMock(EntityStorageInterface::class);
$storage->expects($this->once()) $storage->expects($this->once())
->method('load') ->method('load')
->with(1) ->with(1)
->will($this->returnValue($this->entity)); ->will($this->returnValue($this->entity));
$this->entityManager->expects($this->once())
$this->entityTypeManager->expects($this->once())
->method('getStorage') ->method('getStorage')
->with($this->entityTypeId) ->with($this->entityTypeId)
->will($this->returnValue($storage)); ->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. // Call Entity::load statically and check that it returns the mock entity.
$this->assertSame($this->entity, $class_name::load(1)); $this->assertSame($this->entity, $class_name::load(1));
} }
@ -290,21 +301,25 @@ class EntityUnitTest extends UnitTestCase {
$class_name = get_class($this->entity); $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') ->method('getEntityTypeFromClass')
->with($class_name) ->with($class_name)
->willReturn($this->entityTypeId); ->willReturn($this->entityTypeId);
$storage = $this->getMock('\Drupal\Core\Entity\EntityStorageInterface'); $storage = $this->getMock(EntityStorageInterface::class);
$storage->expects($this->once()) $storage->expects($this->once())
->method('loadMultiple') ->method('loadMultiple')
->with([1]) ->with([1])
->will($this->returnValue([1 => $this->entity])); ->will($this->returnValue([1 => $this->entity]));
$this->entityManager->expects($this->once())
$this->entityTypeManager->expects($this->once())
->method('getStorage') ->method('getStorage')
->with($this->entityTypeId) ->with($this->entityTypeId)
->will($this->returnValue($storage)); ->will($this->returnValue($storage));
\Drupal::getContainer()->set('entity_type.repository', $entity_type_repository);
// Call Entity::loadMultiple statically and check that it returns the mock // Call Entity::loadMultiple statically and check that it returns the mock
// entity. // entity.
$this->assertSame([1 => $this->entity], $class_name::loadMultiple([1])); $this->assertSame([1 => $this->entity], $class_name::loadMultiple([1]));
@ -317,21 +332,26 @@ class EntityUnitTest extends UnitTestCase {
$this->setupTestLoad(); $this->setupTestLoad();
$class_name = get_class($this->entity); $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') ->method('getEntityTypeFromClass')
->with($class_name) ->with($class_name)
->willReturn($this->entityTypeId); ->willReturn($this->entityTypeId);
$storage = $this->getMock('\Drupal\Core\Entity\EntityStorageInterface'); $storage = $this->getMock(EntityStorageInterface::class);
$storage->expects($this->once()) $storage->expects($this->once())
->method('create') ->method('create')
->with([]) ->with([])
->will($this->returnValue($this->entity)); ->will($this->returnValue($this->entity));
$this->entityManager->expects($this->once())
$this->entityTypeManager->expects($this->once())
->method('getStorage') ->method('getStorage')
->with($this->entityTypeId) ->with($this->entityTypeId)
->will($this->returnValue($storage)); ->will($this->returnValue($storage));
\Drupal::getContainer()->set('entity_type.repository', $entity_type_repository);
// Call Entity::create() statically and check that it returns the mock // Call Entity::create() statically and check that it returns the mock
// entity. // entity.
$this->assertSame($this->entity, $class_name::create([])); $this->assertSame($this->entity, $class_name::create([]));
@ -345,10 +365,12 @@ class EntityUnitTest extends UnitTestCase {
$storage->expects($this->once()) $storage->expects($this->once())
->method('save') ->method('save')
->with($this->entity); ->with($this->entity);
$this->entityManager->expects($this->once())
$this->entityTypeManager->expects($this->once())
->method('getStorage') ->method('getStorage')
->with($this->entityTypeId) ->with($this->entityTypeId)
->will($this->returnValue($storage)); ->will($this->returnValue($storage));
$this->entity->save(); $this->entity->save();
} }
@ -361,10 +383,12 @@ class EntityUnitTest extends UnitTestCase {
// Testing the argument of the delete() method consumes too much memory. // Testing the argument of the delete() method consumes too much memory.
$storage->expects($this->once()) $storage->expects($this->once())
->method('delete'); ->method('delete');
$this->entityManager->expects($this->once())
$this->entityTypeManager->expects($this->once())
->method('getStorage') ->method('getStorage')
->with($this->entityTypeId) ->with($this->entityTypeId)
->will($this->returnValue($storage)); ->will($this->returnValue($storage));
$this->entity->delete(); $this->entity->delete();
} }

View File

@ -4,7 +4,7 @@ namespace Drupal\Tests\Core\Entity;
use Drupal\Core\Entity\Entity; use Drupal\Core\Entity\Entity;
use Drupal\Core\Entity\EntityMalformedException; use Drupal\Core\Entity\EntityMalformedException;
use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeInterface; use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\Exception\UndefinedLinkTemplateException; use Drupal\Core\Entity\Exception\UndefinedLinkTemplateException;
use Drupal\Core\Entity\RevisionableInterface; use Drupal\Core\Entity\RevisionableInterface;
@ -21,11 +21,11 @@ use Drupal\Tests\UnitTestCase;
class EntityUrlTest extends UnitTestCase { class EntityUrlTest extends UnitTestCase {
/** /**
* The entity manager mock used in this test. * The entity type bundle info service mock used in this test.
* *
* @var \Prophecy\Prophecy\ProphecyInterface|\Drupal\Core\Entity\EntityManagerInterface * @var \Prophecy\Prophecy\ProphecyInterface|\Drupal\Core\Entity\EntityTypeBundleInfoInterface
*/ */
protected $entityManager; protected $entityTypeBundleInfo;
/** /**
* The ID of the entity type used in this test. * The ID of the entity type used in this test.
@ -511,7 +511,7 @@ class EntityUrlTest extends UnitTestCase {
* @return \Drupal\Core\Entity\Entity|\PHPUnit_Framework_MockObject_MockObject * @return \Drupal\Core\Entity\Entity|\PHPUnit_Framework_MockObject_MockObject
*/ */
protected function getEntity($class, array $values, array $methods = []) { protected function getEntity($class, array $values, array $methods = []) {
$methods = array_merge($methods, ['getEntityType', 'entityManager']); $methods = array_merge($methods, ['getEntityType', 'entityManager', 'entityTypeBundleInfo']);
// Prophecy does not allow prophesizing abstract classes while actually // Prophecy does not allow prophesizing abstract classes while actually
// calling their code. We use Prophecy below because that allows us to // calling their code. We use Prophecy below because that allows us to
@ -526,8 +526,8 @@ class EntityUrlTest extends UnitTestCase {
$this->entityType->getKey('langcode')->willReturn(FALSE); $this->entityType->getKey('langcode')->willReturn(FALSE);
$entity->method('getEntityType')->willReturn($this->entityType->reveal()); $entity->method('getEntityType')->willReturn($this->entityType->reveal());
$this->entityManager = $this->prophesize(EntityManagerInterface::class); $this->entityTypeBundleInfo = $this->prophesize(EntityTypeBundleInfoInterface::class);
$entity->method('entityManager')->willReturn($this->entityManager->reveal()); $entity->method('entityTypeBundleInfo')->willReturn($this->entityTypeBundleInfo->reveal());
return $entity; return $entity;
} }
@ -581,7 +581,7 @@ class EntityUrlTest extends UnitTestCase {
* The bundle information to register. * The bundle information to register.
*/ */
protected function registerBundleInfo($bundle_info) { protected function registerBundleInfo($bundle_info) {
$this->entityManager $this->entityTypeBundleInfo
->getBundleInfo($this->entityTypeId) ->getBundleInfo($this->entityTypeId)
->willReturn([$this->entityTypeId => $bundle_info]) ->willReturn([$this->entityTypeId => $bundle_info])
; ;

View File

@ -4,9 +4,12 @@ namespace Drupal\Tests\Core\Entity\KeyValueStore;
use Drupal\Core\Config\Entity\ConfigEntityInterface; use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityMalformedException; use Drupal\Core\Entity\EntityMalformedException;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Entity\EntityStorageException; use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\Language; use Drupal\Core\Language\Language;
use Drupal\Tests\UnitTestCase; use Drupal\Tests\UnitTestCase;
use Drupal\Core\Entity\KeyValueStore\KeyValueEntityStorage; use Drupal\Core\Entity\KeyValueStore\KeyValueEntityStorage;
@ -58,12 +61,26 @@ class KeyValueEntityStorageTest extends UnitTestCase {
protected $entityStorage; protected $entityStorage;
/** /**
* The mocked entity manager. * The entity manager.
* *
* @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject * @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/ */
protected $entityManager; protected $entityManager;
/**
* The mocked entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $entityTypeManager;
/**
* The mocked entity field manager.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $entityFieldManager;
/** /**
* The mocked cache tags invalidator. * The mocked cache tags invalidator.
* *
@ -102,12 +119,16 @@ class KeyValueEntityStorageTest extends UnitTestCase {
->method('getListCacheTags') ->method('getListCacheTags')
->willReturn(['test_entity_type_list']); ->willReturn(['test_entity_type_list']);
$this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface'); $this->entityManager = new EntityManager();
$this->entityManager->expects($this->any())
$this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
$this->entityTypeManager->expects($this->any())
->method('getDefinition') ->method('getDefinition')
->with('test_entity_type') ->with('test_entity_type')
->will($this->returnValue($this->entityType)); ->will($this->returnValue($this->entityType));
$this->entityFieldManager = $this->getMock(EntityFieldManagerInterface::class);
$this->cacheTagsInvalidator = $this->getMock('Drupal\Core\Cache\CacheTagsInvalidatorInterface'); $this->cacheTagsInvalidator = $this->getMock('Drupal\Core\Cache\CacheTagsInvalidatorInterface');
$this->keyValueStore = $this->getMock('Drupal\Core\KeyValueStore\KeyValueStoreInterface'); $this->keyValueStore = $this->getMock('Drupal\Core\KeyValueStore\KeyValueStoreInterface');
@ -127,8 +148,13 @@ class KeyValueEntityStorageTest extends UnitTestCase {
$container = new ContainerBuilder(); $container = new ContainerBuilder();
$container->set('entity.manager', $this->entityManager); $container->set('entity.manager', $this->entityManager);
$container->set('entity_field.manager', $this->entityFieldManager);
$container->set('entity_type.manager', $this->entityTypeManager);
$container->set('language_manager', $this->languageManager); $container->set('language_manager', $this->languageManager);
$container->set('cache_tags.invalidator', $this->cacheTagsInvalidator); $container->set('cache_tags.invalidator', $this->cacheTagsInvalidator);
// Inject the container into entity.manager so it can defer to
// entity_type.manager and other services.
$this->entityManager->setContainer($container);
\Drupal::setContainer($container); \Drupal::setContainer($container);
} }

View File

@ -8,8 +8,11 @@
namespace Drupal\Tests\Core\Entity\Sql; namespace Drupal\Tests\Core\Entity\Sql;
use Drupal\Core\Cache\CacheBackendInterface; use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Query\QueryFactoryInterface; use Drupal\Core\Entity\Query\QueryFactoryInterface;
use Drupal\Core\Entity\Sql\SqlContentEntityStorage; use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
use Drupal\Core\Language\Language; use Drupal\Core\Language\Language;
@ -50,6 +53,20 @@ class SqlContentEntityStorageTest extends UnitTestCase {
*/ */
protected $entityManager; protected $entityManager;
/**
* The mocked entity type manager used in this test.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $entityTypeManager;
/**
* The mocked entity field manager used in this test.
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $entityFieldManager;
/** /**
* The entity type ID. * The entity type ID.
* *
@ -104,7 +121,12 @@ class SqlContentEntityStorageTest extends UnitTestCase {
$this->container = new ContainerBuilder(); $this->container = new ContainerBuilder();
\Drupal::setContainer($this->container); \Drupal::setContainer($this->container);
$this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface'); $this->entityManager = new EntityManager();
// Inject the container into entity.manager so it can defer to
// entity_type.manager and other services.
$this->entityManager->setContainer($this->container);
$this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
$this->entityFieldManager = $this->getMock(EntityFieldManagerInterface::class);
$this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface'); $this->moduleHandler = $this->getMock('Drupal\Core\Extension\ModuleHandlerInterface');
$this->cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface'); $this->cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
$this->languageManager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface'); $this->languageManager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
@ -114,6 +136,10 @@ class SqlContentEntityStorageTest extends UnitTestCase {
$this->connection = $this->getMockBuilder('Drupal\Core\Database\Connection') $this->connection = $this->getMockBuilder('Drupal\Core\Database\Connection')
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$this->container->set('entity.manager', $this->entityManager);
$this->container->set('entity_type.manager', $this->entityTypeManager);
$this->container->set('entity_field.manager', $this->entityFieldManager);
} }
/** /**
@ -986,7 +1012,6 @@ class SqlContentEntityStorageTest extends UnitTestCase {
->will($this->returnValue($language)); ->will($this->returnValue($language));
$this->container->set('language_manager', $language_manager); $this->container->set('language_manager', $language_manager);
$this->container->set('entity.manager', $this->entityManager);
$this->container->set('module_handler', $this->moduleHandler); $this->container->set('module_handler', $this->moduleHandler);
$entity = $this->getMockBuilder('Drupal\Core\Entity\ContentEntityBase') $entity = $this->getMockBuilder('Drupal\Core\Entity\ContentEntityBase')
@ -1006,14 +1031,14 @@ class SqlContentEntityStorageTest extends UnitTestCase {
// ContentEntityStorageBase iterates over the entity which calls this method // ContentEntityStorageBase iterates over the entity which calls this method
// internally in ContentEntityBase::getProperties(). // internally in ContentEntityBase::getProperties().
$this->entityManager->expects($this->once()) $this->entityFieldManager->expects($this->once())
->method('getFieldDefinitions') ->method('getFieldDefinitions')
->will($this->returnValue([])); ->will($this->returnValue([]));
$this->entityType->expects($this->atLeastOnce()) $this->entityType->expects($this->atLeastOnce())
->method('isRevisionable') ->method('isRevisionable')
->will($this->returnValue(FALSE)); ->will($this->returnValue(FALSE));
$this->entityManager->expects($this->atLeastOnce()) $this->entityTypeManager->expects($this->atLeastOnce())
->method('getDefinition') ->method('getDefinition')
->with($this->entityType->id()) ->with($this->entityType->id())
->will($this->returnValue($this->entityType)); ->will($this->returnValue($this->entityType));
@ -1077,15 +1102,15 @@ class SqlContentEntityStorageTest extends UnitTestCase {
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$this->entityManager->expects($this->any()) $this->entityTypeManager->expects($this->any())
->method('getDefinition') ->method('getDefinition')
->will($this->returnValue($this->entityType)); ->will($this->returnValue($this->entityType));
$this->entityManager->expects($this->any()) $this->entityFieldManager->expects($this->any())
->method('getFieldStorageDefinitions') ->method('getFieldStorageDefinitions')
->will($this->returnValue($this->fieldDefinitions)); ->will($this->returnValue($this->fieldDefinitions));
$this->entityManager->expects($this->any()) $this->entityFieldManager->expects($this->any())
->method('getBaseFieldDefinitions') ->method('getBaseFieldDefinitions')
->will($this->returnValue($this->fieldDefinitions)); ->will($this->returnValue($this->fieldDefinitions));
@ -1259,15 +1284,15 @@ class SqlContentEntityStorageTest extends UnitTestCase {
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$this->entityManager->expects($this->any()) $this->entityTypeManager->expects($this->any())
->method('getDefinition') ->method('getDefinition')
->will($this->returnValue($this->entityType)); ->will($this->returnValue($this->entityType));
$this->entityManager->expects($this->any()) $this->entityFieldManager->expects($this->any())
->method('getFieldStorageDefinitions') ->method('getFieldStorageDefinitions')
->will($this->returnValue($this->fieldDefinitions)); ->will($this->returnValue($this->fieldDefinitions));
$this->entityManager->expects($this->any()) $this->entityFieldManager->expects($this->any())
->method('getBaseFieldDefinitions') ->method('getBaseFieldDefinitions')
->will($this->returnValue($this->fieldDefinitions)); ->will($this->returnValue($this->fieldDefinitions));

View File

@ -3,6 +3,9 @@
namespace Drupal\Tests\Core\Entity\TypedData; namespace Drupal\Tests\Core\Entity\TypedData;
use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Plugin\DataType\EntityAdapter; use Drupal\Core\Entity\Plugin\DataType\EntityAdapter;
use Drupal\Core\Field\BaseFieldDefinition; use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Language\LanguageInterface;
@ -53,6 +56,19 @@ class EntityAdapterUnitTest extends UnitTestCase {
*/ */
protected $entityManager; protected $entityManager;
/**
* The entity type manager used for testing.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $entityTypeManager;
/**
*
* @var \Drupal\Core\Entity\EntityFieldManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
protected $entityFieldManager;
/** /**
* The type ID of the entity under test. * The type ID of the entity under test.
* *
@ -130,8 +146,10 @@ class EntityAdapterUnitTest extends UnitTestCase {
'uuid' => 'uuid', 'uuid' => 'uuid',
])); ]));
$this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface'); $this->entityManager = new EntityManager();
$this->entityManager->expects($this->any())
$this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
$this->entityTypeManager->expects($this->any())
->method('getDefinition') ->method('getDefinition')
->with($this->entityTypeId) ->with($this->entityTypeId)
->will($this->returnValue($this->entityType)); ->will($this->returnValue($this->entityType));
@ -183,23 +201,30 @@ class EntityAdapterUnitTest extends UnitTestCase {
->method('createFieldItemList') ->method('createFieldItemList')
->willReturn($this->fieldItemList); ->willReturn($this->fieldItemList);
$this->entityFieldManager = $this->getMockForAbstractClass(EntityFieldManagerInterface::class);
$container = new ContainerBuilder(); $container = new ContainerBuilder();
$container->set('entity.manager', $this->entityManager); $container->set('entity.manager', $this->entityManager);
$container->set('entity_type.manager', $this->entityTypeManager);
$container->set('entity_field.manager', $this->entityFieldManager);
$container->set('uuid', $this->uuid); $container->set('uuid', $this->uuid);
$container->set('typed_data_manager', $this->typedDataManager); $container->set('typed_data_manager', $this->typedDataManager);
$container->set('language_manager', $this->languageManager); $container->set('language_manager', $this->languageManager);
$container->set('plugin.manager.field.field_type', $this->fieldTypePluginManager); $container->set('plugin.manager.field.field_type', $this->fieldTypePluginManager);
// Inject the container into entity.manager so it can defer to
// entity_type.manager and other services.
$this->entityManager->setContainer($container);
\Drupal::setContainer($container); \Drupal::setContainer($container);
$this->fieldDefinitions = [ $this->fieldDefinitions = [
'id' => BaseFieldDefinition::create('integer'), 'id' => BaseFieldDefinition::create('integer'),
'revision_id' => BaseFieldDefinition::create('integer'), 'revision_id' => BaseFieldDefinition::create('integer'),
]; ];
$this->entityFieldManager->expects($this->any())
$this->entityManager->expects($this->any())
->method('getFieldDefinitions') ->method('getFieldDefinitions')
->with($this->entityTypeId, $this->bundle) ->with($this->entityTypeId, $this->bundle)
->will($this->returnValue($this->fieldDefinitions)); ->will($this->returnValue($this->fieldDefinitions));
$this->entity = $this->getMockForAbstractClass('\Drupal\Core\Entity\ContentEntityBase', [$values, $this->entityTypeId, $this->bundle]); $this->entity = $this->getMockForAbstractClass('\Drupal\Core\Entity\ContentEntityBase', [$values, $this->entityTypeId, $this->bundle]);
$this->entityAdapter = EntityAdapter::createFromEntity($this->entity); $this->entityAdapter = EntityAdapter::createFromEntity($this->entity);