BlockContentCacheTagsTest.php 3.84 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\block_content\Functional;
4

5 6
use Drupal\block_content\Entity\BlockContent;
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 22

  /**
   * {@inheritdoc}
   */
23
  public static $modules = array('block_content');
24 25 26 27 28

  /**
   * {@inheritdoc}
   */
  protected function createEntity() {
29
    $block_content_type = BlockContentType::create(array(
30 31 32 33 34 35 36
      'id' => 'basic',
      'label' => 'basic',
      'revision' => FALSE
    ));
    $block_content_type->save();
    block_content_add_body_field($block_content_type->id());

37
    // Create a "Llama" custom block.
38
    $block_content = BlockContent::create(array(
39 40
      'info' => 'Llama',
      'type' => 'basic',
41 42 43 44
      'body' => array(
        'value' => 'The name "llama" was adopted by European settlers from native Peruvians.',
        'format' => 'plain_text',
      ),
45
    ));
46
    $block_content->save();
47

48
    return $block_content;
49 50
  }

51 52 53 54 55 56 57 58 59
  /**
   * {@inheritdoc}
   *
   * @see \Drupal\block_content\BlockContentAccessControlHandler::checkAccess()
   */
  protected function getAccessCacheContextsForEntity(EntityInterface $entity) {
    return [];
  }

60 61 62 63 64 65
  /**
   * {@inheritdoc}
   *
   * Each comment must have a comment body, which always has a text format.
   */
  protected function getAdditionalCacheTagsForEntity(EntityInterface $entity) {
66
    return ['config:filter.format.plain_text'];
67 68
  }

69 70 71 72
  /**
   * Tests that the block is cached with the correct contexts and tags.
   */
  public function testBlock() {
73
    $block = $this->drupalPlaceBlock('block_content:' . $this->entity->uuid());
74 75 76 77 78
    $build = $this->container->get('entity.manager')->getViewBuilder('block')->view($block, 'block');

    // Render the block.
    // @todo The request stack manipulation won't be necessary once
    //   https://www.drupal.org/node/2367555 is fixed and the
79
    //   corresponding $request->isMethodSafe() checks are removed from
80 81 82
    //   Drupal\Core\Render\Renderer.
    $request_stack = $this->container->get('request_stack');
    $request_stack->push(new Request());
83
    $this->container->get('renderer')->renderRoot($build);
84 85 86 87 88
    $request_stack->pop();

    // Expected keys, contexts, and tags for the block.
    // @see \Drupal\block\BlockViewBuilder::viewMultiple()
    $expected_block_cache_keys = ['entity_view', 'block', $block->id()];
89
    $expected_block_cache_contexts = ['languages:' . LanguageInterface::TYPE_INTERFACE, 'theme', 'user.permissions'];
90 91
    $expected_block_cache_tags = Cache::mergeTags(['block_view', 'rendered'], $block->getCacheTags());
    $expected_block_cache_tags = Cache::mergeTags($expected_block_cache_tags, $block->getPlugin()->getCacheTags());
92 93 94

    // Expected contexts and tags for the BlockContent entity.
    // @see \Drupal\Core\Entity\EntityViewBuilder::getBuildDefaults().
95
    $expected_entity_cache_contexts = ['theme'];
96 97
    $expected_entity_cache_tags = Cache::mergeTags(['block_content_view'], $this->entity->getCacheTags());
    $expected_entity_cache_tags = Cache::mergeTags($expected_entity_cache_tags, $this->getAdditionalCacheTagsForEntity($this->entity));
98 99 100 101

    // Verify that what was render cached matches the above expectations.
    $cid = $this->createCacheId($expected_block_cache_keys, $expected_block_cache_contexts);
    $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
}