You are here

protected function ResourceTestBase::getExpectedGetRelationshipDocument in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::getExpectedGetRelationshipDocument()

Gets an expected document 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 document.

Return value

array The expected document array.

2 calls to ResourceTestBase::getExpectedGetRelationshipDocument()
ResourceTestBase::doTestRelationshipMutation in core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php
Performs one round of relationship POST, PATCH and DELETE route testing.
ResourceTestBase::getExpectedGetRelationshipResponse in core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php
Gets an expected ResourceResponse for the given relationship.

File

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

Class

ResourceTestBase
Subclass this for every JSON:API resource type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function getExpectedGetRelationshipDocument($relationship_field_name, EntityInterface $entity = NULL) {
  $entity = $entity ?: $this->entity;
  $entity_type_id = $entity
    ->getEntityTypeId();
  $bundle = $entity
    ->bundle();
  $id = $entity
    ->uuid();
  $self_link = Url::fromUri("base:/jsonapi/{$entity_type_id}/{$bundle}/{$id}/relationships/{$relationship_field_name}")
    ->setAbsolute();
  $related_link = Url::fromUri("base:/jsonapi/{$entity_type_id}/{$bundle}/{$id}/{$relationship_field_name}")
    ->setAbsolute();
  if (static::$resourceTypeIsVersionable) {
    assert($entity instanceof RevisionableInterface);
    $version_query = [
      'resourceVersion' => 'id:' . $entity
        ->getRevisionId(),
    ];
    $related_link
      ->setOption('query', $version_query);
  }
  $data = $this
    ->getExpectedGetRelationshipDocumentData($relationship_field_name, $entity);
  return [
    'data' => $data,
    'jsonapi' => static::$jsonApiMember,
    'links' => [
      'self' => [
        'href' => $self_link
          ->toString(TRUE)
          ->getGeneratedUrl(),
      ],
      'related' => [
        'href' => $related_link
          ->toString(TRUE)
          ->getGeneratedUrl(),
      ],
    ],
  ];
}