Chris@0
|
1 <?php
|
Chris@0
|
2
|
Chris@0
|
3 namespace Drupal\Tests\hal\Kernel;
|
Chris@0
|
4
|
Chris@14
|
5 use Drupal\Core\Cache\CacheableMetadata;
|
Chris@0
|
6 use Drupal\Core\Url;
|
Chris@0
|
7 use Drupal\field\Entity\FieldConfig;
|
Chris@0
|
8 use Drupal\field\Entity\FieldStorageConfig;
|
Chris@0
|
9 use Drupal\KernelTests\KernelTestBase;
|
Chris@0
|
10 use Drupal\node\Entity\NodeType;
|
Chris@14
|
11 use Drupal\serialization\Normalizer\CacheableNormalizerInterface;
|
Chris@0
|
12
|
Chris@0
|
13 /**
|
Chris@0
|
14 * @coversDefaultClass \Drupal\hal\LinkManager\LinkManager
|
Chris@0
|
15 * @group hal
|
Chris@0
|
16 */
|
Chris@0
|
17 class HalLinkManagerTest extends KernelTestBase {
|
Chris@0
|
18
|
Chris@0
|
19 /**
|
Chris@0
|
20 * {@inheritdoc}
|
Chris@0
|
21 */
|
Chris@0
|
22 public static $modules = ['hal', 'hal_test', 'serialization', 'system', 'node', 'user', 'field'];
|
Chris@0
|
23
|
Chris@0
|
24 /**
|
Chris@0
|
25 * {@inheritdoc}
|
Chris@0
|
26 */
|
Chris@0
|
27 protected function setUp() {
|
Chris@0
|
28 parent::setUp();
|
Chris@0
|
29
|
Chris@0
|
30 $this->installEntitySchema('node');
|
Chris@0
|
31
|
Chris@0
|
32 NodeType::create([
|
Chris@0
|
33 'type' => 'page',
|
Chris@0
|
34 ])->save();
|
Chris@0
|
35 FieldStorageConfig::create([
|
Chris@0
|
36 'entity_type' => 'node',
|
Chris@0
|
37 'type' => 'entity_reference',
|
Chris@0
|
38 'field_name' => 'field_ref',
|
Chris@0
|
39 ])->save();
|
Chris@0
|
40 FieldConfig::create([
|
Chris@0
|
41 'entity_type' => 'node',
|
Chris@0
|
42 'bundle' => 'page',
|
Chris@0
|
43 'field_name' => 'field_ref',
|
Chris@0
|
44 ])->save();
|
Chris@0
|
45
|
Chris@0
|
46 \Drupal::service('router.builder')->rebuild();
|
Chris@0
|
47 }
|
Chris@0
|
48
|
Chris@0
|
49 /**
|
Chris@0
|
50 * @covers ::getTypeUri
|
Chris@0
|
51 * @dataProvider providerTestGetTypeUri
|
Chris@0
|
52 */
|
Chris@0
|
53 public function testGetTypeUri($link_domain, $entity_type, $bundle, array $context, $expected_return, array $expected_context) {
|
Chris@0
|
54 $hal_settings = \Drupal::configFactory()->getEditable('hal.settings');
|
Chris@0
|
55
|
Chris@0
|
56 if ($link_domain === NULL) {
|
Chris@0
|
57 $hal_settings->clear('link_domain');
|
Chris@0
|
58 }
|
Chris@0
|
59 else {
|
Chris@0
|
60 $hal_settings->set('link_domain', $link_domain)->save(TRUE);
|
Chris@0
|
61 }
|
Chris@0
|
62
|
Chris@0
|
63 /* @var \Drupal\rest\LinkManager\TypeLinkManagerInterface $type_manager */
|
Chris@0
|
64 $type_manager = \Drupal::service('hal.link_manager.type');
|
Chris@0
|
65
|
Chris@0
|
66 $link = $type_manager->getTypeUri($entity_type, $bundle, $context);
|
Chris@0
|
67 $this->assertSame($link, str_replace('BASE_URL/', Url::fromRoute('<front>', [], ['absolute' => TRUE])->toString(), $expected_return));
|
Chris@0
|
68 $this->assertEquals($context, $expected_context);
|
Chris@0
|
69 }
|
Chris@0
|
70
|
Chris@0
|
71 public function providerTestGetTypeUri() {
|
Chris@14
|
72 $serialization_context_collecting_cacheability = [
|
Chris@14
|
73 CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => new CacheableMetadata()
|
Chris@14
|
74 ];
|
Chris@14
|
75 $expected_serialization_context_cacheability_url_site = [
|
Chris@14
|
76 CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => (new CacheableMetadata())->setCacheContexts(['url.site'])
|
Chris@14
|
77 ];
|
Chris@14
|
78
|
Chris@0
|
79 $base_test_case = [
|
Chris@0
|
80 'link_domain' => NULL,
|
Chris@0
|
81 'entity_type' => 'node',
|
Chris@0
|
82 'bundle' => 'page',
|
Chris@0
|
83 ];
|
Chris@0
|
84
|
Chris@0
|
85 return [
|
Chris@0
|
86 'site URL' => $base_test_case + [
|
Chris@0
|
87 'context' => [],
|
Chris@0
|
88 'link_domain' => NULL,
|
Chris@0
|
89 'expected return' => 'BASE_URL/rest/type/node/page',
|
Chris@0
|
90 'expected context' => [],
|
Chris@0
|
91 ],
|
Chris@14
|
92 'site URL, with optional context to collect cacheability metadata' => $base_test_case + [
|
Chris@14
|
93 'context' => $serialization_context_collecting_cacheability,
|
Chris@14
|
94 'expected return' => 'BASE_URL/rest/type/node/page',
|
Chris@14
|
95 'expected context' => $expected_serialization_context_cacheability_url_site,
|
Chris@14
|
96 ],
|
Chris@0
|
97 // Test hook_hal_type_uri_alter().
|
Chris@0
|
98 'site URL, with optional context, to test hook_hal_type_uri_alter()' => $base_test_case + [
|
Chris@0
|
99 'context' => ['hal_test' => TRUE],
|
Chris@0
|
100 'expected return' => 'hal_test_type',
|
Chris@0
|
101 'expected context' => ['hal_test' => TRUE],
|
Chris@0
|
102 ],
|
Chris@14
|
103 'site URL, with optional context, to test hook_hal_type_uri_alter(), and collecting cacheability metadata' => $base_test_case + [
|
Chris@14
|
104 'context' => ['hal_test' => TRUE] + $serialization_context_collecting_cacheability,
|
Chris@14
|
105 'expected return' => 'hal_test_type',
|
Chris@14
|
106 // No cacheability metadata bubbled.
|
Chris@14
|
107 'expected context' => ['hal_test' => TRUE] + $serialization_context_collecting_cacheability,
|
Chris@14
|
108 ],
|
Chris@0
|
109 // Test hook_rest_type_uri_alter() — for backwards compatibility.
|
Chris@0
|
110 'site URL, with optional context, to test hook_rest_type_uri_alter()' => $base_test_case + [
|
Chris@0
|
111 'context' => ['rest_test' => TRUE],
|
Chris@0
|
112 'expected return' => 'rest_test_type',
|
Chris@0
|
113 'expected context' => ['rest_test' => TRUE],
|
Chris@0
|
114 ],
|
Chris@14
|
115 'site URL, with optional context, to test hook_rest_type_uri_alter(), and collecting cacheability metadata' => $base_test_case + [
|
Chris@14
|
116 'context' => ['rest_test' => TRUE] + $serialization_context_collecting_cacheability,
|
Chris@14
|
117 'expected return' => 'rest_test_type',
|
Chris@14
|
118 // No cacheability metadata bubbled.
|
Chris@14
|
119 'expected context' => ['rest_test' => TRUE] + $serialization_context_collecting_cacheability,
|
Chris@14
|
120 ],
|
Chris@0
|
121 'configured URL' => [
|
Chris@0
|
122 'link_domain' => 'http://llamas-rock.com/for-real/',
|
Chris@0
|
123 'entity_type' => 'node',
|
Chris@0
|
124 'bundle' => 'page',
|
Chris@0
|
125 'context' => [],
|
Chris@0
|
126 'expected return' => 'http://llamas-rock.com/for-real/rest/type/node/page',
|
Chris@0
|
127 'expected context' => [],
|
Chris@0
|
128 ],
|
Chris@14
|
129 'configured URL, with optional context to collect cacheability metadata' => [
|
Chris@14
|
130 'link_domain' => 'http://llamas-rock.com/for-real/',
|
Chris@14
|
131 'entity_type' => 'node',
|
Chris@14
|
132 'bundle' => 'page',
|
Chris@14
|
133 'context' => $serialization_context_collecting_cacheability,
|
Chris@14
|
134 'expected return' => 'http://llamas-rock.com/for-real/rest/type/node/page',
|
Chris@14
|
135 'expected context' => [
|
Chris@14
|
136 CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => (new CacheableMetadata())->setCacheTags(['config:hal.settings']),
|
Chris@14
|
137 ],
|
Chris@14
|
138 ],
|
Chris@0
|
139 ];
|
Chris@0
|
140 }
|
Chris@0
|
141
|
Chris@0
|
142 /**
|
Chris@0
|
143 * @covers ::getRelationUri
|
Chris@0
|
144 * @dataProvider providerTestGetRelationUri
|
Chris@0
|
145 */
|
Chris@0
|
146 public function testGetRelationUri($link_domain, $entity_type, $bundle, $field_name, array $context, $expected_return, array $expected_context) {
|
Chris@0
|
147 $hal_settings = \Drupal::configFactory()->getEditable('hal.settings');
|
Chris@0
|
148
|
Chris@0
|
149 if ($link_domain === NULL) {
|
Chris@0
|
150 $hal_settings->clear('link_domain');
|
Chris@0
|
151 }
|
Chris@0
|
152 else {
|
Chris@0
|
153 $hal_settings->set('link_domain', $link_domain)->save(TRUE);
|
Chris@0
|
154 }
|
Chris@0
|
155
|
Chris@0
|
156 /* @var \Drupal\rest\LinkManager\RelationLinkManagerInterface $relation_manager */
|
Chris@0
|
157 $relation_manager = \Drupal::service('hal.link_manager.relation');
|
Chris@0
|
158
|
Chris@0
|
159 $link = $relation_manager->getRelationUri($entity_type, $bundle, $field_name, $context);
|
Chris@0
|
160 $this->assertSame($link, str_replace('BASE_URL/', Url::fromRoute('<front>', [], ['absolute' => TRUE])->toString(), $expected_return));
|
Chris@0
|
161 $this->assertEquals($context, $expected_context);
|
Chris@0
|
162 }
|
Chris@0
|
163
|
Chris@0
|
164 public function providerTestGetRelationUri() {
|
Chris@14
|
165 $serialization_context_collecting_cacheability = [
|
Chris@14
|
166 CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => new CacheableMetadata()
|
Chris@14
|
167 ];
|
Chris@14
|
168 $expected_serialization_context_cacheability_url_site = [
|
Chris@14
|
169 CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => (new CacheableMetadata())->setCacheContexts(['url.site'])
|
Chris@14
|
170 ];
|
Chris@14
|
171
|
Chris@0
|
172 $field_name = $this->randomMachineName();
|
Chris@0
|
173 $base_test_case = [
|
Chris@0
|
174 'link_domain' => NULL,
|
Chris@0
|
175 'entity_type' => 'node',
|
Chris@0
|
176 'bundle' => 'page',
|
Chris@0
|
177 'field_name' => $field_name,
|
Chris@0
|
178 ];
|
Chris@0
|
179
|
Chris@0
|
180 return [
|
Chris@0
|
181 'site URL' => $base_test_case + [
|
Chris@0
|
182 'context' => [],
|
Chris@0
|
183 'link_domain' => NULL,
|
Chris@0
|
184 'expected return' => 'BASE_URL/rest/relation/node/page/' . $field_name,
|
Chris@0
|
185 'expected context' => [],
|
Chris@0
|
186 ],
|
Chris@14
|
187 'site URL, with optional context to collect cacheability metadata' => $base_test_case + [
|
Chris@14
|
188 'context' => $serialization_context_collecting_cacheability,
|
Chris@14
|
189 'expected return' => 'BASE_URL/rest/relation/node/page/' . $field_name,
|
Chris@14
|
190 'expected context' => $expected_serialization_context_cacheability_url_site,
|
Chris@14
|
191 ],
|
Chris@0
|
192 // Test hook_hal_relation_uri_alter().
|
Chris@0
|
193 'site URL, with optional context, to test hook_hal_relation_uri_alter()' => $base_test_case + [
|
Chris@0
|
194 'context' => ['hal_test' => TRUE],
|
Chris@0
|
195 'expected return' => 'hal_test_relation',
|
Chris@0
|
196 'expected context' => ['hal_test' => TRUE],
|
Chris@0
|
197 ],
|
Chris@14
|
198 'site URL, with optional context, to test hook_hal_relation_uri_alter(), and collecting cacheability metadata' => $base_test_case + [
|
Chris@14
|
199 'context' => ['hal_test' => TRUE] + $serialization_context_collecting_cacheability,
|
Chris@14
|
200 'expected return' => 'hal_test_relation',
|
Chris@14
|
201 // No cacheability metadata bubbled.
|
Chris@14
|
202 'expected context' => ['hal_test' => TRUE] + $serialization_context_collecting_cacheability,
|
Chris@14
|
203 ],
|
Chris@0
|
204 // Test hook_rest_relation_uri_alter() — for backwards compatibility.
|
Chris@0
|
205 'site URL, with optional context, to test hook_rest_relation_uri_alter()' => $base_test_case + [
|
Chris@0
|
206 'context' => ['rest_test' => TRUE],
|
Chris@0
|
207 'expected return' => 'rest_test_relation',
|
Chris@0
|
208 'expected context' => ['rest_test' => TRUE],
|
Chris@0
|
209 ],
|
Chris@14
|
210 'site URL, with optional context, to test hook_rest_relation_uri_alter(), and collecting cacheability metadata' => $base_test_case + [
|
Chris@14
|
211 'context' => ['rest_test' => TRUE] + $serialization_context_collecting_cacheability,
|
Chris@14
|
212 'expected return' => 'rest_test_relation',
|
Chris@14
|
213 // No cacheability metadata bubbled.
|
Chris@14
|
214 'expected context' => ['rest_test' => TRUE] + $serialization_context_collecting_cacheability,
|
Chris@14
|
215 ],
|
Chris@0
|
216 'configured URL' => [
|
Chris@0
|
217 'link_domain' => 'http://llamas-rock.com/for-real/',
|
Chris@0
|
218 'entity_type' => 'node',
|
Chris@0
|
219 'bundle' => 'page',
|
Chris@0
|
220 'field_name' => $field_name,
|
Chris@0
|
221 'context' => [],
|
Chris@0
|
222 'expected return' => 'http://llamas-rock.com/for-real/rest/relation/node/page/' . $field_name,
|
Chris@0
|
223 'expected context' => [],
|
Chris@0
|
224 ],
|
Chris@14
|
225 'configured URL, with optional context to collect cacheability metadata' => [
|
Chris@14
|
226 'link_domain' => 'http://llamas-rock.com/for-real/',
|
Chris@14
|
227 'entity_type' => 'node',
|
Chris@14
|
228 'bundle' => 'page',
|
Chris@14
|
229 'field_name' => $field_name,
|
Chris@14
|
230 'context' => $serialization_context_collecting_cacheability,
|
Chris@14
|
231 'expected return' => 'http://llamas-rock.com/for-real/rest/relation/node/page/' . $field_name,
|
Chris@14
|
232 'expected context' => [
|
Chris@14
|
233 CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => (new CacheableMetadata())->setCacheTags(['config:hal.settings']),
|
Chris@14
|
234 ],
|
Chris@14
|
235 ],
|
Chris@0
|
236 ];
|
Chris@0
|
237 }
|
Chris@0
|
238
|
Chris@0
|
239 /**
|
Chris@0
|
240 * @covers ::getRelationInternalIds
|
Chris@0
|
241 */
|
Chris@0
|
242 public function testGetRelationInternalIds() {
|
Chris@0
|
243 /* @var \Drupal\rest\LinkManager\RelationLinkManagerInterface $relation_manager */
|
Chris@0
|
244 $relation_manager = \Drupal::service('hal.link_manager.relation');
|
Chris@0
|
245 $link = $relation_manager->getRelationUri('node', 'page', 'field_ref');
|
Chris@0
|
246 $internal_ids = $relation_manager->getRelationInternalIds($link);
|
Chris@0
|
247
|
Chris@0
|
248 $this->assertEquals([
|
Chris@0
|
249 'entity_type_id' => 'node',
|
Chris@0
|
250 'entity_type' => \Drupal::entityTypeManager()->getDefinition('node'),
|
Chris@0
|
251 'bundle' => 'page',
|
Chris@0
|
252 'field_name' => 'field_ref'
|
Chris@0
|
253 ], $internal_ids);
|
Chris@0
|
254 }
|
Chris@0
|
255
|
Chris@0
|
256 /**
|
Chris@0
|
257 * @covers ::setLinkDomain
|
Chris@0
|
258 */
|
Chris@0
|
259 public function testHalLinkManagersSetLinkDomain() {
|
Chris@14
|
260 $serialization_context = [
|
Chris@14
|
261 CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => new CacheableMetadata()
|
Chris@14
|
262 ];
|
Chris@14
|
263
|
Chris@0
|
264 /* @var \Drupal\rest\LinkManager\LinkManager $link_manager */
|
Chris@0
|
265 $link_manager = \Drupal::service('hal.link_manager');
|
Chris@0
|
266 $link_manager->setLinkDomain('http://example.com/');
|
Chris@14
|
267 $link = $link_manager->getTypeUri('node', 'page', $serialization_context);
|
Chris@0
|
268 $this->assertEqual($link, 'http://example.com/rest/type/node/page');
|
Chris@14
|
269 $this->assertEqual(new CacheableMetadata(), $serialization_context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]);
|
Chris@14
|
270 $link = $link_manager->getRelationUri('node', 'page', 'field_ref', $serialization_context);
|
Chris@0
|
271 $this->assertEqual($link, 'http://example.com/rest/relation/node/page/field_ref');
|
Chris@14
|
272 $this->assertEqual(new CacheableMetadata(), $serialization_context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]);
|
Chris@0
|
273 }
|
Chris@0
|
274
|
Chris@0
|
275 }
|