function JsonApiDocumentTopLevelNormalizerTest::testNormalizeUuid

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

@covers ::normalize

File

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

Class

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

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(12, $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());
}

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