You are here

public function CommentAdminTest::testCommentedEntityLabel in Drupal 9

Same name and namespace in other branches
  1. 8 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 191

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');

  // Test that comment listing links to comment author.
  $this
    ->assertSession()
    ->elementExists('xpath', $this
    ->assertSession()
    ->buildXPathQuery('//table/tbody/tr[1]/td/a[contains(@href, :href) and text()=:text]', [
    ':href' => $this->webUser
      ->toUrl()
      ->toString(),
    ':text' => $this->webUser
      ->label(),
  ]));
  $this
    ->assertSession()
    ->elementExists('xpath', $this
    ->assertSession()
    ->buildXPathQuery('//table/tbody/tr[2]/td/a[contains(@href, :href) and text()=:text]', [
    ':href' => $this->webUser
      ->toUrl()
      ->toString(),
    ':text' => $this->webUser
      ->label(),
  ]));

  // Admin page contains label of both entities.
  $this
    ->assertSession()
    ->pageTextContains(Html::escape($this->node
    ->label()));
  $this
    ->assertSession()
    ->pageTextContains(Html::escape($block_content
    ->label()));

  // Admin page contains subject of both entities.
  $this
    ->assertSession()
    ->pageTextContains(Html::escape($node_comment
    ->label()));
  $this
    ->assertSession()
    ->pageTextContains(Html::escape($block_content_comment
    ->label()));
}