You are here

public function JsonApiDocumentTopLevelNormalizerTest::testNormalizeUuid in JSON:API 8.2

Same name and namespace in other branches
  1. 8 tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Kernel\Normalizer\JsonApiDocumentTopLevelNormalizerTest::testNormalizeUuid()

@covers ::normalize

File

tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php, line 342

Class

JsonApiDocumentTopLevelNormalizerTest
@coversDefaultClass \Drupal\jsonapi\Normalizer\JsonApiDocumentTopLevelNormalizer @group jsonapi @group legacy

Namespace

Drupal\Tests\jsonapi\Kernel\Normalizer

Code

public function testNormalizeUuid() {
  list($request, $resource_type) = $this
    ->generateProphecies('node', 'article', 'uuid');
  $resource_object = ResourceObject::createFromEntity($resource_type, $this->node);
  $include_param = 'uid,field_tags';
  $includes = $this->includeResolver
    ->resolve($resource_object, $include_param);
  $document_wrapper = new JsonApiDocumentTopLevel(new ResourceObjectData([
    $resource_object,
  ], 1), $includes, new LinkCollection([]));
  $request->query = new ParameterBag([
    'fields' => [
      'node--article' => 'title,node_type,uid,field_tags',
      'user--user' => 'name',
    ],
    'include' => $include_param,
  ]);
  $jsonapi_doc_object = $this
    ->getNormalizer()
    ->normalize($document_wrapper, 'api_json', [
    'resource_type' => $resource_type,
    'account' => NULL,
    'include' => [
      'uid',
      'field_tags',
    ],
  ]);
  $normalized = $jsonapi_doc_object
    ->getNormalization();
  $this
    ->assertStringMatchesFormat($this->node
    ->uuid(), $normalized['data']['id']);
  $this
    ->assertEquals($this->node->type->entity
    ->uuid(), $normalized['data']['relationships']['node_type']['data']['id']);
  $this
    ->assertEquals($this->user
    ->uuid(), $normalized['data']['relationships']['uid']['data']['id']);
  $this
    ->assertFalse(empty($normalized['included'][0]['id']));
  $this
    ->assertTrue(empty($normalized['meta']['omitted']));
  $this
    ->assertEquals($this->user
    ->uuid(), $normalized['included'][0]['id']);
  $this
    ->assertCount(1, $normalized['included'][0]['attributes']);
  $this
    ->assertCount(floatval(\Drupal::VERSION) >= 8.6 ? floatval(\Drupal::VERSION) >= 8.699999999999999 ? 12 : 8 : 7, $normalized['included'][1]['attributes']);

  // Make sure that the cache tags for the includes and the requested entities
  // are bubbling as expected.
  $this
    ->assertArraySubset([
    'node:1',
    'taxonomy_term:1',
    'taxonomy_term:2',
    'user:1',
  ], $jsonapi_doc_object
    ->getCacheTags());
}