You are here

public function CommentLinksTest::testLinkApprove in Zircon Profile 8.0

Same name and namespace in other branches
  1. 8 core/modules/comment/src/Tests/Views/CommentLinksTest.php \Drupal\comment\Tests\Views\CommentLinksTest::testLinkApprove()

Test the comment approve link.

File

core/modules/comment/src/Tests/Views/CommentLinksTest.php, line 36
Contains \Drupal\comment\Tests\Views\CommentLinksTest.

Class

CommentLinksTest
Tests the comment link field handlers.

Namespace

Drupal\comment\Tests\Views

Code

public function testLinkApprove() {

  // Create an unapproved comment.
  $comment = $this->commentStorage
    ->create([
    'uid' => $this->adminUser
      ->id(),
    'entity_type' => 'entity_test',
    '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
    ->assertEqual(\Drupal::l('Approve', $url), (string) $approve_comment, 'Found a comment approve link for an unapproved comment.');

  // Approve the comment.
  $comment
    ->setPublished(CommentInterface::PUBLISHED);
  $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
    ->assertFalse((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
    ->setPublished(CommentInterface::NOT_PUBLISHED);
  $comment
    ->save();
  $view = Views::getView('test_comment');
  $view
    ->preview();
  $replyto_comment = $view->style_plugin
    ->getField(0, 'approve_comment');
  $this
    ->assertFalse((string) $replyto_comment, "I can't approve the comment as an anonymous user.");
}