Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\content_translation\Tests;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Entity\Sql\SqlContentEntityStorage;
|
Chris@0
|
6 use Drupal\field\Entity\FieldConfig;
|
Chris@0
|
7 use Drupal\language\Entity\ConfigurableLanguage;
|
Chris@0
|
8 use Drupal\simpletest\WebTestBase;
|
Chris@0
|
9 use Drupal\field\Entity\FieldStorageConfig;
|
Chris@0
|
10
|
Chris@0
|
11 /**
|
Chris@0
|
12 * Base class for content translation tests.
|
Chris@0
|
13 *
|
Chris@0
|
14 * @deprecated Scheduled for removal in Drupal 9.0.0.
|
Chris@0
|
15 * Use \Drupal\Tests\content_translation\Functional\ContentTranslationTestBase instead.
|
Chris@0
|
16 */
|
Chris@0
|
17 abstract class ContentTranslationTestBase extends WebTestBase {
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 * Modules to enable.
|
Chris@0
|
21 *
|
Chris@0
|
22 * @var array
|
Chris@0
|
23 */
|
Chris@0
|
24 public static $modules = ['text'];
|
Chris@0
|
25
|
Chris@0
|
26 /**
|
Chris@0
|
27 * The entity type being tested.
|
Chris@0
|
28 *
|
Chris@0
|
29 * @var string
|
Chris@0
|
30 */
|
Chris@0
|
31 protected $entityTypeId = 'entity_test_mul';
|
Chris@0
|
32
|
Chris@0
|
33 /**
|
Chris@0
|
34 * The bundle being tested.
|
Chris@0
|
35 *
|
Chris@0
|
36 * @var string
|
Chris@0
|
37 */
|
Chris@0
|
38 protected $bundle;
|
Chris@0
|
39
|
Chris@0
|
40 /**
|
Chris@0
|
41 * The added languages.
|
Chris@0
|
42 *
|
Chris@0
|
43 * @var array
|
Chris@0
|
44 */
|
Chris@0
|
45 protected $langcodes;
|
Chris@0
|
46
|
Chris@0
|
47 /**
|
Chris@0
|
48 * The account to be used to test translation operations.
|
Chris@0
|
49 *
|
Chris@0
|
50 * @var \Drupal\user\UserInterface
|
Chris@0
|
51 */
|
Chris@0
|
52 protected $translator;
|
Chris@0
|
53
|
Chris@0
|
54 /**
|
Chris@0
|
55 * The account to be used to test multilingual entity editing.
|
Chris@0
|
56 *
|
Chris@0
|
57 * @var \Drupal\user\UserInterface
|
Chris@0
|
58 */
|
Chris@0
|
59 protected $editor;
|
Chris@0
|
60
|
Chris@0
|
61 /**
|
Chris@0
|
62 * The account to be used to test access to both workflows.
|
Chris@0
|
63 *
|
Chris@0
|
64 * @var \Drupal\user\UserInterface
|
Chris@0
|
65 */
|
Chris@0
|
66 protected $administrator;
|
Chris@0
|
67
|
Chris@0
|
68 /**
|
Chris@0
|
69 * The name of the field used to test translation.
|
Chris@0
|
70 *
|
Chris@0
|
71 * @var string
|
Chris@0
|
72 */
|
Chris@0
|
73 protected $fieldName;
|
Chris@0
|
74
|
Chris@0
|
75 /**
|
Chris@0
|
76 * The translation controller for the current entity type.
|
Chris@0
|
77 *
|
Chris@0
|
78 * @var \Drupal\content_translation\ContentTranslationHandlerInterface
|
Chris@0
|
79 */
|
Chris@0
|
80 protected $controller;
|
Chris@0
|
81
|
Chris@0
|
82 /**
|
Chris@0
|
83 * @var \Drupal\content_translation\ContentTranslationManagerInterface
|
Chris@0
|
84 */
|
Chris@0
|
85 protected $manager;
|
Chris@0
|
86
|
Chris@0
|
87 protected function setUp() {
|
Chris@0
|
88 parent::setUp();
|
Chris@0
|
89
|
Chris@0
|
90 $this->setupLanguages();
|
Chris@0
|
91 $this->setupBundle();
|
Chris@0
|
92 $this->enableTranslation();
|
Chris@0
|
93 $this->setupUsers();
|
Chris@0
|
94 $this->setupTestFields();
|
Chris@0
|
95
|
Chris@0
|
96 $this->manager = $this->container->get('content_translation.manager');
|
Chris@0
|
97 $this->controller = $this->manager->getTranslationHandler($this->entityTypeId);
|
Chris@0
|
98
|
Chris@0
|
99 // Rebuild the container so that the new languages are picked up by services
|
Chris@0
|
100 // that hold a list of languages.
|
Chris@0
|
101 $this->rebuildContainer();
|
Chris@0
|
102 }
|
Chris@0
|
103
|
Chris@0
|
104 /**
|
Chris@0
|
105 * Adds additional languages.
|
Chris@0
|
106 */
|
Chris@0
|
107 protected function setupLanguages() {
|
Chris@0
|
108 $this->langcodes = ['it', 'fr'];
|
Chris@0
|
109 foreach ($this->langcodes as $langcode) {
|
Chris@0
|
110 ConfigurableLanguage::createFromLangcode($langcode)->save();
|
Chris@0
|
111 }
|
Chris@0
|
112 array_unshift($this->langcodes, \Drupal::languageManager()->getDefaultLanguage()->getId());
|
Chris@0
|
113 }
|
Chris@0
|
114
|
Chris@0
|
115 /**
|
Chris@0
|
116 * Returns an array of permissions needed for the translator.
|
Chris@0
|
117 */
|
Chris@0
|
118 protected function getTranslatorPermissions() {
|
Chris@0
|
119 return array_filter([$this->getTranslatePermission(), 'create content translations', 'update content translations', 'delete content translations']);
|
Chris@0
|
120 }
|
Chris@0
|
121
|
Chris@0
|
122 /**
|
Chris@0
|
123 * Returns the translate permissions for the current entity and bundle.
|
Chris@0
|
124 */
|
Chris@0
|
125 protected function getTranslatePermission() {
|
Chris@0
|
126 $entity_type = \Drupal::entityManager()->getDefinition($this->entityTypeId);
|
Chris@0
|
127 if ($permission_granularity = $entity_type->getPermissionGranularity()) {
|
Chris@0
|
128 return $permission_granularity == 'bundle' ? "translate {$this->bundle} {$this->entityTypeId}" : "translate {$this->entityTypeId}";
|
Chris@0
|
129 }
|
Chris@0
|
130 }
|
Chris@0
|
131
|
Chris@0
|
132 /**
|
Chris@0
|
133 * Returns an array of permissions needed for the editor.
|
Chris@0
|
134 */
|
Chris@0
|
135 protected function getEditorPermissions() {
|
Chris@0
|
136 // Every entity-type-specific test needs to define these.
|
Chris@0
|
137 return [];
|
Chris@0
|
138 }
|
Chris@0
|
139
|
Chris@0
|
140 /**
|
Chris@0
|
141 * Returns an array of permissions needed for the administrator.
|
Chris@0
|
142 */
|
Chris@0
|
143 protected function getAdministratorPermissions() {
|
Chris@0
|
144 return array_merge($this->getEditorPermissions(), $this->getTranslatorPermissions(), ['administer content translation']);
|
Chris@0
|
145 }
|
Chris@0
|
146
|
Chris@0
|
147 /**
|
Chris@0
|
148 * Creates and activates translator, editor and admin users.
|
Chris@0
|
149 */
|
Chris@0
|
150 protected function setupUsers() {
|
Chris@0
|
151 $this->translator = $this->drupalCreateUser($this->getTranslatorPermissions(), 'translator');
|
Chris@0
|
152 $this->editor = $this->drupalCreateUser($this->getEditorPermissions(), 'editor');
|
Chris@0
|
153 $this->administrator = $this->drupalCreateUser($this->getAdministratorPermissions(), 'administrator');
|
Chris@0
|
154 $this->drupalLogin($this->translator);
|
Chris@0
|
155 }
|
Chris@0
|
156
|
Chris@0
|
157 /**
|
Chris@0
|
158 * Creates or initializes the bundle date if needed.
|
Chris@0
|
159 */
|
Chris@0
|
160 protected function setupBundle() {
|
Chris@0
|
161 if (empty($this->bundle)) {
|
Chris@0
|
162 $this->bundle = $this->entityTypeId;
|
Chris@0
|
163 }
|
Chris@0
|
164 }
|
Chris@0
|
165
|
Chris@0
|
166 /**
|
Chris@0
|
167 * Enables translation for the current entity type and bundle.
|
Chris@0
|
168 */
|
Chris@0
|
169 protected function enableTranslation() {
|
Chris@0
|
170 // Enable translation for the current entity type and ensure the change is
|
Chris@0
|
171 // picked up.
|
Chris@0
|
172 \Drupal::service('content_translation.manager')->setEnabled($this->entityTypeId, $this->bundle, TRUE);
|
Chris@0
|
173 drupal_static_reset();
|
Chris@0
|
174 \Drupal::entityManager()->clearCachedDefinitions();
|
Chris@0
|
175 \Drupal::service('router.builder')->rebuild();
|
Chris@0
|
176 \Drupal::service('entity.definition_update_manager')->applyUpdates();
|
Chris@0
|
177 }
|
Chris@0
|
178
|
Chris@0
|
179 /**
|
Chris@0
|
180 * Creates the test fields.
|
Chris@0
|
181 */
|
Chris@0
|
182 protected function setupTestFields() {
|
Chris@0
|
183 if (empty($this->fieldName)) {
|
Chris@0
|
184 $this->fieldName = 'field_test_et_ui_test';
|
Chris@0
|
185 }
|
Chris@0
|
186 FieldStorageConfig::create([
|
Chris@0
|
187 'field_name' => $this->fieldName,
|
Chris@0
|
188 'type' => 'string',
|
Chris@0
|
189 'entity_type' => $this->entityTypeId,
|
Chris@0
|
190 'cardinality' => 1,
|
Chris@0
|
191 ])->save();
|
Chris@0
|
192 FieldConfig::create([
|
Chris@0
|
193 'entity_type' => $this->entityTypeId,
|
Chris@0
|
194 'field_name' => $this->fieldName,
|
Chris@0
|
195 'bundle' => $this->bundle,
|
Chris@0
|
196 'label' => 'Test translatable text-field',
|
Chris@0
|
197 ])->save();
|
Chris@0
|
198 entity_get_form_display($this->entityTypeId, $this->bundle, 'default')
|
Chris@0
|
199 ->setComponent($this->fieldName, [
|
Chris@0
|
200 'type' => 'string_textfield',
|
Chris@0
|
201 'weight' => 0,
|
Chris@0
|
202 ])
|
Chris@0
|
203 ->save();
|
Chris@0
|
204 }
|
Chris@0
|
205
|
Chris@0
|
206 /**
|
Chris@0
|
207 * Creates the entity to be translated.
|
Chris@0
|
208 *
|
Chris@0
|
209 * @param array $values
|
Chris@0
|
210 * An array of initial values for the entity.
|
Chris@0
|
211 * @param string $langcode
|
Chris@0
|
212 * The initial language code of the entity.
|
Chris@0
|
213 * @param string $bundle_name
|
Chris@0
|
214 * (optional) The entity bundle, if the entity uses bundles. Defaults to
|
Chris@0
|
215 * NULL. If left NULL, $this->bundle will be used.
|
Chris@0
|
216 *
|
Chris@0
|
217 * @return string
|
Chris@0
|
218 * The entity id.
|
Chris@0
|
219 */
|
Chris@0
|
220 protected function createEntity($values, $langcode, $bundle_name = NULL) {
|
Chris@0
|
221 $entity_values = $values;
|
Chris@0
|
222 $entity_values['langcode'] = $langcode;
|
Chris@0
|
223 $entity_type = \Drupal::entityManager()->getDefinition($this->entityTypeId);
|
Chris@0
|
224 if ($bundle_key = $entity_type->getKey('bundle')) {
|
Chris@0
|
225 $entity_values[$bundle_key] = $bundle_name ?: $this->bundle;
|
Chris@0
|
226 }
|
Chris@0
|
227 $controller = $this->container->get('entity.manager')->getStorage($this->entityTypeId);
|
Chris@0
|
228 if (!($controller instanceof SqlContentEntityStorage)) {
|
Chris@0
|
229 foreach ($values as $property => $value) {
|
Chris@0
|
230 if (is_array($value)) {
|
Chris@0
|
231 $entity_values[$property] = [$langcode => $value];
|
Chris@0
|
232 }
|
Chris@0
|
233 }
|
Chris@0
|
234 }
|
Chris@0
|
235 $entity = $this->container->get('entity_type.manager')
|
Chris@0
|
236 ->getStorage($this->entityTypeId)
|
Chris@0
|
237 ->create($entity_values);
|
Chris@0
|
238 $entity->save();
|
Chris@0
|
239 return $entity->id();
|
Chris@0
|
240 }
|
Chris@0
|
241
|
Chris@0
|
242 }
|