bdea71df15fa1a1692b0079cf0247ddf326bccc8
[yaffs-website] / web / core / modules / comment / tests / src / Kernel / CommentDefaultFormatterCacheTagsTest.php
1 <?php
2
3 namespace Drupal\Tests\comment\Kernel;
4
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;
13
14 /**
15  * Tests the bubbling up of comment cache tags when using the Comment list
16  * formatter on an entity.
17  *
18  * @group comment
19  */
20 class CommentDefaultFormatterCacheTagsTest extends EntityKernelTestBase {
21
22   use CommentTestTrait;
23
24   /**
25    * Modules to install.
26    *
27    * @var array
28    */
29   public static $modules = ['entity_test', 'comment'];
30
31   /**
32    * {@inheritdoc}
33    */
34   protected function setUp() {
35     parent::setUp();
36
37     $session = new Session();
38
39     $request = Request::create('/');
40     $request->setSession($session);
41
42     /** @var \Symfony\Component\HttpFoundation\RequestStack $stack */
43     $stack = $this->container->get('request_stack');
44     $stack->pop();
45     $stack->push($request);
46
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']));
52
53     // Install tables and config needed to render comments.
54     $this->installSchema('comment', ['comment_entity_statistics']);
55     $this->installConfig(['system', 'filter', 'comment']);
56
57     // Comment rendering generates links, so build the router.
58     $this->container->get('router.builder')->rebuild();
59
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');
63   }
64
65   /**
66    * Tests the bubbling of cache tags.
67    */
68   public function testCacheTags() {
69     /** @var \Drupal\Core\Render\RendererInterface $renderer */
70     $renderer = $this->container->get('renderer');
71
72     // Create the entity that will be commented upon.
73     $commented_entity = EntityTest::create(['name' => $this->randomMachineName()]);
74     $commented_entity->save();
75
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 = [
82       'entity_test_view',
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',
89     ];
90     sort($expected_cache_tags);
91     $this->assertEqual($build['#cache']['tags'], $expected_cache_tags);
92
93     // Create a comment on that entity. Comment loading requires that the uid
94     // also exists in the {users} table.
95     $user = $this->createUser();
96     $user->save();
97     $comment = Comment::create([
98       'subject' => 'Llama',
99       'comment_body' => [
100         'value' => 'Llamas are cool!',
101         'format' => 'plain_text',
102       ],
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(),
109     ]);
110     $comment->save();
111
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());
118
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 = [
125       'entity_test_view',
126       'entity_test:' . $commented_entity->id(),
127       'comment_view',
128       'comment:' . $comment->id(),
129       'config:filter.format.plain_text',
130       'user_view',
131       'user:2',
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',
137     ];
138     sort($expected_cache_tags);
139     $this->assertEqual($build['#cache']['tags'], $expected_cache_tags);
140
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);
149
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');
154
155     // The children (fields) of the entity render array are only built in case
156     // of a cache miss.
157     $this->assertFalse(isset($build['entity']['comment']), 'Cache hit');
158   }
159
160 }