View source
<?php
namespace Drupal\Tests\comment\Functional;
use Drupal\comment\CommentInterface;
use Drupal\comment\CommentManagerInterface;
use Drupal\comment\Entity\Comment;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Language\LanguageInterface;
use Drupal\entity_test\Entity\EntityTest;
use Drupal\field\Entity\FieldConfig;
use Drupal\Tests\system\Functional\Entity\EntityWithUriCacheTagsTestBase;
use Drupal\user\Entity\Role;
use Drupal\user\RoleInterface;
class CommentCacheTagsTest extends EntityWithUriCacheTagsTestBase {
use CommentTestTrait;
protected static $modules = [
'comment',
];
protected $defaultTheme = 'stark';
protected $entityTestCamelid;
protected $entityTestHippopotamidae;
protected function setUp() : void {
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 = EntityTest::create([
'name' => 'Camelids',
'type' => 'bar',
]);
$this->entityTestCamelid
->save();
$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;
}
public function testCommentEntity() {
$this
->verifyPageCache($this->entityTestCamelid
->toUrl(), 'MISS');
$this
->verifyPageCache($this->entityTestCamelid
->toUrl(), 'HIT');
$this->entityTestHippopotamidae = EntityTest::create([
'name' => 'Hippopotamus',
'type' => 'bar',
]);
$this->entityTestHippopotamidae
->save();
$this
->verifyPageCache($this->entityTestHippopotamidae
->toUrl(), 'MISS');
$this
->verifyPageCache($this->entityTestHippopotamidae
->toUrl(), '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();
$this
->verifyPageCache($this->entityTestCamelid
->toUrl(), 'HIT');
$this
->verifyPageCache($this->entityTestHippopotamidae
->toUrl(), 'MISS');
$this
->assertSession()
->pageTextContains($hippo_comment
->getSubject());
$this->entity
->save();
$this
->verifyPageCache($this->entityTestCamelid
->toUrl(), 'MISS');
$this
->verifyPageCache($this->entityTestHippopotamidae
->toUrl(), 'HIT');
}
protected function getAdditionalCacheContextsForEntity(EntityInterface $entity) {
return [];
}
protected function getAdditionalCacheTagsForEntity(EntityInterface $entity) {
return [
'config:filter.format.plain_text',
'user:' . $entity
->getOwnerId(),
'user_view',
];
}
protected function getDefaultCacheContexts() {
return [
'languages:' . LanguageInterface::TYPE_INTERFACE,
'theme',
'user.permissions',
];
}
}