3 namespace Drupal\comment\Tests;
5 use Drupal\comment\CommentInterface;
6 use Drupal\comment\CommentManagerInterface;
7 use Drupal\comment\Entity\Comment;
8 use Drupal\Core\Entity\EntityInterface;
9 use Drupal\entity_test\Entity\EntityTest;
10 use Drupal\field\Entity\FieldConfig;
11 use Drupal\system\Tests\Entity\EntityWithUriCacheTagsTestBase;
12 use Drupal\user\Entity\Role;
13 use Drupal\user\RoleInterface;
16 * Tests the Comment entity's cache tags.
20 class CommentCacheTagsTest extends EntityWithUriCacheTagsTestBase {
27 public static $modules = ['comment'];
30 * @var \Drupal\entity_test\Entity\EntityTest
32 protected $entityTestCamelid;
35 * @var \Drupal\entity_test\Entity\EntityTest
37 protected $entityTestHippopotamidae;
42 protected function setUp() {
45 // Give anonymous users permission to view comments, so that we can verify
46 // the cache tags of cached versions of comment pages.
47 $user_role = Role::load(RoleInterface::ANONYMOUS_ID);
48 $user_role->grantPermission('access comments');
55 protected function createEntity() {
56 // Create a "bar" bundle for the "entity_test" entity type and create.
58 entity_test_create_bundle($bundle, NULL, 'entity_test');
60 // Create a comment field on this bundle.
61 $this->addDefaultCommentField('entity_test', 'bar', 'comment');
63 // Display comments in a flat list; threaded comments are not render cached.
64 $field = FieldConfig::loadByName('entity_test', 'bar', 'comment');
65 $field->setSetting('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT);
68 // Create a "Camelids" test entity that the comment will be assigned to.
69 $this->entityTestCamelid = EntityTest::create([
73 $this->entityTestCamelid->save();
75 // Create a "Llama" comment.
76 $comment = Comment::create([
79 'value' => 'The name "llama" was adopted by European settlers from native Peruvians.',
80 'format' => 'plain_text',
82 'entity_id' => $this->entityTestCamelid->id(),
83 'entity_type' => 'entity_test',
84 'field_name' => 'comment',
85 'status' => CommentInterface::PUBLISHED,
93 * Test that comments correctly invalidate the cache tag of their host entity.
95 public function testCommentEntity() {
96 $this->verifyPageCache($this->entityTestCamelid->urlInfo(), 'MISS');
97 $this->verifyPageCache($this->entityTestCamelid->urlInfo(), 'HIT');
99 // Create a "Hippopotamus" comment.
100 $this->entityTestHippopotamidae = EntityTest::create([
101 'name' => 'Hippopotamus',
104 $this->entityTestHippopotamidae->save();
106 $this->verifyPageCache($this->entityTestHippopotamidae->urlInfo(), 'MISS');
107 $this->verifyPageCache($this->entityTestHippopotamidae->urlInfo(), 'HIT');
109 $hippo_comment = Comment::create([
110 'subject' => 'Hippopotamus',
112 'value' => 'The common hippopotamus (Hippopotamus amphibius), or hippo, is a large, mostly herbivorous mammal in sub-Saharan Africa',
113 'format' => 'plain_text',
115 'entity_id' => $this->entityTestHippopotamidae->id(),
116 'entity_type' => 'entity_test',
117 'field_name' => 'comment',
118 'status' => CommentInterface::PUBLISHED,
120 $hippo_comment->save();
122 // Ensure that a new comment only invalidates the commented entity.
123 $this->verifyPageCache($this->entityTestCamelid->urlInfo(), 'HIT');
124 $this->verifyPageCache($this->entityTestHippopotamidae->urlInfo(), 'MISS');
125 $this->assertText($hippo_comment->getSubject());
127 // Ensure that updating an existing comment only invalidates the commented
129 $this->entity->save();
130 $this->verifyPageCache($this->entityTestCamelid->urlInfo(), 'MISS');
131 $this->verifyPageCache($this->entityTestHippopotamidae->urlInfo(), 'HIT');
137 protected function getAdditionalCacheContextsForEntity(EntityInterface $entity) {
144 * Each comment must have a comment body, which always has a text format.
146 protected function getAdditionalCacheTagsForEntity(EntityInterface $entity) {
147 /** @var \Drupal\comment\CommentInterface $entity */
149 'config:filter.format.plain_text',
150 'user:' . $entity->getOwnerId(),