ee42dcaddd07ca30e618634c2a0b2d6e8ad654ed
[yaffs-website] / CommentAdminTest.php
1 <?php
2
3 namespace Drupal\Tests\comment\Functional\Views;
4
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;
15
16 /**
17  * Tests comment approval functionality.
18  *
19  * @group comment
20  */
21 class CommentAdminTest extends CommentBrowserTestBase {
22
23   /**
24    * {@inheritdoc}
25    */
26   protected function setUp() {
27     parent::setUp();
28     \Drupal::service('module_installer')->install(['views']);
29     $view = Views::getView('comment');
30     $view->storage->enable()->save();
31     \Drupal::service('router.builder')->rebuildIfNeeded();
32   }
33
34   /**
35    * Test comment approval functionality through admin/content/comment.
36    */
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,
43     ]);
44     $this->drupalPlaceBlock('page_title_block');
45     $this->drupalLogin($this->adminUser);
46     // Ensure that doesn't require contact info.
47     $this->setCommentAnonymous('0');
48
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.'));
52
53     // Assert the expose filters on the admin page.
54     $this->assertField('subject');
55     $this->assertField('author_name');
56     $this->assertField('langcode');
57
58     $this->drupalLogout();
59
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,
67     ], t('Save'));
68     $this->assertText(t('Your comment has been queued for review by site administrators and will be published after approval.'), 'Comment requires approval.');
69
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',
80     ]);
81     $this->drupalLogout();
82
83     $this->assertFalse($this->commentExists($anonymous_comment4), 'Anonymous comment was not published.');
84
85     // Approve comment.
86     $this->drupalLogin($this->adminUser);
87     $edit = [];
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'));
91
92     $this->assertText('Publish comment was applied to 1 item.', new FormattableMarkup('Operation "@operation" was performed on comment.', ['@operation' => 'publish']));
93     $this->drupalLogout();
94
95     $this->drupalGet('node/' . $this->node->id());
96     $this->assertTrue($this->commentExists($anonymous_comment4), 'Anonymous comment visible.');
97
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);
101
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.');
106
107     // Assert the expose filters on the admin page.
108     $this->assertField('subject');
109     $this->assertField('author_name');
110     $this->assertField('langcode');
111
112     $edit = [
113       "action" => 'comment_publish_action',
114       "comment_bulk_form[1]" => $comments[0]->id(),
115       "comment_bulk_form[0]" => $comments[1]->id(),
116     ];
117     $this->drupalPostForm(NULL, $edit, t('Apply to selected items'));
118     $this->assertText(t('Unapproved comments (@count)', ['@count' => 0]), 'All comments were approved.');
119
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.'));
123
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(),
128     ]);
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.');
131
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),
135       ':text' => $subject,
136     ]);
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.');
139
140     // Delete multiple comments in one operation.
141     $edit = [
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(),
146     ];
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.');
151
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',
167     ]);
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');
171   }
172
173   /**
174    * Tests commented entity label of admin view.
175    */
176   public function testCommentedEntityLabel() {
177     \Drupal::service('module_installer')->install(['block_content']);
178     \Drupal::service('router.builder')->rebuildIfNeeded();
179     $bundle = BlockContentType::create([
180       'id' => 'basic',
181       'label' => 'basic',
182       'revision' => FALSE,
183     ]);
184     $bundle->save();
185     $block_content = BlockContent::create([
186       'type' => 'basic',
187       'label' => 'Some block title',
188       'info' => 'Test block',
189     ]);
190     $block_content->save();
191
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');
203
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(),
207     ]);
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(),
212     ]);
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.');
220   }
221
222 }