You are here

protected function ResourceTestBase::assertResourceErrorResponse in Drupal 10

Same name in this branch
  1. 10 core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::assertResourceErrorResponse()
  2. 10 core/modules/rest/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\rest\Functional\ResourceTestBase::assertResourceErrorResponse()
Same name and namespace in other branches
  1. 8 core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::assertResourceErrorResponse()
  2. 9 core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::assertResourceErrorResponse()

Asserts that a resource error response has the given message.

Parameters

int $expected_status_code: The expected response status.

string $expected_message: The expected error message.

\Drupal\Core\Url|null $via_link: The source URL for the errors of the response. NULL if the error occurs for example during entity creation.

\Psr\Http\Message\ResponseInterface $response: The error response to assert.

string|false $pointer: The expected JSON Pointer to the associated entity in the request document. See http://jsonapi.org/format/#error-objects.

string[]|false $expected_cache_tags: (optional) The expected cache tags in the X-Drupal-Cache-Tags response header, or FALSE if that header should be absent. Defaults to FALSE.

string[]|false $expected_cache_contexts: (optional) The expected cache contexts in the X-Drupal-Cache-Contexts response header, or FALSE if that header should be absent. Defaults to FALSE.

string|false $expected_page_cache_header_value: (optional) The expected X-Drupal-Cache response header value, or FALSE if that header should be absent. Possible strings: 'MISS', 'HIT'. Defaults to FALSE.

string|false $expected_dynamic_page_cache_header_value: (optional) The expected X-Drupal-Dynamic-Cache response header value, or FALSE if that header should be absent. Possible strings: 'MISS', 'HIT'. Defaults to FALSE.

File

core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php, line 836

Class

ResourceTestBase
Subclass this for every JSON:API resource type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function assertResourceErrorResponse($expected_status_code, $expected_message, $via_link, ResponseInterface $response, $pointer = FALSE, $expected_cache_tags = FALSE, $expected_cache_contexts = FALSE, $expected_page_cache_header_value = FALSE, $expected_dynamic_page_cache_header_value = FALSE) {
  assert(is_null($via_link) || $via_link instanceof Url);
  $expected_error = [];
  if (!empty(Response::$statusTexts[$expected_status_code])) {
    $expected_error['title'] = Response::$statusTexts[$expected_status_code];
  }
  $expected_error['status'] = (string) $expected_status_code;
  $expected_error['detail'] = $expected_message;
  if ($via_link) {
    $expected_error['links']['via']['href'] = $via_link
      ->setAbsolute()
      ->toString();
  }
  if ($info_url = HttpExceptionNormalizer::getInfoUrl($expected_status_code)) {
    $expected_error['links']['info']['href'] = $info_url;
  }
  if ($pointer !== FALSE) {
    $expected_error['source']['pointer'] = $pointer;
  }
  $expected_document = [
    'jsonapi' => static::$jsonApiMember,
    'errors' => [
      0 => $expected_error,
    ],
  ];
  $this
    ->assertResourceResponse($expected_status_code, $expected_document, $response, $expected_cache_tags, $expected_cache_contexts, $expected_page_cache_header_value, $expected_dynamic_page_cache_header_value);
}