annotate core/modules/hal/tests/src/Kernel/HalLinkManagerTest.php @ 19:fa3358dc1485 tip

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