diff --git a/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php b/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php index 6a54b3c50205760e73d381dcc29618ad5dbf80cd..fce3ddca401743b11d32f933efc1de9f608f9395 100644 --- a/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php +++ b/core/modules/hal/src/Normalizer/ContentEntityNormalizer.php @@ -215,11 +215,17 @@ protected function getTypedDataIds($types, $context = array()) { $types = array($types); } + if (empty($types)) { + throw new UnexpectedValueException('No entity type(s) specified'); + } + foreach ($types as $type) { if (!isset($type['href'])) { throw new UnexpectedValueException('Type must contain an \'href\' attribute.'); } + $type_uri = $type['href']; + // Check whether the URI corresponds to a known type on this site. Break // once one does. if ($typed_data_ids = $this->linkManager->getTypeInternalIds($type['href'], $context)) { diff --git a/core/modules/hal/tests/src/Functional/EntityResource/HalEntityNormalizationTrait.php b/core/modules/hal/tests/src/Functional/EntityResource/HalEntityNormalizationTrait.php index d3bd85a9a9dfe7f84a071d43b1c1cb0625c6f1a0..836a1b4716491a3962bd5564daaa6f7dad61ad5f 100644 --- a/core/modules/hal/tests/src/Functional/EntityResource/HalEntityNormalizationTrait.php +++ b/core/modules/hal/tests/src/Functional/EntityResource/HalEntityNormalizationTrait.php @@ -97,21 +97,18 @@ protected function assertNormalizationEdgeCases($method, Url $url, array $reques if ($this->entity->getEntityType()->hasKey('bundle')) { $normalization = $this->getNormalizedPostEntity(); - // @todo Uncomment this in https://www.drupal.org/node/2824827. - // @codingStandardsIgnoreStart -/* + $normalization['_links']['type'] = Url::fromUri('base:rest/type/' . static::$entityTypeId . '/bad_bundle_name'); $request_options[RequestOptions::BODY] = $this->serializer->encode($normalization, static::$format); // DX: 400 when incorrect entity type bundle is specified. $response = $this->request($method, $url, $request_options); // @todo Uncomment, remove next 3 in https://www.drupal.org/node/2813853. -// $this->assertResourceErrorResponse(400, 'The type link relation must be specified.', $response); + // $this->assertResourceErrorResponse(400, 'No entity type(s) specified', $response); $this->assertSame(400, $response->getStatusCode()); $this->assertSame([static::$mimeType], $response->getHeader('Content-Type')); - $this->assertSame($this->serializer->encode(['error' => 'The type link relation must be specified.'], static::$format), (string) $response->getBody()); -*/ - // @codingStandardsIgnoreEnd + $this->assertSame($this->serializer->encode(['error' => 'No entity type(s) specified'], static::$format), (string) $response->getBody()); + unset($normalization['_links']['type']); $request_options[RequestOptions::BODY] = $this->serializer->encode($normalization, static::$format); diff --git a/core/modules/hal/tests/src/Kernel/DenormalizeTest.php b/core/modules/hal/tests/src/Kernel/DenormalizeTest.php index 8f64049917976381e40fe6afd81f66849653136e..3f9388bce4a462a8163ba406c288d7b8fbb63f11 100644 --- a/core/modules/hal/tests/src/Kernel/DenormalizeTest.php +++ b/core/modules/hal/tests/src/Kernel/DenormalizeTest.php @@ -74,6 +74,36 @@ public function testTypeHandling() { } } + /** + * Tests link relation handling with an invalid type. + */ + public function testTypeHandlingWithInvalidType() { + $data_with_invalid_type = array( + '_links' => array( + 'type' => array( + 'href' => Url::fromUri('base:rest/type/entity_test/entity_test_invalid', array('absolute' => TRUE))->toString(), + ), + ), + ); + + $this->setExpectedException(UnexpectedValueException::class); + $this->serializer->denormalize($data_with_invalid_type, $this->entityClass, $this->format); + } + + /** + * Tests link relation handling with no types. + */ + public function testTypeHandlingWithNoTypes() { + $data_with_no_types = array( + '_links' => array( + 'type' => array(), + ), + ); + + $this->setExpectedException(UnexpectedValueException::class); + $this->serializer->denormalize($data_with_no_types, $this->entityClass, $this->format); + } + /** * Test that a field set to an empty array is different than an absent field. */