function CommentCacheTagsTest::testCommentEntity

Same name and namespace in other branches
  1. 10 core/modules/comment/tests/src/Functional/CommentCacheTagsTest.php \Drupal\Tests\comment\Functional\CommentCacheTagsTest::testCommentEntity()
  2. 9 core/modules/comment/tests/src/Functional/CommentCacheTagsTest.php \Drupal\Tests\comment\Functional\CommentCacheTagsTest::testCommentEntity()
  3. 8.9.x core/modules/comment/tests/src/Functional/CommentCacheTagsTest.php \Drupal\Tests\comment\Functional\CommentCacheTagsTest::testCommentEntity()
  4. main core/modules/comment/tests/src/Functional/CommentCacheTagsTest.php \Drupal\Tests\comment\Functional\CommentCacheTagsTest::testCommentEntity()

Tests that comments invalidate the cache tag of their host entity.

File

core/modules/comment/tests/src/Functional/CommentCacheTagsTest.php, line 108

Class

CommentCacheTagsTest
Tests the Comment entity's cache tags.

Namespace

Drupal\Tests\comment\Functional

Code

public function testCommentEntity() : void {
  $this->verifyPageCache($this->entityTestCamelid
    ->toUrl(), 'MISS');
  $this->verifyPageCache($this->entityTestCamelid
    ->toUrl(), 'HIT');
  // Create a "Hippopotamus" comment.
  $this->entityTestHippopotamidae = EntityTest::create([
    'name' => 'Hippopotamus',
    'type' => 'bar',
  ]);
  $this->entityTestHippopotamidae
    ->save();
  $this->verifyPageCache($this->entityTestHippopotamidae
    ->toUrl(), 'MISS');
  $this->verifyPageCache($this->entityTestHippopotamidae
    ->toUrl(), 'HIT');
  $hippo_comment = Comment::create([
    'subject' => 'Hippopotamus',
    'comment_body' => [
      'value' => 'The common hippopotamus (Hippopotamus amphibius), or hippo, is a large, mostly herbivorous mammal in sub-Saharan Africa',
      'format' => 'plain_text',
    ],
    'entity_id' => $this->entityTestHippopotamidae
      ->id(),
    'entity_type' => 'entity_test',
    'field_name' => 'comment',
    'status' => CommentInterface::PUBLISHED,
  ]);
  $hippo_comment->save();
  // Ensure that a new comment only invalidates the commented entity.
  $this->verifyPageCache($this->entityTestCamelid
    ->toUrl(), 'HIT');
  $this->verifyPageCache($this->entityTestHippopotamidae
    ->toUrl(), 'MISS');
  $this->assertSession()
    ->pageTextContains($hippo_comment->getSubject());
  // Ensure that updating an existing comment only invalidates the commented
  // entity.
  $this->entity
    ->save();
  $this->verifyPageCache($this->entityTestCamelid
    ->toUrl(), 'MISS');
  $this->verifyPageCache($this->entityTestHippopotamidae
    ->toUrl(), 'HIT');
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.