diff --git a/core/lib/Drupal/Core/CoreBundle.php b/core/lib/Drupal/Core/CoreBundle.php index 879bd02..ef12b11 100644 --- a/core/lib/Drupal/Core/CoreBundle.php +++ b/core/lib/Drupal/Core/CoreBundle.php @@ -224,9 +224,7 @@ public function build(ContainerBuilder $container) { ->addArgument(array()); $container->register('serializer.normalizer.entity', 'Drupal\Core\Serialization\EntityNormalizer')->addTag('normalizer'); - $container->register('serializer.encoder.json', 'Drupal\Core\Serialization\DrupalJsonEncoder')->addTag('encoder'); - $container->register('serializer.encoder.ajax', 'Drupal\Core\Serialization\DrupalJsonEncoder')->addTag('encoder'); - $container->register('serializer.encoder.xml', 'Symfony\Component\Serializer\Encoder\XmlEncoder')->addTag('encoder'); + $container->register('serializer.encoder.json', 'Drupal\Core\Serialization\JsonEncoder')->addTag('encoder'); $container->register('flood', 'Drupal\Core\Flood\DatabaseBackend') ->addArgument(new Reference('database')); diff --git a/core/lib/Drupal/Core/Serialization/EntityNormalizer.php b/core/lib/Drupal/Core/Serialization/EntityNormalizer.php index 61239fc..c007c3f 100644 --- a/core/lib/Drupal/Core/Serialization/EntityNormalizer.php +++ b/core/lib/Drupal/Core/Serialization/EntityNormalizer.php @@ -2,7 +2,7 @@ /** * @file - * Contains Drupal\Core\Entity\EntityNormalizer. + * Contains Drupal\Core\Serialization\EntityNormalizer. */ namespace Drupal\Core\Serialization; @@ -14,8 +14,12 @@ /** * Converts the Drupal entity object structures to a normalized array. * - * The format is not checked here, but assumed it will be supported. - * @see \Drupal\Core\Entity\EntityInterface + * This is the default Normalizer for entities. All formats that have Encoders + * registered with the Serializer in the DIC will be normalized with this + * class unless another Normalizer is registered which supersedes it. If a + * module wants to use format-specific or class-specific normalization, then + * that module can register a new Normalizer and give it a higher priority than + * this one. */ class EntityNormalizer implements NormalizerInterface { diff --git a/core/lib/Drupal/Core/Serialization/DrupalJsonEncoder.php b/core/lib/Drupal/Core/Serialization/JsonEncoder.php similarity index 72% rename from core/lib/Drupal/Core/Serialization/DrupalJsonEncoder.php rename to core/lib/Drupal/Core/Serialization/JsonEncoder.php index f661c6d..8d7e3d7 100644 --- a/core/lib/Drupal/Core/Serialization/DrupalJsonEncoder.php +++ b/core/lib/Drupal/Core/Serialization/JsonEncoder.php @@ -2,18 +2,18 @@ /** * @file - * Contains Drupal\Core\Serialization\DrupalJsonEncoder. + * Contains Drupal\Core\Serialization\JsonEncoder. */ namespace Drupal\Core\Serialization; use Symfony\Component\Serializer\Encoder\EncoderInterface; -use Symfony\Component\Serializer\Encoder\JsonEncoder; +use Symfony\Component\Serializer\Encoder\JsonEncoder as BaseJsonEncoder; /** * Adds 'ajax to the supported content types of the JSON encoder' */ -class DrupalJsonEncoder extends JsonEncoder implements EncoderInterface { +class JsonEncoder extends BaseJsonEncoder implements EncoderInterface { /** * The formats that this Encoder supports. diff --git a/core/modules/system/lib/Drupal/system/Tests/Serialization/EntitySerializationTest.php b/core/modules/system/lib/Drupal/system/Tests/Serialization/EntitySerializationTest.php index cf33531..305fe3a 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Serialization/EntitySerializationTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Serialization/EntitySerializationTest.php @@ -9,7 +9,6 @@ use Drupal\simpletest\WebTestBase; use Drupal\Core\Serialization\EntityNormalizer; -use Symfony\Component\Serializer\Encoder\XmlEncoder; class EntitySerializationTest extends WebTestBase { @@ -20,6 +19,20 @@ class EntitySerializationTest extends WebTestBase { */ public static $modules = array('entity_test'); + /** + * The test values. + * + * @var array + */ + protected $values; + + /** + * The test entity. + * + * @var \Drupal\Core\Entity\EntityNG + */ + protected $entity; + public static function getInfo() { return array( 'name' => 'Entity serialization tests', @@ -28,14 +41,9 @@ public static function getInfo() { ); } - /** - * Tests the normalize function. - */ - public function testSerialize() { - $serializer = drupal_container()->get('serializer'); - $normalizer = new EntityNormalizer(); - - $values = array( + protected function setUp() { + parent::setUp(); + $this->values = array( 'name' => $this->randomName(), 'user_id' => $GLOBALS['user']->uid, 'field_test_text' => array( @@ -43,21 +51,65 @@ public function testSerialize() { 'format' => 'full_html', ), ); - $entity = entity_create('entity_test', $values); - $entity->save(); + $this->entity = entity_create('entity_test', $this->values); + $this->entity->save(); + } - // Get a normalized array to use for assertions. - $normalized = $normalizer->normalize($entity); + /** + * Test the normalize function. + */ + public function testNormalize() { + $normalizer = new EntityNormalizer(); + + $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(NULL), + 'name' => array( + array('value' => $this->values['name']), + ), + 'user_id' => array( + array('value' => $this->values['user_id']), + ), + 'field_test_text' => array( + array( + 'value' => $this->values['field_test_text']['value'], + 'format' => $this->values['field_test_text']['format'], + ), + ), + ); + + $normalized = $normalizer->normalize($this->entity); + $this->assertEqual($expected, $normalized, 'EntityNormalizer produces expected array.'); + } + + /** + * Test Serializer's entity serialization for core's registered formats. + */ + public function testSerialize() { + $serializer = drupal_container()->get('serializer'); + $normalizer = new EntityNormalizer(); - // Test the JSON output. + // Test that Serializer responds using the EntityNormalizer and + // JsonEncoder. The output of EntityNormalizer::normalize() is tested + // elsewhere, so we can just assume that it works properly here. + $normalized = $normalizer->normalize($this->entity, 'json'); $expected = json_encode($normalized); - $actual = $serializer->serialize($entity, 'json'); - $this->assertIdentical($actual, $expected); - - // Test the XML output. - $xml_encoder = new XmlEncoder(); - $expected = $xml_encoder->encode($normalized, 'xml'); - $actual = $serializer->serialize($entity, 'xml'); - $this->assertIdentical($actual, $expected); + // Test 'json'. + $actualJson = $serializer->serialize($this->entity, 'json'); + $this->assertIdentical($actualJson, $expected, 'Entity serializes to JSON when json is requested.'); + // Test 'ajax'. + $actualAjax = $serializer->serialize($this->entity, 'ajax'); + $this->assertIdentical($actualAjax, $expected, 'Entity serializes to JSON when ajax is requested.'); } }