X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Fcomment%2Ftests%2Fsrc%2FFunctional%2FCommentCacheTagsTest.php;fp=web%2Fcore%2Fmodules%2Fcomment%2Ftests%2Fsrc%2FFunctional%2FCommentCacheTagsTest.php;h=0b0f4863afe72a9bde70d168fa087694afd558a7;hp=0000000000000000000000000000000000000000;hb=0bf8d09d2542548982e81a441b1f16e75873a04f;hpb=74df008bdbb3a11eeea356744f39b802369bda3c diff --git a/web/core/modules/comment/tests/src/Functional/CommentCacheTagsTest.php b/web/core/modules/comment/tests/src/Functional/CommentCacheTagsTest.php new file mode 100644 index 000000000..0b0f4863a --- /dev/null +++ b/web/core/modules/comment/tests/src/Functional/CommentCacheTagsTest.php @@ -0,0 +1,156 @@ +grantPermission('access comments'); + $user_role->save(); + } + + /** + * {@inheritdoc} + */ + protected function createEntity() { + // Create a "bar" bundle for the "entity_test" entity type and create. + $bundle = 'bar'; + entity_test_create_bundle($bundle, NULL, 'entity_test'); + + // Create a comment field on this bundle. + $this->addDefaultCommentField('entity_test', 'bar', 'comment'); + + // Display comments in a flat list; threaded comments are not render cached. + $field = FieldConfig::loadByName('entity_test', 'bar', 'comment'); + $field->setSetting('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT); + $field->save(); + + // Create a "Camelids" test entity that the comment will be assigned to. + $this->entityTestCamelid = EntityTest::create([ + 'name' => 'Camelids', + 'type' => 'bar', + ]); + $this->entityTestCamelid->save(); + + // Create a "Llama" comment. + $comment = Comment::create([ + 'subject' => 'Llama', + 'comment_body' => [ + 'value' => 'The name "llama" was adopted by European settlers from native Peruvians.', + 'format' => 'plain_text', + ], + 'entity_id' => $this->entityTestCamelid->id(), + 'entity_type' => 'entity_test', + 'field_name' => 'comment', + 'status' => CommentInterface::PUBLISHED, + ]); + $comment->save(); + + return $comment; + } + + /** + * Test that comments correctly invalidate the cache tag of their host entity. + */ + public function testCommentEntity() { + $this->verifyPageCache($this->entityTestCamelid->urlInfo(), 'MISS'); + $this->verifyPageCache($this->entityTestCamelid->urlInfo(), 'HIT'); + + // Create a "Hippopotamus" comment. + $this->entityTestHippopotamidae = EntityTest::create([ + 'name' => 'Hippopotamus', + 'type' => 'bar', + ]); + $this->entityTestHippopotamidae->save(); + + $this->verifyPageCache($this->entityTestHippopotamidae->urlInfo(), 'MISS'); + $this->verifyPageCache($this->entityTestHippopotamidae->urlInfo(), '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->urlInfo(), 'HIT'); + $this->verifyPageCache($this->entityTestHippopotamidae->urlInfo(), 'MISS'); + $this->assertText($hippo_comment->getSubject()); + + // Ensure that updating an existing comment only invalidates the commented + // entity. + $this->entity->save(); + $this->verifyPageCache($this->entityTestCamelid->urlInfo(), 'MISS'); + $this->verifyPageCache($this->entityTestHippopotamidae->urlInfo(), 'HIT'); + } + + /** + * {@inheritdoc} + */ + protected function getAdditionalCacheContextsForEntity(EntityInterface $entity) { + return []; + } + + /** + * {@inheritdoc} + * + * Each comment must have a comment body, which always has a text format. + */ + protected function getAdditionalCacheTagsForEntity(EntityInterface $entity) { + /** @var \Drupal\comment\CommentInterface $entity */ + return [ + 'config:filter.format.plain_text', + 'user:' . $entity->getOwnerId(), + 'user_view', + ]; + } + +}