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