You are here

public function EventOnlyQueryAccessHandlerTest::testCacheableMetadataLeaks in Entity API 8

Tests cacheability with the event only query_access handler.

If there is no additional cacheablility provided to the conditions, there should be no render conrexts leaked.

File

tests/src/Kernel/QueryAccess/EventOnlyQueryAccessHandlerTest.php, line 42

Class

EventOnlyQueryAccessHandlerTest
Tests the generic query access handler.

Namespace

Drupal\Tests\entity\Kernel\QueryAccess

Code

public function testCacheableMetadataLeaks() {
  $renderer = $this->container
    ->get('renderer');
  $render_context = new RenderContext();
  $node_type_storage = $this->entityTypeManager
    ->getStorage('node_type');
  $node_type_storage
    ->create([
    'type' => 'foo',
    'name' => $this
      ->randomString(),
  ])
    ->save();
  $node_storage = $this->entityTypeManager
    ->getStorage('node');
  $node_1 = $node_storage
    ->create([
    'type' => 'foo',
    'title' => $this
      ->randomString(),
  ]);
  $node_1
    ->save();
  $node_2 = $node_storage
    ->create([
    'type' => 'bar',
    'title' => $this
      ->randomString(),
  ]);
  $node_2
    ->save();
  $renderer
    ->executeInRenderContext($render_context, static function () use ($node_storage) {
    $node_storage
      ->getQuery()
      ->execute();
  });
  $this
    ->assertTrue($render_context
    ->isEmpty(), 'Empty cacheability was not bubbled.');
  $cacheability = new CacheableMetadata();
  $cacheability
    ->addCacheContexts([
    'user.permissions',
  ]);
  \Drupal::state()
    ->set('event_only_query_acccess_cacheability', $cacheability);
  $render_context = new RenderContext();
  $renderer
    ->executeInRenderContext($render_context, static function () use ($node_storage) {
    $node_storage
      ->getQuery()
      ->execute();
  });
  $this
    ->assertFalse($render_context
    ->isEmpty(), 'Cacheability was bubbled');
  $this
    ->assertCount(1, $render_context);
  $this
    ->assertEquals([
    'user.permissions',
  ], $render_context[0]
    ->getCacheContexts());
}