protected function ResourceTestBase::getExpectedIncludeResponse in JSON:API 8
Gets an expected ResourceResponse with includes for the given field set.
Parameters
string[] $include_paths: A list of include field paths for which to get an expected response.
array $request_options: Request options to apply.
Return value
\Drupal\jsonapi\ResourceResponse The expected ResourceResponse.
See also
\GuzzleHttp\ClientInterface::request()
1 call to ResourceTestBase::getExpectedIncludeResponse()
- ResourceTestBase::doTestIncluded in tests/
src/ Functional/ ResourceTestBase.php - Tests included resources.
File
- tests/
src/ Functional/ ResourceTestBase.php, line 1754
Class
- ResourceTestBase
- Subclass this for every JSON API resource type.
Namespace
Drupal\Tests\jsonapi\FunctionalCode
protected function getExpectedIncludeResponse(array $include_paths, array $request_options) {
$individual_response = $this
->getExpectedGetIndividualResourceResponse();
$expected_document = $individual_response
->getResponseData();
$self_link = Url::fromRoute(sprintf('jsonapi.%s.individual', static::$resourceTypeName), [
static::$entityTypeId => $this->entity
->uuid(),
], [
'query' => [
'include' => implode(',', $include_paths),
],
])
->setAbsolute()
->toString();
$expected_document['links']['self'] = $self_link;
// If there can be no included data, just return the response with the
// updated 'self' link as is.
if (empty($include_paths)) {
return (new ResourceResponse($expected_document))
->addCacheableDependency($individual_response
->getCacheableMetadata());
}
$resource_data = $this
->getExpectedIncludedResourceResponse($include_paths, $request_options);
$resource_document = $resource_data
->getResponseData();
if (isset($resource_document['data'])) {
foreach ($resource_document['data'] as $related_resource) {
if (empty($expected_document['included']) || !static::collectionHasResourceIdentifier($related_resource, $expected_document['included'])) {
$expected_document['included'][] = $related_resource;
}
}
}
if (!empty($resource_document['meta']['errors'])) {
foreach ($resource_document['meta']['errors'] as $error) {
// @todo remove this when inaccessible relationships are able to raise errors in https://www.drupal.org/project/jsonapi/issues/2956084.
if (strpos($error['detail'], 'The current user is not allowed to view this relationship.') !== 0) {
$expected_document['meta']['errors'][] = $error;
}
}
}
return $expected_response = (new ResourceResponse($expected_document))
->addCacheableDependency($individual_response
->getCacheableMetadata())
->addCacheableDependency($resource_data
->getCacheableMetadata());
}