annotate core/modules/comment/tests/src/Functional/CommentAdminTest.php @ 19:fa3358dc1485 tip

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