3 namespace Drupal\Tests\comment\Functional;
5 use Drupal\comment\CommentInterface;
6 use Drupal\comment\CommentManagerInterface;
7 use Drupal\comment\Entity\Comment;
8 use Drupal\comment\Tests\CommentTestTrait;
9 use Drupal\Core\Entity\EntityInterface;
10 use Drupal\entity_test\Entity\EntityTest;
11 use Drupal\field\Entity\FieldConfig;
12 use Drupal\Tests\system\Functional\Entity\EntityWithUriCacheTagsTestBase;
13 use Drupal\user\Entity\Role;
14 use Drupal\user\RoleInterface;
17 * Tests the Comment entity's cache tags.
21 class CommentCacheTagsTest extends EntityWithUriCacheTagsTestBase {
28 public static $modules = ['comment'];
31 * @var \Drupal\entity_test\Entity\EntityTest
33 protected $entityTestCamelid;
36 * @var \Drupal\entity_test\Entity\EntityTest
38 protected $entityTestHippopotamidae;
43 protected function setUp() {
46 // Give anonymous users permission to view comments, so that we can verify
47 // the cache tags of cached versions of comment pages.
48 $user_role = Role::load(RoleInterface::ANONYMOUS_ID);
49 $user_role->grantPermission('access comments');
56 protected function createEntity() {
57 // Create a "bar" bundle for the "entity_test" entity type and create.
59 entity_test_create_bundle($bundle, NULL, 'entity_test');
61 // Create a comment field on this bundle.
62 $this->addDefaultCommentField('entity_test', 'bar', 'comment');
64 // Display comments in a flat list; threaded comments are not render cached.
65 $field = FieldConfig::loadByName('entity_test', 'bar', 'comment');
66 $field->setSetting('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT);
69 // Create a "Camelids" test entity that the comment will be assigned to.
70 $this->entityTestCamelid = EntityTest::create([
74 $this->entityTestCamelid->save();
76 // Create a "Llama" comment.
77 $comment = Comment::create([
80 'value' => 'The name "llama" was adopted by European settlers from native Peruvians.',
81 'format' => 'plain_text',
83 'entity_id' => $this->entityTestCamelid->id(),
84 'entity_type' => 'entity_test',
85 'field_name' => 'comment',
86 'status' => CommentInterface::PUBLISHED,
94 * Test that comments correctly invalidate the cache tag of their host entity.
96 public function testCommentEntity() {
97 $this->verifyPageCache($this->entityTestCamelid->urlInfo(), 'MISS');
98 $this->verifyPageCache($this->entityTestCamelid->urlInfo(), 'HIT');
100 // Create a "Hippopotamus" comment.
101 $this->entityTestHippopotamidae = EntityTest::create([
102 'name' => 'Hippopotamus',
105 $this->entityTestHippopotamidae->save();
107 $this->verifyPageCache($this->entityTestHippopotamidae->urlInfo(), 'MISS');
108 $this->verifyPageCache($this->entityTestHippopotamidae->urlInfo(), 'HIT');
110 $hippo_comment = Comment::create([
111 'subject' => 'Hippopotamus',
113 'value' => 'The common hippopotamus (Hippopotamus amphibius), or hippo, is a large, mostly herbivorous mammal in sub-Saharan Africa',
114 'format' => 'plain_text',
116 'entity_id' => $this->entityTestHippopotamidae->id(),
117 'entity_type' => 'entity_test',
118 'field_name' => 'comment',
119 'status' => CommentInterface::PUBLISHED,
121 $hippo_comment->save();
123 // Ensure that a new comment only invalidates the commented entity.
124 $this->verifyPageCache($this->entityTestCamelid->urlInfo(), 'HIT');
125 $this->verifyPageCache($this->entityTestHippopotamidae->urlInfo(), 'MISS');
126 $this->assertText($hippo_comment->getSubject());
128 // Ensure that updating an existing comment only invalidates the commented
130 $this->entity->save();
131 $this->verifyPageCache($this->entityTestCamelid->urlInfo(), 'MISS');
132 $this->verifyPageCache($this->entityTestHippopotamidae->urlInfo(), 'HIT');
138 protected function getAdditionalCacheContextsForEntity(EntityInterface $entity) {
145 * Each comment must have a comment body, which always has a text format.
147 protected function getAdditionalCacheTagsForEntity(EntityInterface $entity) {
148 /** @var \Drupal\comment\CommentInterface $entity */
150 'config:filter.format.plain_text',
151 'user:' . $entity->getOwnerId(),