public function EntitySerializationTest::testSerialize in Drupal 8
Same name and namespace in other branches
- 9 core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php \Drupal\Tests\serialization\Kernel\EntitySerializationTest::testSerialize()
Test registered Serializer's entity serialization for core's formats.
File
- core/
modules/ serialization/ tests/ src/ Kernel/ EntitySerializationTest.php, line 207
Class
- EntitySerializationTest
- Tests that entities can be serialized to supported core formats.
Namespace
Drupal\Tests\serialization\KernelCode
public function testSerialize() {
// Test that Serializer responds using the ComplexDataNormalizer and
// JsonEncoder. The output of ComplexDataNormalizer::normalize() is tested
// elsewhere, so we can just assume that it works properly here.
$normalized = $this->serializer
->normalize($this->entity, 'json');
$expected = Json::encode($normalized);
// Test 'json'.
$actual = $this->serializer
->serialize($this->entity, 'json');
$this
->assertIdentical($actual, $expected, 'Entity serializes to JSON when "json" is requested.');
$actual = $this->serializer
->serialize($normalized, 'json');
$this
->assertIdentical($actual, $expected, 'A normalized array serializes to JSON when "json" is requested');
// Test 'ajax'.
$actual = $this->serializer
->serialize($this->entity, 'ajax');
$this
->assertIdentical($actual, $expected, 'Entity serializes to JSON when "ajax" is requested.');
$actual = $this->serializer
->serialize($normalized, 'ajax');
$this
->assertIdentical($actual, $expected, 'A normalized array serializes to JSON when "ajax" is requested');
// Generate the expected xml in a way that allows changes to entity property
// order.
$expected_created = $this
->formatExpectedTimestampItemValues($this->entity->created->value);
$expected = [
'id' => '<id><value>' . $this->entity
->id() . '</value></id>',
'uuid' => '<uuid><value>' . $this->entity
->uuid() . '</value></uuid>',
'langcode' => '<langcode><value>en</value></langcode>',
'name' => '<name><value>' . $this->values['name'] . '</value></name>',
'type' => '<type><value>entity_test_mulrev</value></type>',
'created' => '<created><value>' . $expected_created['value'] . '</value><format>' . $expected_created['format'] . '</format></created>',
'user_id' => '<user_id><target_id>' . $this->user
->id() . '</target_id><target_type>' . $this->user
->getEntityTypeId() . '</target_type><target_uuid>' . $this->user
->uuid() . '</target_uuid><url>' . $this->user
->toUrl()
->toString() . '</url></user_id>',
'revision_id' => '<revision_id><value>' . $this->entity
->getRevisionId() . '</value></revision_id>',
'default_langcode' => '<default_langcode><value>1</value></default_langcode>',
'revision_translation_affected' => '<revision_translation_affected><value>1</value></revision_translation_affected>',
'non_mul_field' => '<non_mul_field/>',
'non_rev_field' => '<non_rev_field/>',
'field_test_text' => '<field_test_text><value>' . $this->values['field_test_text']['value'] . '</value><format>' . $this->values['field_test_text']['format'] . '</format><processed><![CDATA[<p>' . $this->values['field_test_text']['value'] . '</p>]]></processed></field_test_text>',
];
// Sort it in the same order as normalized.
$expected = array_merge($normalized, $expected);
// Add header and footer.
array_unshift($expected, '<?xml version="1.0"?>' . PHP_EOL . '<response>');
$expected[] = '</response>' . PHP_EOL;
// Reduced the array to a string.
$expected = implode('', $expected);
// Test 'xml'. The output should match that of Symfony's XmlEncoder.
$actual = $this->serializer
->serialize($this->entity, 'xml');
$this
->assertIdentical($actual, $expected);
$actual = $this->serializer
->serialize($normalized, 'xml');
$this
->assertIdentical($actual, $expected);
}