You are here

public function JsonApiDocumentTopLevelNormalizerTest::testNormalize 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::testNormalize()

@covers ::normalize @dataProvider normalizeValueProvider

File

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

Class

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

Namespace

Drupal\Tests\jsonapi\Kernel\Normalizer

Code

public function testNormalize($include) {
  list($request, $resource_type) = $this
    ->generateProphecies('node', 'article');
  $request->query = new ParameterBag([
    'fields' => [
      'node--article' => 'title,type,uid,field_tags,field_image',
      'user--user' => 'name',
    ],
    'include' => $include,
  ]);
  $jsonapi_doc_object = $this
    ->getNormalizer()
    ->normalize(new JsonApiDocumentTopLevel($this->node), 'api_json', [
    'request' => $request,
    'resource_type' => $resource_type,
  ]);
  $normalized = $jsonapi_doc_object
    ->rasterizeValue();

  // @see http://jsonapi.org/format/#document-jsonapi-object
  $this
    ->assertEquals($normalized['jsonapi']['version'], '1.0');
  $this
    ->assertEquals($normalized['jsonapi']['meta']['links']['self'], 'http://jsonapi.org/format/1.0/');
  $this
    ->assertSame($normalized['data']['attributes']['title'], 'dummy_title');
  $this
    ->assertEquals($normalized['data']['id'], $this->node
    ->uuid());
  $this
    ->assertSame([
    'data' => [
      'type' => 'node_type--node_type',
      'id' => NodeType::load('article')
        ->uuid(),
    ],
    'links' => [
      'self' => 'dummy_entity_link',
      'related' => 'dummy_entity_link',
    ],
  ], $normalized['data']['relationships']['type']);
  $this
    ->assertTrue(!isset($normalized['data']['attributes']['created']));
  $this
    ->assertEquals([
    'alt' => 'test alt',
    'title' => 'test title',
    'width' => 10,
    'height' => 11,
  ], $normalized['data']['relationships']['field_image']['data']['meta']);
  $this
    ->assertSame('node--article', $normalized['data']['type']);
  $this
    ->assertEquals([
    'data' => [
      'type' => 'user--user',
      'id' => $this->user
        ->uuid(),
    ],
    'links' => [
      'self' => 'dummy_entity_link',
      'related' => 'dummy_entity_link',
    ],
  ], $normalized['data']['relationships']['uid']);
  $this
    ->assertEquals("The current user is not allowed to GET the selected resource. The 'access user profiles' permission is required and the user must be active.", $normalized['meta']['errors'][0]['detail']);
  $this
    ->assertEquals(403, $normalized['meta']['errors'][0]['status']);
  $expected_index_of_term1 = floatval(\Drupal::VERSION) >= 8.6 ? 1 : 0;
  $this
    ->assertEquals($this->term1
    ->uuid(), $normalized['included'][$expected_index_of_term1]['id']);
  $this
    ->assertEquals('taxonomy_term--tags', $normalized['included'][$expected_index_of_term1]['type']);
  $this
    ->assertEquals($this->term1
    ->label(), $normalized['included'][$expected_index_of_term1]['attributes']['name']);
  $this
    ->assertCount(floatval(\Drupal::VERSION) >= 8.6 ? 9 : 8, $normalized['included'][$expected_index_of_term1]['attributes']);
  $this
    ->assertTrue(!isset($normalized['included'][$expected_index_of_term1]['attributes']['created']));

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