3 namespace Drupal\Tests\comment\Kernel;
5 use Drupal\comment\Tests\CommentTestTrait;
6 use Drupal\Core\Cache\Cache;
7 use Drupal\comment\CommentInterface;
8 use Drupal\KernelTests\Core\Entity\EntityKernelTestBase;
9 use Symfony\Component\HttpFoundation\Request;
10 use Symfony\Component\HttpFoundation\Session\Session;
11 use Drupal\comment\Entity\Comment;
12 use Drupal\entity_test\Entity\EntityTest;
15 * Tests the bubbling up of comment cache tags when using the Comment list
16 * formatter on an entity.
20 class CommentDefaultFormatterCacheTagsTest extends EntityKernelTestBase {
29 public static $modules = ['entity_test', 'comment'];
34 protected function setUp() {
37 $session = new Session();
39 $request = Request::create('/');
40 $request->setSession($session);
42 /** @var \Symfony\Component\HttpFoundation\RequestStack $stack */
43 $stack = $this->container->get('request_stack');
45 $stack->push($request);
47 // Set the current user to one that can access comments. Specifically, this
48 // user does not have access to the 'administer comments' permission, to
49 // ensure only published comments are visible to the end user.
50 $current_user = $this->container->get('current_user');
51 $current_user->setAccount($this->createUser([], ['access comments']));
53 // Install tables and config needed to render comments.
54 $this->installSchema('comment', ['comment_entity_statistics']);
55 $this->installConfig(['system', 'filter', 'comment']);
57 // Comment rendering generates links, so build the router.
58 $this->container->get('router.builder')->rebuild();
60 // Set up a field, so that the entity that'll be referenced bubbles up a
61 // cache tag when rendering it entirely.
62 $this->addDefaultCommentField('entity_test', 'entity_test');
66 * Tests the bubbling of cache tags.
68 public function testCacheTags() {
69 /** @var \Drupal\Core\Render\RendererInterface $renderer */
70 $renderer = $this->container->get('renderer');
72 // Create the entity that will be commented upon.
73 $commented_entity = EntityTest::create(['name' => $this->randomMachineName()]);
74 $commented_entity->save();
76 // Verify cache tags on the rendered entity before it has comments.
77 $build = \Drupal::entityManager()
78 ->getViewBuilder('entity_test')
79 ->view($commented_entity);
80 $renderer->renderRoot($build);
81 $expected_cache_tags = [
83 'entity_test:' . $commented_entity->id(),
84 'config:core.entity_form_display.comment.comment.default',
85 'config:field.field.comment.comment.comment_body',
86 'config:field.field.entity_test.entity_test.comment',
87 'config:field.storage.comment.comment_body',
88 'config:user.settings',
90 sort($expected_cache_tags);
91 $this->assertEqual($build['#cache']['tags'], $expected_cache_tags);
93 // Create a comment on that entity. Comment loading requires that the uid
94 // also exists in the {users} table.
95 $user = $this->createUser();
97 $comment = Comment::create([
100 'value' => 'Llamas are cool!',
101 'format' => 'plain_text',
103 'entity_id' => $commented_entity->id(),
104 'entity_type' => 'entity_test',
105 'field_name' => 'comment',
106 'comment_type' => 'comment',
107 'status' => CommentInterface::PUBLISHED,
108 'uid' => $user->id(),
112 // Load commented entity so comment_count gets computed.
113 // @todo Remove the $reset = TRUE parameter after
114 // https://www.drupal.org/node/597236 lands. It's a temporary work-around.
115 $storage = $this->container->get('entity_type.manager')->getStorage('entity_test');
116 $storage->resetCache([$commented_entity->id()]);
117 $commented_entity = $storage->load($commented_entity->id());
119 // Verify cache tags on the rendered entity when it has comments.
120 $build = \Drupal::entityManager()
121 ->getViewBuilder('entity_test')
122 ->view($commented_entity);
123 $renderer->renderRoot($build);
124 $expected_cache_tags = [
126 'entity_test:' . $commented_entity->id(),
128 'comment:' . $comment->id(),
129 'config:filter.format.plain_text',
132 'config:core.entity_form_display.comment.comment.default',
133 'config:field.field.comment.comment.comment_body',
134 'config:field.field.entity_test.entity_test.comment',
135 'config:field.storage.comment.comment_body',
136 'config:user.settings',
138 sort($expected_cache_tags);
139 $this->assertEqual($build['#cache']['tags'], $expected_cache_tags);
141 // Build a render array with the entity in a sub-element so that lazy
142 // builder elements bubble up outside of the entity and we can check that
143 // it got the correct cache max age.
144 $build = ['#type' => 'container'];
145 $build['entity'] = \Drupal::entityManager()
146 ->getViewBuilder('entity_test')
147 ->view($commented_entity);
148 $renderer->renderRoot($build);
150 // The entity itself was cached but the top-level element is max-age 0 due
151 // to the bubbled up max age due to the lazy-built comment form.
152 $this->assertIdentical(Cache::PERMANENT, $build['entity']['#cache']['max-age']);
153 $this->assertIdentical(0, $build['#cache']['max-age'], 'Top level render array has max-age 0');
155 // The children (fields) of the entity render array are only built in case
157 $this->assertFalse(isset($build['entity']['comment']), 'Cache hit');