function JsonApiDocumentTopLevelNormalizerTest::testNormalize

Same name in other branches
  1. 9 core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Kernel\Normalizer\JsonApiDocumentTopLevelNormalizerTest::testNormalize()
  2. 8.9.x core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Kernel\Normalizer\JsonApiDocumentTopLevelNormalizerTest::testNormalize()
  3. 11.x core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php \Drupal\Tests\jsonapi\Kernel\Normalizer\JsonApiDocumentTopLevelNormalizerTest::testNormalize()

@covers ::normalize

File

core/modules/jsonapi/tests/src/Kernel/Normalizer/JsonApiDocumentTopLevelNormalizerTest.php, line 264

Class

JsonApiDocumentTopLevelNormalizerTest
@coversDefaultClass \Drupal\jsonapi\Normalizer\JsonApiDocumentTopLevelNormalizer @group jsonapi @group #slow

Namespace

Drupal\Tests\jsonapi\Kernel\Normalizer

Code

public function testNormalize() : void {
    $resource_type = $this->container
        ->get('jsonapi.resource_type.repository')
        ->get('node', 'article');
    $resource_object = ResourceObject::createFromEntity($resource_type, $this->node);
    $includes = $this->includeResolver
        ->resolve($resource_object, 'uid,field_tags,field_image');
    $jsonapi_doc_object = $this->getNormalizer()
        ->normalize(new JsonApiDocumentTopLevel(new ResourceObjectData([
        $resource_object,
    ], 1), $includes, new LinkCollection([])), 'api_json', [
        'resource_type' => $resource_type,
        'account' => NULL,
        'sparse_fieldset' => [
            'node--article' => [
                'title',
                'node_type',
                'uid',
                'field_tags',
                'field_image',
            ],
            'user--user' => [
                'display_name',
            ],
        ],
        'include' => [
            'uid',
            'field_tags',
            'field_image',
        ],
    ]);
    $normalized = $jsonapi_doc_object->getNormalization();
    // @see http://jsonapi.org/format/#document-jsonapi-object
    $this->assertEquals('1.0', $normalized['jsonapi']['version']);
    $this->assertEquals('http://jsonapi.org/format/1.0/', $normalized['jsonapi']['meta']['links']['self']['href']);
    $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(),
            'meta' => [
                'drupal_internal__target_id' => 'article',
            ],
        ],
        'links' => [
            'related' => [
                'href' => Url::fromUri('internal:/jsonapi/node/article/' . $this->node
                    ->uuid() . '/node_type', [
                    'query' => [
                        'resourceVersion' => 'id:' . $this->node
                            ->getRevisionId(),
                    ],
                ])
                    ->setAbsolute()
                    ->toString(TRUE)
                    ->getGeneratedUrl(),
            ],
            'self' => [
                'href' => Url::fromUri('internal:/jsonapi/node/article/' . $this->node
                    ->uuid() . '/relationships/node_type', [
                    'query' => [
                        'resourceVersion' => 'id:' . $this->node
                            ->getRevisionId(),
                    ],
                ])
                    ->setAbsolute()
                    ->toString(TRUE)
                    ->getGeneratedUrl(),
            ],
        ],
    ], $normalized['data']['relationships']['node_type']);
    $this->assertTrue(!isset($normalized['data']['attributes']['created']));
    $this->assertEquals([
        'alt' => 'test alt',
        'title' => 'test title',
        'width' => 10,
        'height' => 11,
        'drupal_internal__target_id' => $this->file
            ->id(),
    ], $normalized['data']['relationships']['field_image']['data']['meta']);
    $this->assertSame('node--article', $normalized['data']['type']);
    $this->assertEquals([
        'data' => [
            'type' => 'user--user',
            'id' => $this->user
                ->uuid(),
            'meta' => [
                'drupal_internal__target_id' => $this->user
                    ->id(),
            ],
        ],
        'links' => [
            'self' => [
                'href' => Url::fromUri('internal:/jsonapi/node/article/' . $this->node
                    ->uuid() . '/relationships/uid', [
                    'query' => [
                        'resourceVersion' => 'id:' . $this->node
                            ->getRevisionId(),
                    ],
                ])
                    ->setAbsolute()
                    ->toString(TRUE)
                    ->getGeneratedUrl(),
            ],
            'related' => [
                'href' => Url::fromUri('internal:/jsonapi/node/article/' . $this->node
                    ->uuid() . '/uid', [
                    'query' => [
                        'resourceVersion' => 'id:' . $this->node
                            ->getRevisionId(),
                    ],
                ])
                    ->setAbsolute()
                    ->toString(TRUE)
                    ->getGeneratedUrl(),
            ],
        ],
    ], $normalized['data']['relationships']['uid']);
    $this->assertArrayNotHasKey('meta', $normalized);
    $this->assertSame($this->user
        ->uuid(), $normalized['included'][0]['id']);
    $this->assertSame('user--user', $normalized['included'][0]['type']);
    $this->assertSame('user1', $normalized['included'][0]['attributes']['display_name']);
    $this->assertCount(1, $normalized['included'][0]['attributes']);
    $this->assertSame($this->term1
        ->uuid(), $normalized['included'][1]['id']);
    $this->assertSame('taxonomy_term--tags', $normalized['included'][1]['type']);
    $this->assertSame($this->term1
        ->label(), $normalized['included'][1]['attributes']['name']);
    $this->assertCount(11, $normalized['included'][1]['attributes']);
    $this->assertTrue(!isset($normalized['included'][1]['attributes']['created']));
    // Make sure that the cache tags for the includes and the requested entities
    // are bubbling as expected.
    $this->assertEqualsCanonicalizing([
        'file:1',
        'node:1',
        'taxonomy_term:1',
        'taxonomy_term:2',
        'user:1',
    ], $jsonapi_doc_object->getCacheTags());
    $this->assertSame(Cache::PERMANENT, $jsonapi_doc_object->getCacheMaxAge());
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.