Chris@0: installEntitySchema('node'); Chris@0: Chris@0: NodeType::create([ Chris@0: 'type' => 'page', Chris@0: ])->save(); Chris@0: FieldStorageConfig::create([ Chris@0: 'entity_type' => 'node', Chris@0: 'type' => 'entity_reference', Chris@0: 'field_name' => 'field_ref', Chris@0: ])->save(); Chris@0: FieldConfig::create([ Chris@0: 'entity_type' => 'node', Chris@0: 'bundle' => 'page', Chris@0: 'field_name' => 'field_ref', Chris@0: ])->save(); Chris@0: Chris@0: \Drupal::service('router.builder')->rebuild(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @covers ::getTypeUri Chris@0: * @dataProvider providerTestGetTypeUri Chris@17: * @expectedDeprecation The deprecated alter hook hook_rest_type_uri_alter() is implemented in these functions: hal_test_rest_type_uri_alter. This hook is deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0. Implement hook_hal_type_uri_alter() instead. Chris@0: */ Chris@0: public function testGetTypeUri($link_domain, $entity_type, $bundle, array $context, $expected_return, array $expected_context) { Chris@0: $hal_settings = \Drupal::configFactory()->getEditable('hal.settings'); Chris@0: Chris@0: if ($link_domain === NULL) { Chris@0: $hal_settings->clear('link_domain'); Chris@0: } Chris@0: else { Chris@0: $hal_settings->set('link_domain', $link_domain)->save(TRUE); Chris@0: } Chris@0: Chris@0: /* @var \Drupal\rest\LinkManager\TypeLinkManagerInterface $type_manager */ Chris@0: $type_manager = \Drupal::service('hal.link_manager.type'); Chris@0: Chris@0: $link = $type_manager->getTypeUri($entity_type, $bundle, $context); Chris@0: $this->assertSame($link, str_replace('BASE_URL/', Url::fromRoute('', [], ['absolute' => TRUE])->toString(), $expected_return)); Chris@0: $this->assertEquals($context, $expected_context); Chris@0: } Chris@0: Chris@0: public function providerTestGetTypeUri() { Chris@14: $serialization_context_collecting_cacheability = [ Chris@17: CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => new CacheableMetadata(), Chris@14: ]; Chris@14: $expected_serialization_context_cacheability_url_site = [ Chris@17: CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => (new CacheableMetadata())->setCacheContexts(['url.site']), Chris@14: ]; Chris@14: Chris@0: $base_test_case = [ Chris@0: 'link_domain' => NULL, Chris@0: 'entity_type' => 'node', Chris@0: 'bundle' => 'page', Chris@0: ]; Chris@0: Chris@0: return [ Chris@0: 'site URL' => $base_test_case + [ Chris@0: 'context' => [], Chris@0: 'link_domain' => NULL, Chris@0: 'expected return' => 'BASE_URL/rest/type/node/page', Chris@0: 'expected context' => [], Chris@0: ], Chris@14: 'site URL, with optional context to collect cacheability metadata' => $base_test_case + [ Chris@14: 'context' => $serialization_context_collecting_cacheability, Chris@14: 'expected return' => 'BASE_URL/rest/type/node/page', Chris@14: 'expected context' => $expected_serialization_context_cacheability_url_site, Chris@14: ], Chris@0: // Test hook_hal_type_uri_alter(). Chris@0: 'site URL, with optional context, to test hook_hal_type_uri_alter()' => $base_test_case + [ Chris@0: 'context' => ['hal_test' => TRUE], Chris@0: 'expected return' => 'hal_test_type', Chris@0: 'expected context' => ['hal_test' => TRUE], Chris@0: ], Chris@14: 'site URL, with optional context, to test hook_hal_type_uri_alter(), and collecting cacheability metadata' => $base_test_case + [ Chris@14: 'context' => ['hal_test' => TRUE] + $serialization_context_collecting_cacheability, Chris@14: 'expected return' => 'hal_test_type', Chris@14: // No cacheability metadata bubbled. Chris@14: 'expected context' => ['hal_test' => TRUE] + $serialization_context_collecting_cacheability, Chris@14: ], Chris@0: // Test hook_rest_type_uri_alter() — for backwards compatibility. Chris@0: 'site URL, with optional context, to test hook_rest_type_uri_alter()' => $base_test_case + [ Chris@0: 'context' => ['rest_test' => TRUE], Chris@0: 'expected return' => 'rest_test_type', Chris@0: 'expected context' => ['rest_test' => TRUE], Chris@0: ], Chris@14: 'site URL, with optional context, to test hook_rest_type_uri_alter(), and collecting cacheability metadata' => $base_test_case + [ Chris@14: 'context' => ['rest_test' => TRUE] + $serialization_context_collecting_cacheability, Chris@14: 'expected return' => 'rest_test_type', Chris@14: // No cacheability metadata bubbled. Chris@14: 'expected context' => ['rest_test' => TRUE] + $serialization_context_collecting_cacheability, Chris@14: ], Chris@0: 'configured URL' => [ Chris@0: 'link_domain' => 'http://llamas-rock.com/for-real/', Chris@0: 'entity_type' => 'node', Chris@0: 'bundle' => 'page', Chris@0: 'context' => [], Chris@0: 'expected return' => 'http://llamas-rock.com/for-real/rest/type/node/page', Chris@0: 'expected context' => [], Chris@0: ], Chris@14: 'configured URL, with optional context to collect cacheability metadata' => [ Chris@14: 'link_domain' => 'http://llamas-rock.com/for-real/', Chris@14: 'entity_type' => 'node', Chris@14: 'bundle' => 'page', Chris@14: 'context' => $serialization_context_collecting_cacheability, Chris@14: 'expected return' => 'http://llamas-rock.com/for-real/rest/type/node/page', Chris@14: 'expected context' => [ Chris@14: CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => (new CacheableMetadata())->setCacheTags(['config:hal.settings']), Chris@14: ], Chris@14: ], Chris@0: ]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @covers ::getRelationUri Chris@0: * @dataProvider providerTestGetRelationUri Chris@17: * @expectedDeprecation The deprecated alter hook hook_rest_relation_uri_alter() is implemented in these functions: hal_test_rest_relation_uri_alter. This hook is deprecated in Drupal 8.3.x and will be removed before Drupal 9.0.0. Implement hook_hal_relation_uri_alter() instead. Chris@0: */ Chris@0: public function testGetRelationUri($link_domain, $entity_type, $bundle, $field_name, array $context, $expected_return, array $expected_context) { Chris@0: $hal_settings = \Drupal::configFactory()->getEditable('hal.settings'); Chris@0: Chris@0: if ($link_domain === NULL) { Chris@0: $hal_settings->clear('link_domain'); Chris@0: } Chris@0: else { Chris@0: $hal_settings->set('link_domain', $link_domain)->save(TRUE); Chris@0: } Chris@0: Chris@0: /* @var \Drupal\rest\LinkManager\RelationLinkManagerInterface $relation_manager */ Chris@0: $relation_manager = \Drupal::service('hal.link_manager.relation'); Chris@0: Chris@0: $link = $relation_manager->getRelationUri($entity_type, $bundle, $field_name, $context); Chris@0: $this->assertSame($link, str_replace('BASE_URL/', Url::fromRoute('', [], ['absolute' => TRUE])->toString(), $expected_return)); Chris@0: $this->assertEquals($context, $expected_context); Chris@0: } Chris@0: Chris@0: public function providerTestGetRelationUri() { Chris@14: $serialization_context_collecting_cacheability = [ Chris@17: CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => new CacheableMetadata(), Chris@14: ]; Chris@14: $expected_serialization_context_cacheability_url_site = [ Chris@17: CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => (new CacheableMetadata())->setCacheContexts(['url.site']), Chris@14: ]; Chris@14: Chris@0: $field_name = $this->randomMachineName(); Chris@0: $base_test_case = [ Chris@0: 'link_domain' => NULL, Chris@0: 'entity_type' => 'node', Chris@0: 'bundle' => 'page', Chris@0: 'field_name' => $field_name, Chris@0: ]; Chris@0: Chris@0: return [ Chris@0: 'site URL' => $base_test_case + [ Chris@0: 'context' => [], Chris@0: 'link_domain' => NULL, Chris@0: 'expected return' => 'BASE_URL/rest/relation/node/page/' . $field_name, Chris@0: 'expected context' => [], Chris@0: ], Chris@14: 'site URL, with optional context to collect cacheability metadata' => $base_test_case + [ Chris@14: 'context' => $serialization_context_collecting_cacheability, Chris@14: 'expected return' => 'BASE_URL/rest/relation/node/page/' . $field_name, Chris@14: 'expected context' => $expected_serialization_context_cacheability_url_site, Chris@14: ], Chris@0: // Test hook_hal_relation_uri_alter(). Chris@0: 'site URL, with optional context, to test hook_hal_relation_uri_alter()' => $base_test_case + [ Chris@0: 'context' => ['hal_test' => TRUE], Chris@0: 'expected return' => 'hal_test_relation', Chris@0: 'expected context' => ['hal_test' => TRUE], Chris@0: ], Chris@14: 'site URL, with optional context, to test hook_hal_relation_uri_alter(), and collecting cacheability metadata' => $base_test_case + [ Chris@14: 'context' => ['hal_test' => TRUE] + $serialization_context_collecting_cacheability, Chris@14: 'expected return' => 'hal_test_relation', Chris@14: // No cacheability metadata bubbled. Chris@14: 'expected context' => ['hal_test' => TRUE] + $serialization_context_collecting_cacheability, Chris@14: ], Chris@0: // Test hook_rest_relation_uri_alter() — for backwards compatibility. Chris@0: 'site URL, with optional context, to test hook_rest_relation_uri_alter()' => $base_test_case + [ Chris@0: 'context' => ['rest_test' => TRUE], Chris@0: 'expected return' => 'rest_test_relation', Chris@0: 'expected context' => ['rest_test' => TRUE], Chris@0: ], Chris@14: 'site URL, with optional context, to test hook_rest_relation_uri_alter(), and collecting cacheability metadata' => $base_test_case + [ Chris@14: 'context' => ['rest_test' => TRUE] + $serialization_context_collecting_cacheability, Chris@14: 'expected return' => 'rest_test_relation', Chris@14: // No cacheability metadata bubbled. Chris@14: 'expected context' => ['rest_test' => TRUE] + $serialization_context_collecting_cacheability, Chris@14: ], Chris@0: 'configured URL' => [ Chris@0: 'link_domain' => 'http://llamas-rock.com/for-real/', Chris@0: 'entity_type' => 'node', Chris@0: 'bundle' => 'page', Chris@0: 'field_name' => $field_name, Chris@0: 'context' => [], Chris@0: 'expected return' => 'http://llamas-rock.com/for-real/rest/relation/node/page/' . $field_name, Chris@0: 'expected context' => [], Chris@0: ], Chris@14: 'configured URL, with optional context to collect cacheability metadata' => [ Chris@14: 'link_domain' => 'http://llamas-rock.com/for-real/', Chris@14: 'entity_type' => 'node', Chris@14: 'bundle' => 'page', Chris@14: 'field_name' => $field_name, Chris@14: 'context' => $serialization_context_collecting_cacheability, Chris@14: 'expected return' => 'http://llamas-rock.com/for-real/rest/relation/node/page/' . $field_name, Chris@14: 'expected context' => [ Chris@14: CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => (new CacheableMetadata())->setCacheTags(['config:hal.settings']), Chris@14: ], Chris@14: ], Chris@0: ]; Chris@0: } Chris@0: Chris@0: /** Chris@0: * @covers ::getRelationInternalIds Chris@0: */ Chris@0: public function testGetRelationInternalIds() { Chris@0: /* @var \Drupal\rest\LinkManager\RelationLinkManagerInterface $relation_manager */ Chris@0: $relation_manager = \Drupal::service('hal.link_manager.relation'); Chris@0: $link = $relation_manager->getRelationUri('node', 'page', 'field_ref'); Chris@0: $internal_ids = $relation_manager->getRelationInternalIds($link); Chris@0: Chris@0: $this->assertEquals([ Chris@0: 'entity_type_id' => 'node', Chris@0: 'entity_type' => \Drupal::entityTypeManager()->getDefinition('node'), Chris@0: 'bundle' => 'page', Chris@17: 'field_name' => 'field_ref', Chris@0: ], $internal_ids); Chris@0: } Chris@0: Chris@0: /** Chris@0: * @covers ::setLinkDomain Chris@0: */ Chris@0: public function testHalLinkManagersSetLinkDomain() { Chris@14: $serialization_context = [ Chris@17: CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => new CacheableMetadata(), Chris@14: ]; Chris@14: Chris@0: /* @var \Drupal\rest\LinkManager\LinkManager $link_manager */ Chris@0: $link_manager = \Drupal::service('hal.link_manager'); Chris@0: $link_manager->setLinkDomain('http://example.com/'); Chris@14: $link = $link_manager->getTypeUri('node', 'page', $serialization_context); Chris@0: $this->assertEqual($link, 'http://example.com/rest/type/node/page'); Chris@14: $this->assertEqual(new CacheableMetadata(), $serialization_context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]); Chris@14: $link = $link_manager->getRelationUri('node', 'page', 'field_ref', $serialization_context); Chris@0: $this->assertEqual($link, 'http://example.com/rest/relation/node/page/field_ref'); Chris@14: $this->assertEqual(new CacheableMetadata(), $serialization_context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]); Chris@0: } Chris@0: Chris@0: }