3 namespace Drupal\KernelTests\Core\Entity;
5 use Drupal\Core\Entity\EntityInterface;
6 use Drupal\KernelTests\KernelTestBase;
7 use Drupal\user\Entity\Role;
8 use Drupal\user\Entity\User;
11 * Defines an abstract test base for entity kernel tests.
13 abstract class EntityKernelTestBase extends KernelTestBase {
20 public static $modules = ['user', 'system', 'field', 'text', 'filter', 'entity_test'];
23 * The entity manager service.
25 * @var \Drupal\Core\Entity\EntityManagerInterface
27 protected $entityManager;
30 * A list of generated identifiers.
34 protected $generatedIds = [];
39 * @var \Drupal\Core\State\StateInterface
43 protected function setUp() {
46 $this->entityManager = $this->container->get('entity.manager');
47 $this->state = $this->container->get('state');
49 $this->installSchema('system', 'sequences');
51 $this->installEntitySchema('user');
52 $this->installEntitySchema('entity_test');
54 // If the concrete test sub-class installs the Node or Comment modules,
55 // ensure that the node and comment entity schema are created before the
56 // field configurations are installed. This is because the entity tables
57 // need to be created before the body field storage tables. This prevents
58 // trying to create the body field tables twice.
59 $class = get_class($this);
61 if (property_exists($class, 'modules')) {
62 // Only check the modules, if the $modules property was not inherited.
63 $rp = new \ReflectionProperty($class, 'modules');
64 if ($rp->class == $class) {
65 foreach (array_intersect(['node', 'comment'], $class::$modules) as $module) {
66 $this->installEntitySchema($module);
68 if (in_array('forum', $class::$modules, TRUE)) {
69 // Forum module is particular about the order that dependencies are
70 // enabled in. The comment, node and taxonomy config and the
71 // taxonomy_term schema need to be installed before the forum config
72 // which in turn needs to be installed before field config.
73 $this->installConfig(['comment', 'node', 'taxonomy']);
74 $this->installEntitySchema('taxonomy_term');
75 $this->installConfig(['forum']);
79 $class = get_parent_class($class);
82 $this->installConfig(['field']);
88 * @param array $values
89 * (optional) The values used to create the entity.
90 * @param array $permissions
91 * (optional) Array of permission names to assign to user.
93 * @return \Drupal\user\Entity\User
94 * The created user entity.
96 protected function createUser($values = [], $permissions = []) {
98 // Create a new role and apply permissions to it.
99 $role = Role::create([
100 'id' => strtolower($this->randomMachineName(8)),
101 'label' => $this->randomMachineName(8),
104 user_role_grant_permissions($role->id(), $permissions);
105 $values['roles'][] = $role->id();
108 $account = User::create($values + [
109 'name' => $this->randomMachineName(),
112 $account->enforceIsNew();
118 * Reloads the given entity from the storage and returns it.
120 * @param \Drupal\Core\Entity\EntityInterface $entity
121 * The entity to be reloaded.
123 * @return \Drupal\Core\Entity\EntityInterface
124 * The reloaded entity.
126 protected function reloadEntity(EntityInterface $entity) {
127 $controller = $this->entityManager->getStorage($entity->getEntityTypeId());
128 $controller->resetCache([$entity->id()]);
129 return $controller->load($entity->id());
133 * Returns the entity_test hook invocation info.
136 * An associative array of arbitrary hook data keyed by hook name.
138 protected function getHooksInfo() {
139 $key = 'entity_test.hooks';
140 $hooks = $this->state->get($key);
141 $this->state->set($key, []);
146 * Installs a module and refreshes services.
148 * @param string $module
149 * The module to install.
151 protected function installModule($module) {
152 $this->enableModules([$module]);
153 $this->refreshServices();
157 * Uninstalls a module and refreshes services.
159 * @param string $module
160 * The module to uninstall.
162 protected function uninstallModule($module) {
163 $this->disableModules([$module]);
164 $this->refreshServices();
170 protected function refreshServices() {
171 $this->container = \Drupal::getContainer();
172 $this->entityManager = $this->container->get('entity.manager');
173 $this->state = $this->container->get('state');
177 * Generates a random ID avoiding collisions.
179 * @param bool $string
180 * (optional) Whether the id should have string type. Defaults to FALSE.
183 * The entity identifier.
185 protected function generateRandomEntityId($string = FALSE) {
188 // 0x7FFFFFFF is the maximum allowed value for integers that works for all
189 // Drupal supported databases and is known to work for other databases
190 // like SQL Server 2014 and Oracle 10 too.
191 $id = $string ? $this->randomMachineName() : mt_rand(1, 0x7FFFFFFF);
192 } while (isset($this->generatedIds[$id]));
193 $this->generatedIds[$id] = $id;