View source
<?php
namespace Drupal\Tests\inline_entity_form\FunctionalJavascript;
use Drupal\node\Entity\Node;
class TranslationTest extends InlineEntityFormTestBase {
protected static $modules = [
'content_translation',
'inline_entity_form_translation_test',
'language',
];
protected function setUp() : void {
parent::setUp();
$this->user = $this
->createUser([
'create ief_reference_type content',
'edit any ief_reference_type content',
'delete any ief_reference_type content',
'create ief_test_complex content',
'edit any ief_test_complex content',
'delete any ief_test_complex content',
'view own unpublished content',
'administer content translation',
'translate any entity',
'create content translations',
'administer languages',
]);
$this
->drupalLogin($this->user);
$form_display_storage = $this->container
->get('entity_type.manager')
->getStorage('entity_form_display');
$display = $form_display_storage
->load('node.ief_test_complex.default');
$component = $display
->getComponent('multi');
$component['settings']['allow_existing'] = TRUE;
$display
->setComponent('multi', $component)
->save();
}
public function testTranslation() {
$first_nested_title_field_xpath = $this
->getXpathForNthInputByLabelText('Title', 2);
$first_name_field_xpath = $this
->getXpathForNthInputByLabelText('First name', 1);
$last_name_field_xpath = $this
->getXpathForNthInputByLabelText('Last name', 1);
$assert_session = $this
->assertSession();
$page = $this
->getSession()
->getPage();
$first_inline_node = Node::create([
'type' => 'ief_reference_type',
'langcode' => 'de',
'title' => 'Kann ein Känguru höher als ein Haus springen?',
'first_name' => 'Dieter',
]);
$translation = $first_inline_node
->toArray();
$translation['title'][0]['value'] = "Un kangourou peut-il sauter plus haut qu'une maison?";
$translation['first_name'][0]['value'] = 'Pierre';
$first_inline_node
->addTranslation('fr', $translation);
$first_inline_node
->save();
$this
->drupalGet('node/add/ief_test_complex');
$multi_fieldset = $assert_session
->elementExists('css', 'fieldset[data-drupal-selector="edit-multi"]');
$multi_fieldset
->pressButton('Add existing node');
$this
->assertNotEmpty($field = $assert_session
->waitForElement('xpath', $this
->getXpathForAutoCompleteInput()));
$field
->setValue('Kann ein Känguru höher als ein Haus springen? (' . $first_inline_node
->id() . ')');
$page
->pressButton('Add node');
$this
->waitForRowByTitle('Kann ein Känguru höher als ein Haus springen?');
$multi_fieldset
->pressButton('Add new node');
$this
->assertNotEmpty($create_button = $assert_session
->waitForButton('Create node'));
$assert_session
->elementExists('xpath', $first_nested_title_field_xpath)
->setValue('Can a kangaroo jump higher than a house?');
$assert_session
->elementExists('xpath', $first_name_field_xpath)
->setValue('John');
$assert_session
->elementExists('xpath', $last_name_field_xpath)
->setValue('Smith');
$create_button
->press();
$this
->waitForRowByTitle('Can a kangaroo jump higher than a house?');
$this
->assertRowByTitle('Kann ein Känguru höher als ein Haus springen?');
$assert_session
->elementsCount('css', 'tr.ief-row-entity', 2);
$page
->fillField('title[0][value]', 'A node');
$page
->selectFieldOption('langcode[0][value]', 'en');
$page
->pressButton('Save');
$assert_session
->pageTextContains('IEF test complex A node has been created.');
$first_inline_node = $this
->drupalGetNodeByTitle('Kann ein Känguru höher als ein Haus springen?');
$second_inline_node = $this
->drupalGetNodeByTitle('Can a kangaroo jump higher than a house?');
$this
->assertSame('en', $first_inline_node
->get('langcode')->value, 'The first inline entity has the correct langcode.');
$this
->assertEquals('en', $second_inline_node
->get('langcode')->value, 'The second inline entity has the correct langcode.');
$node = $this
->drupalGetNodeByTitle('A node');
$this
->drupalGet('node/' . $node
->id() . '/edit');
$page
->selectFieldOption('langcode[0][value]', 'de');
$page
->pressButton('Save');
$first_inline_node = $this
->drupalGetNodeByTitle('Kann ein Känguru höher als ein Haus springen?', TRUE);
$second_inline_node = $this
->drupalGetNodeByTitle('Can a kangaroo jump higher than a house?', TRUE);
$this
->assertSame('de', $first_inline_node
->get('langcode')->value, 'The first inline entity has the correct langcode.');
$this
->assertSame('de', $second_inline_node
->get('langcode')->value, 'The second inline entity has the correct langcode.');
$this
->drupalGet('node/' . $node
->id() . '/translations/add/de/fr');
$assert_session
->elementTextContains('xpath', '//fieldset[@id="edit-multi"]/legend/span', 'Multiple nodes');
$multi_fieldset = $assert_session
->elementExists('css', 'fieldset[data-drupal-selector="edit-multi"]');
$this
->assertEquals(FALSE, $multi_fieldset
->hasButton('Add new node'));
$this
->assertEquals(FALSE, $multi_fieldset
->hasButton('Remove'));
$first_reference = $this
->assertRowByTitle("Un kangourou peut-il sauter plus haut qu'une maison?");
$second_reference = $this
->assertRowByTitle('Can a kangaroo jump higher than a house?');
$assert_session
->elementsCount('css', 'tr.ief-row-entity', 2);
$first_reference
->getParent()
->pressButton('Edit');
$this
->assertNotEmpty($update_button = $assert_session
->waitForButton('Update node'));
$assert_session
->elementExists('xpath', $first_nested_title_field_xpath)
->setValue("Un kangourou peut-il sauter plus haut qu'une maison? - mis à jour");
$assert_session
->elementExists('xpath', $first_name_field_xpath)
->setValue('Damien');
$update_button
->press();
$this
->waitForRowByTitle("Un kangourou peut-il sauter plus haut qu'une maison? - mis à jour");
$second_reference
->getParent()
->pressButton('Edit');
$this
->assertNotEmpty($update_button = $assert_session
->waitForButton('Update node'));
$assert_session
->elementExists('xpath', $first_nested_title_field_xpath)
->setValue('tous les animaux qui sautent');
$assert_session
->elementExists('xpath', $first_name_field_xpath)
->setValue('Jacques');
$update_button
->press();
$this
->waitForRowByTitle('tous les animaux qui sautent');
$page
->pressButton('Save (this translation)');
$assert_session
->pageTextContains('IEF test complex A node has been updated.');
$first_inline_node = $this
->drupalGetNodeByTitle('Kann ein Känguru höher als ein Haus springen?', TRUE);
$second_inline_node = $this
->drupalGetNodeByTitle('Can a kangaroo jump higher than a house?', TRUE);
$this
->assertEquals(TRUE, $first_inline_node
->hasTranslation('fr'), 'The first inline entity has a FR translation');
$this
->assertEquals(TRUE, $second_inline_node
->hasTranslation('fr'), 'The second inline entity has a FR translation');
$first_translation = $first_inline_node
->getTranslation('fr');
$this
->assertSame("Un kangourou peut-il sauter plus haut qu'une maison? - mis à jour", $first_translation->title->value);
$this
->assertSame('Damien', $first_translation->first_name->value);
$second_translation = $second_inline_node
->getTranslation('fr');
$this
->assertEquals('tous les animaux qui sautent', $second_translation->title->value);
$this
->assertSame('Jacques', $second_translation->first_name->value);
}
}