comparison core/modules/hal/tests/src/Kernel/HalLinkManagerTest.php @ 0:4c8ae668cc8c

Initial import (non-working)
author Chris Cannam
date Wed, 29 Nov 2017 16:09:58 +0000
parents
children 1fec387a4317
comparison
equal deleted inserted replaced
-1:000000000000 0:4c8ae668cc8c
1 <?php
2
3 namespace Drupal\Tests\hal\Kernel;
4
5 use Drupal\Core\Url;
6 use Drupal\field\Entity\FieldConfig;
7 use Drupal\field\Entity\FieldStorageConfig;
8 use Drupal\KernelTests\KernelTestBase;
9 use Drupal\node\Entity\NodeType;
10
11 /**
12 * @coversDefaultClass \Drupal\hal\LinkManager\LinkManager
13 * @group hal
14 */
15 class HalLinkManagerTest extends KernelTestBase {
16
17 /**
18 * {@inheritdoc}
19 */
20 public static $modules = ['hal', 'hal_test', 'serialization', 'system', 'node', 'user', 'field'];
21
22 /**
23 * {@inheritdoc}
24 */
25 protected function setUp() {
26 parent::setUp();
27
28 $this->installEntitySchema('node');
29
30 NodeType::create([
31 'type' => 'page',
32 ])->save();
33 FieldStorageConfig::create([
34 'entity_type' => 'node',
35 'type' => 'entity_reference',
36 'field_name' => 'field_ref',
37 ])->save();
38 FieldConfig::create([
39 'entity_type' => 'node',
40 'bundle' => 'page',
41 'field_name' => 'field_ref',
42 ])->save();
43
44 \Drupal::service('router.builder')->rebuild();
45 }
46
47 /**
48 * @covers ::getTypeUri
49 * @dataProvider providerTestGetTypeUri
50 */
51 public function testGetTypeUri($link_domain, $entity_type, $bundle, array $context, $expected_return, array $expected_context) {
52 $hal_settings = \Drupal::configFactory()->getEditable('hal.settings');
53
54 if ($link_domain === NULL) {
55 $hal_settings->clear('link_domain');
56 }
57 else {
58 $hal_settings->set('link_domain', $link_domain)->save(TRUE);
59 }
60
61 /* @var \Drupal\rest\LinkManager\TypeLinkManagerInterface $type_manager */
62 $type_manager = \Drupal::service('hal.link_manager.type');
63
64 $link = $type_manager->getTypeUri($entity_type, $bundle, $context);
65 $this->assertSame($link, str_replace('BASE_URL/', Url::fromRoute('<front>', [], ['absolute' => TRUE])->toString(), $expected_return));
66 $this->assertEquals($context, $expected_context);
67 }
68
69 public function providerTestGetTypeUri() {
70 $base_test_case = [
71 'link_domain' => NULL,
72 'entity_type' => 'node',
73 'bundle' => 'page',
74 ];
75
76 return [
77 'site URL' => $base_test_case + [
78 'context' => [],
79 'link_domain' => NULL,
80 'expected return' => 'BASE_URL/rest/type/node/page',
81 'expected context' => [],
82 ],
83 // Test hook_hal_type_uri_alter().
84 'site URL, with optional context, to test hook_hal_type_uri_alter()' => $base_test_case + [
85 'context' => ['hal_test' => TRUE],
86 'expected return' => 'hal_test_type',
87 'expected context' => ['hal_test' => TRUE],
88 ],
89 // 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 + [
91 'context' => ['rest_test' => TRUE],
92 'expected return' => 'rest_test_type',
93 'expected context' => ['rest_test' => TRUE],
94 ],
95 'configured URL' => [
96 'link_domain' => 'http://llamas-rock.com/for-real/',
97 'entity_type' => 'node',
98 'bundle' => 'page',
99 'context' => [],
100 'expected return' => 'http://llamas-rock.com/for-real/rest/type/node/page',
101 'expected context' => [],
102 ],
103 ];
104 }
105
106 /**
107 * @covers ::getRelationUri
108 * @dataProvider providerTestGetRelationUri
109 */
110 public function testGetRelationUri($link_domain, $entity_type, $bundle, $field_name, array $context, $expected_return, array $expected_context) {
111 $hal_settings = \Drupal::configFactory()->getEditable('hal.settings');
112
113 if ($link_domain === NULL) {
114 $hal_settings->clear('link_domain');
115 }
116 else {
117 $hal_settings->set('link_domain', $link_domain)->save(TRUE);
118 }
119
120 /* @var \Drupal\rest\LinkManager\RelationLinkManagerInterface $relation_manager */
121 $relation_manager = \Drupal::service('hal.link_manager.relation');
122
123 $link = $relation_manager->getRelationUri($entity_type, $bundle, $field_name, $context);
124 $this->assertSame($link, str_replace('BASE_URL/', Url::fromRoute('<front>', [], ['absolute' => TRUE])->toString(), $expected_return));
125 $this->assertEquals($context, $expected_context);
126 }
127
128 public function providerTestGetRelationUri() {
129 $field_name = $this->randomMachineName();
130 $base_test_case = [
131 'link_domain' => NULL,
132 'entity_type' => 'node',
133 'bundle' => 'page',
134 'field_name' => $field_name,
135 ];
136
137 return [
138 'site URL' => $base_test_case + [
139 'context' => [],
140 'link_domain' => NULL,
141 'expected return' => 'BASE_URL/rest/relation/node/page/' . $field_name,
142 'expected context' => [],
143 ],
144 // Test hook_hal_relation_uri_alter().
145 'site URL, with optional context, to test hook_hal_relation_uri_alter()' => $base_test_case + [
146 'context' => ['hal_test' => TRUE],
147 'expected return' => 'hal_test_relation',
148 'expected context' => ['hal_test' => TRUE],
149 ],
150 // 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 + [
152 'context' => ['rest_test' => TRUE],
153 'expected return' => 'rest_test_relation',
154 'expected context' => ['rest_test' => TRUE],
155 ],
156 'configured URL' => [
157 'link_domain' => 'http://llamas-rock.com/for-real/',
158 'entity_type' => 'node',
159 'bundle' => 'page',
160 'field_name' => $field_name,
161 'context' => [],
162 'expected return' => 'http://llamas-rock.com/for-real/rest/relation/node/page/' . $field_name,
163 'expected context' => [],
164 ],
165 ];
166 }
167
168 /**
169 * @covers ::getRelationInternalIds
170 */
171 public function testGetRelationInternalIds() {
172 /* @var \Drupal\rest\LinkManager\RelationLinkManagerInterface $relation_manager */
173 $relation_manager = \Drupal::service('hal.link_manager.relation');
174 $link = $relation_manager->getRelationUri('node', 'page', 'field_ref');
175 $internal_ids = $relation_manager->getRelationInternalIds($link);
176
177 $this->assertEquals([
178 'entity_type_id' => 'node',
179 'entity_type' => \Drupal::entityTypeManager()->getDefinition('node'),
180 'bundle' => 'page',
181 'field_name' => 'field_ref'
182 ], $internal_ids);
183 }
184
185 /**
186 * @covers ::setLinkDomain
187 */
188 public function testHalLinkManagersSetLinkDomain() {
189 /* @var \Drupal\rest\LinkManager\LinkManager $link_manager */
190 $link_manager = \Drupal::service('hal.link_manager');
191 $link_manager->setLinkDomain('http://example.com/');
192 $link = $link_manager->getTypeUri('node', 'page');
193 $this->assertEqual($link, 'http://example.com/rest/type/node/page');
194 $link = $link_manager->getRelationUri('node', 'page', 'field_ref');
195 $this->assertEqual($link, 'http://example.com/rest/relation/node/page/field_ref');
196 }
197
198 }