annotate core/modules/serialization/tests/src/Kernel/EntitySerializationTest.php @ 12:7a779792577d

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