3 namespace Drupal\system\Tests\Entity;
5 use Drupal\simpletest\KernelTestBase;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\user\Entity\Role;
8 use Drupal\user\Entity\User;
11 * Defines an abstract test base for entity unit tests.
13 * @deprecated in Drupal 8.1.x, will be removed before Drupal 8.2.x. Use
14 * \Drupal\KernelTests\Core\Entity\EntityKernelTestBase instead.
16 abstract class EntityUnitTestBase extends KernelTestBase {
23 public static $modules = ['user', 'system', 'field', 'text', 'filter', 'entity_test'];
26 * The entity manager service.
28 * @var \Drupal\Core\Entity\EntityManagerInterface
30 protected $entityManager;
33 * A list of generated identifiers.
37 protected $generatedIds = [];
42 * @var \Drupal\Core\State\StateInterface
46 protected function setUp() {
49 $this->entityManager = $this->container->get('entity.manager');
50 $this->state = $this->container->get('state');
52 $this->installSchema('system', 'sequences');
54 $this->installEntitySchema('user');
55 $this->installEntitySchema('entity_test');
57 // If the concrete test sub-class installs the Node or Comment modules,
58 // ensure that the node and comment entity schema are created before the
59 // field configurations are installed. This is because the entity tables
60 // need to be created before the body field storage tables. This prevents
61 // trying to create the body field tables twice.
62 $class = get_class($this);
64 if (property_exists($class, 'modules')) {
65 // Only check the modules, if the $modules property was not inherited.
66 $rp = new \ReflectionProperty($class, 'modules');
67 if ($rp->class == $class) {
68 foreach (array_intersect(['node', 'comment'], $class::$modules) as $module) {
69 $this->installEntitySchema($module);
71 if (in_array('forum', $class::$modules, TRUE)) {
72 // Forum module is particular about the order that dependencies are
73 // enabled in. The comment, node and taxonomy config and the
74 // taxonomy_term schema need to be installed before the forum config
75 // which in turn needs to be installed before field config.
76 $this->installConfig(['comment', 'node', 'taxonomy']);
77 $this->installEntitySchema('taxonomy_term');
78 $this->installConfig(['forum']);
82 $class = get_parent_class($class);
85 $this->installConfig(['field']);
91 * @param array $values
92 * (optional) The values used to create the entity.
93 * @param array $permissions
94 * (optional) Array of permission names to assign to user.
96 * @return \Drupal\user\Entity\User
97 * The created user entity.
99 protected function createUser($values = [], $permissions = []) {
101 // Create a new role and apply permissions to it.
102 $role = Role::create([
103 'id' => strtolower($this->randomMachineName(8)),
104 'label' => $this->randomMachineName(8),
107 user_role_grant_permissions($role->id(), $permissions);
108 $values['roles'][] = $role->id();
111 $account = User::create($values + [
112 'name' => $this->randomMachineName(),
115 $account->enforceIsNew();
121 * Reloads the given entity from the storage and returns it.
123 * @param \Drupal\Core\Entity\EntityInterface $entity
124 * The entity to be reloaded.
126 * @return \Drupal\Core\Entity\EntityInterface
127 * The reloaded entity.
129 protected function reloadEntity(EntityInterface $entity) {
130 $controller = $this->entityManager->getStorage($entity->getEntityTypeId());
131 $controller->resetCache([$entity->id()]);
132 return $controller->load($entity->id());
136 * Returns the entity_test hook invocation info.
139 * An associative array of arbitrary hook data keyed by hook name.
141 protected function getHooksInfo() {
142 $key = 'entity_test.hooks';
143 $hooks = $this->state->get($key);
144 $this->state->set($key, []);
149 * Installs a module and refreshes services.
151 * @param string $module
152 * The module to install.
154 protected function installModule($module) {
155 $this->enableModules([$module]);
156 $this->refreshServices();
160 * Uninstalls a module and refreshes services.
162 * @param string $module
163 * The module to uninstall.
165 protected function uninstallModule($module) {
166 $this->disableModules([$module]);
167 $this->refreshServices();
173 protected function refreshServices() {
174 $this->container = \Drupal::getContainer();
175 $this->entityManager = $this->container->get('entity.manager');
176 $this->state = $this->container->get('state');
180 * Generates a random ID avoiding collisions.
182 * @param bool $string
183 * (optional) Whether the id should have string type. Defaults to FALSE.
186 * The entity identifier.
188 protected function generateRandomEntityId($string = FALSE) {
191 // 0x7FFFFFFF is the maximum allowed value for integers that works for all
192 // Drupal supported databases and is known to work for other databases
193 // like SQL Server 2014 and Oracle 10 too.
194 $id = $string ? $this->randomMachineName() : mt_rand(1, 0x7FFFFFFF);
195 } while (isset($this->generatedIds[$id]));
196 $this->generatedIds[$id] = $id;