3 namespace Drupal\Tests\hal\Kernel;
5 use Drupal\Core\Cache\CacheableMetadata;
7 use Drupal\field\Entity\FieldConfig;
8 use Drupal\field\Entity\FieldStorageConfig;
9 use Drupal\KernelTests\KernelTestBase;
10 use Drupal\node\Entity\NodeType;
11 use Drupal\serialization\Normalizer\CacheableNormalizerInterface;
14 * @coversDefaultClass \Drupal\hal\LinkManager\LinkManager
18 class HalLinkManagerTest extends KernelTestBase {
23 public static $modules = ['hal', 'hal_test', 'serialization', 'system', 'node', 'user', 'field'];
28 protected function setUp() {
31 $this->installEntitySchema('node');
36 FieldStorageConfig::create([
37 'entity_type' => 'node',
38 'type' => 'entity_reference',
39 'field_name' => 'field_ref',
42 'entity_type' => 'node',
44 'field_name' => 'field_ref',
47 \Drupal::service('router.builder')->rebuild();
51 * @covers ::getTypeUri
52 * @dataProvider providerTestGetTypeUri
53 * @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.
55 public function testGetTypeUri($link_domain, $entity_type, $bundle, array $context, $expected_return, array $expected_context) {
56 $hal_settings = \Drupal::configFactory()->getEditable('hal.settings');
58 if ($link_domain === NULL) {
59 $hal_settings->clear('link_domain');
62 $hal_settings->set('link_domain', $link_domain)->save(TRUE);
65 /* @var \Drupal\rest\LinkManager\TypeLinkManagerInterface $type_manager */
66 $type_manager = \Drupal::service('hal.link_manager.type');
68 $link = $type_manager->getTypeUri($entity_type, $bundle, $context);
69 $this->assertSame($link, str_replace('BASE_URL/', Url::fromRoute('<front>', [], ['absolute' => TRUE])->toString(), $expected_return));
70 $this->assertEquals($context, $expected_context);
73 public function providerTestGetTypeUri() {
74 $serialization_context_collecting_cacheability = [
75 CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => new CacheableMetadata(),
77 $expected_serialization_context_cacheability_url_site = [
78 CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => (new CacheableMetadata())->setCacheContexts(['url.site']),
82 'link_domain' => NULL,
83 'entity_type' => 'node',
88 'site URL' => $base_test_case + [
90 'link_domain' => NULL,
91 'expected return' => 'BASE_URL/rest/type/node/page',
92 'expected context' => [],
94 'site URL, with optional context to collect cacheability metadata' => $base_test_case + [
95 'context' => $serialization_context_collecting_cacheability,
96 'expected return' => 'BASE_URL/rest/type/node/page',
97 'expected context' => $expected_serialization_context_cacheability_url_site,
99 // Test hook_hal_type_uri_alter().
100 'site URL, with optional context, to test hook_hal_type_uri_alter()' => $base_test_case + [
101 'context' => ['hal_test' => TRUE],
102 'expected return' => 'hal_test_type',
103 'expected context' => ['hal_test' => TRUE],
105 'site URL, with optional context, to test hook_hal_type_uri_alter(), and collecting cacheability metadata' => $base_test_case + [
106 'context' => ['hal_test' => TRUE] + $serialization_context_collecting_cacheability,
107 'expected return' => 'hal_test_type',
108 // No cacheability metadata bubbled.
109 'expected context' => ['hal_test' => TRUE] + $serialization_context_collecting_cacheability,
111 // Test hook_rest_type_uri_alter() — for backwards compatibility.
112 'site URL, with optional context, to test hook_rest_type_uri_alter()' => $base_test_case + [
113 'context' => ['rest_test' => TRUE],
114 'expected return' => 'rest_test_type',
115 'expected context' => ['rest_test' => TRUE],
117 'site URL, with optional context, to test hook_rest_type_uri_alter(), and collecting cacheability metadata' => $base_test_case + [
118 'context' => ['rest_test' => TRUE] + $serialization_context_collecting_cacheability,
119 'expected return' => 'rest_test_type',
120 // No cacheability metadata bubbled.
121 'expected context' => ['rest_test' => TRUE] + $serialization_context_collecting_cacheability,
123 'configured URL' => [
124 'link_domain' => 'http://llamas-rock.com/for-real/',
125 'entity_type' => 'node',
128 'expected return' => 'http://llamas-rock.com/for-real/rest/type/node/page',
129 'expected context' => [],
131 'configured URL, with optional context to collect cacheability metadata' => [
132 'link_domain' => 'http://llamas-rock.com/for-real/',
133 'entity_type' => 'node',
135 'context' => $serialization_context_collecting_cacheability,
136 'expected return' => 'http://llamas-rock.com/for-real/rest/type/node/page',
137 'expected context' => [
138 CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => (new CacheableMetadata())->setCacheTags(['config:hal.settings']),
145 * @covers ::getRelationUri
146 * @dataProvider providerTestGetRelationUri
147 * @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.
149 public function testGetRelationUri($link_domain, $entity_type, $bundle, $field_name, array $context, $expected_return, array $expected_context) {
150 $hal_settings = \Drupal::configFactory()->getEditable('hal.settings');
152 if ($link_domain === NULL) {
153 $hal_settings->clear('link_domain');
156 $hal_settings->set('link_domain', $link_domain)->save(TRUE);
159 /* @var \Drupal\rest\LinkManager\RelationLinkManagerInterface $relation_manager */
160 $relation_manager = \Drupal::service('hal.link_manager.relation');
162 $link = $relation_manager->getRelationUri($entity_type, $bundle, $field_name, $context);
163 $this->assertSame($link, str_replace('BASE_URL/', Url::fromRoute('<front>', [], ['absolute' => TRUE])->toString(), $expected_return));
164 $this->assertEquals($context, $expected_context);
167 public function providerTestGetRelationUri() {
168 $serialization_context_collecting_cacheability = [
169 CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => new CacheableMetadata(),
171 $expected_serialization_context_cacheability_url_site = [
172 CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => (new CacheableMetadata())->setCacheContexts(['url.site']),
175 $field_name = $this->randomMachineName();
177 'link_domain' => NULL,
178 'entity_type' => 'node',
180 'field_name' => $field_name,
184 'site URL' => $base_test_case + [
186 'link_domain' => NULL,
187 'expected return' => 'BASE_URL/rest/relation/node/page/' . $field_name,
188 'expected context' => [],
190 'site URL, with optional context to collect cacheability metadata' => $base_test_case + [
191 'context' => $serialization_context_collecting_cacheability,
192 'expected return' => 'BASE_URL/rest/relation/node/page/' . $field_name,
193 'expected context' => $expected_serialization_context_cacheability_url_site,
195 // Test hook_hal_relation_uri_alter().
196 'site URL, with optional context, to test hook_hal_relation_uri_alter()' => $base_test_case + [
197 'context' => ['hal_test' => TRUE],
198 'expected return' => 'hal_test_relation',
199 'expected context' => ['hal_test' => TRUE],
201 'site URL, with optional context, to test hook_hal_relation_uri_alter(), and collecting cacheability metadata' => $base_test_case + [
202 'context' => ['hal_test' => TRUE] + $serialization_context_collecting_cacheability,
203 'expected return' => 'hal_test_relation',
204 // No cacheability metadata bubbled.
205 'expected context' => ['hal_test' => TRUE] + $serialization_context_collecting_cacheability,
207 // Test hook_rest_relation_uri_alter() — for backwards compatibility.
208 'site URL, with optional context, to test hook_rest_relation_uri_alter()' => $base_test_case + [
209 'context' => ['rest_test' => TRUE],
210 'expected return' => 'rest_test_relation',
211 'expected context' => ['rest_test' => TRUE],
213 'site URL, with optional context, to test hook_rest_relation_uri_alter(), and collecting cacheability metadata' => $base_test_case + [
214 'context' => ['rest_test' => TRUE] + $serialization_context_collecting_cacheability,
215 'expected return' => 'rest_test_relation',
216 // No cacheability metadata bubbled.
217 'expected context' => ['rest_test' => TRUE] + $serialization_context_collecting_cacheability,
219 'configured URL' => [
220 'link_domain' => 'http://llamas-rock.com/for-real/',
221 'entity_type' => 'node',
223 'field_name' => $field_name,
225 'expected return' => 'http://llamas-rock.com/for-real/rest/relation/node/page/' . $field_name,
226 'expected context' => [],
228 'configured URL, with optional context to collect cacheability metadata' => [
229 'link_domain' => 'http://llamas-rock.com/for-real/',
230 'entity_type' => 'node',
232 'field_name' => $field_name,
233 'context' => $serialization_context_collecting_cacheability,
234 'expected return' => 'http://llamas-rock.com/for-real/rest/relation/node/page/' . $field_name,
235 'expected context' => [
236 CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => (new CacheableMetadata())->setCacheTags(['config:hal.settings']),
243 * @covers ::getRelationInternalIds
245 public function testGetRelationInternalIds() {
246 /* @var \Drupal\rest\LinkManager\RelationLinkManagerInterface $relation_manager */
247 $relation_manager = \Drupal::service('hal.link_manager.relation');
248 $link = $relation_manager->getRelationUri('node', 'page', 'field_ref');
249 $internal_ids = $relation_manager->getRelationInternalIds($link);
251 $this->assertEquals([
252 'entity_type_id' => 'node',
253 'entity_type' => \Drupal::entityTypeManager()->getDefinition('node'),
255 'field_name' => 'field_ref',
260 * @covers ::setLinkDomain
262 public function testHalLinkManagersSetLinkDomain() {
263 $serialization_context = [
264 CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => new CacheableMetadata(),
267 /* @var \Drupal\rest\LinkManager\LinkManager $link_manager */
268 $link_manager = \Drupal::service('hal.link_manager');
269 $link_manager->setLinkDomain('http://example.com/');
270 $link = $link_manager->getTypeUri('node', 'page', $serialization_context);
271 $this->assertEqual($link, 'http://example.com/rest/type/node/page');
272 $this->assertEqual(new CacheableMetadata(), $serialization_context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]);
273 $link = $link_manager->getRelationUri('node', 'page', 'field_ref', $serialization_context);
274 $this->assertEqual($link, 'http://example.com/rest/relation/node/page/field_ref');
275 $this->assertEqual(new CacheableMetadata(), $serialization_context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]);