function ResourceTestBase::doTestIncluded

Same name in other branches
  1. 9 core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::doTestIncluded()
  2. 8.9.x core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::doTestIncluded()
  3. 11.x core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\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 2755

Class

ResourceTestBase
Subclass this for every JSON:API resource type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

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 $type => $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();
        // Dynamic Page Cache miss because cache should vary based on the
        // 'include' query param.
        $expected_cacheability = $expected_response->getCacheableMetadata();
        // MISS or UNCACHEABLE depends on data. It must not be HIT.
        $dynamic_cache = $expected_cacheability->getCacheMaxAge() === 0 || !empty(array_intersect([
            'user',
            'session',
        ], $this->getExpectedCacheContexts())) ? 'UNCACHEABLE' : 'MISS';
        $this->assertResourceResponse(200, $expected_document, $actual_response, $expected_cacheability->getCacheTags(), $expected_cacheability->getCacheContexts(), FALSE, $dynamic_cache);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.