3 namespace Drupal\Tests\content_translation\Functional;
5 use Drupal\Core\Entity\ContentEntityInterface;
6 use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\language\Entity\ConfigurableLanguage;
9 use Drupal\Tests\BrowserTestBase;
10 use Drupal\field\Entity\FieldStorageConfig;
13 * Base class for content translation tests.
15 abstract class ContentTranslationTestBase extends BrowserTestBase {
22 public static $modules = ['text'];
25 * The entity type being tested.
29 protected $entityTypeId = 'entity_test_mul';
32 * The bundle being tested.
39 * The added languages.
46 * The account to be used to test translation operations.
48 * @var \Drupal\user\UserInterface
50 protected $translator;
53 * The account to be used to test multilingual entity editing.
55 * @var \Drupal\user\UserInterface
60 * The account to be used to test access to both workflows.
62 * @var \Drupal\user\UserInterface
64 protected $administrator;
67 * The name of the field used to test translation.
74 * The translation controller for the current entity type.
76 * @var \Drupal\content_translation\ContentTranslationHandlerInterface
78 protected $controller;
81 * @var \Drupal\content_translation\ContentTranslationManagerInterface
85 protected function setUp() {
88 $this->setupLanguages();
90 $this->enableTranslation();
92 $this->setupTestFields();
94 $this->manager = $this->container->get('content_translation.manager');
95 $this->controller = $this->manager->getTranslationHandler($this->entityTypeId);
97 // Rebuild the container so that the new languages are picked up by services
98 // that hold a list of languages.
99 $this->rebuildContainer();
103 * Adds additional languages.
105 protected function setupLanguages() {
106 $this->langcodes = ['it', 'fr'];
107 foreach ($this->langcodes as $langcode) {
108 ConfigurableLanguage::createFromLangcode($langcode)->save();
110 array_unshift($this->langcodes, \Drupal::languageManager()->getDefaultLanguage()->getId());
114 * Returns an array of permissions needed for the translator.
116 protected function getTranslatorPermissions() {
117 return array_filter([$this->getTranslatePermission(), 'create content translations', 'update content translations', 'delete content translations']);
121 * Returns the translate permissions for the current entity and bundle.
123 protected function getTranslatePermission() {
124 $entity_type = \Drupal::entityManager()->getDefinition($this->entityTypeId);
125 if ($permission_granularity = $entity_type->getPermissionGranularity()) {
126 return $permission_granularity == 'bundle' ? "translate {$this->bundle} {$this->entityTypeId}" : "translate {$this->entityTypeId}";
131 * Returns an array of permissions needed for the editor.
133 protected function getEditorPermissions() {
134 // Every entity-type-specific test needs to define these.
139 * Returns an array of permissions needed for the administrator.
141 protected function getAdministratorPermissions() {
142 return array_merge($this->getEditorPermissions(), $this->getTranslatorPermissions(), ['administer languages', 'administer content translation']);
146 * Creates and activates translator, editor and admin users.
148 protected function setupUsers() {
149 $this->translator = $this->drupalCreateUser($this->getTranslatorPermissions(), 'translator');
150 $this->editor = $this->drupalCreateUser($this->getEditorPermissions(), 'editor');
151 $this->administrator = $this->drupalCreateUser($this->getAdministratorPermissions(), 'administrator');
152 $this->drupalLogin($this->translator);
156 * Creates or initializes the bundle date if needed.
158 protected function setupBundle() {
159 if (empty($this->bundle)) {
160 $this->bundle = $this->entityTypeId;
165 * Enables translation for the current entity type and bundle.
167 protected function enableTranslation() {
168 // Enable translation for the current entity type and ensure the change is
170 \Drupal::service('content_translation.manager')->setEnabled($this->entityTypeId, $this->bundle, TRUE);
171 drupal_static_reset();
172 \Drupal::entityManager()->clearCachedDefinitions();
173 \Drupal::service('router.builder')->rebuild();
174 \Drupal::service('entity.definition_update_manager')->applyUpdates();
178 * Creates the test fields.
180 protected function setupTestFields() {
181 if (empty($this->fieldName)) {
182 $this->fieldName = 'field_test_et_ui_test';
184 FieldStorageConfig::create([
185 'field_name' => $this->fieldName,
187 'entity_type' => $this->entityTypeId,
190 FieldConfig::create([
191 'entity_type' => $this->entityTypeId,
192 'field_name' => $this->fieldName,
193 'bundle' => $this->bundle,
194 'label' => 'Test translatable text-field',
196 entity_get_form_display($this->entityTypeId, $this->bundle, 'default')
197 ->setComponent($this->fieldName, [
198 'type' => 'string_textfield',
205 * Creates the entity to be translated.
207 * @param array $values
208 * An array of initial values for the entity.
209 * @param string $langcode
210 * The initial language code of the entity.
211 * @param string $bundle_name
212 * (optional) The entity bundle, if the entity uses bundles. Defaults to
213 * NULL. If left NULL, $this->bundle will be used.
218 protected function createEntity($values, $langcode, $bundle_name = NULL) {
219 $entity_values = $values;
220 $entity_values['langcode'] = $langcode;
221 $entity_type = \Drupal::entityManager()->getDefinition($this->entityTypeId);
222 if ($bundle_key = $entity_type->getKey('bundle')) {
223 $entity_values[$bundle_key] = $bundle_name ?: $this->bundle;
225 $controller = $this->container->get('entity.manager')->getStorage($this->entityTypeId);
226 if (!($controller instanceof SqlContentEntityStorage)) {
227 foreach ($values as $property => $value) {
228 if (is_array($value)) {
229 $entity_values[$property] = [$langcode => $value];
233 $entity = $this->container->get('entity_type.manager')
234 ->getStorage($this->entityTypeId)
235 ->create($entity_values);
237 return $entity->id();
241 * Returns the edit URL for the specified entity.
243 * @param \Drupal\Core\Entity\ContentEntityInterface $entity
244 * The entity being edited.
246 * @return \Drupal\Core\Url
249 protected function getEditUrl(ContentEntityInterface $entity) {
250 if ($entity->access('update', $this->loggedInUser)) {
251 $url = $entity->toUrl('edit-form');
254 $url = $entity->toUrl('drupal:content-translation-edit');
255 $url->setRouteParameter('language', $entity->language()->getId());