annotate core/modules/system/src/Tests/Entity/EntityUnitTestBase.php @ 19:fa3358dc1485 tip

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