3 namespace Drupal\Tests\block_content\Functional;
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;
14 * Tests the Custom Block entity's cache tags.
16 * @group block_content
18 class BlockContentCacheTagsTest extends EntityCacheTagsTestBase {
23 public static $modules = ['block_content'];
28 protected function createEntity() {
29 $block_content_type = BlockContentType::create([
34 $block_content_type->save();
35 block_content_add_body_field($block_content_type->id());
37 // Create a "Llama" custom block.
38 $block_content = BlockContent::create([
42 'value' => 'The name "llama" was adopted by European settlers from native Peruvians.',
43 'format' => 'plain_text',
46 $block_content->save();
48 return $block_content;
54 * @see \Drupal\block_content\BlockContentAccessControlHandler::checkAccess()
56 protected function getAccessCacheContextsForEntity(EntityInterface $entity) {
63 * Each comment must have a comment body, which always has a text format.
65 protected function getAdditionalCacheTagsForEntity(EntityInterface $entity) {
66 return ['config:filter.format.plain_text'];
70 * Tests that the block is cached with the correct contexts and tags.
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');
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();
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());
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));
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);