diff --git a/core/modules/rest/src/Tests/NodeTest.php b/core/modules/rest/src/Tests/NodeTest.php index 9eedd2c..605d9fa 100644 --- a/core/modules/rest/src/Tests/NodeTest.php +++ b/core/modules/rest/src/Tests/NodeTest.php @@ -83,6 +83,7 @@ public function testNodes() { $this->httpRequest($node->urlInfo()->setRouteParameter('_format', $this->defaultFormat), 'GET'); $this->assertResponse(200); $this->assertHeader('Content-type', $this->defaultMimeType); + $this->assertLinkHeader($node, ['canonical', 'edit-form', 'version-history']); // Also check that JSON works and the routing system selects the correct // REST route. @@ -90,6 +91,7 @@ public function testNodes() { $this->httpRequest($node->urlInfo()->setRouteParameter('_format', 'json'), 'GET'); $this->assertResponse(200); $this->assertHeader('Content-type', 'application/json'); + $this->assertLinkHeader($node, ['canonical', 'edit-form', 'version-history']); // Check that a simple PATCH update to the node title works as expected. $this->enableNodeConfiguration('PATCH', 'update'); diff --git a/core/modules/rest/src/Tests/RESTTestBase.php b/core/modules/rest/src/Tests/RESTTestBase.php index 4779b6b..080c5a4 100644 --- a/core/modules/rest/src/Tests/RESTTestBase.php +++ b/core/modules/rest/src/Tests/RESTTestBase.php @@ -528,4 +528,32 @@ protected function configEntityValues($entity_type_id) { return $values; } + /** + * Tests whether the link header was produced correctly. + * + * @param \Drupal\Core\Entity\EntityInterface $entity + * The entity + * @param array $link_relationships + * The used link relationships. + * + * @return bool + */ + protected function assertLinkHeader($entity, array $link_relationships = [ + 'canonical', + 'edit-form' + ]) { + // Add expected Link Headers. + $link_headers = []; + foreach ($link_relationships as $link) { + if ($entity->hasLinkTemplate($link)) { + $canonical_url = $entity->toUrl($link) + ->setAbsolute(TRUE) + ->toString(TRUE) + ->getGeneratedUrl(); + $link_headers[] = '<' . $canonical_url . '>; rel="' . $link . '"'; + } + } + return $this->assertHeader('Link', implode(',', $link_headers)); + } + } diff --git a/core/modules/rest/src/Tests/ReadTest.php b/core/modules/rest/src/Tests/ReadTest.php index b522ff7..d06eda9 100644 --- a/core/modules/rest/src/Tests/ReadTest.php +++ b/core/modules/rest/src/Tests/ReadTest.php @@ -92,16 +92,6 @@ public function testRead() { $expected_message = Json::encode(['message' => 'The "' . $entity_type . '" parameter was not converted for the path "' . $path . '" (route name: "rest.entity.' . $entity_type . '.GET.hal_json")']); $this->assertIdentical($expected_message, $response, 'Response message is correct.'); - // Add expected Link Headers. - $canonical_url = $entity->toUrl('canonical') - ->toString(TRUE) - ->getGeneratedUrl(); - $edit_form = $entity->toUrl('edit-form') - ->toString(TRUE) - ->getGeneratedUrl(); - $expected_header = $canonical_url; - $this->assertHeader('Link', $expected_header); - // Make sure that field level access works and that the according field is // not available in the response. Only applies to entity_test. // @see entity_test_entity_field_access() @@ -110,9 +100,9 @@ public function testRead() { $entity->save(); $response = $this->httpRequest($this->getReadUrl($entity), 'GET'); $this->assertResponse(200); - $this->assertHeader('Link', 'foo'); $data = Json::decode($response); $this->assertFalse(isset($data['field_test_text']), 'Field access protected field is not visible in the response.'); + $this->assertLinkHeader($entity); } // Try to read an entity without proper permissions.