Newer
Older
Dries Buytaert
committed
<?php
/**
* @file
Angie Byron
committed
* Contains \Drupal\serialization\Tests\EntitySerializationTest.
Dries Buytaert
committed
*/
Angie Byron
committed
namespace Drupal\serialization\Tests;
Dries Buytaert
committed
Angie Byron
committed
use Drupal\serialization\Encoder\JsonEncoder;
use Drupal\serialization\Normalizer\ComplexDataNormalizer;
use Drupal\serialization\Normalizer\TypedDataNormalizer;
use Drupal\simpletest\DrupalUnitTestBase;
Dries Buytaert
committed
use Symfony\Component\Serializer\Serializer;
/**
* Tests entity normalization and serialization of supported core formats.
*/
class EntitySerializationTest extends DrupalUnitTestBase {
Dries Buytaert
committed
/**
* Modules to enable.
*
* @var array
*/
public static $modules = array('serialization', 'system', 'entity', 'field', 'entity_test', 'text', 'field_sql_storage');
Dries Buytaert
committed
/**
* The test values.
*
* @var array
*/
protected $values;
/**
* The test entity.
*
* @var \Drupal\Core\Entity\EntityNG
*/
protected $entity;
/**
* The serializer serivce.
*
* @var \Symfony\Component\Serializer\Serializer.
*/
protected $serializer;
Dries Buytaert
committed
public static function getInfo() {
return array(
'name' => 'Entity serialization tests',
'description' => 'Tests that entities can be serialized to supported core formats.',
'group' => 'Serialization',
);
}
protected function setUp() {
parent::setUp();
$this->installSchema('field', array('field_config', 'field_config_instance'));
$this->installSchema('entity_test', array('entity_test_mulrev', 'entity_test_mulrev_property_revision', 'entity_test_mulrev_property_data'));
// Auto-create a field for testing.
field_create_field(array(
'field_name' => 'field_test_text',
'type' => 'text',
'cardinality' => 1,
'translatable' => FALSE,
));
$instance = array(
'entity_type' => 'entity_test_mulrev',
'field_name' => 'field_test_text',
'bundle' => 'entity_test_mulrev',
'label' => 'Test text-field',
'widget' => array(
'type' => 'text_textfield',
'weight' => 0,
),
);
field_create_instance($instance);
Dries Buytaert
committed
// Create a test entity to serialize.
$this->values = array(
'name' => $this->randomName(),
'user_id' => $GLOBALS['user']->uid,
'field_test_text' => array(
'value' => $this->randomName(),
'format' => 'full_html',
),
);
$this->entity = entity_create('entity_test_mulrev', $this->values);
Dries Buytaert
committed
$this->entity->save();
$this->serializer = $this->container->get('serializer');
Dries Buytaert
committed
}
/**
* Test the normalize function.
*/
public function testNormalize() {
$expected = array(
'id' => array(
array('value' => 1),
),
'revision_id' => array(
array('value' => 1),
),
'uuid' => array(
array('value' => $this->entity->uuid()),
),
'langcode' => array(
array('value' => LANGUAGE_NOT_SPECIFIED),
),
'default_langcode' => array(
array('value' => NULL),
),
Dries Buytaert
committed
'name' => array(
array('value' => $this->values['name']),
),
'user_id' => array(
Dries Buytaert
committed
array('target_id' => $this->values['user_id']),
Dries Buytaert
committed
),
'field_test_text' => array(
array(
'value' => $this->values['field_test_text']['value'],
'format' => $this->values['field_test_text']['format'],
),
),
);
$normalized = $this->serializer->normalize($this->entity);
Dries Buytaert
committed
foreach (array_keys($expected) as $fieldName) {
$this->assertEqual($expected[$fieldName], $normalized[$fieldName], "ComplexDataNormalizer produces expected array for $fieldName.");
}
$this->assertEqual(array_diff_key($normalized, $expected), array(), 'No unexpected data is added to the normalized array.');
Dries Buytaert
committed
}
/**
* Test registered Serializer's entity serialization for core's formats.
*/
public function testSerialize() {
// Test that Serializer responds using the ComplexDataNormalizer and
// JsonEncoder. The output of ComplexDataNormalizer::normalize() is tested
// elsewhere, so we can just assume that it works properly here.
$normalized = $this->serializer->normalize($this->entity, 'json');
Dries Buytaert
committed
$expected = json_encode($normalized);
// Test 'json'.
$actual = $this->serializer->serialize($this->entity, 'json');
Angie Byron
committed
$this->assertIdentical($actual, $expected, 'Entity serializes to JSON when "json" is requested.');
$actual = $this->serializer->serialize($normalized, 'json');
Angie Byron
committed
$this->assertIdentical($actual, $expected, 'A normalized array serializes to JSON when "json" is requested');
Dries Buytaert
committed
// Test 'ajax'.
$actual = $this->serializer->serialize($this->entity, 'ajax');
Angie Byron
committed
$this->assertIdentical($actual, $expected, 'Entity serializes to JSON when "ajax" is requested.');
$actual = $this->serializer->serialize($normalized, 'ajax');
Angie Byron
committed
$this->assertIdentical($actual, $expected, 'A normalized array serializes to JSON when "ajax" is requested');
// Generate the expected xml in a way that allows changes to entity property
// order.
$expected = array(
'id' => '<id><value>' . $this->entity->id() . '</value></id>',
'revision_id' => '<revision_id><value>' . $this->entity->getRevisionId() . '</value></revision_id>',
'uuid' => '<uuid><value>' . $this->entity->uuid() . '</value></uuid>',
'langcode' => '<langcode><value>' . LANGUAGE_NOT_SPECIFIED . '</value></langcode>',
'default_langcode' => '<default_langcode><value/></default_langcode>',
'name' => '<name><value>' . $this->values['name'] . '</value></name>',
Dries Buytaert
committed
'user_id' => '<user_id><target_id>' . $this->values['user_id'] . '</target_id></user_id>',
Angie Byron
committed
'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>',
);
// Sort it in the same order as normalised.
$expected = array_merge($normalized, $expected);
// Add header and footer.
array_unshift($expected, '<?xml version="1.0"?>' . PHP_EOL . '<response>');
$expected[] = '</response>' . PHP_EOL;
// Reduced the array to a string.
$expected = implode('', $expected);
// Test 'xml'. The output should match that of Symfony's XmlEncoder.
$actual = $this->serializer->serialize($this->entity, 'xml');
Angie Byron
committed
$this->assertIdentical($actual, $expected);
$actual = $this->serializer->serialize($normalized, 'xml');
Angie Byron
committed
$this->assertIdentical($actual, $expected);