function FieldItemNormalizerTest::testNormalizeFieldItem

Same name in other branches
  1. 9 core/modules/jsonapi/tests/src/Kernel/Normalizer/FieldItemNormalizerTest.php \Drupal\Tests\jsonapi\Kernel\Normalizer\FieldItemNormalizerTest::testNormalizeFieldItem()
  2. 10 core/modules/jsonapi/tests/src/Kernel/Normalizer/FieldItemNormalizerTest.php \Drupal\Tests\jsonapi\Kernel\Normalizer\FieldItemNormalizerTest::testNormalizeFieldItem()

Tests normalizing field item.

File

core/modules/jsonapi/tests/src/Kernel/Normalizer/FieldItemNormalizerTest.php, line 76

Class

FieldItemNormalizerTest
@coversDefaultClass \Drupal\jsonapi\Normalizer\FieldItemNormalizer @group jsonapi

Namespace

Drupal\Tests\jsonapi\Kernel\Normalizer

Code

public function testNormalizeFieldItem() : void {
    $entity = EntityTest::create([
        'name' => 'Test entity',
        'links' => [
            [
                'uri' => 'https://www.drupal.org',
                'title' => 'Drupal.org',
                'options' => [
                    'query' => 'foo=bar',
                ],
            ],
        ],
        'internal_property_value' => [
            [
                'value' => 'Internal property testing!',
            ],
        ],
        'no_main_property_value' => [
            [
                'value' => 'No main property testing!',
            ],
        ],
    ]);
    // Verify a field with one property is flattened.
    $result = $this->normalizer
        ->normalize($entity->get('name')
        ->first());
    assert($result instanceof CacheableNormalization);
    $this->assertEquals('Test entity', $result->getNormalization());
    // Verify a field with multiple public properties has all of them returned.
    $result = $this->normalizer
        ->normalize($entity->get('links')
        ->first());
    assert($result instanceof CacheableNormalization);
    $this->assertEquals([
        'uri' => 'https://www.drupal.org',
        'title' => 'Drupal.org',
        'options' => [
            'query' => 'foo=bar',
        ],
    ], $result->getNormalization());
    // Verify a field with one public property and one internal only returns the
    // public property, and is flattened.
    $result = $this->normalizer
        ->normalize($entity->get('internal_property_value')
        ->first());
    assert($result instanceof CacheableNormalization);
    // Property `internal_value` will not exist.
    $this->assertEquals('Internal property testing!', $result->getNormalization());
    // Verify a field with one public property but no main property is not
    // flattened.
    $result = $this->normalizer
        ->normalize($entity->get('no_main_property_value')
        ->first());
    assert($result instanceof CacheableNormalization);
    $this->assertEquals([
        'value' => 'No main property testing!',
    ], $result->getNormalization());
}

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