comparison core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Tests\serialization\Kernel;
4
5 use Drupal\Component\Utility\SafeMarkup;
6 use Drupal\entity_test\Entity\EntityTestMulRev;
7 use Drupal\Tests\rest\Functional\BcTimestampNormalizerUnixTestTrait;
8
9 /**
10 * Tests that entities can be serialized to supported core formats.
11 *
12 * @group serialization
13 */
14 class EntitySerializationTest extends NormalizerTestBase {
15
16 use BcTimestampNormalizerUnixTestTrait;
17
18 /**
19 * Modules to install.
20 *
21 * @var array
22 */
23 public static $modules = ['serialization', 'system', 'field', 'entity_test', 'text', 'filter', 'user', 'entity_serialization_test'];
24
25 /**
26 * The test values.
27 *
28 * @var array
29 */
30 protected $values;
31
32 /**
33 * The test entity.
34 *
35 * @var \Drupal\Core\Entity\ContentEntityInterface
36 */
37 protected $entity;
38
39 /**
40 * The test user.
41 *
42 * @var \Drupal\user\Entity\User
43 */
44 protected $user;
45
46 /**
47 * The serializer service.
48 *
49 * @var \Symfony\Component\Serializer\Serializer.
50 */
51 protected $serializer;
52
53 /**
54 * The class name of the test class.
55 *
56 * @var string
57 */
58 protected $entityClass = 'Drupal\entity_test\Entity\EntityTest';
59
60 protected function setUp() {
61 parent::setUp();
62
63 // User create needs sequence table.
64 $this->installSchema('system', ['sequences']);
65
66 // Create a test user to use as the entity owner.
67 $this->user = \Drupal::entityManager()->getStorage('user')->create([
68 'name' => 'serialization_test_user',
69 'mail' => 'foo@example.com',
70 'pass' => '123456',
71 ]);
72 $this->user->save();
73
74 // Create a test entity to serialize.
75 $this->values = [
76 'name' => $this->randomMachineName(),
77 'user_id' => $this->user->id(),
78 'field_test_text' => [
79 'value' => $this->randomMachineName(),
80 'format' => 'full_html',
81 ],
82 ];
83 $this->entity = EntityTestMulRev::create($this->values);
84 $this->entity->save();
85
86 $this->serializer = $this->container->get('serializer');
87
88 $this->installConfig(['field']);
89 }
90
91 /**
92 * Test the normalize function.
93 */
94 public function testNormalize() {
95 $expected = [
96 'id' => [
97 ['value' => 1],
98 ],
99 'uuid' => [
100 ['value' => $this->entity->uuid()],
101 ],
102 'langcode' => [
103 ['value' => 'en'],
104 ],
105 'name' => [
106 ['value' => $this->values['name']],
107 ],
108 'type' => [
109 ['value' => 'entity_test_mulrev'],
110 ],
111 'created' => [
112 $this->formatExpectedTimestampItemValues($this->entity->created->value),
113 ],
114 'user_id' => [
115 [
116 // id() will return the string value as it comes from the database.
117 'target_id' => (int) $this->user->id(),
118 'target_type' => $this->user->getEntityTypeId(),
119 'target_uuid' => $this->user->uuid(),
120 'url' => $this->user->url(),
121 ],
122 ],
123 'revision_id' => [
124 ['value' => 1],
125 ],
126 'default_langcode' => [
127 ['value' => TRUE],
128 ],
129 'revision_translation_affected' => [
130 ['value' => TRUE],
131 ],
132 'non_rev_field' => [],
133 'field_test_text' => [
134 [
135 'value' => $this->values['field_test_text']['value'],
136 'format' => $this->values['field_test_text']['format'],
137 ],
138 ],
139 ];
140
141 $normalized = $this->serializer->normalize($this->entity);
142
143 foreach (array_keys($expected) as $fieldName) {
144 $this->assertSame($expected[$fieldName], $normalized[$fieldName], "Normalization produces expected array for $fieldName.");
145 }
146 $this->assertEqual(array_diff_key($normalized, $expected), [], 'No unexpected data is added to the normalized array.');
147 }
148
149 /**
150 * Tests user normalization, using the entity_serialization_test module to
151 * override some default access controls.
152 */
153 public function testUserNormalize() {
154 // Test password isn't available.
155 $normalized = $this->serializer->normalize($this->user);
156
157 $this->assertFalse(array_key_exists('pass', $normalized), '"pass" key does not exist in normalized user');
158 $this->assertFalse(array_key_exists('mail', $normalized), '"mail" key does not exist in normalized user');
159
160 // Test again using our test user, so that our access control override will
161 // allow password viewing.
162 $normalized = $this->serializer->normalize($this->user, NULL, ['account' => $this->user]);
163
164 // The key 'pass' will now exist, but the password value should be
165 // normalized to NULL.
166 $this->assertIdentical($normalized['pass'], [NULL], '"pass" value is normalized to [NULL]');
167 }
168
169 /**
170 * Test registered Serializer's entity serialization for core's formats.
171 */
172 public function testSerialize() {
173 // Test that Serializer responds using the ComplexDataNormalizer and
174 // JsonEncoder. The output of ComplexDataNormalizer::normalize() is tested
175 // elsewhere, so we can just assume that it works properly here.
176 $normalized = $this->serializer->normalize($this->entity, 'json');
177 $expected = json_encode($normalized);
178 // Test 'json'.
179 $actual = $this->serializer->serialize($this->entity, 'json');
180 $this->assertIdentical($actual, $expected, 'Entity serializes to JSON when "json" is requested.');
181 $actual = $this->serializer->serialize($normalized, 'json');
182 $this->assertIdentical($actual, $expected, 'A normalized array serializes to JSON when "json" is requested');
183 // Test 'ajax'.
184 $actual = $this->serializer->serialize($this->entity, 'ajax');
185 $this->assertIdentical($actual, $expected, 'Entity serializes to JSON when "ajax" is requested.');
186 $actual = $this->serializer->serialize($normalized, 'ajax');
187 $this->assertIdentical($actual, $expected, 'A normalized array serializes to JSON when "ajax" is requested');
188
189 // Generate the expected xml in a way that allows changes to entity property
190 // order.
191 $expected_created = $this->formatExpectedTimestampItemValues($this->entity->created->value);
192
193 $expected = [
194 'id' => '<id><value>' . $this->entity->id() . '</value></id>',
195 'uuid' => '<uuid><value>' . $this->entity->uuid() . '</value></uuid>',
196 'langcode' => '<langcode><value>en</value></langcode>',
197 'name' => '<name><value>' . $this->values['name'] . '</value></name>',
198 'type' => '<type><value>entity_test_mulrev</value></type>',
199 '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>',
201 'revision_id' => '<revision_id><value>' . $this->entity->getRevisionId() . '</value></revision_id>',
202 'default_langcode' => '<default_langcode><value>1</value></default_langcode>',
203 'revision_translation_affected' => '<revision_translation_affected><value>1</value></revision_translation_affected>',
204 '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>',
206 ];
207 // Sort it in the same order as normalised.
208 $expected = array_merge($normalized, $expected);
209 // Add header and footer.
210 array_unshift($expected, '<?xml version="1.0"?>' . PHP_EOL . '<response>');
211 $expected[] = '</response>' . PHP_EOL;
212 // Reduced the array to a string.
213 $expected = implode('', $expected);
214 // Test 'xml'. The output should match that of Symfony's XmlEncoder.
215 $actual = $this->serializer->serialize($this->entity, 'xml');
216 $this->assertIdentical($actual, $expected);
217 $actual = $this->serializer->serialize($normalized, 'xml');
218 $this->assertIdentical($actual, $expected);
219 }
220
221 /**
222 * Tests denormalization of an entity.
223 */
224 public function testDenormalize() {
225 $normalized = $this->serializer->normalize($this->entity);
226
227 foreach (['json', 'xml'] as $type) {
228 $denormalized = $this->serializer->denormalize($normalized, $this->entityClass, $type, ['entity_type' => 'entity_test_mulrev']);
229 $this->assertTrue($denormalized instanceof $this->entityClass, SafeMarkup::format('Denormalized entity is an instance of @class', ['@class' => $this->entityClass]));
230 $this->assertIdentical($denormalized->getEntityTypeId(), $this->entity->getEntityTypeId(), 'Expected entity type found.');
231 $this->assertIdentical($denormalized->bundle(), $this->entity->bundle(), 'Expected entity bundle found.');
232 $this->assertIdentical($denormalized->uuid(), $this->entity->uuid(), 'Expected entity UUID found.');
233 }
234 }
235
236 }