Commit e3dbbbd9 authored by catch's avatar catch
Browse files

Issue #2928882 by mkalkbrenner, neclimdul, manuel.adan, yogeshmpawar, Berdir,...

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 7dabb044)
parent bbf3dcad
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -129,7 +129,7 @@ public function getRelationInternalIds($relation_uri, $context = []) {
   * @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 @@ protected function writeCache($context = []) {
    }
    // 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;
  }

+2 −2
Original line number Diff line number Diff line
@@ -108,7 +108,7 @@ public function getTypeInternalIds($type_uri, $context = []) {
   *   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 @@ protected function writeCache($context = []) {
    }
    // 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;
  }

+21 −7
Original line number Diff line number Diff line
@@ -244,13 +244,27 @@ public function testHalLinkManagersSetLinkDomain() {

    /** @var \Drupal\hal\LinkManager\LinkManager $link_manager */
    $link_manager = \Drupal::service('hal.link_manager');
    $link_manager->setLinkDomain('http://example.com/');
    /** @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('http://example.com/rest/type/node/page', $link);
      $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('http://example.com/rest/relation/node/page/field_ref', $link);
      $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);
    }
  }

}