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