Issue #2928882 by mkalkbrenner, neclimdul, manuel.adan, yogeshmpawar, Berdir, alexpott: HAL links are broken if diffferent domains, protocols or ports are used in multisite or multi-domain setup

(cherry picked from commit 7dabb044d5)
merge-requests/1068/merge
catch 2021-09-09 14:45:59 +01:00
parent bbf3dcad25
commit e3dbbbd9c7
3 changed files with 25 additions and 11 deletions

View File

@ -129,7 +129,7 @@ class RelationLinkManager extends LinkManagerBase implements RelationLinkManager
* @see https://www.drupal.org/node/2877608
*/
protected function getRelations($context = []) {
$cid = 'hal:links:relations';
$cid = 'hal:links:relations:' . $this->getLinkDomain($context);
$cache = $this->cache->get($cid);
if (!$cache) {
$data = $this->writeCache($context);
@ -174,7 +174,7 @@ class RelationLinkManager extends LinkManagerBase implements RelationLinkManager
}
// These URIs only change when field info changes, so cache it permanently
// and only clear it when the fields cache is cleared.
$this->cache->set('hal:links:relations', $data, Cache::PERMANENT, ['entity_field_info']);
$this->cache->set('hal:links:relations:' . $this->getLinkDomain($context), $data, Cache::PERMANENT, ['entity_field_info']);
return $data;
}

View File

@ -108,7 +108,7 @@ class TypeLinkManager extends LinkManagerBase implements TypeLinkManagerInterfac
* corresponding type URI.
*/
protected function getTypes($context = []) {
$cid = 'hal:links:types';
$cid = 'hal:links:types:' . $this->getLinkDomain($context);
$cache = $this->cache->get($cid);
if (!$cache) {
$data = $this->writeCache($context);
@ -152,7 +152,7 @@ class TypeLinkManager extends LinkManagerBase implements TypeLinkManagerInterfac
}
// These URIs only change when entity info changes, so cache it permanently
// and only clear it when entity_info is cleared.
$this->cache->set('hal:links:types', $data, Cache::PERMANENT, ['entity_types']);
$this->cache->set('hal:links:types:' . $this->getLinkDomain($context), $data, Cache::PERMANENT, ['entity_types']);
return $data;
}

View File

@ -244,13 +244,27 @@ class HalLinkManagerTest extends KernelTestBase {
/** @var \Drupal\hal\LinkManager\LinkManager $link_manager */
$link_manager = \Drupal::service('hal.link_manager');
$link_manager->setLinkDomain('http://example.com/');
$link = $link_manager->getTypeUri('node', 'page', $serialization_context);
$this->assertEquals('http://example.com/rest/type/node/page', $link);
$this->assertEquals($serialization_context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY], new CacheableMetadata());
$link = $link_manager->getRelationUri('node', 'page', 'field_ref', $serialization_context);
$this->assertEquals('http://example.com/rest/relation/node/page/field_ref', $link);
$this->assertEquals($serialization_context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY], new CacheableMetadata());
/** @var \Drupal\hal\LinkManager\TypeLinkManager $type_link_manager */
$type_link_manager = \Drupal::service('hal.link_manager.type');
/** @var \Drupal\hal\LinkManager\RelationLinkManager $relation_link_manager */
$relation_link_manager = \Drupal::service('hal.link_manager.relation');
// One Drupal installation can serve multiple domains, protocols or ports.
foreach (['http://example.com/', 'https://example.com/', 'https://example.com:443/', 'http://drupal.org/'] as $domain) {
$link_manager->setLinkDomain($domain);
$link = $link_manager->getTypeUri('node', 'page', $serialization_context);
$this->assertEquals($domain . 'rest/type/node/page', $link);
$this->assertEquals($serialization_context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY], new CacheableMetadata());
$type_ids = $type_link_manager->getTypeInternalIds($link, $serialization_context);
$this->assertEquals(['entity_type' => 'node', 'bundle' => 'page'], $type_ids);
$link = $link_manager->getRelationUri('node', 'page', 'field_ref', $serialization_context);
$this->assertEquals($domain . 'rest/relation/node/page/field_ref', $link);
$this->assertEquals($serialization_context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY], new CacheableMetadata());
$relation_ids = $relation_link_manager->getRelationInternalIds($link, $serialization_context);
$this->assertEquals(['entity_type_id' => 'node', 'bundle' => 'page', 'field_name' => 'field_ref'], $relation_ids);
}
}
}