annotate 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
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\system\Tests\Entity;
Chris@0 4
Chris@0 5 use Drupal\simpletest\KernelTestBase;
Chris@0 6 use Drupal\Core\Entity\EntityInterface;
Chris@0 7 use Drupal\user\Entity\Role;
Chris@0 8 use Drupal\user\Entity\User;
Chris@0 9
Chris@0 10 /**
Chris@0 11 * Defines an abstract test base for entity unit tests.
Chris@0 12 *
Chris@0 13 * @deprecated in Drupal 8.1.x, will be removed before Drupal 8.2.x. Use
Chris@0 14 * \Drupal\KernelTests\Core\Entity\EntityKernelTestBase instead.
Chris@0 15 */
Chris@0 16 abstract class EntityUnitTestBase extends KernelTestBase {
Chris@0 17
Chris@0 18 /**
Chris@0 19 * Modules to enable.
Chris@0 20 *
Chris@0 21 * @var array
Chris@0 22 */
Chris@0 23 public static $modules = ['user', 'system', 'field', 'text', 'filter', 'entity_test'];
Chris@0 24
Chris@0 25 /**
Chris@0 26 * The entity manager service.
Chris@0 27 *
Chris@0 28 * @var \Drupal\Core\Entity\EntityManagerInterface
Chris@0 29 */
Chris@0 30 protected $entityManager;
Chris@0 31
Chris@0 32 /**
Chris@0 33 * A list of generated identifiers.
Chris@0 34 *
Chris@0 35 * @var array
Chris@0 36 */
Chris@0 37 protected $generatedIds = [];
Chris@0 38
Chris@0 39 /**
Chris@0 40 * The state service.
Chris@0 41 *
Chris@0 42 * @var \Drupal\Core\State\StateInterface
Chris@0 43 */
Chris@0 44 protected $state;
Chris@0 45
Chris@0 46 protected function setUp() {
Chris@0 47 parent::setUp();
Chris@0 48
Chris@0 49 $this->entityManager = $this->container->get('entity.manager');
Chris@0 50 $this->state = $this->container->get('state');
Chris@0 51
Chris@0 52 $this->installSchema('system', 'sequences');
Chris@0 53
Chris@0 54 $this->installEntitySchema('user');
Chris@0 55 $this->installEntitySchema('entity_test');
Chris@0 56
Chris@0 57 // If the concrete test sub-class installs the Node or Comment modules,
Chris@0 58 // ensure that the node and comment entity schema are created before the
Chris@0 59 // field configurations are installed. This is because the entity tables
Chris@0 60 // need to be created before the body field storage tables. This prevents
Chris@0 61 // trying to create the body field tables twice.
Chris@0 62 $class = get_class($this);
Chris@0 63 while ($class) {
Chris@0 64 if (property_exists($class, 'modules')) {
Chris@0 65 // Only check the modules, if the $modules property was not inherited.
Chris@0 66 $rp = new \ReflectionProperty($class, 'modules');
Chris@0 67 if ($rp->class == $class) {
Chris@0 68 foreach (array_intersect(['node', 'comment'], $class::$modules) as $module) {
Chris@0 69 $this->installEntitySchema($module);
Chris@0 70 }
Chris@0 71 if (in_array('forum', $class::$modules, TRUE)) {
Chris@0 72 // Forum module is particular about the order that dependencies are
Chris@0 73 // enabled in. The comment, node and taxonomy config and the
Chris@0 74 // taxonomy_term schema need to be installed before the forum config
Chris@0 75 // which in turn needs to be installed before field config.
Chris@0 76 $this->installConfig(['comment', 'node', 'taxonomy']);
Chris@0 77 $this->installEntitySchema('taxonomy_term');
Chris@0 78 $this->installConfig(['forum']);
Chris@0 79 }
Chris@0 80 }
Chris@0 81 }
Chris@0 82 $class = get_parent_class($class);
Chris@0 83 }
Chris@0 84
Chris@0 85 $this->installConfig(['field']);
Chris@0 86 }
Chris@0 87
Chris@0 88 /**
Chris@0 89 * Creates a user.
Chris@0 90 *
Chris@0 91 * @param array $values
Chris@0 92 * (optional) The values used to create the entity.
Chris@0 93 * @param array $permissions
Chris@0 94 * (optional) Array of permission names to assign to user.
Chris@0 95 *
Chris@0 96 * @return \Drupal\user\Entity\User
Chris@0 97 * The created user entity.
Chris@0 98 */
Chris@0 99 protected function createUser($values = [], $permissions = []) {
Chris@0 100 if ($permissions) {
Chris@0 101 // Create a new role and apply permissions to it.
Chris@0 102 $role = Role::create([
Chris@0 103 'id' => strtolower($this->randomMachineName(8)),
Chris@0 104 'label' => $this->randomMachineName(8),
Chris@0 105 ]);
Chris@0 106 $role->save();
Chris@0 107 user_role_grant_permissions($role->id(), $permissions);
Chris@0 108 $values['roles'][] = $role->id();
Chris@0 109 }
Chris@0 110
Chris@0 111 $account = User::create($values + [
Chris@0 112 'name' => $this->randomMachineName(),
Chris@0 113 'status' => 1,
Chris@0 114 ]);
Chris@0 115 $account->enforceIsNew();
Chris@0 116 $account->save();
Chris@0 117 return $account;
Chris@0 118 }
Chris@0 119
Chris@0 120 /**
Chris@0 121 * Reloads the given entity from the storage and returns it.
Chris@0 122 *
Chris@0 123 * @param \Drupal\Core\Entity\EntityInterface $entity
Chris@0 124 * The entity to be reloaded.
Chris@0 125 *
Chris@0 126 * @return \Drupal\Core\Entity\EntityInterface
Chris@0 127 * The reloaded entity.
Chris@0 128 */
Chris@0 129 protected function reloadEntity(EntityInterface $entity) {
Chris@0 130 $controller = $this->entityManager->getStorage($entity->getEntityTypeId());
Chris@0 131 $controller->resetCache([$entity->id()]);
Chris@0 132 return $controller->load($entity->id());
Chris@0 133 }
Chris@0 134
Chris@0 135 /**
Chris@0 136 * Returns the entity_test hook invocation info.
Chris@0 137 *
Chris@0 138 * @return array
Chris@0 139 * An associative array of arbitrary hook data keyed by hook name.
Chris@0 140 */
Chris@0 141 protected function getHooksInfo() {
Chris@0 142 $key = 'entity_test.hooks';
Chris@0 143 $hooks = $this->state->get($key);
Chris@0 144 $this->state->set($key, []);
Chris@0 145 return $hooks;
Chris@0 146 }
Chris@0 147
Chris@0 148 /**
Chris@0 149 * Installs a module and refreshes services.
Chris@0 150 *
Chris@0 151 * @param string $module
Chris@0 152 * The module to install.
Chris@0 153 */
Chris@0 154 protected function installModule($module) {
Chris@0 155 $this->enableModules([$module]);
Chris@0 156 $this->refreshServices();
Chris@0 157 }
Chris@0 158
Chris@0 159 /**
Chris@0 160 * Uninstalls a module and refreshes services.
Chris@0 161 *
Chris@0 162 * @param string $module
Chris@0 163 * The module to uninstall.
Chris@0 164 */
Chris@0 165 protected function uninstallModule($module) {
Chris@0 166 $this->disableModules([$module]);
Chris@0 167 $this->refreshServices();
Chris@0 168 }
Chris@0 169
Chris@0 170 /**
Chris@0 171 * Refresh services.
Chris@0 172 */
Chris@0 173 protected function refreshServices() {
Chris@0 174 $this->container = \Drupal::getContainer();
Chris@0 175 $this->entityManager = $this->container->get('entity.manager');
Chris@0 176 $this->state = $this->container->get('state');
Chris@0 177 }
Chris@0 178
Chris@0 179 /**
Chris@0 180 * Generates a random ID avoiding collisions.
Chris@0 181 *
Chris@0 182 * @param bool $string
Chris@0 183 * (optional) Whether the id should have string type. Defaults to FALSE.
Chris@0 184 *
Chris@0 185 * @return int|string
Chris@0 186 * The entity identifier.
Chris@0 187 */
Chris@0 188 protected function generateRandomEntityId($string = FALSE) {
Chris@0 189 srand(time());
Chris@0 190 do {
Chris@0 191 // 0x7FFFFFFF is the maximum allowed value for integers that works for all
Chris@0 192 // Drupal supported databases and is known to work for other databases
Chris@0 193 // like SQL Server 2014 and Oracle 10 too.
Chris@0 194 $id = $string ? $this->randomMachineName() : mt_rand(1, 0x7FFFFFFF);
Chris@0 195 } while (isset($this->generatedIds[$id]));
Chris@0 196 $this->generatedIds[$id] = $id;
Chris@0 197 return $id;
Chris@0 198 }
Chris@0 199
Chris@0 200 }