3 namespace Drupal\inline_entity_form\Tests;
5 use Drupal\node\Entity\Node;
8 * Tests translating inline entities.
10 * @group inline_entity_form
12 class TranslationTest extends InlineEntityFormTestBase {
19 public static $modules = [
20 'content_translation', 'inline_entity_form_test', 'language',
26 protected function setUp() {
29 $this->user = $this->createUser([
30 'create ief_reference_type content',
31 'edit any ief_reference_type content',
32 'delete any ief_reference_type content',
33 'create ief_test_complex content',
34 'edit any ief_test_complex content',
35 'delete any ief_test_complex content',
36 'view own unpublished content',
37 'administer content translation',
38 'translate any entity',
39 'create content translations',
40 'administer languages',
42 $this->drupalLogin($this->user);
44 // Enable translations for both entity types.
46 'entity_types[node]' => TRUE,
48 foreach (['ief_test_complex', 'ief_reference_type'] as $node_type) {
49 $edit['settings[node][' . $node_type . '][translatable]'] = TRUE;
50 $edit['settings[node][' . $node_type . '][settings][language][language_alterable]'] = TRUE;
52 $this->drupalPostForm('admin/config/regional/content-language', $edit, t('Save configuration'));
54 // Allow referencing existing entities.
55 $form_display_storage = $this->container->get('entity_type.manager')->getStorage('entity_form_display');
56 /** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $display */
57 $display = $form_display_storage->load('node.ief_test_complex.default');
58 $component = $display->getComponent('multi');
59 $component['settings']['allow_existing'] = TRUE;
60 $display->setComponent('multi', $component)->save();
64 * Tests translating inline entities.
66 protected function testTranslation() {
67 // Create a German node with a French translation.
68 $first_inline_node = Node::create([
69 'type' => 'ief_reference_type',
71 'title' => 'An inline node',
72 'first_name' => 'Dieter',
74 $translation = $first_inline_node->toArray();
75 $translation['title'][0]['value'] = 'An inline node in French';
76 $translation['first_name'][0]['value'] = 'Pierre';
77 $first_inline_node->addTranslation('fr', $translation);
78 $first_inline_node->save();
80 $this->drupalGet('node/add/ief_test_complex');
81 // Reference the German node.
82 $this->drupalPostAjaxForm(NULL, [], $this->getButtonName('//input[@type="submit" and @value="Add existing node" and @data-drupal-selector="edit-multi-actions-ief-add-existing"]'));
84 'multi[form][entity_id]' => 'An inline node (' . $first_inline_node->id() . ')',
86 $this->drupalPostAjaxForm(NULL, $edit, $this->getButtonName('//input[@type="submit" and @data-drupal-selector="edit-multi-form-actions-ief-reference-save"]'));
87 $this->assertResponse(200, 'Adding a new referenced entity was successful.');
89 // Add a new English inline node.
90 $this->drupalPostAjaxForm(NULL, [], $this->getButtonName('//input[@type="submit" and @value="Add new node" and @data-drupal-selector="edit-multi-actions-ief-add"]'));
92 'multi[form][inline_entity_form][title][0][value]' => 'Another inline node',
93 'multi[form][inline_entity_form][first_name][0][value]' => 'John',
94 'multi[form][inline_entity_form][last_name][0][value]' => 'Smith',
96 $this->drupalPostAjaxForm(NULL, $edit, $this->getButtonName('//input[@type="submit" and @value="Create node" and @data-drupal-selector="edit-multi-form-inline-entity-form-actions-ief-add-save"]'));
97 $this->assertResponse(200, 'Creating a new inline entity was successful.');
100 'title[0][value]' => 'A node',
101 'langcode[0][value]' => 'en',
103 $this->drupalPostForm(NULL, $edit, t('Save'));
104 $this->assertResponse(200, 'Saving the parent entity was successful.');
106 // Both inline nodes should now be in English.
107 $first_inline_node = $this->drupalGetNodeByTitle('An inline node');
108 $second_inline_node = $this->drupalGetNodeByTitle('Another inline node');
109 $this->assertEqual($first_inline_node->get('langcode')->value, 'en', 'The first inline entity has the correct langcode.');
110 $this->assertEqual($second_inline_node->get('langcode')->value, 'en', 'The second inline entity has the correct langcode.');
112 // Edit the node, change the source language to German.
113 $node = $this->drupalGetNodeByTitle('A node');
114 $this->drupalGet('node/' . $node->id() . '/edit');
116 'langcode[0][value]' => 'de',
118 $this->drupalPostForm(NULL, $edit, t('Save'));
119 $this->assertResponse(200, 'Saving the parent entity was successful.');
121 // Both inline nodes should now be in German.
122 $first_inline_node = $this->drupalGetNodeByTitle('An inline node', TRUE);
123 $second_inline_node = $this->drupalGetNodeByTitle('Another inline node', TRUE);
124 $this->assertEqual($first_inline_node->get('langcode')->value, 'de', 'The first inline entity has the correct langcode.');
125 $this->assertEqual($second_inline_node->get('langcode')->value, 'de', 'The second inline entity has the correct langcode.');
127 // Add a German -> French translation.
128 $this->drupalGet('node/' . $node->id() . '/translations/add/de/fr');
130 // Confirm that the translatability clue has been removed.
131 $widget_title_element = $this->xpath('//fieldset[@id="edit-multi"]/legend/span');
132 $this->assertEqual((string) $widget_title_element[0], 'Multiple nodes', 'The widget has the expected title.');
133 // Confirm that the add and remove buttons are not present.
134 $this->assertFalse((bool) $this->xpath('//input[@type="submit" and @value="Add new node" and @data-drupal-selector="edit-multi-actions-ief-add"]'), 'Add new node button does not appear in the table.');
135 $this->assertFalse((bool) $this->xpath('//input[@type="submit" and @value="Remove"]'), 'Remove button does not appear in the table.');
136 // Confirm the presence of the two node titles, in the expected languages.
137 $this->assertTrue((bool) $this->xpath('//td[@class="inline-entity-form-node-label" and contains(.,"An inline node in French")]'), 'First inline node title appears in the table');
138 $this->assertTrue((bool) $this->xpath('//td[@class="inline-entity-form-node-label" and contains(.,"Another inline node")]'), 'Second node title appears in the table');
140 // Edit the translations of both inline entities.
141 $this->drupalPostAjaxForm(NULL, [], $this->getButtonName('//input[@type="submit" and @value="Edit" and @data-drupal-selector="edit-multi-entities-0-actions-ief-entity-edit"]'));
142 $this->assertNoText('Last name', 'The non-translatable last_name field is hidden.');
144 'multi[form][inline_entity_form][entities][0][form][title][0][value]' => 'An inline node in French!',
145 'multi[form][inline_entity_form][entities][0][form][first_name][0][value]' => 'Damien',
147 $this->drupalPostAjaxForm(NULL, $edit, $this->getButtonName('//input[@type="submit" and @value="Update node" and @data-drupal-selector="edit-multi-form-inline-entity-form-entities-0-form-actions-ief-edit-save"]'));
149 $this->drupalPostAjaxForm(NULL, [], $this->getButtonName('//input[@type="submit" and @value="Edit" and @data-drupal-selector="edit-multi-entities-1-actions-ief-entity-edit"]'));
151 'multi[form][inline_entity_form][entities][1][form][title][0][value]' => 'Another inline node in French!',
152 'multi[form][inline_entity_form][entities][1][form][first_name][0][value]' => 'Jacques',
154 $this->drupalPostAjaxForm(NULL, $edit, $this->getButtonName('//input[@type="submit" and @value="Update node" and @data-drupal-selector="edit-multi-form-inline-entity-form-entities-1-form-actions-ief-edit-save"]'));
156 $this->drupalPostForm(NULL, [], t('Save (this translation)'));
157 $this->assertResponse(200, 'Saving the parent entity was successful.');
159 // Load using the original titles, confirming they haven't changed.
160 $first_inline_node = $this->drupalGetNodeByTitle('An inline node', TRUE);
161 $second_inline_node = $this->drupalGetNodeByTitle('Another inline node', TRUE);
162 // Confirm that the expected translated values are present.
163 $this->assertTrue($first_inline_node->hasTranslation('fr'), 'The first inline entity has a FR translation');
164 $this->assertTrue($second_inline_node->hasTranslation('fr'), 'The second inline entity has a FR translation');
165 $first_translation = $first_inline_node->getTranslation('fr');
166 $this->assertEqual($first_translation->title->value, 'An inline node in French!');
167 $this->assertEqual($first_translation->first_name->value, 'Damien');
168 $second_translation = $second_inline_node->getTranslation('fr');
169 $this->assertEqual($second_translation->title->value, 'Another inline node in French!');
170 $this->assertEqual($second_translation->first_name->value, 'Jacques');