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