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