You are here

public function NodeTest::testPostNonExistingAuthor in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/jsonapi/tests/src/Functional/NodeTest.php \Drupal\Tests\jsonapi\Functional\NodeTest::testPostNonExistingAuthor()
  2. 10 core/modules/jsonapi/tests/src/Functional/NodeTest.php \Drupal\Tests\jsonapi\Functional\NodeTest::testPostNonExistingAuthor()

Creating relationships to missing resources should be 404 per JSON:API 1.1.

See also

https://github.com/json-api/json-api/issues/1033

File

core/modules/jsonapi/tests/src/Functional/NodeTest.php, line 434

Class

NodeTest
JSON:API integration test for the "Node" content entity type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testPostNonExistingAuthor() {
  $this
    ->setUpAuthorization('POST');
  $this
    ->config('jsonapi.settings')
    ->set('read_only', FALSE)
    ->save(TRUE);
  $this
    ->grantPermissionsToTestedRole([
    'administer nodes',
  ]);
  $random_uuid = \Drupal::service('uuid')
    ->generate();
  $doc = $this
    ->getPostDocument();
  $doc['data']['relationships']['uid']['data'] = [
    'type' => 'user--user',
    'id' => $random_uuid,
  ];

  // Create node POST request.
  $url = Url::fromRoute(sprintf('jsonapi.%s.collection.post', static::$resourceTypeName));
  $request_options = $this
    ->getAuthenticationRequestOptions();
  $request_options[RequestOptions::HEADERS]['Accept'] = 'application/vnd.api+json';
  $request_options[RequestOptions::HEADERS]['Content-Type'] = 'application/vnd.api+json';
  $request_options[RequestOptions::BODY] = Json::encode($doc);

  // POST request: 404 when adding relationships to non-existing resources.
  $response = $this
    ->request('POST', $url, $request_options);
  $expected_document = [
    'errors' => [
      0 => [
        'status' => '404',
        'title' => 'Not Found',
        'detail' => "The resource identified by `user--user:{$random_uuid}` (given as a relationship item) could not be found.",
        'links' => [
          'info' => [
            'href' => HttpExceptionNormalizer::getInfoUrl(404),
          ],
          'via' => [
            'href' => $url
              ->setAbsolute()
              ->toString(),
          ],
        ],
      ],
    ],
    'jsonapi' => static::$jsonApiMember,
  ];
  $this
    ->assertResourceResponse(404, $expected_document, $response);
}