05a6b39f1ee78deffbb0372b17b8cae61c8be11a
[yaffs-website] / web / core / modules / hal / tests / src / Kernel / HalLinkManagerTest.php
1 <?php
2
3 namespace Drupal\Tests\hal\Kernel;
4
5 use Drupal\Core\Cache\CacheableMetadata;
6 use Drupal\Core\Url;
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;
12
13 /**
14  * @coversDefaultClass \Drupal\hal\LinkManager\LinkManager
15  * @group hal
16  * @group legacy
17  */
18 class HalLinkManagerTest extends KernelTestBase {
19
20   /**
21    * {@inheritdoc}
22    */
23   public static $modules = ['hal', 'hal_test', 'serialization', 'system', 'node', 'user', 'field'];
24
25   /**
26    * {@inheritdoc}
27    */
28   protected function setUp() {
29     parent::setUp();
30
31     $this->installEntitySchema('node');
32
33     NodeType::create([
34       'type' => 'page',
35     ])->save();
36     FieldStorageConfig::create([
37       'entity_type' => 'node',
38       'type' => 'entity_reference',
39       'field_name' => 'field_ref',
40     ])->save();
41     FieldConfig::create([
42       'entity_type' => 'node',
43       'bundle' => 'page',
44       'field_name' => 'field_ref',
45     ])->save();
46
47     \Drupal::service('router.builder')->rebuild();
48   }
49
50   /**
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.
54    */
55   public function testGetTypeUri($link_domain, $entity_type, $bundle, array $context, $expected_return, array $expected_context) {
56     $hal_settings = \Drupal::configFactory()->getEditable('hal.settings');
57
58     if ($link_domain === NULL) {
59       $hal_settings->clear('link_domain');
60     }
61     else {
62       $hal_settings->set('link_domain', $link_domain)->save(TRUE);
63     }
64
65     /* @var \Drupal\rest\LinkManager\TypeLinkManagerInterface $type_manager */
66     $type_manager = \Drupal::service('hal.link_manager.type');
67
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);
71   }
72
73   public function providerTestGetTypeUri() {
74     $serialization_context_collecting_cacheability = [
75       CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => new CacheableMetadata(),
76     ];
77     $expected_serialization_context_cacheability_url_site = [
78       CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => (new CacheableMetadata())->setCacheContexts(['url.site']),
79     ];
80
81     $base_test_case = [
82       'link_domain' => NULL,
83       'entity_type' => 'node',
84       'bundle' => 'page',
85     ];
86
87     return [
88       'site URL' => $base_test_case + [
89         'context' => [],
90         'link_domain' => NULL,
91         'expected return' => 'BASE_URL/rest/type/node/page',
92         'expected context' => [],
93       ],
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,
98       ],
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],
104       ],
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,
110       ],
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],
116       ],
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,
122       ],
123       'configured URL' => [
124         'link_domain' => 'http://llamas-rock.com/for-real/',
125         'entity_type' => 'node',
126         'bundle' => 'page',
127         'context' => [],
128         'expected return' => 'http://llamas-rock.com/for-real/rest/type/node/page',
129         'expected context' => [],
130       ],
131       'configured URL, with optional context to collect cacheability metadata' => [
132         'link_domain' => 'http://llamas-rock.com/for-real/',
133         'entity_type' => 'node',
134         'bundle' => 'page',
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']),
139         ],
140       ],
141     ];
142   }
143
144   /**
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.
148    */
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');
151
152     if ($link_domain === NULL) {
153       $hal_settings->clear('link_domain');
154     }
155     else {
156       $hal_settings->set('link_domain', $link_domain)->save(TRUE);
157     }
158
159     /* @var \Drupal\rest\LinkManager\RelationLinkManagerInterface $relation_manager */
160     $relation_manager = \Drupal::service('hal.link_manager.relation');
161
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);
165   }
166
167   public function providerTestGetRelationUri() {
168     $serialization_context_collecting_cacheability = [
169       CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => new CacheableMetadata(),
170     ];
171     $expected_serialization_context_cacheability_url_site = [
172       CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => (new CacheableMetadata())->setCacheContexts(['url.site']),
173     ];
174
175     $field_name = $this->randomMachineName();
176     $base_test_case = [
177       'link_domain' => NULL,
178       'entity_type' => 'node',
179       'bundle' => 'page',
180       'field_name' => $field_name,
181     ];
182
183     return [
184       'site URL' => $base_test_case + [
185         'context' => [],
186         'link_domain' => NULL,
187         'expected return' => 'BASE_URL/rest/relation/node/page/' . $field_name,
188         'expected context' => [],
189       ],
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,
194       ],
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],
200       ],
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,
206       ],
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],
212       ],
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,
218       ],
219       'configured URL' => [
220         'link_domain' => 'http://llamas-rock.com/for-real/',
221         'entity_type' => 'node',
222         'bundle' => 'page',
223         'field_name' => $field_name,
224         'context' => [],
225         'expected return' => 'http://llamas-rock.com/for-real/rest/relation/node/page/' . $field_name,
226         'expected context' => [],
227       ],
228       'configured URL, with optional context to collect cacheability metadata' => [
229         'link_domain' => 'http://llamas-rock.com/for-real/',
230         'entity_type' => 'node',
231         'bundle' => 'page',
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']),
237         ],
238       ],
239     ];
240   }
241
242   /**
243    * @covers ::getRelationInternalIds
244    */
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);
250
251     $this->assertEquals([
252       'entity_type_id' => 'node',
253       'entity_type' => \Drupal::entityTypeManager()->getDefinition('node'),
254       'bundle' => 'page',
255       'field_name' => 'field_ref',
256     ], $internal_ids);
257   }
258
259   /**
260    * @covers ::setLinkDomain
261    */
262   public function testHalLinkManagersSetLinkDomain() {
263     $serialization_context = [
264       CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => new CacheableMetadata(),
265     ];
266
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]);
276   }
277
278 }