3 namespace Drupal\Tests\comment\Functional;
5 use Drupal\Component\Render\FormattableMarkup;
6 use Drupal\Component\Utility\Html;
7 use Drupal\language\Entity\ConfigurableLanguage;
8 use Drupal\user\RoleInterface;
9 use Drupal\comment\Entity\Comment;
12 * Tests comment approval functionality.
16 class CommentAdminTest extends CommentTestBase {
18 protected function setUp() {
21 $this->drupalPlaceBlock('page_title_block');
25 * Test comment approval functionality through admin/content/comment.
27 public function testApprovalAdminInterface() {
28 // Set anonymous comments to require approval.
29 user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
30 'access comments' => TRUE,
31 'post comments' => TRUE,
32 'skip comment approval' => FALSE,
34 $this->drupalLogin($this->adminUser);
35 // Ensure that doesn't require contact info.
36 $this->setCommentAnonymous('0');
38 // Test that the comments page loads correctly when there are no comments
39 $this->drupalGet('admin/content/comment');
40 $this->assertText(t('No comments available.'));
42 $this->drupalLogout();
44 // Post anonymous comment without contact info.
45 $subject = $this->randomMachineName();
46 $body = $this->randomMachineName();
47 // Set $contact to true so that it won't check for id and message.
48 $this->postComment($this->node, $body, $subject, TRUE);
49 $this->assertText(t('Your comment has been queued for review by site administrators and will be published after approval.'), 'Comment requires approval.');
51 // Get unapproved comment id.
52 $this->drupalLogin($this->adminUser);
53 $anonymous_comment4 = $this->getUnapprovedComment($subject);
54 $anonymous_comment4 = Comment::create([
55 'cid' => $anonymous_comment4,
56 'subject' => $subject,
57 'comment_body' => $body,
58 'entity_id' => $this->node->id(),
59 'entity_type' => 'node',
60 'field_name' => 'comment',
62 $this->drupalLogout();
64 $this->assertFalse($this->commentExists($anonymous_comment4), 'Anonymous comment was not published.');
67 $this->drupalLogin($this->adminUser);
68 $this->performCommentOperation($anonymous_comment4, 'publish', TRUE);
69 $this->drupalLogout();
71 $this->drupalGet('node/' . $this->node->id());
72 $this->assertTrue($this->commentExists($anonymous_comment4), 'Anonymous comment visible.');
74 // Post 2 anonymous comments without contact info.
75 $comments[] = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
76 $comments[] = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
78 // Publish multiple comments in one operation.
79 $this->drupalLogin($this->adminUser);
80 $this->drupalGet('admin/content/comment/approval');
81 $this->assertText(t('Unapproved comments (@count)', ['@count' => 2]), 'Two unapproved comments waiting for approval.');
83 "comments[{$comments[0]->id()}]" => 1,
84 "comments[{$comments[1]->id()}]" => 1,
86 $this->drupalPostForm(NULL, $edit, t('Update'));
87 $this->assertText(t('Unapproved comments (@count)', ['@count' => 0]), 'All comments were approved.');
89 // Delete multiple comments in one operation.
91 'operation' => 'delete',
92 "comments[{$comments[0]->id()}]" => 1,
93 "comments[{$comments[1]->id()}]" => 1,
94 "comments[{$anonymous_comment4->id()}]" => 1,
96 $this->drupalPostForm(NULL, $edit, t('Update'));
97 $this->assertText(t('Are you sure you want to delete these comments and all their children?'), 'Confirmation required.');
98 $this->drupalPostForm(NULL, [], t('Delete'));
99 $this->assertText(t('No comments available.'), 'All comments were deleted.');
100 // Test message when no comments selected.
102 'operation' => 'delete',
104 $this->drupalPostForm(NULL, $edit, t('Update'));
105 $this->assertText(t('Select one or more comments to perform the update on.'));
107 // Make sure the label of unpublished node is not visible on listing page.
108 $this->drupalGet('admin/content/comment');
109 $this->postComment($this->node, $this->randomMachineName());
110 $this->drupalGet('admin/content/comment');
111 $this->assertText(Html::escape($this->node->label()));
112 $this->node->setUnpublished()->save();
113 $this->drupalGet('admin/content/comment');
114 $this->assertNoText(Html::escape($this->node->label()));
118 * Tests comment approval functionality through the node interface.
120 public function testApprovalNodeInterface() {
121 // Set anonymous comments to require approval.
122 user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
123 'access comments' => TRUE,
124 'post comments' => TRUE,
125 'skip comment approval' => FALSE,
127 $this->drupalLogin($this->adminUser);
128 // Ensure that doesn't require contact info.
129 $this->setCommentAnonymous('0');
130 $this->drupalLogout();
132 // Post anonymous comment without contact info.
133 $subject = $this->randomMachineName();
134 $body = $this->randomMachineName();
135 // Set $contact to true so that it won't check for id and message.
136 $this->postComment($this->node, $body, $subject, TRUE);
137 $this->assertText(t('Your comment has been queued for review by site administrators and will be published after approval.'), 'Comment requires approval.');
139 // Get unapproved comment id.
140 $this->drupalLogin($this->adminUser);
141 $anonymous_comment4 = $this->getUnapprovedComment($subject);
142 $anonymous_comment4 = Comment::create([
143 'cid' => $anonymous_comment4,
144 'subject' => $subject,
145 'comment_body' => $body,
146 'entity_id' => $this->node->id(),
147 'entity_type' => 'node',
148 'field_name' => 'comment',
150 $this->drupalLogout();
152 $this->assertFalse($this->commentExists($anonymous_comment4), 'Anonymous comment was not published.');
155 $this->drupalLogin($this->adminUser);
156 $this->drupalGet('comment/1/approve');
157 $this->assertResponse(403, 'Forged comment approval was denied.');
158 $this->drupalGet('comment/1/approve', ['query' => ['token' => 'forged']]);
159 $this->assertResponse(403, 'Forged comment approval was denied.');
160 $this->drupalGet('comment/1/edit');
161 $this->assertFieldChecked('edit-status-0');
162 $this->drupalGet('node/' . $this->node->id());
163 $this->clickLink(t('Approve'));
164 $this->drupalLogout();
166 $this->drupalGet('node/' . $this->node->id());
167 $this->assertTrue($this->commentExists($anonymous_comment4), 'Anonymous comment visible.');
171 * Tests comment bundle admin.
173 public function testCommentAdmin() {
175 $this->drupalLogin($this->adminUser);
176 // Browse to comment bundle overview.
177 $this->drupalGet('admin/structure/comment');
178 $this->assertResponse(200);
179 // Make sure titles visible.
180 $this->assertText('Comment type');
181 $this->assertText('Description');
182 // Make sure the description is present.
183 $this->assertText('Default comment field');
185 $this->clickLink('Manage fields');
186 $this->assertResponse(200);
187 // Make sure comment_body field is shown.
188 $this->assertText('comment_body');
189 // Rest from here on in is field_ui.
193 * Tests editing a comment as an admin.
195 public function testEditComment() {
196 // Enable anonymous user comments.
197 user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
200 'skip comment approval',
203 // Log in as a web user.
204 $this->drupalLogin($this->webUser);
206 $comment = $this->postComment($this->node, $this->randomMachineName());
208 $this->drupalLogout();
210 // Post anonymous comment.
211 $this->drupalLogin($this->adminUser);
212 // Ensure that we need email id before posting comment.
213 $this->setCommentAnonymous('2');
214 $this->drupalLogout();
216 // Post comment with contact info (required).
217 $author_name = $this->randomMachineName();
218 $author_mail = $this->randomMachineName() . '@example.com';
219 $anonymous_comment = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), ['name' => $author_name, 'mail' => $author_mail]);
221 // Log in as an admin user.
222 $this->drupalLogin($this->adminUser);
224 // Make sure the comment field is not visible when
225 // the comment was posted by an authenticated user.
226 $this->drupalGet('comment/' . $comment->id() . '/edit');
227 $this->assertNoFieldById('edit-mail', $comment->getAuthorEmail());
229 // Make sure the comment field is visible when
230 // the comment was posted by an anonymous user.
231 $this->drupalGet('comment/' . $anonymous_comment->id() . '/edit');
232 $this->assertFieldById('edit-mail', $anonymous_comment->getAuthorEmail());
236 * Tests commented translation deletion admin view.
238 public function testCommentedTranslationDeletion() {
239 \Drupal::service('module_installer')->install([
243 \Drupal::service('router.builder')->rebuildIfNeeded();
245 ConfigurableLanguage::createFromLangcode('ur')->save();
246 // Rebuild the container to update the default language container variable.
247 $this->rebuildContainer();
248 // Ensure that doesn't require contact info.
249 $this->setCommentAnonymous('0');
250 $this->drupalLogin($this->webUser);
251 $count_query = \Drupal::entityTypeManager()
252 ->getStorage('comment')
255 $before_count = $count_query->execute();
256 // Post 2 anonymous comments without contact info.
257 $comment1 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
258 $comment2 = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
260 $comment1->addTranslation('ur', ['subject' => 'ur ' . $comment1->label()])
262 $comment2->addTranslation('ur', ['subject' => 'ur ' . $comment1->label()])
264 $this->drupalLogout();
265 $this->drupalLogin($this->adminUser);
266 // Delete multiple comments in one operation.
268 'operation' => 'delete',
269 "comments[{$comment1->id()}]" => 1,
270 "comments[{$comment2->id()}]" => 1,
272 $this->drupalPostForm('admin/content/comment', $edit, t('Update'));
273 $this->assertRaw(new FormattableMarkup('@label (Original translation) - <em>The following comment translations will be deleted:</em>', ['@label' => $comment1->label()]));
274 $this->assertRaw(new FormattableMarkup('@label (Original translation) - <em>The following comment translations will be deleted:</em>', ['@label' => $comment2->label()]));
275 $this->assertText('English');
276 $this->assertText('Urdu');
277 $this->drupalPostForm(NULL, [], t('Delete'));
278 $after_count = $count_query->execute();
279 $this->assertEqual($after_count, $before_count, 'No comment or translation found.');