You are here

public function CommentAdminTest::testCommentedEntityLabel in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/comment/tests/src/Functional/Views/CommentAdminTest.php \Drupal\Tests\comment\Functional\Views\CommentAdminTest::testCommentedEntityLabel()
  2. 10 core/modules/comment/tests/src/Functional/Views/CommentAdminTest.php \Drupal\Tests\comment\Functional\Views\CommentAdminTest::testCommentedEntityLabel()

Tests commented entity label of admin view.

File

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

Class

CommentAdminTest
Tests comment approval functionality.

Namespace

Drupal\Tests\comment\Functional\Views

Code

public function testCommentedEntityLabel() {
  \Drupal::service('module_installer')
    ->install([
    'block_content',
  ]);
  \Drupal::service('router.builder')
    ->rebuildIfNeeded();
  $bundle = BlockContentType::create([
    'id' => 'basic',
    'label' => 'basic',
    'revision' => FALSE,
  ]);
  $bundle
    ->save();
  $block_content = BlockContent::create([
    'type' => 'basic',
    'label' => 'Some block title',
    'info' => 'Test block',
  ]);
  $block_content
    ->save();

  // Create comment field on block_content.
  $this
    ->addDefaultCommentField('block_content', 'basic', 'block_comment', CommentItemInterface::OPEN, 'block_comment');
  $this
    ->drupalLogin($this->webUser);

  // Post a comment to node.
  $node_comment = $this
    ->postComment($this->node, $this
    ->randomMachineName(), $this
    ->randomMachineName(), TRUE);

  // Post a comment to block content.
  $block_content_comment = $this
    ->postComment($block_content, $this
    ->randomMachineName(), $this
    ->randomMachineName(), TRUE, 'block_comment');
  $this
    ->drupalLogout();

  // Login as admin to test the admin comment page.
  $this
    ->drupalLogin($this->adminUser);
  $this
    ->drupalGet('admin/content/comment');
  $comment_author_link = $this
    ->xpath('//table/tbody/tr[1]/td/a[contains(@href, :href) and text()=:text]', [
    ':href' => $this->webUser
      ->toUrl()
      ->toString(),
    ':text' => $this->webUser
      ->label(),
  ]);
  $this
    ->assertTrue(!empty($comment_author_link), 'Comment listing links to comment author.');
  $comment_author_link = $this
    ->xpath('//table/tbody/tr[2]/td/a[contains(@href, :href) and text()=:text]', [
    ':href' => $this->webUser
      ->toUrl()
      ->toString(),
    ':text' => $this->webUser
      ->label(),
  ]);
  $this
    ->assertTrue(!empty($comment_author_link), 'Comment listing links to comment author.');

  // Admin page contains label of both entities.
  $this
    ->assertText(Html::escape($this->node
    ->label()), 'Node title is visible.');
  $this
    ->assertText(Html::escape($block_content
    ->label()), 'Block content label is visible.');

  // Admin page contains subject of both entities.
  $this
    ->assertText(Html::escape($node_comment
    ->label()), 'Node comment is visible.');
  $this
    ->assertText(Html::escape($block_content_comment
    ->label()), 'Block content comment is visible.');
}