Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / block_content / tests / src / Functional / BlockContentCacheTagsTest.php
1 <?php
2
3 namespace Drupal\Tests\block_content\Functional;
4
5 use Drupal\block_content\Entity\BlockContent;
6 use Drupal\block_content\Entity\BlockContentType;
7 use Drupal\Core\Cache\Cache;
8 use Drupal\Core\Entity\EntityInterface;
9 use Drupal\Core\Language\LanguageInterface;
10 use Drupal\system\Tests\Entity\EntityCacheTagsTestBase;
11 use Symfony\Component\HttpFoundation\Request;
12
13 /**
14  * Tests the Custom Block entity's cache tags.
15  *
16  * @group block_content
17  */
18 class BlockContentCacheTagsTest extends EntityCacheTagsTestBase {
19
20   /**
21    * {@inheritdoc}
22    */
23   public static $modules = ['block_content'];
24
25   /**
26    * {@inheritdoc}
27    */
28   protected function createEntity() {
29     $block_content_type = BlockContentType::create([
30       'id' => 'basic',
31       'label' => 'basic',
32       'revision' => FALSE
33     ]);
34     $block_content_type->save();
35     block_content_add_body_field($block_content_type->id());
36
37     // Create a "Llama" custom block.
38     $block_content = BlockContent::create([
39       'info' => 'Llama',
40       'type' => 'basic',
41       'body' => [
42         'value' => 'The name "llama" was adopted by European settlers from native Peruvians.',
43         'format' => 'plain_text',
44       ],
45     ]);
46     $block_content->save();
47
48     return $block_content;
49   }
50
51   /**
52    * {@inheritdoc}
53    *
54    * @see \Drupal\block_content\BlockContentAccessControlHandler::checkAccess()
55    */
56   protected function getAccessCacheContextsForEntity(EntityInterface $entity) {
57     return [];
58   }
59
60   /**
61    * {@inheritdoc}
62    *
63    * Each comment must have a comment body, which always has a text format.
64    */
65   protected function getAdditionalCacheTagsForEntity(EntityInterface $entity) {
66     return ['config:filter.format.plain_text'];
67   }
68
69   /**
70    * Tests that the block is cached with the correct contexts and tags.
71    */
72   public function testBlock() {
73     $block = $this->drupalPlaceBlock('block_content:' . $this->entity->uuid());
74     $build = $this->container->get('entity.manager')->getViewBuilder('block')->view($block, 'block');
75
76     // Render the block.
77     // @todo The request stack manipulation won't be necessary once
78     //   https://www.drupal.org/node/2367555 is fixed and the
79     //   corresponding $request->isMethodSafe() checks are removed from
80     //   Drupal\Core\Render\Renderer.
81     $request_stack = $this->container->get('request_stack');
82     $request_stack->push(new Request());
83     $this->container->get('renderer')->renderRoot($build);
84     $request_stack->pop();
85
86     // Expected keys, contexts, and tags for the block.
87     // @see \Drupal\block\BlockViewBuilder::viewMultiple()
88     $expected_block_cache_keys = ['entity_view', 'block', $block->id()];
89     $expected_block_cache_contexts = ['languages:' . LanguageInterface::TYPE_INTERFACE, 'theme', 'user.permissions'];
90     $expected_block_cache_tags = Cache::mergeTags(['block_view', 'rendered'], $block->getCacheTags());
91     $expected_block_cache_tags = Cache::mergeTags($expected_block_cache_tags, $block->getPlugin()->getCacheTags());
92
93     // Expected contexts and tags for the BlockContent entity.
94     // @see \Drupal\Core\Entity\EntityViewBuilder::getBuildDefaults().
95     $expected_entity_cache_contexts = ['theme'];
96     $expected_entity_cache_tags = Cache::mergeTags(['block_content_view'], $this->entity->getCacheTags());
97     $expected_entity_cache_tags = Cache::mergeTags($expected_entity_cache_tags, $this->getAdditionalCacheTagsForEntity($this->entity));
98
99     // Verify that what was render cached matches the above expectations.
100     $cid = $this->createCacheId($expected_block_cache_keys, $expected_block_cache_contexts);
101     $redirected_cid = $this->createCacheId($expected_block_cache_keys, Cache::mergeContexts($expected_block_cache_contexts, $expected_entity_cache_contexts));
102     $this->verifyRenderCache($cid, Cache::mergeTags($expected_block_cache_tags, $expected_entity_cache_tags), ($cid !== $redirected_cid) ? $redirected_cid : NULL);
103   }
104
105 }