|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Drupal\Tests\graphql\Kernel\Framework; |
| 4 | + |
| 5 | +use Drupal\Tests\graphql\Kernel\GraphQLTestBase; |
| 6 | +use Symfony\Component\HttpFoundation\Request; |
| 7 | + |
| 8 | +/** |
| 9 | + * Tests the automatic persisted query plugin. |
| 10 | + * |
| 11 | + * @group graphql |
| 12 | + */ |
| 13 | +class AutomaticPersistedQueriesTest extends GraphQLTestBase { |
| 14 | + |
| 15 | + /** |
| 16 | + * {@inheritdoc} |
| 17 | + */ |
| 18 | + protected function setUp(): void { |
| 19 | + parent::setUp(); |
| 20 | + $schema = <<<GQL |
| 21 | + schema { |
| 22 | + query: Query |
| 23 | + } |
| 24 | + type Query { |
| 25 | + field_one: String |
| 26 | + } |
| 27 | + GQL; |
| 28 | + |
| 29 | + $this->setUpSchema($schema); |
| 30 | + $this->mockResolver('Query', 'field_one', 'this is the field one'); |
| 31 | + |
| 32 | + /** @var \Drupal\graphql\Plugin\DataProducerPluginManager $manager */ |
| 33 | + $manager = $this->container->get('plugin.manager.graphql.persisted_query'); |
| 34 | + |
| 35 | + $this->plugin_apq = $manager->createInstance('automatic_persisted_query'); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Test the automatic persisted queries plugin. |
| 40 | + */ |
| 41 | + public function testAutomaticPersistedQueries(): void { |
| 42 | + // Before adding the persisted query plugins to the server, we want to make |
| 43 | + // sure that there are no existing plugins already there. |
| 44 | + $this->server->removeAllPersistedQueryInstances(); |
| 45 | + $this->server->addPersistedQueryInstance($this->plugin_apq); |
| 46 | + $this->server->save(); |
| 47 | + |
| 48 | + $endpoint = $this->server->get('endpoint'); |
| 49 | + |
| 50 | + $query = 'query { field_one } '; |
| 51 | + $parameters['extensions']['persistedQuery']['sha256Hash'] = 'some random hash'; |
| 52 | + |
| 53 | + // Check we get PersistedQueryNotFound. |
| 54 | + $request = Request::create($endpoint, 'GET', $parameters); |
| 55 | + $result = $this->container->get('http_kernel')->handle($request); |
| 56 | + $this->assertSame(200, $result->getStatusCode()); |
| 57 | + $this->assertSame([ |
| 58 | + 'errors' => [ |
| 59 | + [ |
| 60 | + 'message' => 'PersistedQueryNotFound', |
| 61 | + 'extensions' => ['category' => 'request'], |
| 62 | + ], |
| 63 | + ], |
| 64 | + ], json_decode($result->getContent(), TRUE)); |
| 65 | + |
| 66 | + // Post query to endpoint with a not matching hash. |
| 67 | + $content = json_encode(['query' => $query] + $parameters); |
| 68 | + $request = Request::create($endpoint, 'POST', [], [], [], [], $content); |
| 69 | + $result = $this->container->get('http_kernel')->handle($request); |
| 70 | + $this->assertSame(200, $result->getStatusCode()); |
| 71 | + $this->assertSame([ |
| 72 | + 'errors' => [ |
| 73 | + [ |
| 74 | + 'message' => 'Provided sha does not match query', |
| 75 | + 'extensions' => ['category' => 'graphql'], |
| 76 | + ], |
| 77 | + ], |
| 78 | + ], json_decode($result->getContent(), TRUE)); |
| 79 | + |
| 80 | + // Post query to endpoint to get the result and cache it. |
| 81 | + $parameters['extensions']['persistedQuery']['sha256Hash'] = hash('sha256', $query); |
| 82 | + |
| 83 | + $content = json_encode(['query' => $query] + $parameters); |
| 84 | + $request = Request::create($endpoint, 'POST', [], [], [], [], $content); |
| 85 | + $result = $this->container->get('http_kernel')->handle($request); |
| 86 | + $this->assertSame(200, $result->getStatusCode()); |
| 87 | + $this->assertSame(['data' => ['field_one' => 'this is the field one']], json_decode($result->getContent(), TRUE)); |
| 88 | + |
| 89 | + // Execute first request again. |
| 90 | + $request = Request::create($endpoint, 'GET', $parameters); |
| 91 | + $result = $this->container->get('http_kernel')->handle($request); |
| 92 | + $this->assertSame(200, $result->getStatusCode()); |
| 93 | + $this->assertSame(['data' => ['field_one' => 'this is the field one']], json_decode($result->getContent(), TRUE)); |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments