function EntitySerializationTest::testNormalize

Same name in other branches
  1. 8.9.x core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php \Drupal\Tests\serialization\Kernel\EntitySerializationTest::testNormalize()
  2. 10 core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php \Drupal\Tests\serialization\Kernel\EntitySerializationTest::testNormalize()
  3. 11.x core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php \Drupal\Tests\serialization\Kernel\EntitySerializationTest::testNormalize()

Tests the normalize function.

File

core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php, line 132

Class

EntitySerializationTest
Tests that entities can be serialized to supported core formats.

Namespace

Drupal\Tests\serialization\Kernel

Code

public function testNormalize() {
    $expected = [
        'id' => [
            [
                'value' => 1,
            ],
        ],
        'uuid' => [
            [
                'value' => $this->entity
                    ->uuid(),
            ],
        ],
        'langcode' => [
            [
                'value' => 'en',
            ],
        ],
        'name' => [
            [
                'value' => $this->values['name'],
            ],
        ],
        'type' => [
            [
                'value' => 'entity_test_mulrev',
            ],
        ],
        'created' => [
            [
                'value' => (new \DateTime())->setTimestamp((int) $this->entity
                    ->get('created')->value)
                    ->setTimezone(new \DateTimeZone('UTC'))
                    ->format(\DateTime::RFC3339),
                'format' => \DateTime::RFC3339,
            ],
        ],
        'user_id' => [
            [
                // id() will return the string value as it comes from the database.
'target_id' => (int) $this->user
                    ->id(),
                'target_type' => $this->user
                    ->getEntityTypeId(),
                'target_uuid' => $this->user
                    ->uuid(),
                'url' => $this->user
                    ->toUrl()
                    ->toString(),
            ],
        ],
        'revision_id' => [
            [
                'value' => 1,
            ],
        ],
        'default_langcode' => [
            [
                'value' => TRUE,
            ],
        ],
        'revision_translation_affected' => [
            [
                'value' => TRUE,
            ],
        ],
        'non_rev_field' => [],
        'non_mul_field' => [],
        'field_test_text' => [
            [
                'value' => $this->values['field_test_text']['value'],
                'format' => $this->values['field_test_text']['format'],
                'processed' => "<p>{$this->values['field_test_text']['value']}</p>",
            ],
        ],
    ];
    $normalized = $this->serializer
        ->normalize($this->entity);
    foreach (array_keys($expected) as $fieldName) {
        $this->assertSame($expected[$fieldName], $normalized[$fieldName], "Normalization produces expected array for {$fieldName}.");
    }
    $this->assertEquals([], array_diff_key($normalized, $expected), 'No unexpected data is added to the normalized array.');
}

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