3104450fcef441ff929d611c0839011826b36df4
[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       'moderation_state[0][state]' => 'published',
57     ];
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');
61
62     // Add a French translation.
63     $this->drupalGet('node/' . $english_node->id() . '/translations');
64     $this->clickLink(t('Add'));
65     $edit = [
66       'title[0][value]' => 'French node Draft',
67       'moderation_state[0][state]' => 'draft',
68     ];
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.'));
74
75     // Create an article in English.
76     $edit = [
77       'title[0][value]' => 'English node',
78       'langcode[0][value]' => 'en',
79       'moderation_state[0][state]' => 'draft',
80     ];
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');
84
85     // Add a French translation.
86     $this->drupalGet('node/' . $english_node->id() . '/translations');
87     $this->clickLink(t('Add'));
88     $edit = [
89       'title[0][value]' => 'French node',
90       'moderation_state[0][state]' => 'draft',
91     ];
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);
95
96     // Publish the English article and check that the translation stays
97     // unpublished.
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());
105
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());
110
111     // Create another article with its translation. This time we will publish
112     // the translation first.
113     $edit = [
114       'title[0][value]' => 'Another node',
115       'moderation_state[0][state]' => 'draft',
116     ];
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');
120
121     // Add a French translation.
122     $this->drupalGet('node/' . $english_node->id() . '/translations');
123     $this->clickLink(t('Add'));
124     $edit = [
125       'title[0][value]' => 'Translated node',
126       'moderation_state[0][state]' => 'draft',
127     ];
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);
131
132     // Publish the translation and check that the source language version stays
133     // unpublished.
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());
144
145     // Now check that we can create a new draft of the translation.
146     $edit = [
147       'title[0][value]' => 'New draft of translated node',
148       'moderation_state[0][state]' => 'draft',
149     ];
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.');
157
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.');
168
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');
176
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());
192   }
193
194 }