Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\serialization\Kernel;
|
Chris@0
|
4
|
Chris@14
|
5 use Drupal\Component\Serialization\Json;
|
Chris@17
|
6 use Drupal\Component\Render\FormattableMarkup;
|
Chris@17
|
7 use Drupal\entity_test\Entity\EntitySerializedField;
|
Chris@0
|
8 use Drupal\entity_test\Entity\EntityTestMulRev;
|
Chris@14
|
9 use Drupal\filter\Entity\FilterFormat;
|
Chris@0
|
10 use Drupal\Tests\rest\Functional\BcTimestampNormalizerUnixTestTrait;
|
Chris@0
|
11
|
Chris@0
|
12 /**
|
Chris@0
|
13 * Tests that entities can be serialized to supported core formats.
|
Chris@0
|
14 *
|
Chris@0
|
15 * @group serialization
|
Chris@0
|
16 */
|
Chris@0
|
17 class EntitySerializationTest extends NormalizerTestBase {
|
Chris@0
|
18
|
Chris@0
|
19 use BcTimestampNormalizerUnixTestTrait;
|
Chris@0
|
20
|
Chris@0
|
21 /**
|
Chris@0
|
22 * Modules to install.
|
Chris@0
|
23 *
|
Chris@0
|
24 * @var array
|
Chris@0
|
25 */
|
Chris@0
|
26 public static $modules = ['serialization', 'system', 'field', 'entity_test', 'text', 'filter', 'user', 'entity_serialization_test'];
|
Chris@0
|
27
|
Chris@0
|
28 /**
|
Chris@0
|
29 * The test values.
|
Chris@0
|
30 *
|
Chris@0
|
31 * @var array
|
Chris@0
|
32 */
|
Chris@0
|
33 protected $values;
|
Chris@0
|
34
|
Chris@0
|
35 /**
|
Chris@0
|
36 * The test entity.
|
Chris@0
|
37 *
|
Chris@0
|
38 * @var \Drupal\Core\Entity\ContentEntityInterface
|
Chris@0
|
39 */
|
Chris@0
|
40 protected $entity;
|
Chris@0
|
41
|
Chris@0
|
42 /**
|
Chris@0
|
43 * The test user.
|
Chris@0
|
44 *
|
Chris@0
|
45 * @var \Drupal\user\Entity\User
|
Chris@0
|
46 */
|
Chris@0
|
47 protected $user;
|
Chris@0
|
48
|
Chris@0
|
49 /**
|
Chris@0
|
50 * The serializer service.
|
Chris@0
|
51 *
|
Chris@17
|
52 * @var \Symfony\Component\Serializer\Serializer
|
Chris@0
|
53 */
|
Chris@0
|
54 protected $serializer;
|
Chris@0
|
55
|
Chris@0
|
56 /**
|
Chris@0
|
57 * The class name of the test class.
|
Chris@0
|
58 *
|
Chris@0
|
59 * @var string
|
Chris@0
|
60 */
|
Chris@0
|
61 protected $entityClass = 'Drupal\entity_test\Entity\EntityTest';
|
Chris@0
|
62
|
Chris@0
|
63 protected function setUp() {
|
Chris@0
|
64 parent::setUp();
|
Chris@0
|
65
|
Chris@0
|
66 // User create needs sequence table.
|
Chris@0
|
67 $this->installSchema('system', ['sequences']);
|
Chris@0
|
68
|
Chris@14
|
69 FilterFormat::create([
|
Chris@14
|
70 'format' => 'my_text_format',
|
Chris@14
|
71 'name' => 'My Text Format',
|
Chris@14
|
72 'filters' => [
|
Chris@14
|
73 'filter_html' => [
|
Chris@14
|
74 'module' => 'filter',
|
Chris@14
|
75 'status' => TRUE,
|
Chris@14
|
76 'weight' => 10,
|
Chris@14
|
77 'settings' => [
|
Chris@14
|
78 'allowed_html' => '<p>',
|
Chris@14
|
79 ],
|
Chris@14
|
80 ],
|
Chris@14
|
81 'filter_autop' => [
|
Chris@14
|
82 'module' => 'filter',
|
Chris@14
|
83 'status' => TRUE,
|
Chris@14
|
84 'weight' => 10,
|
Chris@14
|
85 'settings' => [],
|
Chris@14
|
86 ],
|
Chris@14
|
87 ],
|
Chris@14
|
88 ])->save();
|
Chris@14
|
89
|
Chris@0
|
90 // Create a test user to use as the entity owner.
|
Chris@0
|
91 $this->user = \Drupal::entityManager()->getStorage('user')->create([
|
Chris@0
|
92 'name' => 'serialization_test_user',
|
Chris@0
|
93 'mail' => 'foo@example.com',
|
Chris@0
|
94 'pass' => '123456',
|
Chris@0
|
95 ]);
|
Chris@0
|
96 $this->user->save();
|
Chris@0
|
97
|
Chris@0
|
98 // Create a test entity to serialize.
|
Chris@14
|
99 $test_text_value = $this->randomMachineName();
|
Chris@0
|
100 $this->values = [
|
Chris@0
|
101 'name' => $this->randomMachineName(),
|
Chris@0
|
102 'user_id' => $this->user->id(),
|
Chris@0
|
103 'field_test_text' => [
|
Chris@14
|
104 'value' => $test_text_value,
|
Chris@14
|
105 'format' => 'my_text_format',
|
Chris@0
|
106 ],
|
Chris@0
|
107 ];
|
Chris@0
|
108 $this->entity = EntityTestMulRev::create($this->values);
|
Chris@0
|
109 $this->entity->save();
|
Chris@0
|
110
|
Chris@0
|
111 $this->serializer = $this->container->get('serializer');
|
Chris@0
|
112
|
Chris@0
|
113 $this->installConfig(['field']);
|
Chris@0
|
114 }
|
Chris@0
|
115
|
Chris@0
|
116 /**
|
Chris@0
|
117 * Test the normalize function.
|
Chris@0
|
118 */
|
Chris@0
|
119 public function testNormalize() {
|
Chris@0
|
120 $expected = [
|
Chris@0
|
121 'id' => [
|
Chris@0
|
122 ['value' => 1],
|
Chris@0
|
123 ],
|
Chris@0
|
124 'uuid' => [
|
Chris@0
|
125 ['value' => $this->entity->uuid()],
|
Chris@0
|
126 ],
|
Chris@0
|
127 'langcode' => [
|
Chris@0
|
128 ['value' => 'en'],
|
Chris@0
|
129 ],
|
Chris@0
|
130 'name' => [
|
Chris@0
|
131 ['value' => $this->values['name']],
|
Chris@0
|
132 ],
|
Chris@0
|
133 'type' => [
|
Chris@0
|
134 ['value' => 'entity_test_mulrev'],
|
Chris@0
|
135 ],
|
Chris@0
|
136 'created' => [
|
Chris@0
|
137 $this->formatExpectedTimestampItemValues($this->entity->created->value),
|
Chris@0
|
138 ],
|
Chris@0
|
139 'user_id' => [
|
Chris@0
|
140 [
|
Chris@0
|
141 // id() will return the string value as it comes from the database.
|
Chris@0
|
142 'target_id' => (int) $this->user->id(),
|
Chris@0
|
143 'target_type' => $this->user->getEntityTypeId(),
|
Chris@0
|
144 'target_uuid' => $this->user->uuid(),
|
Chris@18
|
145 'url' => $this->user->toUrl()->toString(),
|
Chris@0
|
146 ],
|
Chris@0
|
147 ],
|
Chris@0
|
148 'revision_id' => [
|
Chris@0
|
149 ['value' => 1],
|
Chris@0
|
150 ],
|
Chris@0
|
151 'default_langcode' => [
|
Chris@0
|
152 ['value' => TRUE],
|
Chris@0
|
153 ],
|
Chris@0
|
154 'revision_translation_affected' => [
|
Chris@0
|
155 ['value' => TRUE],
|
Chris@0
|
156 ],
|
Chris@0
|
157 'non_rev_field' => [],
|
Chris@14
|
158 'non_mul_field' => [],
|
Chris@0
|
159 'field_test_text' => [
|
Chris@0
|
160 [
|
Chris@0
|
161 'value' => $this->values['field_test_text']['value'],
|
Chris@0
|
162 'format' => $this->values['field_test_text']['format'],
|
Chris@14
|
163 'processed' => "<p>{$this->values['field_test_text']['value']}</p>",
|
Chris@0
|
164 ],
|
Chris@0
|
165 ],
|
Chris@0
|
166 ];
|
Chris@0
|
167
|
Chris@0
|
168 $normalized = $this->serializer->normalize($this->entity);
|
Chris@0
|
169
|
Chris@0
|
170 foreach (array_keys($expected) as $fieldName) {
|
Chris@0
|
171 $this->assertSame($expected[$fieldName], $normalized[$fieldName], "Normalization produces expected array for $fieldName.");
|
Chris@0
|
172 }
|
Chris@0
|
173 $this->assertEqual(array_diff_key($normalized, $expected), [], 'No unexpected data is added to the normalized array.');
|
Chris@0
|
174 }
|
Chris@0
|
175
|
Chris@0
|
176 /**
|
Chris@0
|
177 * Tests user normalization, using the entity_serialization_test module to
|
Chris@0
|
178 * override some default access controls.
|
Chris@0
|
179 */
|
Chris@0
|
180 public function testUserNormalize() {
|
Chris@0
|
181 // Test password isn't available.
|
Chris@0
|
182 $normalized = $this->serializer->normalize($this->user);
|
Chris@0
|
183
|
Chris@0
|
184 $this->assertFalse(array_key_exists('pass', $normalized), '"pass" key does not exist in normalized user');
|
Chris@0
|
185 $this->assertFalse(array_key_exists('mail', $normalized), '"mail" key does not exist in normalized user');
|
Chris@0
|
186
|
Chris@0
|
187 // Test again using our test user, so that our access control override will
|
Chris@0
|
188 // allow password viewing.
|
Chris@0
|
189 $normalized = $this->serializer->normalize($this->user, NULL, ['account' => $this->user]);
|
Chris@0
|
190
|
Chris@0
|
191 // The key 'pass' will now exist, but the password value should be
|
Chris@0
|
192 // normalized to NULL.
|
Chris@0
|
193 $this->assertIdentical($normalized['pass'], [NULL], '"pass" value is normalized to [NULL]');
|
Chris@0
|
194 }
|
Chris@0
|
195
|
Chris@0
|
196 /**
|
Chris@0
|
197 * Test registered Serializer's entity serialization for core's formats.
|
Chris@0
|
198 */
|
Chris@0
|
199 public function testSerialize() {
|
Chris@0
|
200 // Test that Serializer responds using the ComplexDataNormalizer and
|
Chris@0
|
201 // JsonEncoder. The output of ComplexDataNormalizer::normalize() is tested
|
Chris@0
|
202 // elsewhere, so we can just assume that it works properly here.
|
Chris@0
|
203 $normalized = $this->serializer->normalize($this->entity, 'json');
|
Chris@14
|
204 $expected = Json::encode($normalized);
|
Chris@0
|
205 // Test 'json'.
|
Chris@0
|
206 $actual = $this->serializer->serialize($this->entity, 'json');
|
Chris@0
|
207 $this->assertIdentical($actual, $expected, 'Entity serializes to JSON when "json" is requested.');
|
Chris@0
|
208 $actual = $this->serializer->serialize($normalized, 'json');
|
Chris@0
|
209 $this->assertIdentical($actual, $expected, 'A normalized array serializes to JSON when "json" is requested');
|
Chris@0
|
210 // Test 'ajax'.
|
Chris@0
|
211 $actual = $this->serializer->serialize($this->entity, 'ajax');
|
Chris@0
|
212 $this->assertIdentical($actual, $expected, 'Entity serializes to JSON when "ajax" is requested.');
|
Chris@0
|
213 $actual = $this->serializer->serialize($normalized, 'ajax');
|
Chris@0
|
214 $this->assertIdentical($actual, $expected, 'A normalized array serializes to JSON when "ajax" is requested');
|
Chris@0
|
215
|
Chris@0
|
216 // Generate the expected xml in a way that allows changes to entity property
|
Chris@0
|
217 // order.
|
Chris@0
|
218 $expected_created = $this->formatExpectedTimestampItemValues($this->entity->created->value);
|
Chris@0
|
219
|
Chris@0
|
220 $expected = [
|
Chris@0
|
221 'id' => '<id><value>' . $this->entity->id() . '</value></id>',
|
Chris@0
|
222 'uuid' => '<uuid><value>' . $this->entity->uuid() . '</value></uuid>',
|
Chris@0
|
223 'langcode' => '<langcode><value>en</value></langcode>',
|
Chris@0
|
224 'name' => '<name><value>' . $this->values['name'] . '</value></name>',
|
Chris@0
|
225 'type' => '<type><value>entity_test_mulrev</value></type>',
|
Chris@0
|
226 'created' => '<created><value>' . $expected_created['value'] . '</value><format>' . $expected_created['format'] . '</format></created>',
|
Chris@18
|
227 '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>',
|
Chris@0
|
228 'revision_id' => '<revision_id><value>' . $this->entity->getRevisionId() . '</value></revision_id>',
|
Chris@0
|
229 'default_langcode' => '<default_langcode><value>1</value></default_langcode>',
|
Chris@0
|
230 'revision_translation_affected' => '<revision_translation_affected><value>1</value></revision_translation_affected>',
|
Chris@14
|
231 'non_mul_field' => '<non_mul_field/>',
|
Chris@0
|
232 'non_rev_field' => '<non_rev_field/>',
|
Chris@14
|
233 '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>',
|
Chris@0
|
234 ];
|
Chris@0
|
235 // Sort it in the same order as normalised.
|
Chris@0
|
236 $expected = array_merge($normalized, $expected);
|
Chris@0
|
237 // Add header and footer.
|
Chris@0
|
238 array_unshift($expected, '<?xml version="1.0"?>' . PHP_EOL . '<response>');
|
Chris@0
|
239 $expected[] = '</response>' . PHP_EOL;
|
Chris@0
|
240 // Reduced the array to a string.
|
Chris@0
|
241 $expected = implode('', $expected);
|
Chris@0
|
242 // Test 'xml'. The output should match that of Symfony's XmlEncoder.
|
Chris@0
|
243 $actual = $this->serializer->serialize($this->entity, 'xml');
|
Chris@0
|
244 $this->assertIdentical($actual, $expected);
|
Chris@0
|
245 $actual = $this->serializer->serialize($normalized, 'xml');
|
Chris@0
|
246 $this->assertIdentical($actual, $expected);
|
Chris@0
|
247 }
|
Chris@0
|
248
|
Chris@0
|
249 /**
|
Chris@0
|
250 * Tests denormalization of an entity.
|
Chris@0
|
251 */
|
Chris@0
|
252 public function testDenormalize() {
|
Chris@0
|
253 $normalized = $this->serializer->normalize($this->entity);
|
Chris@0
|
254
|
Chris@0
|
255 foreach (['json', 'xml'] as $type) {
|
Chris@0
|
256 $denormalized = $this->serializer->denormalize($normalized, $this->entityClass, $type, ['entity_type' => 'entity_test_mulrev']);
|
Chris@17
|
257 $this->assertTrue($denormalized instanceof $this->entityClass, new FormattableMarkup('Denormalized entity is an instance of @class', ['@class' => $this->entityClass]));
|
Chris@0
|
258 $this->assertIdentical($denormalized->getEntityTypeId(), $this->entity->getEntityTypeId(), 'Expected entity type found.');
|
Chris@0
|
259 $this->assertIdentical($denormalized->bundle(), $this->entity->bundle(), 'Expected entity bundle found.');
|
Chris@0
|
260 $this->assertIdentical($denormalized->uuid(), $this->entity->uuid(), 'Expected entity UUID found.');
|
Chris@0
|
261 }
|
Chris@0
|
262 }
|
Chris@0
|
263
|
Chris@18
|
264 /**
|
Chris@18
|
265 * Tests denormalizing serialized columns.
|
Chris@18
|
266 */
|
Chris@18
|
267 public function testDenormalizeSerializedItem() {
|
Chris@18
|
268 $this->setExpectedException(\LogicException::class, 'The generic FieldItemNormalizer cannot denormalize string values for "value" properties of the "serialized" field (field item class: Drupal\entity_test\Plugin\Field\FieldType\SerializedItem).');
|
Chris@18
|
269 $this->serializer->denormalize([
|
Chris@18
|
270 'serialized' => [
|
Chris@18
|
271 [
|
Chris@18
|
272 'value' => 'boo',
|
Chris@18
|
273 ],
|
Chris@18
|
274 ],
|
Chris@18
|
275 'type' => 'entity_test_serialized_field',
|
Chris@18
|
276 ], EntitySerializedField::class);
|
Chris@18
|
277 }
|
Chris@18
|
278
|
Chris@18
|
279 /**
|
Chris@18
|
280 * Tests normalizing/denormalizing custom serialized columns.
|
Chris@18
|
281 */
|
Chris@18
|
282 public function testDenormalizeCustomSerializedItem() {
|
Chris@18
|
283 $entity = EntitySerializedField::create(['serialized_text' => serialize(['Hello world!'])]);
|
Chris@18
|
284 $normalized = $this->serializer->normalize($entity);
|
Chris@18
|
285 $this->assertEquals($normalized['serialized_text'][0]['value'], ['Hello world!']);
|
Chris@18
|
286 $this->setExpectedException(\LogicException::class, 'The generic FieldItemNormalizer cannot denormalize string values for "value" properties of the "serialized_text" field (field item class: Drupal\entity_test\Plugin\Field\FieldType\SerializedPropertyItem).');
|
Chris@18
|
287 $this->serializer->denormalize([
|
Chris@18
|
288 'serialized_text' => [
|
Chris@18
|
289 [
|
Chris@18
|
290 'value' => 'boo',
|
Chris@18
|
291 ],
|
Chris@18
|
292 ],
|
Chris@18
|
293 'type' => 'entity_test_serialized_field',
|
Chris@18
|
294 ], EntitySerializedField::class);
|
Chris@18
|
295 }
|
Chris@18
|
296
|
Chris@18
|
297 /**
|
Chris@18
|
298 * Tests normalizing/denormalizing invalid custom serialized fields.
|
Chris@18
|
299 */
|
Chris@18
|
300 public function testDenormalizeInvalidCustomSerializedField() {
|
Chris@18
|
301 $entity = EntitySerializedField::create(['serialized_long' => serialize(['Hello world!'])]);
|
Chris@18
|
302 $normalized = $this->serializer->normalize($entity);
|
Chris@18
|
303 $this->assertEquals($normalized['serialized_long'][0]['value'], ['Hello world!']);
|
Chris@18
|
304 $this->setExpectedException(\LogicException::class, 'The generic FieldItemNormalizer cannot denormalize string values for "value" properties of the "serialized_long" field (field item class: Drupal\Core\Field\Plugin\Field\FieldType\StringLongItem).');
|
Chris@18
|
305 $this->serializer->denormalize([
|
Chris@18
|
306 'serialized_long' => [
|
Chris@18
|
307 [
|
Chris@18
|
308 'value' => 'boo',
|
Chris@18
|
309 ],
|
Chris@18
|
310 ],
|
Chris@18
|
311 'type' => 'entity_test_serialized_field',
|
Chris@18
|
312 ], EntitySerializedField::class);
|
Chris@18
|
313 }
|
Chris@18
|
314
|
Chris@18
|
315 /**
|
Chris@18
|
316 * Tests normalizing/denormalizing empty custom serialized fields.
|
Chris@18
|
317 */
|
Chris@18
|
318 public function testDenormalizeEmptyCustomSerializedField() {
|
Chris@18
|
319 $entity = EntitySerializedField::create(['serialized_long' => serialize([])]);
|
Chris@18
|
320 $normalized = $this->serializer->normalize($entity);
|
Chris@18
|
321 $this->assertEquals([], $normalized['serialized_long'][0]['value']);
|
Chris@18
|
322
|
Chris@18
|
323 $entity = $this->serializer->denormalize($normalized, EntitySerializedField::class);
|
Chris@18
|
324
|
Chris@18
|
325 $this->assertEquals(serialize([]), $entity->get('serialized_long')->value);
|
Chris@18
|
326 }
|
Chris@18
|
327
|
Chris@18
|
328 /**
|
Chris@18
|
329 * Tests normalizing/denormalizing valid custom serialized fields.
|
Chris@18
|
330 */
|
Chris@18
|
331 public function testDenormalizeValidCustomSerializedField() {
|
Chris@18
|
332 $entity = EntitySerializedField::create(['serialized_long' => serialize(['key' => 'value'])]);
|
Chris@18
|
333 $normalized = $this->serializer->normalize($entity);
|
Chris@18
|
334 $this->assertEquals(['key' => 'value'], $normalized['serialized_long'][0]['value']);
|
Chris@18
|
335
|
Chris@18
|
336 $entity = $this->serializer->denormalize($normalized, EntitySerializedField::class);
|
Chris@18
|
337
|
Chris@18
|
338 $this->assertEquals(serialize(['key' => 'value']), $entity->get('serialized_long')->value);
|
Chris@18
|
339 }
|
Chris@18
|
340
|
Chris@18
|
341 /**
|
Chris@18
|
342 * Tests normalizing/denormalizing using string values.
|
Chris@18
|
343 */
|
Chris@18
|
344 public function testDenormalizeStringValue() {
|
Chris@18
|
345 $this->setExpectedException(\LogicException::class, 'The generic FieldItemNormalizer cannot denormalize string values for "value" properties of the "serialized_long" field (field item class: Drupal\Core\Field\Plugin\Field\FieldType\StringLongItem).');
|
Chris@18
|
346 $this->serializer->denormalize([
|
Chris@18
|
347 'serialized_long' => ['boo'],
|
Chris@18
|
348 'type' => 'entity_test_serialized_field',
|
Chris@18
|
349 ], EntitySerializedField::class);
|
Chris@18
|
350 }
|
Chris@18
|
351
|
Chris@0
|
352 }
|