You are here

public function EntitySerializationTest::testNormalize in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php \Drupal\Tests\serialization\Kernel\EntitySerializationTest::testNormalize()

Test the normalize function.

File

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

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' => [
      $this
        ->formatExpectedTimestampItemValues($this->entity->created->value),
    ],
    '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
    ->assertEqual(array_diff_key($normalized, $expected), [], 'No unexpected data is added to the normalized array.');
}