diff -u b/core/lib/Drupal/Core/DependencyInjection/Compiler/RegisterSerializationClassesPass.php b/core/lib/Drupal/Core/DependencyInjection/Compiler/RegisterSerializationClassesPass.php --- b/core/lib/Drupal/Core/DependencyInjection/Compiler/RegisterSerializationClassesPass.php +++ b/core/lib/Drupal/Core/DependencyInjection/Compiler/RegisterSerializationClassesPass.php @@ -45,15 +45,18 @@ } /** - * Sort by priority. + * Sorts by priority. * - * The highest priority number is the highest priority (reverse sorting). + * Order services from highest priority number to lowest (reverse sorting). * * @param array $services - * A nested array of Normalizers/Encoders. + * A nested array keyed on priority number. For each priority number, the + * value is an array of Symfony\Component\DependencyInjection\Reference + * objects, each a reference to a normalizer or encoder service. * * @return array - * An array of Normalizers/Encoders to be used by Serializer. + * A flattened array of Reference objects from $services, ordered from high + * to low priority. */ protected function sort($services) { $sorted = array(); diff -u b/core/modules/system/lib/Drupal/system/Tests/Serialization/SerializationTest.php b/core/modules/system/lib/Drupal/system/Tests/Serialization/SerializationTest.php --- b/core/modules/system/lib/Drupal/system/Tests/Serialization/SerializationTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Serialization/SerializationTest.php @@ -8,6 +8,7 @@ namespace Drupal\system\Tests\Serialization; use Drupal\simpletest\WebTestBase; +use Symfony\Component\Serializer\Exception\UnexpectedValueException; class SerializationTest extends WebTestBase { @@ -18,6 +19,13 @@ */ public static $modules = array('serialization_test'); + /** + * The serializer service to test. + * + * @var Symfony\Component\Serializer\SerializerInterface + */ + protected $serializer; + public static function getInfo() { return array( 'name' => 'Serialization tests', @@ -27,20 +35,38 @@ } + protected function setUp() { + parent::setUp(); + $this->serializer = drupal_container()->get('serializer'); + } + /** * Confirms that modules can register normalizers and encoders. */ public function testSerilizerComponentRegistration() { - $obj = (object) array(); + $object = new \stdClass(); + $object->foo = 'bar'; $format = 'serialization_test'; - $container = drupal_container(); - $serializer = $container->get('serializer'); + $expected_normalization = array('foo' => 'bar', 'normalized_by' => 'SerializationTestNormalizer'); + $expected_encoding = 'Normalized by SerializationTestNormalizer, Encoded by SerializationTestEncoder'; - // Test that normalizer is added. The supportsNormalization function cannot - // be used to check this because if a default Normalizer is added, it will - // respond to any format, including the 'serialization_test' format. - $normalized = $serializer->normalize($obj, $format); - $this->assertEqual($normalized[0], 'normalize works', 'Registered normalizers are added to Serializer.'); - // Test that encoder is added. - $this->assertTrue($serializer->supportsEncoding($format), 'Registered encoders are added to Serializer.'); + // Ensure the expected normalizer and encoder get invoked. + $normalized = $this->serializer->normalize($object, $format); + $this->assertIdentical($normalized, $expected_normalization); + $this->assertIdentical($this->serializer->encode($normalized, $format), $expected_encoding); + + // Although redundant with Symfony tests, also ensure the serialize() method + // correctly normalizes and then encodes. + $this->assertIdentical($this->serializer->serialize($object, $format), $expected_encoding); + + // Ensure the normalizer and encoder do not get invoked for a format they do + // not support. For this, we test the serialize() method only and leave its + // implementation of format support checking as a black box. + $serialized = NULL; + try { + $serialized = $this->serializer->serialize($object, 'unsupported_format'); + } + catch (UnexpectedValueException $e) { + } + $this->assertNotIdentical($serialized, $expected_encoding); } } diff -u b/core/modules/system/tests/modules/serilization_test/lib/Drupal/serialization_test/SerializationTestEncoder.php b/core/modules/system/tests/modules/serilization_test/lib/Drupal/serialization_test/SerializationTestEncoder.php --- b/core/modules/system/tests/modules/serilization_test/lib/Drupal/serialization_test/SerializationTestEncoder.php +++ b/core/modules/system/tests/modules/serilization_test/lib/Drupal/serialization_test/SerializationTestEncoder.php @@ -19,24 +19,29 @@ static protected $format = 'serialization_test'; /** - * Implementation required. This function not used for testing. + * Encodes data into the requested format. * * @param mixed $data * Data to encode. * @param string $format * Format name. + * + * @return string + * A string representation of $data in the requested format. */ public function encode($data, $format) { + // @see Drupal\serialization_test\SerializationTestNormalizer::normalize(). + return 'Normalized by ' . $data['normalized_by'] . ', Encoded by SerializationTestEncoder'; } /** - * Check whether the request is for 'serialization_test'. + * Checks whether this encoder can encode to the requested format. * * @param string $format - * The short name of the format returned by ContentNegotiation. + * The short name of the format. * * @return bool - * Returns TRUE if the encoder can handle the request. + * Returns TRUE if this encoder can encode to the requested format. */ public function supportsEncoding($format) { return static::$format === $format; diff -u b/core/modules/system/tests/modules/serilization_test/lib/Drupal/serialization_test/SerializationTestNormalizer.php b/core/modules/system/tests/modules/serilization_test/lib/Drupal/serialization_test/SerializationTestNormalizer.php --- b/core/modules/system/tests/modules/serilization_test/lib/Drupal/serialization_test/SerializationTestNormalizer.php +++ b/core/modules/system/tests/modules/serilization_test/lib/Drupal/serialization_test/SerializationTestNormalizer.php @@ -27,10 +27,15 @@ * Format the normalization result will be encoded as. * * @return array - * An array containing expected result. + * An array containing a normalized representation of $object, appropriate + * for encoding to the requested format. */ public function normalize($object, $format = NULL) { - return array('normalize works'); + $normalized = (array) $object; + // Add identifying value that can be used to verify that the expected + // normalizer was invoked. + $normalized['normalized_by'] = 'SerializationTestNormalizer'; + return $normalized; } /** diff -u b/core/modules/system/tests/modules/serilization_test/serialization_test.module b/core/modules/system/tests/modules/serilization_test/serialization_test.module --- b/core/modules/system/tests/modules/serilization_test/serialization_test.module +++ b/core/modules/system/tests/modules/serilization_test/serialization_test.module @@ -4,3 +4,4 @@ * @file - * Test serialization module. + * Helper module for serialization tests. This file is empty, because all + * implementation is in autoloaded classes. */