a9d95f0b9ff1ce83d22984a774ed96ef4427a472
[yaffs-website] / web / core / modules / content_moderation / tests / src / Functional / ModerationLocaleTest.php
1 <?php
2
3 namespace Drupal\Tests\content_moderation\Functional;
4
5 /**
6  * Test content_moderation functionality with localization and translation.
7  *
8  * @group content_moderation
9  */
10 class ModerationLocaleTest extends ModerationStateTestBase {
11
12   /**
13    * Modules to enable.
14    *
15    * @var array
16    */
17   public static $modules = [
18     'node',
19     'content_moderation',
20     'locale',
21     'content_translation',
22   ];
23
24   /**
25    * Tests article translations can be moderated separately.
26    */
27   public function testTranslateModeratedContent() {
28     $this->drupalLogin($this->rootUser);
29
30     // Enable moderation on Article node type.
31     $this->createContentTypeFromUi('Article', 'article', TRUE);
32
33     // Add French language.
34     $edit = [
35       'predefined_langcode' => 'fr',
36     ];
37     $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language'));
38
39     // Enable content translation on articles.
40     $this->drupalGet('admin/config/regional/content-language');
41     $edit = [
42       'entity_types[node]' => TRUE,
43       'settings[node][article][translatable]' => TRUE,
44       'settings[node][article][settings][language][language_alterable]' => TRUE,
45     ];
46     $this->drupalPostForm(NULL, $edit, t('Save configuration'));
47
48     // Adding languages requires a container rebuild in the test running
49     // environment so that multilingual services are used.
50     $this->rebuildContainer();
51
52     // Create a published article in English.
53     $edit = [
54       'title[0][value]' => 'Published English node',
55       'langcode[0][value]' => 'en',
56     ];
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');
60
61     // Add a French translation.
62     $this->drupalGet('node/' . $english_node->id() . '/translations');
63     $this->clickLink(t('Add'));
64     $edit = [
65       'title[0][value]' => 'French node Draft',
66     ];
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.'));
72
73     // Create an article in English.
74     $edit = [
75       'title[0][value]' => 'English node',
76       'langcode[0][value]' => 'en',
77     ];
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');
81
82     // Add a French translation.
83     $this->drupalGet('node/' . $english_node->id() . '/translations');
84     $this->clickLink(t('Add'));
85     $edit = [
86       'title[0][value]' => 'French node',
87     ];
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);
91
92     // Publish the English article and check that the translation stays
93     // unpublished.
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());
99
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());
104
105     // Create another article with its translation. This time we will publish
106     // the translation first.
107     $edit = [
108       'title[0][value]' => 'Another node',
109     ];
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');
113
114     // Add a French translation.
115     $this->drupalGet('node/' . $english_node->id() . '/translations');
116     $this->clickLink(t('Add'));
117     $edit = [
118       'title[0][value]' => 'Translated node',
119     ];
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);
123
124     // Publish the translation and check that the source language version stays
125     // unpublished.
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());
134
135     // Now check that we can create a new draft of the translation.
136     $edit = [
137       'title[0][value]' => 'New draft of translated node',
138     ];
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.');
146
147     // Publish the draft.
148     $edit = [
149       'new_state' => 'published',
150     ];
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.');
158
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');
164
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());
176
177     // Create another article with its translation. This time publishing english
178     // after creating a forward french revision.
179     $edit = [
180       'title[0][value]' => 'An english node',
181     ];
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());
186
187     // Add a French translation.
188     $this->drupalGet('node/' . $english_node->id() . '/translations');
189     $this->clickLink(t('Add'));
190     $edit = [
191       'title[0][value]' => 'A french node',
192     ];
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());
198
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());
205
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());
213   }
214
215 }