1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
|
<?php
declare(strict_types=1);
namespace Drupal\Tests\jsonapi\Functional;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Entity\EntityInterface;
use Drupal\entity_test\Entity\EntityTestBundle;
use Drupal\entity_test\Entity\EntityTestNoLabel;
use Drupal\entity_test\Entity\EntityTestWithBundle;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\field\Traits\EntityReferenceFieldCreationTrait;
/**
* Makes assertions about the JSON:API behavior for internal entities.
*
* @group jsonapi
*
* @internal
*/
class InternalEntitiesTest extends BrowserTestBase {
use EntityReferenceFieldCreationTrait;
/**
* {@inheritdoc}
*/
protected static $modules = [
'jsonapi',
'entity_test',
'serialization',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* A test user.
*
* @var \Drupal\user\UserInterface
*/
protected $testUser;
/**
* An entity of an internal entity type.
*
* @var \Drupal\Core\Entity\EntityInterface
*/
protected $internalEntity;
/**
* An entity referencing an internal entity.
*
* @var \Drupal\Core\Entity\EntityInterface
*/
protected $referencingEntity;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->testUser = $this->drupalCreateUser([
'view test entity',
'administer entity_test_with_bundle content',
], $this->randomString(), TRUE);
EntityTestBundle::create([
'id' => 'internal_referencer',
'label' => 'Entity Test Internal Referencer',
])->save();
$this->createEntityReferenceField(
'entity_test_with_bundle',
'internal_referencer',
'field_internal',
'Internal Entities',
'entity_test_no_label'
);
$this->internalEntity = EntityTestNoLabel::create([]);
$this->internalEntity->save();
$this->referencingEntity = EntityTestWithBundle::create([
'type' => 'internal_referencer',
'field_internal' => $this->internalEntity->id(),
]);
$this->referencingEntity->save();
\Drupal::service('router.builder')->rebuild();
}
/**
* Ensures that internal resources types aren't present in the entry point.
*/
public function testEntryPoint(): void {
$document = $this->jsonapiGet('/jsonapi');
$this->assertArrayNotHasKey(
"{$this->internalEntity->getEntityTypeId()}--{$this->internalEntity->bundle()}",
$document['links'],
'The entry point should not contain links to internal resource type routes.'
);
}
/**
* Ensures that internal resources types aren't present in the routes.
*/
public function testRoutes(): void {
// This cannot be in a data provider because it needs values created by the
// setUp method.
$paths = [
'individual' => "/jsonapi/entity_test_no_label/entity_test_no_label/{$this->internalEntity->uuid()}",
'collection' => "/jsonapi/entity_test_no_label/entity_test_no_label",
'related' => "/jsonapi/entity_test_no_label/entity_test_no_label/{$this->internalEntity->uuid()}/field_internal",
];
$this->drupalLogin($this->testUser);
foreach ($paths as $path) {
$this->drupalGet($path, ['Accept' => 'application/vnd.api+json']);
$this->assertSame(404, $this->getSession()->getStatusCode());
}
}
/**
* Asserts that internal entities are not included in compound documents.
*/
public function testIncludes(): void {
$document = $this->getIndividual($this->referencingEntity, [
'query' => ['include' => 'field_internal'],
]);
$this->assertArrayNotHasKey(
'included',
$document,
'Internal entities should not be included in compound documents.'
);
}
/**
* Asserts that links to internal relationships aren't generated.
*/
public function testLinks(): void {
$document = $this->getIndividual($this->referencingEntity);
$this->assertArrayNotHasKey(
'related',
$document['data']['relationships']['field_internal']['links'],
'Links to internal-only related routes should not be in the document.'
);
}
/**
* Returns the decoded JSON:API document for the for the given entity.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to request.
* @param array $options
* URL options.
*
* @return array
* The decoded response document.
*/
protected function getIndividual(EntityInterface $entity, array $options = []) {
$entity_type_id = $entity->getEntityTypeId();
$bundle = $entity->bundle();
$path = "/jsonapi/{$entity_type_id}/{$bundle}/{$entity->uuid()}";
return $this->jsonapiGet($path, $options);
}
/**
* Performs an authenticated request and returns the decoded document.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to request.
* @param string $relationship
* The field name of the relationship to request.
* @param array $options
* URL options.
*
* @return array
* The decoded response document.
*/
protected function getRelated(EntityInterface $entity, $relationship, array $options = []) {
$entity_type_id = $entity->getEntityTypeId();
$bundle = $entity->bundle();
$path = "/jsonapi/{$entity_type_id}/{$bundle}/{$entity->uuid()}/{$relationship}";
return $this->jsonapiGet($path, $options);
}
/**
* Performs an authenticated request and returns the decoded document.
*/
protected function jsonapiGet($path, array $options = []) {
$this->drupalLogin($this->testUser);
$response = $this->drupalGet($path, $options, ['Accept' => 'application/vnd.api+json']);
return Json::decode($response);
}
}
|