Newer
Older
Bojan Živanović
committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
<?php
namespace Drupal\inline_entity_form\Tests;
use Drupal\node\Entity\Node;
/**
* Tests translating inline entities.
*
* @group inline_entity_form
*/
class TranslationTest extends InlineEntityFormTestBase {
/**
* Modules to enable.
*
* @var array
*/
public static $modules = [
'content_translation', 'inline_entity_form_test', 'language',
];
/**
* {@inheritdoc}
*/
protected function setUp() {
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);
// Enable translations for both entity types.
$edit = [
'entity_types[node]' => TRUE,
];
foreach (['ief_test_complex', 'ief_reference_type'] as $node_type) {
$edit['settings[node][' . $node_type . '][translatable]'] = TRUE;
$edit['settings[node][' . $node_type . '][settings][language][language_alterable]'] = TRUE;
}
$this->drupalPostForm('admin/config/regional/content-language', $edit, t('Save configuration'));
// Allow referencing existing entities.
$form_display_storage = $this->container->get('entity_type.manager')->getStorage('entity_form_display');
/** @var \Drupal\Core\Entity\Display\EntityFormDisplayInterface $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();
}
/**
* Tests translating inline entities.
*/
protected function testTranslation() {
// Create a German node with a French translation.
$first_inline_node = Node::create([
'type' => 'ief_reference_type',
'langcode' => 'de',
'title' => 'An inline node',
'first_name' => 'Dieter',
]);
$translation = $first_inline_node->toArray();
$translation['title'][0]['value'] = 'An inline node in French';
$translation['first_name'][0]['value'] = 'Pierre';
$first_inline_node->addTranslation('fr', $translation);
$first_inline_node->save();
$this->drupalGet('node/add/ief_test_complex');
// Reference the German node.
$this->drupalPostAjaxForm(NULL, [], $this->getButtonName('//input[@type="submit" and @value="Add existing node" and @data-drupal-selector="edit-multi-actions-ief-add-existing"]'));
$edit = [
'multi[form][entity_id]' => 'An inline node (' . $first_inline_node->id() . ')',
];
$this->drupalPostAjaxForm(NULL, $edit, $this->getButtonName('//input[@type="submit" and @data-drupal-selector="edit-multi-form-actions-ief-reference-save"]'));
$this->assertResponse(200, 'Adding a new referenced entity was successful.');
// Add a new English inline node.
$this->drupalPostAjaxForm(NULL, [], $this->getButtonName('//input[@type="submit" and @value="Add new node" and @data-drupal-selector="edit-multi-actions-ief-add"]'));
$edit = [
'multi[form][inline_entity_form][title][0][value]' => 'Another inline node',
'multi[form][inline_entity_form][first_name][0][value]' => 'John',
'multi[form][inline_entity_form][last_name][0][value]' => 'Smith',
];
$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"]'));
$this->assertResponse(200, 'Creating a new inline entity was successful.');
$edit = [
'title[0][value]' => 'A node',
'langcode[0][value]' => 'en',
];
$this->drupalPostForm(NULL, $edit, t('Save'));
$this->assertResponse(200, 'Saving the parent entity was successful.');
// Both inline nodes should now be in English.
$first_inline_node = $this->drupalGetNodeByTitle('An inline node');
$second_inline_node = $this->drupalGetNodeByTitle('Another inline node');
$this->assertEqual($first_inline_node->get('langcode')->value, 'en', 'The first inline entity has the correct langcode.');
$this->assertEqual($second_inline_node->get('langcode')->value, 'en', 'The second inline entity has the correct langcode.');
// Edit the node, change the source language to German.
$node = $this->drupalGetNodeByTitle('A node');
$this->drupalGet('node/' . $node->id() . '/edit');
$edit = [
'langcode[0][value]' => 'de',
];
$this->drupalPostForm(NULL, $edit, t('Save'));
$this->assertResponse(200, 'Saving the parent entity was successful.');
// Both inline nodes should now be in German.
$first_inline_node = $this->drupalGetNodeByTitle('An inline node', TRUE);
$second_inline_node = $this->drupalGetNodeByTitle('Another inline node', TRUE);
$this->assertEqual($first_inline_node->get('langcode')->value, 'de', 'The first inline entity has the correct langcode.');
$this->assertEqual($second_inline_node->get('langcode')->value, 'de', 'The second inline entity has the correct langcode.');
// Add a German -> French translation.
$this->drupalGet('node/' . $node->id() . '/translations/add/de/fr');
// Confirm that the translatability clue has been removed.
Bojan Živanović
committed
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
$widget_title_element = $this->xpath('//fieldset[@id="edit-multi"]/legend/span');
$this->assertEqual((string) $widget_title_element[0], 'Multiple nodes', 'The widget has the expected title.');
// Confirm that the add and remove buttons are not present.
$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.');
$this->assertFalse((bool) $this->xpath('//input[@type="submit" and @value="Remove"]'), 'Remove button does not appear in the table.');
// Confirm the presence of the two node titles, in the expected languages.
$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');
$this->assertTrue((bool) $this->xpath('//td[@class="inline-entity-form-node-label" and contains(.,"Another inline node")]'), 'Second node title appears in the table');
// Edit the translations of both inline entities.
$this->drupalPostAjaxForm(NULL, [], $this->getButtonName('//input[@type="submit" and @value="Edit" and @data-drupal-selector="edit-multi-entities-0-actions-ief-entity-edit"]'));
$this->assertNoText('Last name', 'The non-translatable last_name field is hidden.');
$edit = [
'multi[form][inline_entity_form][entities][0][form][title][0][value]' => 'An inline node in French!',
'multi[form][inline_entity_form][entities][0][form][first_name][0][value]' => 'Damien',
];
$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"]'));
$this->drupalPostAjaxForm(NULL, [], $this->getButtonName('//input[@type="submit" and @value="Edit" and @data-drupal-selector="edit-multi-entities-1-actions-ief-entity-edit"]'));
$edit = [
'multi[form][inline_entity_form][entities][1][form][title][0][value]' => 'Another inline node in French!',
'multi[form][inline_entity_form][entities][1][form][first_name][0][value]' => 'Jacques',
];
$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"]'));
$this->drupalPostForm(NULL, [], t('Save (this translation)'));
$this->assertResponse(200, 'Saving the parent entity was successful.');
// Load using the original titles, confirming they haven't changed.
$first_inline_node = $this->drupalGetNodeByTitle('An inline node', TRUE);
$second_inline_node = $this->drupalGetNodeByTitle('Another inline node', TRUE);
// Confirm that the expected translated values are present.
$this->assertTrue($first_inline_node->hasTranslation('fr'), 'The first inline entity has a FR translation');
$this->assertTrue($second_inline_node->hasTranslation('fr'), 'The second inline entity has a FR translation');
$first_translation = $first_inline_node->getTranslation('fr');
$this->assertEqual($first_translation->title->value, 'An inline node in French!');
$this->assertEqual($first_translation->first_name->value, 'Damien');
$second_translation = $second_inline_node->getTranslation('fr');
$this->assertEqual($second_translation->title->value, 'Another inline node in French!');
$this->assertEqual($second_translation->first_name->value, 'Jacques');
}
}