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();
}
/**
* 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.
*
@ -198,7 +207,7 @@ abstract class Entity implements EntityInterface {
$bundle = $this->bundle();
// A bundle-specific callback takes precedence over the generic one for
// the entity type.
$bundles = $this->entityManager()->getBundleInfo($this->getEntityTypeId());
$bundles = $this->entityTypeBundleInfo()->getBundleInfo($this->getEntityTypeId());
if (isset($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) {
if ($operation == 'create') {
return $this->entityManager()
return $this->entityTypeManager()
->getAccessControlHandler($this->entityTypeId)
->createAccess($this->bundle(), $account, [], $return_as_object);
}
return $this->entityManager()
return $this->entityTypeManager()
->getAccessControlHandler($this->entityTypeId)
->access($this, $operation, $account, $return_as_object);
}
@ -374,7 +383,8 @@ abstract class Entity implements EntityInterface {
* {@inheritdoc}
*/
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() {
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}
*/
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}
*/
public static function load($id) {
$entity_manager = \Drupal::entityManager();
return $entity_manager->getStorage($entity_manager->getEntityTypeFromClass(get_called_class()))->load($id);
$entity_type_repository = \Drupal::service('entity_type.repository');
$entity_type_manager = \Drupal::entityTypeManager();
$storage = $entity_type_manager->getStorage($entity_type_repository->getEntityTypeFromClass(get_called_class()));
return $storage->load($id);
}
/**
* {@inheritdoc}
*/
public static function loadMultiple(array $ids = NULL) {
$entity_manager = \Drupal::entityManager();
return $entity_manager->getStorage($entity_manager->getEntityTypeFromClass(get_called_class()))->loadMultiple($ids);
$entity_type_repository = \Drupal::service('entity_type.repository');
$entity_type_manager = \Drupal::entityTypeManager();
$storage = $entity_type_manager->getStorage($entity_type_repository->getEntityTypeFromClass(get_called_class()));
return $storage->loadMultiple($ids);
}
/**
* {@inheritdoc}
*/
public static function create(array $values = []) {
$entity_manager = \Drupal::entityManager();
return $entity_manager->getStorage($entity_manager->getEntityTypeFromClass(get_called_class()))->create($values);
$entity_type_repository = \Drupal::service('entity_type.repository');
$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;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Tests\Core\Plugin\Fixtures\TestConfigurablePlugin;
use Drupal\Tests\UnitTestCase;
@ -20,11 +21,11 @@ class BlockConfigEntityUnitTest 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.
@ -51,8 +52,8 @@ class BlockConfigEntityUnitTest extends UnitTestCase {
->method('getProvider')
->will($this->returnValue('block'));
$this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
$this->entityManager->expects($this->any())
$this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
$this->entityTypeManager->expects($this->any())
->method('getDefinition')
->with($this->entityTypeId)
->will($this->returnValue($this->entityType));
@ -60,7 +61,7 @@ class BlockConfigEntityUnitTest extends UnitTestCase {
$this->uuid = $this->getMock('\Drupal\Component\Uuid\UuidInterface');
$container = new ContainerBuilder();
$container->set('entity.manager', $this->entityManager);
$container->set('entity_type.manager', $this->entityTypeManager);
$container->set('uuid', $this->uuid);
\Drupal::setContainer($container);
}

View File

@ -3,6 +3,8 @@
namespace Drupal\Tests\editor\Unit;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\editor\Entity\Editor;
use Drupal\Tests\UnitTestCase;
@ -22,9 +24,9 @@ class EditorConfigEntityUnitTest extends UnitTestCase {
/**
* 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.
@ -66,8 +68,8 @@ class EditorConfigEntityUnitTest extends UnitTestCase {
->method('getProvider')
->will($this->returnValue('editor'));
$this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
$this->entityManager->expects($this->any())
$this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
$this->entityTypeManager->expects($this->any())
->method('getDefinition')
->with($this->entityTypeId)
->will($this->returnValue($this->entityType));
@ -78,10 +80,16 @@ class EditorConfigEntityUnitTest extends UnitTestCase {
->disableOriginalConstructor()
->getMock();
$entity_manager = new EntityManager();
$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('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);
}
@ -120,7 +128,7 @@ class EditorConfigEntityUnitTest extends UnitTestCase {
->with($format_id)
->will($this->returnValue($filter_format));
$this->entityManager->expects($this->once())
$this->entityTypeManager->expects($this->once())
->method('getStorage')
->with('filter_format')
->will($this->returnValue($storage));

View File

@ -10,6 +10,9 @@ namespace Drupal\Tests\field\Unit;
use Drupal\Core\Entity\EntityType;
use Drupal\Core\Field\FieldDefinitionInterface;
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\Tests\UnitTestCase;
@ -33,6 +36,20 @@ class FieldConfigEntityUnitTest extends UnitTestCase {
*/
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.
*
@ -75,7 +92,9 @@ class FieldConfigEntityUnitTest extends UnitTestCase {
$this->entityTypeId = $this->randomMachineName();
$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');
@ -85,9 +104,14 @@ class FieldConfigEntityUnitTest extends UnitTestCase {
$container = new ContainerBuilder();
$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('config.typed', $this->typedConfigManager);
$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);
// Create a mock FieldStorageConfig object.
@ -102,7 +126,7 @@ class FieldConfigEntityUnitTest extends UnitTestCase {
->method('getSettings')
->willReturn([]);
// Place the field in the mocked entity manager's field registry.
$this->entityManager->expects($this->any())
$this->entityFieldManager->expects($this->any())
->method('getFieldStorageDefinitions')
->with('test_entity_type')
->will($this->returnValue([
@ -120,19 +144,19 @@ class FieldConfigEntityUnitTest extends UnitTestCase {
->method('getBundleConfigDependency')
->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')
->with($this->entityTypeId)
->willReturn($this->entityType);
$this->entityManager->expects($this->at(1))
$this->entityTypeManager->expects($this->at(1))
->method('getDefinition')
->with($this->entityTypeId)
->willReturn($this->entityType);
$this->entityManager->expects($this->at(2))
$this->entityTypeManager->expects($this->at(2))
->method('getDefinition')
->with($this->entityTypeId)
->willReturn($this->entityType);
$this->entityManager->expects($this->at(3))
$this->entityTypeManager->expects($this->at(3))
->method('getDefinition')
->with('test_entity_type')
->willReturn($target_entity_type);
@ -168,7 +192,7 @@ class FieldConfigEntityUnitTest extends UnitTestCase {
->with('test_bundle_not_exists')
->will($this->returnValue(NULL));
$this->entityManager->expects($this->any())
$this->entityTypeManager->expects($this->any())
->method('getStorage')
->with('bundle_entity_type')
->will($this->returnValue($storage));
@ -178,19 +202,19 @@ class FieldConfigEntityUnitTest extends UnitTestCase {
'bundle_entity_type' => 'bundle_entity_type',
]);
$this->entityManager->expects($this->at(0))
$this->entityTypeManager->expects($this->at(0))
->method('getDefinition')
->with($this->entityTypeId)
->willReturn($this->entityType);
$this->entityManager->expects($this->at(1))
$this->entityTypeManager->expects($this->at(1))
->method('getDefinition')
->with($this->entityTypeId)
->willReturn($this->entityType);
$this->entityManager->expects($this->at(2))
$this->entityTypeManager->expects($this->at(2))
->method('getDefinition')
->with($this->entityTypeId)
->willReturn($this->entityType);
$this->entityManager->expects($this->at(3))
$this->entityTypeManager->expects($this->at(3))
->method('getDefinition')
->with('test_entity_type')
->willReturn($target_entity_type);
@ -267,7 +291,7 @@ class FieldConfigEntityUnitTest extends UnitTestCase {
'dependencies' => [],
'field_type' => 'test_field',
];
$this->entityManager->expects($this->any())
$this->entityTypeManager->expects($this->any())
->method('getDefinition')
->with($this->entityTypeId)
->will($this->returnValue($this->entityType));
@ -289,7 +313,7 @@ class FieldConfigEntityUnitTest extends UnitTestCase {
public function testGetType() {
// Ensure that FieldConfig::getType() is not delegated to
// FieldStorage.
$this->entityManager->expects($this->never())
$this->entityFieldManager->expects($this->never())
->method('getFieldStorageDefinitions');
$this->fieldStorage->expects($this->never())
->method('getType');

View File

@ -6,8 +6,9 @@ use Drupal\Component\Uuid\UuidInterface;
use Drupal\Core\Cache\Context\CacheContextsManager;
use Drupal\Core\Config\Entity\ConfigEntityTypeInterface;
use Drupal\Core\DependencyInjection\Container;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\field\Entity\FieldStorageConfig;
@ -126,31 +127,37 @@ class FieldStorageConfigAccessControlHandlerTest extends UnitTestCase {
$storage_access_control_handler = new FieldStorageConfigAccessControlHandler($storageType);
$storage_access_control_handler->setModuleHandler($this->moduleHandler);
$entityManager = $this->getMock(EntityManagerInterface::class);
$entityManager
$entity_type_manager = $this->getMock(EntityTypeManagerInterface::class);
$entity_type_manager
->expects($this->any())
->method('getDefinition')
->willReturnMap([
['field_storage_config', TRUE, $storageType],
['node', TRUE, $entityType],
]);
$entityManager
$entity_type_manager
->expects($this->any())
->method('getStorage')
->willReturnMap([
['field_storage_config', $this->getMock(EntityStorageInterface::class)],
]);
$entityManager
$entity_type_manager
->expects($this->any())
->method('getAccessControlHandler')
->willReturnMap([
['field_storage_config', $storage_access_control_handler],
]);
$entity_manager = new EntityManager();
$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('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);
$this->fieldStorage = new FieldStorageConfig([

View File

@ -8,6 +8,8 @@
namespace Drupal\Tests\field\Unit;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldException;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Field\FieldTypePluginManagerInterface;
@ -22,11 +24,11 @@ use Drupal\Tests\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.
@ -53,14 +55,19 @@ class FieldStorageConfigEntityUnitTest extends UnitTestCase {
* {@inheritdoc}
*/
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->fieldTypeManager = $this->getMock(FieldTypePluginManagerInterface::class);
$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('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);
}
@ -85,7 +92,7 @@ class FieldStorageConfigEntityUnitTest extends UnitTestCase {
// ConfigEntityBase::addDependency() to get the provider of the field config
// entity type and once in FieldStorageConfig::calculateDependencies() 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')
->willReturnMap([
['field_storage_config', TRUE, $fieldStorageConfigentityType],

View File

@ -4,6 +4,9 @@ namespace Drupal\Tests\language\Unit;
use Drupal\Core\Language\LanguageInterface;
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\Tests\UnitTestCase;
@ -27,6 +30,13 @@ class ContentLanguageSettingsUnitTest extends UnitTestCase {
*/
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.
*
@ -62,7 +72,8 @@ class ContentLanguageSettingsUnitTest extends UnitTestCase {
$this->entityTypeId = $this->randomMachineName();
$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');
@ -72,9 +83,13 @@ class ContentLanguageSettingsUnitTest extends UnitTestCase {
$container = new ContainerBuilder();
$container->set('entity.manager', $this->entityManager);
$container->set('entity_type.manager', $this->entityTypeManager);
$container->set('uuid', $this->uuid);
$container->set('config.typed', $this->typedConfigManager);
$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);
}
@ -88,7 +103,7 @@ class ContentLanguageSettingsUnitTest extends UnitTestCase {
->method('getBundleConfigDependency')
->will($this->returnValue(['type' => 'config', 'name' => 'test.test_entity_type.id']));
$this->entityManager->expects($this->any())
$this->entityTypeManager->expects($this->any())
->method('getDefinition')
->with('test_entity_type')
->will($this->returnValue($target_entity_type));
@ -254,16 +269,20 @@ class ContentLanguageSettingsUnitTest extends UnitTestCase {
->method('create')
->will($this->returnValue($nullConfig));
$this->entityManager
$this->entityTypeManager
->expects($this->any())
->method('getStorage')
->with('language_content_settings')
->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')
->with('Drupal\language\Entity\ContentLanguageSettings')
->with(ContentLanguageSettings::class)
->willReturn('language_content_settings');
\Drupal::getContainer()->set('entity_type.repository', $entity_type_repository);
$config = ContentLanguageSettings::loadByEntityTypeBundle($type, $bundle);
$this->assertSame($expected_langcode, $config->getDefaultLangcode());

View File

@ -3,6 +3,8 @@
namespace Drupal\Tests\rdf\Unit;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\rdf\Entity\RdfMapping;
@ -26,6 +28,13 @@ class RdfMappingConfigEntityUnitTest extends UnitTestCase {
*/
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.
*
@ -51,13 +60,16 @@ class RdfMappingConfigEntityUnitTest extends UnitTestCase {
->method('getProvider')
->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');
$container = new ContainerBuilder();
$container->set('entity.manager', $this->entityManager);
$container->set('entity_type.manager', $this->entityTypeManager);
$container->set('uuid', $this->uuid);
$this->entityManager->setContainer($container);
\Drupal::setContainer($container);
}
@ -77,11 +89,11 @@ class RdfMappingConfigEntityUnitTest extends UnitTestCase {
->method('getBundleEntityType')
->will($this->returnValue(NULL));
$this->entityManager->expects($this->at(0))
$this->entityTypeManager->expects($this->at(0))
->method('getDefinition')
->with($target_entity_type_id)
->will($this->returnValue($target_entity_type));
$this->entityManager->expects($this->at(1))
$this->entityTypeManager->expects($this->at(1))
->method('getDefinition')
->with($this->entityTypeId)
->will($this->returnValue($this->entityType));
@ -108,11 +120,11 @@ class RdfMappingConfigEntityUnitTest extends UnitTestCase {
->method('getBundleConfigDependency')
->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')
->with($target_entity_type_id)
->will($this->returnValue($target_entity_type));
$this->entityManager->expects($this->at(1))
$this->entityTypeManager->expects($this->at(1))
->method('getDefinition')
->with($this->entityTypeId)
->will($this->returnValue($this->entityType));

View File

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

View File

@ -3,6 +3,8 @@
namespace Drupal\Tests\user\Unit\Views\Argument;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Tests\UnitTestCase;
use Drupal\user\Entity\Role;
use Drupal\user\Plugin\views\argument\RolesRid;
@ -44,23 +46,27 @@ class RolesRidTest extends UnitTestCase {
->with('label')
->will($this->returnValue('label'));
$entity_manager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
$entity_manager->expects($this->any())
$entity_manager = new EntityManager();
$entity_type_manager = $this->getMock(EntityTypeManagerInterface::class);
$entity_type_manager->expects($this->any())
->method('getDefinition')
->with($this->equalTo('user_role'))
->will($this->returnValue($entity_type));
$entity_manager
$entity_type_manager
->expects($this->once())
->method('getStorage')
->with($this->equalTo('user_role'))
->will($this->returnValue($role_storage));
// @todo \Drupal\Core\Entity\Entity::entityType() uses a global call to
// entity_get_info(), which in turn wraps \Drupal::entityManager(). Set
// the entity manager until this is fixed.
// Set up a minimal container to satisfy Drupal\Core\Entity\Entity's
// dependency on it.
$container = new ContainerBuilder();
$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);
$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\Core\Config\Schema\SchemaIncompleteException;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\Language;
use Drupal\Core\Plugin\DefaultLazyPluginCollection;
use Drupal\Tests\Core\Config\Entity\Fixtures\ConfigEntityBaseWithPluginCollections;
@ -37,11 +38,11 @@ class ConfigEntityBaseUnitTest 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.
@ -112,8 +113,8 @@ class ConfigEntityBaseUnitTest extends UnitTestCase {
->method('getConfigPrefix')
->willReturn('test_provider.' . $this->entityTypeId);
$this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
$this->entityManager->expects($this->any())
$this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
$this->entityTypeManager->expects($this->any())
->method('getDefinition')
->with($this->entityTypeId)
->will($this->returnValue($this->entityType));
@ -131,7 +132,7 @@ class ConfigEntityBaseUnitTest extends UnitTestCase {
$this->typedConfigManager = $this->getMock('Drupal\Core\Config\TypedConfigManagerInterface');
$container = new ContainerBuilder();
$container->set('entity.manager', $this->entityManager);
$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);
@ -468,7 +469,7 @@ class ConfigEntityBaseUnitTest extends UnitTestCase {
* @covers ::sort
*/
public function testSort() {
$this->entityManager->expects($this->any())
$this->entityTypeManager->expects($this->any())
->method('getDefinition')
->with($this->entityTypeId)
->will($this->returnValue([

View File

@ -17,8 +17,8 @@ use Drupal\Core\Config\ImmutableConfig;
use Drupal\Core\Config\TypedConfigManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityMalformedException;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Query\QueryFactoryInterface;
use Drupal\Core\Entity\Query\QueryInterface;
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->setModuleHandler($this->moduleHandler->reveal());
$entity_manager = $this->prophesize(EntityManagerInterface::class);
$entity_manager->getDefinition('test_entity_type')->willReturn($entity_type);
$entity_type_manager = $this->prophesize(EntityTypeManagerInterface::class);
$entity_type_manager->getDefinition('test_entity_type')->willReturn($entity_type);
$this->cacheTagsInvalidator = $this->prophesize(CacheTagsInvalidatorInterface::class);
@ -149,7 +149,7 @@ class ConfigEntityStorageTest extends UnitTestCase {
$this->configManager = $this->prophesize(ConfigManagerInterface::class);
$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('config.typed', $typed_config_manager->reveal());
$container->set('cache_tags.invalidator', $this->cacheTagsInvalidator->reveal());

View File

@ -3,6 +3,8 @@
namespace Drupal\Tests\Core\Config\Entity;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Tests\UnitTestCase;
/**
@ -32,6 +34,13 @@ class EntityDisplayModeBaseUnitTest extends UnitTestCase {
*/
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.
*
@ -57,13 +66,21 @@ class EntityDisplayModeBaseUnitTest extends UnitTestCase {
->method('getProvider')
->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');
$container = new ContainerBuilder();
$container->set('entity.manager', $this->entityManager);
$container->set('entity_type.manager', $this->entityTypeManager);
$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);
}
@ -79,11 +96,11 @@ class EntityDisplayModeBaseUnitTest extends UnitTestCase {
->will($this->returnValue('test_module'));
$values = ['targetEntityType' => $target_entity_type_id];
$this->entityManager->expects($this->at(0))
$this->entityTypeManager->expects($this->at(0))
->method('getDefinition')
->with($target_entity_type_id)
->will($this->returnValue($target_entity_type));
$this->entityManager->expects($this->at(1))
$this->entityTypeManager->expects($this->at(1))
->method('getDefinition')
->with($this->entityType)
->will($this->returnValue($this->entityInfo));

View File

@ -4,7 +4,12 @@ namespace Drupal\Tests\Core\Entity;
use Drupal\Core\Access\AccessResult;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\ContentEntityBase;
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\Language\LanguageInterface;
use Drupal\Core\TypedData\TypedDataManagerInterface;
@ -54,6 +59,27 @@ class ContentEntityBaseUnitTest extends UnitTestCase {
*/
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.
*
@ -124,12 +150,18 @@ class ContentEntityBaseUnitTest extends UnitTestCase {
'uuid' => 'uuid',
]));
$this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
$this->entityManager->expects($this->any())
$this->entityManager = new EntityManager();
$this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
$this->entityTypeManager->expects($this->any())
->method('getDefinition')
->with($this->entityTypeId)
->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->typedDataManager = $this->getMock(TypedDataManagerInterface::class);
@ -168,10 +200,16 @@ class ContentEntityBaseUnitTest extends UnitTestCase {
$container = new ContainerBuilder();
$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('typed_data_manager', $this->typedDataManager);
$container->set('language_manager', $this->languageManager);
$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);
$this->fieldDefinitions = [
@ -179,14 +217,14 @@ class ContentEntityBaseUnitTest extends UnitTestCase {
'revision_id' => BaseFieldDefinition::create('integer'),
];
$this->entityManager->expects($this->any())
$this->entityFieldManager->expects($this->any())
->method('getFieldDefinitions')
->with($this->entityTypeId, $this->bundle)
->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];
$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
*/
public function testIsTranslatable() {
$this->entityManager->expects($this->any())
$this->entityTypeBundleInfo->expects($this->any())
->method('getBundleInfo')
->with($this->entityTypeId)
->will($this->returnValue([
@ -382,7 +420,7 @@ class ContentEntityBaseUnitTest extends UnitTestCase {
$entity->preSave($storage);
});
$this->entityManager->expects($this->any())
$this->entityTypeManager->expects($this->any())
->method('getStorage')
->with($this->entityTypeId)
->will($this->returnValue($storage));
@ -434,7 +472,7 @@ class ContentEntityBaseUnitTest extends UnitTestCase {
$access->expects($this->at(3))
->method('createAccess')
->will($this->returnValue(AccessResult::allowed()));
$this->entityManager->expects($this->exactly(4))
$this->entityTypeManager->expects($this->exactly(4))
->method('getAccessControlHandler')
->will($this->returnValue($access));
$this->assertTrue($this->entity->access($operation));

View File

@ -3,6 +3,7 @@
namespace Drupal\Tests\Core\Entity;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\Language;
use Drupal\Core\Link;
use Drupal\Tests\UnitTestCase;
@ -14,11 +15,11 @@ use Drupal\Tests\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.
@ -40,12 +41,12 @@ class EntityLinkTest extends UnitTestCase {
protected function 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->languageManager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
$container = new ContainerBuilder();
$container->set('entity.manager', $this->entityManager);
$container->set('entity_type.manager', $this->entityTypeManager);
$container->set('link_generator', $this->linkGenerator);
$container->set('language_manager', $this->languageManager);
\Drupal::setContainer($container);
@ -86,7 +87,7 @@ class EntityLinkTest extends UnitTestCase {
['langcode', 'langcode'],
]);
$this->entityManager
$this->entityTypeManager
->expects($this->any())
->method('getDefinition')
->with($entity_type_id)
@ -148,7 +149,7 @@ class EntityLinkTest extends UnitTestCase {
['langcode', 'langcode'],
]);
$this->entityManager
$this->entityTypeManager
->expects($this->any())
->method('getDefinition')
->with($entity_type_id)

View File

@ -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);
@ -186,7 +192,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 +219,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 +246,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 +267,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 +301,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 +332,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 +365,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 +383,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();
}

View File

@ -4,7 +4,7 @@ namespace Drupal\Tests\Core\Entity;
use Drupal\Core\Entity\Entity;
use Drupal\Core\Entity\EntityMalformedException;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityTypeBundleInfoInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Entity\Exception\UndefinedLinkTemplateException;
use Drupal\Core\Entity\RevisionableInterface;
@ -21,11 +21,11 @@ use Drupal\Tests\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.
@ -511,7 +511,7 @@ class EntityUrlTest extends UnitTestCase {
* @return \Drupal\Core\Entity\Entity|\PHPUnit_Framework_MockObject_MockObject
*/
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
// 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);
$entity->method('getEntityType')->willReturn($this->entityType->reveal());
$this->entityManager = $this->prophesize(EntityManagerInterface::class);
$entity->method('entityManager')->willReturn($this->entityManager->reveal());
$this->entityTypeBundleInfo = $this->prophesize(EntityTypeBundleInfoInterface::class);
$entity->method('entityTypeBundleInfo')->willReturn($this->entityTypeBundleInfo->reveal());
return $entity;
}
@ -581,7 +581,7 @@ class EntityUrlTest extends UnitTestCase {
* The bundle information to register.
*/
protected function registerBundleInfo($bundle_info) {
$this->entityManager
$this->entityTypeBundleInfo
->getBundleInfo($this->entityTypeId)
->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\DependencyInjection\ContainerBuilder;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityMalformedException;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Entity\EntityStorageException;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\Language;
use Drupal\Tests\UnitTestCase;
use Drupal\Core\Entity\KeyValueStore\KeyValueEntityStorage;
@ -58,12 +61,26 @@ class KeyValueEntityStorageTest extends UnitTestCase {
protected $entityStorage;
/**
* The mocked entity manager.
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
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.
*
@ -102,12 +119,16 @@ class KeyValueEntityStorageTest extends UnitTestCase {
->method('getListCacheTags')
->willReturn(['test_entity_type_list']);
$this->entityManager = $this->getMock('Drupal\Core\Entity\EntityManagerInterface');
$this->entityManager->expects($this->any())
$this->entityManager = new EntityManager();
$this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
$this->entityTypeManager->expects($this->any())
->method('getDefinition')
->with('test_entity_type')
->will($this->returnValue($this->entityType));
$this->entityFieldManager = $this->getMock(EntityFieldManagerInterface::class);
$this->cacheTagsInvalidator = $this->getMock('Drupal\Core\Cache\CacheTagsInvalidatorInterface');
$this->keyValueStore = $this->getMock('Drupal\Core\KeyValueStore\KeyValueStoreInterface');
@ -127,8 +148,13 @@ class KeyValueEntityStorageTest extends UnitTestCase {
$container = new ContainerBuilder();
$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('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);
}

View File

@ -8,8 +8,11 @@
namespace Drupal\Tests\Core\Entity\Sql;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Entity\Query\QueryFactoryInterface;
use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
use Drupal\Core\Language\Language;
@ -50,6 +53,20 @@ class SqlContentEntityStorageTest extends UnitTestCase {
*/
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.
*
@ -104,7 +121,12 @@ class SqlContentEntityStorageTest extends UnitTestCase {
$this->container = new ContainerBuilder();
\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->cache = $this->getMock('Drupal\Core\Cache\CacheBackendInterface');
$this->languageManager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface');
@ -114,6 +136,10 @@ class SqlContentEntityStorageTest extends UnitTestCase {
$this->connection = $this->getMockBuilder('Drupal\Core\Database\Connection')
->disableOriginalConstructor()
->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));
$this->container->set('language_manager', $language_manager);
$this->container->set('entity.manager', $this->entityManager);
$this->container->set('module_handler', $this->moduleHandler);
$entity = $this->getMockBuilder('Drupal\Core\Entity\ContentEntityBase')
@ -1006,14 +1031,14 @@ class SqlContentEntityStorageTest extends UnitTestCase {
// ContentEntityStorageBase iterates over the entity which calls this method
// internally in ContentEntityBase::getProperties().
$this->entityManager->expects($this->once())
$this->entityFieldManager->expects($this->once())
->method('getFieldDefinitions')
->will($this->returnValue([]));
$this->entityType->expects($this->atLeastOnce())
->method('isRevisionable')
->will($this->returnValue(FALSE));
$this->entityManager->expects($this->atLeastOnce())
$this->entityTypeManager->expects($this->atLeastOnce())
->method('getDefinition')
->with($this->entityType->id())
->will($this->returnValue($this->entityType));
@ -1077,15 +1102,15 @@ class SqlContentEntityStorageTest extends UnitTestCase {
->disableOriginalConstructor()
->getMock();
$this->entityManager->expects($this->any())
$this->entityTypeManager->expects($this->any())
->method('getDefinition')
->will($this->returnValue($this->entityType));
$this->entityManager->expects($this->any())
$this->entityFieldManager->expects($this->any())
->method('getFieldStorageDefinitions')
->will($this->returnValue($this->fieldDefinitions));
$this->entityManager->expects($this->any())
$this->entityFieldManager->expects($this->any())
->method('getBaseFieldDefinitions')
->will($this->returnValue($this->fieldDefinitions));
@ -1259,15 +1284,15 @@ class SqlContentEntityStorageTest extends UnitTestCase {
->disableOriginalConstructor()
->getMock();
$this->entityManager->expects($this->any())
$this->entityTypeManager->expects($this->any())
->method('getDefinition')
->will($this->returnValue($this->entityType));
$this->entityManager->expects($this->any())
$this->entityFieldManager->expects($this->any())
->method('getFieldStorageDefinitions')
->will($this->returnValue($this->fieldDefinitions));
$this->entityManager->expects($this->any())
$this->entityFieldManager->expects($this->any())
->method('getBaseFieldDefinitions')
->will($this->returnValue($this->fieldDefinitions));

View File

@ -3,6 +3,9 @@
namespace Drupal\Tests\Core\Entity\TypedData;
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\Field\BaseFieldDefinition;
use Drupal\Core\Language\LanguageInterface;
@ -53,6 +56,19 @@ class EntityAdapterUnitTest extends UnitTestCase {
*/
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.
*
@ -130,8 +146,10 @@ class EntityAdapterUnitTest extends UnitTestCase {
'uuid' => 'uuid',
]));
$this->entityManager = $this->getMock('\Drupal\Core\Entity\EntityManagerInterface');
$this->entityManager->expects($this->any())
$this->entityManager = new EntityManager();
$this->entityTypeManager = $this->getMock(EntityTypeManagerInterface::class);
$this->entityTypeManager->expects($this->any())
->method('getDefinition')
->with($this->entityTypeId)
->will($this->returnValue($this->entityType));
@ -183,23 +201,30 @@ class EntityAdapterUnitTest extends UnitTestCase {
->method('createFieldItemList')
->willReturn($this->fieldItemList);
$this->entityFieldManager = $this->getMockForAbstractClass(EntityFieldManagerInterface::class);
$container = new ContainerBuilder();
$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('typed_data_manager', $this->typedDataManager);
$container->set('language_manager', $this->languageManager);
$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);
$this->fieldDefinitions = [
'id' => BaseFieldDefinition::create('integer'),
'revision_id' => BaseFieldDefinition::create('integer'),
];
$this->entityManager->expects($this->any())
$this->entityFieldManager->expects($this->any())
->method('getFieldDefinitions')
->with($this->entityTypeId, $this->bundle)
->will($this->returnValue($this->fieldDefinitions));
$this->entity = $this->getMockForAbstractClass('\Drupal\Core\Entity\ContentEntityBase', [$values, $this->entityTypeId, $this->bundle]);
$this->entityAdapter = EntityAdapter::createFromEntity($this->entity);