Upgraded imagemagick and manually altered pdf to image module to handle changes....
[yaffs-website] / web / modules / contrib / paragraphs / src / Tests / Classic / ParagraphsTranslationTest.php
1 <?php
2
3 namespace Drupal\paragraphs\Tests\Classic;
4
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;
11
12 /**
13  * Tests the configuration of paragraphs.
14  *
15  * @group paragraphs
16  */
17 class ParagraphsTranslationTest extends ParagraphsTestBase {
18
19   /**
20    * Modules to enable.
21    *
22    * @var array
23    */
24   public static $modules = array(
25     'paragraphs_demo',
26     'content_translation',
27     'link',
28   );
29
30   /**
31    * A user with admin permissions.
32    *
33    * @var array
34    */
35   protected $admin_user;
36
37   /**
38    * {@inheritdoc}
39    */
40   protected function setUp() {
41     parent::setUp();
42     $this->loginAsAdmin([
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',
51     ]);
52     $edit = [
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,
56     ];
57     $this->drupalPostForm('admin/config/regional/content-language', $edit, t('Save configuration'));
58
59     // Set the form display to classic.
60     EntityFormDisplay::load('node.paragraphed_content_demo.default')
61       ->setComponent('field_paragraphs_demo', ['type' => 'entity_reference_paragraphs'])
62       ->save();
63     EntityFormDisplay::load('paragraph.nested_paragraph.default')
64       ->setComponent('field_paragraphs_demo', ['type' => 'entity_reference_paragraphs'])
65       ->save();
66
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)
72         ->save();
73     }
74   }
75
76   /**
77    * Tests the paragraph translation.
78    */
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');
84
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');
91
92     // Check if the publish/unpublish option works.
93     $this->drupalGet('admin/structure/paragraphs_type/text_image/form-display');
94     $edit = array(
95       'fields[status][type]' => 'boolean_checkbox',
96       'fields[status][region]' => 'content',
97     );
98
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');
103     $edit = [
104       'title[0][value]' => 'example_publish_unpublish',
105       'field_paragraphs_demo[0][subform][field_text_demo][0][value]' => 'Example published and unpublished',
106     ];
107     $this->drupalPostForm(NULL, $edit, t('Save'));
108     $this->assertText(t('Example published and unpublished'));
109     $this->clickLink(t('Edit'));
110
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');
113     $edit = [
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'
116     ];
117     $this->drupalPostForm(NULL, $edit + ['status[value]' => FALSE], t('Save'));
118     $this->assertNoText(t('Example published and unpublished'));
119
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');
136       }
137     }
138
139     // Add paragraphed content.
140     $this->drupalGet('node/add/paragraphed_content_demo');
141     $this->drupalPostForm(NULL, NULL, t('Add Text + Image'));
142     $edit = array(
143       'title[0][value]' => 'Title in english',
144       'field_paragraphs_demo[0][subform][field_text_demo][0][value]' => 'Text in english',
145     );
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');
154
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');
163
164     $edit = array(
165       'title[0][value]' => 'Title in french',
166       'field_paragraphs_demo[0][subform][field_text_demo][0][value]' => 'Text in french',
167       'revision' => TRUE,
168       'revision_log[0][value]' => 'french 1',
169     );
170     $this->drupalPostForm(NULL, $edit, t('Save (this translation)'));
171     $this->assertText('Paragraphed article Title in french has been updated.');
172
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');
179
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');
189     $edit = array(
190       'title[0][value]' => 'Title Change in french',
191       'field_paragraphs_demo[0][subform][field_text_demo][0][value]' => 'New text in french',
192       'revision' => TRUE,
193       'revision_log[0][value]' => 'french 2',
194     );
195     $this->drupalPostForm(NULL, $edit, t('Save (this translation)'));
196     $this->assertText('Title Change in french');
197     $this->assertText('New text in french');
198
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.');
207
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');
219
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);
225
226     // Make 'Images' paragraph field translatable, enable alt and title fields.
227     $this->drupalGet('admin/structure/paragraphs_type/images/fields');
228     $this->clickLink('Edit');
229     $edit = [
230       'translatable' => 1,
231       'settings[alt_field]' => 1,
232       'settings[title_field]' => 1,
233     ];
234     $this->drupalPostForm(NULL, $edit, t('Save settings'));
235
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'));
243     $edit = [
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,
249     ];
250     $this->drupalPostForm(NULL, $edit, t('Save'));
251
252     // Translate the node with the image paragraph.
253     $this->clickLink('Translate');
254     $this->clickLink(t('Add'), 1);
255     $edit = [
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',
259     ];
260     $this->drupalPostForm(NULL, $edit, t('Save (this translation)'));
261     $this->assertRaw('Title FR');
262
263     $this->drupalGet('node/add/paragraphed_content_demo');
264     $this->drupalPostForm(NULL, [], t('Add Text'));
265     $edit = [
266       'field_paragraphs_demo[0][subform][field_text_demo][0][value]' => 'texto',
267       'title[0][value]' => 'titulo',
268       'langcode[0][value]' => 'de',
269     ];
270     $this->drupalPostForm(NULL, $edit, t('Save'));
271     $node = $this->drupalGetNodeByTitle('titulo');
272     $this->assertParagraphsLangcode($node->id(), 'de');
273
274     // Test langcode matching when Paragraphs and node have different language.
275     $paragraph_1 = Paragraph::create([
276       'title' => 'Paragraph',
277       'type' => 'text',
278       'langcode' => 'en',
279       'field_text_demo' => 'english_text_1',
280     ]);
281     $paragraph_1->save();
282
283     $paragraph_2 = Paragraph::create([
284       'title' => 'Paragraph',
285       'type' => 'text',
286       'langcode' => 'en',
287       'field_text_demo' => 'english_text_2',
288     ]);
289     $paragraph_2->save();
290
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');
296
297     $node = $this->createNode([
298       'langcode' => 'de',
299       'type' => 'paragraphed_content_demo',
300       'field_paragraphs_demo' => [$paragraph_1, $translated_paragraph],
301     ]);
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');
314
315     // Create an english translation of the node.
316     $edit = [
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',
319     ];
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');
330
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);
341
342     // Add a non translatable field to Text Paragraph type.
343     $edit = [
344       'new_storage_type' => 'text_long',
345       'label' => 'untranslatable_field',
346       'field_name' => 'untranslatable_field',
347     ];
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'));
351
352     // Add a non translatable reference field.
353     $edit = [
354       'new_storage_type' => 'field_ui:entity_reference:node',
355       'label' => 'untranslatable_ref_field',
356       'field_name' => 'untranslatable_ref_field',
357     ];
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'));
361
362     // Add a non translatable link field.
363     $edit = [
364       'new_storage_type' => 'link',
365       'label' => 'untranslatable_link_field',
366       'field_name' => 'untranslatable_link_field',
367     ];
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'));
371
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)');
378
379     // Enable translations for the reference and link field.
380     $edit = [
381       'translatable' => TRUE,
382     ];
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'));
385
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)');
392   }
393
394   /**
395    * Tests the paragraph buttons presence in translation multilingual workflow.
396    *
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
400    *    langcode to EN.
401    */
402   public function testParagraphTranslationMultilingual() {
403     // Case 1: original node langcode in EN, translate in FR, change to DE.
404
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];
411     $edit = [
412       'title[0][value]' => 'Title in english',
413       'files[field_paragraphs_demo_0_subform_field_images_demo_0][]' => $images->uri,
414     ];
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());
422
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);
429     $edit = [
430       'title[0][value]' => 'Title in french',
431     ];
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());
439
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,
445     ], t('Upload'));
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');
453
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.
465     $edit = [
466       'title[0][value]' => 'Title in english (de)',
467       'langcode[0][value]' => 'de',
468     ];
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,
482     ], t('Upload'));
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');
490
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);
500
501     // Case 2: original node langcode in DE, change site langcode to DE, change
502     // node langcode to EN.
503
504     // Change the site langcode to french.
505     $this->drupalPostForm('admin/config/regional/language', [
506       'site_default_language' => 'fr',
507     ], t('Save configuration'));
508
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);
514
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,
526     ], t('Upload'));
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)');
536
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.
548     $edit = [
549       'title[0][value]' => 'Title in english',
550       'langcode[0][value]' => 'en',
551     ];
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,
560     ], t('Upload'));
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());
567   }
568
569   /**
570    * Tests the paragraphs buttons presence in multilingual workflow.
571    *
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.
577    */
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).
581
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'.
586     $edit = [
587       'title[0][value]' => 'Title in german',
588       'langcode[0][value]' => 'de',
589     ];
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,
599     ], t('Upload'));
600     $this->assertParagraphsButtons(1);
601     $this->drupalPostForm(NULL, NULL, t('Save'));
602     $this->assertText('Title in german');
603     $node1 = $this->getNodeByTitle('Title in german');
604
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];
613     $edit = [
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,
617     ];
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());
625
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);
630
631     // Case 2: Check the paragraphs buttons after changing the NODE language
632     // (original node langcode in ENGLISH, default site langcode in english).
633
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];
645     $edit = [
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,
648     ];
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');
654
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.
662     $edit = [
663       'title[0][value]' => 'Title in english (de)',
664       'langcode[0][value]' => 'de',
665     ];
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,
675     ], t('Upload'));
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');
681
682     // Change node langcode back to 'english' and save.
683     $this->clickLink(t('Edit'));
684     $edit = [
685       'title[0][value]' => 'Title in english',
686       'langcode[0][value]' => 'en',
687     ];
688     $this->drupalPostForm(NULL, $edit, t('Save'));
689     // Check the paragraphs langcode are now 'en' after saving.
690     $this->assertParagraphsLangcode($node2->id());
691
692     // Case 3: Check the paragraphs buttons after changing the SITE language.
693
694     // Change the site langcode to german.
695     $edit = [
696       'site_default_language' => 'de',
697     ];
698     $this->drupalPostForm('admin/config/regional/language', $edit, t('Save configuration'));
699
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,
715     ], t('Upload'));
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());
721
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.
727     $edit = [
728       'title[0][value]' => 'Title in english (de)',
729       'langcode[0][value]' => 'de',
730     ];
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,
740     ], t('Upload'));
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');
746   }
747
748   /**
749    * Passes if the paragraphs buttons are present.
750    *
751    * @param int $count
752    *   Number of paragraphs buttons to look for.
753    */
754   protected function assertParagraphsButtons($count) {
755     $this->assertParagraphsButtonsHelper($count, FALSE);
756   }
757
758   /**
759    * Passes if the paragraphs buttons are NOT present.
760    *
761    * @param int $count
762    *   Number of paragraphs buttons to look for.
763    */
764   protected function assertNoParagraphsButtons($count) {
765     $this->assertParagraphsButtonsHelper($count, TRUE);
766   }
767
768   /**
769    * Helper for assertParagraphsButtons and assertNoParagraphsButtons.
770    *
771    * @param int $count
772    *   Number of paragraphs buttons to look for.
773    * @param bool $hidden
774    *   TRUE if these buttons should not be shown, FALSE otherwise.
775    *   Defaults to TRUE.
776    */
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"]');
780       if (!$hidden) {
781         $this->assertNotEqual(count($remove_button), 0);
782       }
783       else {
784         $this->assertEqual(count($remove_button), 0);
785       }
786     }
787
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"]');
791     if (!$hidden) {
792       $this->assertNotEqual(count($add_button), 0);
793     }
794     else {
795       $this->assertEqual(count($add_button), 0);
796     }
797   }
798
799   /**
800    * Assert each paragraph items have the same langcode as the node one.
801    *
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.
808    */
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.');
816
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);
822     }
823
824     // Check the translation.
825     if (!empty($trans_lang)) {
826       $this->assertTrue($node->hasTranslation($trans_lang), 'Translation exists.');
827     }
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.');
832
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);
840         }
841       }
842     }
843   }
844 }