You are here

protected function ResourceTestBase::assertResourceResponse in JSON:API 8

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

13 calls to ResourceTestBase::assertResourceResponse()
CommentTest::testPostIndividualSkipCommentApproval in tests/src/Functional/CommentTest.php
Tests POSTing a comment with and without 'skip comment approval'.
NodeTest::testGetIndividual in tests/src/Functional/NodeTest.php
Tests GETting an individual resource, plus edge cases to ensure good DX.
NodeTest::testPatchPath in tests/src/Functional/NodeTest.php
Tests PATCHing a node's path with and without 'create url aliases'.
ResourceTestBase::assertResourceErrorResponse in tests/src/Functional/ResourceTestBase.php
Asserts that a resource error response has the given message.
ResourceTestBase::doTestRelationshipPost in tests/src/Functional/ResourceTestBase.php
Performs one round of relationship POST, PATCH and DELETE route testing.

... See full list

File

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

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());
  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->assertSame(FALSE, $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
      ->assertSame($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
      ->assertSame($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'));
  }
}