You are here

public function CommentCacheTagsTest::testCommentEntity in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/comment/tests/src/Functional/CommentCacheTagsTest.php \Drupal\Tests\comment\Functional\CommentCacheTagsTest::testCommentEntity()

Test that comments correctly invalidate the cache tag of their host entity.

File

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

Class

CommentCacheTagsTest
Tests the Comment entity's cache tags.

Namespace

Drupal\Tests\comment\Functional

Code

public function testCommentEntity() {
  $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
    ->assertText($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');
}