comparison core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
1 <?php 1 <?php
2 2
3 namespace Drupal\Tests\serialization\Kernel; 3 namespace Drupal\Tests\serialization\Kernel;
4 4
5 use Drupal\Component\Serialization\Json;
5 use Drupal\Component\Utility\SafeMarkup; 6 use Drupal\Component\Utility\SafeMarkup;
6 use Drupal\entity_test\Entity\EntityTestMulRev; 7 use Drupal\entity_test\Entity\EntityTestMulRev;
8 use Drupal\filter\Entity\FilterFormat;
7 use Drupal\Tests\rest\Functional\BcTimestampNormalizerUnixTestTrait; 9 use Drupal\Tests\rest\Functional\BcTimestampNormalizerUnixTestTrait;
8 10
9 /** 11 /**
10 * Tests that entities can be serialized to supported core formats. 12 * Tests that entities can be serialized to supported core formats.
11 * 13 *
60 protected function setUp() { 62 protected function setUp() {
61 parent::setUp(); 63 parent::setUp();
62 64
63 // User create needs sequence table. 65 // User create needs sequence table.
64 $this->installSchema('system', ['sequences']); 66 $this->installSchema('system', ['sequences']);
67
68 FilterFormat::create([
69 'format' => 'my_text_format',
70 'name' => 'My Text Format',
71 'filters' => [
72 'filter_html' => [
73 'module' => 'filter',
74 'status' => TRUE,
75 'weight' => 10,
76 'settings' => [
77 'allowed_html' => '<p>',
78 ],
79 ],
80 'filter_autop' => [
81 'module' => 'filter',
82 'status' => TRUE,
83 'weight' => 10,
84 'settings' => [],
85 ],
86 ],
87 ])->save();
65 88
66 // Create a test user to use as the entity owner. 89 // Create a test user to use as the entity owner.
67 $this->user = \Drupal::entityManager()->getStorage('user')->create([ 90 $this->user = \Drupal::entityManager()->getStorage('user')->create([
68 'name' => 'serialization_test_user', 91 'name' => 'serialization_test_user',
69 'mail' => 'foo@example.com', 92 'mail' => 'foo@example.com',
70 'pass' => '123456', 93 'pass' => '123456',
71 ]); 94 ]);
72 $this->user->save(); 95 $this->user->save();
73 96
74 // Create a test entity to serialize. 97 // Create a test entity to serialize.
98 $test_text_value = $this->randomMachineName();
75 $this->values = [ 99 $this->values = [
76 'name' => $this->randomMachineName(), 100 'name' => $this->randomMachineName(),
77 'user_id' => $this->user->id(), 101 'user_id' => $this->user->id(),
78 'field_test_text' => [ 102 'field_test_text' => [
79 'value' => $this->randomMachineName(), 103 'value' => $test_text_value,
80 'format' => 'full_html', 104 'format' => 'my_text_format',
81 ], 105 ],
82 ]; 106 ];
83 $this->entity = EntityTestMulRev::create($this->values); 107 $this->entity = EntityTestMulRev::create($this->values);
84 $this->entity->save(); 108 $this->entity->save();
85 109
128 ], 152 ],
129 'revision_translation_affected' => [ 153 'revision_translation_affected' => [
130 ['value' => TRUE], 154 ['value' => TRUE],
131 ], 155 ],
132 'non_rev_field' => [], 156 'non_rev_field' => [],
157 'non_mul_field' => [],
133 'field_test_text' => [ 158 'field_test_text' => [
134 [ 159 [
135 'value' => $this->values['field_test_text']['value'], 160 'value' => $this->values['field_test_text']['value'],
136 'format' => $this->values['field_test_text']['format'], 161 'format' => $this->values['field_test_text']['format'],
162 'processed' => "<p>{$this->values['field_test_text']['value']}</p>",
137 ], 163 ],
138 ], 164 ],
139 ]; 165 ];
140 166
141 $normalized = $this->serializer->normalize($this->entity); 167 $normalized = $this->serializer->normalize($this->entity);
172 public function testSerialize() { 198 public function testSerialize() {
173 // Test that Serializer responds using the ComplexDataNormalizer and 199 // Test that Serializer responds using the ComplexDataNormalizer and
174 // JsonEncoder. The output of ComplexDataNormalizer::normalize() is tested 200 // JsonEncoder. The output of ComplexDataNormalizer::normalize() is tested
175 // elsewhere, so we can just assume that it works properly here. 201 // elsewhere, so we can just assume that it works properly here.
176 $normalized = $this->serializer->normalize($this->entity, 'json'); 202 $normalized = $this->serializer->normalize($this->entity, 'json');
177 $expected = json_encode($normalized); 203 $expected = Json::encode($normalized);
178 // Test 'json'. 204 // Test 'json'.
179 $actual = $this->serializer->serialize($this->entity, 'json'); 205 $actual = $this->serializer->serialize($this->entity, 'json');
180 $this->assertIdentical($actual, $expected, 'Entity serializes to JSON when "json" is requested.'); 206 $this->assertIdentical($actual, $expected, 'Entity serializes to JSON when "json" is requested.');
181 $actual = $this->serializer->serialize($normalized, 'json'); 207 $actual = $this->serializer->serialize($normalized, 'json');
182 $this->assertIdentical($actual, $expected, 'A normalized array serializes to JSON when "json" is requested'); 208 $this->assertIdentical($actual, $expected, 'A normalized array serializes to JSON when "json" is requested');
199 'created' => '<created><value>' . $expected_created['value'] . '</value><format>' . $expected_created['format'] . '</format></created>', 225 'created' => '<created><value>' . $expected_created['value'] . '</value><format>' . $expected_created['format'] . '</format></created>',
200 '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->url() . '</url></user_id>', 226 '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->url() . '</url></user_id>',
201 'revision_id' => '<revision_id><value>' . $this->entity->getRevisionId() . '</value></revision_id>', 227 'revision_id' => '<revision_id><value>' . $this->entity->getRevisionId() . '</value></revision_id>',
202 'default_langcode' => '<default_langcode><value>1</value></default_langcode>', 228 'default_langcode' => '<default_langcode><value>1</value></default_langcode>',
203 'revision_translation_affected' => '<revision_translation_affected><value>1</value></revision_translation_affected>', 229 'revision_translation_affected' => '<revision_translation_affected><value>1</value></revision_translation_affected>',
230 'non_mul_field' => '<non_mul_field/>',
204 'non_rev_field' => '<non_rev_field/>', 231 'non_rev_field' => '<non_rev_field/>',
205 'field_test_text' => '<field_test_text><value>' . $this->values['field_test_text']['value'] . '</value><format>' . $this->values['field_test_text']['format'] . '</format></field_test_text>', 232 '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>',
206 ]; 233 ];
207 // Sort it in the same order as normalised. 234 // Sort it in the same order as normalised.
208 $expected = array_merge($normalized, $expected); 235 $expected = array_merge($normalized, $expected);
209 // Add header and footer. 236 // Add header and footer.
210 array_unshift($expected, '<?xml version="1.0"?>' . PHP_EOL . '<response>'); 237 array_unshift($expected, '<?xml version="1.0"?>' . PHP_EOL . '<response>');