3 namespace Drupal\Tests\serialization\Kernel;
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;
12 * Tests that entities can be serialized to supported core formats.
14 * @group serialization
16 class EntitySerializationTest extends NormalizerTestBase {
18 use BcTimestampNormalizerUnixTestTrait;
25 public static $modules = ['serialization', 'system', 'field', 'entity_test', 'text', 'filter', 'user', 'entity_serialization_test'];
37 * @var \Drupal\Core\Entity\ContentEntityInterface
44 * @var \Drupal\user\Entity\User
49 * The serializer service.
51 * @var \Symfony\Component\Serializer\Serializer.
53 protected $serializer;
56 * The class name of the test class.
60 protected $entityClass = 'Drupal\entity_test\Entity\EntityTest';
62 protected function setUp() {
65 // User create needs sequence table.
66 $this->installSchema('system', ['sequences']);
68 FilterFormat::create([
69 'format' => 'my_text_format',
70 'name' => 'My Text Format',
77 'allowed_html' => '<p>',
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',
97 // Create a test entity to serialize.
98 $test_text_value = $this->randomMachineName();
100 'name' => $this->randomMachineName(),
101 'user_id' => $this->user->id(),
102 'field_test_text' => [
103 'value' => $test_text_value,
104 'format' => 'my_text_format',
107 $this->entity = EntityTestMulRev::create($this->values);
108 $this->entity->save();
110 $this->serializer = $this->container->get('serializer');
112 $this->installConfig(['field']);
116 * Test the normalize function.
118 public function testNormalize() {
124 ['value' => $this->entity->uuid()],
130 ['value' => $this->values['name']],
133 ['value' => 'entity_test_mulrev'],
136 $this->formatExpectedTimestampItemValues($this->entity->created->value),
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(),
150 'default_langcode' => [
153 'revision_translation_affected' => [
156 'non_rev_field' => [],
157 'non_mul_field' => [],
158 'field_test_text' => [
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>",
167 $normalized = $this->serializer->normalize($this->entity);
169 foreach (array_keys($expected) as $fieldName) {
170 $this->assertSame($expected[$fieldName], $normalized[$fieldName], "Normalization produces expected array for $fieldName.");
172 $this->assertEqual(array_diff_key($normalized, $expected), [], 'No unexpected data is added to the normalized array.');
176 * Tests user normalization, using the entity_serialization_test module to
177 * override some default access controls.
179 public function testUserNormalize() {
180 // Test password isn't available.
181 $normalized = $this->serializer->normalize($this->user);
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');
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]);
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]');
196 * Test registered Serializer's entity serialization for core's formats.
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);
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');
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');
215 // Generate the expected xml in a way that allows changes to entity property
217 $expected_created = $this->formatExpectedTimestampItemValues($this->entity->created->value);
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>',
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);
249 * Tests denormalization of an entity.
251 public function testDenormalize() {
252 $normalized = $this->serializer->normalize($this->entity);
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.');