You are here

protected function ResourceTestBase::assertResourceErrorResponse in JSON:API 8

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

\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.

8 calls to ResourceTestBase::assertResourceErrorResponse()
CommentTest::testPostIndividualDxWithoutCriticalBaseFields in tests/src/Functional/CommentTest.php
Tests POSTing a comment without critical base fields.
MessageTest::testCollection in tests/src/Functional/MessageTest.php
Tests GETting a collection of resources.
ResourceTestBase::doTestRelationshipPost in tests/src/Functional/ResourceTestBase.php
Performs one round of relationship POST, PATCH and DELETE route testing.
ResourceTestBase::testCollection in tests/src/Functional/ResourceTestBase.php
Tests GETting a collection of resources.
ResourceTestBase::testGetIndividual in tests/src/Functional/ResourceTestBase.php
Tests GETting an individual resource, plus edge cases to ensure good DX.

... See full list

File

tests/src/Functional/ResourceTestBase.php, line 791

Class

ResourceTestBase
Subclass this for every JSON API resource type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function assertResourceErrorResponse($expected_status_code, $expected_message, 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) {
  $expected_error = [];
  if (!empty(Response::$statusTexts[$expected_status_code])) {
    $expected_error['title'] = Response::$statusTexts[$expected_status_code];
  }
  $expected_error['status'] = $expected_status_code;
  $expected_error['detail'] = $expected_message;
  if ($info_url = HttpExceptionNormalizer::getInfoUrl($expected_status_code)) {
    $expected_error['links']['info'] = $info_url;
  }

  // @todo Remove in https://www.drupal.org/project/jsonapi/issues/2934362.
  $expected_error['code'] = 0;
  if ($pointer !== FALSE) {
    $expected_error['source']['pointer'] = $pointer;
  }
  $expected_document = [
    '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);
}