function ResourceTestBase::doTestRelated
Same name in other branches
- 9 core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::doTestRelated()
- 8.9.x core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::doTestRelated()
- 11.x core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::doTestRelated()
Performs one round of related route testing.
By putting this behavior in its own method, authorization and other variations can be done in the calling method around assertions. For example, it can be run once with an authorized user and again without one.
Parameters
array $request_options: Request options to apply.
See also
\GuzzleHttp\ClientInterface::request()
1 call to ResourceTestBase::doTestRelated()
- ResourceTestBase::testRelated in core/
modules/ jsonapi/ tests/ src/ Functional/ ResourceTestBase.php - Tests GET of the related resource of an individual resource.
File
-
core/
modules/ jsonapi/ tests/ src/ Functional/ ResourceTestBase.php, line 1364
Class
- ResourceTestBase
- Subclass this for every JSON:API resource type.
Namespace
Drupal\Tests\jsonapi\FunctionalCode
protected function doTestRelated(array $request_options) {
$relationship_field_names = $this->getRelationshipFieldNames($this->entity);
// If there are no relationship fields, we can't test related routes.
if (empty($relationship_field_names)) {
return;
}
// Builds an array of expected responses, keyed by relationship field name.
$expected_relationship_responses = $this->getExpectedRelatedResponses($relationship_field_names, $request_options);
// Fetches actual responses as an array keyed by relationship field name.
$related_responses = $this->getRelatedResponses($relationship_field_names, $request_options);
foreach ($relationship_field_names as $relationship_field_name) {
/** @var \Drupal\jsonapi\ResourceResponse $expected_resource_response */
$expected_resource_response = $expected_relationship_responses[$relationship_field_name];
/** @var \Psr\Http\Message\ResponseInterface $actual_response */
$actual_response = $related_responses[$relationship_field_name];
// Dynamic Page Cache miss because cache should vary based on the
// 'include' query param.
$expected_cacheability = $expected_resource_response->getCacheableMetadata();
$this->assertResourceResponse($expected_resource_response->getStatusCode(), $expected_resource_response->getResponseData(), $actual_response, $expected_cacheability->getCacheTags(), $expected_cacheability->getCacheContexts(), FALSE, $actual_response->getStatusCode() === 200 ? $expected_cacheability->getCacheMaxAge() === 0 ? 'UNCACHEABLE' : 'MISS' : (!empty(array_intersect([
'user',
'session',
], $expected_cacheability->getCacheContexts())) ? 'UNCACHEABLE' : FALSE));
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.