3 namespace Drupal\system\Tests\Entity;
5 @trigger_error(__FILE__ . ' is deprecated in Drupal 8.1.0 and will be removed before Drupal 9.0.0. Use \Drupal\KernelTests\Core\Entity\EntityKernelTestBase instead.', E_USER_DEPRECATED);
7 use Drupal\simpletest\KernelTestBase;
8 use Drupal\Core\Entity\EntityInterface;
9 use Drupal\user\Entity\Role;
10 use Drupal\user\Entity\User;
13 * Defines an abstract test base for entity unit tests.
15 * @deprecated in Drupal 8.1.0, will be removed before Drupal 9.0.0. Use
16 * \Drupal\KernelTests\Core\Entity\EntityKernelTestBase instead.
18 abstract class EntityUnitTestBase extends KernelTestBase {
25 public static $modules = ['user', 'system', 'field', 'text', 'filter', 'entity_test'];
28 * The entity manager service.
30 * @var \Drupal\Core\Entity\EntityManagerInterface
32 protected $entityManager;
35 * A list of generated identifiers.
39 protected $generatedIds = [];
44 * @var \Drupal\Core\State\StateInterface
48 protected function setUp() {
51 $this->entityManager = $this->container->get('entity.manager');
52 $this->state = $this->container->get('state');
54 $this->installSchema('system', 'sequences');
56 $this->installEntitySchema('user');
57 $this->installEntitySchema('entity_test');
59 // If the concrete test sub-class installs the Node or Comment modules,
60 // ensure that the node and comment entity schema are created before the
61 // field configurations are installed. This is because the entity tables
62 // need to be created before the body field storage tables. This prevents
63 // trying to create the body field tables twice.
64 $class = get_class($this);
66 if (property_exists($class, 'modules')) {
67 // Only check the modules, if the $modules property was not inherited.
68 $rp = new \ReflectionProperty($class, 'modules');
69 if ($rp->class == $class) {
70 foreach (array_intersect(['node', 'comment'], $class::$modules) as $module) {
71 $this->installEntitySchema($module);
73 if (in_array('forum', $class::$modules, TRUE)) {
74 // Forum module is particular about the order that dependencies are
75 // enabled in. The comment, node and taxonomy config and the
76 // taxonomy_term schema need to be installed before the forum config
77 // which in turn needs to be installed before field config.
78 $this->installConfig(['comment', 'node', 'taxonomy']);
79 $this->installEntitySchema('taxonomy_term');
80 $this->installConfig(['forum']);
84 $class = get_parent_class($class);
87 $this->installConfig(['field']);
93 * @param array $values
94 * (optional) The values used to create the entity.
95 * @param array $permissions
96 * (optional) Array of permission names to assign to user.
98 * @return \Drupal\user\Entity\User
99 * The created user entity.
101 protected function createUser($values = [], $permissions = []) {
103 // Create a new role and apply permissions to it.
104 $role = Role::create([
105 'id' => strtolower($this->randomMachineName(8)),
106 'label' => $this->randomMachineName(8),
109 user_role_grant_permissions($role->id(), $permissions);
110 $values['roles'][] = $role->id();
113 $account = User::create($values + [
114 'name' => $this->randomMachineName(),
117 $account->enforceIsNew();
123 * Reloads the given entity from the storage and returns it.
125 * @param \Drupal\Core\Entity\EntityInterface $entity
126 * The entity to be reloaded.
128 * @return \Drupal\Core\Entity\EntityInterface
129 * The reloaded entity.
131 protected function reloadEntity(EntityInterface $entity) {
132 $controller = $this->entityManager->getStorage($entity->getEntityTypeId());
133 $controller->resetCache([$entity->id()]);
134 return $controller->load($entity->id());
138 * Returns the entity_test hook invocation info.
141 * An associative array of arbitrary hook data keyed by hook name.
143 protected function getHooksInfo() {
144 $key = 'entity_test.hooks';
145 $hooks = $this->state->get($key);
146 $this->state->set($key, []);
151 * Installs a module and refreshes services.
153 * @param string $module
154 * The module to install.
156 protected function installModule($module) {
157 $this->enableModules([$module]);
158 $this->refreshServices();
162 * Uninstalls a module and refreshes services.
164 * @param string $module
165 * The module to uninstall.
167 protected function uninstallModule($module) {
168 $this->disableModules([$module]);
169 $this->refreshServices();
175 protected function refreshServices() {
176 $this->container = \Drupal::getContainer();
177 $this->entityManager = $this->container->get('entity.manager');
178 $this->state = $this->container->get('state');
182 * Generates a random ID avoiding collisions.
184 * @param bool $string
185 * (optional) Whether the id should have string type. Defaults to FALSE.
188 * The entity identifier.
190 protected function generateRandomEntityId($string = FALSE) {
193 // 0x7FFFFFFF is the maximum allowed value for integers that works for all
194 // Drupal supported databases and is known to work for other databases
195 // like SQL Server 2014 and Oracle 10 too.
196 $id = $string ? $this->randomMachineName() : mt_rand(1, 0x7FFFFFFF);
197 } while (isset($this->generatedIds[$id]));
198 $this->generatedIds[$id] = $id;