Chris@0: entity = EntityTest::create([ Chris@0: 'name' => $this->randomMachineName(), Chris@0: 'user_id' => 1, Chris@0: 'field_test_text' => [ Chris@0: 0 => [ Chris@0: 'value' => $this->randomString(), Chris@0: 'format' => 'plain_text', Chris@0: ], Chris@0: ], Chris@0: ]); Chris@0: $this->entity->save(); Chris@0: Chris@0: Role::load(AccountInterface::ANONYMOUS_ROLE) Chris@0: ->grantPermission('view test entity') Chris@0: ->save(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that a resource without formats cannot be enabled. Chris@0: */ Chris@0: public function testFormats() { Chris@0: RestResourceConfig::create([ Chris@0: 'id' => 'entity.entity_test', Chris@0: 'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY, Chris@0: 'configuration' => [ Chris@0: 'GET' => [ Chris@0: 'supported_auth' => [ Chris@0: 'basic_auth', Chris@0: ], Chris@0: ], Chris@0: ], Chris@0: ])->save(); Chris@0: Chris@0: // Verify that accessing the resource returns 406. Chris@18: $this->drupalGet($this->entity->toUrl()->setRouteParameter('_format', 'hal_json')); Chris@0: // \Drupal\Core\Routing\RequestFormatRouteFilter considers the canonical, Chris@0: // non-REST route a match, but a lower quality one: no format restrictions Chris@0: // means there's always a match and hence when there is no matching REST Chris@0: // route, the non-REST route is used, but can't render into Chris@0: // application/hal+json, so it returns a 406. Chris@0: $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.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that a resource without authentication cannot be enabled. Chris@0: */ Chris@0: public function testAuthentication() { Chris@0: RestResourceConfig::create([ Chris@0: 'id' => 'entity.entity_test', Chris@0: 'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY, Chris@0: 'configuration' => [ Chris@0: 'GET' => [ Chris@0: 'supported_formats' => [ Chris@0: 'hal_json', Chris@0: ], Chris@0: ], Chris@0: ], Chris@0: ])->save(); Chris@0: Chris@0: // Verify that accessing the resource returns 401. Chris@18: $this->drupalGet($this->entity->toUrl()->setRouteParameter('_format', 'hal_json')); Chris@0: // \Drupal\Core\Routing\RequestFormatRouteFilter considers the canonical, Chris@0: // non-REST route a match, but a lower quality one: no format restrictions Chris@0: // means there's always a match and hence when there is no matching REST Chris@0: // route, the non-REST route is used, but can't render into Chris@0: // application/hal+json, so it returns a 406. Chris@0: $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.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that serialization_class is optional. Chris@0: */ Chris@0: public function testSerializationClassIsOptional() { Chris@0: RestResourceConfig::create([ Chris@0: 'id' => 'serialization_test', Chris@0: 'granularity' => RestResourceConfigInterface::METHOD_GRANULARITY, Chris@0: 'configuration' => [ Chris@0: 'POST' => [ Chris@0: 'supported_formats' => [ Chris@0: 'json', Chris@0: ], Chris@0: 'supported_auth' => [ Chris@0: 'cookie', Chris@17: ], Chris@0: ], Chris@0: ], Chris@0: ])->save(); Chris@0: \Drupal::service('router.builder')->rebuildIfNeeded(); Chris@0: Chris@0: Role::load(RoleInterface::ANONYMOUS_ID) Chris@0: ->grantPermission('restful post serialization_test') Chris@0: ->save(); Chris@0: Chris@0: $serialized = $this->container->get('serializer')->serialize(['foo', 'bar'], 'json'); Chris@0: $request_options = [ Chris@0: RequestOptions::HEADERS => ['Content-Type' => 'application/json'], Chris@0: RequestOptions::BODY => $serialized, Chris@0: ]; Chris@0: /** @var \GuzzleHttp\ClientInterface $client */ Chris@0: $client = $this->getSession()->getDriver()->getClient()->getClient(); Chris@0: $response = $client->request('POST', $this->buildUrl('serialization_test', ['query' => ['_format' => 'json']]), $request_options); Chris@0: $this->assertSame(200, $response->getStatusCode()); Chris@0: $this->assertSame('["foo","bar"]', (string) $response->getBody()); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Tests that resource URI paths are formatted properly. Chris@0: */ Chris@0: public function testUriPaths() { Chris@0: /** @var \Drupal\rest\Plugin\Type\ResourcePluginManager $manager */ Chris@0: $manager = \Drupal::service('plugin.manager.rest'); Chris@0: Chris@0: foreach ($manager->getDefinitions() as $resource => $definition) { Chris@0: foreach ($definition['uri_paths'] as $key => $uri_path) { Chris@0: $this->assertFalse(strpos($uri_path, '//'), 'The resource URI path does not have duplicate slashes.'); Chris@0: } Chris@0: } Chris@0: } Chris@0: Chris@0: }