You are here

protected function ResourceTestBase::getExpectedGetRelationshipDocumentData in JSON:API 8

Same name and namespace in other branches
  1. 8.2 tests/src/Functional/ResourceTestBase.php \Drupal\Tests\jsonapi\Functional\ResourceTestBase::getExpectedGetRelationshipDocumentData()

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

Return value

mixed The expected document data.

3 calls to ResourceTestBase::getExpectedGetRelationshipDocumentData()
MediaTest::getExpectedGetRelationshipDocumentData in tests/src/Functional/MediaTest.php
@todo Determine if this override should be removed in https://www.drupal.org/project/jsonapi/issues/2952522
ResourceTestBase::getExpectedGetRelationshipDocument in tests/src/Functional/ResourceTestBase.php
Gets an expected document for the given relationship.
TermTest::getExpectedGetRelationshipDocumentData in tests/src/Functional/TermTest.php
Gets the expected document data for the given relationship.
2 methods override ResourceTestBase::getExpectedGetRelationshipDocumentData()
MediaTest::getExpectedGetRelationshipDocumentData in tests/src/Functional/MediaTest.php
@todo Determine if this override should be removed in https://www.drupal.org/project/jsonapi/issues/2952522
TermTest::getExpectedGetRelationshipDocumentData in tests/src/Functional/TermTest.php
Gets the expected document data for the given relationship.

File

tests/src/Functional/ResourceTestBase.php, line 1632

Class

ResourceTestBase
Subclass this for every JSON API resource type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function getExpectedGetRelationshipDocumentData($relationship_field_name, EntityInterface $entity = NULL) {
  $entity = $entity ?: $this->entity;

  /* @var \Drupal\Core\Field\FieldItemListInterface $field */
  $field = $entity->{$relationship_field_name};
  $is_multiple = $field
    ->getFieldDefinition()
    ->getFieldStorageDefinition()
    ->getCardinality() !== 1;
  if ($field
    ->isEmpty()) {
    return $is_multiple ? [] : NULL;
  }
  if (!$is_multiple) {
    $target_entity = $field->entity;
    return is_null($target_entity) ? NULL : static::toResourceIdentifier($target_entity);
  }
  else {
    return array_filter(array_map(function ($item) {
      $target_entity = $item->entity;
      return is_null($target_entity) ? NULL : static::toResourceIdentifier($target_entity);
    }, iterator_to_array($field)));
  }
}