3882c764f191193c197e0213246cc9bbf45e6e9e
[yaffs-website] / web / core / modules / content_moderation / tests / src / Functional / ModerationFormTest.php
1 <?php
2
3 namespace Drupal\Tests\content_moderation\Functional;
4
5 use Drupal\workflows\Entity\Workflow;
6
7 /**
8  * Tests the moderation form, specifically on nodes.
9  *
10  * @group content_moderation
11  */
12 class ModerationFormTest extends ModerationStateTestBase {
13
14   /**
15    * {@inheritdoc}
16    */
17   protected function setUp() {
18     parent::setUp();
19     $this->drupalLogin($this->adminUser);
20     $this->createContentTypeFromUi('Moderated content', 'moderated_content', TRUE);
21     $this->grantUserPermissionToCreateContentOfType($this->adminUser, 'moderated_content');
22   }
23
24   /**
25    * Tests the moderation form that shows on the latest version page.
26    *
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.
30    *
31    * @see \Drupal\content_moderation\EntityOperations
32    * @see \Drupal\Tests\content_moderation\Functional\ModerationStateBlockTest::testCustomBlockModeration
33    */
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'));
40
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());
45
46     $this->assertTrue($this->adminUser->hasPermission('edit any moderated_content content'));
47
48     // The canonical view should have a moderation form, because it is not the
49     // live revision.
50     $this->drupalGet($canonical_path);
51     $this->assertResponse(200);
52     $this->assertField('edit-new-state', 'The node view page has a moderation form.');
53
54     // The latest version page should not show, because there is no forward
55     // revision.
56     $this->drupalGet($latest_version_path);
57     $this->assertResponse(403);
58
59     // Update the draft.
60     $this->drupalPostForm($edit_path, [
61       'body[0][value]' => 'Second version of the content.',
62     ], t('Save and Create New Draft'));
63
64     // The canonical view should have a moderation form, because it is not the
65     // live revision.
66     $this->drupalGet($canonical_path);
67     $this->assertResponse(200);
68     $this->assertField('edit-new-state', 'The node view page has a moderation form.');
69
70     // The latest version page should not show, because there is still no
71     // forward revision.
72     $this->drupalGet($latest_version_path);
73     $this->assertResponse(403);
74
75     // Publish the draft.
76     $this->drupalPostForm($edit_path, [
77       'body[0][value]' => 'Third version of the content.',
78     ], t('Save and Publish'));
79
80     // The published view should not have a moderation form, because it is the
81     // live revision.
82     $this->drupalGet($canonical_path);
83     $this->assertResponse(200);
84     $this->assertNoField('edit-new-state', 'The node view page has no moderation form.');
85
86     // The latest version page should not show, because there is still no
87     // forward revision.
88     $this->drupalGet($latest_version_path);
89     $this->assertResponse(403);
90
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'));
95
96     // The published view should not have a moderation form, because it is the
97     // live revision.
98     $this->drupalGet($canonical_path);
99     $this->assertResponse(200);
100     $this->assertNoField('edit-new-state', 'The node view page has no moderation form.');
101
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.');
108
109     // Submit the moderation form to change status to published.
110     $this->drupalPostForm($latest_version_path, [
111       'new_state' => 'published',
112     ], t('Apply'));
113
114     // The latest version page should not show, because there is no
115     // forward revision.
116     $this->drupalGet($latest_version_path);
117     $this->assertResponse(403);
118   }
119
120   /**
121    * Test moderation non-bundle entity type.
122    */
123   public function testNonBundleModerationForm() {
124     $this->drupalLogin($this->rootUser);
125     $workflow = Workflow::load('editorial');
126     $workflow->getTypePlugin()->addEntityTypeAndBundle('entity_test_mulrevpub', 'entity_test_mulrevpub');
127     $workflow->save();
128
129     // Create new moderated content in draft.
130     $this->drupalPostForm('entity_test_mulrevpub/add', [], t('Save and Create New Draft'));
131
132     // The latest version page should not show, because there is no forward
133     // revision.
134     $this->drupalGet('/entity_test_mulrevpub/manage/1/latest');
135     $this->assertResponse(403);
136
137     // Update the draft.
138     $this->drupalPostForm('entity_test_mulrevpub/manage/1/edit', [], t('Save and Create New Draft'));
139
140     // The latest version page should not show, because there is still no
141     // forward revision.
142     $this->drupalGet('/entity_test_mulrevpub/manage/1/latest');
143     $this->assertResponse(403);
144
145     // Publish the draft.
146     $this->drupalPostForm('entity_test_mulrevpub/manage/1/edit', [], t('Save and Publish'));
147
148     // The published view should not have a moderation form, because it is the
149     // default revision.
150     $this->drupalGet('entity_test_mulrevpub/manage/1');
151     $this->assertResponse(200);
152     $this->assertNoText('Status', 'The node view page has no moderation form.');
153
154     // The latest version page should not show, because there is still no
155     // forward revision.
156     $this->drupalGet('entity_test_mulrevpub/manage/1/latest');
157     $this->assertResponse(403);
158
159     // Make a forward revision.
160     $this->drupalPostForm('entity_test_mulrevpub/manage/1/edit', [], t('Save and Create New Draft'));
161
162     // The published view should not have a moderation form, because it is the
163     // default revision.
164     $this->drupalGet('entity_test_mulrevpub/manage/1');
165     $this->assertResponse(200);
166     $this->assertNoText('Status', 'The node view page has no moderation form.');
167
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.');
174
175     // Submit the moderation form to change status to published.
176     $this->drupalPostForm('entity_test_mulrevpub/manage/1/latest', [
177       'new_state' => 'published',
178     ], t('Apply'));
179
180     // The latest version page should not show, because there is no
181     // forward revision.
182     $this->drupalGet('entity_test_mulrevpub/manage/1/latest');
183     $this->assertResponse(403);
184   }
185
186   /**
187    * Tests the revision author is updated when the moderation form is used.
188    */
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';
195     $node->save();
196
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',
202     ], t('Apply'));
203
204     $this->drupalGet(sprintf('node/%d/revisions', $node->id()));
205     $this->assertText('by ' . $another_user->getAccountName());
206   }
207
208 }