3 namespace Drupal\Tests\comment\Functional\Views;
5 use Drupal\block_content\Entity\BlockContent;
6 use Drupal\block_content\Entity\BlockContentType;
7 use Drupal\comment\Entity\Comment;
8 use Drupal\comment\Plugin\Field\FieldType\CommentItemInterface;
9 use Drupal\Component\Render\FormattableMarkup;
10 use Drupal\Tests\comment\Functional\CommentTestBase as CommentBrowserTestBase;
11 use Drupal\Component\Utility\Html;
12 use Drupal\Component\Utility\Unicode;
13 use Drupal\user\RoleInterface;
14 use Drupal\views\Views;
17 * Tests comment approval functionality.
21 class CommentAdminTest extends CommentBrowserTestBase {
26 protected function setUp() {
28 \Drupal::service('module_installer')->install(['views']);
29 $view = Views::getView('comment');
30 $view->storage->enable()->save();
31 \Drupal::service('router.builder')->rebuildIfNeeded();
35 * Test comment approval functionality through admin/content/comment.
37 public function testApprovalAdminInterface() {
38 // Set anonymous comments to require approval.
39 user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
40 'access comments' => TRUE,
41 'post comments' => TRUE,
42 'skip comment approval' => FALSE,
44 $this->drupalPlaceBlock('page_title_block');
45 $this->drupalLogin($this->adminUser);
46 // Ensure that doesn't require contact info.
47 $this->setCommentAnonymous('0');
49 // Test that the comments page loads correctly when there are no comments.
50 $this->drupalGet('admin/content/comment');
51 $this->assertText(t('No comments available.'));
53 // Assert the expose filters on the admin page.
54 $this->assertField('subject');
55 $this->assertField('author_name');
56 $this->assertField('langcode');
58 $this->drupalLogout();
60 // Post anonymous comment without contact info.
61 $body = $this->getRandomGenerator()->sentences(4);
62 $subject = Unicode::truncate(trim(Html::decodeEntities(strip_tags($body))), 29, TRUE, TRUE);
63 $author_name = $this->randomMachineName();
64 $this->drupalPostForm('comment/reply/node/' . $this->node->id() . '/comment', [
65 'name' => $author_name,
66 'comment_body[0][value]' => $body,
68 $this->assertText(t('Your comment has been queued for review by site administrators and will be published after approval.'), 'Comment requires approval.');
70 // Get unapproved comment id.
71 $this->drupalLogin($this->adminUser);
72 $anonymous_comment4 = $this->getUnapprovedComment($subject);
73 $anonymous_comment4 = Comment::create([
74 'cid' => $anonymous_comment4,
75 'subject' => $subject,
76 'comment_body' => $body,
77 'entity_id' => $this->node->id(),
78 'entity_type' => 'node',
79 'field_name' => 'comment',
81 $this->drupalLogout();
83 $this->assertFalse($this->commentExists($anonymous_comment4), 'Anonymous comment was not published.');
86 $this->drupalLogin($this->adminUser);
88 $edit['action'] = 'comment_publish_action';
89 $edit['comment_bulk_form[0]'] = $anonymous_comment4->id();
90 $this->drupalPostForm('admin/content/comment/approval', $edit, t('Apply to selected items'));
92 $this->assertText('Publish comment was applied to 1 item.', new FormattableMarkup('Operation "@operation" was performed on comment.', ['@operation' => 'publish']));
93 $this->drupalLogout();
95 $this->drupalGet('node/' . $this->node->id());
96 $this->assertTrue($this->commentExists($anonymous_comment4), 'Anonymous comment visible.');
98 // Post 2 anonymous comments without contact info.
99 $comments[] = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
100 $comments[] = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
102 // Publish multiple comments in one operation.
103 $this->drupalLogin($this->adminUser);
104 $this->drupalGet('admin/content/comment/approval');
105 $this->assertText(t('Unapproved comments (@count)', ['@count' => 2]), 'Two unapproved comments waiting for approval.');
107 // Assert the expose filters on the admin page.
108 $this->assertField('subject');
109 $this->assertField('author_name');
110 $this->assertField('langcode');
113 "action" => 'comment_publish_action',
114 "comment_bulk_form[1]" => $comments[0]->id(),
115 "comment_bulk_form[0]" => $comments[1]->id(),
117 $this->drupalPostForm(NULL, $edit, t('Apply to selected items'));
118 $this->assertText(t('Unapproved comments (@count)', ['@count' => 0]), 'All comments were approved.');
120 // Test message when no comments selected.
121 $this->drupalPostForm('admin/content/comment', [], t('Apply to selected items'));
122 $this->assertText(t('Select one or more comments to perform the update on.'));
124 $subject_link = $this->xpath('//table/tbody/tr/td/a[contains(@href, :href) and contains(@title, :title) and text()=:text]', [
125 ':href' => $comments[0]->permalink()->toString(),
126 ':title' => Unicode::truncate($comments[0]->get('comment_body')->value, 128),
127 ':text' => $comments[0]->getSubject(),
129 $this->assertTrue(!empty($subject_link), 'Comment listing shows the correct subject link.');
130 $this->assertText($author_name . ' (not verified)', 'Anonymous author name is displayed correctly.');
132 $subject_link = $this->xpath('//table/tbody/tr/td/a[contains(@href, :href) and contains(@title, :title) and text()=:text]', [
133 ':href' => $anonymous_comment4->permalink()->toString(),
134 ':title' => Unicode::truncate($body, 128),
137 $this->assertTrue(!empty($subject_link), 'Comment listing shows the correct subject link.');
138 $this->assertText($author_name . ' (not verified)', 'Anonymous author name is displayed correctly.');
140 // Delete multiple comments in one operation.
142 'action' => 'comment_delete_action',
143 "comment_bulk_form[1]" => $comments[0]->id(),
144 "comment_bulk_form[0]" => $comments[1]->id(),
145 "comment_bulk_form[2]" => $anonymous_comment4->id(),
147 $this->drupalPostForm(NULL, $edit, t('Apply to selected items'));
148 $this->assertText(t('Are you sure you want to delete these comments and all their children?'), 'Confirmation required.');
149 $this->drupalPostForm(NULL, [], t('Delete'));
150 $this->assertText(t('No comments available.'), 'All comments were deleted.');
152 // Make sure the label of unpublished node is not visible on listing page.
153 $this->drupalGet('admin/content/comment');
154 $this->postComment($this->node, $this->randomMachineName());
155 $this->drupalLogout();
156 $this->drupalLogin($this->adminUser);
157 $this->drupalGet('admin/content/comment');
158 $this->assertText(Html::escape($this->node->label()), 'Comment admin can see the title of a published node');
159 $this->node->setUnpublished()->save();
160 $this->assertFalse($this->node->isPublished(), 'Node is unpublished now.');
161 $this->drupalGet('admin/content/comment');
162 $this->assertNoText(Html::escape($this->node->label()), 'Comment admin cannot see the title of an unpublished node');
163 $this->drupalLogout();
164 $node_access_user = $this->drupalCreateUser([
165 'administer comments',
166 'bypass node access',
168 $this->drupalLogin($node_access_user);
169 $this->drupalGet('admin/content/comment');
170 $this->assertText(Html::escape($this->node->label()), 'Comment admin with bypass node access permissions can still see the title of a published node');
174 * Tests commented entity label of admin view.
176 public function testCommentedEntityLabel() {
177 \Drupal::service('module_installer')->install(['block_content']);
178 \Drupal::service('router.builder')->rebuildIfNeeded();
179 $bundle = BlockContentType::create([
185 $block_content = BlockContent::create([
187 'label' => 'Some block title',
188 'info' => 'Test block',
190 $block_content->save();
192 // Create comment field on block_content.
193 $this->addDefaultCommentField('block_content', 'basic', 'block_comment', CommentItemInterface::OPEN, 'block_comment');
194 $this->drupalLogin($this->webUser);
195 // Post a comment to node.
196 $node_comment = $this->postComment($this->node, $this->randomMachineName(), $this->randomMachineName(), TRUE);
197 // Post a comment to block content.
198 $block_content_comment = $this->postComment($block_content, $this->randomMachineName(), $this->randomMachineName(), TRUE, 'block_comment');
199 $this->drupalLogout();
200 // Login as admin to test the admin comment page.
201 $this->drupalLogin($this->adminUser);
202 $this->drupalGet('admin/content/comment');
204 $comment_author_link = $this->xpath('//table/tbody/tr[1]/td/a[contains(@href, :href) and text()=:text]', [
205 ':href' => $this->webUser->toUrl()->toString(),
206 ':text' => $this->webUser->label(),
208 $this->assertTrue(!empty($comment_author_link), 'Comment listing links to comment author.');
209 $comment_author_link = $this->xpath('//table/tbody/tr[2]/td/a[contains(@href, :href) and text()=:text]', [
210 ':href' => $this->webUser->toUrl()->toString(),
211 ':text' => $this->webUser->label(),
213 $this->assertTrue(!empty($comment_author_link), 'Comment listing links to comment author.');
214 // Admin page contains label of both entities.
215 $this->assertText(Html::escape($this->node->label()), 'Node title is visible.');
216 $this->assertText(Html::escape($block_content->label()), 'Block content label is visible.');
217 // Admin page contains subject of both entities.
218 $this->assertText(Html::escape($node_comment->label()), 'Node comment is visible.');
219 $this->assertText(Html::escape($block_content_comment->label()), 'Block content comment is visible.');