comparison core/modules/hal/tests/src/Kernel/HalLinkManagerTest.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 4c8ae668cc8c
children 129ea1e6d783
comparison
equal deleted inserted replaced
13:5fb285c0d0e3 14:1fec387a4317
1 <?php 1 <?php
2 2
3 namespace Drupal\Tests\hal\Kernel; 3 namespace Drupal\Tests\hal\Kernel;
4 4
5 use Drupal\Core\Cache\CacheableMetadata;
5 use Drupal\Core\Url; 6 use Drupal\Core\Url;
6 use Drupal\field\Entity\FieldConfig; 7 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig; 8 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\KernelTests\KernelTestBase; 9 use Drupal\KernelTests\KernelTestBase;
9 use Drupal\node\Entity\NodeType; 10 use Drupal\node\Entity\NodeType;
11 use Drupal\serialization\Normalizer\CacheableNormalizerInterface;
10 12
11 /** 13 /**
12 * @coversDefaultClass \Drupal\hal\LinkManager\LinkManager 14 * @coversDefaultClass \Drupal\hal\LinkManager\LinkManager
13 * @group hal 15 * @group hal
14 */ 16 */
65 $this->assertSame($link, str_replace('BASE_URL/', Url::fromRoute('<front>', [], ['absolute' => TRUE])->toString(), $expected_return)); 67 $this->assertSame($link, str_replace('BASE_URL/', Url::fromRoute('<front>', [], ['absolute' => TRUE])->toString(), $expected_return));
66 $this->assertEquals($context, $expected_context); 68 $this->assertEquals($context, $expected_context);
67 } 69 }
68 70
69 public function providerTestGetTypeUri() { 71 public function providerTestGetTypeUri() {
72 $serialization_context_collecting_cacheability = [
73 CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => new CacheableMetadata()
74 ];
75 $expected_serialization_context_cacheability_url_site = [
76 CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => (new CacheableMetadata())->setCacheContexts(['url.site'])
77 ];
78
70 $base_test_case = [ 79 $base_test_case = [
71 'link_domain' => NULL, 80 'link_domain' => NULL,
72 'entity_type' => 'node', 81 'entity_type' => 'node',
73 'bundle' => 'page', 82 'bundle' => 'page',
74 ]; 83 ];
77 'site URL' => $base_test_case + [ 86 'site URL' => $base_test_case + [
78 'context' => [], 87 'context' => [],
79 'link_domain' => NULL, 88 'link_domain' => NULL,
80 'expected return' => 'BASE_URL/rest/type/node/page', 89 'expected return' => 'BASE_URL/rest/type/node/page',
81 'expected context' => [], 90 'expected context' => [],
91 ],
92 'site URL, with optional context to collect cacheability metadata' => $base_test_case + [
93 'context' => $serialization_context_collecting_cacheability,
94 'expected return' => 'BASE_URL/rest/type/node/page',
95 'expected context' => $expected_serialization_context_cacheability_url_site,
82 ], 96 ],
83 // Test hook_hal_type_uri_alter(). 97 // Test hook_hal_type_uri_alter().
84 'site URL, with optional context, to test hook_hal_type_uri_alter()' => $base_test_case + [ 98 'site URL, with optional context, to test hook_hal_type_uri_alter()' => $base_test_case + [
85 'context' => ['hal_test' => TRUE], 99 'context' => ['hal_test' => TRUE],
86 'expected return' => 'hal_test_type', 100 'expected return' => 'hal_test_type',
87 'expected context' => ['hal_test' => TRUE], 101 'expected context' => ['hal_test' => TRUE],
88 ], 102 ],
103 'site URL, with optional context, to test hook_hal_type_uri_alter(), and collecting cacheability metadata' => $base_test_case + [
104 'context' => ['hal_test' => TRUE] + $serialization_context_collecting_cacheability,
105 'expected return' => 'hal_test_type',
106 // No cacheability metadata bubbled.
107 'expected context' => ['hal_test' => TRUE] + $serialization_context_collecting_cacheability,
108 ],
89 // Test hook_rest_type_uri_alter() — for backwards compatibility. 109 // Test hook_rest_type_uri_alter() — for backwards compatibility.
90 'site URL, with optional context, to test hook_rest_type_uri_alter()' => $base_test_case + [ 110 'site URL, with optional context, to test hook_rest_type_uri_alter()' => $base_test_case + [
91 'context' => ['rest_test' => TRUE], 111 'context' => ['rest_test' => TRUE],
92 'expected return' => 'rest_test_type', 112 'expected return' => 'rest_test_type',
93 'expected context' => ['rest_test' => TRUE], 113 'expected context' => ['rest_test' => TRUE],
94 ], 114 ],
115 'site URL, with optional context, to test hook_rest_type_uri_alter(), and collecting cacheability metadata' => $base_test_case + [
116 'context' => ['rest_test' => TRUE] + $serialization_context_collecting_cacheability,
117 'expected return' => 'rest_test_type',
118 // No cacheability metadata bubbled.
119 'expected context' => ['rest_test' => TRUE] + $serialization_context_collecting_cacheability,
120 ],
95 'configured URL' => [ 121 'configured URL' => [
96 'link_domain' => 'http://llamas-rock.com/for-real/', 122 'link_domain' => 'http://llamas-rock.com/for-real/',
97 'entity_type' => 'node', 123 'entity_type' => 'node',
98 'bundle' => 'page', 124 'bundle' => 'page',
99 'context' => [], 125 'context' => [],
100 'expected return' => 'http://llamas-rock.com/for-real/rest/type/node/page', 126 'expected return' => 'http://llamas-rock.com/for-real/rest/type/node/page',
101 'expected context' => [], 127 'expected context' => [],
128 ],
129 'configured URL, with optional context to collect cacheability metadata' => [
130 'link_domain' => 'http://llamas-rock.com/for-real/',
131 'entity_type' => 'node',
132 'bundle' => 'page',
133 'context' => $serialization_context_collecting_cacheability,
134 'expected return' => 'http://llamas-rock.com/for-real/rest/type/node/page',
135 'expected context' => [
136 CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => (new CacheableMetadata())->setCacheTags(['config:hal.settings']),
137 ],
102 ], 138 ],
103 ]; 139 ];
104 } 140 }
105 141
106 /** 142 /**
124 $this->assertSame($link, str_replace('BASE_URL/', Url::fromRoute('<front>', [], ['absolute' => TRUE])->toString(), $expected_return)); 160 $this->assertSame($link, str_replace('BASE_URL/', Url::fromRoute('<front>', [], ['absolute' => TRUE])->toString(), $expected_return));
125 $this->assertEquals($context, $expected_context); 161 $this->assertEquals($context, $expected_context);
126 } 162 }
127 163
128 public function providerTestGetRelationUri() { 164 public function providerTestGetRelationUri() {
165 $serialization_context_collecting_cacheability = [
166 CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => new CacheableMetadata()
167 ];
168 $expected_serialization_context_cacheability_url_site = [
169 CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => (new CacheableMetadata())->setCacheContexts(['url.site'])
170 ];
171
129 $field_name = $this->randomMachineName(); 172 $field_name = $this->randomMachineName();
130 $base_test_case = [ 173 $base_test_case = [
131 'link_domain' => NULL, 174 'link_domain' => NULL,
132 'entity_type' => 'node', 175 'entity_type' => 'node',
133 'bundle' => 'page', 176 'bundle' => 'page',
138 'site URL' => $base_test_case + [ 181 'site URL' => $base_test_case + [
139 'context' => [], 182 'context' => [],
140 'link_domain' => NULL, 183 'link_domain' => NULL,
141 'expected return' => 'BASE_URL/rest/relation/node/page/' . $field_name, 184 'expected return' => 'BASE_URL/rest/relation/node/page/' . $field_name,
142 'expected context' => [], 185 'expected context' => [],
186 ],
187 'site URL, with optional context to collect cacheability metadata' => $base_test_case + [
188 'context' => $serialization_context_collecting_cacheability,
189 'expected return' => 'BASE_URL/rest/relation/node/page/' . $field_name,
190 'expected context' => $expected_serialization_context_cacheability_url_site,
143 ], 191 ],
144 // Test hook_hal_relation_uri_alter(). 192 // Test hook_hal_relation_uri_alter().
145 'site URL, with optional context, to test hook_hal_relation_uri_alter()' => $base_test_case + [ 193 'site URL, with optional context, to test hook_hal_relation_uri_alter()' => $base_test_case + [
146 'context' => ['hal_test' => TRUE], 194 'context' => ['hal_test' => TRUE],
147 'expected return' => 'hal_test_relation', 195 'expected return' => 'hal_test_relation',
148 'expected context' => ['hal_test' => TRUE], 196 'expected context' => ['hal_test' => TRUE],
149 ], 197 ],
198 'site URL, with optional context, to test hook_hal_relation_uri_alter(), and collecting cacheability metadata' => $base_test_case + [
199 'context' => ['hal_test' => TRUE] + $serialization_context_collecting_cacheability,
200 'expected return' => 'hal_test_relation',
201 // No cacheability metadata bubbled.
202 'expected context' => ['hal_test' => TRUE] + $serialization_context_collecting_cacheability,
203 ],
150 // Test hook_rest_relation_uri_alter() — for backwards compatibility. 204 // Test hook_rest_relation_uri_alter() — for backwards compatibility.
151 'site URL, with optional context, to test hook_rest_relation_uri_alter()' => $base_test_case + [ 205 'site URL, with optional context, to test hook_rest_relation_uri_alter()' => $base_test_case + [
152 'context' => ['rest_test' => TRUE], 206 'context' => ['rest_test' => TRUE],
153 'expected return' => 'rest_test_relation', 207 'expected return' => 'rest_test_relation',
154 'expected context' => ['rest_test' => TRUE], 208 'expected context' => ['rest_test' => TRUE],
155 ], 209 ],
210 'site URL, with optional context, to test hook_rest_relation_uri_alter(), and collecting cacheability metadata' => $base_test_case + [
211 'context' => ['rest_test' => TRUE] + $serialization_context_collecting_cacheability,
212 'expected return' => 'rest_test_relation',
213 // No cacheability metadata bubbled.
214 'expected context' => ['rest_test' => TRUE] + $serialization_context_collecting_cacheability,
215 ],
156 'configured URL' => [ 216 'configured URL' => [
157 'link_domain' => 'http://llamas-rock.com/for-real/', 217 'link_domain' => 'http://llamas-rock.com/for-real/',
158 'entity_type' => 'node', 218 'entity_type' => 'node',
159 'bundle' => 'page', 219 'bundle' => 'page',
160 'field_name' => $field_name, 220 'field_name' => $field_name,
161 'context' => [], 221 'context' => [],
162 'expected return' => 'http://llamas-rock.com/for-real/rest/relation/node/page/' . $field_name, 222 'expected return' => 'http://llamas-rock.com/for-real/rest/relation/node/page/' . $field_name,
163 'expected context' => [], 223 'expected context' => [],
224 ],
225 'configured URL, with optional context to collect cacheability metadata' => [
226 'link_domain' => 'http://llamas-rock.com/for-real/',
227 'entity_type' => 'node',
228 'bundle' => 'page',
229 'field_name' => $field_name,
230 'context' => $serialization_context_collecting_cacheability,
231 'expected return' => 'http://llamas-rock.com/for-real/rest/relation/node/page/' . $field_name,
232 'expected context' => [
233 CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => (new CacheableMetadata())->setCacheTags(['config:hal.settings']),
234 ],
164 ], 235 ],
165 ]; 236 ];
166 } 237 }
167 238
168 /** 239 /**
184 255
185 /** 256 /**
186 * @covers ::setLinkDomain 257 * @covers ::setLinkDomain
187 */ 258 */
188 public function testHalLinkManagersSetLinkDomain() { 259 public function testHalLinkManagersSetLinkDomain() {
260 $serialization_context = [
261 CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY => new CacheableMetadata()
262 ];
263
189 /* @var \Drupal\rest\LinkManager\LinkManager $link_manager */ 264 /* @var \Drupal\rest\LinkManager\LinkManager $link_manager */
190 $link_manager = \Drupal::service('hal.link_manager'); 265 $link_manager = \Drupal::service('hal.link_manager');
191 $link_manager->setLinkDomain('http://example.com/'); 266 $link_manager->setLinkDomain('http://example.com/');
192 $link = $link_manager->getTypeUri('node', 'page'); 267 $link = $link_manager->getTypeUri('node', 'page', $serialization_context);
193 $this->assertEqual($link, 'http://example.com/rest/type/node/page'); 268 $this->assertEqual($link, 'http://example.com/rest/type/node/page');
194 $link = $link_manager->getRelationUri('node', 'page', 'field_ref'); 269 $this->assertEqual(new CacheableMetadata(), $serialization_context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]);
270 $link = $link_manager->getRelationUri('node', 'page', 'field_ref', $serialization_context);
195 $this->assertEqual($link, 'http://example.com/rest/relation/node/page/field_ref'); 271 $this->assertEqual($link, 'http://example.com/rest/relation/node/page/field_ref');
272 $this->assertEqual(new CacheableMetadata(), $serialization_context[CacheableNormalizerInterface::SERIALIZATION_CONTEXT_CACHEABILITY]);
196 } 273 }
197 274
198 } 275 }