View source
<?php
namespace Drupal\Tests\paragraphs_asymmetric_translation_widgets\Functional;
use Drupal\Tests\field_ui\Traits\FieldUiTestTrait;
use Drupal\Tests\paragraphs\Traits\ParagraphsCoreVersionUiTestTrait;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\paragraphs\FunctionalJavascript\ParagraphsTestBaseTrait;
class ParagraphsAsymmetricTranslationTest extends BrowserTestBase {
use FieldUiTestTrait, ParagraphsCoreVersionUiTestTrait, ParagraphsTestBaseTrait;
public static $modules = [
'node',
'paragraphs_demo',
'content_translation',
'block',
'paragraphs_asymmetric_translation_widgets',
];
protected $adminUser;
protected function setUp() {
parent::setUp();
$this
->drupalPlaceBlock('local_tasks_block');
$this
->drupalPlaceBlock('page_title_block');
$this->adminUser = $this
->drupalCreateUser([
'administer site configuration',
'administer nodes',
'create paragraphed_content_demo content',
'edit any paragraphed_content_demo content',
'delete any paragraphed_content_demo content',
'administer paragraph form display',
'administer node form display',
'administer paragraph fields',
'administer content translation',
'translate any entity',
'create content translations',
'administer languages',
'administer content types',
]);
$this
->drupalLogin($this->adminUser);
$edit = [
'entity_types[paragraph]' => TRUE,
'settings[node][paragraphed_content_demo][fields][field_paragraphs_demo]' => TRUE,
'settings[paragraph][nested_paragraph][translatable]' => TRUE,
'settings[paragraph][nested_paragraph][settings][language][language_alterable]' => FALSE,
'settings[paragraph][text][translatable]' => TRUE,
'settings[paragraph][text][settings][language][language_alterable]' => FALSE,
'settings[paragraph][nested_paragraph][fields][field_paragraphs_demo]' => TRUE,
];
$this
->drupalPostForm('admin/config/regional/content-language', $edit, t('Save configuration'));
}
public function testParagraphsMultilingualFieldTranslation() {
$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'));
$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'));
$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)'));
$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');
$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');
$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');
$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)'));
$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');
$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)'));
$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');
$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)'));
$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');
$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)'));
$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');
$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');
$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');
$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);
$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);
$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);
}
public function testParagraphsMultilingualFieldTranslationNested() {
$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'));
$this
->drupalGet('node/add/paragraphed_content_demo');
$this
->drupalPostForm(NULL, NULL, t('Add Nested Paragraph'), []);
$this
->drupalPostForm(NULL, NULL, 'field_paragraphs_demo_0_subform_field_paragraphs_demo_text_add_more');
$edit = [
'title[0][value]' => 'Title in english',
'field_paragraphs_demo[0][subform][field_paragraphs_demo][0][subform][field_text_demo][0][value]' => 'Text in english',
];
$this
->drupalPostForm(NULL, $edit, t('Save'));
$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_paragraphs_demo][0][subform][field_text_demo][0][value]' => 'Text in french',
];
$this
->drupalPostForm(NULL, $edit, t('Save (this 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');
$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');
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->drupalPostForm(NULL, [], 'field_paragraphs_demo_0_edit');
$edit = [
'field_paragraphs_demo[0][subform][field_paragraphs_demo][0][subform][field_text_demo][0][value]' => 'The updated english text',
];
$this
->drupalPostForm(NULL, $edit, t('Save (this translation)'));
$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');
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->drupalPostForm(NULL, [], 'field_paragraphs_demo_0_edit');
$this
->drupalPostForm(NULL, NULL, 'field_paragraphs_demo_0_subform_field_paragraphs_demo_text_add_more');
$edit = [
'field_paragraphs_demo[0][subform][field_paragraphs_demo][1][subform][field_text_demo][0][value]' => 'New english text',
];
$this
->drupalPostForm(NULL, $edit, t('Save (this translation)'));
$this
->drupalGet('node/' . $node
->id());
$this
->assertSession()
->pageTextContains('The updated english text');
$this
->assertSession()
->pageTextContains('New english text');
$this
->assertSession()
->responseNotContains('Text in french');
$this
->drupalGet('fr/node/' . $node
->id());
$this
->assertSession()
->pageTextContains('Text in french');
$this
->assertSession()
->responseNotContains('The updated english text');
$this
->assertSession()
->responseNotContains('New english text');
}
public function testParagraphsMultilingualFieldDeleteTranslation() {
$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'));
$this
->drupalGet('node/add/paragraphed_content_demo');
$this
->drupalPostForm(NULL, NULL, t('Add Nested Paragraph'));
$this
->drupalPostForm(NULL, NULL, 'field_paragraphs_demo_0_subform_field_paragraphs_demo_text_add_more');
$edit = [
'title[0][value]' => 'Title in english',
'field_paragraphs_demo[0][subform][field_paragraphs_demo][0][subform][field_text_demo][0][value]' => 'Text in english',
];
$this
->drupalPostForm(NULL, $edit, t('Save'));
$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_paragraphs_demo][0][subform][field_text_demo][0][value]' => 'Text in french',
];
$this
->drupalPostForm(NULL, $edit, t('Save (this 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');
$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');
$this
->drupalGet('fr/node/' . $node
->id() . '/delete');
$this
->drupalPostForm(NULL, NULL, t('Delete French 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');
}
}