Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\serialization\Kernel;
|
Chris@0
|
4
|
Chris@0
|
5 use Drupal\Component\Serialization\Json;
|
Chris@4
|
6 use Drupal\Component\Render\FormattableMarkup;
|
Chris@4
|
7 use Drupal\entity_test\Entity\EntitySerializedField;
|
Chris@0
|
8 use Drupal\entity_test\Entity\EntityTestMulRev;
|
Chris@0
|
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@4
|
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@0
|
69 FilterFormat::create([
|
Chris@0
|
70 'format' => 'my_text_format',
|
Chris@0
|
71 'name' => 'My Text Format',
|
Chris@0
|
72 'filters' => [
|
Chris@0
|
73 'filter_html' => [
|
Chris@0
|
74 'module' => 'filter',
|
Chris@0
|
75 'status' => TRUE,
|
Chris@0
|
76 'weight' => 10,
|
Chris@0
|
77 'settings' => [
|
Chris@0
|
78 'allowed_html' => '<p>',
|
Chris@0
|
79 ],
|
Chris@0
|
80 ],
|
Chris@0
|
81 'filter_autop' => [
|
Chris@0
|
82 'module' => 'filter',
|
Chris@0
|
83 'status' => TRUE,
|
Chris@0
|
84 'weight' => 10,
|
Chris@0
|
85 'settings' => [],
|
Chris@0
|
86 ],
|
Chris@0
|
87 ],
|
Chris@0
|
88 ])->save();
|
Chris@0
|
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@0
|
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@0
|
104 'value' => $test_text_value,
|
Chris@0
|
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@0
|
145 'url' => $this->user->url(),
|
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@0
|
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@0
|
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@0
|
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@0
|
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->url() . '</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@0
|
231 'non_mul_field' => '<non_mul_field/>',
|
Chris@0
|
232 'non_rev_field' => '<non_rev_field/>',
|
Chris@0
|
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@4
|
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@0
|
264 }
|