Skip to content

Commit 9268332

Browse files
authored
test(caching): Add test case for operation name in cache prefix (#1375)
1 parent 840f2c8 commit 9268332

File tree

4 files changed

+48
-8
lines changed

4 files changed

+48
-8
lines changed

tests/src/Kernel/Framework/PersistedQueriesTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function testPersistedQueries(array $instanceIds, string $queryId, array
6666
}
6767
$this->server->save();
6868

69-
$result = $this->query($queryId, NULL, [], NULL, TRUE);
69+
$result = $this->query($queryId, NULL, [], [], TRUE);
7070

7171
$this->assertSame(200, $result->getStatusCode());
7272
$this->assertSame($expected, json_decode($result->getContent(), TRUE));

tests/src/Kernel/Framework/ResultCacheTest.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Drupal\Tests\graphql\Kernel\GraphQLTestBase;
1212
use GraphQL\Deferred;
1313
use Prophecy\Argument;
14+
use Symfony\Component\HttpFoundation\Request;
1415

1516
/**
1617
* Test query result caching.
@@ -385,4 +386,34 @@ function ($a, $b, $c, $d, FieldContext $field) use ($renderer) {
385386
$this->assertEquals(200, $result->getStatusCode());
386387
}
387388

389+
/**
390+
* Ensure that a different operation name does not get a cached result.
391+
*/
392+
public function testOperationNameCaching(): void {
393+
$dummy = $this->getMockBuilder(Server::class)
394+
->disableOriginalConstructor()
395+
->onlyMethods(['id'])
396+
->getMock();
397+
398+
// The dataproducer should be called twice because 2 differently named
399+
// queries are not cached.
400+
$dummy->expects($this->exactly(2))
401+
->method('id')
402+
->willReturn('test');
403+
404+
// Use the same resolver for both fields.
405+
foreach (['root', 'leakA'] as $field_name) {
406+
$this->mockResolver('Query', $field_name,
407+
function () use ($dummy) {
408+
return $dummy->id();
409+
}
410+
);
411+
}
412+
413+
// First call is uncached.
414+
$this->query('query one { root } query two { leakA }', NULL, [], [], FALSE, Request::METHOD_GET, 'one');
415+
// Second call is uncached.
416+
$this->query('query one { root } query two { leakA }', NULL, [], [], FALSE, Request::METHOD_GET, 'two');
417+
}
418+
388419
}

tests/src/Kernel/Framework/ResultTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public function testQuery(): void {
5858
* @coversClass \Drupal\graphql\Cache\RequestPolicy\DenyPost
5959
*/
6060
public function testPostQuery(): void {
61-
$result = $this->query('query { root }', NULL, [], NULL, FALSE, Request::METHOD_POST);
61+
$result = $this->query('query { root }', NULL, [], [], FALSE, Request::METHOD_POST);
6262
$this->assertSame(200, $result->getStatusCode());
6363
$this->assertSame([
6464
'data' => [

tests/src/Traits/HttpRequestTrait.php

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,22 +27,28 @@ trait HttpRequestTrait {
2727
* The server instance.
2828
* @param array $variables
2929
* Query variables.
30-
* @param array|null $extensions
30+
* @param array $extensions
3131
* The query extensions.
3232
* @param bool $persisted
3333
* Flag if the query is actually the identifier of a persisted query.
3434
* @param string $method
3535
* Method, GET or POST.
36+
* @param string $operationName
37+
* Optional operation name if $query contains multiple operations.
3638
*
3739
* @return \Symfony\Component\HttpFoundation\Response
3840
* The http response object.
3941
*/
40-
protected function query($query, $server = NULL, array $variables = [], array $extensions = NULL, $persisted = FALSE, string $method = Request::METHOD_GET) {
42+
protected function query(
43+
string $query,
44+
?Server $server = NULL,
45+
array $variables = [],
46+
array $extensions = [],
47+
bool $persisted = FALSE,
48+
string $method = Request::METHOD_GET,
49+
string $operationName = ''
50+
) {
4151
$server = $server ?: $this->server;
42-
if (!($server instanceof Server)) {
43-
throw new \LogicException('Invalid server.');
44-
}
45-
4652
$endpoint = $this->server->get('endpoint');
4753
$extensions = !empty($extensions) ? ['extensions' => $extensions] : [];
4854
// If the persisted flag is true, then instead of sending the full query to
@@ -52,6 +58,9 @@ protected function query($query, $server = NULL, array $variables = [], array $e
5258
$query_key => $query,
5359
'variables' => $variables,
5460
] + $extensions;
61+
if ($operationName) {
62+
$data['operationName'] = $operationName;
63+
}
5564
if ($method === Request::METHOD_GET) {
5665
$request = Request::create($endpoint, $method, $data);
5766
}

0 commit comments

Comments
 (0)