You are here

function CommentStatisticsTest::testCommentNodeCommentStatistics in Multiversion 8

Tests the node comment statistics.

File

tests/src/Functional/CommentStatisticsTest.php, line 68

Class

CommentStatisticsTest
Tests comment statistics.

Namespace

Drupal\Tests\multiversion\Functional

Code

function testCommentNodeCommentStatistics() {
  $node_storage = $this->container
    ->get('entity.manager')
    ->getStorage('node');
  $this
    ->drupalGet('<front>');
  $this
    ->assertNoLink(t('1 comment'));
  $this
    ->assertEqual($this->node
    ->get('comment')->comment_count, 0, 'The number of comments for the node is correct (0 comments)');

  // Test comment statistic when creating comments.
  $comment1 = Comment::create([
    'entity_type' => 'node',
    'field_name' => 'comment',
    'subject' => 'How much wood would a woodchuck chuck',
    'comment_body' => $this
      ->randomMachineName(128),
    'entity_id' => $this->node
      ->id(),
  ]);
  $comment1
    ->save();
  $node_storage
    ->resetCache([
    $this->node
      ->id(),
  ]);
  $node = $node_storage
    ->load($this->node
    ->id());
  $this
    ->assertEqual($node
    ->get('comment')->comment_count, 1, 'The number of comments for the node is correct (1 comment)');
  $this
    ->drupalGet('<front>');
  $this
    ->assertLink(t('1 comment'));
  $comment2 = Comment::create([
    'entity_type' => 'node',
    'field_name' => 'comment',
    'subject' => 'A big black bug bit a big black dog',
    'comment_body' => $this
      ->randomMachineName(128),
    'entity_id' => $this->node
      ->id(),
  ]);
  $comment2
    ->save();
  $comment3 = Comment::create([
    'entity_type' => 'node',
    'field_name' => 'comment',
    'subject' => 'How much pot, could a pot roast roast',
    'comment_body' => $this
      ->randomMachineName(128),
    'entity_id' => $this->node
      ->id(),
  ]);
  $comment3
    ->save();
  $node_storage
    ->resetCache([
    $this->node
      ->id(),
  ]);
  $node = $node_storage
    ->load($this->node
    ->id());
  $this
    ->assertEqual($node
    ->get('comment')->comment_count, 3, 'The number of comments for the node is correct (3 comments)');
  $this
    ->drupalGet('<front>');
  $this
    ->assertLink(t('3 comments'));

  // Test comment statistic when deleting comments.
  $comment1
    ->delete();
  $comment2
    ->delete();
  $node_storage
    ->resetCache([
    $this->node
      ->id(),
  ]);
  $node = $node_storage
    ->load($this->node
    ->id());
  $this
    ->assertEqual($node
    ->get('comment')->comment_count, 1, 'The number of comments for the node is correct (1 comment)');
  $this
    ->drupalGet('<front>');
  $this
    ->assertLink(t('1 comment'));
  $comment3
    ->delete();
  $node_storage
    ->resetCache([
    $this->node
      ->id(),
  ]);
  $node = $node_storage
    ->load($this->node
    ->id());
  $this
    ->assertEqual($node
    ->get('comment')->comment_count, 0, 'The number of comments for the node is correct (0 comments)');
  $this
    ->drupalGet('<front>');
  $this
    ->assertNoLink(t('1 comment'));
  $this
    ->assertNoLink(t('comments'));
}