Commit dd23c88a authored by catch's avatar catch

Issue #2530846 by Berdir, mbovan: Fix and improve comment cache tag usage

parent 8fbc6dae
...@@ -8,6 +8,7 @@ ...@@ -8,6 +8,7 @@
namespace Drupal\comment\Entity; namespace Drupal\comment\Entity;
use Drupal\Component\Utility\Number; use Drupal\Component\Utility\Number;
use Drupal\Core\Cache\Cache;
use Drupal\Core\Entity\ContentEntityBase; use Drupal\Core\Entity\ContentEntityBase;
use Drupal\comment\CommentInterface; use Drupal\comment\CommentInterface;
use Drupal\Core\Entity\EntityChangedTrait; use Drupal\Core\Entity\EntityChangedTrait;
...@@ -153,6 +154,11 @@ public function preSave(EntityStorageInterface $storage) { ...@@ -153,6 +154,11 @@ public function preSave(EntityStorageInterface $storage) {
public function postSave(EntityStorageInterface $storage, $update = TRUE) { public function postSave(EntityStorageInterface $storage, $update = TRUE) {
parent::postSave($storage, $update); parent::postSave($storage, $update);
// Always invalidate the cache tag for the commented entity.
if ($commented_entity = $this->getCommentedEntity()) {
Cache::invalidateTags($commented_entity->getCacheTagsToInvalidate());
}
$this->releaseThreadLock(); $this->releaseThreadLock();
// Update the {comment_entity_statistics} table prior to executing the hook. // Update the {comment_entity_statistics} table prior to executing the hook.
\Drupal::service('comment.statistics')->update($this); \Drupal::service('comment.statistics')->update($this);
......
...@@ -159,9 +159,7 @@ public function viewElements(FieldItemListInterface $items) { ...@@ -159,9 +159,7 @@ public function viewElements(FieldItemListInterface $items) {
// should display if the user is an administrator. // should display if the user is an administrator.
$elements['#cache']['contexts'][] = 'user.permissions'; $elements['#cache']['contexts'][] = 'user.permissions';
if ($this->currentUser->hasPermission('access comments') || $this->currentUser->hasPermission('administer comments')) { if ($this->currentUser->hasPermission('access comments') || $this->currentUser->hasPermission('administer comments')) {
// This is a listing of Comment entities, so associate its list cache $output['comments'] = [];
// tag for correct invalidation.
$output['comments']['#cache']['tags'] = $this->entityManager->getDefinition('comment')->getListCacheTags();
if ($entity->get($field_name)->comment_count || $this->currentUser->hasPermission('administer comments')) { if ($entity->get($field_name)->comment_count || $this->currentUser->hasPermission('administer comments')) {
$mode = $comment_settings['default_mode']; $mode = $comment_settings['default_mode'];
......
...@@ -7,7 +7,9 @@ ...@@ -7,7 +7,9 @@
namespace Drupal\comment\Tests; namespace Drupal\comment\Tests;
use Drupal\comment\CommentInterface;
use Drupal\comment\CommentManagerInterface; use Drupal\comment\CommentManagerInterface;
use Drupal\comment\Entity\Comment;
use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityInterface;
use Drupal\field\Entity\FieldConfig; use Drupal\field\Entity\FieldConfig;
use Drupal\system\Tests\Entity\EntityWithUriCacheTagsTestBase; use Drupal\system\Tests\Entity\EntityWithUriCacheTagsTestBase;
...@@ -28,6 +30,16 @@ class CommentCacheTagsTest extends EntityWithUriCacheTagsTestBase { ...@@ -28,6 +30,16 @@ class CommentCacheTagsTest extends EntityWithUriCacheTagsTestBase {
*/ */
public static $modules = array('comment'); public static $modules = array('comment');
/**
* @var \Drupal\entity_test\Entity\EntityTest
*/
protected $entityTestCamelid;
/**
* @var \Drupal\entity_test\Entity\EntityTest
*/
protected $entityTestHippopotamidae;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
...@@ -57,12 +69,12 @@ protected function createEntity() { ...@@ -57,12 +69,12 @@ protected function createEntity() {
$field->setSetting('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT); $field->setSetting('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT);
$field->save(); $field->save();
// Create a "Camelids" test entity. // Create a "Camelids" test entity that the comment will be assigned to.
$entity_test = entity_create('entity_test', array( $this->entityTestCamelid = entity_create('entity_test', array(
'name' => 'Camelids', 'name' => 'Camelids',
'type' => 'bar', 'type' => 'bar',
)); ));
$entity_test->save(); $this->entityTestCamelid->save();
// Create a "Llama" comment. // Create a "Llama" comment.
$comment = entity_create('comment', array( $comment = entity_create('comment', array(
...@@ -71,7 +83,7 @@ protected function createEntity() { ...@@ -71,7 +83,7 @@ protected function createEntity() {
'value' => 'The name "llama" was adopted by European settlers from native Peruvians.', 'value' => 'The name "llama" was adopted by European settlers from native Peruvians.',
'format' => 'plain_text', 'format' => 'plain_text',
), ),
'entity_id' => $entity_test->id(), 'entity_id' => $this->entityTestCamelid->id(),
'entity_type' => 'entity_test', 'entity_type' => 'entity_test',
'field_name' => 'comment', 'field_name' => 'comment',
'status' => \Drupal\comment\CommentInterface::PUBLISHED, 'status' => \Drupal\comment\CommentInterface::PUBLISHED,
...@@ -81,6 +93,47 @@ protected function createEntity() { ...@@ -81,6 +93,47 @@ protected function createEntity() {
return $comment; 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 = entity_create('entity_test', array(
'name' => 'Hippopotamus',
'type' => 'bar',
));
$this->entityTestHippopotamidae->save();
$this->verifyPageCache($this->entityTestHippopotamidae->urlInfo(), 'MISS');
$this->verifyPageCache($this->entityTestHippopotamidae->urlInfo(), 'HIT');
$hippo_comment = Comment::create(array(
'subject' => 'Hippopotamus',
'comment_body' => array(
'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} * {@inheritdoc}
......
...@@ -73,7 +73,6 @@ public function testCacheTags() { ...@@ -73,7 +73,6 @@ public function testCacheTags() {
$expected_cache_tags = [ $expected_cache_tags = [
'entity_test_view', 'entity_test_view',
'entity_test:' . $commented_entity->id(), 'entity_test:' . $commented_entity->id(),
'comment_list',
'config:core.entity_form_display.comment.comment.default', 'config:core.entity_form_display.comment.comment.default',
'config:field.field.comment.comment.comment_body', 'config:field.field.comment.comment.comment_body',
'config:field.field.entity_test.entity_test.comment', 'config:field.field.entity_test.entity_test.comment',
...@@ -115,7 +114,6 @@ public function testCacheTags() { ...@@ -115,7 +114,6 @@ public function testCacheTags() {
$expected_cache_tags = [ $expected_cache_tags = [
'entity_test_view', 'entity_test_view',
'entity_test:' . $commented_entity->id(), 'entity_test:' . $commented_entity->id(),
'comment_list',
'comment_view', 'comment_view',
'comment:' . $comment->id(), 'comment:' . $comment->id(),
'config:filter.format.plain_text', 'config:filter.format.plain_text',
......
...@@ -29,6 +29,7 @@ protected function setUp() { ...@@ -29,6 +29,7 @@ protected function setUp() {
$this->installEntitySchema('node'); $this->installEntitySchema('node');
$this->installEntitySchema('comment'); $this->installEntitySchema('comment');
$this->installSchema('comment', ['comment_entity_statistics']);
$this->installConfig(['node', 'comment']); $this->installConfig(['node', 'comment']);
entity_create('node_type', array('type' => 'page'))->save(); entity_create('node_type', array('type' => 'page'))->save();
......
...@@ -45,6 +45,10 @@ public function testLocks() { ...@@ -45,6 +45,10 @@ public function testLocks() {
$lock->expects($this->exactly(2)) $lock->expects($this->exactly(2))
->method($this->anything()); ->method($this->anything());
$container->set('lock', $lock); $container->set('lock', $lock);
$cache_tag_invalidator = $this->getMock('Drupal\Core\Cache\CacheTagsInvalidator');
$container->set('cache_tags.invalidator', $cache_tag_invalidator);
\Drupal::setContainer($container); \Drupal::setContainer($container);
$methods = get_class_methods('Drupal\comment\Entity\Comment'); $methods = get_class_methods('Drupal\comment\Entity\Comment');
unset($methods[array_search('preSave', $methods)]); unset($methods[array_search('preSave', $methods)]);
...@@ -70,6 +74,14 @@ public function testLocks() { ...@@ -70,6 +74,14 @@ public function testLocks() {
->method('getThread') ->method('getThread')
->will($this->returnValue('')); ->will($this->returnValue(''));
$parent_entity = $this->getMock('\Drupal\Core\Entity\ContentEntityInterface');
$parent_entity->expects($this->atLeastOnce())
->method('getCacheTagsToInvalidate')
->willReturn(['node:1']);
$comment->expects($this->once())
->method('getCommentedEntity')
->willReturn($parent_entity);
$entity_type = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface'); $entity_type = $this->getMock('\Drupal\Core\Entity\EntityTypeInterface');
$comment->expects($this->any()) $comment->expects($this->any())
->method('getEntityType') ->method('getEntityType')
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment