function JsonApiDocumentTopLevelNormalizerTest::testAliasFieldRouteException

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

Tests the message and exceptions when requesting a Label only resource.

File

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

Class

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

Namespace

Drupal\Tests\jsonapi\Kernel\Normalizer

Code

public function testAliasFieldRouteException() {
    $this->assertSame('uid', $this->resourceTypeRepository
        ->getByTypeName('node--article')
        ->getPublicName('uid'));
    $this->assertSame('roles', $this->resourceTypeRepository
        ->getByTypeName('user--user')
        ->getPublicName('roles'));
    $resource_type_field_aliases = [
        'node--article' => [
            'uid' => 'author',
        ],
        'user--user' => [
            'roles' => 'user_roles',
        ],
    ];
    \Drupal::state()->set('jsonapi_test_resource_type_builder.resource_type_field_aliases', $resource_type_field_aliases);
    Cache::invalidateTags([
        'jsonapi_resource_types',
    ]);
    $this->assertSame('author', $this->resourceTypeRepository
        ->getByTypeName('node--article')
        ->getPublicName('uid'));
    $this->assertSame('user_roles', $this->resourceTypeRepository
        ->getByTypeName('user--user')
        ->getPublicName('roles'));
    // Create the request to fetch the articles and fetch included user.
    $resource_type = $this->container
        ->get('jsonapi.resource_type.repository')
        ->get('node', 'article');
    $user = User::load($this->node
        ->getOwnerId());
    $resource_object = ResourceObject::createFromEntity($resource_type, $this->node);
    $user_resource_type = $this->container
        ->get('jsonapi.resource_type.repository')
        ->get('user', 'user');
    $resource_object_user = LabelOnlyResourceObject::createFromEntity($user_resource_type, $user);
    $includes = $this->includeResolver
        ->resolve($resource_object_user, 'user_roles');
    
    /** @var \Drupal\jsonapi\Normalizer\Value\CacheableNormalization $jsonapi_doc_object */
    $jsonapi_doc_object = $this->getNormalizer()
        ->normalize(new JsonApiDocumentTopLevel(new ResourceObjectData([
        $resource_object,
        $resource_object_user,
    ], 2), $includes, new LinkCollection([])), 'api_json', [
        'resource_type' => $resource_type,
        'account' => NULL,
        'sparse_fieldset' => [
            'node--article' => [
                'title',
                'node_type',
                'uid',
            ],
            'user--user' => [
                'user_roles',
            ],
        ],
        'include' => [
            'user_roles',
        ],
    ])
        ->getNormalization();
    $this->assertNotEmpty($jsonapi_doc_object['meta']['omitted']);
    foreach ($jsonapi_doc_object['meta']['omitted']['links'] as $key => $link) {
        if (strpos($key, 'item--') === 0) {
            // Ensure that resource link contains url with the alias field.
            $resource_link = Url::fromUri('internal:/jsonapi/user/user/' . $user->uuid() . '/user_roles')
                ->setAbsolute()
                ->toString(TRUE);
            $this->assertEquals($resource_link->getGeneratedUrl(), $link['href']);
            $this->assertEquals("The current user is not allowed to view this relationship. The user only has authorization for the 'view label' operation.", $link['meta']['detail']);
        }
    }
}

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