View source
<?php
namespace Drupal\Tests\comment\Kernel;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\Core\Cache\Cache;
use Drupal\comment\CommentInterface;
use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Session\Session;
use Drupal\comment\Entity\Comment;
use Drupal\entity_test\Entity\EntityTest;
class CommentDefaultFormatterCacheTagsTest extends EntityKernelTestBase {
use CommentTestTrait;
public static $modules = [
'entity_test',
'comment',
];
protected function setUp() {
parent::setUp();
$session = new Session();
$request = Request::create('/');
$request
->setSession($session);
$stack = $this->container
->get('request_stack');
$stack
->pop();
$stack
->push($request);
$current_user = $this->container
->get('current_user');
$current_user
->setAccount($this
->createUser([], [
'access comments',
]));
$this
->installSchema('comment', [
'comment_entity_statistics',
]);
$this
->installConfig([
'system',
'filter',
'comment',
]);
$this->container
->get('router.builder')
->rebuild();
$this
->addDefaultCommentField('entity_test', 'entity_test');
}
public function testCacheTags() {
$renderer = $this->container
->get('renderer');
$commented_entity = EntityTest::create([
'name' => $this
->randomMachineName(),
]);
$commented_entity
->save();
$build = \Drupal::entityTypeManager()
->getViewBuilder('entity_test')
->view($commented_entity);
$renderer
->renderRoot($build);
$expected_cache_tags = [
'entity_test_view',
'entity_test:' . $commented_entity
->id(),
'config:core.entity_form_display.comment.comment.default',
'config:field.field.comment.comment.comment_body',
'config:field.field.entity_test.entity_test.comment',
'config:field.storage.comment.comment_body',
'config:user.settings',
];
sort($expected_cache_tags);
$this
->assertEqual($build['#cache']['tags'], $expected_cache_tags);
$user = $this
->createUser();
$user
->save();
$comment = Comment::create([
'subject' => 'Llama',
'comment_body' => [
'value' => 'Llamas are cool!',
'format' => 'plain_text',
],
'entity_id' => $commented_entity
->id(),
'entity_type' => 'entity_test',
'field_name' => 'comment',
'comment_type' => 'comment',
'status' => CommentInterface::PUBLISHED,
'uid' => $user
->id(),
]);
$comment
->save();
$storage = $this->container
->get('entity_type.manager')
->getStorage('entity_test');
$storage
->resetCache([
$commented_entity
->id(),
]);
$commented_entity = $storage
->load($commented_entity
->id());
$build = \Drupal::entityTypeManager()
->getViewBuilder('entity_test')
->view($commented_entity);
$renderer
->renderRoot($build);
$expected_cache_tags = [
'entity_test_view',
'entity_test:' . $commented_entity
->id(),
'comment_view',
'comment:' . $comment
->id(),
'config:filter.format.plain_text',
'user_view',
'user:2',
'config:core.entity_form_display.comment.comment.default',
'config:field.field.comment.comment.comment_body',
'config:field.field.entity_test.entity_test.comment',
'config:field.storage.comment.comment_body',
'config:user.settings',
];
sort($expected_cache_tags);
$this
->assertEqual($build['#cache']['tags'], $expected_cache_tags);
$build = [
'#type' => 'container',
];
$build['entity'] = \Drupal::entityTypeManager()
->getViewBuilder('entity_test')
->view($commented_entity);
$renderer
->renderRoot($build);
$this
->assertIdentical(Cache::PERMANENT, $build['entity']['#cache']['max-age']);
$this
->assertIdentical(0, $build['#cache']['max-age'], 'Top level render array has max-age 0');
$this
->assertFalse(isset($build['entity']['comment']), 'Cache hit');
}
}