You are here

public function ParagraphsTranslationTest::testParagraphsMultilingualWorkflow in Paragraphs 8

Same name in this branch
  1. 8 tests/src/Functional/WidgetLegacy/ParagraphsTranslationTest.php \Drupal\Tests\paragraphs\Functional\WidgetLegacy\ParagraphsTranslationTest::testParagraphsMultilingualWorkflow()
  2. 8 tests/src/Functional/WidgetStable/ParagraphsTranslationTest.php \Drupal\Tests\paragraphs\Functional\WidgetStable\ParagraphsTranslationTest::testParagraphsMultilingualWorkflow()

Tests the paragraphs buttons presence in multilingual workflow.

This test covers the following test cases: 1) original node langcode in german, change to english. 2) original node langcode in english, change to german. 3) original node langcode in english, change site langcode to german, change node langcode to german.

File

tests/src/Functional/WidgetStable/ParagraphsTranslationTest.php, line 682

Class

ParagraphsTranslationTest
Tests the configuration of paragraphs.

Namespace

Drupal\Tests\paragraphs\Functional\WidgetStable

Code

public function testParagraphsMultilingualWorkflow() {

  // Case 1: Check the paragraphs buttons after changing the NODE language
  // (original node langcode in GERMAN, default site langcode in english).
  // Use the stable widget.
  $form_display = EntityFormDisplay::load('node.paragraphed_content_demo.default')
    ->setComponent('field_paragraphs_demo', [
    'type' => 'paragraphs',
  ]);
  $form_display
    ->save();

  // Use the stable widget.
  $form_display = EntityFormDisplay::load('paragraph.nested_paragraph.default')
    ->setComponent('field_paragraphs_demo', [
    'type' => 'paragraphs',
  ]);
  $form_display
    ->save();

  // Create a node and check that the node langcode is 'english'.
  $this
    ->drupalGet('node/add/paragraphed_content_demo');
  $option_field = $this
    ->assertSession()
    ->optionExists('edit-langcode-0-value', 'en');
  $this
    ->assertTrue($option_field
    ->hasAttribute('selected'));

  // Change the node langcode to 'german' and add a 'Nested Paragraph'.
  $edit = [
    'title[0][value]' => 'Title in german',
    'langcode[0][value]' => 'de',
  ];
  $this
    ->submitForm($edit, 'Add nested_paragraph');

  // Check that the paragraphs buttons are displayed and add an 'Images'
  // paragraph inside the nested paragraph.
  $this
    ->assertParagraphsButtons(1);
  $this
    ->submitForm([], 'field_paragraphs_demo_0_subform_field_paragraphs_demo_images_add_more');

  // Upload an image and check the paragraphs buttons are still displayed.
  $images = $this
    ->getTestFiles('image')[0];
  $this
    ->submitForm([
    'files[field_paragraphs_demo_0_subform_field_paragraphs_demo_0_subform_field_images_demo_0][]' => $images->uri,
  ], 'Upload');
  $this
    ->assertParagraphsButtons(1);
  $this
    ->submitForm([], 'Save');
  $this
    ->assertSession()
    ->pageTextContains('Title in german');
  $node1 = $this
    ->getNodeByTitle('Title in german');

  // Check the paragraph langcode is 'de' and its buttons are displayed.
  // @todo check for the nested children paragraphs buttons and langcode
  // when it's supported.
  $this
    ->clickLink('Edit');
  $this
    ->assertParagraphsLangcode($node1
    ->id(), 'de');
  $this
    ->assertParagraphsButtons(1);

  // Change the node langcode to 'english' and upload another image.
  $images = $this
    ->getTestFiles('image')[1];
  $edit = [
    'title[0][value]' => 'Title in german (en)',
    'langcode[0][value]' => 'en',
    'files[field_paragraphs_demo_0_subform_field_paragraphs_demo_0_subform_field_images_demo_1][]' => $images->uri,
  ];
  $this
    ->submitForm($edit, 'Upload');

  // Check the paragraph langcode is still 'de' and its buttons are shown.
  $this
    ->assertParagraphsLangcode($node1
    ->id(), 'de');
  $this
    ->assertParagraphsButtons(1);
  $this
    ->submitForm([], 'Save');

  // Check the paragraph langcode is now 'en' after saving.
  $this
    ->assertParagraphsLangcode($node1
    ->id());

  // Check the paragraph langcode is 'en' and its buttons are still shown.
  $this
    ->clickLink('Edit');
  $this
    ->assertParagraphsLangcode($node1
    ->id());
  $this
    ->assertParagraphsButtons(1);

  // Case 2: Check the paragraphs buttons after changing the NODE language
  // (original node langcode in ENGLISH, default site langcode in english).
  // Create another node.
  $this
    ->drupalGet('node/add/paragraphed_content_demo');

  // Check that the node langcode is 'english' and add a 'Nested Paragraph'.
  $option_field = $this
    ->assertSession()
    ->optionExists('edit-langcode-0-value', 'en');
  $this
    ->assertTrue($option_field
    ->hasAttribute('selected'));
  $this
    ->submitForm([], 'Add nested_paragraph');

  // Check that the paragraphs buttons are displayed and add an 'Images'
  // paragraph inside the nested paragraph.
  $this
    ->assertParagraphsButtons(1);
  $this
    ->submitForm([], 'field_paragraphs_demo_0_subform_field_paragraphs_demo_images_add_more');

  // Upload an image and check the paragraphs buttons are still displayed.
  $images = $this
    ->getTestFiles('image')[0];
  $edit = [
    'title[0][value]' => 'Title in english',
    'files[field_paragraphs_demo_0_subform_field_paragraphs_demo_0_subform_field_images_demo_0][]' => $images->uri,
  ];
  $this
    ->submitForm($edit, 'Upload');
  $this
    ->assertParagraphsButtons(1);
  $this
    ->submitForm([], 'Save');
  $this
    ->assertSession()
    ->pageTextContains('Title in english');
  $node2 = $this
    ->drupalGetNodeByTitle('Title in english');

  // Check the paragraph langcode is 'en' and its buttons are displayed.
  // @todo check for the nested children paragraphs buttons and langcode
  // when it's supported.
  $this
    ->clickLink('Edit');
  $this
    ->assertParagraphsLangcode($node2
    ->id());
  $this
    ->assertParagraphsButtons(1);

  // Change the node langcode to 'german' and add another 'Images' paragraph.
  $edit = [
    'title[0][value]' => 'Title in english (de)',
    'langcode[0][value]' => 'de',
  ];
  $this
    ->submitForm($edit, 'field_paragraphs_demo_images_add_more');

  // Check the paragraphs langcode are still 'en' and their buttons are shown.
  $this
    ->assertParagraphsLangcode($node2
    ->id());
  $this
    ->assertParagraphsButtons(2);

  // Upload an image, check the paragraphs langcode are still 'en' and their
  // buttons are displayed.
  $images = $this
    ->getTestFiles('image')[1];
  $this
    ->submitForm([
    'files[field_paragraphs_demo_1_subform_field_images_demo_0][]' => $images->uri,
  ], 'Upload');
  $this
    ->assertParagraphsLangcode($node2
    ->id());
  $this
    ->assertParagraphsButtons(2);
  $this
    ->submitForm([], 'Save');

  // Check the paragraphs langcode are now 'de' after saving.
  $this
    ->assertParagraphsLangcode($node2
    ->id(), 'de');

  // Change node langcode back to 'english' and save.
  $this
    ->clickLink('Edit');
  $edit = [
    'title[0][value]' => 'Title in english',
    'langcode[0][value]' => 'en',
  ];
  $this
    ->submitForm($edit, 'Save');

  // Check the paragraphs langcode are now 'en' after saving.
  $this
    ->assertParagraphsLangcode($node2
    ->id());

  // Case 3: Check the paragraphs buttons after changing the SITE language.
  // Change the site langcode to german.
  $edit = [
    'site_default_language' => 'de',
  ];
  $this
    ->drupalGet('admin/config/regional/language');
  $this
    ->submitForm($edit, 'Save configuration');

  // Check the original node and the paragraphs langcode are still 'en' and
  // check that the paragraphs buttons are still displayed.
  $this
    ->drupalGet('node/' . $node2
    ->id() . '/edit');
  $this
    ->assertParagraphsLangcode($node2
    ->id());
  $this
    ->assertParagraphsButtons(2);

  // Add another 'Images' paragraph with node langcode as 'english'.
  $this
    ->submitForm([], 'field_paragraphs_demo_images_add_more');

  // Check the paragraph langcode are still 'en' and their buttons are shown.
  $this
    ->assertParagraphsLangcode($node2
    ->id());
  $this
    ->assertParagraphsButtons(3);

  // Upload an image, check the paragraphs langcode are still 'en' and their
  // buttons are displayed.
  $images = $this
    ->getTestFiles('image')[2];
  $this
    ->submitForm([
    'files[field_paragraphs_demo_2_subform_field_images_demo_0][]' => $images->uri,
  ], 'Upload');
  $this
    ->assertParagraphsLangcode($node2
    ->id());
  $this
    ->assertParagraphsButtons(3);
  $this
    ->submitForm([], 'Save');

  // Check the paragraphs langcode are still 'en' after saving.
  $this
    ->assertParagraphsLangcode($node2
    ->id());

  // Check the paragraphs langcode are still 'en' and their buttons are shown.
  $this
    ->clickLink('Edit');
  $this
    ->assertParagraphsLangcode($node2
    ->id());
  $this
    ->assertParagraphsButtons(3);

  // Change node langcode to 'german' and add another 'Images' paragraph.
  $edit = [
    'title[0][value]' => 'Title in english (de)',
    'langcode[0][value]' => 'de',
  ];
  $this
    ->submitForm($edit, 'field_paragraphs_demo_images_add_more');

  // Check the paragraphs langcode are still 'en' and their buttons are shown.
  $this
    ->assertParagraphsLangcode($node2
    ->id());
  $this
    ->assertParagraphsButtons(4);

  // Upload an image, check the paragraphs langcode are still 'en' and their
  // buttons are displayed.
  $images = $this
    ->getTestFiles('image')[3];
  $this
    ->submitForm([
    'files[field_paragraphs_demo_3_subform_field_images_demo_0][]' => $images->uri,
  ], 'Upload');
  $this
    ->assertParagraphsLangcode($node2
    ->id());
  $this
    ->assertParagraphsButtons(4);
  $this
    ->submitForm([], 'Save');

  // Check the paragraphs langcode are now 'de' after saving.
  $this
    ->assertParagraphsLangcode($node2
    ->id(), 'de');
}