You are here

public function CommentAdminTest::testApprovalNodeInterface in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/comment/tests/src/Functional/CommentAdminTest.php \Drupal\Tests\comment\Functional\CommentAdminTest::testApprovalNodeInterface()

Tests comment approval functionality through the node interface.

File

core/modules/comment/tests/src/Functional/CommentAdminTest.php, line 125

Class

CommentAdminTest
Tests comment approval functionality.

Namespace

Drupal\Tests\comment\Functional

Code

public function testApprovalNodeInterface() {

  // Set anonymous comments to require approval.
  user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
    'access comments' => TRUE,
    'post comments' => TRUE,
    'skip comment approval' => FALSE,
  ]);
  $this
    ->drupalLogin($this->adminUser);

  // Ensure that doesn't require contact info.
  $this
    ->setCommentAnonymous('0');
  $this
    ->drupalLogout();

  // Post anonymous comment without contact info.
  $subject = $this
    ->randomMachineName();
  $body = $this
    ->randomMachineName();

  // Set $contact to true so that it won't check for id and message.
  $this
    ->postComment($this->node, $body, $subject, TRUE);
  $this
    ->assertSession()
    ->pageTextContains('Your comment has been queued for review by site administrators and will be published after approval.');

  // Get unapproved comment id.
  $this
    ->drupalLogin($this->adminUser);
  $anonymous_comment4 = $this
    ->getUnapprovedComment($subject);
  $anonymous_comment4 = Comment::create([
    'cid' => $anonymous_comment4,
    'subject' => $subject,
    'comment_body' => $body,
    'entity_id' => $this->node
      ->id(),
    'entity_type' => 'node',
    'field_name' => 'comment',
  ]);
  $this
    ->drupalLogout();
  $this
    ->assertFalse($this
    ->commentExists($anonymous_comment4), 'Anonymous comment was not published.');

  // Ensure comments cannot be approved without a valid token.
  $this
    ->drupalLogin($this->adminUser);
  $this
    ->drupalGet('comment/1/approve');
  $this
    ->assertSession()
    ->statusCodeEquals(403);
  $this
    ->drupalGet('comment/1/approve', [
    'query' => [
      'token' => 'forged',
    ],
  ]);
  $this
    ->assertSession()
    ->statusCodeEquals(403);

  // Approve comment.
  $this
    ->drupalGet('comment/1/edit');
  $this
    ->assertSession()
    ->checkboxChecked('edit-status-0');
  $this
    ->drupalGet('node/' . $this->node
    ->id());
  $this
    ->clickLink('Approve');
  $this
    ->drupalLogout();
  $this
    ->drupalGet('node/' . $this->node
    ->id());
  $this
    ->assertTrue($this
    ->commentExists($anonymous_comment4), 'Anonymous comment visible.');
}