diff --git a/core/modules/hal/lib/Drupal/hal/Normalizer/EntityCollectionNormalizer.php b/core/modules/hal/lib/Drupal/hal/Normalizer/EntityCollectionNormalizer.php index 05e5dfc..4d8de85 100644 --- a/core/modules/hal/lib/Drupal/hal/Normalizer/EntityCollectionNormalizer.php +++ b/core/modules/hal/lib/Drupal/hal/Normalizer/EntityCollectionNormalizer.php @@ -37,10 +37,8 @@ public function normalize($object, $format = NULL, array $context = array()) { ); // Add the list of items. - foreach ($object->getItems() as $item) { - $link_relation = $this->linkManager->getCollectionItemRelation($object->getCollectionId()); - $normalized['_embedded'][$link_relation][] = $this->serializer->normalize($item, $format, $context); - } + $link_relation = $this->linkManager->getCollectionItemRelation($object->getCollectionId()); + $normalized['_embedded'][$link_relation] = $this->serializer->normalize($object->getItems(), $format, $context); return $normalized; } diff --git a/core/modules/hal/tests/EntityCollectionNormalizeTest.php b/core/modules/hal/tests/EntityCollectionNormalizeTest.php new file mode 100644 index 0000000..0a45874 --- /dev/null +++ b/core/modules/hal/tests/EntityCollectionNormalizeTest.php @@ -0,0 +1,120 @@ + 'EntityCollection normalize test', + 'description' => "Unit test of the EntityCollectionNormalizer's normalize support.", + 'group' => 'HAL', + ); + } + + /** + * Test the normalize function. + */ + public function testNormalize() { + $test_values = $this->getTestValues(); + $collection = $this->getEntityCollection(); + + // Create the normalizer and inject the LinkManagerStub. + $normalizer = new EntityCollectionNormalizer(); + $normalizer->setLinkManager($this->getLinkManagerStub()); + // Inject the Serializer. Handle the call to Serializer::normalize, + // ensuring that the items array is passed in. + $serializer = $this->getSerializerStub(); + $serializer->expects($this->any()) + ->method('normalize') + ->with($collection->getItems()) + ->will($this->returnValue($test_values['items'])); + $normalizer->setSerializer($serializer); + // Get the normalized array. + $normalized = $normalizer->normalize($collection, 'hal_json'); + + // Test that self link points to collection URI. + $this->assertEquals($normalized['_links']['self']['href'], $test_values['uri']); + // Test that the correct link relation was retrieved from the LinkManager + // and added to _embedded. + $this->assertArrayHasKey($test_values['item_link_relation'], $normalized['_embedded']); + // Test that the item link relation points to the serialized item array. + $this->assertEquals($normalized['_embedded'][$test_values['item_link_relation']], $test_values['items']); + } + + /** + * Get an EntityCollection for testing. + * + * @return \Drupal\serialization\EntityCollection + */ + protected function getEntityCollection() { + $test_values = $this->getTestValues(); + + $node = $this->getMockBuilder('Drupal\node\Entity\Node') + ->disableOriginalConstructor() + ->getMock(); + + $collection = new EntityCollection('test_id'); + $collection->setUri($test_values['uri']); + $collection->setItems(array($node)); + + return $collection; + } + + /** + * Get a stub LinkManager for testing. + * + * @return \PHPUnit_Framework_MockObject_MockObject + */ + protected function getLinkManagerStub() { + $test_values = $this->getTestValues(); + + $link_manager = $this->getMockBuilder('Drupal\rest\LinkManager\LinkManager') + ->disableOriginalConstructor() + ->getMock(); + + $link_manager->expects($this->any()) + ->method('getCollectionItemRelation') + ->will($this->returnValue($test_values['item_link_relation'])); + + return $link_manager; + } + + /** + * Get a stub Serializer for testing. + * + * @return \PHPUnit_Framework_MockObject_MockObject + */ + protected function getSerializerStub() { + $serializer = $this->getMockBuilder('Symfony\Component\Serializer\Serializer') + ->disableOriginalConstructor() + ->getMock(); + + return $serializer; + } + + /** + * Get the array of test values. + * + * @return array + */ + protected function getTestValues() { + return array( + 'item_link_relation' => 'item', + 'items' => 'Array of serialized entities goes here', + 'uri' => 'http://example.com/test-path', + ); + } +}