View source
<?php
namespace Drupal\comment\Tests;
use Drupal\comment\CommentInterface;
use Drupal\comment\CommentManagerInterface;
use Drupal\comment\Entity\Comment;
use Drupal\Core\Entity\EntityInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\system\Tests\Entity\EntityWithUriCacheTagsTestBase;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
class CommentCacheTagsTest extends EntityWithUriCacheTagsTestBase {
use CommentTestTrait;
public static $modules = array(
'comment',
);
protected $entityTestCamelid;
protected $entityTestHippopotamidae;
protected function setUp() {
parent::setUp();
$user_role = Role::load(RoleInterface::ANONYMOUS_ID);
$user_role
->grantPermission('access comments');
$user_role
->save();
}
protected function createEntity() {
$bundle = 'bar';
entity_test_create_bundle($bundle, NULL, 'entity_test');
$this
->addDefaultCommentField('entity_test', 'bar', 'comment');
$field = FieldConfig::loadByName('entity_test', 'bar', 'comment');
$field
->setSetting('default_mode', CommentManagerInterface::COMMENT_MODE_FLAT);
$field
->save();
$this->entityTestCamelid = entity_create('entity_test', array(
'name' => 'Camelids',
'type' => 'bar',
));
$this->entityTestCamelid
->save();
$comment = entity_create('comment', array(
'subject' => 'Llama',
'comment_body' => array(
'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' => \Drupal\comment\CommentInterface::PUBLISHED,
));
$comment
->save();
return $comment;
}
public function testCommentEntity() {
$this
->verifyPageCache($this->entityTestCamelid
->urlInfo(), 'MISS');
$this
->verifyPageCache($this->entityTestCamelid
->urlInfo(), 'HIT');
$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();
$this
->verifyPageCache($this->entityTestCamelid
->urlInfo(), 'HIT');
$this
->verifyPageCache($this->entityTestHippopotamidae
->urlInfo(), 'MISS');
$this
->assertText($hippo_comment
->getSubject());
$this->entity
->save();
$this
->verifyPageCache($this->entityTestCamelid
->urlInfo(), 'MISS');
$this
->verifyPageCache($this->entityTestHippopotamidae
->urlInfo(), 'HIT');
}
protected function getAdditionalCacheContextsForEntity(EntityInterface $entity) {
return [];
}
protected function getAdditionalCacheTagsForEntity(EntityInterface $entity) {
return array(
'config:filter.format.plain_text',
'user:' . $entity
->getOwnerId(),
'user_view',
);
}
}