public function EntitySerializationTest::testNormalize in Drupal 10
Same name and namespace in other branches
- 8 core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php \Drupal\Tests\serialization\Kernel\EntitySerializationTest::testNormalize()
- 9 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 129
Class
- EntitySerializationTest
- Tests that entities can be serialized to supported core formats.
Namespace
Drupal\Tests\serialization\KernelCode
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.');
}