Chris@0: installSchema('system', ['sequences']);
Chris@0:
Chris@0: // Create a test user to use as the entity owner.
Chris@0: $this->user = \Drupal::entityManager()->getStorage('user')->create([
Chris@0: 'name' => 'serialization_test_user',
Chris@0: 'mail' => 'foo@example.com',
Chris@0: 'pass' => '123456',
Chris@0: ]);
Chris@0: $this->user->save();
Chris@0:
Chris@0: // Create a test entity to serialize.
Chris@0: $this->values = [
Chris@0: 'name' => $this->randomMachineName(),
Chris@0: 'user_id' => $this->user->id(),
Chris@0: 'field_test_text' => [
Chris@0: 'value' => $this->randomMachineName(),
Chris@0: 'format' => 'full_html',
Chris@0: ],
Chris@0: ];
Chris@0: $this->entity = EntityTestMulRev::create($this->values);
Chris@0: $this->entity->save();
Chris@0:
Chris@0: $this->serializer = $this->container->get('serializer');
Chris@0:
Chris@0: $this->installConfig(['field']);
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Test the normalize function.
Chris@0: */
Chris@0: public function testNormalize() {
Chris@0: $expected = [
Chris@0: 'id' => [
Chris@0: ['value' => 1],
Chris@0: ],
Chris@0: 'uuid' => [
Chris@0: ['value' => $this->entity->uuid()],
Chris@0: ],
Chris@0: 'langcode' => [
Chris@0: ['value' => 'en'],
Chris@0: ],
Chris@0: 'name' => [
Chris@0: ['value' => $this->values['name']],
Chris@0: ],
Chris@0: 'type' => [
Chris@0: ['value' => 'entity_test_mulrev'],
Chris@0: ],
Chris@0: 'created' => [
Chris@0: $this->formatExpectedTimestampItemValues($this->entity->created->value),
Chris@0: ],
Chris@0: 'user_id' => [
Chris@0: [
Chris@0: // id() will return the string value as it comes from the database.
Chris@0: 'target_id' => (int) $this->user->id(),
Chris@0: 'target_type' => $this->user->getEntityTypeId(),
Chris@0: 'target_uuid' => $this->user->uuid(),
Chris@0: 'url' => $this->user->url(),
Chris@0: ],
Chris@0: ],
Chris@0: 'revision_id' => [
Chris@0: ['value' => 1],
Chris@0: ],
Chris@0: 'default_langcode' => [
Chris@0: ['value' => TRUE],
Chris@0: ],
Chris@0: 'revision_translation_affected' => [
Chris@0: ['value' => TRUE],
Chris@0: ],
Chris@0: 'non_rev_field' => [],
Chris@0: 'field_test_text' => [
Chris@0: [
Chris@0: 'value' => $this->values['field_test_text']['value'],
Chris@0: 'format' => $this->values['field_test_text']['format'],
Chris@0: ],
Chris@0: ],
Chris@0: ];
Chris@0:
Chris@0: $normalized = $this->serializer->normalize($this->entity);
Chris@0:
Chris@0: foreach (array_keys($expected) as $fieldName) {
Chris@0: $this->assertSame($expected[$fieldName], $normalized[$fieldName], "Normalization produces expected array for $fieldName.");
Chris@0: }
Chris@0: $this->assertEqual(array_diff_key($normalized, $expected), [], 'No unexpected data is added to the normalized array.');
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Tests user normalization, using the entity_serialization_test module to
Chris@0: * override some default access controls.
Chris@0: */
Chris@0: public function testUserNormalize() {
Chris@0: // Test password isn't available.
Chris@0: $normalized = $this->serializer->normalize($this->user);
Chris@0:
Chris@0: $this->assertFalse(array_key_exists('pass', $normalized), '"pass" key does not exist in normalized user');
Chris@0: $this->assertFalse(array_key_exists('mail', $normalized), '"mail" key does not exist in normalized user');
Chris@0:
Chris@0: // Test again using our test user, so that our access control override will
Chris@0: // allow password viewing.
Chris@0: $normalized = $this->serializer->normalize($this->user, NULL, ['account' => $this->user]);
Chris@0:
Chris@0: // The key 'pass' will now exist, but the password value should be
Chris@0: // normalized to NULL.
Chris@0: $this->assertIdentical($normalized['pass'], [NULL], '"pass" value is normalized to [NULL]');
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Test registered Serializer's entity serialization for core's formats.
Chris@0: */
Chris@0: public function testSerialize() {
Chris@0: // Test that Serializer responds using the ComplexDataNormalizer and
Chris@0: // JsonEncoder. The output of ComplexDataNormalizer::normalize() is tested
Chris@0: // elsewhere, so we can just assume that it works properly here.
Chris@0: $normalized = $this->serializer->normalize($this->entity, 'json');
Chris@0: $expected = json_encode($normalized);
Chris@0: // Test 'json'.
Chris@0: $actual = $this->serializer->serialize($this->entity, 'json');
Chris@0: $this->assertIdentical($actual, $expected, 'Entity serializes to JSON when "json" is requested.');
Chris@0: $actual = $this->serializer->serialize($normalized, 'json');
Chris@0: $this->assertIdentical($actual, $expected, 'A normalized array serializes to JSON when "json" is requested');
Chris@0: // Test 'ajax'.
Chris@0: $actual = $this->serializer->serialize($this->entity, 'ajax');
Chris@0: $this->assertIdentical($actual, $expected, 'Entity serializes to JSON when "ajax" is requested.');
Chris@0: $actual = $this->serializer->serialize($normalized, 'ajax');
Chris@0: $this->assertIdentical($actual, $expected, 'A normalized array serializes to JSON when "ajax" is requested');
Chris@0:
Chris@0: // Generate the expected xml in a way that allows changes to entity property
Chris@0: // order.
Chris@0: $expected_created = $this->formatExpectedTimestampItemValues($this->entity->created->value);
Chris@0:
Chris@0: $expected = [
Chris@0: 'id' => '' . $this->entity->id() . '',
Chris@0: 'uuid' => '' . $this->entity->uuid() . '',
Chris@0: 'langcode' => 'en',
Chris@0: 'name' => '' . $this->values['name'] . '',
Chris@0: 'type' => 'entity_test_mulrev',
Chris@0: 'created' => '' . $expected_created['value'] . '' . $expected_created['format'] . '',
Chris@0: 'user_id' => '' . $this->user->id() . '' . $this->user->getEntityTypeId() . '' . $this->user->uuid() . '' . $this->user->url() . '',
Chris@0: 'revision_id' => '' . $this->entity->getRevisionId() . '',
Chris@0: 'default_langcode' => '1',
Chris@0: 'revision_translation_affected' => '1',
Chris@0: 'non_rev_field' => '',
Chris@0: 'field_test_text' => '' . $this->values['field_test_text']['value'] . '' . $this->values['field_test_text']['format'] . '',
Chris@0: ];
Chris@0: // Sort it in the same order as normalised.
Chris@0: $expected = array_merge($normalized, $expected);
Chris@0: // Add header and footer.
Chris@0: array_unshift($expected, '' . PHP_EOL . '');
Chris@0: $expected[] = '' . PHP_EOL;
Chris@0: // Reduced the array to a string.
Chris@0: $expected = implode('', $expected);
Chris@0: // Test 'xml'. The output should match that of Symfony's XmlEncoder.
Chris@0: $actual = $this->serializer->serialize($this->entity, 'xml');
Chris@0: $this->assertIdentical($actual, $expected);
Chris@0: $actual = $this->serializer->serialize($normalized, 'xml');
Chris@0: $this->assertIdentical($actual, $expected);
Chris@0: }
Chris@0:
Chris@0: /**
Chris@0: * Tests denormalization of an entity.
Chris@0: */
Chris@0: public function testDenormalize() {
Chris@0: $normalized = $this->serializer->normalize($this->entity);
Chris@0:
Chris@0: foreach (['json', 'xml'] as $type) {
Chris@0: $denormalized = $this->serializer->denormalize($normalized, $this->entityClass, $type, ['entity_type' => 'entity_test_mulrev']);
Chris@0: $this->assertTrue($denormalized instanceof $this->entityClass, SafeMarkup::format('Denormalized entity is an instance of @class', ['@class' => $this->entityClass]));
Chris@0: $this->assertIdentical($denormalized->getEntityTypeId(), $this->entity->getEntityTypeId(), 'Expected entity type found.');
Chris@0: $this->assertIdentical($denormalized->bundle(), $this->entity->bundle(), 'Expected entity bundle found.');
Chris@0: $this->assertIdentical($denormalized->uuid(), $this->entity->uuid(), 'Expected entity UUID found.');
Chris@0: }
Chris@0: }
Chris@0:
Chris@0: }