X-Git-Url: http://www.aleph1.co.uk/gitweb/?p=yaffs-website;a=blobdiff_plain;f=web%2Fcore%2Fmodules%2Frest%2Ftests%2Fsrc%2FFunctional%2FResourceTest.php;fp=web%2Fcore%2Fmodules%2Frest%2Ftests%2Fsrc%2FFunctional%2FResourceTest.php;h=1e8babe0db20aa9cbad1184c87c8f8b23372cbdb;hp=0000000000000000000000000000000000000000;hb=9917807b03b64faf00f6a1f29dcb6eafc454efa5;hpb=aea91e65e895364e460983b890e295aa5d5540a5 diff --git a/web/core/modules/rest/tests/src/Functional/ResourceTest.php b/web/core/modules/rest/tests/src/Functional/ResourceTest.php new file mode 100644 index 000000000..1e8babe0d --- /dev/null +++ b/web/core/modules/rest/tests/src/Functional/ResourceTest.php @@ -0,0 +1,160 @@ +entity = EntityTest::create([ + 'name' => $this->randomMachineName(), + 'user_id' => 1, + 'field_test_text' => [ + 0 => [ + 'value' => $this->randomString(), + 'format' => 'plain_text', + ], + ], + ]); + $this->entity->save(); + + Role::load(AccountInterface::ANONYMOUS_ROLE) + ->grantPermission('view test entity') + ->save(); + } + + /** + * Tests that a resource without formats cannot be enabled. + */ + public function testFormats() { + RestResourceConfig::create([ + 'id' => 'entity.entity_test', + 'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY, + 'configuration' => [ + 'GET' => [ + 'supported_auth' => [ + 'basic_auth', + ], + ], + ], + ])->save(); + + // Verify that accessing the resource returns 406. + $this->drupalGet($this->entity->urlInfo()->setRouteParameter('_format', 'hal_json')); + // \Drupal\Core\Routing\RequestFormatRouteFilter considers the canonical, + // non-REST route a match, but a lower quality one: no format restrictions + // means there's always a match and hence when there is no matching REST + // route, the non-REST route is used, but can't render into + // application/hal+json, so it returns a 406. + $this->assertResponse('406', 'HTTP response code is 406 when the resource does not define formats, because it falls back to the canonical, non-REST route.'); + } + + /** + * Tests that a resource without authentication cannot be enabled. + */ + public function testAuthentication() { + RestResourceConfig::create([ + 'id' => 'entity.entity_test', + 'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY, + 'configuration' => [ + 'GET' => [ + 'supported_formats' => [ + 'hal_json', + ], + ], + ], + ])->save(); + + // Verify that accessing the resource returns 401. + $this->drupalGet($this->entity->urlInfo()->setRouteParameter('_format', 'hal_json')); + // \Drupal\Core\Routing\RequestFormatRouteFilter considers the canonical, + // non-REST route a match, but a lower quality one: no format restrictions + // means there's always a match and hence when there is no matching REST + // route, the non-REST route is used, but can't render into + // application/hal+json, so it returns a 406. + $this->assertResponse('406', 'HTTP response code is 406 when the resource does not define formats, because it falls back to the canonical, non-REST route.'); + } + + /** + * Tests that serialization_class is optional. + */ + public function testSerializationClassIsOptional() { + RestResourceConfig::create([ + 'id' => 'serialization_test', + 'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY, + 'configuration' => [ + 'POST' => [ + 'supported_formats' => [ + 'json', + ], + 'supported_auth' => [ + 'cookie', + ] + ], + ], + ])->save(); + \Drupal::service('router.builder')->rebuildIfNeeded(); + + Role::load(RoleInterface::ANONYMOUS_ID) + ->grantPermission('restful post serialization_test') + ->save(); + + $serialized = $this->container->get('serializer')->serialize(['foo', 'bar'], 'json'); + $request_options = [ + RequestOptions::HEADERS => ['Content-Type' => 'application/json'], + RequestOptions::BODY => $serialized, + ]; + /** @var \GuzzleHttp\ClientInterface $client */ + $client = $this->getSession()->getDriver()->getClient()->getClient(); + $response = $client->request('POST', $this->buildUrl('serialization_test', ['query' => ['_format' => 'json']]), $request_options); + $this->assertSame(200, $response->getStatusCode()); + $this->assertSame('["foo","bar"]', (string) $response->getBody()); + } + + /** + * Tests that resource URI paths are formatted properly. + */ + public function testUriPaths() { + /** @var \Drupal\rest\Plugin\Type\ResourcePluginManager $manager */ + $manager = \Drupal::service('plugin.manager.rest'); + + foreach ($manager->getDefinitions() as $resource => $definition) { + foreach ($definition['uri_paths'] as $key => $uri_path) { + $this->assertFalse(strpos($uri_path, '//'), 'The resource URI path does not have duplicate slashes.'); + } + } + } + +}