function CommentLinksTest::testLinkApprove

Same name in other branches
  1. 9 core/modules/comment/tests/src/Kernel/Views/CommentLinksTest.php \Drupal\Tests\comment\Kernel\Views\CommentLinksTest::testLinkApprove()
  2. 8.9.x core/modules/comment/tests/src/Kernel/Views/CommentLinksTest.php \Drupal\Tests\comment\Kernel\Views\CommentLinksTest::testLinkApprove()
  3. 11.x core/modules/comment/tests/src/Kernel/Views/CommentLinksTest.php \Drupal\Tests\comment\Kernel\Views\CommentLinksTest::testLinkApprove()

Tests the comment approve link.

File

core/modules/comment/tests/src/Kernel/Views/CommentLinksTest.php, line 47

Class

CommentLinksTest
Tests the comment link field handlers.

Namespace

Drupal\Tests\comment\Kernel\Views

Code

public function testLinkApprove() : void {
    $host = EntityTest::create([
        'name' => $this->randomString(),
    ]);
    $host->save();
    // Create an unapproved comment.
    $comment = $this->commentStorage
        ->create([
        'uid' => $this->adminUser
            ->id(),
        'entity_type' => 'entity_test',
        'field_name' => 'comment',
        'entity_id' => $host->id(),
        'comment_type' => 'entity_test',
        'status' => 0,
    ]);
    $comment->save();
    $view = Views::getView('test_comment');
    $view->setDisplay();
    $view->displayHandlers
        ->get('default')
        ->overrideOption('fields', [
        'approve_comment' => [
            'table' => 'comment',
            'field' => 'approve_comment',
            'id' => 'approve_comment',
            'plugin_id' => 'comment_link_approve',
        ],
    ]);
    $view->save();
    
    /** @var \Drupal\Core\Session\AccountSwitcherInterface $account_switcher */
    $account_switcher = \Drupal::service('account_switcher');
    $account_switcher->switchTo($this->adminUser);
    $view->preview();
    // Check if I can see the comment approve link on an unapproved comment.
    $approve_comment = $view->style_plugin
        ->getField(0, 'approve_comment');
    $options = [
        'query' => [
            'destination' => '/',
        ],
    ];
    $url = Url::fromRoute('comment.approve', [
        'comment' => $comment->id(),
    ], $options);
    $this->assertSame((string) $approve_comment, (string) Link::fromTextAndUrl('Approve', $url)->toString(), 'Found a comment approve link for an unapproved comment.');
    // Approve the comment.
    $comment->setPublished();
    $comment->save();
    $view = Views::getView('test_comment');
    $view->preview();
    // Check if I can see the comment approve link on an approved comment.
    $approve_comment = $view->style_plugin
        ->getField(1, 'approve_comment');
    $this->assertEmpty((string) $approve_comment, "Didn't find a comment approve link for an already approved comment.");
    // Check if I can see the comment approve link on an approved comment as an
    // anonymous user.
    $account_switcher->switchTo(new AnonymousUserSession());
    // Set the comment as unpublished again.
    $comment->setUnpublished();
    $comment->save();
    $view = Views::getView('test_comment');
    $view->preview();
    $replyto_comment = $view->style_plugin
        ->getField(0, 'approve_comment');
    $this->assertEmpty((string) $replyto_comment, "I can't approve the comment as an anonymous user.");
}

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