3 namespace Drupal\Tests\path\Functional;
5 use Drupal\language\Entity\ConfigurableLanguage;
6 use Drupal\Tests\BrowserTestBase;
7 use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
10 * Tests path aliases with Content Moderation.
12 * @group content_moderation
15 class PathContentModerationTest extends BrowserTestBase {
17 use ContentModerationTestTrait;
24 public static $modules = [
28 'content_translation',
34 protected function setUp() {
36 ConfigurableLanguage::createFromLangcode('fr')->save();
37 $this->rebuildContainer();
39 // Created a content type.
40 $this->drupalCreateContentType([
41 'name' => 'moderated',
42 'type' => 'moderated',
45 // Set the content type as moderated.
46 $workflow = $this->createEditorialWorkflow();
47 $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'moderated');
50 $this->drupalLogin($this->rootUser);
52 // Enable URL language detection and selection.
53 $edit = ['language_interface[enabled][language-url]' => 1];
54 $this->drupalPostForm('admin/config/regional/language/detection', $edit, 'Save settings');
56 // Enable translation for moderated node.
58 'entity_types[node]' => 1,
59 'settings[node][moderated][translatable]' => 1,
60 'settings[node][moderated][fields][path]' => 1,
61 'settings[node][moderated][fields][body]' => 1,
62 'settings[node][moderated][settings][language][language_alterable]' => 1,
64 $this->drupalPostForm('admin/config/regional/content-language', $edit, 'Save configuration');
65 \Drupal::entityTypeManager()->clearCachedDefinitions();
69 * Tests node path aliases on a moderated content type.
71 public function testNodePathAlias() {
72 // Create some moderated content with a path alias.
73 $this->drupalGet('node/add/moderated');
74 $this->assertSession()->fieldValueEquals('path[0][alias]', '');
75 $this->drupalPostForm(NULL, [
76 'title[0][value]' => 'moderated content',
77 'path[0][alias]' => '/moderated-content',
78 'moderation_state[0][state]' => 'published',
80 $node = $this->getNodeByTitle('moderated content');
82 // Add a pending revision with the same alias.
83 $this->drupalGet('node/' . $node->id() . '/edit');
84 $this->assertSession()->fieldValueEquals('path[0][alias]', '/moderated-content');
85 $this->drupalPostForm(NULL, [
86 'title[0][value]' => 'pending revision',
87 'path[0][alias]' => '/moderated-content',
88 'moderation_state[0][state]' => 'draft',
90 $this->assertSession()->pageTextNotContains('You can only change the URL alias for the published version of this content.');
92 // Create some moderated content with no path alias.
93 $this->drupalGet('node/add/moderated');
94 $this->assertSession()->fieldValueEquals('path[0][alias]', '');
95 $this->drupalPostForm(NULL, [
96 'title[0][value]' => 'moderated content 2',
97 'path[0][alias]' => '',
98 'moderation_state[0][state]' => 'published',
100 $node = $this->getNodeByTitle('moderated content 2');
102 // Add a pending revision with a new alias.
103 $this->drupalGet('node/' . $node->id() . '/edit');
104 $this->assertSession()->fieldValueEquals('path[0][alias]', '');
105 $this->drupalPostForm(NULL, [
106 'title[0][value]' => 'pending revision',
107 'path[0][alias]' => '/pending-revision',
108 'moderation_state[0][state]' => 'draft',
110 $this->assertSession()->pageTextContains('You can only change the URL alias for the published version of this content.');
112 // Create some moderated content with no path alias.
113 $this->drupalGet('node/add/moderated');
114 $this->assertSession()->fieldValueEquals('path[0][alias]', '');
115 $this->drupalPostForm(NULL, [
116 'title[0][value]' => 'moderated content 3',
117 'path[0][alias]' => '',
118 'moderation_state[0][state]' => 'published',
120 $node = $this->getNodeByTitle('moderated content 3');
122 // Add a pending revision with no path alias.
123 $this->drupalGet('node/' . $node->id() . '/edit');
124 $this->assertSession()->fieldValueEquals('path[0][alias]', '');
125 $this->drupalPostForm(NULL, [
126 'title[0][value]' => 'pending revision',
127 'path[0][alias]' => '',
128 'moderation_state[0][state]' => 'draft',
130 $this->assertSession()->pageTextNotContains('You can only change the URL alias for the published version of this content.');
134 * Tests that translated and moderated node can get new draft revision.
136 public function testTranslatedModeratedNodeAlias() {
137 // Create one node with a random alias.
138 $default_node = $this->drupalCreateNode([
139 'type' => 'moderated',
141 'moderation_state' => 'published',
142 'path' => '/' . $this->randomMachineName(),
145 // Add published translation with another alias.
146 $this->drupalGet('node/' . $default_node->id());
147 $this->drupalGet('node/' . $default_node->id() . '/translations');
148 $this->clickLink('Add');
149 $edit_translation = [
150 'body[0][value]' => $this->randomMachineName(),
151 'moderation_state[0][state]' => 'published',
152 'path[0][alias]' => '/' . $this->randomMachineName(),
154 $this->drupalPostForm(NULL, $edit_translation, 'Save (this translation)');
155 // Confirm that the alias works.
156 $this->drupalGet('fr' . $edit_translation['path[0][alias]']);
157 $this->assertSession()->pageTextContains($edit_translation['body[0][value]']);
159 $default_path = $default_node->path->alias;
160 $translation_path = 'fr' . $edit_translation['path[0][alias]'];
162 $this->assertPathsAreAccessible([$default_path, $translation_path]);
164 // Try to create new draft revision for translation with a new path alias.
165 $edit_new_translation_draft_with_alias = [
166 'moderation_state[0][state]' => 'draft',
167 'path[0][alias]' => '/' . $this->randomMachineName(),
169 $this->drupalPostForm('fr/node/' . $default_node->id() . '/edit', $edit_new_translation_draft_with_alias, 'Save (this translation)');
170 // Confirm the expected error.
171 $this->assertSession()->pageTextContains('You can only change the URL alias for the published version of this content.');
173 // Create new draft revision for translation without changing path alias.
174 $edit_new_translation_draft = [
175 'body[0][value]' => $this->randomMachineName(),
176 'moderation_state[0][state]' => 'draft',
178 $this->drupalPostForm('fr/node/' . $default_node->id() . '/edit', $edit_new_translation_draft, t('Save (this translation)'));
179 // Confirm that the new draft revision was created.
180 $this->assertSession()->pageTextNotContains('You can only change the URL alias for the published version of this content.');
181 $this->assertSession()->pageTextContains($edit_new_translation_draft['body[0][value]']);
182 $this->assertPathsAreAccessible([$default_path, $translation_path]);
184 // Try to create a new draft revision for translation with path alias from
185 // the original language's default revision.
186 $edit_new_translation_draft_with_defaults_alias = [
187 'moderation_state[0][state]' => 'draft',
188 'path[0][alias]' => $default_node->path->alias,
190 $this->drupalPostForm('fr/node/' . $default_node->id() . '/edit', $edit_new_translation_draft_with_defaults_alias, 'Save (this translation)');
191 // Verify the expected error.
192 $this->assertSession()->pageTextContains('You can only change the URL alias for the published version of this content.');
194 // Try to create new draft revision for translation with deleted (empty)
196 $edit_new_translation_draft_empty_alias = [
197 'body[0][value]' => $this->randomMachineName(),
198 'moderation_state[0][state]' => 'draft',
199 'path[0][alias]' => '',
201 $this->drupalPostForm('fr/node/' . $default_node->id() . '/edit', $edit_new_translation_draft_empty_alias, 'Save (this translation)');
202 // Confirm the expected error.
203 $this->assertSession()->pageTextContains('You can only change the URL alias for the published version of this content.');
205 // Create new default (published) revision for translation with new path
207 $edit_new_translation = [
208 'body[0][value]' => $this->randomMachineName(),
209 'moderation_state[0][state]' => 'published',
210 'path[0][alias]' => '/' . $this->randomMachineName(),
212 $this->drupalPostForm('fr/node/' . $default_node->id() . '/edit', $edit_new_translation, 'Save (this translation)');
213 // Confirm that the new published revision was created.
214 $this->assertSession()->pageTextNotContains('You can only change the URL alias for the published version of this content.');
215 $this->assertSession()->pageTextContains($edit_new_translation['body[0][value]']);
216 $this->assertSession()->addressEquals('fr' . $edit_new_translation['path[0][alias]']);
217 $this->assertPathsAreAccessible([$default_path]);
221 * Helper callback to verify paths are responding with status 200.
223 * @param string[] $paths
224 * An array of paths to check for.
226 public function assertPathsAreAccessible(array $paths) {
227 foreach ($paths as $path) {
228 $this->drupalGet($path);
229 $this->assertSession()->statusCodeEquals(200);