Chris@0: entityManager = $this->container->get('entity.manager'); Chris@0: $this->state = $this->container->get('state'); Chris@0: Chris@0: $this->installSchema('system', 'sequences'); Chris@0: Chris@0: $this->installEntitySchema('user'); Chris@0: $this->installEntitySchema('entity_test'); Chris@0: Chris@0: // If the concrete test sub-class installs the Node or Comment modules, Chris@0: // ensure that the node and comment entity schema are created before the Chris@0: // field configurations are installed. This is because the entity tables Chris@0: // need to be created before the body field storage tables. This prevents Chris@0: // trying to create the body field tables twice. Chris@0: $class = get_class($this); Chris@0: while ($class) { Chris@0: if (property_exists($class, 'modules')) { Chris@0: // Only check the modules, if the $modules property was not inherited. Chris@0: $rp = new \ReflectionProperty($class, 'modules'); Chris@0: if ($rp->class == $class) { Chris@0: foreach (array_intersect(['node', 'comment'], $class::$modules) as $module) { Chris@0: $this->installEntitySchema($module); Chris@0: } Chris@0: if (in_array('forum', $class::$modules, TRUE)) { Chris@0: // Forum module is particular about the order that dependencies are Chris@0: // enabled in. The comment, node and taxonomy config and the Chris@0: // taxonomy_term schema need to be installed before the forum config Chris@0: // which in turn needs to be installed before field config. Chris@0: $this->installConfig(['comment', 'node', 'taxonomy']); Chris@0: $this->installEntitySchema('taxonomy_term'); Chris@0: $this->installConfig(['forum']); Chris@0: } Chris@0: } Chris@0: } Chris@0: $class = get_parent_class($class); Chris@0: } Chris@0: Chris@0: $this->installConfig(['field']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates a user. Chris@0: * Chris@0: * @param array $values Chris@0: * (optional) The values used to create the entity. Chris@0: * @param array $permissions Chris@0: * (optional) Array of permission names to assign to user. Chris@0: * Chris@0: * @return \Drupal\user\Entity\User Chris@0: * The created user entity. Chris@0: */ Chris@0: protected function createUser($values = [], $permissions = []) { Chris@0: if ($permissions) { Chris@0: // Create a new role and apply permissions to it. Chris@0: $role = Role::create([ Chris@0: 'id' => strtolower($this->randomMachineName(8)), Chris@0: 'label' => $this->randomMachineName(8), Chris@0: ]); Chris@0: $role->save(); Chris@0: user_role_grant_permissions($role->id(), $permissions); Chris@0: $values['roles'][] = $role->id(); Chris@0: } Chris@0: Chris@0: $account = User::create($values + [ Chris@0: 'name' => $this->randomMachineName(), Chris@0: 'status' => 1, Chris@0: ]); Chris@0: $account->enforceIsNew(); Chris@0: $account->save(); Chris@0: return $account; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Reloads the given entity from the storage and returns it. Chris@0: * Chris@0: * @param \Drupal\Core\Entity\EntityInterface $entity Chris@0: * The entity to be reloaded. Chris@0: * Chris@0: * @return \Drupal\Core\Entity\EntityInterface Chris@0: * The reloaded entity. Chris@0: */ Chris@0: protected function reloadEntity(EntityInterface $entity) { Chris@0: $controller = $this->entityManager->getStorage($entity->getEntityTypeId()); Chris@0: $controller->resetCache([$entity->id()]); Chris@0: return $controller->load($entity->id()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the entity_test hook invocation info. Chris@0: * Chris@0: * @return array Chris@0: * An associative array of arbitrary hook data keyed by hook name. Chris@0: */ Chris@0: protected function getHooksInfo() { Chris@0: $key = 'entity_test.hooks'; Chris@0: $hooks = $this->state->get($key); Chris@0: $this->state->set($key, []); Chris@0: return $hooks; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Installs a module and refreshes services. Chris@0: * Chris@0: * @param string $module Chris@0: * The module to install. Chris@0: */ Chris@0: protected function installModule($module) { Chris@0: $this->enableModules([$module]); Chris@0: $this->refreshServices(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Uninstalls a module and refreshes services. Chris@0: * Chris@0: * @param string $module Chris@0: * The module to uninstall. Chris@0: */ Chris@0: protected function uninstallModule($module) { Chris@0: $this->disableModules([$module]); Chris@0: $this->refreshServices(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Refresh services. Chris@0: */ Chris@0: protected function refreshServices() { Chris@0: $this->container = \Drupal::getContainer(); Chris@0: $this->entityManager = $this->container->get('entity.manager'); Chris@0: $this->state = $this->container->get('state'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Generates a random ID avoiding collisions. Chris@0: * Chris@0: * @param bool $string Chris@0: * (optional) Whether the id should have string type. Defaults to FALSE. Chris@0: * Chris@0: * @return int|string Chris@0: * The entity identifier. Chris@0: */ Chris@0: protected function generateRandomEntityId($string = FALSE) { Chris@0: srand(time()); Chris@0: do { Chris@0: // 0x7FFFFFFF is the maximum allowed value for integers that works for all Chris@0: // Drupal supported databases and is known to work for other databases Chris@0: // like SQL Server 2014 and Oracle 10 too. Chris@0: $id = $string ? $this->randomMachineName() : mt_rand(1, 0x7FFFFFFF); Chris@0: } while (isset($this->generatedIds[$id])); Chris@0: $this->generatedIds[$id] = $id; Chris@0: return $id; Chris@0: } Chris@0: Chris@0: }