3 namespace Drupal\Tests\content_translation\Functional;
5 use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\language\Entity\ConfigurableLanguage;
8 use Drupal\Tests\BrowserTestBase;
9 use Drupal\field\Entity\FieldStorageConfig;
12 * Base class for content translation tests.
14 abstract class ContentTranslationTestBase extends BrowserTestBase {
21 public static $modules = ['text'];
24 * The entity type being tested.
28 protected $entityTypeId = 'entity_test_mul';
31 * The bundle being tested.
38 * The added languages.
45 * The account to be used to test translation operations.
47 * @var \Drupal\user\UserInterface
49 protected $translator;
52 * The account to be used to test multilingual entity editing.
54 * @var \Drupal\user\UserInterface
59 * The account to be used to test access to both workflows.
61 * @var \Drupal\user\UserInterface
63 protected $administrator;
66 * The name of the field used to test translation.
73 * The translation controller for the current entity type.
75 * @var \Drupal\content_translation\ContentTranslationHandlerInterface
77 protected $controller;
80 * @var \Drupal\content_translation\ContentTranslationManagerInterface
84 protected function setUp() {
87 $this->setupLanguages();
89 $this->enableTranslation();
91 $this->setupTestFields();
93 $this->manager = $this->container->get('content_translation.manager');
94 $this->controller = $this->manager->getTranslationHandler($this->entityTypeId);
96 // Rebuild the container so that the new languages are picked up by services
97 // that hold a list of languages.
98 $this->rebuildContainer();
102 * Adds additional languages.
104 protected function setupLanguages() {
105 $this->langcodes = ['it', 'fr'];
106 foreach ($this->langcodes as $langcode) {
107 ConfigurableLanguage::createFromLangcode($langcode)->save();
109 array_unshift($this->langcodes, \Drupal::languageManager()->getDefaultLanguage()->getId());
113 * Returns an array of permissions needed for the translator.
115 protected function getTranslatorPermissions() {
116 return array_filter([$this->getTranslatePermission(), 'create content translations', 'update content translations', 'delete content translations']);
120 * Returns the translate permissions for the current entity and bundle.
122 protected function getTranslatePermission() {
123 $entity_type = \Drupal::entityManager()->getDefinition($this->entityTypeId);
124 if ($permission_granularity = $entity_type->getPermissionGranularity()) {
125 return $permission_granularity == 'bundle' ? "translate {$this->bundle} {$this->entityTypeId}" : "translate {$this->entityTypeId}";
130 * Returns an array of permissions needed for the editor.
132 protected function getEditorPermissions() {
133 // Every entity-type-specific test needs to define these.
138 * Returns an array of permissions needed for the administrator.
140 protected function getAdministratorPermissions() {
141 return array_merge($this->getEditorPermissions(), $this->getTranslatorPermissions(), ['administer content translation']);
145 * Creates and activates translator, editor and admin users.
147 protected function setupUsers() {
148 $this->translator = $this->drupalCreateUser($this->getTranslatorPermissions(), 'translator');
149 $this->editor = $this->drupalCreateUser($this->getEditorPermissions(), 'editor');
150 $this->administrator = $this->drupalCreateUser($this->getAdministratorPermissions(), 'administrator');
151 $this->drupalLogin($this->translator);
155 * Creates or initializes the bundle date if needed.
157 protected function setupBundle() {
158 if (empty($this->bundle)) {
159 $this->bundle = $this->entityTypeId;
164 * Enables translation for the current entity type and bundle.
166 protected function enableTranslation() {
167 // Enable translation for the current entity type and ensure the change is
169 \Drupal::service('content_translation.manager')->setEnabled($this->entityTypeId, $this->bundle, TRUE);
170 drupal_static_reset();
171 \Drupal::entityManager()->clearCachedDefinitions();
172 \Drupal::service('router.builder')->rebuild();
173 \Drupal::service('entity.definition_update_manager')->applyUpdates();
177 * Creates the test fields.
179 protected function setupTestFields() {
180 if (empty($this->fieldName)) {
181 $this->fieldName = 'field_test_et_ui_test';
183 FieldStorageConfig::create([
184 'field_name' => $this->fieldName,
186 'entity_type' => $this->entityTypeId,
189 FieldConfig::create([
190 'entity_type' => $this->entityTypeId,
191 'field_name' => $this->fieldName,
192 'bundle' => $this->bundle,
193 'label' => 'Test translatable text-field',
195 entity_get_form_display($this->entityTypeId, $this->bundle, 'default')
196 ->setComponent($this->fieldName, [
197 'type' => 'string_textfield',
204 * Creates the entity to be translated.
206 * @param array $values
207 * An array of initial values for the entity.
208 * @param string $langcode
209 * The initial language code of the entity.
210 * @param string $bundle_name
211 * (optional) The entity bundle, if the entity uses bundles. Defaults to
212 * NULL. If left NULL, $this->bundle will be used.
217 protected function createEntity($values, $langcode, $bundle_name = NULL) {
218 $entity_values = $values;
219 $entity_values['langcode'] = $langcode;
220 $entity_type = \Drupal::entityManager()->getDefinition($this->entityTypeId);
221 if ($bundle_key = $entity_type->getKey('bundle')) {
222 $entity_values[$bundle_key] = $bundle_name ?: $this->bundle;
224 $controller = $this->container->get('entity.manager')->getStorage($this->entityTypeId);
225 if (!($controller instanceof SqlContentEntityStorage)) {
226 foreach ($values as $property => $value) {
227 if (is_array($value)) {
228 $entity_values[$property] = [$langcode => $value];
232 $entity = $this->container->get('entity_type.manager')
233 ->getStorage($this->entityTypeId)
234 ->create($entity_values);
236 return $entity->id();