You are here

public function ResultCacheTest::testLeakingCacheMetadata in GraphQL 8.3

Same name and namespace in other branches
  1. 8.4 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 267

Class

ResultCacheTest
Test query result caching.

Namespace

Drupal\Tests\graphql\Kernel\Framework

Code

public function testLeakingCacheMetadata() {

  /** @var \Drupal\Core\Render\RendererInterface $renderer */
  $renderer = $this->container
    ->get('renderer');
  $this
    ->mockField('leakA', [
    'id' => 'leakA',
    'name' => 'leakA',
    'type' => 'String',
  ], function () use ($renderer) {
    $el = [
      '#plain_text' => 'Leak A',
      '#cache' => [
        'tags' => [
          'a',
        ],
      ],
    ];
    (yield $renderer
      ->render($el)
      ->__toString());
  });
  $this
    ->mockField('leakB', [
    'id' => 'leakB',
    'name' => 'leakB',
    'type' => 'String',
  ], function () use ($renderer) {
    $el = [
      '#plain_text' => 'Leak B',
      '#cache' => [
        'tags' => [
          'b',
        ],
      ],
    ];
    $value = $renderer
      ->render($el)
      ->__toString();
    return function () use ($value) {
      (yield $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());
}