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