diff --git a/core/modules/hal/src/Normalizer/CollectionNormalizer.php b/core/modules/hal/src/Normalizer/CollectionNormalizer.php index 3fe1e47..66ede8d 100644 --- a/core/modules/hal/src/Normalizer/CollectionNormalizer.php +++ b/core/modules/hal/src/Normalizer/CollectionNormalizer.php @@ -8,6 +8,7 @@ namespace Drupal\hal\Normalizer; use Drupal\rest\LinkManager\LinkManagerInterface; +use Drupal\serialization\Collection; /** * Converts the Drupal entity object structure to a HAL array structure. @@ -51,6 +52,12 @@ public function normalize($object, $format = NULL, array $context = array()) { ), ); + // $object is not type-hinted, so we cannot be sure this method will not be + // called on a non-Collection object, and there is no interface to check on. + if (!$object instanceof Collection) { + return $normalized; + } + // If we have additional hypermedia links add them here. $links = $object->getLinks(); if (is_array($links) && count($links)) { @@ -76,6 +83,8 @@ public function denormalize($data, $class, $format = NULL, array $context = arra /** * {@inheritdoc} + * + * @todo Implement denormalization once normalization has settled. */ public function supportsDenormalization($data, $type, $format = NULL) { return FALSE; diff --git a/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php b/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php index c18e8f5..fc9ffc1 100644 --- a/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php +++ b/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php @@ -73,7 +73,7 @@ public function __construct(LinkManagerInterface $link_manager, EntityManagerInt * @param array $context * Context options for the normalizer. * - * @return array|scalar + * @return array * * @see \Drupal\hal\HalSubscriber * Defines the format to support. diff --git a/core/modules/hal/src/Normalizer/EntityReferenceItemNormalizer.php b/core/modules/hal/src/Normalizer/EntityReferenceItemNormalizer.php index 1bffc67..36ae614 100644 --- a/core/modules/hal/src/Normalizer/EntityReferenceItemNormalizer.php +++ b/core/modules/hal/src/Normalizer/EntityReferenceItemNormalizer.php @@ -53,12 +53,6 @@ public function __construct(LinkManagerInterface $link_manager, EntityResolverIn /** * {@inheritdoc} - * - * @param \Drupal\Core\Field\FieldItemInterface $field_item - * @param null $format - * @param array $context - * - * @return array|scalar */ public function normalize($field_item, $format = NULL, array $context = array()) { $target_entity = $field_item->get('entity')->getValue(); diff --git a/core/modules/hal/src/Tests/CollectionNormalizerTest.php b/core/modules/hal/src/Tests/CollectionNormalizerTest.php deleted file mode 100644 index f135a28..0000000 --- a/core/modules/hal/src/Tests/CollectionNormalizerTest.php +++ /dev/null @@ -1,208 +0,0 @@ - 'CollectionNormalizer test', - 'description' => "Unit test of the CollectionNormalizer's normalize support.", - 'group' => 'HAL', - ); - } - - /** - * Tests the supportsNormalization method. - */ - public function testSupportsNormalization() { - $collection = $this->getCollection(); - $normalizer = new CollectionNormalizer($this->getLinkManagerStub()); - $this->assertTrue($normalizer->supportsNormalization($collection, 'hal_json')); - $this->assertFalse($normalizer->supportsNormalization($collection, 'json')); - $this->assertFalse($normalizer->supportsNormalization(new \stdClass(), 'hal_json')); - } - - /** - * Tests the normalize method. - */ - public function testNormalize() { - $test_values = $this->getTestValues(); - $collection = $this->getCollection(); - - // Create the normalizer and inject the LinkManagerStub. - $normalizer = new CollectionNormalizer($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']); - // There should only be a self-key. - $this->assertEquals(array_keys($normalized['_links']), array('self')); - - // 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']); - } - - /** - * Tests the normalize method on pageable collection. - */ - public function testNormalizePageableCollection() { - $test_values = $this->getTestValues(); - $collection = $this->getPageableCollection(); - - // Create the normalizer and inject the LinkManagerStub. - $normalizer = new CollectionNormalizer($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']); - // There should be the self-key, _first, _prev, _next and _last-keys. - $this->assertArrayHasKey('self', $normalized['_links']); - $this->assertArrayHasKey('first', $normalized['_links']); - $this->assertArrayHasKey('prev', $normalized['_links']); - $this->assertArrayHasKey('next', $normalized['_links']); - $this->assertArrayHasKey('last', $normalized['_links']); - $this->assertEquals(array_keys($normalized['_links']), - array('self', 'first', 'prev', 'next', 'last') - ); - - // 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 \Drupal\serialization\Collection for testing. - * - * @return \Drupal\serialization\Collection - * The Collection object, configured with test values. - */ - protected function getCollection() { - $test_values = $this->getTestValues(); - - // Get a dummy node. - $node = $this->getMockBuilder('Drupal\node\Entity\Node') - ->disableOriginalConstructor() - ->getMock(); - - $collection = new Collection('test_id'); - $collection->setUri($test_values['uri']); - $collection->setItems(array($node)); - - return $collection; - } - - /** - * Get an pageable \Drupal\serialization\Collection for testing. - * - * @return \Drupal\serialization\Collection - * The Collection object, configured with test values. - */ - protected function getPageableCollection() { - $test_values = $this->getTestValues(); - - // Get a dummy node. - $node = $this->getMockBuilder('Drupal\node\Entity\Node') - ->disableOriginalConstructor() - ->getMock(); - - $collection = new Collection('test_id'); - $collection->setUri($test_values['uri']); - $collection->setItems(array($node)); - - $collection->setLinks(array( - 'first' => $test_values['uri'] . '?page=0', - 'prev' => $test_values['uri'] . '?page=0', - 'next' => $test_values['uri'] . '?page=2', - 'last' => $test_values['uri'] . '?page=2', - )); - - return $collection; - } - - /** - * Get a stub LinkManager for testing. - * - * @return \Drupal\rest\LinkManager\LinkManagerInterface - * The LinkManager stub. - */ - 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 \Symfony\Component\Serializer\SerializerInterface - * The Serializer stub. - */ - protected function getSerializerStub() { - $serializer = $this->getMockBuilder('Symfony\Component\Serializer\Serializer') - ->disableOriginalConstructor() - ->getMock(); - - return $serializer; - } - - /** - * Get the array of test values. - * - * @return array - * An array of test values, used for configuring stub methods and testing. - */ - protected function getTestValues() { - return array( - 'item_link_relation' => 'item', - 'items' => 'Array of serialized entities goes here', - 'uri' => 'http://example.com/test-path', - ); - } -} diff --git a/core/modules/hal/src/Tests/NormalizerTestBase.php b/core/modules/hal/src/Tests/NormalizerTestBase.php index 45a7a65..1c0b4c7 100644 --- a/core/modules/hal/src/Tests/NormalizerTestBase.php +++ b/core/modules/hal/src/Tests/NormalizerTestBase.php @@ -16,7 +16,6 @@ use Drupal\language\Entity\ConfigurableLanguage; use Drupal\rest\LinkManager\CollectionLinkManager; use Drupal\rest\LinkManager\LinkManager; -use Drupal\rest\LinkManager\CollectionLinkManager; use Drupal\rest\LinkManager\RelationLinkManager; use Drupal\rest\LinkManager\TypeLinkManager; use Drupal\serialization\EntityResolver\ChainEntityResolver; @@ -119,8 +118,8 @@ protected function setUp() { $entity_manager = \Drupal::entityManager(); $link_manager = new LinkManager( - new TypeLinkManager(new MemoryBackend('cache')), - new RelationLinkManager(new MemoryBackend('cache'), $entity_manager), + new TypeLinkManager(new MemoryBackend('default')), + new RelationLinkManager(new MemoryBackend('default'), $entity_manager), new CollectionLinkManager() ); diff --git a/core/modules/hal/tests/CollectionNormalizerTest.php b/core/modules/hal/tests/src/Unit/CollectionNormalizerTest.php similarity index 92% rename from core/modules/hal/tests/CollectionNormalizerTest.php rename to core/modules/hal/tests/src/Unit/CollectionNormalizerTest.php index f135a28..7953815 100644 --- a/core/modules/hal/tests/CollectionNormalizerTest.php +++ b/core/modules/hal/tests/src/Unit/CollectionNormalizerTest.php @@ -4,28 +4,19 @@ * Contains \Drupal\hal\Tests\CollectionNormalizerTest. */ -namespace Drupal\hal\Tests; +namespace Drupal\Tests\hal\Unit; use Drupal\hal\Normalizer\CollectionNormalizer; use Drupal\serialization\Collection; use Drupal\Tests\UnitTestCase; /** - * Tests the CollectionNormalizer's normalize support. + * Tests the CollectionNormalizer's normalize supports. * + * @coversDefaultClass \Drupal\hal\Normalizer\CollectionNormalizer * @group HAL */ class CollectionNormalizerTest extends UnitTestCase { - /** - * {@inheritdoc} - */ - public static function getInfo() { - return array( - 'name' => 'CollectionNormalizer test', - 'description' => "Unit test of the CollectionNormalizer's normalize support.", - 'group' => 'HAL', - ); - } /** * Tests the supportsNormalization method. @@ -118,7 +109,7 @@ public function testNormalizePageableCollection() { protected function getCollection() { $test_values = $this->getTestValues(); - // Get a dummy node. + // Get a mock node. $node = $this->getMockBuilder('Drupal\node\Entity\Node') ->disableOriginalConstructor() ->getMock(); @@ -181,8 +172,8 @@ protected function getLinkManagerStub() { /** * Get a stub Serializer for testing. * - * @return \Symfony\Component\Serializer\SerializerInterface - * The Serializer stub. + * @return \Symfony\Component\Serializer\SerializerInterface|\Symfony\Component\Serializer\Normalizer\NormalizerInterface|\Symfony\Component\Serializer\Normalizer\DenormalizerInterface|\Symfony\Component\Serializer\Encoder\EncoderInterface|\Symfony\Component\Serializer\Encoder\DecoderInterface|\PHPUnit_Framework_MockObject_MockObject + * The Serializer mock. */ protected function getSerializerStub() { $serializer = $this->getMockBuilder('Symfony\Component\Serializer\Serializer') diff --git a/core/modules/rest/rest.services.yml b/core/modules/rest/rest.services.yml index 388e513..58dde91 100644 --- a/core/modules/rest/rest.services.yml +++ b/core/modules/rest/rest.services.yml @@ -32,5 +32,3 @@ services: logger.channel.rest: parent: logger.channel_base arguments: ['rest'] - rest.link_manager.collection: - class: Drupal\rest\LinkManager\CollectionLinkManager diff --git a/core/modules/rest/src/Plugin/views/style/Serializer.php b/core/modules/rest/src/Plugin/views/style/Serializer.php index 8643fca..2c1a28b 100644 --- a/core/modules/rest/src/Plugin/views/style/Serializer.php +++ b/core/modules/rest/src/Plugin/views/style/Serializer.php @@ -55,6 +55,8 @@ class Serializer extends StylePluginBase { protected $formats = array(); /** + * The URL generator service. + * * @var \Drupal\Core\Routing\UrlGeneratorInterface */ protected $urlGenerator; diff --git a/core/modules/rest/src/Tests/Views/StyleSerializerTest.php b/core/modules/rest/src/Tests/Views/StyleSerializerTest.php index 72692ec..676cf8b 100644 --- a/core/modules/rest/src/Tests/Views/StyleSerializerTest.php +++ b/core/modules/rest/src/Tests/Views/StyleSerializerTest.php @@ -467,145 +467,6 @@ public function testSerializerFieldDisplayPagingResponse() { $this->assertTrue(isset($actual_json['_embedded']) && isset($actual_json['_links']), 'Has _links and _embedded keys'); $this->assertEqual(count($actual_json['_embedded']['item']), 1); - $this->assertEqual($actual_json['_links']['self']['href'], url($view->getUrl(), array('absolute' => TRUE))); - $this->assertEqual($actual_json['_links']['first']['href'], url($view->getUrl(), array('query' => array('page' => 0), 'absolute' => TRUE))); - $this->assertEqual($actual_json['_links']['next']['href'], url($view->getUrl(), array('query' => array('page' => 1), 'absolute' => TRUE))); - $this->assertEqual($actual_json['_links']['last']['href'], url($view->getUrl(), array('query' => array('page' => 4), 'absolute' => TRUE))); - $this->assertEqual(array_keys($actual_json['_links']), array( - 'self', - 'first', - 'next', - 'last', - )); - - // Load the second page. - $actual_json_page_1 = $this->drupalGetHalJson($actual_json['_links']['next']['href']); - - $this->assertTrue(isset($actual_json_page_1['_embedded']) && isset($actual_json_page_1['_links']), 'Has _links and _embedded keys'); - - $this->assertEqual(count($actual_json_page_1['_embedded']['item']), 1); - $this->assertEqual($actual_json_page_1['_links']['self']['href'], url($view->getUrl(), array('query' => array('page' => 1), 'absolute' => TRUE))); - $this->assertEqual($actual_json_page_1['_links']['first']['href'], url($view->getUrl(), array('query' => array('page' => 0), 'absolute' => TRUE))); - $this->assertEqual($actual_json_page_1['_links']['prev']['href'], url($view->getUrl(), array('query' => array('page' => 0), 'absolute' => TRUE))); - $this->assertEqual($actual_json_page_1['_links']['next']['href'], url($view->getUrl(), array('query' => array('page' => 2), 'absolute' => TRUE))); - $this->assertEqual($actual_json_page_1['_links']['last']['href'], url($view->getUrl(), array('query' => array('page' => 4), 'absolute' => TRUE))); - $this->assertEqual(array_keys($actual_json_page_1['_links']), array( - 'self', - 'first', - 'prev', - 'next', - 'last', - )); - - // Test the entity rows - with paging. - $view = Views::getView('test_serializer_display_field'); - $view->setDisplay('rest_export_paging'); - $view->setCurrentPage(1); - $this->executeView($view); - - // Create the entity collection. - $collection = $this->getCollectionFromView($view); - $this->assertTrue($collection->hasLinks(), 'Collection created from a paging view has (hypermedia) link relations'); - $expected = $serializer->serialize($collection, 'hal_json'); - - $this->assertEqual(Json::encode($actual_json_page_1), $expected, 'The expected HAL output for page=1 was found.'); - - // Load the last page. - $actual_json_page_last = $this->drupalGetHalJson($actual_json['_links']['last']['href']); - - $this->assertTrue(isset($actual_json_page_last['_embedded']) && isset($actual_json_page_last['_links']), 'Has _links and _embedded keys'); - - $this->assertEqual(count($actual_json_page_last['_embedded']['item']), 1); - $this->assertEqual($actual_json_page_last['_links']['self']['href'], url($view->getUrl(), array('query' => array('page' => 4), 'absolute' => TRUE))); - $this->assertEqual($actual_json_page_last['_links']['first']['href'], url($view->getUrl(), array('query' => array('page' => 0), 'absolute' => TRUE))); - $this->assertEqual($actual_json_page_last['_links']['prev']['href'], url($view->getUrl(), array('query' => array('page' => 3), 'absolute' => TRUE))); - $this->assertEqual($actual_json_page_last['_links']['last']['href'], url($view->getUrl(), array('query' => array('page' => 4), 'absolute' => TRUE))); - $this->assertEqual(array_keys($actual_json_page_last['_links']), array( - 'self', - 'first', - 'prev', - 'last', - )); - - // Test the entity rows - with paging. - $view = Views::getView('test_serializer_display_field'); - $view->setDisplay('rest_export_paging'); - $view->setCurrentPage(4); - $this->executeView($view); - - // Create the entity collection. - $collection = $this->getCollectionFromView($view); - $this->assertTrue($collection->hasLinks(), 'Collection created from a paging view has (hypermedia) link relations'); - $expected = $serializer->serialize($collection, 'hal_json'); - - $this->assertEqual(Json::encode($actual_json_page_last), $expected, 'The expected HAL output for last page was found.'); - } - - /** - * Tests the Serializer paths and responses for field-based views. - */ - public function testSerializerFieldDisplayResponse() { - $view = Views::getView('test_serializer_display_field'); - $view->setDisplay('rest_export_1'); - // Mock the request content type by setting it on the display handler. - $view->display_handler->setContentType('hal_json'); - $this->executeView($view); - - $view_output = $view->preview(); - $view_result = array(); - foreach ($view->result as $row) { - $expected_row = array(); - foreach ($view->field as $id => $field) { - $expected_row[$id] = $field->render($row); - } - $view_result[] = $expected_row; - } - - $serializer = $this->container->get('serializer'); - $collection = $this->getCollectionFromView($view); - $expected = $serializer->serialize($collection, 'hal_json'); - - $actual_json = $this->drupalGetHalJson('test/serialize/field'); - - $this->assertIdentical(Json::encode($actual_json), drupal_render($view_output), 'Preview output matches the (reserialized) JSON returned from the view via HTTP GET.'); - $this->assertIdentical(Json::encode($actual_json), $expected, 'HAL serializer output matches the (reserialized) JSON returned from the view via HTTP GET.'); - $this->assertIdentical($actual_json['_embedded']['item'], $view_result, 'View result matches JSON returned from the view via HTTP GET'); - } - - /** - * Tests the Serializer paths and responses for field-based views with paging. - */ - public function testSerializerFieldDisplayPagingResponse() { - $view = Views::getView('test_serializer_display_field'); - $view->setDisplay('rest_export_paging'); - // Mock the request content type by setting it on the display handler. - $view->display_handler->setContentType('hal_json'); - $this->executeView($view); - - $view_output = $view->preview(); - $view_result = array(); - foreach ($view->result as $row) { - $expected_row = array(); - foreach ($view->field as $id => $field) { - $expected_row[$id] = $field->render($row); - } - $view_result[] = $expected_row; - } - - $serializer = $this->container->get('serializer'); - $collection = $this->getCollectionFromView($view); - $expected = $serializer->serialize($collection, 'hal_json'); - - $actual_json = $this->drupalGetHalJson('test/serialize/field-paging'); - - $this->assertIdentical(Json::encode($actual_json), drupal_render($view_output), 'Preview output matches the (reserialized) JSON returned from the view via HTTP GET.'); - $this->assertIdentical(Json::encode($actual_json), $expected, 'HAL serializer output matches the (reserialized) JSON returned from the view via HTTP GET.'); - $this->assertIdentical($actual_json['_embedded']['item'], $view_result, 'View result matches JSON returned from the view via HTTP GET'); - - // Make assertions on the structure of the response. - $this->assertTrue(isset($actual_json['_embedded']) && isset($actual_json['_links']), 'Has _links and _embedded keys'); - - $this->assertEqual(count($actual_json['_embedded']['item']), 1); $this->assertEqual($actual_json['_links']['self']['href'], $this->viewUrl($view)); $this->assertEqual($actual_json['_links']['first']['href'], $this->viewUrl($view, 0)); $this->assertEqual($actual_json['_links']['next']['href'], $this->viewUrl($view, 1));