3 namespace Drupal\comment\Tests;
5 use Drupal\user\RoleInterface;
6 use Drupal\comment\Entity\Comment;
9 * Tests comment approval functionality.
13 class CommentAdminTest extends CommentTestBase {
15 protected function setUp() {
18 $this->drupalPlaceBlock('page_title_block');
22 * Test comment approval functionality through admin/content/comment.
24 public function testApprovalAdminInterface() {
25 // Set anonymous comments to require approval.
26 user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
27 'access comments' => TRUE,
28 'post comments' => TRUE,
29 'skip comment approval' => FALSE,
31 $this->drupalLogin($this->adminUser);
32 $this->setCommentAnonymous('0'); // Ensure that doesn't require contact info.
34 // Test that the comments page loads correctly when there are no comments
35 $this->drupalGet('admin/content/comment');
36 $this->assertText(t('No comments available.'));
38 $this->drupalLogout();
40 // Post anonymous comment without contact info.
41 $subject = $this->randomMachineName();
42 $body = $this->randomMachineName();
43 $this->postComment($this->node, $body, $subject, TRUE); // Set $contact to true so that it won't check for id and message.
44 $this->assertText(t('Your comment has been queued for review by site administrators and will be published after approval.'), 'Comment requires approval.');
46 // Get unapproved comment id.
47 $this->drupalLogin($this->adminUser);
48 $anonymous_comment4 = $this->getUnapprovedComment($subject);
49 $anonymous_comment4 = Comment::create([
50 'cid' => $anonymous_comment4,
51 'subject' => $subject,
52 'comment_body' => $body,
53 'entity_id' => $this->node->id(),
54 'entity_type' => 'node',
55 'field_name' => 'comment'
57 $this->drupalLogout();
59 $this->assertFalse($this->commentExists($anonymous_comment4), 'Anonymous comment was not published.');
62 $this->drupalLogin($this->adminUser);
63 $this->performCommentOperation($anonymous_comment4, 'publish', TRUE);
64 $this->drupalLogout();
66 $this->drupalGet('node/' . $this->node->id());
67 $this->assertTrue($this->commentExists($anonymous_comment4), 'Anonymous comment visible.');
69 // Post 2 anonymous comments without contact info.
70 $comments[] = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
71 $comments[] = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
73 // Publish multiple comments in one operation.
74 $this->drupalLogin($this->adminUser);
75 $this->drupalGet('admin/content/comment/approval');
76 $this->assertText(t('Unapproved comments (@count)', ['@count' => 2]), 'Two unapproved comments waiting for approval.');
78 "comments[{$comments[0]->id()}]" => 1,
79 "comments[{$comments[1]->id()}]" => 1,
81 $this->drupalPostForm(NULL, $edit, t('Update'));
82 $this->assertText(t('Unapproved comments (@count)', ['@count' => 0]), 'All comments were approved.');
84 // Delete multiple comments in one operation.
86 'operation' => 'delete',
87 "comments[{$comments[0]->id()}]" => 1,
88 "comments[{$comments[1]->id()}]" => 1,
89 "comments[{$anonymous_comment4->id()}]" => 1,
91 $this->drupalPostForm(NULL, $edit, t('Update'));
92 $this->assertText(t('Are you sure you want to delete these comments and all their children?'), 'Confirmation required.');
93 $this->drupalPostForm(NULL, $edit, t('Delete comments'));
94 $this->assertText(t('No comments available.'), 'All comments were deleted.');
95 // Test message when no comments selected.
97 'operation' => 'delete',
99 $this->drupalPostForm(NULL, $edit, t('Update'));
100 $this->assertText(t('Select one or more comments to perform the update on.'));
104 * Tests comment approval functionality through the node interface.
106 public function testApprovalNodeInterface() {
107 // Set anonymous comments to require approval.
108 user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
109 'access comments' => TRUE,
110 'post comments' => TRUE,
111 'skip comment approval' => FALSE,
113 $this->drupalLogin($this->adminUser);
114 $this->setCommentAnonymous('0'); // Ensure that doesn't require contact info.
115 $this->drupalLogout();
117 // Post anonymous comment without contact info.
118 $subject = $this->randomMachineName();
119 $body = $this->randomMachineName();
120 $this->postComment($this->node, $body, $subject, TRUE); // Set $contact to true so that it won't check for id and message.
121 $this->assertText(t('Your comment has been queued for review by site administrators and will be published after approval.'), 'Comment requires approval.');
123 // Get unapproved comment id.
124 $this->drupalLogin($this->adminUser);
125 $anonymous_comment4 = $this->getUnapprovedComment($subject);
126 $anonymous_comment4 = Comment::create([
127 'cid' => $anonymous_comment4,
128 'subject' => $subject,
129 'comment_body' => $body,
130 'entity_id' => $this->node->id(),
131 'entity_type' => 'node',
132 'field_name' => 'comment'
134 $this->drupalLogout();
136 $this->assertFalse($this->commentExists($anonymous_comment4), 'Anonymous comment was not published.');
139 $this->drupalLogin($this->adminUser);
140 $this->drupalGet('comment/1/approve');
141 $this->assertResponse(403, 'Forged comment approval was denied.');
142 $this->drupalGet('comment/1/approve', ['query' => ['token' => 'forged']]);
143 $this->assertResponse(403, 'Forged comment approval was denied.');
144 $this->drupalGet('comment/1/edit');
145 $this->assertFieldChecked('edit-status-0');
146 $this->drupalGet('node/' . $this->node->id());
147 $this->clickLink(t('Approve'));
148 $this->drupalLogout();
150 $this->drupalGet('node/' . $this->node->id());
151 $this->assertTrue($this->commentExists($anonymous_comment4), 'Anonymous comment visible.');
155 * Tests comment bundle admin.
157 public function testCommentAdmin() {
159 $this->drupalLogin($this->adminUser);
160 // Browse to comment bundle overview.
161 $this->drupalGet('admin/structure/comment');
162 $this->assertResponse(200);
163 // Make sure titles visible.
164 $this->assertText('Comment type');
165 $this->assertText('Description');
166 // Make sure the description is present.
167 $this->assertText('Default comment field');
169 $this->clickLink('Manage fields');
170 $this->assertResponse(200);
171 // Make sure comment_body field is shown.
172 $this->assertText('comment_body');
173 // Rest from here on in is field_ui.
177 * Tests editing a comment as an admin.
179 public function testEditComment() {
180 // Enable anonymous user comments.
181 user_role_grant_permissions(RoleInterface::ANONYMOUS_ID, [
184 'skip comment approval',
187 // Log in as a web user.
188 $this->drupalLogin($this->webUser);
190 $comment = $this->postComment($this->node, $this->randomMachineName());
192 $this->drupalLogout();
194 // Post anonymous comment.
195 $this->drupalLogin($this->adminUser);
196 $this->setCommentAnonymous('2'); // Ensure that we need email id before posting comment.
197 $this->drupalLogout();
199 // Post comment with contact info (required).
200 $author_name = $this->randomMachineName();
201 $author_mail = $this->randomMachineName() . '@example.com';
202 $anonymous_comment = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), ['name' => $author_name, 'mail' => $author_mail]);
204 // Log in as an admin user.
205 $this->drupalLogin($this->adminUser);
207 // Make sure the comment field is not visible when
208 // the comment was posted by an authenticated user.
209 $this->drupalGet('comment/' . $comment->id() . '/edit');
210 $this->assertNoFieldById('edit-mail', $comment->getAuthorEmail());
212 // Make sure the comment field is visible when
213 // the comment was posted by an anonymous user.
214 $this->drupalGet('comment/' . $anonymous_comment->id() . '/edit');
215 $this->assertFieldById('edit-mail', $anonymous_comment->getAuthorEmail());