function ResourceTestBase::assertResourceResponse
Same name in this branch
- 11.x core/modules/rest/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\rest\Functional\ResourceTestBase::assertResourceResponse()
Same name in other branches
- 9 core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::assertResourceResponse()
- 9 core/modules/rest/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\rest\Functional\ResourceTestBase::assertResourceResponse()
- 8.9.x core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::assertResourceResponse()
- 8.9.x core/modules/rest/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\rest\Functional\ResourceTestBase::assertResourceResponse()
- 10 core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::assertResourceResponse()
- 10 core/modules/rest/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\rest\Functional\ResourceTestBase::assertResourceResponse()
Asserts that a resource response has the given status code and body.
Cache max-age is not yet considered when expected header is calculated.
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.
'MISS','HIT','UNCACHEABLE (request policy)','UNCACHEABLE (response policy)'|NULL $expected_page_cache_header_value: (optional) The expected X-Drupal-Cache response header value, or NULL in case of no opinion on that. For possible string values, see the parameter type hint. Defaults to NULL.
'HIT','MISS','UNCACHEABLE (poor cacheability)','UNCACHEABLE (no cacheability)','UNCACHEABLE (request policy)','UNCACHEABLE (response policy)'|bool $expected_dynamic_page_cache_header_value: (optional) The expected X-Drupal-Dynamic-Cache response header value
- for possible string values, see the parameter type hint - or TRUE when
the value should be autogenerated from expected cache contexts, or FALSE if that header should be absent. Defaults to FALSE.
22 calls to ResourceTestBase::assertResourceResponse()
- CommentExtrasTest::testPostIndividualSkipCommentApproval in core/
modules/ jsonapi/ tests/ src/ Functional/ CommentExtrasTest.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'.
File
-
core/
modules/ jsonapi/ tests/ src/ Functional/ ResourceTestBase.php, line 707
Class
- ResourceTestBase
- Subclass this for every JSON:API resource type.
Namespace
Drupal\Tests\jsonapi\FunctionalCode
protected function assertResourceResponse($expected_status_code, $expected_document, ResponseInterface $response, $expected_cache_tags = FALSE, $expected_cache_contexts = FALSE, $expected_page_cache_header_value = NULL, $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 = $this->getDocumentFromResponse($response, FALSE);
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)) {
$actual_cache_tags = explode(' ', $response->getHeader('X-Drupal-Cache-Tags')[0]);
$tag = 'config:system.logging';
if (!in_array($tag, $expected_cache_tags) && in_array($tag, $actual_cache_tags)) {
$expected_cache_tags[] = $tag;
}
$this->assertEqualsCanonicalizing($expected_cache_tags, $actual_cache_tags);
}
// 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 !== NULL) {
$this->assertTrue($response->hasHeader('X-Drupal-Cache'));
$this->assertSame($expected_page_cache_header_value, $response->getHeader('X-Drupal-Cache')[0]);
}
// Expected Dynamic Page Cache header value: X-Drupal-Dynamic-Cache header.
if ($expected_dynamic_page_cache_header_value === FALSE) {
$this->assertFalse($response->hasHeader('X-Drupal-Dynamic-Cache'));
}
else {
$this->assertTrue($response->hasHeader('X-Drupal-Dynamic-Cache'));
$this->assertSame($expected_dynamic_page_cache_header_value === TRUE ? $this->generateDynamicPageCacheExpectedHeaderValue($expected_cache_contexts) : $expected_dynamic_page_cache_header_value, $response->getHeader('X-Drupal-Dynamic-Cache')[0]);
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.