Chris@0: setupLanguages(); Chris@0: $this->setupBundle(); Chris@0: $this->enableTranslation(); Chris@0: $this->setupUsers(); Chris@0: $this->setupTestFields(); Chris@0: Chris@0: $this->manager = $this->container->get('content_translation.manager'); Chris@0: $this->controller = $this->manager->getTranslationHandler($this->entityTypeId); Chris@0: Chris@0: // Rebuild the container so that the new languages are picked up by services Chris@0: // that hold a list of languages. Chris@0: $this->rebuildContainer(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Adds additional languages. Chris@0: */ Chris@0: protected function setupLanguages() { Chris@0: $this->langcodes = ['it', 'fr']; Chris@0: foreach ($this->langcodes as $langcode) { Chris@0: ConfigurableLanguage::createFromLangcode($langcode)->save(); Chris@0: } Chris@0: array_unshift($this->langcodes, \Drupal::languageManager()->getDefaultLanguage()->getId()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns an array of permissions needed for the translator. Chris@0: */ Chris@0: protected function getTranslatorPermissions() { Chris@0: return array_filter([$this->getTranslatePermission(), 'create content translations', 'update content translations', 'delete content translations']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns the translate permissions for the current entity and bundle. Chris@0: */ Chris@0: protected function getTranslatePermission() { Chris@0: $entity_type = \Drupal::entityManager()->getDefinition($this->entityTypeId); Chris@0: if ($permission_granularity = $entity_type->getPermissionGranularity()) { Chris@0: return $permission_granularity == 'bundle' ? "translate {$this->bundle} {$this->entityTypeId}" : "translate {$this->entityTypeId}"; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns an array of permissions needed for the editor. Chris@0: */ Chris@0: protected function getEditorPermissions() { Chris@0: // Every entity-type-specific test needs to define these. Chris@0: return []; Chris@0: } Chris@0: Chris@0: /** Chris@0: * Returns an array of permissions needed for the administrator. Chris@0: */ Chris@0: protected function getAdministratorPermissions() { Chris@0: return array_merge($this->getEditorPermissions(), $this->getTranslatorPermissions(), ['administer content translation']); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates and activates translator, editor and admin users. Chris@0: */ Chris@0: protected function setupUsers() { Chris@0: $this->translator = $this->drupalCreateUser($this->getTranslatorPermissions(), 'translator'); Chris@0: $this->editor = $this->drupalCreateUser($this->getEditorPermissions(), 'editor'); Chris@0: $this->administrator = $this->drupalCreateUser($this->getAdministratorPermissions(), 'administrator'); Chris@0: $this->drupalLogin($this->translator); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates or initializes the bundle date if needed. Chris@0: */ Chris@0: protected function setupBundle() { Chris@0: if (empty($this->bundle)) { Chris@0: $this->bundle = $this->entityTypeId; Chris@0: } Chris@0: } Chris@0: Chris@0: /** Chris@0: * Enables translation for the current entity type and bundle. Chris@0: */ Chris@0: protected function enableTranslation() { Chris@0: // Enable translation for the current entity type and ensure the change is Chris@0: // picked up. Chris@0: \Drupal::service('content_translation.manager')->setEnabled($this->entityTypeId, $this->bundle, TRUE); Chris@18: Chris@0: \Drupal::entityManager()->clearCachedDefinitions(); Chris@0: \Drupal::service('router.builder')->rebuild(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates the test fields. Chris@0: */ Chris@0: protected function setupTestFields() { Chris@0: if (empty($this->fieldName)) { Chris@0: $this->fieldName = 'field_test_et_ui_test'; Chris@0: } Chris@0: FieldStorageConfig::create([ Chris@0: 'field_name' => $this->fieldName, Chris@0: 'type' => 'string', Chris@0: 'entity_type' => $this->entityTypeId, Chris@0: 'cardinality' => 1, Chris@0: ])->save(); Chris@0: FieldConfig::create([ Chris@0: 'entity_type' => $this->entityTypeId, Chris@0: 'field_name' => $this->fieldName, Chris@0: 'bundle' => $this->bundle, Chris@0: 'label' => 'Test translatable text-field', Chris@0: ])->save(); Chris@0: entity_get_form_display($this->entityTypeId, $this->bundle, 'default') Chris@0: ->setComponent($this->fieldName, [ Chris@0: 'type' => 'string_textfield', Chris@0: 'weight' => 0, Chris@0: ]) Chris@0: ->save(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Creates the entity to be translated. Chris@0: * Chris@0: * @param array $values Chris@0: * An array of initial values for the entity. Chris@0: * @param string $langcode Chris@0: * The initial language code of the entity. Chris@0: * @param string $bundle_name Chris@0: * (optional) The entity bundle, if the entity uses bundles. Defaults to Chris@0: * NULL. If left NULL, $this->bundle will be used. Chris@0: * Chris@0: * @return string Chris@0: * The entity id. Chris@0: */ Chris@0: protected function createEntity($values, $langcode, $bundle_name = NULL) { Chris@0: $entity_values = $values; Chris@0: $entity_values['langcode'] = $langcode; Chris@0: $entity_type = \Drupal::entityManager()->getDefinition($this->entityTypeId); Chris@0: if ($bundle_key = $entity_type->getKey('bundle')) { Chris@0: $entity_values[$bundle_key] = $bundle_name ?: $this->bundle; Chris@0: } Chris@0: $controller = $this->container->get('entity.manager')->getStorage($this->entityTypeId); Chris@0: if (!($controller instanceof SqlContentEntityStorage)) { Chris@0: foreach ($values as $property => $value) { Chris@0: if (is_array($value)) { Chris@0: $entity_values[$property] = [$langcode => $value]; Chris@0: } Chris@0: } Chris@0: } Chris@0: $entity = $this->container->get('entity_type.manager') Chris@0: ->getStorage($this->entityTypeId) Chris@0: ->create($entity_values); Chris@0: $entity->save(); Chris@0: return $entity->id(); Chris@0: } Chris@0: Chris@0: }