function ResourceTestBase::getExpectedGetRelationshipResponse

Same name in other branches
  1. 9 core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::getExpectedGetRelationshipResponse()
  2. 8.9.x core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::getExpectedGetRelationshipResponse()
  3. 11.x core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::getExpectedGetRelationshipResponse()

Gets an expected ResourceResponse for the given relationship.

Parameters

string $relationship_field_name: The relationship for which to get an expected response.

\Drupal\Core\Entity\EntityInterface|null $entity: (optional) The entity for which to get expected relationship response.

Return value

\Drupal\jsonapi\CacheableResourceResponse The expected ResourceResponse.

3 calls to ResourceTestBase::getExpectedGetRelationshipResponse()
ResourceTestBase::doTestRelationshipGet in core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php
Performs one round of relationship route testing.
ResourceTestBase::getExpectedRelatedResponse in core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php
Builds an expected related ResourceResponse for the given field.
ResourceTestBase::testRevisions in core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php
Tests individual and collection revisions.

File

core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php, line 1694

Class

ResourceTestBase
Subclass this for every JSON:API resource type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function getExpectedGetRelationshipResponse($relationship_field_name, ?EntityInterface $entity = NULL) {
    $entity = $entity ?: $this->entity;
    $access = AccessResult::neutral()->addCacheContexts($entity->getEntityType()
        ->isRevisionable() ? [
        'url.query_args',
    ] : []);
    $access = $access->orIf(static::entityFieldAccess($entity, $this->resourceType
        ->getInternalName($relationship_field_name), 'view', $this->account));
    if (!$access->isAllowed()) {
        $via_link = Url::fromRoute(sprintf('jsonapi.%s.%s.relationship.get', static::$resourceTypeName, $relationship_field_name), [
            'entity' => $entity->uuid(),
        ]);
        return static::getAccessDeniedResponse($this->entity, $access, $via_link, $relationship_field_name, 'The current user is not allowed to view this relationship.', FALSE);
    }
    $expected_document = $this->getExpectedGetRelationshipDocument($relationship_field_name, $entity);
    $expected_cacheability = (new CacheableMetadata())->addCacheTags([
        'http_response',
    ])
        ->addCacheContexts([
        'url.site',
        'url.query_args',
    ])
        ->addCacheableDependency($entity)
        ->addCacheableDependency($access);
    $status_code = $expected_document['errors'][0]['status'] ?? 200;
    $resource_response = new CacheableResourceResponse($expected_document, $status_code);
    $resource_response->addCacheableDependency($expected_cacheability);
    return $resource_response;
}

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