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',
57 $this->drupalPostForm('node/add/article', $edit, t('Save and Publish'));
58 $this->assertText(t('Article Published English node has been created.'));
59 $english_node = $this->drupalGetNodeByTitle('Published English node');
61 // Add a French translation.
62 $this->drupalGet('node/' . $english_node->id() . '/translations');
63 $this->clickLink(t('Add'));
65 'title[0][value]' => 'French node Draft',
67 $this->drupalPostForm(NULL, $edit, t('Save and Create New Draft (this translation)'));
68 // Here the error has occurred "The website encountered an unexpected error.
69 // Please try again later."
70 // If the translation has got lost.
71 $this->assertText(t('Article French node Draft has been updated.'));
73 // Create an article in English.
75 'title[0][value]' => 'English node',
76 'langcode[0][value]' => 'en',
78 $this->drupalPostForm('node/add/article', $edit, t('Save and Create New Draft'));
79 $this->assertText(t('Article English node has been created.'));
80 $english_node = $this->drupalGetNodeByTitle('English node');
82 // Add a French translation.
83 $this->drupalGet('node/' . $english_node->id() . '/translations');
84 $this->clickLink(t('Add'));
86 'title[0][value]' => 'French node',
88 $this->drupalPostForm(NULL, $edit, t('Save and Create New Draft (this translation)'));
89 $this->assertText(t('Article French node has been updated.'));
90 $english_node = $this->drupalGetNodeByTitle('English node', TRUE);
92 // Publish the English article and check that the translation stays
94 $this->drupalPostForm('node/' . $english_node->id() . '/edit', [], t('Save and Publish (this translation)'));
95 $this->assertText(t('Article English node has been updated.'));
96 $english_node = $this->drupalGetNodeByTitle('English node', TRUE);
97 $french_node = $english_node->getTranslation('fr');
98 $this->assertEqual('French node', $french_node->label());
100 $this->assertEqual($english_node->moderation_state->value, 'published');
101 $this->assertTrue($english_node->isPublished());
102 $this->assertEqual($french_node->moderation_state->value, 'draft');
103 $this->assertFalse($french_node->isPublished());
105 // Create another article with its translation. This time we will publish
106 // the translation first.
108 'title[0][value]' => 'Another node',
110 $this->drupalPostForm('node/add/article', $edit, t('Save and Create New Draft'));
111 $this->assertText(t('Article Another node has been created.'));
112 $english_node = $this->drupalGetNodeByTitle('Another node');
114 // Add a French translation.
115 $this->drupalGet('node/' . $english_node->id() . '/translations');
116 $this->clickLink(t('Add'));
118 'title[0][value]' => 'Translated node',
120 $this->drupalPostForm(NULL, $edit, t('Save and Create New Draft (this translation)'));
121 $this->assertText(t('Article Translated node has been updated.'));
122 $english_node = $this->drupalGetNodeByTitle('Another node', TRUE);
124 // Publish the translation and check that the source language version stays
126 $this->drupalPostForm('fr/node/' . $english_node->id() . '/edit', [], t('Save and Publish (this translation)'));
127 $this->assertText(t('Article Translated node has been updated.'));
128 $english_node = $this->drupalGetNodeByTitle('Another node', TRUE);
129 $french_node = $english_node->getTranslation('fr');
130 $this->assertEqual($french_node->moderation_state->value, 'published');
131 $this->assertTrue($french_node->isPublished());
132 $this->assertEqual($english_node->moderation_state->value, 'draft');
133 $this->assertFalse($english_node->isPublished());
135 // Now check that we can create a new draft of the translation.
137 'title[0][value]' => 'New draft of translated node',
139 $this->drupalPostForm('fr/node/' . $english_node->id() . '/edit', $edit, t('Save and Create New Draft (this translation)'));
140 $this->assertText(t('Article New draft of translated node has been updated.'));
141 $english_node = $this->drupalGetNodeByTitle('Another node', TRUE);
142 $french_node = $english_node->getTranslation('fr');
143 $this->assertEqual($french_node->moderation_state->value, 'published');
144 $this->assertTrue($french_node->isPublished());
145 $this->assertEqual($french_node->getTitle(), 'Translated node', 'The default revision of the published translation remains the same.');
147 // Publish the draft.
149 'new_state' => 'published',
151 $this->drupalPostForm('fr/node/' . $english_node->id() . '/latest', $edit, t('Apply'));
152 $this->assertText(t('The moderation state has been updated.'));
153 $english_node = $this->drupalGetNodeByTitle('Another node', TRUE);
154 $french_node = $english_node->getTranslation('fr');
155 $this->assertEqual($french_node->moderation_state->value, 'published');
156 $this->assertTrue($french_node->isPublished());
157 $this->assertEqual($french_node->getTitle(), 'New draft of translated node', 'The draft has replaced the published revision.');
159 // Publish the English article before testing the archive transition.
160 $this->drupalPostForm('node/' . $english_node->id() . '/edit', [], t('Save and Publish (this translation)'));
161 $this->assertText(t('Article Another node has been updated.'));
162 $english_node = $this->drupalGetNodeByTitle('Another node', TRUE);
163 $this->assertEqual($english_node->moderation_state->value, 'published');
165 // Archive the node and its translation.
166 $this->drupalPostForm('node/' . $english_node->id() . '/edit', [], t('Save and Archive (this translation)'));
167 $this->assertText(t('Article Another node has been updated.'));
168 $this->drupalPostForm('fr/node/' . $english_node->id() . '/edit', [], t('Save and Archive (this translation)'));
169 $this->assertText(t('Article New draft of translated node has been updated.'));
170 $english_node = $this->drupalGetNodeByTitle('Another node', TRUE);
171 $french_node = $english_node->getTranslation('fr');
172 $this->assertEqual($english_node->moderation_state->value, 'archived');
173 $this->assertFalse($english_node->isPublished());
174 $this->assertEqual($french_node->moderation_state->value, 'archived');
175 $this->assertFalse($french_node->isPublished());
177 // Create another article with its translation. This time publishing english
178 // after creating a forward french revision.
180 'title[0][value]' => 'An english node',
182 $this->drupalPostForm('node/add/article', $edit, t('Save and Create New Draft'));
183 $this->assertText(t('Article An english node has been created.'));
184 $english_node = $this->drupalGetNodeByTitle('An english node');
185 $this->assertFalse($english_node->isPublished());
187 // Add a French translation.
188 $this->drupalGet('node/' . $english_node->id() . '/translations');
189 $this->clickLink(t('Add'));
191 'title[0][value]' => 'A french node',
193 $this->drupalPostForm(NULL, $edit, t('Save and Publish (this translation)'));
194 $english_node = $this->drupalGetNodeByTitle('An english node', TRUE);
195 $french_node = $english_node->getTranslation('fr');
196 $this->assertTrue($french_node->isPublished());
197 $this->assertFalse($english_node->isPublished());
199 // Create a forward revision
200 $this->drupalPostForm('fr/node/' . $english_node->id() . '/edit', [], t('Save and Create New Draft (this translation)'));
201 $english_node = $this->drupalGetNodeByTitle('An english node', TRUE);
202 $french_node = $english_node->getTranslation('fr');
203 $this->assertTrue($french_node->isPublished());
204 $this->assertFalse($english_node->isPublished());
206 // Publish the english node and the default french node not the latest
207 // french node should be used.
208 $this->drupalPostForm('/node/' . $english_node->id() . '/edit', [], t('Save and Publish (this translation)'));
209 $english_node = $this->drupalGetNodeByTitle('An english node', TRUE);
210 $french_node = $english_node->getTranslation('fr');
211 $this->assertTrue($french_node->isPublished());
212 $this->assertTrue($english_node->isPublished());