You are here

public function ResultCacheTest::testLeakingCacheMetadata in GraphQL 8.4

Same name and namespace in other branches
  1. 8.3 tests/src/Kernel/Framework/ResultCacheTest.php \Drupal\Tests\graphql\Kernel\Framework\ResultCacheTest::testLeakingCacheMetadata()

Test behavior in case of leaking cache metadata.

Intentionally emit undeclared cache metadata as side effect of field resolvers. Should still be added to the processors result.

File

tests/src/Kernel/Framework/ResultCacheTest.php, line 319

Class

ResultCacheTest
Test query result caching.

Namespace

Drupal\Tests\graphql\Kernel\Framework

Code

public function testLeakingCacheMetadata() : void {

  /** @var \Drupal\Core\Render\RendererInterface $renderer */
  $renderer = $this->container
    ->get('renderer');
  $this
    ->mockResolver('Query', 'leakA', function ($a, $b, $c, $d, FieldContext $field) use ($renderer) {
    $el = [
      '#plain_text' => 'Leak A',
      '#cache' => [
        'tags' => [
          'a',
        ],
      ],
    ];
    $renderContext = new RenderContext();
    $value = $renderer
      ->executeInRenderContext($renderContext, function () use ($renderer, $el) {
      return $renderer
        ->render($el)
        ->__toString();
    });
    if (!$renderContext
      ->isEmpty()) {
      $field
        ->addCacheableDependency($renderContext
        ->pop());
    }
    return $value;
  });
  $this
    ->mockResolver('Query', 'leakB', function ($a, $b, $c, $d, FieldContext $field) use ($renderer) {
    $el = [
      '#plain_text' => 'Leak B',
      '#cache' => [
        'tags' => [
          'b',
        ],
      ],
    ];
    $renderContext = new RenderContext();
    $value = $renderer
      ->executeInRenderContext($renderContext, function () use ($renderer, $el) {
      return $renderer
        ->render($el)
        ->__toString();
    });
    if (!$renderContext
      ->isEmpty()) {
      $field
        ->addCacheableDependency($renderContext
        ->pop());
    }
    return new Deferred(function () use ($value) {
      return $value;
    });
  });
  $query = <<<GQL
      query {
        leakA
        leakB
      }
GQL;
  $metadata = $this
    ->defaultCacheMetaData()
    ->addCacheTags([
    'a',
    'b',
  ]);
  $this
    ->assertResults($query, [], [
    'leakA' => 'Leak A',
    'leakB' => 'Leak B',
  ], $metadata);
  $result = $this
    ->query($query);
  $this
    ->assertEquals(200, $result
    ->getStatusCode());
}