You are here

public function JsonApiRegressionTest::testRecursionDetectedWhenResponseContainsViolationsFrom3042124 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::testRecursionDetectedWhenResponseContainsViolationsFrom3042124()
  2. 10 core/modules/jsonapi/tests/src/Functional/JsonApiRegressionTest.php \Drupal\Tests\jsonapi\Functional\JsonApiRegressionTest::testRecursionDetectedWhenResponseContainsViolationsFrom3042124()

Tests that the response still has meaningful error messages.

File

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

Class

JsonApiRegressionTest
JSON:API regression tests.

Namespace

Drupal\Tests\jsonapi\Functional

Code

public function testRecursionDetectedWhenResponseContainsViolationsFrom3042124() {
  $this
    ->config('jsonapi.settings')
    ->set('read_only', FALSE)
    ->save(TRUE);

  // Set up default request.
  $url = Url::fromUri('internal:/jsonapi/node/article');
  $request_options = [
    RequestOptions::HEADERS => [
      'Content-Type' => 'application/vnd.api+json',
      'Accept' => 'application/vnd.api+json',
    ],
    RequestOptions::JSON => [
      'data' => [
        'type' => 'node--article',
        'attributes' => [],
      ],
    ],
  ];

  // Set up test users.
  $user = $this
    ->drupalCreateUser([
    'bypass node access',
  ], 'Sam');
  $admin = $this
    ->drupalCreateUser([], 'Gandalf', TRUE);

  // Make request as regular user.
  $request_options[RequestOptions::AUTH] = [
    $user
      ->getAccountName(),
    $user->pass_raw,
  ];
  $this
    ->request('POST', $url, $request_options);
  $response = $this
    ->request('POST', $url, $request_options);

  // Assert that the response has a body.
  $data = Json::decode((string) $response
    ->getBody());
  $this
    ->assertSame(422, $response
    ->getStatusCode());
  $this
    ->assertNotNull($data);
  $this
    ->assertSame(sprintf('title: This value should not be null.'), $data['errors'][0]['detail']);

  // Make request as regular user.
  $request_options[RequestOptions::AUTH] = [
    $admin
      ->getAccountName(),
    $admin->pass_raw,
  ];
  $this
    ->request('POST', $url, $request_options);
  $response = $this
    ->request('POST', $url, $request_options);

  // Assert that the response has a body.
  $data = Json::decode((string) $response
    ->getBody());
  $this
    ->assertSame(422, $response
    ->getStatusCode());
  $this
    ->assertNotNull($data);
  $this
    ->assertSame(sprintf('title: This value should not be null.'), $data['errors'][0]['detail']);
}