function NodeTest::testPostNonExistingAuthor

Same name in other branches
  1. 9 core/modules/jsonapi/tests/src/Functional/NodeTest.php \Drupal\Tests\jsonapi\Functional\NodeTest::testPostNonExistingAuthor()
  2. 8.9.x core/modules/jsonapi/tests/src/Functional/NodeTest.php \Drupal\Tests\jsonapi\Functional\NodeTest::testPostNonExistingAuthor()
  3. 11.x 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 432

Class

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

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testPostNonExistingAuthor() : void {
    $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);
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.