You are here

public function JsonApiRegressionTest::testInvalidDataTriggersUnprocessableEntityErrorFromIssue3052954 in Drupal 9

Same name and namespace in other branches
  1. 8 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 1136

Class

JsonApiRegressionTest
JSON:API regression tests.

Namespace

Drupal\Tests\jsonapi\Functional

Code

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