You are here

protected function ResourceTestBase::assertResourceResponse in Drupal 9

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

Asserts that a resource response has the given status code and body.

Parameters

int $expected_status_code: The expected response status.

array|null|false $expected_document: The expected document or NULL if there should not be a response body. FALSE in case this should not be asserted.

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

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.

20 calls to ResourceTestBase::assertResourceResponse()
CommentTest::testPostIndividualSkipCommentApproval in core/modules/jsonapi/tests/src/Functional/CommentTest.php
Tests POSTing a comment with and without 'skip comment approval'.
FileUploadTest::testPostFileUpload in core/modules/jsonapi/tests/src/Functional/FileUploadTest.php
Tests using the file upload POST route; needs second request to "use" file.
MenuLinkContentTest::testLinkOptionsSerialization in core/modules/jsonapi/tests/src/Functional/MenuLinkContentTest.php
Tests requests using a serialized field item property.
NodeTest::testGetIndividual in core/modules/jsonapi/tests/src/Functional/NodeTest.php
Tests GETting an individual resource, plus edge cases to ensure good DX.
NodeTest::testPatchPath in core/modules/jsonapi/tests/src/Functional/NodeTest.php
Tests PATCHing a node's path with and without 'create url aliases'.

... See full list

File

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

Class

ResourceTestBase
Subclass this for every JSON:API resource type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function assertResourceResponse($expected_status_code, $expected_document, ResponseInterface $response, $expected_cache_tags = FALSE, $expected_cache_contexts = FALSE, $expected_page_cache_header_value = FALSE, $expected_dynamic_page_cache_header_value = FALSE) {
  $this
    ->assertSame($expected_status_code, $response
    ->getStatusCode(), var_export(Json::decode((string) $response
    ->getBody()), TRUE));
  if ($expected_status_code === 204) {

    // DELETE responses should not include a Content-Type header. But Apache
    // sets it to 'text/html' by default. We also cannot detect the presence
    // of Apache either here in the CLI. For now having this documented here
    // is all we can do.

    /* $this->assertFalse($response->hasHeader('Content-Type')); */
    $this
      ->assertSame('', (string) $response
      ->getBody());
  }
  else {
    $this
      ->assertSame([
      'application/vnd.api+json',
    ], $response
      ->getHeader('Content-Type'));
    if ($expected_document !== FALSE) {
      $response_document = Json::decode((string) $response
        ->getBody());
      if ($expected_document === NULL) {
        $this
          ->assertNull($response_document);
      }
      else {
        $this
          ->assertSameDocument($expected_document, $response_document);
      }
    }
  }

  // Expected cache tags: X-Drupal-Cache-Tags header.
  $this
    ->assertSame($expected_cache_tags !== FALSE, $response
    ->hasHeader('X-Drupal-Cache-Tags'));
  if (is_array($expected_cache_tags)) {
    $this
      ->assertEqualsCanonicalizing($expected_cache_tags, explode(' ', $response
      ->getHeader('X-Drupal-Cache-Tags')[0]));
  }

  // Expected cache contexts: X-Drupal-Cache-Contexts header.
  $this
    ->assertSame($expected_cache_contexts !== FALSE, $response
    ->hasHeader('X-Drupal-Cache-Contexts'));
  if (is_array($expected_cache_contexts)) {
    $optimized_expected_cache_contexts = \Drupal::service('cache_contexts_manager')
      ->optimizeTokens($expected_cache_contexts);
    $this
      ->assertEqualsCanonicalizing($optimized_expected_cache_contexts, explode(' ', $response
      ->getHeader('X-Drupal-Cache-Contexts')[0]));
  }

  // Expected Page Cache header value: X-Drupal-Cache header.
  if ($expected_page_cache_header_value !== FALSE) {
    $this
      ->assertTrue($response
      ->hasHeader('X-Drupal-Cache'));
    $this
      ->assertSame($expected_page_cache_header_value, $response
      ->getHeader('X-Drupal-Cache')[0]);
  }
  else {
    $this
      ->assertFalse($response
      ->hasHeader('X-Drupal-Cache'));
  }

  // Expected Dynamic Page Cache header value: X-Drupal-Dynamic-Cache header.
  if ($expected_dynamic_page_cache_header_value !== FALSE) {
    $this
      ->assertTrue($response
      ->hasHeader('X-Drupal-Dynamic-Cache'));
    $this
      ->assertSame($expected_dynamic_page_cache_header_value, $response
      ->getHeader('X-Drupal-Dynamic-Cache')[0]);
  }
  else {
    $this
      ->assertFalse($response
      ->hasHeader('X-Drupal-Dynamic-Cache'));
  }
}