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

Tests the comment reply link.

File

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

Class

CommentLinksTest
Tests the comment link field handlers.

Namespace

Drupal\Tests\comment\Kernel\Views

Code

public function testLinkReply() {
  $this
    ->enableModules([
    'field',
  ]);
  $this
    ->installSchema('comment', [
    'comment_entity_statistics',
  ]);
  $this
    ->installConfig([
    'field',
  ]);
  $field_storage_comment = FieldStorageConfig::create([
    'field_name' => 'comment',
    'type' => 'comment',
    'entity_type' => 'entity_test',
  ]);
  $field_storage_comment
    ->save();

  // Create a comment field which allows threading.
  $field_comment = FieldConfig::create([
    'field_name' => 'comment',
    'entity_type' => 'entity_test',
    'bundle' => 'entity_test',
    'settings' => [
      'default_mode' => CommentManagerInterface::COMMENT_MODE_THREADED,
    ],
  ]);
  $field_comment
    ->save();
  $host = EntityTest::create([
    'name' => $this
      ->randomString(),
  ]);
  $host
    ->save();

  // Attach an unapproved comment to the test entity.
  $comment = $this->commentStorage
    ->create([
    'uid' => $this->adminUser
      ->id(),
    'entity_type' => 'entity_test',
    'entity_id' => $host
      ->id(),
    'comment_type' => 'entity_test',
    'field_name' => $field_storage_comment
      ->getName(),
    'status' => 0,
  ]);
  $comment
    ->save();
  $view = Views::getView('test_comment');
  $view
    ->setDisplay();
  $view->displayHandlers
    ->get('default')
    ->overrideOption('fields', [
    'replyto_comment' => [
      'table' => 'comment',
      'field' => 'replyto_comment',
      'id' => 'replyto_comment',
      'plugin_id' => 'comment_link_reply',
      'entity_type' => 'comment',
    ],
  ]);
  $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 reply link on an unapproved comment.
  $replyto_comment = $view->style_plugin
    ->getField(0, 'replyto_comment');
  $this
    ->assertEmpty((string) $replyto_comment, "I can't reply to an unapproved comment.");

  // Approve the comment.
  $comment
    ->setPublished();
  $comment
    ->save();
  $view = Views::getView('test_comment');
  $view
    ->preview();

  // Check if I can see the reply link on an approved comment.
  $replyto_comment = $view->style_plugin
    ->getField(0, 'replyto_comment');
  $url = Url::fromRoute('comment.reply', [
    'entity_type' => 'entity_test',
    'entity' => $host
      ->id(),
    'field_name' => 'comment',
    'pid' => $comment
      ->id(),
  ]);
  $this
    ->assertSame((string) $replyto_comment, (string) Link::fromTextAndUrl('Reply', $url)
    ->toString(), 'Found the comment reply link as an admin user.');

  // Check if I can see the reply link as an anonymous user.
  $account_switcher
    ->switchTo(new AnonymousUserSession());
  $view = Views::getView('test_comment');
  $view
    ->preview();
  $replyto_comment = $view->style_plugin
    ->getField(0, 'replyto_comment');
  $this
    ->assertEmpty((string) $replyto_comment, "Didn't find the comment reply link as an anonymous user.");
}