Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\content_translation\Tests;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Core\Cache\Cache;
|
Chris@0
|
6 use Drupal\Core\Entity\EntityChangedInterface;
|
Chris@0
|
7 use Drupal\Core\Entity\EntityInterface;
|
Chris@0
|
8 use Drupal\Core\Language\Language;
|
Chris@0
|
9 use Drupal\Core\Language\LanguageInterface;
|
Chris@0
|
10 use Drupal\Core\Url;
|
Chris@0
|
11 use Drupal\language\Entity\ConfigurableLanguage;
|
Chris@0
|
12 use Drupal\Component\Utility\SafeMarkup;
|
Chris@0
|
13 use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait;
|
Chris@0
|
14
|
Chris@0
|
15 /**
|
Chris@0
|
16 * Tests the Content Translation UI.
|
Chris@0
|
17 *
|
Chris@0
|
18 * @deprecated Scheduled for removal in Drupal 9.0.0.
|
Chris@0
|
19 * Use \Drupal\Tests\content_translation\Functional\ContentTranslationUITestBase instead.
|
Chris@0
|
20 */
|
Chris@0
|
21 abstract class ContentTranslationUITestBase extends ContentTranslationTestBase {
|
Chris@0
|
22
|
Chris@0
|
23 use AssertPageCacheContextsAndTagsTrait;
|
Chris@0
|
24
|
Chris@0
|
25 /**
|
Chris@0
|
26 * The id of the entity being translated.
|
Chris@0
|
27 *
|
Chris@0
|
28 * @var mixed
|
Chris@0
|
29 */
|
Chris@0
|
30 protected $entityId;
|
Chris@0
|
31
|
Chris@0
|
32 /**
|
Chris@0
|
33 * Whether the behavior of the language selector should be tested.
|
Chris@0
|
34 *
|
Chris@0
|
35 * @var bool
|
Chris@0
|
36 */
|
Chris@0
|
37 protected $testLanguageSelector = TRUE;
|
Chris@0
|
38
|
Chris@0
|
39 /**
|
Chris@0
|
40 * Flag that tells whether the HTML escaping of all languages works or not
|
Chris@0
|
41 * after SafeMarkup change.
|
Chris@0
|
42 *
|
Chris@0
|
43 * @var bool
|
Chris@0
|
44 */
|
Chris@0
|
45 protected $testHTMLEscapeForAllLanguages = FALSE;
|
Chris@0
|
46
|
Chris@0
|
47 /**
|
Chris@0
|
48 * Default cache contexts expected on a non-translated entity.
|
Chris@0
|
49 *
|
Chris@0
|
50 * Cache contexts will not be checked if this list is empty.
|
Chris@0
|
51 *
|
Chris@0
|
52 * @var string[]
|
Chris@0
|
53 */
|
Chris@0
|
54 protected $defaultCacheContexts = ['languages:language_interface', 'theme', 'url.query_args:_wrapper_format', 'user.permissions'];
|
Chris@0
|
55
|
Chris@0
|
56 /**
|
Chris@0
|
57 * Tests the basic translation UI.
|
Chris@0
|
58 */
|
Chris@0
|
59 public function testTranslationUI() {
|
Chris@0
|
60 $this->doTestBasicTranslation();
|
Chris@0
|
61 $this->doTestTranslationOverview();
|
Chris@0
|
62 $this->doTestOutdatedStatus();
|
Chris@0
|
63 $this->doTestPublishedStatus();
|
Chris@0
|
64 $this->doTestAuthoringInfo();
|
Chris@0
|
65 $this->doTestTranslationEdit();
|
Chris@0
|
66 $this->doTestTranslationChanged();
|
Chris@0
|
67 $this->doTestChangedTimeAfterSaveWithoutChanges();
|
Chris@0
|
68 $this->doTestTranslationDeletion();
|
Chris@0
|
69 }
|
Chris@0
|
70
|
Chris@0
|
71 /**
|
Chris@0
|
72 * Tests the basic translation workflow.
|
Chris@0
|
73 */
|
Chris@0
|
74 protected function doTestBasicTranslation() {
|
Chris@0
|
75 // Create a new test entity with original values in the default language.
|
Chris@0
|
76 $default_langcode = $this->langcodes[0];
|
Chris@0
|
77 $values[$default_langcode] = $this->getNewEntityValues($default_langcode);
|
Chris@0
|
78 // Create the entity with the editor as owner, so that afterwards a new
|
Chris@0
|
79 // translation is created by the translator and the translation author is
|
Chris@0
|
80 // tested.
|
Chris@0
|
81 $this->drupalLogin($this->editor);
|
Chris@0
|
82 $this->entityId = $this->createEntity($values[$default_langcode], $default_langcode);
|
Chris@0
|
83 $this->drupalLogin($this->translator);
|
Chris@0
|
84 $storage = $this->container->get('entity_type.manager')
|
Chris@0
|
85 ->getStorage($this->entityTypeId);
|
Chris@0
|
86 $storage->resetCache([$this->entityId]);
|
Chris@0
|
87 $entity = $storage->load($this->entityId);
|
Chris@0
|
88 $this->assertTrue($entity, 'Entity found in the database.');
|
Chris@0
|
89 $this->drupalGet($entity->urlInfo());
|
Chris@0
|
90 $this->assertResponse(200, 'Entity URL is valid.');
|
Chris@0
|
91
|
Chris@0
|
92 // Ensure that the content language cache context is not yet added to the
|
Chris@0
|
93 // page.
|
Chris@0
|
94 $this->assertCacheContexts($this->defaultCacheContexts);
|
Chris@0
|
95
|
Chris@0
|
96 $this->drupalGet($entity->urlInfo('drupal:content-translation-overview'));
|
Chris@0
|
97 $this->assertNoText('Source language', 'Source language column correctly hidden.');
|
Chris@0
|
98
|
Chris@0
|
99 $translation = $this->getTranslation($entity, $default_langcode);
|
Chris@0
|
100 foreach ($values[$default_langcode] as $property => $value) {
|
Chris@0
|
101 $stored_value = $this->getValue($translation, $property, $default_langcode);
|
Chris@0
|
102 $value = is_array($value) ? $value[0]['value'] : $value;
|
Chris@0
|
103 $message = format_string('@property correctly stored in the default language.', ['@property' => $property]);
|
Chris@0
|
104 $this->assertEqual($stored_value, $value, $message);
|
Chris@0
|
105 }
|
Chris@0
|
106
|
Chris@0
|
107 // Add a content translation.
|
Chris@0
|
108 $langcode = 'it';
|
Chris@0
|
109 $language = ConfigurableLanguage::load($langcode);
|
Chris@0
|
110 $values[$langcode] = $this->getNewEntityValues($langcode);
|
Chris@0
|
111
|
Chris@0
|
112 $entity_type_id = $entity->getEntityTypeId();
|
Chris@0
|
113 $add_url = Url::fromRoute("entity.$entity_type_id.content_translation_add", [
|
Chris@0
|
114 $entity->getEntityTypeId() => $entity->id(),
|
Chris@0
|
115 'source' => $default_langcode,
|
Chris@0
|
116 'target' => $langcode
|
Chris@0
|
117 ], ['language' => $language]);
|
Chris@0
|
118 $this->drupalPostForm($add_url, $this->getEditValues($values, $langcode), $this->getFormSubmitActionForNewTranslation($entity, $langcode));
|
Chris@0
|
119
|
Chris@0
|
120 // Assert that HTML is escaped in "all languages" in UI after SafeMarkup
|
Chris@0
|
121 // change.
|
Chris@0
|
122 if ($this->testHTMLEscapeForAllLanguages) {
|
Chris@0
|
123 $this->assertNoRaw('<span class="translation-entity-all-languages">(all languages)</span>');
|
Chris@0
|
124 $this->assertRaw('<span class="translation-entity-all-languages">(all languages)</span>');
|
Chris@0
|
125 }
|
Chris@0
|
126
|
Chris@0
|
127 // Ensure that the content language cache context is not yet added to the
|
Chris@0
|
128 // page.
|
Chris@0
|
129 $storage = $this->container->get('entity_type.manager')
|
Chris@0
|
130 ->getStorage($this->entityTypeId);
|
Chris@0
|
131 $storage->resetCache([$this->entityId]);
|
Chris@0
|
132 $entity = $storage->load($this->entityId);
|
Chris@0
|
133 $this->drupalGet($entity->urlInfo());
|
Chris@0
|
134 $this->assertCacheContexts(Cache::mergeContexts(['languages:language_content'], $this->defaultCacheContexts));
|
Chris@0
|
135
|
Chris@0
|
136 // Reset the cache of the entity, so that the new translation gets the
|
Chris@0
|
137 // updated values.
|
Chris@0
|
138 $metadata_source_translation = $this->manager->getTranslationMetadata($entity->getTranslation($default_langcode));
|
Chris@0
|
139 $metadata_target_translation = $this->manager->getTranslationMetadata($entity->getTranslation($langcode));
|
Chris@0
|
140
|
Chris@0
|
141 $author_field_name = $entity->hasField('content_translation_uid') ? 'content_translation_uid' : 'uid';
|
Chris@0
|
142 if ($entity->getFieldDefinition($author_field_name)->isTranslatable()) {
|
Chris@0
|
143 $this->assertEqual($metadata_target_translation->getAuthor()->id(), $this->translator->id(),
|
Chris@0
|
144 SafeMarkup::format('Author of the target translation @langcode correctly stored for translatable owner field.', ['@langcode' => $langcode]));
|
Chris@0
|
145
|
Chris@0
|
146 $this->assertNotEqual($metadata_target_translation->getAuthor()->id(), $metadata_source_translation->getAuthor()->id(),
|
Chris@0
|
147 SafeMarkup::format('Author of the target translation @target different from the author of the source translation @source for translatable owner field.',
|
Chris@0
|
148 ['@target' => $langcode, '@source' => $default_langcode]));
|
Chris@0
|
149 }
|
Chris@0
|
150 else {
|
Chris@0
|
151 $this->assertEqual($metadata_target_translation->getAuthor()->id(), $this->editor->id(), 'Author of the entity remained untouched after translation for non translatable owner field.');
|
Chris@0
|
152 }
|
Chris@0
|
153
|
Chris@0
|
154 $created_field_name = $entity->hasField('content_translation_created') ? 'content_translation_created' : 'created';
|
Chris@0
|
155 if ($entity->getFieldDefinition($created_field_name)->isTranslatable()) {
|
Chris@0
|
156 $this->assertTrue($metadata_target_translation->getCreatedTime() > $metadata_source_translation->getCreatedTime(),
|
Chris@0
|
157 SafeMarkup::format('Translation creation timestamp of the target translation @target is newer than the creation timestamp of the source translation @source for translatable created field.',
|
Chris@0
|
158 ['@target' => $langcode, '@source' => $default_langcode]));
|
Chris@0
|
159 }
|
Chris@0
|
160 else {
|
Chris@0
|
161 $this->assertEqual($metadata_target_translation->getCreatedTime(), $metadata_source_translation->getCreatedTime(), 'Creation timestamp of the entity remained untouched after translation for non translatable created field.');
|
Chris@0
|
162 }
|
Chris@0
|
163
|
Chris@0
|
164 if ($this->testLanguageSelector) {
|
Chris@0
|
165 $this->assertNoFieldByXPath('//select[@id="edit-langcode-0-value"]', NULL, 'Language selector correctly disabled on translations.');
|
Chris@0
|
166 }
|
Chris@0
|
167 $storage->resetCache([$this->entityId]);
|
Chris@0
|
168 $entity = $storage->load($this->entityId);
|
Chris@0
|
169 $this->drupalGet($entity->urlInfo('drupal:content-translation-overview'));
|
Chris@0
|
170 $this->assertNoText('Source language', 'Source language column correctly hidden.');
|
Chris@0
|
171
|
Chris@0
|
172 // Switch the source language.
|
Chris@0
|
173 $langcode = 'fr';
|
Chris@0
|
174 $language = ConfigurableLanguage::load($langcode);
|
Chris@0
|
175 $source_langcode = 'it';
|
Chris@0
|
176 $edit = ['source_langcode[source]' => $source_langcode];
|
Chris@0
|
177 $entity_type_id = $entity->getEntityTypeId();
|
Chris@0
|
178 $add_url = Url::fromRoute("entity.$entity_type_id.content_translation_add", [
|
Chris@0
|
179 $entity->getEntityTypeId() => $entity->id(),
|
Chris@0
|
180 'source' => $default_langcode,
|
Chris@0
|
181 'target' => $langcode
|
Chris@0
|
182 ], ['language' => $language]);
|
Chris@0
|
183 // This does not save anything, it merely reloads the form and fills in the
|
Chris@0
|
184 // fields with the values from the different source language.
|
Chris@0
|
185 $this->drupalPostForm($add_url, $edit, t('Change'));
|
Chris@0
|
186 $this->assertFieldByXPath("//input[@name=\"{$this->fieldName}[0][value]\"]", $values[$source_langcode][$this->fieldName][0]['value'], 'Source language correctly switched.');
|
Chris@0
|
187
|
Chris@0
|
188 // Add another translation and mark the other ones as outdated.
|
Chris@0
|
189 $values[$langcode] = $this->getNewEntityValues($langcode);
|
Chris@0
|
190 $edit = $this->getEditValues($values, $langcode) + ['content_translation[retranslate]' => TRUE];
|
Chris@0
|
191 $entity_type_id = $entity->getEntityTypeId();
|
Chris@0
|
192 $add_url = Url::fromRoute("entity.$entity_type_id.content_translation_add", [
|
Chris@0
|
193 $entity->getEntityTypeId() => $entity->id(),
|
Chris@0
|
194 'source' => $source_langcode,
|
Chris@0
|
195 'target' => $langcode
|
Chris@0
|
196 ], ['language' => $language]);
|
Chris@0
|
197 $this->drupalPostForm($add_url, $edit, $this->getFormSubmitActionForNewTranslation($entity, $langcode));
|
Chris@0
|
198 $storage->resetCache([$this->entityId]);
|
Chris@0
|
199 $entity = $storage->load($this->entityId);
|
Chris@0
|
200 $this->drupalGet($entity->urlInfo('drupal:content-translation-overview'));
|
Chris@0
|
201 $this->assertText('Source language', 'Source language column correctly shown.');
|
Chris@0
|
202
|
Chris@0
|
203 // Check that the entered values have been correctly stored.
|
Chris@0
|
204 foreach ($values as $langcode => $property_values) {
|
Chris@0
|
205 $translation = $this->getTranslation($entity, $langcode);
|
Chris@0
|
206 foreach ($property_values as $property => $value) {
|
Chris@0
|
207 $stored_value = $this->getValue($translation, $property, $langcode);
|
Chris@0
|
208 $value = is_array($value) ? $value[0]['value'] : $value;
|
Chris@0
|
209 $message = format_string('%property correctly stored with language %language.', ['%property' => $property, '%language' => $langcode]);
|
Chris@0
|
210 $this->assertEqual($stored_value, $value, $message);
|
Chris@0
|
211 }
|
Chris@0
|
212 }
|
Chris@0
|
213 }
|
Chris@0
|
214
|
Chris@0
|
215 /**
|
Chris@0
|
216 * Tests that the translation overview shows the correct values.
|
Chris@0
|
217 */
|
Chris@0
|
218 protected function doTestTranslationOverview() {
|
Chris@0
|
219 $storage = $this->container->get('entity_type.manager')
|
Chris@0
|
220 ->getStorage($this->entityTypeId);
|
Chris@0
|
221 $storage->resetCache([$this->entityId]);
|
Chris@0
|
222 $entity = $storage->load($this->entityId);
|
Chris@0
|
223 $translate_url = $entity->urlInfo('drupal:content-translation-overview');
|
Chris@0
|
224 $this->drupalGet($translate_url);
|
Chris@0
|
225 $translate_url->setAbsolute(FALSE);
|
Chris@0
|
226
|
Chris@0
|
227 foreach ($this->langcodes as $langcode) {
|
Chris@0
|
228 if ($entity->hasTranslation($langcode)) {
|
Chris@0
|
229 $language = new Language(['id' => $langcode]);
|
Chris@0
|
230 $view_url = $entity->url('canonical', ['language' => $language]);
|
Chris@0
|
231 $elements = $this->xpath('//table//a[@href=:href]', [':href' => $view_url]);
|
Chris@0
|
232 $this->assertEqual((string) $elements[0], $entity->getTranslation($langcode)->label(), format_string('Label correctly shown for %language translation.', ['%language' => $langcode]));
|
Chris@0
|
233 $edit_path = $entity->url('edit-form', ['language' => $language]);
|
Chris@0
|
234 $elements = $this->xpath('//table//ul[@class="dropbutton"]/li/a[@href=:href]', [':href' => $edit_path]);
|
Chris@0
|
235 $this->assertEqual((string) $elements[0], t('Edit'), format_string('Edit link correct for %language translation.', ['%language' => $langcode]));
|
Chris@0
|
236 }
|
Chris@0
|
237 }
|
Chris@0
|
238 }
|
Chris@0
|
239
|
Chris@0
|
240 /**
|
Chris@0
|
241 * Tests up-to-date status tracking.
|
Chris@0
|
242 */
|
Chris@0
|
243 protected function doTestOutdatedStatus() {
|
Chris@0
|
244 $storage = $this->container->get('entity_type.manager')
|
Chris@0
|
245 ->getStorage($this->entityTypeId);
|
Chris@0
|
246 $storage->resetCache([$this->entityId]);
|
Chris@0
|
247 $entity = $storage->load($this->entityId);
|
Chris@0
|
248 $langcode = 'fr';
|
Chris@0
|
249 $languages = \Drupal::languageManager()->getLanguages();
|
Chris@0
|
250
|
Chris@0
|
251 // Mark translations as outdated.
|
Chris@0
|
252 $edit = ['content_translation[retranslate]' => TRUE];
|
Chris@0
|
253 $edit_path = $entity->urlInfo('edit-form', ['language' => $languages[$langcode]]);
|
Chris@0
|
254 $this->drupalPostForm($edit_path, $edit, $this->getFormSubmitAction($entity, $langcode));
|
Chris@0
|
255 $storage->resetCache([$this->entityId]);
|
Chris@0
|
256 $entity = $storage->load($this->entityId);
|
Chris@0
|
257
|
Chris@0
|
258 // Check that every translation has the correct "outdated" status, and that
|
Chris@0
|
259 // the Translation fieldset is open if the translation is "outdated".
|
Chris@0
|
260 foreach ($this->langcodes as $added_langcode) {
|
Chris@0
|
261 $url = $entity->urlInfo('edit-form', ['language' => ConfigurableLanguage::load($added_langcode)]);
|
Chris@0
|
262 $this->drupalGet($url);
|
Chris@0
|
263 if ($added_langcode == $langcode) {
|
Chris@0
|
264 $this->assertFieldByXPath('//input[@name="content_translation[retranslate]"]', FALSE, 'The retranslate flag is not checked by default.');
|
Chris@0
|
265 $this->assertFalse($this->xpath('//details[@id="edit-content-translation" and @open="open"]'), 'The translation tab should be collapsed by default.');
|
Chris@0
|
266 }
|
Chris@0
|
267 else {
|
Chris@0
|
268 $this->assertFieldByXPath('//input[@name="content_translation[outdated]"]', TRUE, 'The translate flag is checked by default.');
|
Chris@0
|
269 $this->assertTrue($this->xpath('//details[@id="edit-content-translation" and @open="open"]'), 'The translation tab is correctly expanded when the translation is outdated.');
|
Chris@0
|
270 $edit = ['content_translation[outdated]' => FALSE];
|
Chris@0
|
271 $this->drupalPostForm($url, $edit, $this->getFormSubmitAction($entity, $added_langcode));
|
Chris@0
|
272 $this->drupalGet($url);
|
Chris@0
|
273 $this->assertFieldByXPath('//input[@name="content_translation[retranslate]"]', FALSE, 'The retranslate flag is now shown.');
|
Chris@0
|
274 $storage = $this->container->get('entity_type.manager')
|
Chris@0
|
275 ->getStorage($this->entityTypeId);
|
Chris@0
|
276 $storage->resetCache([$this->entityId]);
|
Chris@0
|
277 $entity = $storage->load($this->entityId);
|
Chris@0
|
278 $this->assertFalse($this->manager->getTranslationMetadata($entity->getTranslation($added_langcode))->isOutdated(), 'The "outdated" status has been correctly stored.');
|
Chris@0
|
279 }
|
Chris@0
|
280 }
|
Chris@0
|
281 }
|
Chris@0
|
282
|
Chris@0
|
283 /**
|
Chris@0
|
284 * Tests the translation publishing status.
|
Chris@0
|
285 */
|
Chris@0
|
286 protected function doTestPublishedStatus() {
|
Chris@0
|
287 $storage = $this->container->get('entity_type.manager')
|
Chris@0
|
288 ->getStorage($this->entityTypeId);
|
Chris@0
|
289 $storage->resetCache([$this->entityId]);
|
Chris@0
|
290 $entity = $storage->load($this->entityId);
|
Chris@0
|
291
|
Chris@0
|
292 // Unpublish translations.
|
Chris@0
|
293 foreach ($this->langcodes as $index => $langcode) {
|
Chris@0
|
294 if ($index > 0) {
|
Chris@0
|
295 $url = $entity->urlInfo('edit-form', ['language' => ConfigurableLanguage::load($langcode)]);
|
Chris@0
|
296 $edit = ['content_translation[status]' => FALSE];
|
Chris@0
|
297 $this->drupalPostForm($url, $edit, $this->getFormSubmitAction($entity, $langcode));
|
Chris@0
|
298 $storage = $this->container->get('entity_type.manager')
|
Chris@0
|
299 ->getStorage($this->entityTypeId);
|
Chris@0
|
300 $storage->resetCache([$this->entityId]);
|
Chris@0
|
301 $entity = $storage->load($this->entityId);
|
Chris@0
|
302 $this->assertFalse($this->manager->getTranslationMetadata($entity->getTranslation($langcode))->isPublished(), 'The translation has been correctly unpublished.');
|
Chris@0
|
303 }
|
Chris@0
|
304 }
|
Chris@0
|
305
|
Chris@0
|
306 // Check that the last published translation cannot be unpublished.
|
Chris@0
|
307 $this->drupalGet($entity->urlInfo('edit-form'));
|
Chris@0
|
308 $this->assertFieldByXPath('//input[@name="content_translation[status]" and @disabled="disabled"]', TRUE, 'The last translation is published and cannot be unpublished.');
|
Chris@0
|
309 }
|
Chris@0
|
310
|
Chris@0
|
311 /**
|
Chris@0
|
312 * Tests the translation authoring information.
|
Chris@0
|
313 */
|
Chris@0
|
314 protected function doTestAuthoringInfo() {
|
Chris@0
|
315 $storage = $this->container->get('entity_type.manager')
|
Chris@0
|
316 ->getStorage($this->entityTypeId);
|
Chris@0
|
317 $storage->resetCache([$this->entityId]);
|
Chris@0
|
318 $entity = $storage->load($this->entityId);
|
Chris@0
|
319 $values = [];
|
Chris@0
|
320
|
Chris@0
|
321 // Post different authoring information for each translation.
|
Chris@0
|
322 foreach ($this->langcodes as $index => $langcode) {
|
Chris@0
|
323 $user = $this->drupalCreateUser();
|
Chris@0
|
324 $values[$langcode] = [
|
Chris@0
|
325 'uid' => $user->id(),
|
Chris@0
|
326 'created' => REQUEST_TIME - mt_rand(0, 1000),
|
Chris@0
|
327 ];
|
Chris@0
|
328 $edit = [
|
Chris@0
|
329 'content_translation[uid]' => $user->getUsername(),
|
Chris@0
|
330 'content_translation[created]' => format_date($values[$langcode]['created'], 'custom', 'Y-m-d H:i:s O'),
|
Chris@0
|
331 ];
|
Chris@0
|
332 $url = $entity->urlInfo('edit-form', ['language' => ConfigurableLanguage::load($langcode)]);
|
Chris@0
|
333 $this->drupalPostForm($url, $edit, $this->getFormSubmitAction($entity, $langcode));
|
Chris@0
|
334 }
|
Chris@0
|
335
|
Chris@0
|
336 $storage = $this->container->get('entity_type.manager')
|
Chris@0
|
337 ->getStorage($this->entityTypeId);
|
Chris@0
|
338 $storage->resetCache([$this->entityId]);
|
Chris@0
|
339 $entity = $storage->load($this->entityId);
|
Chris@0
|
340 foreach ($this->langcodes as $langcode) {
|
Chris@0
|
341 $metadata = $this->manager->getTranslationMetadata($entity->getTranslation($langcode));
|
Chris@0
|
342 $this->assertEqual($metadata->getAuthor()->id(), $values[$langcode]['uid'], 'Translation author correctly stored.');
|
Chris@0
|
343 $this->assertEqual($metadata->getCreatedTime(), $values[$langcode]['created'], 'Translation date correctly stored.');
|
Chris@0
|
344 }
|
Chris@0
|
345
|
Chris@0
|
346 // Try to post non valid values and check that they are rejected.
|
Chris@0
|
347 $langcode = 'en';
|
Chris@0
|
348 $edit = [
|
Chris@0
|
349 // User names have by default length 8.
|
Chris@0
|
350 'content_translation[uid]' => $this->randomMachineName(12),
|
Chris@0
|
351 'content_translation[created]' => '19/11/1978',
|
Chris@0
|
352 ];
|
Chris@0
|
353 $this->drupalPostForm($entity->urlInfo('edit-form'), $edit, $this->getFormSubmitAction($entity, $langcode));
|
Chris@0
|
354 $this->assertTrue($this->xpath('//div[contains(@class, "error")]//ul'), 'Invalid values generate a list of form errors.');
|
Chris@0
|
355 $metadata = $this->manager->getTranslationMetadata($entity->getTranslation($langcode));
|
Chris@0
|
356 $this->assertEqual($metadata->getAuthor()->id(), $values[$langcode]['uid'], 'Translation author correctly kept.');
|
Chris@0
|
357 $this->assertEqual($metadata->getCreatedTime(), $values[$langcode]['created'], 'Translation date correctly kept.');
|
Chris@0
|
358 }
|
Chris@0
|
359
|
Chris@0
|
360 /**
|
Chris@0
|
361 * Tests translation deletion.
|
Chris@0
|
362 */
|
Chris@0
|
363 protected function doTestTranslationDeletion() {
|
Chris@0
|
364 // Confirm and delete a translation.
|
Chris@0
|
365 $this->drupalLogin($this->translator);
|
Chris@0
|
366 $langcode = 'fr';
|
Chris@0
|
367 $storage = $this->container->get('entity_type.manager')
|
Chris@0
|
368 ->getStorage($this->entityTypeId);
|
Chris@0
|
369 $storage->resetCache([$this->entityId]);
|
Chris@0
|
370 $entity = $storage->load($this->entityId);
|
Chris@0
|
371 $language = ConfigurableLanguage::load($langcode);
|
Chris@0
|
372 $url = $entity->urlInfo('edit-form', ['language' => $language]);
|
Chris@0
|
373 $this->drupalPostForm($url, [], t('Delete translation'));
|
Chris@0
|
374 $this->drupalPostForm(NULL, [], t('Delete @language translation', ['@language' => $language->getName()]));
|
Chris@0
|
375 $storage->resetCache([$this->entityId]);
|
Chris@0
|
376 $entity = $storage->load($this->entityId, TRUE);
|
Chris@0
|
377 if ($this->assertTrue(is_object($entity), 'Entity found')) {
|
Chris@0
|
378 $translations = $entity->getTranslationLanguages();
|
Chris@0
|
379 $this->assertTrue(count($translations) == 2 && empty($translations[$langcode]), 'Translation successfully deleted.');
|
Chris@0
|
380 }
|
Chris@0
|
381
|
Chris@0
|
382 // Check that the translator cannot delete the original translation.
|
Chris@0
|
383 $args = [$this->entityTypeId => $entity->id(), 'language' => 'en'];
|
Chris@0
|
384 $this->drupalGet(Url::fromRoute("entity.$this->entityTypeId.content_translation_delete", $args));
|
Chris@0
|
385 $this->assertResponse(403);
|
Chris@0
|
386 }
|
Chris@0
|
387
|
Chris@0
|
388 /**
|
Chris@0
|
389 * Returns an array of entity field values to be tested.
|
Chris@0
|
390 */
|
Chris@0
|
391 protected function getNewEntityValues($langcode) {
|
Chris@0
|
392 return [$this->fieldName => [['value' => $this->randomMachineName(16)]]];
|
Chris@0
|
393 }
|
Chris@0
|
394
|
Chris@0
|
395 /**
|
Chris@0
|
396 * Returns an edit array containing the values to be posted.
|
Chris@0
|
397 */
|
Chris@0
|
398 protected function getEditValues($values, $langcode, $new = FALSE) {
|
Chris@0
|
399 $edit = $values[$langcode];
|
Chris@0
|
400 $langcode = $new ? LanguageInterface::LANGCODE_NOT_SPECIFIED : $langcode;
|
Chris@0
|
401 foreach ($values[$langcode] as $property => $value) {
|
Chris@0
|
402 if (is_array($value)) {
|
Chris@0
|
403 $edit["{$property}[0][value]"] = $value[0]['value'];
|
Chris@0
|
404 unset($edit[$property]);
|
Chris@0
|
405 }
|
Chris@0
|
406 }
|
Chris@0
|
407 return $edit;
|
Chris@0
|
408 }
|
Chris@0
|
409
|
Chris@0
|
410 /**
|
Chris@0
|
411 * Returns the form action value when submitting a new translation.
|
Chris@0
|
412 *
|
Chris@0
|
413 * @param \Drupal\Core\Entity\EntityInterface $entity
|
Chris@0
|
414 * The entity being tested.
|
Chris@0
|
415 * @param string $langcode
|
Chris@0
|
416 * Language code for the form.
|
Chris@0
|
417 *
|
Chris@0
|
418 * @return string
|
Chris@0
|
419 * Name of the button to hit.
|
Chris@0
|
420 */
|
Chris@0
|
421 protected function getFormSubmitActionForNewTranslation(EntityInterface $entity, $langcode) {
|
Chris@0
|
422 $entity->addTranslation($langcode, $entity->toArray());
|
Chris@0
|
423 return $this->getFormSubmitAction($entity, $langcode);
|
Chris@0
|
424 }
|
Chris@0
|
425
|
Chris@0
|
426 /**
|
Chris@0
|
427 * Returns the form action value to be used to submit the entity form.
|
Chris@0
|
428 *
|
Chris@0
|
429 * @param \Drupal\Core\Entity\EntityInterface $entity
|
Chris@0
|
430 * The entity being tested.
|
Chris@0
|
431 * @param string $langcode
|
Chris@0
|
432 * Language code for the form.
|
Chris@0
|
433 *
|
Chris@0
|
434 * @return string
|
Chris@0
|
435 * Name of the button to hit.
|
Chris@0
|
436 */
|
Chris@0
|
437 protected function getFormSubmitAction(EntityInterface $entity, $langcode) {
|
Chris@0
|
438 return t('Save') . $this->getFormSubmitSuffix($entity, $langcode);
|
Chris@0
|
439 }
|
Chris@0
|
440
|
Chris@0
|
441 /**
|
Chris@0
|
442 * Returns appropriate submit button suffix based on translatability.
|
Chris@0
|
443 *
|
Chris@0
|
444 * @param \Drupal\Core\Entity\EntityInterface $entity
|
Chris@0
|
445 * The entity being tested.
|
Chris@0
|
446 * @param string $langcode
|
Chris@0
|
447 * Language code for the form.
|
Chris@0
|
448 *
|
Chris@0
|
449 * @return string
|
Chris@0
|
450 * Submit button suffix based on translatability.
|
Chris@0
|
451 */
|
Chris@0
|
452 protected function getFormSubmitSuffix(EntityInterface $entity, $langcode) {
|
Chris@0
|
453 return '';
|
Chris@0
|
454 }
|
Chris@0
|
455
|
Chris@0
|
456 /**
|
Chris@0
|
457 * Returns the translation object to use to retrieve the translated values.
|
Chris@0
|
458 *
|
Chris@0
|
459 * @param \Drupal\Core\Entity\EntityInterface $entity
|
Chris@0
|
460 * The entity being tested.
|
Chris@0
|
461 * @param string $langcode
|
Chris@0
|
462 * The language code identifying the translation to be retrieved.
|
Chris@0
|
463 *
|
Chris@0
|
464 * @return \Drupal\Core\TypedData\TranslatableInterface
|
Chris@0
|
465 * The translation object to act on.
|
Chris@0
|
466 */
|
Chris@0
|
467 protected function getTranslation(EntityInterface $entity, $langcode) {
|
Chris@0
|
468 return $entity->getTranslation($langcode);
|
Chris@0
|
469 }
|
Chris@0
|
470
|
Chris@0
|
471 /**
|
Chris@0
|
472 * Returns the value for the specified property in the given language.
|
Chris@0
|
473 *
|
Chris@0
|
474 * @param \Drupal\Core\Entity\EntityInterface $translation
|
Chris@0
|
475 * The translation object the property value should be retrieved from.
|
Chris@0
|
476 * @param string $property
|
Chris@0
|
477 * The property name.
|
Chris@0
|
478 * @param string $langcode
|
Chris@0
|
479 * The property value.
|
Chris@0
|
480 *
|
Chris@0
|
481 * @return
|
Chris@0
|
482 * The property value.
|
Chris@0
|
483 */
|
Chris@0
|
484 protected function getValue(EntityInterface $translation, $property, $langcode) {
|
Chris@0
|
485 $key = $property == 'user_id' ? 'target_id' : 'value';
|
Chris@0
|
486 return $translation->get($property)->{$key};
|
Chris@0
|
487 }
|
Chris@0
|
488
|
Chris@0
|
489 /**
|
Chris@0
|
490 * Returns the name of the field that implements the changed timestamp.
|
Chris@0
|
491 *
|
Chris@0
|
492 * @param \Drupal\Core\Entity\EntityInterface $entity
|
Chris@0
|
493 * The entity being tested.
|
Chris@0
|
494 *
|
Chris@0
|
495 * @return string
|
Chris@0
|
496 * The field name.
|
Chris@0
|
497 */
|
Chris@0
|
498 protected function getChangedFieldName($entity) {
|
Chris@0
|
499 return $entity->hasField('content_translation_changed') ? 'content_translation_changed' : 'changed';
|
Chris@0
|
500 }
|
Chris@0
|
501
|
Chris@0
|
502 /**
|
Chris@0
|
503 * Tests edit content translation.
|
Chris@0
|
504 */
|
Chris@0
|
505 protected function doTestTranslationEdit() {
|
Chris@0
|
506 $storage = $this->container->get('entity_type.manager')
|
Chris@0
|
507 ->getStorage($this->entityTypeId);
|
Chris@0
|
508 $storage->resetCache([$this->entityId]);
|
Chris@0
|
509 $entity = $storage->load($this->entityId);
|
Chris@0
|
510 $languages = $this->container->get('language_manager')->getLanguages();
|
Chris@0
|
511
|
Chris@0
|
512 foreach ($this->langcodes as $langcode) {
|
Chris@0
|
513 // We only want to test the title for non-english translations.
|
Chris@0
|
514 if ($langcode != 'en') {
|
Chris@0
|
515 $options = ['language' => $languages[$langcode]];
|
Chris@0
|
516 $url = $entity->urlInfo('edit-form', $options);
|
Chris@0
|
517 $this->drupalGet($url);
|
Chris@0
|
518
|
Chris@0
|
519 $this->assertRaw($entity->getTranslation($langcode)->label());
|
Chris@0
|
520 }
|
Chris@0
|
521 }
|
Chris@0
|
522 }
|
Chris@0
|
523
|
Chris@0
|
524 /**
|
Chris@0
|
525 * Tests the basic translation workflow.
|
Chris@0
|
526 */
|
Chris@0
|
527 protected function doTestTranslationChanged() {
|
Chris@0
|
528 $storage = $this->container->get('entity_type.manager')
|
Chris@0
|
529 ->getStorage($this->entityTypeId);
|
Chris@0
|
530 $storage->resetCache([$this->entityId]);
|
Chris@0
|
531 $entity = $storage->load($this->entityId);
|
Chris@0
|
532 $changed_field_name = $this->getChangedFieldName($entity);
|
Chris@0
|
533 $definition = $entity->getFieldDefinition($changed_field_name);
|
Chris@0
|
534 $config = $definition->getConfig($entity->bundle());
|
Chris@0
|
535
|
Chris@0
|
536 foreach ([FALSE, TRUE] as $translatable_changed_field) {
|
Chris@0
|
537 if ($definition->isTranslatable()) {
|
Chris@0
|
538 // For entities defining a translatable changed field we want to test
|
Chris@0
|
539 // the correct behavior of that field even if the translatability is
|
Chris@0
|
540 // revoked. In that case the changed timestamp should be synchronized
|
Chris@0
|
541 // across all translations.
|
Chris@0
|
542 $config->setTranslatable($translatable_changed_field);
|
Chris@0
|
543 $config->save();
|
Chris@0
|
544 }
|
Chris@0
|
545 elseif ($translatable_changed_field) {
|
Chris@0
|
546 // For entities defining a non-translatable changed field we cannot
|
Chris@0
|
547 // declare the field as translatable on the fly by modifying its config
|
Chris@0
|
548 // because the schema doesn't support this.
|
Chris@0
|
549 break;
|
Chris@0
|
550 }
|
Chris@0
|
551
|
Chris@0
|
552 foreach ($entity->getTranslationLanguages() as $language) {
|
Chris@0
|
553 // Ensure different timestamps.
|
Chris@0
|
554 sleep(1);
|
Chris@0
|
555
|
Chris@0
|
556 $langcode = $language->getId();
|
Chris@0
|
557
|
Chris@0
|
558 $edit = [
|
Chris@0
|
559 $this->fieldName . '[0][value]' => $this->randomString(),
|
Chris@0
|
560 ];
|
Chris@0
|
561 $edit_path = $entity->urlInfo('edit-form', ['language' => $language]);
|
Chris@0
|
562 $this->drupalPostForm($edit_path, $edit, $this->getFormSubmitAction($entity, $langcode));
|
Chris@0
|
563
|
Chris@0
|
564 $storage = $this->container->get('entity_type.manager')
|
Chris@0
|
565 ->getStorage($this->entityTypeId);
|
Chris@0
|
566 $storage->resetCache([$this->entityId]);
|
Chris@0
|
567 $entity = $storage->load($this->entityId);
|
Chris@0
|
568 $this->assertEqual(
|
Chris@0
|
569 $entity->getChangedTimeAcrossTranslations(), $entity->getTranslation($langcode)->getChangedTime(),
|
Chris@0
|
570 format_string('Changed time for language %language is the latest change over all languages.', ['%language' => $language->getName()])
|
Chris@0
|
571 );
|
Chris@0
|
572 }
|
Chris@0
|
573
|
Chris@0
|
574 $timestamps = [];
|
Chris@0
|
575 foreach ($entity->getTranslationLanguages() as $language) {
|
Chris@0
|
576 $next_timestamp = $entity->getTranslation($language->getId())->getChangedTime();
|
Chris@0
|
577 if (!in_array($next_timestamp, $timestamps)) {
|
Chris@0
|
578 $timestamps[] = $next_timestamp;
|
Chris@0
|
579 }
|
Chris@0
|
580 }
|
Chris@0
|
581
|
Chris@0
|
582 if ($translatable_changed_field) {
|
Chris@0
|
583 $this->assertEqual(
|
Chris@0
|
584 count($timestamps), count($entity->getTranslationLanguages()),
|
Chris@0
|
585 'All timestamps from all languages are different.'
|
Chris@0
|
586 );
|
Chris@0
|
587 }
|
Chris@0
|
588 else {
|
Chris@0
|
589 $this->assertEqual(
|
Chris@0
|
590 count($timestamps), 1,
|
Chris@0
|
591 'All timestamps from all languages are identical.'
|
Chris@0
|
592 );
|
Chris@0
|
593 }
|
Chris@0
|
594 }
|
Chris@0
|
595 }
|
Chris@0
|
596
|
Chris@0
|
597 /**
|
Chris@0
|
598 * Test the changed time after API and FORM save without changes.
|
Chris@0
|
599 */
|
Chris@0
|
600 public function doTestChangedTimeAfterSaveWithoutChanges() {
|
Chris@0
|
601 $storage = $this->container->get('entity_type.manager')
|
Chris@0
|
602 ->getStorage($this->entityTypeId);
|
Chris@0
|
603 $storage->resetCache([$this->entityId]);
|
Chris@0
|
604 $entity = $storage->load($this->entityId);
|
Chris@0
|
605 // Test only entities, which implement the EntityChangedInterface.
|
Chris@0
|
606 if ($entity instanceof EntityChangedInterface) {
|
Chris@0
|
607 $changed_timestamp = $entity->getChangedTime();
|
Chris@0
|
608
|
Chris@0
|
609 $entity->save();
|
Chris@0
|
610 $storage = $this->container->get('entity_type.manager')
|
Chris@0
|
611 ->getStorage($this->entityTypeId);
|
Chris@0
|
612 $storage->resetCache([$this->entityId]);
|
Chris@0
|
613 $entity = $storage->load($this->entityId);
|
Chris@0
|
614 $this->assertEqual($changed_timestamp, $entity->getChangedTime(), 'The entity\'s changed time wasn\'t updated after API save without changes.');
|
Chris@0
|
615
|
Chris@0
|
616 // Ensure different save timestamps.
|
Chris@0
|
617 sleep(1);
|
Chris@0
|
618
|
Chris@0
|
619 // Save the entity on the regular edit form.
|
Chris@0
|
620 $language = $entity->language();
|
Chris@0
|
621 $edit_path = $entity->urlInfo('edit-form', ['language' => $language]);
|
Chris@0
|
622 $this->drupalPostForm($edit_path, [], $this->getFormSubmitAction($entity, $language->getId()));
|
Chris@0
|
623
|
Chris@0
|
624 $storage->resetCache([$this->entityId]);
|
Chris@0
|
625 $entity = $storage->load($this->entityId);
|
Chris@0
|
626 $this->assertNotEqual($changed_timestamp, $entity->getChangedTime(), 'The entity\'s changed time was updated after form save without changes.');
|
Chris@0
|
627 }
|
Chris@0
|
628 }
|
Chris@0
|
629
|
Chris@0
|
630 }
|