function FieldItemNormalizerTest::testNormalizeFieldItem

Same name and namespace in other branches
  1. 10 core/modules/jsonapi/tests/src/Kernel/Normalizer/FieldItemNormalizerTest.php \Drupal\Tests\jsonapi\Kernel\Normalizer\FieldItemNormalizerTest::testNormalizeFieldItem()
  2. 11.x 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 73

Class

FieldItemNormalizerTest
@coversDefaultClass <a href="/api/drupal/core%21modules%21jsonapi%21src%21Normalizer%21FieldItemNormalizer.php/class/FieldItemNormalizer/9" title="Converts the Drupal field item object to a JSON:API array structure." class="local">\Drupal\jsonapi\Normalizer\FieldItemNormalizer</a> @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.