You are here

public function EntityQueryAlterCacheabilityTest::testCacheableMetadataLeaks in Group 2.0.x

Same name and namespace in other branches
  1. 8 tests/src/Kernel/EntityQueryAlterCacheabilityTest.php \Drupal\Tests\group\Kernel\EntityQueryAlterCacheabilityTest::testCacheableMetadataLeaks()

Tests that cacheable metadata is only bubbled when there is any.

File

tests/src/Kernel/EntityQueryAlterCacheabilityTest.php, line 52

Class

EntityQueryAlterCacheabilityTest
Tests grouped entities query access cacheability.

Namespace

Drupal\Tests\group\Kernel

Code

public function testCacheableMetadataLeaks() {
  $renderer = $this->container
    ->get('renderer');
  $storage = $this->storage;

  // Create an ungrouped node. This should not trigger the query access and
  // therefore not leak cacheable metadata.
  $this
    ->createNode([
    'type' => 'page',
  ]);
  $render_context = new RenderContext();
  $renderer
    ->executeInRenderContext($render_context, static function () use ($storage) {
    $storage
      ->getQuery()
      ->execute();
  });
  $this
    ->assertTrue($render_context
    ->isEmpty(), 'Empty cacheability was not bubbled.');

  // Install the test module so we have an access plugin for nodes.
  $this
    ->enableModules([
    'group_test_plugin',
  ]);
  $this
    ->installConfig('group_test_plugin');

  // Refresh the managers so they use the new namespaces.
  $this->entityTypeManager = $this->container
    ->get('entity_type.manager');
  $this->pluginManager = $this->container
    ->get('plugin.manager.group_relation');

  // Install the plugin and add a node to a group so query access kicks in and
  // cacheable metadata is added to the query.

  /** @var \Drupal\group\Entity\Storage\GroupContentTypeStorageInterface $gct_storage */
  $gct_storage = $this->entityTypeManager
    ->getStorage('group_content_type');
  $gct_storage
    ->save($gct_storage
    ->createFromPlugin($this->groupType, 'node_as_content:page'));
  $group = $this
    ->createGroup([
    'type' => $this->groupType
      ->id(),
  ]);
  $group
    ->addContent($this
    ->createNode([
    'type' => 'page',
  ]), 'node_as_content:page');
  $render_context = new RenderContext();
  $renderer
    ->executeInRenderContext($render_context, static function () use ($storage) {
    $storage
      ->getQuery()
      ->execute();
  });
  $this
    ->assertFalse($render_context
    ->isEmpty(), 'Cacheability was bubbled');
  $this
    ->assertCount(1, $render_context);
  $this
    ->assertEqualsCanonicalizing([
    'group_content_list:plugin:node_as_content:article',
    'group_content_list:plugin:node_as_content:page',
  ], $render_context[0]
    ->getCacheTags());
}