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