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