Same filename and directory in other branches
- 8.9.x core/tests/Drupal/KernelTests/Core/Entity/EntityKernelTestBase.php
- 9 core/tests/Drupal/KernelTests/Core/Entity/EntityKernelTestBase.php
Namespace
Drupal\KernelTests\Core\Entity
File
core/tests/Drupal/KernelTests/Core/Entity/EntityKernelTestBase.php
View source
<?php
declare (strict_types=1);
namespace Drupal\KernelTests\Core\Entity;
use Drupal\Core\Entity\EntityInterface;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Tests\user\Traits\UserCreationTrait;
abstract class EntityKernelTestBase extends KernelTestBase {
use UserCreationTrait {
checkPermissions as drupalCheckPermissions;
createAdminRole as drupalCreateAdminRole;
createRole as drupalCreateRole;
createUser as drupalCreateUser;
grantPermissions as drupalGrantPermissions;
setCurrentUser as drupalSetCurrentUser;
setUpCurrentUser as drupalSetUpCurrentUser;
}
protected static $modules = [
'user',
'system',
'field',
'text',
'filter',
'entity_test',
];
protected $entityTypeManager;
protected $generatedIds = [];
protected $state;
protected function setUp() : void {
parent::setUp();
$this->entityTypeManager = $this->container
->get('entity_type.manager');
$this->state = $this->container
->get('state');
$this
->installEntitySchema('user');
$this
->installEntitySchema('entity_test');
$class = static::class;
while ($class) {
if (property_exists($class, 'modules')) {
$rp = new \ReflectionProperty($class, 'modules');
if ($rp->class == $class) {
foreach (array_intersect([
'node',
'comment',
], $class::$modules) as $module) {
$this
->installEntitySchema($module);
}
}
}
$class = get_parent_class($class);
}
$this
->installConfig([
'field',
]);
}
protected function createUser(array $permissions = [], $name = NULL, bool $admin = FALSE, array $values = []) {
if (!array_is_list($permissions)) {
@trigger_error('Calling createUser() with $values as the first parameter is deprecated in drupal:10.1.0 and will be removed from drupal:11.0.0. Use createUser(array $permissions = [], $name = NULL, $admin = FALSE, array $values = []) instead. See https://www.drupal.org/node/3330762', E_USER_DEPRECATED);
$values = $permissions;
$permissions = [];
}
if (is_array($name)) {
@trigger_error('Calling createUser() with $permissions as the second parameter is deprecated in drupal:10.1.0 and will be removed from drupal:11.0.0. Use createUser(array $permissions = [], $name = NULL, $admin = FALSE, array $values = []) instead. See https://www.drupal.org/node/3330762', E_USER_DEPRECATED);
$permissions = $name;
$name = NULL;
}
return $this
->drupalCreateUser($permissions, $name, $admin, $values);
}
protected function reloadEntity(EntityInterface $entity) {
$controller = $this->entityTypeManager
->getStorage($entity
->getEntityTypeId());
$controller
->resetCache([
$entity
->id(),
]);
return $controller
->load($entity
->id());
}
protected function getHooksInfo() {
$key = 'entity_test.hooks';
$hooks = $this->state
->get($key);
$this->state
->set($key, []);
return $hooks;
}
protected function installModule($module) {
$this
->enableModules([
$module,
]);
$this
->refreshServices();
}
protected function uninstallModule($module) {
$this
->disableModules([
$module,
]);
$this
->refreshServices();
}
protected function refreshServices() {
$this->container = \Drupal::getContainer();
$this->entityTypeManager = $this->container
->get('entity_type.manager');
$this->state = $this->container
->get('state');
}
protected function generateRandomEntityId($string = FALSE) {
srand(time());
do {
$id = $string ? $this
->randomMachineName() : mt_rand(1, 0x7fffffff);
} while (isset($this->generatedIds[$id]));
$this->generatedIds[$id] = $id;
return $id;
}
}
Classes