3 namespace Drupal\Tests\content_moderation\Functional;
5 use Drupal\workflows\Entity\Workflow;
8 * Tests the moderation form, specifically on nodes.
10 * @group content_moderation
12 class ModerationFormTest extends ModerationStateTestBase {
17 protected function setUp() {
19 $this->drupalLogin($this->adminUser);
20 $this->createContentTypeFromUi('Moderated content', 'moderated_content', TRUE);
21 $this->grantUserPermissionToCreateContentOfType($this->adminUser, 'moderated_content');
25 * Tests the moderation form that shows on the latest version page.
27 * The latest version page only shows if there is a forward revision. There
28 * is only a forward revision if a draft revision is created on a node where
29 * the default revision is not a published moderation state.
31 * @see \Drupal\content_moderation\EntityOperations
32 * @see \Drupal\Tests\content_moderation\Functional\ModerationStateBlockTest::testCustomBlockModeration
34 public function testModerationForm() {
35 // Create new moderated content in draft.
36 $this->drupalPostForm('node/add/moderated_content', [
37 'title[0][value]' => 'Some moderated content',
38 'body[0][value]' => 'First version of the content.',
39 ], t('Save and Create New Draft'));
41 $node = $this->drupalGetNodeByTitle('Some moderated content');
42 $canonical_path = sprintf('node/%d', $node->id());
43 $edit_path = sprintf('node/%d/edit', $node->id());
44 $latest_version_path = sprintf('node/%d/latest', $node->id());
46 $this->assertTrue($this->adminUser->hasPermission('edit any moderated_content content'));
48 // The canonical view should have a moderation form, because it is not the
50 $this->drupalGet($canonical_path);
51 $this->assertResponse(200);
52 $this->assertField('edit-new-state', 'The node view page has a moderation form.');
54 // The latest version page should not show, because there is no forward
56 $this->drupalGet($latest_version_path);
57 $this->assertResponse(403);
60 $this->drupalPostForm($edit_path, [
61 'body[0][value]' => 'Second version of the content.',
62 ], t('Save and Create New Draft'));
64 // The canonical view should have a moderation form, because it is not the
66 $this->drupalGet($canonical_path);
67 $this->assertResponse(200);
68 $this->assertField('edit-new-state', 'The node view page has a moderation form.');
70 // The latest version page should not show, because there is still no
72 $this->drupalGet($latest_version_path);
73 $this->assertResponse(403);
76 $this->drupalPostForm($edit_path, [
77 'body[0][value]' => 'Third version of the content.',
78 ], t('Save and Publish'));
80 // The published view should not have a moderation form, because it is the
82 $this->drupalGet($canonical_path);
83 $this->assertResponse(200);
84 $this->assertNoField('edit-new-state', 'The node view page has no moderation form.');
86 // The latest version page should not show, because there is still no
88 $this->drupalGet($latest_version_path);
89 $this->assertResponse(403);
91 // Make a forward revision.
92 $this->drupalPostForm($edit_path, [
93 'body[0][value]' => 'Fourth version of the content.',
94 ], t('Save and Create New Draft'));
96 // The published view should not have a moderation form, because it is the
98 $this->drupalGet($canonical_path);
99 $this->assertResponse(200);
100 $this->assertNoField('edit-new-state', 'The node view page has no moderation form.');
102 // The latest version page should show the moderation form and have "Draft"
103 // status, because the forward revision is in "Draft".
104 $this->drupalGet($latest_version_path);
105 $this->assertResponse(200);
106 $this->assertField('edit-new-state', 'The latest-version page has a moderation form.');
107 $this->assertText('Draft', 'Correct status found on the latest-version page.');
109 // Submit the moderation form to change status to published.
110 $this->drupalPostForm($latest_version_path, [
111 'new_state' => 'published',
114 // The latest version page should not show, because there is no
116 $this->drupalGet($latest_version_path);
117 $this->assertResponse(403);
121 * Test moderation non-bundle entity type.
123 public function testNonBundleModerationForm() {
124 $this->drupalLogin($this->rootUser);
125 $workflow = Workflow::load('editorial');
126 $workflow->getTypePlugin()->addEntityTypeAndBundle('entity_test_mulrevpub', 'entity_test_mulrevpub');
129 // Create new moderated content in draft.
130 $this->drupalPostForm('entity_test_mulrevpub/add', [], t('Save and Create New Draft'));
132 // The latest version page should not show, because there is no forward
134 $this->drupalGet('/entity_test_mulrevpub/manage/1/latest');
135 $this->assertResponse(403);
138 $this->drupalPostForm('entity_test_mulrevpub/manage/1/edit', [], t('Save and Create New Draft'));
140 // The latest version page should not show, because there is still no
142 $this->drupalGet('/entity_test_mulrevpub/manage/1/latest');
143 $this->assertResponse(403);
145 // Publish the draft.
146 $this->drupalPostForm('entity_test_mulrevpub/manage/1/edit', [], t('Save and Publish'));
148 // The published view should not have a moderation form, because it is the
150 $this->drupalGet('entity_test_mulrevpub/manage/1');
151 $this->assertResponse(200);
152 $this->assertNoText('Status', 'The node view page has no moderation form.');
154 // The latest version page should not show, because there is still no
156 $this->drupalGet('entity_test_mulrevpub/manage/1/latest');
157 $this->assertResponse(403);
159 // Make a forward revision.
160 $this->drupalPostForm('entity_test_mulrevpub/manage/1/edit', [], t('Save and Create New Draft'));
162 // The published view should not have a moderation form, because it is the
164 $this->drupalGet('entity_test_mulrevpub/manage/1');
165 $this->assertResponse(200);
166 $this->assertNoText('Status', 'The node view page has no moderation form.');
168 // The latest version page should show the moderation form and have "Draft"
169 // status, because the forward revision is in "Draft".
170 $this->drupalGet('entity_test_mulrevpub/manage/1/latest');
171 $this->assertResponse(200);
172 $this->assertText('Status', 'Form text found on the latest-version page.');
173 $this->assertText('Draft', 'Correct status found on the latest-version page.');
175 // Submit the moderation form to change status to published.
176 $this->drupalPostForm('entity_test_mulrevpub/manage/1/latest', [
177 'new_state' => 'published',
180 // The latest version page should not show, because there is no
182 $this->drupalGet('entity_test_mulrevpub/manage/1/latest');
183 $this->assertResponse(403);
187 * Tests the revision author is updated when the moderation form is used.
189 public function testModerationFormSetsRevisionAuthor() {
190 // Create new moderated content in published.
191 $node = $this->createNode(['type' => 'moderated_content', 'moderation_state' => 'published']);
192 // Make a forward revision.
193 $node->title = $this->randomMachineName();
194 $node->moderation_state->value = 'draft';
197 $another_user = $this->drupalCreateUser($this->permissions);
198 $this->grantUserPermissionToCreateContentOfType($another_user, 'moderated_content');
199 $this->drupalLogin($another_user);
200 $this->drupalPostForm(sprintf('node/%d/latest', $node->id()), [
201 'new_state' => 'published',
204 $this->drupalGet(sprintf('node/%d/revisions', $node->id()));
205 $this->assertText('by ' . $another_user->getAccountName());