comparison core/modules/system/src/Tests/Entity/EntityUnitTestBase.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children c2387f117808
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\system\Tests\Entity;
4
5 use Drupal\simpletest\KernelTestBase;
6 use Drupal\Core\Entity\EntityInterface;
7 use Drupal\user\Entity\Role;
8 use Drupal\user\Entity\User;
9
10 /**
11 * Defines an abstract test base for entity unit tests.
12 *
13 * @deprecated in Drupal 8.1.x, will be removed before Drupal 8.2.x. Use
14 * \Drupal\KernelTests\Core\Entity\EntityKernelTestBase instead.
15 */
16 abstract class EntityUnitTestBase extends KernelTestBase {
17
18 /**
19 * Modules to enable.
20 *
21 * @var array
22 */
23 public static $modules = ['user', 'system', 'field', 'text', 'filter', 'entity_test'];
24
25 /**
26 * The entity manager service.
27 *
28 * @var \Drupal\Core\Entity\EntityManagerInterface
29 */
30 protected $entityManager;
31
32 /**
33 * A list of generated identifiers.
34 *
35 * @var array
36 */
37 protected $generatedIds = [];
38
39 /**
40 * The state service.
41 *
42 * @var \Drupal\Core\State\StateInterface
43 */
44 protected $state;
45
46 protected function setUp() {
47 parent::setUp();
48
49 $this->entityManager = $this->container->get('entity.manager');
50 $this->state = $this->container->get('state');
51
52 $this->installSchema('system', 'sequences');
53
54 $this->installEntitySchema('user');
55 $this->installEntitySchema('entity_test');
56
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);
63 while ($class) {
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);
70 }
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']);
79 }
80 }
81 }
82 $class = get_parent_class($class);
83 }
84
85 $this->installConfig(['field']);
86 }
87
88 /**
89 * Creates a user.
90 *
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.
95 *
96 * @return \Drupal\user\Entity\User
97 * The created user entity.
98 */
99 protected function createUser($values = [], $permissions = []) {
100 if ($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),
105 ]);
106 $role->save();
107 user_role_grant_permissions($role->id(), $permissions);
108 $values['roles'][] = $role->id();
109 }
110
111 $account = User::create($values + [
112 'name' => $this->randomMachineName(),
113 'status' => 1,
114 ]);
115 $account->enforceIsNew();
116 $account->save();
117 return $account;
118 }
119
120 /**
121 * Reloads the given entity from the storage and returns it.
122 *
123 * @param \Drupal\Core\Entity\EntityInterface $entity
124 * The entity to be reloaded.
125 *
126 * @return \Drupal\Core\Entity\EntityInterface
127 * The reloaded entity.
128 */
129 protected function reloadEntity(EntityInterface $entity) {
130 $controller = $this->entityManager->getStorage($entity->getEntityTypeId());
131 $controller->resetCache([$entity->id()]);
132 return $controller->load($entity->id());
133 }
134
135 /**
136 * Returns the entity_test hook invocation info.
137 *
138 * @return array
139 * An associative array of arbitrary hook data keyed by hook name.
140 */
141 protected function getHooksInfo() {
142 $key = 'entity_test.hooks';
143 $hooks = $this->state->get($key);
144 $this->state->set($key, []);
145 return $hooks;
146 }
147
148 /**
149 * Installs a module and refreshes services.
150 *
151 * @param string $module
152 * The module to install.
153 */
154 protected function installModule($module) {
155 $this->enableModules([$module]);
156 $this->refreshServices();
157 }
158
159 /**
160 * Uninstalls a module and refreshes services.
161 *
162 * @param string $module
163 * The module to uninstall.
164 */
165 protected function uninstallModule($module) {
166 $this->disableModules([$module]);
167 $this->refreshServices();
168 }
169
170 /**
171 * Refresh services.
172 */
173 protected function refreshServices() {
174 $this->container = \Drupal::getContainer();
175 $this->entityManager = $this->container->get('entity.manager');
176 $this->state = $this->container->get('state');
177 }
178
179 /**
180 * Generates a random ID avoiding collisions.
181 *
182 * @param bool $string
183 * (optional) Whether the id should have string type. Defaults to FALSE.
184 *
185 * @return int|string
186 * The entity identifier.
187 */
188 protected function generateRandomEntityId($string = FALSE) {
189 srand(time());
190 do {
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;
197 return $id;
198 }
199
200 }