function CommentAdminTest::testApprovalNodeInterface

Same name in other branches
  1. 9 core/modules/comment/tests/src/Functional/CommentAdminTest.php \Drupal\Tests\comment\Functional\CommentAdminTest::testApprovalNodeInterface()
  2. 8.9.x core/modules/comment/tests/src/Functional/CommentAdminTest.php \Drupal\Tests\comment\Functional\CommentAdminTest::testApprovalNodeInterface()
  3. 11.x 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 131

Class

CommentAdminTest
Tests comment approval functionality.

Namespace

Drupal\Tests\comment\Functional

Code

public function testApprovalNodeInterface() : void {
    // Set anonymous comments to require approval.
    user_role_change_permissions(RoleInterface::ANONYMOUS_ID, [
        'access comments' => TRUE,
        'post comments' => TRUE,
        'skip comment approval' => FALSE,
    ]);
    // Ensure that doesn't require contact info.
    $this->setCommentAnonymous(CommentInterface::ANONYMOUS_MAYNOT_CONTACT);
    // 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.');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.