3 namespace Drupal\content_translation\Tests;
5 use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\language\Entity\ConfigurableLanguage;
8 use Drupal\simpletest\WebTestBase;
9 use Drupal\field\Entity\FieldStorageConfig;
12 * Base class for content translation tests.
14 * @deprecated Scheduled for removal in Drupal 9.0.0.
15 * Use \Drupal\Tests\content_translation\Functional\ContentTranslationTestBase instead.
17 abstract class ContentTranslationTestBase extends WebTestBase {
24 public static $modules = ['text'];
27 * The entity type being tested.
31 protected $entityTypeId = 'entity_test_mul';
34 * The bundle being tested.
41 * The added languages.
48 * The account to be used to test translation operations.
50 * @var \Drupal\user\UserInterface
52 protected $translator;
55 * The account to be used to test multilingual entity editing.
57 * @var \Drupal\user\UserInterface
62 * The account to be used to test access to both workflows.
64 * @var \Drupal\user\UserInterface
66 protected $administrator;
69 * The name of the field used to test translation.
76 * The translation controller for the current entity type.
78 * @var \Drupal\content_translation\ContentTranslationHandlerInterface
80 protected $controller;
83 * @var \Drupal\content_translation\ContentTranslationManagerInterface
87 protected function setUp() {
90 $this->setupLanguages();
92 $this->enableTranslation();
94 $this->setupTestFields();
96 $this->manager = $this->container->get('content_translation.manager');
97 $this->controller = $this->manager->getTranslationHandler($this->entityTypeId);
99 // Rebuild the container so that the new languages are picked up by services
100 // that hold a list of languages.
101 $this->rebuildContainer();
105 * Adds additional languages.
107 protected function setupLanguages() {
108 $this->langcodes = ['it', 'fr'];
109 foreach ($this->langcodes as $langcode) {
110 ConfigurableLanguage::createFromLangcode($langcode)->save();
112 array_unshift($this->langcodes, \Drupal::languageManager()->getDefaultLanguage()->getId());
116 * Returns an array of permissions needed for the translator.
118 protected function getTranslatorPermissions() {
119 return array_filter([$this->getTranslatePermission(), 'create content translations', 'update content translations', 'delete content translations']);
123 * Returns the translate permissions for the current entity and bundle.
125 protected function getTranslatePermission() {
126 $entity_type = \Drupal::entityManager()->getDefinition($this->entityTypeId);
127 if ($permission_granularity = $entity_type->getPermissionGranularity()) {
128 return $permission_granularity == 'bundle' ? "translate {$this->bundle} {$this->entityTypeId}" : "translate {$this->entityTypeId}";
133 * Returns an array of permissions needed for the editor.
135 protected function getEditorPermissions() {
136 // Every entity-type-specific test needs to define these.
141 * Returns an array of permissions needed for the administrator.
143 protected function getAdministratorPermissions() {
144 return array_merge($this->getEditorPermissions(), $this->getTranslatorPermissions(), ['administer content translation']);
148 * Creates and activates translator, editor and admin users.
150 protected function setupUsers() {
151 $this->translator = $this->drupalCreateUser($this->getTranslatorPermissions(), 'translator');
152 $this->editor = $this->drupalCreateUser($this->getEditorPermissions(), 'editor');
153 $this->administrator = $this->drupalCreateUser($this->getAdministratorPermissions(), 'administrator');
154 $this->drupalLogin($this->translator);
158 * Creates or initializes the bundle date if needed.
160 protected function setupBundle() {
161 if (empty($this->bundle)) {
162 $this->bundle = $this->entityTypeId;
167 * Enables translation for the current entity type and bundle.
169 protected function enableTranslation() {
170 // Enable translation for the current entity type and ensure the change is
172 \Drupal::service('content_translation.manager')->setEnabled($this->entityTypeId, $this->bundle, TRUE);
173 drupal_static_reset();
174 \Drupal::entityManager()->clearCachedDefinitions();
175 \Drupal::service('router.builder')->rebuild();
176 \Drupal::service('entity.definition_update_manager')->applyUpdates();
180 * Creates the test fields.
182 protected function setupTestFields() {
183 if (empty($this->fieldName)) {
184 $this->fieldName = 'field_test_et_ui_test';
186 FieldStorageConfig::create([
187 'field_name' => $this->fieldName,
189 'entity_type' => $this->entityTypeId,
192 FieldConfig::create([
193 'entity_type' => $this->entityTypeId,
194 'field_name' => $this->fieldName,
195 'bundle' => $this->bundle,
196 'label' => 'Test translatable text-field',
198 entity_get_form_display($this->entityTypeId, $this->bundle, 'default')
199 ->setComponent($this->fieldName, [
200 'type' => 'string_textfield',
207 * Creates the entity to be translated.
209 * @param array $values
210 * An array of initial values for the entity.
211 * @param string $langcode
212 * The initial language code of the entity.
213 * @param string $bundle_name
214 * (optional) The entity bundle, if the entity uses bundles. Defaults to
215 * NULL. If left NULL, $this->bundle will be used.
220 protected function createEntity($values, $langcode, $bundle_name = NULL) {
221 $entity_values = $values;
222 $entity_values['langcode'] = $langcode;
223 $entity_type = \Drupal::entityManager()->getDefinition($this->entityTypeId);
224 if ($bundle_key = $entity_type->getKey('bundle')) {
225 $entity_values[$bundle_key] = $bundle_name ?: $this->bundle;
227 $controller = $this->container->get('entity.manager')->getStorage($this->entityTypeId);
228 if (!($controller instanceof SqlContentEntityStorage)) {
229 foreach ($values as $property => $value) {
230 if (is_array($value)) {
231 $entity_values[$property] = [$langcode => $value];
235 $entity = $this->container->get('entity_type.manager')
236 ->getStorage($this->entityTypeId)
237 ->create($entity_values);
239 return $entity->id();