You are here

public function ParagraphsAsymmetricTranslationTest::testParagraphsMultilingualFieldTranslation in Paragraphs asymmetric translation widgets 8

Test asymmetric translation.

1. Translate node and create different paragraphs. 2. Update the paragraphs on each translation. 3. Add different number of paragraphs on each translation. 4. Remove paragraphs from each translation. 5. Reorder the paragraphs on each translation.

File

tests/src/Functional/ParagraphsAsymmetricTranslationTest.php, line 89

Class

ParagraphsAsymmetricTranslationTest
Tests asymmetric translation of paragraphs.

Namespace

Drupal\Tests\paragraphs_asymmetric_translation_widgets\Functional

Code

public function testParagraphsMultilingualFieldTranslation() {

  // Set widget to assymetric one.
  $this
    ->drupalGet('/admin/structure/types/manage/paragraphed_content_demo/form-display');
  $this
    ->drupalPostForm(NULL, array(
    'fields[field_paragraphs_demo][type]' => 'paragraphs_classic_asymmetric',
  ), t('Save'));

  // 1. Translate node and create different paragraphs.
  // Add an English node.
  $this
    ->drupalGet('node/add/paragraphed_content_demo');
  $this
    ->drupalPostForm(NULL, NULL, t('Add Text'));
  $edit = [
    'title[0][value]' => 'Title in english',
    'field_paragraphs_demo[0][subform][field_text_demo][0][value]' => 'Text in english',
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));

  // Translate the node to French.
  $node = $this
    ->drupalGetNodeByTitle('Title in english');
  $this
    ->drupalGet('node/' . $node
    ->id() . '/translations/add/en/fr');
  $this
    ->drupalPostForm(NULL, [], 'field_paragraphs_demo_0_edit');
  $edit = [
    'title[0][value]' => 'Title in french',
    'field_paragraphs_demo[0][subform][field_text_demo][0][value]' => 'Text in french',
    'revision' => TRUE,
    'revision_log[0][value]' => 'french 1',
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save (this translation)'));

  // Check the english translation.
  $this
    ->drupalGet('node/' . $node
    ->id());
  $this
    ->assertSession()
    ->pageTextContains('Title in english');
  $this
    ->assertSession()
    ->pageTextContains('Text in english');
  $this
    ->assertSession()
    ->responseNotContains('Title in french');
  $this
    ->assertSession()
    ->responseNotContains('Text in french');

  // Check the french translation.
  $this
    ->drupalGet('fr/node/' . $node
    ->id());
  $this
    ->assertSession()
    ->pageTextContains('Title in french');
  $this
    ->assertSession()
    ->pageTextContains('Text in french');
  $this
    ->assertSession()
    ->responseNotContains('Title in english');
  $this
    ->assertSession()
    ->responseNotContains('Text in english');

  // Check the db.
  $select = \Drupal::database()
    ->select('node__field_paragraphs_demo', 'n');
  $select
    ->innerJoin('paragraphs_item', 'p', 'p.id = n.field_paragraphs_demo_target_id');
  $select
    ->addField('p', 'langcode');
  $select
    ->condition('n.entity_id', $node
    ->id());
  $paragraph_langcodes = $select
    ->execute()
    ->fetchCol();
  $this
    ->assertEquals([
    'en',
    'fr',
  ], $paragraph_langcodes, 'Translated paragraphs are separate entities');

  // 2. Update the paragraphs on each translation.
  // Try to edit the paragraphs, to see if the correct translation gets
  // updated. Start with the english.
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');
  $this
    ->drupalPostForm(NULL, [], 'field_paragraphs_demo_0_edit');
  $edit = [
    'field_paragraphs_demo[0][subform][field_text_demo][0][value]' => 'The updated english text',
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save (this translation)'));

  // Check if only the english node had its paragraph text updated, and that
  // there has been no mixing-up of the paragraph entities.
  $this
    ->drupalGet('node/' . $node
    ->id());
  $this
    ->assertSession()
    ->pageTextContains('The updated english text');
  $this
    ->assertSession()
    ->responseNotContains('Text in english');
  $this
    ->assertSession()
    ->responseNotContains('Text in french');
  $this
    ->drupalGet('fr/node/' . $node
    ->id());
  $this
    ->assertSession()
    ->pageTextContains('Text in french');
  $this
    ->assertSession()
    ->responseNotContains('Text in english');
  $this
    ->assertSession()
    ->responseNotContains('The updated english text');

  // Now repeat for the french.
  $this
    ->drupalGet('fr/node/' . $node
    ->id() . '/edit');
  $this
    ->drupalPostForm(NULL, [], 'field_paragraphs_demo_0_edit');
  $edit = [
    'field_paragraphs_demo[0][subform][field_text_demo][0][value]' => 'The updated french text',
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save (this translation)'));

  // Check if only the french node had its paragraph text updated, and that
  // there has been no mixing-up of the paragraph entities.
  $this
    ->drupalGet('node/' . $node
    ->id());
  $this
    ->assertSession()
    ->pageTextContains('The updated english text');
  $this
    ->assertSession()
    ->responseNotContains('Text in french');
  $this
    ->assertSession()
    ->responseNotContains('The updated french text');
  $this
    ->drupalGet('fr/node/' . $node
    ->id());
  $this
    ->assertSession()
    ->pageTextContains('The updated french text');
  $this
    ->assertSession()
    ->responseNotContains('Text in french');
  $this
    ->assertSession()
    ->responseNotContains('The updated english text');

  // 3. Add different number of paragraphs on each translation.
  // Add 2 more paragraphs on the english node.
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');
  $this
    ->drupalPostForm(NULL, NULL, t('Add Text'));
  $this
    ->drupalPostForm(NULL, NULL, t('Add Text'));
  $edit = [
    'field_paragraphs_demo[1][subform][field_text_demo][0][value]' => 'Second text in english',
    'field_paragraphs_demo[2][subform][field_text_demo][0][value]' => 'Third text in english',
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save (this translation)'));

  // Confirm that the english node has the new paragraphs, and the french
  // node is intact.
  $this
    ->drupalGet('node/' . $node
    ->id());
  $this
    ->assertSession()
    ->pageTextContains('The updated english text');
  $this
    ->assertSession()
    ->pageTextContains('Second text in english');
  $this
    ->assertSession()
    ->pageTextContains('Third text in english');
  $this
    ->assertSession()
    ->responseNotContains('The updated french text');
  $this
    ->drupalGet('fr/node/' . $node
    ->id());
  $this
    ->assertSession()
    ->pageTextContains('The updated french text');
  $this
    ->assertSession()
    ->responseNotContains('The updated english text');
  $this
    ->assertSession()
    ->responseNotContains('Second text in english');
  $this
    ->assertSession()
    ->responseNotContains('Third text in english');

  // Repeat the same, this time adding 3 new paragraphs on the french node.
  $this
    ->drupalGet('fr/node/' . $node
    ->id() . '/edit');
  $this
    ->drupalPostForm(NULL, NULL, t('Add Text'));
  $this
    ->drupalPostForm(NULL, NULL, t('Add Text'));
  $this
    ->drupalPostForm(NULL, NULL, t('Add Text'));
  $edit = [
    'field_paragraphs_demo[1][subform][field_text_demo][0][value]' => 'Second text in french',
    'field_paragraphs_demo[2][subform][field_text_demo][0][value]' => 'Third text in french',
    'field_paragraphs_demo[3][subform][field_text_demo][0][value]' => 'Fourth text in french',
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save (this translation)'));

  // Confirm that the french node has the new paragraphs, and the english
  // node is intact.
  $this
    ->drupalGet('node/' . $node
    ->id());
  $this
    ->assertSession()
    ->pageTextContains('The updated english text');
  $this
    ->assertSession()
    ->pageTextContains('Second text in english');
  $this
    ->assertSession()
    ->pageTextContains('Third text in english');
  $this
    ->assertSession()
    ->responseNotContains('The updated french text');
  $this
    ->assertSession()
    ->responseNotContains('Second text in french');
  $this
    ->assertSession()
    ->responseNotContains('Third text in french');
  $this
    ->assertSession()
    ->responseNotContains('Fourth text in french');
  $this
    ->drupalGet('fr/node/' . $node
    ->id());
  $this
    ->assertSession()
    ->pageTextContains('The updated french text');
  $this
    ->assertSession()
    ->pageTextContains('Second text in french');
  $this
    ->assertSession()
    ->pageTextContains('Third text in french');
  $this
    ->assertSession()
    ->pageTextContains('Fourth text in french');
  $this
    ->assertSession()
    ->responseNotContains('The updated english text');
  $this
    ->assertSession()
    ->responseNotContains('Second text in english');
  $this
    ->assertSession()
    ->responseNotContains('Third text in english');

  // 4. Remove paragraphs from each translation.
  // Delete one of the paragraphs on the english node, and confirm that the
  // rest are intact, and also that the french node is intact.
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');
  $this
    ->assertNotNull($this
    ->xpath('//*[@name="field_paragraphs_demo_1_remove"]'));
  $this
    ->drupalPostForm(NULL, [], 'field_paragraphs_demo_1_remove');
  $this
    ->assertNotNull($this
    ->xpath('//*[@name="field_paragraphs_demo_1_confirm_remove"]'));
  $this
    ->drupalPostForm(NULL, [], 'field_paragraphs_demo_1_confirm_remove');
  $this
    ->drupalPostForm(NULL, NULL, t('Save (this translation)'));
  $this
    ->drupalGet('node/' . $node
    ->id());
  $this
    ->assertSession()
    ->pageTextContains('The updated english text');
  $this
    ->assertSession()
    ->responseNotContains('Second text in english');
  $this
    ->assertSession()
    ->pageTextContains('Third text in english');
  $this
    ->assertSession()
    ->responseNotContains('The updated french text');
  $this
    ->assertSession()
    ->responseNotContains('Second text in french');
  $this
    ->assertSession()
    ->responseNotContains('Third text in french');
  $this
    ->assertSession()
    ->responseNotContains('Fourth text in french');
  $this
    ->drupalGet('fr/node/' . $node
    ->id());
  $this
    ->assertSession()
    ->responseNotContains('The updated english text');
  $this
    ->assertSession()
    ->responseNotContains('Second text in english');
  $this
    ->assertSession()
    ->responseNotContains('Third text in english');
  $this
    ->assertSession()
    ->pageTextContains('The updated french text');
  $this
    ->assertSession()
    ->pageTextContains('Second text in french');
  $this
    ->assertSession()
    ->pageTextContains('Third text in french');
  $this
    ->assertSession()
    ->pageTextContains('Fourth text in french');

  // Repeat the same for the french node, deleting 2 paragraphs now.
  $this
    ->drupalGet('fr/node/' . $node
    ->id() . '/edit');
  $this
    ->assertNotNull($this
    ->xpath('//*[@name="field_paragraphs_demo_1_remove"]'));
  $this
    ->drupalPostForm(NULL, [], 'field_paragraphs_demo_1_remove');
  $this
    ->assertNotNull($this
    ->xpath('//*[@name="field_paragraphs_demo_1_confirm_remove"]'));
  $this
    ->drupalPostForm(NULL, [], 'field_paragraphs_demo_1_confirm_remove');
  $this
    ->assertNotNull($this
    ->xpath('//*[@name="field_paragraphs_demo_3_remove"]'));
  $this
    ->drupalPostForm(NULL, [], 'field_paragraphs_demo_3_remove');
  $this
    ->assertNotNull($this
    ->xpath('//*[@name="field_paragraphs_demo_3_confirm_remove"]'));
  $this
    ->drupalPostForm(NULL, [], 'field_paragraphs_demo_3_confirm_remove');
  $this
    ->drupalPostForm(NULL, NULL, t('Save (this translation)'));
  $this
    ->drupalGet('fr/node/' . $node
    ->id());
  $this
    ->assertSession()
    ->responseNotContains('The updated english text');
  $this
    ->assertSession()
    ->responseNotContains('Second text in english');
  $this
    ->assertSession()
    ->responseNotContains('Third text in english');
  $this
    ->assertSession()
    ->pageTextContains('The updated french text');
  $this
    ->assertSession()
    ->responseNotContains('Second text in french');
  $this
    ->assertSession()
    ->pageTextContains('Third text in french');
  $this
    ->assertSession()
    ->responseNotContains('Fourth text in french');
  $this
    ->drupalGet('node/' . $node
    ->id());
  $this
    ->assertSession()
    ->pageTextContains('The updated english text');
  $this
    ->assertSession()
    ->responseNotContains('Second text in english');
  $this
    ->assertSession()
    ->pageTextContains('Third text in english');
  $this
    ->assertSession()
    ->responseNotContains('The updated french text');
  $this
    ->assertSession()
    ->responseNotContains('Second text in french');
  $this
    ->assertSession()
    ->responseNotContains('Third text in french');
  $this
    ->assertSession()
    ->responseNotContains('Fourth text in french');

  // 5. Reorder the paragraphs on each translation.
  // Reminder: This is our current content
  //
  // - English node
  // -- The updated english text.
  // -- Third text in english.
  //
  // - French node
  // -- The updated french text.
  // -- Third text in french.
  // First check the english node, that "The updated english text" is indeed
  // before the "Third text in english", as it has been set on the previous
  // steps.
  $this
    ->drupalGet('node/' . $node
    ->id());
  $regex = '/The updated english text.*Third text in english/s';
  $this
    ->assertSession()
    ->responseMatches($regex);
  $this
    ->drupalGet('fr/node/' . $node
    ->id());
  $regex = '/The updated french text.*Third text in french/s';
  $this
    ->assertSession()
    ->responseMatches($regex);

  // Reorder the paragraphs in the english node, and check if it applied
  // correctly. Check also that the french node is intact.
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');
  $edit = [
    'field_paragraphs_demo[0][_weight]' => 2,
    'field_paragraphs_demo[1][_weight]' => -2,
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save (this translation)'));
  $this
    ->drupalGet('node/' . $node
    ->id());
  $regex = '/Third text in english.*The updated english text/s';
  $this
    ->assertSession()
    ->responseMatches($regex);
  $this
    ->drupalGet('fr/node/' . $node
    ->id());
  $regex = '/The updated french text.*Third text in french/s';
  $this
    ->assertSession()
    ->responseMatches($regex);

  // And now reorder the french node, and then confirm that the new order
  // applied correctly, and that the english node is intact.
  $this
    ->drupalGet('fr/node/' . $node
    ->id() . '/edit');
  $edit = [
    'field_paragraphs_demo[0][_weight]' => 2,
    'field_paragraphs_demo[1][_weight]' => -2,
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save (this translation)'));
  $this
    ->drupalGet('fr/node/' . $node
    ->id());
  $regex = '/Third text in french.*The updated french text/s';
  $this
    ->assertSession()
    ->responseMatches($regex);
  $this
    ->drupalGet('node/' . $node
    ->id());
  $regex = '/Third text in english.*The updated english text/s';
  $this
    ->assertSession()
    ->responseMatches($regex);
}