You are here

protected function ResourceTestBase::normalize 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::normalize()

Generates a JSON:API normalization for the given entity.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity to generate a JSON:API normalization for.

\Drupal\Core\Url $url: The URL to use as the "self" link.

Return value

array The JSON:API normalization for the given entity.

3 calls to ResourceTestBase::normalize()
ResourceTestBase::testPatchIndividual in core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php
Tests PATCHing an individual resource, plus edge cases to ensure good DX.
ResourceTestBase::testPostIndividual in core/modules/jsonapi/tests/src/Functional/ResourceTestBase.php
Tests POSTing an individual resource, plus edge cases to ensure good DX.
UserTest::testPatchSecurityOtherUser in core/modules/jsonapi/tests/src/Functional/UserTest.php
Tests PATCHing security-sensitive base fields to change other users.

File

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

Class

ResourceTestBase
Subclass this for every JSON:API resource type.

Namespace

Drupal\Tests\jsonapi\Functional

Code

protected function normalize(EntityInterface $entity, Url $url) {

  // Don't use cached normalizations in tests.
  $this->container
    ->get('cache.jsonapi_normalizations')
    ->deleteAll();
  $self_link = new Link(new CacheableMetadata(), $url, 'self');
  $resource_type = $this->container
    ->get('jsonapi.resource_type.repository')
    ->getByTypeName(static::$resourceTypeName);
  $doc = new JsonApiDocumentTopLevel(new ResourceObjectData([
    ResourceObject::createFromEntity($resource_type, $entity),
  ], 1), new NullIncludedData(), new LinkCollection([
    'self' => $self_link,
  ]));
  return $this->serializer
    ->normalize($doc, 'api_json', [
    'resource_type' => $resource_type,
    'account' => $this->account,
  ])
    ->getNormalization();
}