3 namespace Drupal\Tests\content_moderation\Functional;
6 * Test content_moderation functionality with localization and translation.
8 * @group content_moderation
10 class ModerationLocaleTest extends ModerationStateTestBase {
17 public static $modules = [
21 'content_translation',
25 * Tests article translations can be moderated separately.
27 public function testTranslateModeratedContent() {
28 $this->drupalLogin($this->rootUser);
30 // Enable moderation on Article node type.
31 $this->createContentTypeFromUi('Article', 'article', TRUE);
33 // Add French language.
35 'predefined_langcode' => 'fr',
37 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
39 // Enable content translation on articles.
40 $this->drupalGet('admin/config/regional/content-language');
42 'entity_types[node]' => TRUE,
43 'settings[node][article][translatable]' => TRUE,
44 'settings[node][article][settings][language][language_alterable]' => TRUE,
46 $this->drupalPostForm(NULL, $edit, t('Save configuration'));
48 // Adding languages requires a container rebuild in the test running
49 // environment so that multilingual services are used.
50 $this->rebuildContainer();
52 // Create a published article in English.
54 'title[0][value]' => 'Published English node',
55 'langcode[0][value]' => 'en',
56 'moderation_state[0][state]' => 'published',
58 $this->drupalPostForm('node/add/article', $edit, t('Save'));
59 $this->assertText(t('Article Published English node has been created.'));
60 $english_node = $this->drupalGetNodeByTitle('Published English node');
62 // Add a French translation.
63 $this->drupalGet('node/' . $english_node->id() . '/translations');
64 $this->clickLink(t('Add'));
66 'title[0][value]' => 'French node Draft',
67 'moderation_state[0][state]' => 'draft',
69 $this->drupalPostForm(NULL, $edit, t('Save (this translation)'));
70 // Here the error has occurred "The website encountered an unexpected error.
71 // Please try again later."
72 // If the translation has got lost.
73 $this->assertText(t('Article French node Draft has been updated.'));
75 // Create an article in English.
77 'title[0][value]' => 'English node',
78 'langcode[0][value]' => 'en',
79 'moderation_state[0][state]' => 'draft',
81 $this->drupalPostForm('node/add/article', $edit, t('Save'));
82 $this->assertText(t('Article English node has been created.'));
83 $english_node = $this->drupalGetNodeByTitle('English node');
85 // Add a French translation.
86 $this->drupalGet('node/' . $english_node->id() . '/translations');
87 $this->clickLink(t('Add'));
89 'title[0][value]' => 'French node',
90 'moderation_state[0][state]' => 'draft',
92 $this->drupalPostForm(NULL, $edit, t('Save (this translation)'));
93 $this->assertText(t('Article French node has been updated.'));
94 $english_node = $this->drupalGetNodeByTitle('English node', TRUE);
96 // Publish the English article and check that the translation stays
98 $this->drupalPostForm('node/' . $english_node->id() . '/edit', [
99 'moderation_state[0][state]' => 'published',
100 ], t('Save (this translation)'));
101 $this->assertText(t('Article English node has been updated.'));
102 $english_node = $this->drupalGetNodeByTitle('English node', TRUE);
103 $french_node = $english_node->getTranslation('fr');
104 $this->assertEqual('French node', $french_node->label());
106 $this->assertEqual($english_node->moderation_state->value, 'published');
107 $this->assertTrue($english_node->isPublished());
108 $this->assertEqual($french_node->moderation_state->value, 'draft');
109 $this->assertFalse($french_node->isPublished());
111 // Create another article with its translation. This time we will publish
112 // the translation first.
114 'title[0][value]' => 'Another node',
115 'moderation_state[0][state]' => 'draft',
117 $this->drupalPostForm('node/add/article', $edit, t('Save'));
118 $this->assertText(t('Article Another node has been created.'));
119 $english_node = $this->drupalGetNodeByTitle('Another node');
121 // Add a French translation.
122 $this->drupalGet('node/' . $english_node->id() . '/translations');
123 $this->clickLink(t('Add'));
125 'title[0][value]' => 'Translated node',
126 'moderation_state[0][state]' => 'draft',
128 $this->drupalPostForm(NULL, $edit, t('Save (this translation)'));
129 $this->assertText(t('Article Translated node has been updated.'));
130 $english_node = $this->drupalGetNodeByTitle('Another node', TRUE);
132 // Publish the translation and check that the source language version stays
134 $this->drupalPostForm('fr/node/' . $english_node->id() . '/edit', [
135 'moderation_state[0][state]' => 'published',
136 ], t('Save (this translation)'));
137 $this->assertText(t('Article Translated node has been updated.'));
138 $english_node = $this->drupalGetNodeByTitle('Another node', TRUE);
139 $french_node = $english_node->getTranslation('fr');
140 $this->assertEqual($french_node->moderation_state->value, 'published');
141 $this->assertTrue($french_node->isPublished());
142 $this->assertEqual($english_node->moderation_state->value, 'draft');
143 $this->assertFalse($english_node->isPublished());
145 // Now check that we can create a new draft of the translation.
147 'title[0][value]' => 'New draft of translated node',
148 'moderation_state[0][state]' => 'draft',
150 $this->drupalPostForm('fr/node/' . $english_node->id() . '/edit', $edit, t('Save (this translation)'));
151 $this->assertText(t('Article New draft of translated node has been updated.'));
152 $english_node = $this->drupalGetNodeByTitle('Another node', TRUE);
153 $french_node = $english_node->getTranslation('fr');
154 $this->assertEqual($french_node->moderation_state->value, 'published');
155 $this->assertTrue($french_node->isPublished());
156 $this->assertEqual($french_node->getTitle(), 'Translated node', 'The default revision of the published translation remains the same.');
158 // Publish the French article before testing the archive transition.
159 $this->drupalPostForm('fr/node/' . $english_node->id() . '/edit', [
160 'moderation_state[0][state]' => 'published',
161 ], t('Save (this translation)'));
162 $this->assertText(t('Article New draft of translated node has been updated.'));
163 $english_node = $this->drupalGetNodeByTitle('Another node', TRUE);
164 $french_node = $english_node->getTranslation('fr');
165 $this->assertEqual($french_node->moderation_state->value, 'published');
166 $this->assertTrue($french_node->isPublished());
167 $this->assertEqual($french_node->getTitle(), 'New draft of translated node', 'The draft has replaced the published revision.');
169 // Publish the English article before testing the archive transition.
170 $this->drupalPostForm('node/' . $english_node->id() . '/edit', [
171 'moderation_state[0][state]' => 'published',
172 ], t('Save (this translation)'));
173 $this->assertText(t('Article Another node has been updated.'));
174 $english_node = $this->drupalGetNodeByTitle('Another node', TRUE);
175 $this->assertEqual($english_node->moderation_state->value, 'published');
177 // Archive the node and its translation.
178 $this->drupalPostForm('node/' . $english_node->id() . '/edit', [
179 'moderation_state[0][state]' => 'archived',
180 ], t('Save (this translation)'));
181 $this->assertText(t('Article Another node has been updated.'));
182 $this->drupalPostForm('fr/node/' . $english_node->id() . '/edit', [
183 'moderation_state[0][state]' => 'archived',
184 ], t('Save (this translation)'));
185 $this->assertText(t('Article New draft of translated node has been updated.'));
186 $english_node = $this->drupalGetNodeByTitle('Another node', TRUE);
187 $french_node = $english_node->getTranslation('fr');
188 $this->assertEqual($english_node->moderation_state->value, 'archived');
189 $this->assertFalse($english_node->isPublished());
190 $this->assertEqual($french_node->moderation_state->value, 'archived');
191 $this->assertFalse($french_node->isPublished());