Skip to content
Snippets Groups Projects
Commit ff6381f7 authored by Alex Pott's avatar Alex Pott
Browse files

Issue #2915490 by Wim Leers: Modernize & harden HalLinkManagerTest

parent 8e7dff7a
No related branches found
No related tags found
2 merge requests!7452Issue #1797438. HTML5 validation is preventing form submit and not fully...,!789Issue #3210310: Adjust Database API to remove deprecated Drupal 9 code in Drupal 10
...@@ -46,36 +46,123 @@ protected function setUp() { ...@@ -46,36 +46,123 @@ protected function setUp() {
/** /**
* @covers ::getTypeUri * @covers ::getTypeUri
* @dataProvider providerTestGetTypeUri
*/ */
public function testGetTypeUri() { public function testGetTypeUri($link_domain, $entity_type, $bundle, array $context, $expected_return, array $expected_context) {
$hal_settings = \Drupal::configFactory()->getEditable('hal.settings');
if ($link_domain === NULL) {
$hal_settings->clear('link_domain');
}
else {
$hal_settings->set('link_domain', $link_domain)->save(TRUE);
}
/* @var \Drupal\rest\LinkManager\TypeLinkManagerInterface $type_manager */ /* @var \Drupal\rest\LinkManager\TypeLinkManagerInterface $type_manager */
$type_manager = \Drupal::service('hal.link_manager.type'); $type_manager = \Drupal::service('hal.link_manager.type');
$base = Url::fromRoute('<front>', [], ['absolute' => TRUE])->toString();
$link = $type_manager->getTypeUri('node', 'page'); $link = $type_manager->getTypeUri($entity_type, $bundle, $context);
$this->assertSame($link, $base . 'rest/type/node/page'); $this->assertSame($link, str_replace('BASE_URL/', Url::fromRoute('<front>', [], ['absolute' => TRUE])->toString(), $expected_return));
// Now with optional context. $this->assertEquals($context, $expected_context);
$link = $type_manager->getTypeUri('node', 'page', ['hal_test' => TRUE]); }
$this->assertSame($link, 'hal_test_type');
// Test BC: hook_rest_type_uri_alter(). public function providerTestGetTypeUri() {
$link = $type_manager->getTypeUri('node', 'page', ['rest_test' => TRUE]); $base_test_case = [
$this->assertSame($link, 'rest_test_type'); 'link_domain' => NULL,
'entity_type' => 'node',
'bundle' => 'page',
];
return [
'site URL' => $base_test_case + [
'context' => [],
'link_domain' => NULL,
'expected return' => 'BASE_URL/rest/type/node/page',
'expected context' => [],
],
// Test hook_hal_type_uri_alter().
'site URL, with optional context, to test hook_hal_type_uri_alter()' => $base_test_case + [
'context' => ['hal_test' => TRUE],
'expected return' => 'hal_test_type',
'expected context' => ['hal_test' => TRUE],
],
// Test hook_rest_type_uri_alter() — for backwards compatibility.
'site URL, with optional context, to test hook_rest_type_uri_alter()' => $base_test_case + [
'context' => ['rest_test' => TRUE],
'expected return' => 'rest_test_type',
'expected context' => ['rest_test' => TRUE],
],
'configured URL' => [
'link_domain' => 'http://llamas-rock.com/for-real/',
'entity_type' => 'node',
'bundle' => 'page',
'context' => [],
'expected return' => 'http://llamas-rock.com/for-real/rest/type/node/page',
'expected context' => [],
],
];
} }
/** /**
* @covers ::getRelationUri * @covers ::getRelationUri
* @dataProvider providerTestGetRelationUri
*/ */
public function testGetRelationUri() { public function testGetRelationUri($link_domain, $entity_type, $bundle, $field_name, array $context, $expected_return, array $expected_context) {
$hal_settings = \Drupal::configFactory()->getEditable('hal.settings');
if ($link_domain === NULL) {
$hal_settings->clear('link_domain');
}
else {
$hal_settings->set('link_domain', $link_domain)->save(TRUE);
}
/* @var \Drupal\rest\LinkManager\RelationLinkManagerInterface $relation_manager */ /* @var \Drupal\rest\LinkManager\RelationLinkManagerInterface $relation_manager */
$relation_manager = \Drupal::service('hal.link_manager.relation'); $relation_manager = \Drupal::service('hal.link_manager.relation');
$base = Url::fromRoute('<front>', [], ['absolute' => TRUE])->toString();
$link = $relation_manager->getRelationUri('node', 'page', 'field_ref'); $link = $relation_manager->getRelationUri($entity_type, $bundle, $field_name, $context);
$this->assertSame($link, $base . 'rest/relation/node/page/field_ref'); $this->assertSame($link, str_replace('BASE_URL/', Url::fromRoute('<front>', [], ['absolute' => TRUE])->toString(), $expected_return));
// Now with optional context. $this->assertEquals($context, $expected_context);
$link = $relation_manager->getRelationUri('node', 'page', 'foobar', ['hal_test' => TRUE]); }
$this->assertSame($link, 'hal_test_relation');
// Test BC: hook_rest_relation_uri_alter(). public function providerTestGetRelationUri() {
$link = $relation_manager->getRelationUri('node', 'page', 'foobar', ['rest_test' => TRUE]); $field_name = $this->randomMachineName();
$this->assertSame($link, 'rest_test_relation'); $base_test_case = [
'link_domain' => NULL,
'entity_type' => 'node',
'bundle' => 'page',
'field_name' => $field_name,
];
return [
'site URL' => $base_test_case + [
'context' => [],
'link_domain' => NULL,
'expected return' => 'BASE_URL/rest/relation/node/page/' . $field_name,
'expected context' => [],
],
// Test hook_hal_relation_uri_alter().
'site URL, with optional context, to test hook_hal_relation_uri_alter()' => $base_test_case + [
'context' => ['hal_test' => TRUE],
'expected return' => 'hal_test_relation',
'expected context' => ['hal_test' => TRUE],
],
// Test hook_rest_relation_uri_alter() — for backwards compatibility.
'site URL, with optional context, to test hook_rest_relation_uri_alter()' => $base_test_case + [
'context' => ['rest_test' => TRUE],
'expected return' => 'rest_test_relation',
'expected context' => ['rest_test' => TRUE],
],
'configured URL' => [
'link_domain' => 'http://llamas-rock.com/for-real/',
'entity_type' => 'node',
'bundle' => 'page',
'field_name' => $field_name,
'context' => [],
'expected return' => 'http://llamas-rock.com/for-real/rest/relation/node/page/' . $field_name,
'expected context' => [],
],
];
} }
/** /**
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment