You are here

public function JsonApiDocumentTopLevelNormalizerTest::testNormalizeUuid in JSON:API 8

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

@covers ::normalize

File

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

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');
  $document_wrapper = $this
    ->prophesize(JsonApiDocumentTopLevel::class);
  $document_wrapper
    ->getData()
    ->willReturn($this->node);
  $request->query = new ParameterBag([
    'fields' => [
      'node--article' => 'title,type,uid,field_tags',
      'user--user' => 'name',
    ],
    'include' => 'uid,field_tags',
  ]);
  $jsonapi_doc_object = $this
    ->getNormalizer()
    ->normalize($document_wrapper
    ->reveal(), 'api_json', [
    'request' => $request,
    'resource_type' => $resource_type,
  ]);
  $normalized = $jsonapi_doc_object
    ->rasterizeValue();
  $this
    ->assertStringMatchesFormat($this->node
    ->uuid(), $normalized['data']['id']);
  $this
    ->assertEquals($this->node->type->entity
    ->uuid(), $normalized['data']['relationships']['type']['data']['id']);
  $this
    ->assertEquals($this->user
    ->uuid(), $normalized['data']['relationships']['uid']['data']['id']);
  $this
    ->assertFalse(empty($normalized['included'][0]['id']));
  $this
    ->assertFalse(empty($normalized['meta']['errors']));
  $this
    ->assertEquals($this->term1
    ->uuid(), $normalized['included'][0]['id']);
  $this
    ->assertCount(floatval(\Drupal::VERSION) >= 8.6 ? 9 : 8, $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',
  ], $jsonapi_doc_object
    ->getCacheTags());
}