jsonapi.services.yml | 13 +++++++---- ...ported.php => RestJsonApiFormatUnsupported.php} | 27 +++++++++------------- 2 files changed, 19 insertions(+), 21 deletions(-) diff --git a/jsonapi.services.yml b/jsonapi.services.yml index 0b47f98..ecd6c36 100644 --- a/jsonapi.services.yml +++ b/jsonapi.services.yml @@ -98,11 +98,14 @@ services: - { name: event_subscriber } arguments: ['@jsonapi.serializer', '%serializer.formats%'] - # We want the 'api_json' format to not be supported in the REST module, so we cannot register its encoder with the - # Serialization module. Hence we have an encoder without the 'encoder' tag and 'format' property. But that also means - # we cannot reuse the @serializer service. So we define our own serializer service. But we still do want to reuse the - # generic normalizers from the Serialization module. So we inject all normalizers just like the default @serializer - # service does. Only appropriate normalizers are called thanks to the supportsNormalization() API method. + # We want the 'api_json' format to not be supported in the REST module, so we + # cannot register its encoder with the Serialization module. Hence we have an + # encoder without the 'encoder' tag and 'format' property. But that also + # means we cannot reuse the @serializer service. So we define our own + # serializer service. But we still do want to reuse the generic normalizers + # from the Serialization module. So we inject all normalizers just like the + # default @serializer service does. Only appropriate normalizers are called + # thanks to the supportsNormalization() API method. # @see \Drupal\jsonapi\DependencyInjection\Compiler\RegisterNormalizerClassesCompilerPass # @see \Drupal\jsonapi\JsonapiServiceProvider::register() # @see \Drupal\Tests\jsonapi\Functional\RestJsonApiUnsupported diff --git a/tests/src/Functional/RestJsonApiUnsupported.php b/tests/src/Functional/RestJsonApiFormatUnsupported.php similarity index 76% rename from tests/src/Functional/RestJsonApiUnsupported.php rename to tests/src/Functional/RestJsonApiFormatUnsupported.php index 18e0878..1c7d783 100644 --- a/tests/src/Functional/RestJsonApiUnsupported.php +++ b/tests/src/Functional/RestJsonApiFormatUnsupported.php @@ -12,7 +12,7 @@ use Drupal\Tests\rest\Functional\ResourceTestBase; /** * @group jsonapi */ -class RestJsonApiUnsupported extends ResourceTestBase { +class RestJsonApiFormatUnsupported extends ResourceTestBase { use AnonResourceTestTrait; @@ -35,13 +35,7 @@ class RestJsonApiUnsupported extends ResourceTestBase { * {@inheritdoc} */ protected function setUpAuthorization($method) { - switch ($method) { - case 'GET': - $this->grantPermissionsToTestedRole(['access content']); - break; - default: - throw new \UnexpectedValueException(); - } + $this->grantPermissionsToTestedRole(['access content']); } /** @@ -69,19 +63,20 @@ class RestJsonApiUnsupported extends ResourceTestBase { } /** - * Deploying a REST resource using api_json format results in 406 responses. + * Tests that JSON API is not supported */ - public function testApiJsonNotSupportedInRest() { + public function testJsonApiFormatNotSupportedInRest() { + // First, verify that 'api_json' does not appear in the 'serializer.foramts' + // container parameter, which is what the REST module uses to determine + // which formats it supports. $this->assertSame(['json', 'xml'], $this->container->getParameter('serializer.formats')); + // Second, verify that provisioning a REST resource that lists 'api_json' as + // one of its formats $this->provisionResource('entity.node', ['api_json'], []); $this->setUpAuthorization('GET'); - - $url = Node::load(1)->toUrl() - ->setOption('query', ['_format' => 'api_json']); - $request_options = []; - - $response = $this->request('GET', $url, $request_options); + $url = Node::load(1)->toUrl()->setOption('query', ['_format' => 'api_json']); + $response = $this->request('GET', $url, []); $expected_body = Json::encode([ 'errors' => [ [