3 namespace Drupal\paragraphs\Tests\Classic;
5 use Drupal\Component\Render\FormattableMarkup;
6 use Drupal\Core\Entity\Entity\EntityFormDisplay;
7 use Drupal\Core\Language\LanguageInterface;
8 use Drupal\paragraphs\Entity\Paragraph;
9 use Drupal\node\Entity\Node;
10 use Drupal\user\Entity\Role;
13 * Tests the configuration of paragraphs.
17 class ParagraphsTranslationTest extends ParagraphsTestBase {
24 public static $modules = array(
26 'content_translation',
31 * A user with admin permissions.
35 protected $admin_user;
40 protected function setUp() {
43 'administer site configuration',
44 'create paragraphed_content_demo content',
45 'edit any paragraphed_content_demo content',
46 'delete any paragraphed_content_demo content',
47 'administer content translation',
48 'translate any entity',
49 'create content translations',
50 'administer languages',
53 'settings[paragraph][nested_paragraph][translatable]' => TRUE,
54 'settings[paragraph][nested_paragraph][settings][language][language_alterable]' => TRUE,
55 'settings[paragraph][images][fields][field_images_demo]' => TRUE,
57 $this->drupalPostForm('admin/config/regional/content-language', $edit, t('Save configuration'));
59 // Set the form display to classic.
60 EntityFormDisplay::load('node.paragraphed_content_demo.default')
61 ->setComponent('field_paragraphs_demo', ['type' => 'entity_reference_paragraphs'])
63 EntityFormDisplay::load('paragraph.nested_paragraph.default')
64 ->setComponent('field_paragraphs_demo', ['type' => 'entity_reference_paragraphs'])
67 if (version_compare(\Drupal::VERSION, '8.4', '>=')) {
68 // @todo Workaround for file usage/unable to save the node with no usages.
69 // Remove when https://www.drupal.org/node/2801777 is fixed.
70 \Drupal::configFactory()->getEditable('file.settings')
71 ->set('make_unused_managed_files_temporary', TRUE)
77 * Tests the paragraph translation.
79 public function testParagraphTranslation() {
80 // We need to add a permission to administer roles to deal with revisions.
81 $roles = $this->loggedInUser->getRoles();
82 $this->grantPermissions(Role::load(array_shift($roles)), ['administer nodes']);
83 $this->drupalGet('admin/config/regional/content-language');
85 // Check the settings are saved correctly.
86 $this->assertFieldChecked('edit-entity-types-paragraph');
87 $this->assertFieldChecked('edit-settings-node-paragraphed-content-demo-translatable');
88 $this->assertFieldChecked('edit-settings-paragraph-text-image-translatable');
89 $this->assertFieldChecked('edit-settings-paragraph-images-columns-field-images-demo-alt');
90 $this->assertFieldChecked('edit-settings-paragraph-images-columns-field-images-demo-title');
92 // Check if the publish/unpublish option works.
93 $this->drupalGet('admin/structure/paragraphs_type/text_image/form-display');
95 'fields[status][type]' => 'boolean_checkbox',
96 'fields[status][region]' => 'content',
99 $this->drupalPostForm(NULL, $edit, t('Save'));
100 $this->drupalGet('node/add/paragraphed_content_demo');
101 $this->drupalPostForm(NULL, NULL, t('Add Text + Image'));
102 $this->assertRaw('edit-field-paragraphs-demo-0-subform-status-value');
104 'title[0][value]' => 'example_publish_unpublish',
105 'field_paragraphs_demo[0][subform][field_text_demo][0][value]' => 'Example published and unpublished',
107 $this->drupalPostForm(NULL, $edit, t('Save'));
108 $this->assertText(t('Example published and unpublished'));
109 $this->clickLink(t('Edit'));
111 $this->drupalPostAjaxForm(NULL, NULL, 'field_paragraphs_demo_nested_paragraph_add_more');
112 $this->drupalPostAjaxForm(NULL, NULL, 'field_paragraphs_demo_1_subform_field_paragraphs_demo_text_add_more');
114 'field_paragraphs_demo[0][subform][status][value]' => FALSE,
115 'field_paragraphs_demo[1][subform][field_paragraphs_demo][0][subform][field_text_demo][0][value]' => 'Dummy text'
117 $this->drupalPostForm(NULL, $edit + ['status[value]' => FALSE], t('Save'));
118 $this->assertNoText(t('Example published and unpublished'));
120 // Check the parent fields are set properly. Get the node.
121 $node = $this->drupalGetNodeByTitle('example_publish_unpublish');
122 // Loop over the paragraphs of the node.
123 foreach ($node->field_paragraphs_demo->referencedEntities() as $paragraph) {
124 $node_paragraph = Paragraph::load($paragraph->id())->toArray();
125 // Check if the fields are set properly.
126 $this->assertEqual($node_paragraph['parent_id'][0]['value'], $node->id());
127 $this->assertEqual($node_paragraph['parent_type'][0]['value'], 'node');
128 $this->assertEqual($node_paragraph['parent_field_name'][0]['value'], 'field_paragraphs_demo');
129 // If the paragraph is nested type load the child.
130 if ($node_paragraph['type'][0]['target_id'] == 'nested_paragraph') {
131 $nested_paragraph = Paragraph::load($node_paragraph['field_paragraphs_demo'][0]['target_id'])->toArray();
132 // Check if the fields are properly set.
133 $this->assertEqual($nested_paragraph['parent_id'][0]['value'], $paragraph->id());
134 $this->assertEqual($nested_paragraph['parent_type'][0]['value'], 'paragraph');
135 $this->assertEqual($nested_paragraph['parent_field_name'][0]['value'], 'field_paragraphs_demo');
139 // Add paragraphed content.
140 $this->drupalGet('node/add/paragraphed_content_demo');
141 $this->drupalPostForm(NULL, NULL, t('Add Text + Image'));
143 'title[0][value]' => 'Title in english',
144 'field_paragraphs_demo[0][subform][field_text_demo][0][value]' => 'Text in english',
146 // The button to remove a paragraph is present.
147 $this->assertRaw(t('Remove'));
148 $this->drupalPostForm(NULL, $edit, t('Save'));
149 $node = $this->drupalGetNodeByTitle('Title in english');
150 // The text is present when editing again.
151 $this->clickLink(t('Edit'));
152 $this->assertText('Title in english');
153 $this->assertText('Text in english');
155 // Add french translation.
156 $this->clickLink(t('Translate'));
157 $this->clickLink(t('Add'), 1);
158 // Make sure the Add / Remove paragraph buttons are hidden.
159 $this->assertNoRaw(t('Remove'));
160 $this->assertNoRaw(t('Add Text + Image'));
161 // Make sure that the original paragraph text is displayed.
162 $this->assertText('Text in english');
165 'title[0][value]' => 'Title in french',
166 'field_paragraphs_demo[0][subform][field_text_demo][0][value]' => 'Text in french',
168 'revision_log[0][value]' => 'french 1',
170 $this->drupalPostForm(NULL, $edit, t('Save (this translation)'));
171 $this->assertText('Paragraphed article Title in french has been updated.');
173 // Check the english translation.
174 $this->drupalGet('node/' . $node->id());
175 $this->assertText('Title in english');
176 $this->assertText('Text in english');
177 $this->assertNoText('Title in french');
178 $this->assertNoText('Text in french');
180 // Check the french translation.
181 $this->drupalGet('fr/node/' . $node->id());
182 $this->assertText('Title in french');
183 $this->assertText('Text in french');
184 $this->assertNoText('Title in english');
185 // The translation is still present when editing again.
186 $this->clickLink(t('Edit'));
187 $this->assertText('Title in french');
188 $this->assertText('Text in french');
190 'title[0][value]' => 'Title Change in french',
191 'field_paragraphs_demo[0][subform][field_text_demo][0][value]' => 'New text in french',
193 'revision_log[0][value]' => 'french 2',
195 $this->drupalPostForm(NULL, $edit, t('Save (this translation)'));
196 $this->assertText('Title Change in french');
197 $this->assertText('New text in french');
199 // Back to the source language.
200 $this->drupalGet('node/' . $node->id());
201 $this->clickLink(t('Edit'));
202 $this->assertText('Title in english');
203 $this->assertText('Text in english');
204 // Save the original content on second request.
205 $this->drupalPostForm(NULL, NULL, t('Save (this translation)'));
206 $this->assertText('Paragraphed article Title in english has been updated.');
208 // Test if reverting to old paragraphs revisions works, make sure that
209 // the reverted node can be saved again.
210 $this->drupalGet('fr/node/' . $node->id() . '/revisions');
211 $this->clickLink(t('Revert'));
212 $this->drupalPostForm(NULL, ['revert_untranslated_fields' => TRUE], t('Revert'));
213 $this->clickLink(t('Edit'));
214 $this->assertRaw('Title in french');
215 $this->assertText('Text in french');
216 $this->drupalPostForm(NULL, [], t('Save (this translation)'));
217 $this->assertNoRaw('The content has either been modified by another user, or you have already submitted modifications');
218 $this->assertText('Text in french');
220 //Add paragraphed content with untranslatable language
221 $this->drupalGet('node/add/paragraphed_content_demo');
222 $edit = array('langcode[0][value]' => LanguageInterface::LANGCODE_NOT_SPECIFIED);
223 $this->drupalPostForm(NULL, $edit, t('Add Text + Image'));
224 $this->assertResponse(200);
226 // Make 'Images' paragraph field translatable, enable alt and title fields.
227 $this->drupalGet('admin/structure/paragraphs_type/images/fields');
228 $this->clickLink('Edit');
231 'settings[alt_field]' => 1,
232 'settings[title_field]' => 1,
234 $this->drupalPostForm(NULL, $edit, t('Save settings'));
236 // Create a node with an image paragraph, its alt and title text.
237 $text = 'Trust me I\'m an image';
238 file_put_contents('temporary://Image.jpg', $text);
239 $file_path = $this->container->get('file_system')->realpath('temporary://Image.jpg');
240 $this->drupalGet('node/add/paragraphed_content_demo');
241 $this->drupalPostForm(NULL, [], t('Add Images'));
242 $this->drupalPostForm(NULL, ['files[field_paragraphs_demo_0_subform_field_images_demo_0][]' => $file_path], t('Upload'));
244 'title[0][value]' => 'Title EN',
245 'field_paragraphs_demo[0][subform][field_images_demo][0][alt]' => 'Image alt',
246 'field_paragraphs_demo[0][subform][field_images_demo][0][title]' => 'Image title',
247 'field_paragraphs_demo[0][subform][field_images_demo][0][width]' => 100,
248 'field_paragraphs_demo[0][subform][field_images_demo][0][height]' => 100,
250 $this->drupalPostForm(NULL, $edit, t('Save'));
252 // Translate the node with the image paragraph.
253 $this->clickLink('Translate');
254 $this->clickLink(t('Add'), 1);
256 'title[0][value]' => 'Title FR',
257 'field_paragraphs_demo[0][subform][field_images_demo][0][alt]' => 'Image alt FR',
258 'field_paragraphs_demo[0][subform][field_images_demo][0][title]' => 'Image title FR',
260 $this->drupalPostForm(NULL, $edit, t('Save (this translation)'));
261 $this->assertRaw('Title FR');
263 $this->drupalGet('node/add/paragraphed_content_demo');
264 $this->drupalPostForm(NULL, [], t('Add Text'));
266 'field_paragraphs_demo[0][subform][field_text_demo][0][value]' => 'texto',
267 'title[0][value]' => 'titulo',
268 'langcode[0][value]' => 'de',
270 $this->drupalPostForm(NULL, $edit, t('Save'));
271 $node = $this->drupalGetNodeByTitle('titulo');
272 $this->assertParagraphsLangcode($node->id(), 'de');
274 // Test langcode matching when Paragraphs and node have different language.
275 $paragraph_1 = Paragraph::create([
276 'title' => 'Paragraph',
279 'field_text_demo' => 'english_text_1',
281 $paragraph_1->save();
283 $paragraph_2 = Paragraph::create([
284 'title' => 'Paragraph',
287 'field_text_demo' => 'english_text_2',
289 $paragraph_2->save();
291 $paragraph_data = $paragraph_2->toArray();
292 $paragraph_data['field_text_demo'] = 'german_text_2';
293 $paragraph_2->addTranslation('de', $paragraph_data);
294 $paragraph_2->save();
295 $translated_paragraph = $paragraph_2->getTranslation('en');
297 $node = $this->createNode([
299 'type' => 'paragraphed_content_demo',
300 'field_paragraphs_demo' => [$paragraph_1, $translated_paragraph],
302 $this->drupalGet('node/' . $node->id() . '/edit');
303 $this->drupalPostForm(NULL, [], t('Save'));
304 $this->assertText('Paragraphed article ' . $node->label() . ' has been updated.');
305 // Check that first paragraph langcode has been updated.
306 $paragraph = Paragraph::load($paragraph_1->id());
307 $this->assertEqual($paragraph->language()->getId(), 'de');
308 $this->assertFalse($paragraph->hasTranslation('en'));
309 // Check that second paragraph has two translations.
310 $paragraph = Paragraph::load($paragraph_2->id());
311 $this->assertTrue($paragraph->hasTranslation('de'));
312 $this->assertTrue($paragraph->hasTranslation('en'));
313 $this->assertRaw('german_text');
315 // Create an english translation of the node.
317 'field_paragraphs_demo[0][subform][field_text_demo][0][value]' => 'english_translation_1',
318 'field_paragraphs_demo[1][subform][field_text_demo][0][value]' => 'english_translation_2',
320 $this->drupalPostForm('node/' . $node->id() . '/translations/add/de/en', $edit, t('Save (this translation)'));
321 // Attempt to create a french translation.
322 $this->drupalGet('node/' . $node->id() . '/translations/add/de/fr');
323 // Check that the german translation of the paragraphs is displayed.
324 $this->assertFieldByName('field_paragraphs_demo[0][subform][field_text_demo][0][value]', 'english_text_1');
325 $this->assertFieldByName('field_paragraphs_demo[1][subform][field_text_demo][0][value]', 'german_text_2');
326 $this->drupalPostForm(NULL, ['source_langcode[source]' => 'en'], t('Change'));
327 // Check that the english translation of the paragraphs is displayed.
328 $this->assertFieldByName('field_paragraphs_demo[0][subform][field_text_demo][0][value]', 'english_translation_1');
329 $this->assertFieldByName('field_paragraphs_demo[1][subform][field_text_demo][0][value]', 'english_translation_2');
331 // Create a node with empty Paragraphs.
332 $this->drupalGet('node/add/paragraphed_content_demo');
333 $this->drupalPostForm(NULL, [], t('Add Nested Paragraph'));
334 $edit = ['title[0][value]' => 'empty_node'];
335 $this->drupalPostForm(NULL, $edit, t('Save'));
336 // Attempt to translate it.
337 $this->clickLink(t('Translate'));
338 $this->clickLink(t('Add'));
339 // Check the add button is not displayed.
340 $this->assertEqual(count($this->xpath('//*[@name="field_paragraphs_demo_0_subform_field_paragraphs_demo_images_add_more"]')), 0);
342 // Add a non translatable field to Text Paragraph type.
344 'new_storage_type' => 'text_long',
345 'label' => 'untranslatable_field',
346 'field_name' => 'untranslatable_field',
348 $this->drupalPostForm('admin/structure/paragraphs_type/text/fields/add-field', $edit, t('Save and continue'));
349 $this->drupalPostForm(NULL, [], t('Save field settings'));
350 $this->drupalPostForm(NULL, [], t('Save settings'));
352 // Add a non translatable reference field.
354 'new_storage_type' => 'field_ui:entity_reference:node',
355 'label' => 'untranslatable_ref_field',
356 'field_name' => 'untranslatable_ref_field',
358 $this->drupalPostForm('admin/structure/paragraphs_type/text/fields/add-field', $edit, t('Save and continue'));
359 $this->drupalPostForm(NULL, [], t('Save field settings'));
360 $this->drupalPostForm(NULL, ['settings[handler_settings][target_bundles][paragraphed_content_demo]' => TRUE], t('Save settings'));
362 // Add a non translatable link field.
364 'new_storage_type' => 'link',
365 'label' => 'untranslatable_link_field',
366 'field_name' => 'untranslatable_link_field',
368 $this->drupalPostForm('admin/structure/paragraphs_type/text/fields/add-field', $edit, t('Save and continue'));
369 $this->drupalPostForm(NULL, [], t('Save field settings'));
370 $this->drupalPostForm(NULL, [], t('Save settings'));
372 // Attempt to add a translation.
373 $this->drupalGet('node/' . $node->id() . '/translations/add/de/fr');
374 $this->assertText('untranslatable_field (all languages)');
375 $this->assertText('untranslatable_ref_field (all languages)');
376 $this->assertText('untranslatable_link_field (all languages)');
377 $this->assertNoText('Text (all languages)');
379 // Enable translations for the reference and link field.
381 'translatable' => TRUE,
383 $this->drupalPostForm('admin/structure/paragraphs_type/text/fields/paragraph.text.field_untranslatable_ref_field', $edit, t('Save settings'));
384 $this->drupalPostForm('admin/structure/paragraphs_type/text/fields/paragraph.text.field_untranslatable_link_field', $edit, t('Save settings'));
386 // Attempt to add a translation.
387 $this->drupalGet('node/' . $node->id() . '/translations/add/de/fr');
388 $this->assertText('untranslatable_field (all languages)');
389 $this->assertNoText('untranslatable_link_field (all languages)');
390 $this->assertNoText('untranslatable_ref_field (all languages)');
391 $this->assertNoText('Text (all languages)');
395 * Tests the paragraph buttons presence in translation multilingual workflow.
397 * This test covers the following test cases:
398 * 1) original node langcode in EN, translate in FR, change to DE.
399 * 2) original node langcode in DE, change site langcode to DE, change node
402 public function testParagraphTranslationMultilingual() {
403 // Case 1: original node langcode in EN, translate in FR, change to DE.
405 // Add 'Images' paragraph and check the paragraphs buttons are displayed.
406 $this->drupalGet('node/add/paragraphed_content_demo');
407 $this->drupalPostForm(NULL, NULL, t('Add Images'));
408 $this->assertParagraphsButtons(1);
409 // Upload an image and check the paragraphs buttons are still displayed.
410 $images = $this->drupalGetTestFiles('image')[0];
412 'title[0][value]' => 'Title in english',
413 'files[field_paragraphs_demo_0_subform_field_images_demo_0][]' => $images->uri,
415 $this->drupalPostForm(NULL, $edit, t('Upload'));
416 $this->assertParagraphsButtons(1);
417 $this->drupalPostForm(NULL, NULL, t('Save'));
418 $this->assertText('Title in english');
419 $node = $this->drupalGetNodeByTitle('Title in english');
420 // Check the paragraph langcode is 'en'.
421 $this->assertParagraphsLangcode($node->id());
423 // Add french translation.
424 $this->clickLink(t('Translate'));
425 $this->clickLink(t('Add'), 1);
426 // Make sure the host entity and its paragraphs have valid source language
427 // and check that the paragraphs buttons are hidden.
428 $this->assertNoParagraphsButtons(1);
430 'title[0][value]' => 'Title in french',
432 $this->drupalPostForm(NULL, $edit, t('Save (this translation)'));
433 $this->assertParagraphsLangcode($node->id(), 'en', 'fr');
434 $this->assertText('Paragraphed article Title in french has been updated.');
435 $this->assertText('Title in french');
436 $this->assertNoText('Title in english');
437 // Check the original node and the paragraph langcode is still 'en'.
438 $this->assertParagraphsLangcode($node->id());
440 // Edit the french translation and upload a new image.
441 $this->clickLink('Edit');
442 $images = $this->drupalGetTestFiles('image')[1];
443 $this->drupalPostForm(NULL, [
444 'files[field_paragraphs_demo_0_subform_field_images_demo_1][]' => $images->uri,
446 // Check editing a translation does not affect the source langcode and
447 // check that the paragraphs buttons are still hidden.
448 $this->assertParagraphsLangcode($node->id(), 'en', 'fr');
449 $this->assertNoParagraphsButtons(1);
450 $this->drupalPostForm(NULL, NULL, t('Save (this translation)'));
451 $this->assertText('Title in french');
452 $this->assertNoText('Title in english');
454 // Back to the original node.
455 $this->drupalGet('node/' . $node->id());
456 $this->assertText('Title in english');
457 $this->assertNoText('Title in french');
458 // Check the original node and the paragraph langcode are still 'en' and
459 // check that the paragraphs buttons are still displayed.
460 $this->clickLink('Edit');
461 $this->assertParagraphsLangcode($node->id());
462 $this->assertParagraphsButtons(1);
463 // Change the node langcode to 'german', add a 'Nested Paragraph', check
464 // the paragraphs langcode are still 'en' and their buttons are displayed.
466 'title[0][value]' => 'Title in english (de)',
467 'langcode[0][value]' => 'de',
469 $this->drupalPostForm(NULL, $edit, t('Add Nested Paragraph'));
470 $this->assertParagraphsLangcode($node->id());
471 $this->assertParagraphsButtons(2);
472 // Add an 'Images' paragraph inside the nested one, check the paragraphs
473 // langcode are still 'en' and the paragraphs buttons are still displayed.
474 $this->drupalPostAjaxForm(NULL, NULL, 'field_paragraphs_demo_1_subform_field_paragraphs_demo_images_add_more');
475 $this->assertParagraphsLangcode($node->id());
476 $this->assertParagraphsButtons(2);
477 // Upload a new image, check the paragraphs langcode are still 'en' and the
478 // paragraphs buttons are displayed.
479 $images = $this->drupalGetTestFiles('image')[2];
480 $this->drupalPostForm(NULL, [
481 'files[field_paragraphs_demo_1_subform_field_paragraphs_demo_0_subform_field_images_demo_0][]' => $images->uri,
483 $this->assertParagraphsLangcode($node->id());
484 $this->assertParagraphsButtons(2);
485 $this->drupalPostForm(NULL, NULL, t('Save (this translation)'));
486 $this->assertText('Title in english (de)');
487 $this->assertNoText('Title in french');
488 // Check the original node and the paragraphs langcode are now 'de'.
489 $this->assertParagraphsLangcode($node->id(), 'de');
491 // Check the french translation.
492 $this->drupalGet('fr/node/' . $node->id());
493 $this->assertText('Title in french');
494 $this->assertNoText('Title in english (de)');
495 // Check editing a translation does not affect the source langcode and
496 // check that the paragraphs buttons are still hidden.
497 $this->clickLink('Edit');
498 $this->assertParagraphsLangcode($node->id(), 'de', 'fr');
499 $this->assertNoParagraphsButtons(2);
501 // Case 2: original node langcode in DE, change site langcode to DE, change
502 // node langcode to EN.
504 // Change the site langcode to french.
505 $this->drupalPostForm('admin/config/regional/language', [
506 'site_default_language' => 'fr',
507 ], t('Save configuration'));
509 // Check the original node and its paragraphs langcode are still 'de'
510 // and the paragraphs buttons are still displayed.
511 $this->drupalGet('node/' . $node->id() . '/edit');
512 $this->assertParagraphsLangcode($node->id(), 'de');
513 $this->assertParagraphsButtons(2);
515 // Go to the french translation.
516 $this->drupalGet('node/' . $node->id() . '/translations');
517 $this->clickLink(t('Edit'), 1);
518 // Check editing a translation does not affect the source langcode and
519 // check that the paragraphs buttons are still hidden.
520 $this->assertParagraphsLangcode($node->id(), 'de', 'fr');
521 $this->assertNoParagraphsButtons(2);
522 // Upload another image.
523 $images = $this->drupalGetTestFiles('image')[3];
524 $this->drupalPostForm(NULL, [
525 'files[field_paragraphs_demo_1_subform_field_paragraphs_demo_0_subform_field_images_demo_1][]' => $images->uri,
527 // Check editing a translation does not affect the source langcode and
528 // check that the paragraphs buttons are still hidden.
529 $this->assertParagraphsLangcode($node->id(), 'de', 'fr');
530 $this->assertNoParagraphsButtons(2);
531 $this->drupalPostForm(NULL, NULL, t('Save (this translation)'));
532 // Check the paragraphs langcode are still 'de' after saving the translation.
533 $this->assertParagraphsLangcode($node->id(), 'de', 'fr');
534 $this->assertText('Title in french');
535 $this->assertNoText('Title in english (de)');
537 // Back to the original node.
538 $this->drupalGet('node/' . $node->id());
539 $this->assertText('Title in english (de)');
540 $this->assertNoText('Title in french');
541 // Check the original node and the paragraphs langcode are still 'de' and
542 // check that the paragraphs buttons are still displayed.
543 $this->clickLink('Edit');
544 $this->assertParagraphsLangcode($node->id(), 'de');
545 $this->assertParagraphsButtons(2);
546 // Change the node langcode back to 'english', add an 'Images' paragraph,
547 // check the paragraphs langcode are still 'de' and their buttons are shown.
549 'title[0][value]' => 'Title in english',
550 'langcode[0][value]' => 'en',
552 $this->drupalPostForm(NULL, $edit, t('Add Images'));
553 $this->assertParagraphsLangcode($node->id(), 'de');
554 $this->assertParagraphsButtons(3);
555 // Upload a new image, check the paragraphs langcode are still 'de' and the
556 // paragraphs buttons are displayed.
557 $images = $this->drupalGetTestFiles('image')[4];
558 $this->drupalPostForm(NULL, [
559 'files[field_paragraphs_demo_2_subform_field_images_demo_0][]' => $images->uri,
561 $this->assertParagraphsLangcode($node->id(), 'de');
562 $this->assertParagraphsButtons(3);
563 $this->drupalPostForm(NULL, NULL, t('Save (this translation)'));
564 $this->assertText('Paragraphed article Title in english has been updated.');
565 // Check the original node and the paragraphs langcode are now 'en'.
566 $this->assertParagraphsLangcode($node->id());
570 * Tests the paragraphs buttons presence in multilingual workflow.
572 * This test covers the following test cases:
573 * 1) original node langcode in german, change to english.
574 * 2) original node langcode in english, change to german.
575 * 3) original node langcode in english, change site langcode to german,
576 * change node langcode to german.
578 public function testParagraphsMultilingualWorkflow() {
579 // Case 1: Check the paragraphs buttons after changing the NODE language
580 // (original node langcode in GERMAN, default site langcode in english).
582 // Create a node and check that the node langcode is 'english'.
583 $this->drupalGet('node/add/paragraphed_content_demo');
584 $this->assertOptionSelected('edit-langcode-0-value', 'en');
585 // Change the node langcode to 'german' and add a 'Nested Paragraph'.
587 'title[0][value]' => 'Title in german',
588 'langcode[0][value]' => 'de',
590 $this->drupalPostForm(NULL, $edit, t('Add Nested Paragraph'));
591 // Check that the paragraphs buttons are displayed and add an 'Images'
592 // paragraph inside the nested paragraph.
593 $this->assertParagraphsButtons(1);
594 $this->drupalPostAjaxForm(NULL, NULL, 'field_paragraphs_demo_0_subform_field_paragraphs_demo_images_add_more');
595 // Upload an image and check the paragraphs buttons are still displayed.
596 $images = $this->drupalGetTestFiles('image')[0];
597 $this->drupalPostForm(NULL, [
598 'files[field_paragraphs_demo_0_subform_field_paragraphs_demo_0_subform_field_images_demo_0][]' => $images->uri,
600 $this->assertParagraphsButtons(1);
601 $this->drupalPostForm(NULL, NULL, t('Save'));
602 $this->assertText('Title in german');
603 $node1 = $this->getNodeByTitle('Title in german');
605 // Check the paragraph langcode is 'de' and its buttons are displayed.
606 // @todo check for the nested children paragraphs buttons and langcode
607 // when it's supported.
608 $this->clickLink(t('Edit'));
609 $this->assertParagraphsLangcode($node1->id(), 'de');
610 $this->assertParagraphsButtons(1);
611 // Change the node langcode to 'english' and upload another image.
612 $images = $this->drupalGetTestFiles('image')[1];
614 'title[0][value]' => 'Title in german (en)',
615 'langcode[0][value]' => 'en',
616 'files[field_paragraphs_demo_0_subform_field_paragraphs_demo_0_subform_field_images_demo_1][]' => $images->uri,
618 $this->drupalPostForm(NULL, $edit, t('Upload'));
619 // Check the paragraph langcode is still 'de' and its buttons are shown.
620 $this->assertParagraphsLangcode($node1->id(), 'de');
621 $this->assertParagraphsButtons(1);
622 $this->drupalPostForm(NULL, NULL, t('Save'));
623 // Check the paragraph langcode is now 'en' after saving.
624 $this->assertParagraphsLangcode($node1->id());
626 // Check the paragraph langcode is 'en' and its buttons are still shown.
627 $this->clickLink(t('Edit'));
628 $this->assertParagraphsLangcode($node1->id());
629 $this->assertParagraphsButtons(1);
631 // Case 2: Check the paragraphs buttons after changing the NODE language
632 // (original node langcode in ENGLISH, default site langcode in english).
634 // Create another node.
635 $this->drupalGet('node/add/paragraphed_content_demo');
636 // Check that the node langcode is 'english' and add a 'Nested Paragraph'.
637 $this->assertOptionSelected('edit-langcode-0-value', 'en');
638 $this->drupalPostForm(NULL, NULL, t('Add Nested Paragraph'));
639 // Check that the paragraphs buttons are displayed and add an 'Images'
640 // paragraph inside the nested paragraph.
641 $this->assertParagraphsButtons(1);
642 $this->drupalPostAjaxForm(NULL, NULL, 'field_paragraphs_demo_0_subform_field_paragraphs_demo_images_add_more');
643 // Upload an image and check the paragraphs buttons are still displayed.
644 $images = $this->drupalGetTestFiles('image')[0];
646 'title[0][value]' => 'Title in english',
647 'files[field_paragraphs_demo_0_subform_field_paragraphs_demo_0_subform_field_images_demo_0][]' => $images->uri,
649 $this->drupalPostForm(NULL, $edit, t('Upload'));
650 $this->assertParagraphsButtons(1);
651 $this->drupalPostForm(NULL, NULL, t('Save'));
652 $this->assertText('Title in english');
653 $node2 = $this->drupalGetNodeByTitle('Title in english');
655 // Check the paragraph langcode is 'en' and its buttons are displayed.
656 // @todo check for the nested children paragraphs buttons and langcode
657 // when it's supported.
658 $this->clickLink(t('Edit'));
659 $this->assertParagraphsLangcode($node2->id());
660 $this->assertParagraphsButtons(1);
661 // Change the node langcode to 'german' and add another 'Images' paragraph.
663 'title[0][value]' => 'Title in english (de)',
664 'langcode[0][value]' => 'de',
666 $this->drupalPostForm(NULL, $edit, t('Add Images'));
667 // Check the paragraphs langcode are still 'en' and their buttons are shown.
668 $this->assertParagraphsLangcode($node2->id());
669 $this->assertParagraphsButtons(2);
670 // Upload an image, check the paragraphs langcode are still 'en' and their
671 // buttons are displayed.
672 $images = $this->drupalGetTestFiles('image')[1];
673 $this->drupalPostForm(NULL, [
674 'files[field_paragraphs_demo_1_subform_field_images_demo_0][]' => $images->uri,
676 $this->assertParagraphsLangcode($node2->id());
677 $this->assertParagraphsButtons(2);
678 $this->drupalPostForm(NULL, NULL, t('Save'));
679 // Check the paragraphs langcode are now 'de' after saving.
680 $this->assertParagraphsLangcode($node2->id(), 'de');
682 // Change node langcode back to 'english' and save.
683 $this->clickLink(t('Edit'));
685 'title[0][value]' => 'Title in english',
686 'langcode[0][value]' => 'en',
688 $this->drupalPostForm(NULL, $edit, t('Save'));
689 // Check the paragraphs langcode are now 'en' after saving.
690 $this->assertParagraphsLangcode($node2->id());
692 // Case 3: Check the paragraphs buttons after changing the SITE language.
694 // Change the site langcode to german.
696 'site_default_language' => 'de',
698 $this->drupalPostForm('admin/config/regional/language', $edit, t('Save configuration'));
700 // Check the original node and the paragraphs langcode are still 'en' and
701 // check that the paragraphs buttons are still displayed.
702 $this->drupalGet('node/' . $node2->id() . '/edit');
703 $this->assertParagraphsLangcode($node2->id());
704 $this->assertParagraphsButtons(2);
705 // Add another 'Images' paragraph with node langcode as 'english'.
706 $this->drupalPostForm(NULL, NULL, t('Add Images'));
707 // Check the paragraph langcode are still 'en' and their buttons are shown.
708 $this->assertParagraphsLangcode($node2->id());
709 $this->assertParagraphsButtons(3);
710 // Upload an image, check the paragraphs langcode are still 'en' and their
711 // buttons are displayed.
712 $images = $this->drupalGetTestFiles('image')[2];
713 $this->drupalPostForm(NULL, [
714 'files[field_paragraphs_demo_2_subform_field_images_demo_0][]' => $images->uri,
716 $this->assertParagraphsLangcode($node2->id());
717 $this->assertParagraphsButtons(3);
718 $this->drupalPostForm(NULL, NULL, t('Save'));
719 // Check the paragraphs langcode are still 'en' after saving.
720 $this->assertParagraphsLangcode($node2->id());
722 // Check the paragraphs langcode are still 'en' and their buttons are shown.
723 $this->clickLink(t('Edit'));
724 $this->assertParagraphsLangcode($node2->id());
725 $this->assertParagraphsButtons(3);
726 // Change node langcode to 'german' and add another 'Images' paragraph.
728 'title[0][value]' => 'Title in english (de)',
729 'langcode[0][value]' => 'de',
731 $this->drupalPostForm(NULL, $edit, t('Add Images'));
732 // Check the paragraphs langcode are still 'en' and their buttons are shown.
733 $this->assertParagraphsLangcode($node2->id());
734 $this->assertParagraphsButtons(4);
735 // Upload an image, check the paragraphs langcode are still 'en' and their
736 // buttons are displayed.
737 $images = $this->drupalGetTestFiles('image')[3];
738 $this->drupalPostForm(NULL, [
739 'files[field_paragraphs_demo_3_subform_field_images_demo_0][]' => $images->uri,
741 $this->assertParagraphsLangcode($node2->id());
742 $this->assertParagraphsButtons(4);
743 $this->drupalPostForm(NULL, NULL, t('Save'));
744 // Check the paragraphs langcode are now 'de' after saving.
745 $this->assertParagraphsLangcode($node2->id(), 'de');
749 * Passes if the paragraphs buttons are present.
752 * Number of paragraphs buttons to look for.
754 protected function assertParagraphsButtons($count) {
755 $this->assertParagraphsButtonsHelper($count, FALSE);
759 * Passes if the paragraphs buttons are NOT present.
762 * Number of paragraphs buttons to look for.
764 protected function assertNoParagraphsButtons($count) {
765 $this->assertParagraphsButtonsHelper($count, TRUE);
769 * Helper for assertParagraphsButtons and assertNoParagraphsButtons.
772 * Number of paragraphs buttons to look for.
773 * @param bool $hidden
774 * TRUE if these buttons should not be shown, FALSE otherwise.
777 protected function assertParagraphsButtonsHelper($count, $hidden = TRUE) {
778 for ($i = 0; $i < $count; $i++) {
779 $remove_button = $this->xpath('//*[@name="field_paragraphs_demo_' . $i . '_remove"]');
781 $this->assertNotEqual(count($remove_button), 0);
784 $this->assertEqual(count($remove_button), 0);
788 // It is enough to check for the specific paragraph type 'Images' to assert
789 // the add more buttons presence for this test class.
790 $add_button = $this->xpath('//input[@value="Add Images"]');
792 $this->assertNotEqual(count($add_button), 0);
795 $this->assertEqual(count($add_button), 0);
800 * Assert each paragraph items have the same langcode as the node one.
802 * @param string $node_id
803 * The node ID which contains the paragraph items to be checked.
804 * @param string $source_lang
805 * The expected node source langcode. Defaults to 'en'.
806 * @param string $trans_lang
807 * The expected translated node langcode. Defaults to NULL.
809 protected function assertParagraphsLangcode($node_id, $source_lang = 'en', $trans_lang = NULL) {
810 // Update the outdated node and check all the paragraph items langcodes.
811 \Drupal::entityTypeManager()->getStorage('node')->resetCache([$node_id]);
812 /** @var \Drupal\node\NodeInterface $node */
813 $node = Node::load($node_id);
814 $node_langcode = $node->langcode->value;
815 $this->assertEqual($node_langcode, $source_lang, 'Host langcode matches.');
817 /** @var \Drupal\Core\Entity\ContentEntityBase $paragraph */
818 foreach ($node->field_paragraphs_demo->referencedEntities() as $paragraph) {
819 $paragraph_langcode = $paragraph->language()->getId();
820 $message = new FormattableMarkup('Node langcode is "@node", paragraph item langcode is "@item".', ['@node' => $source_lang, '@item' => $paragraph_langcode]);
821 $this->assertEqual($paragraph_langcode, $source_lang, $message);
824 // Check the translation.
825 if (!empty($trans_lang)) {
826 $this->assertTrue($node->hasTranslation($trans_lang), 'Translation exists.');
828 if ($node->hasTranslation($trans_lang)) {
829 $trans_node = $node->getTranslation($trans_lang);
830 $trans_node_langcode = $trans_node->language()->getId();
831 $this->assertEqual($trans_node_langcode, $trans_lang, 'Translated node langcode matches.');
833 // Check the paragraph item langcode matching the translated node langcode.
834 foreach ($trans_node->field_paragraphs_demo->referencedEntities() as $paragraph) {
835 if ($paragraph->hasTranslation($trans_lang)) {
836 $trans_item = $paragraph->getTranslation($trans_lang);
837 $paragraph_langcode = $trans_item->language()->getId();
838 $message = new FormattableMarkup('Translated node langcode is "@node", paragraph item langcode is "@item".', ['@node' => $trans_lang, '@item' => $paragraph_langcode]);
839 $this->assertEqual($paragraph_langcode, $trans_lang, $message);