function JsonApiRegressionTest::testInvalidDataTriggersUnprocessableEntityErrorFromIssue3052954

Same name in other branches
  1. 9 core/modules/jsonapi/tests/src/Functional/JsonApiRegressionTest.php \Drupal\Tests\jsonapi\Functional\JsonApiRegressionTest::testInvalidDataTriggersUnprocessableEntityErrorFromIssue3052954()
  2. 8.9.x core/modules/jsonapi/tests/src/Functional/JsonApiRegressionTest.php \Drupal\Tests\jsonapi\Functional\JsonApiRegressionTest::testInvalidDataTriggersUnprocessableEntityErrorFromIssue3052954()
  3. 11.x core/modules/jsonapi/tests/src/Functional/JsonApiRegressionTest.php \Drupal\Tests\jsonapi\Functional\JsonApiRegressionTest::testInvalidDataTriggersUnprocessableEntityErrorFromIssue3052954()

Ensure POSTing invalid data results in a 422 response, not a PHP error.

See also

https://www.drupal.org/project/drupal/issues/3052954

File

core/modules/jsonapi/tests/src/Functional/JsonApiRegressionTest.php, line 659

Class

JsonApiRegressionTest
JSON:API regression tests.

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testInvalidDataTriggersUnprocessableEntityErrorFromIssue3052954() : void {
    $this->config('jsonapi.settings')
        ->set('read_only', FALSE)
        ->save(TRUE);
    // Set up data model.
    $user = $this->drupalCreateUser([
        'bypass node access',
    ]);
    // Test.
    $request_options = [
        RequestOptions::HEADERS => [
            'Content-Type' => 'application/vnd.api+json',
            'Accept' => 'application/vnd.api+json',
        ],
        RequestOptions::JSON => [
            'data' => [
                'type' => 'article',
                'attributes' => [
                    'title' => 'foobar',
                    'created' => 'not_a_date',
                ],
            ],
        ],
        RequestOptions::AUTH => [
            $user->getAccountName(),
            $user->pass_raw,
        ],
    ];
    $response = $this->request('POST', Url::fromUri('internal:/jsonapi/node/article'), $request_options);
    $this->assertSame(422, $response->getStatusCode());
}

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