12e0ef8451bc2014f03d797f6e34704821ada0a0
[yaffs-website] / web / core / modules / comment / src / Tests / CommentCacheTagsTest.php
1 <?php
2
3 namespace Drupal\comment\Tests;
4
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;
14
15 /**
16  * Tests the Comment entity's cache tags.
17  *
18  * @group comment
19  */
20 class CommentCacheTagsTest extends EntityWithUriCacheTagsTestBase {
21
22   use CommentTestTrait;
23
24   /**
25    * {@inheritdoc}
26    */
27   public static $modules = ['comment'];
28
29   /**
30    * @var \Drupal\entity_test\Entity\EntityTest
31    */
32   protected $entityTestCamelid;
33
34   /**
35    * @var \Drupal\entity_test\Entity\EntityTest
36    */
37   protected $entityTestHippopotamidae;
38
39   /**
40    * {@inheritdoc}
41    */
42   protected function setUp() {
43     parent::setUp();
44
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');
49     $user_role->save();
50   }
51
52   /**
53    * {@inheritdoc}
54    */
55   protected function createEntity() {
56     // Create a "bar" bundle for the "entity_test" entity type and create.
57     $bundle = 'bar';
58     entity_test_create_bundle($bundle, NULL, 'entity_test');
59
60     // Create a comment field on this bundle.
61     $this->addDefaultCommentField('entity_test', 'bar', 'comment');
62
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);
66     $field->save();
67
68     // Create a "Camelids" test entity that the comment will be assigned to.
69     $this->entityTestCamelid = EntityTest::create([
70       'name' => 'Camelids',
71       'type' => 'bar',
72     ]);
73     $this->entityTestCamelid->save();
74
75     // Create a "Llama" comment.
76     $comment = Comment::create([
77       'subject' => 'Llama',
78       'comment_body' => [
79         'value' => 'The name "llama" was adopted by European settlers from native Peruvians.',
80         'format' => 'plain_text',
81       ],
82       'entity_id' => $this->entityTestCamelid->id(),
83       'entity_type' => 'entity_test',
84       'field_name' => 'comment',
85       'status' => CommentInterface::PUBLISHED,
86     ]);
87     $comment->save();
88
89     return $comment;
90   }
91
92   /**
93    * Test that comments correctly invalidate the cache tag of their host entity.
94    */
95   public function testCommentEntity() {
96     $this->verifyPageCache($this->entityTestCamelid->urlInfo(), 'MISS');
97     $this->verifyPageCache($this->entityTestCamelid->urlInfo(), 'HIT');
98
99     // Create a "Hippopotamus" comment.
100     $this->entityTestHippopotamidae = EntityTest::create([
101       'name' => 'Hippopotamus',
102       'type' => 'bar',
103     ]);
104     $this->entityTestHippopotamidae->save();
105
106     $this->verifyPageCache($this->entityTestHippopotamidae->urlInfo(), 'MISS');
107     $this->verifyPageCache($this->entityTestHippopotamidae->urlInfo(), 'HIT');
108
109     $hippo_comment = Comment::create([
110       'subject' => 'Hippopotamus',
111       'comment_body' => [
112         'value' => 'The common hippopotamus (Hippopotamus amphibius), or hippo, is a large, mostly herbivorous mammal in sub-Saharan Africa',
113         'format' => 'plain_text',
114       ],
115       'entity_id' => $this->entityTestHippopotamidae->id(),
116       'entity_type' => 'entity_test',
117       'field_name' => 'comment',
118       'status' => CommentInterface::PUBLISHED,
119     ]);
120     $hippo_comment->save();
121
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());
126
127     // Ensure that updating an existing comment only invalidates the commented
128     // entity.
129     $this->entity->save();
130     $this->verifyPageCache($this->entityTestCamelid->urlInfo(), 'MISS');
131     $this->verifyPageCache($this->entityTestHippopotamidae->urlInfo(), 'HIT');
132   }
133
134   /**
135    * {@inheritdoc}
136    */
137   protected function getAdditionalCacheContextsForEntity(EntityInterface $entity) {
138     return [];
139   }
140
141   /**
142    * {@inheritdoc}
143    *
144    * Each comment must have a comment body, which always has a text format.
145    */
146   protected function getAdditionalCacheTagsForEntity(EntityInterface $entity) {
147     /** @var \Drupal\comment\CommentInterface $entity */
148     return [
149       'config:filter.format.plain_text',
150       'user:' . $entity->getOwnerId(),
151       'user_view',
152     ];
153   }
154
155 }