function ResourceTestBase::doTestIncluded
Tests included resources.
Parameters
\Drupal\Core\Url $url: The base URL with which to test includes.
array $request_options: Request options to apply.
See also
\GuzzleHttp\ClientInterface::request()
1 call to ResourceTestBase::doTestIncluded()
- ResourceTestBase::testGetIndividual in core/
modules/ jsonapi/ tests/ src/ Functional/ ResourceTestBase.php - Tests GETting an individual resource, plus edge cases to ensure good DX.
File
-
core/
modules/ jsonapi/ tests/ src/ Functional/ ResourceTestBase.php, line 2802
Class
- ResourceTestBase
- Subclass this for every JSON:API resource type.
Namespace
Drupal\Tests\jsonapi\FunctionalCode
protected function doTestIncluded(Url $url, array $request_options) {
$relationship_field_names = $this->getRelationshipFieldNames($this->entity);
// If there are no relationship fields, we can't include anything.
if (empty($relationship_field_names)) {
return;
}
$field_sets = [
'empty' => [],
'all' => $relationship_field_names,
];
if (count($relationship_field_names) > 1) {
$about_half_the_fields = (int) floor(count($relationship_field_names) / 2);
$field_sets['some'] = array_slice($relationship_field_names, $about_half_the_fields);
$nested_includes = $this->getNestedIncludePaths();
if (!empty($nested_includes) && !in_array($nested_includes, $field_sets)) {
$field_sets['nested'] = $nested_includes;
}
}
foreach ($field_sets as $included_paths) {
$this->grantIncludedPermissions($included_paths);
$query = [
'include' => implode(',', $included_paths),
];
$url->setOption('query', $query);
$actual_response = $this->request('GET', $url, $request_options);
$expected_response = $this->getExpectedIncludedResourceResponse($included_paths, $request_options);
$expected_document = $expected_response->getResponseData();
if ($this->entity
->getEntityType()
->isRevisionable()) {
$rel_working_copy_url = clone $url;
$rel_working_copy_url->setOption('query', [
'resourceVersion' => 'rel:working-copy',
]);
$expected_document['data']['links']['working-copy']['href'] = $rel_working_copy_url->setAbsolute()
->toString();
}
// Dynamic Page Cache miss because cache should vary based on the
// 'include' query param.
$expected_cacheability = $expected_response->getCacheableMetadata();
$this->assertResourceResponse(200, $expected_document, $actual_response, $expected_cacheability->getCacheTags(), $expected_cacheability->getCacheContexts(), NULL, $this->generateDynamicPageCacheExpectedHeaderValue($this->getExpectedCacheContexts(), $expected_cacheability->getCacheMaxAge()));
}
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.