Mercurial > hg > cmmr2012-drupal-site
comparison core/modules/jsonapi/tests/src/Functional/MediaTest.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\Core\Entity\EntityInterface; | |
6 use Drupal\Core\Url; | |
7 use Drupal\file\Entity\File; | |
8 use Drupal\media\Entity\Media; | |
9 use Drupal\media\Entity\MediaType; | |
10 use Drupal\Tests\jsonapi\Traits\CommonCollectionFilterAccessTestPatternsTrait; | |
11 use Drupal\user\Entity\User; | |
12 | |
13 /** | |
14 * JSON:API integration test for the "Media" content entity type. | |
15 * | |
16 * @group jsonapi | |
17 */ | |
18 class MediaTest extends ResourceTestBase { | |
19 | |
20 use CommonCollectionFilterAccessTestPatternsTrait; | |
21 | |
22 /** | |
23 * {@inheritdoc} | |
24 */ | |
25 public static $modules = ['media']; | |
26 | |
27 /** | |
28 * {@inheritdoc} | |
29 */ | |
30 protected static $entityTypeId = 'media'; | |
31 | |
32 /** | |
33 * {@inheritdoc} | |
34 */ | |
35 protected static $resourceTypeName = 'media--camelids'; | |
36 | |
37 /** | |
38 * {@inheritdoc} | |
39 */ | |
40 protected static $resourceTypeIsVersionable = TRUE; | |
41 | |
42 /** | |
43 * {@inheritdoc} | |
44 * | |
45 * @var \Drupal\media\MediaInterface | |
46 */ | |
47 protected $entity; | |
48 | |
49 /** | |
50 * {@inheritdoc} | |
51 */ | |
52 protected static $patchProtectedFieldNames = [ | |
53 'changed' => NULL, | |
54 ]; | |
55 | |
56 /** | |
57 * {@inheritdoc} | |
58 */ | |
59 protected function setUpAuthorization($method) { | |
60 switch ($method) { | |
61 case 'GET': | |
62 $this->grantPermissionsToTestedRole(['view media']); | |
63 break; | |
64 | |
65 case 'POST': | |
66 $this->grantPermissionsToTestedRole(['create camelids media', 'access content']); | |
67 break; | |
68 | |
69 case 'PATCH': | |
70 $this->grantPermissionsToTestedRole(['edit any camelids media']); | |
71 // @todo Remove this in https://www.drupal.org/node/2824851. | |
72 $this->grantPermissionsToTestedRole(['access content']); | |
73 break; | |
74 | |
75 case 'DELETE': | |
76 $this->grantPermissionsToTestedRole(['delete any camelids media']); | |
77 break; | |
78 } | |
79 } | |
80 | |
81 /** | |
82 * {@inheritdoc} | |
83 */ | |
84 protected function setUpRevisionAuthorization($method) { | |
85 parent::setUpRevisionAuthorization($method); | |
86 $this->grantPermissionsToTestedRole(['view all media revisions']); | |
87 } | |
88 | |
89 /** | |
90 * {@inheritdoc} | |
91 */ | |
92 protected function createEntity() { | |
93 if (!MediaType::load('camelids')) { | |
94 // Create a "Camelids" media type. | |
95 $media_type = MediaType::create([ | |
96 'name' => 'Camelids', | |
97 'id' => 'camelids', | |
98 'description' => 'Camelids are large, strictly herbivorous animals with slender necks and long legs.', | |
99 'source' => 'file', | |
100 ]); | |
101 $media_type->save(); | |
102 // Create the source field. | |
103 $source_field = $media_type->getSource()->createSourceField($media_type); | |
104 $source_field->getFieldStorageDefinition()->save(); | |
105 $source_field->save(); | |
106 $media_type | |
107 ->set('source_configuration', [ | |
108 'source_field' => $source_field->getName(), | |
109 ]) | |
110 ->save(); | |
111 } | |
112 | |
113 // Create a file to upload. | |
114 $file = File::create([ | |
115 'uri' => 'public://llama.txt', | |
116 ]); | |
117 $file->setPermanent(); | |
118 $file->save(); | |
119 | |
120 // @see \Drupal\Tests\jsonapi\Functional\MediaTest::testPostIndividual() | |
121 $post_file = File::create([ | |
122 'uri' => 'public://llama2.txt', | |
123 ]); | |
124 $post_file->setPermanent(); | |
125 $post_file->save(); | |
126 | |
127 // Create a "Llama" media item. | |
128 $media = Media::create([ | |
129 'bundle' => 'camelids', | |
130 'field_media_file' => [ | |
131 'target_id' => $file->id(), | |
132 ], | |
133 ]); | |
134 $media | |
135 ->setName('Llama') | |
136 ->setPublished() | |
137 ->setCreatedTime(123456789) | |
138 ->setOwnerId($this->account->id()) | |
139 ->setRevisionUserId($this->account->id()) | |
140 ->save(); | |
141 | |
142 return $media; | |
143 } | |
144 | |
145 /** | |
146 * {@inheritdoc} | |
147 */ | |
148 protected function getExpectedDocument() { | |
149 $file = File::load(1); | |
150 $thumbnail = File::load(3); | |
151 $author = User::load($this->entity->getOwnerId()); | |
152 $base_url = Url::fromUri('base:/jsonapi/media/camelids/' . $this->entity->uuid())->setAbsolute(); | |
153 $self_url = clone $base_url; | |
154 $version_identifier = 'id:' . $this->entity->getRevisionId(); | |
155 $self_url = $self_url->setOption('query', ['resourceVersion' => $version_identifier]); | |
156 $version_query_string = '?resourceVersion=' . urlencode($version_identifier); | |
157 return [ | |
158 'jsonapi' => [ | |
159 'meta' => [ | |
160 'links' => [ | |
161 'self' => ['href' => 'http://jsonapi.org/format/1.0/'], | |
162 ], | |
163 ], | |
164 'version' => '1.0', | |
165 ], | |
166 'links' => [ | |
167 'self' => ['href' => $base_url->toString()], | |
168 ], | |
169 'data' => [ | |
170 'id' => $this->entity->uuid(), | |
171 'type' => 'media--camelids', | |
172 'links' => [ | |
173 'self' => ['href' => $self_url->toString()], | |
174 ], | |
175 'attributes' => [ | |
176 'langcode' => 'en', | |
177 'name' => 'Llama', | |
178 'status' => TRUE, | |
179 'created' => '1973-11-29T21:33:09+00:00', | |
180 'changed' => (new \DateTime())->setTimestamp($this->entity->getChangedTime())->setTimezone(new \DateTimeZone('UTC'))->format(\DateTime::RFC3339), | |
181 'revision_created' => (new \DateTime())->setTimestamp($this->entity->getRevisionCreationTime())->setTimezone(new \DateTimeZone('UTC'))->format(\DateTime::RFC3339), | |
182 'default_langcode' => TRUE, | |
183 'revision_log_message' => NULL, | |
184 // @todo Attempt to remove this in https://www.drupal.org/project/drupal/issues/2933518. | |
185 'revision_translation_affected' => TRUE, | |
186 'drupal_internal__mid' => 1, | |
187 'drupal_internal__vid' => 1, | |
188 ], | |
189 'relationships' => [ | |
190 'field_media_file' => [ | |
191 'data' => [ | |
192 'id' => $file->uuid(), | |
193 'meta' => [ | |
194 'description' => NULL, | |
195 'display' => NULL, | |
196 ], | |
197 'type' => 'file--file', | |
198 ], | |
199 'links' => [ | |
200 'related' => [ | |
201 'href' => $base_url->toString() . '/field_media_file' . $version_query_string, | |
202 ], | |
203 'self' => [ | |
204 'href' => $base_url->toString() . '/relationships/field_media_file' . $version_query_string, | |
205 ], | |
206 ], | |
207 ], | |
208 'thumbnail' => [ | |
209 'data' => [ | |
210 'id' => $thumbnail->uuid(), | |
211 'meta' => [ | |
212 'alt' => '', | |
213 'width' => 180, | |
214 'height' => 180, | |
215 'title' => NULL, | |
216 ], | |
217 'type' => 'file--file', | |
218 ], | |
219 'links' => [ | |
220 'related' => [ | |
221 'href' => $base_url->toString() . '/thumbnail' . $version_query_string, | |
222 ], | |
223 'self' => [ | |
224 'href' => $base_url->toString() . '/relationships/thumbnail' . $version_query_string, | |
225 ], | |
226 ], | |
227 ], | |
228 'bundle' => [ | |
229 'data' => [ | |
230 'id' => MediaType::load('camelids')->uuid(), | |
231 'type' => 'media_type--media_type', | |
232 ], | |
233 'links' => [ | |
234 'related' => [ | |
235 'href' => $base_url->toString() . '/bundle' . $version_query_string, | |
236 ], | |
237 'self' => [ | |
238 'href' => $base_url->toString() . '/relationships/bundle' . $version_query_string, | |
239 ], | |
240 ], | |
241 ], | |
242 'uid' => [ | |
243 'data' => [ | |
244 'id' => $author->uuid(), | |
245 'type' => 'user--user', | |
246 ], | |
247 'links' => [ | |
248 'related' => [ | |
249 'href' => $base_url->toString() . '/uid' . $version_query_string, | |
250 ], | |
251 'self' => [ | |
252 'href' => $base_url->toString() . '/relationships/uid' . $version_query_string, | |
253 ], | |
254 ], | |
255 ], | |
256 'revision_user' => [ | |
257 'data' => [ | |
258 'id' => $author->uuid(), | |
259 'type' => 'user--user', | |
260 ], | |
261 'links' => [ | |
262 'related' => [ | |
263 'href' => $base_url->toString() . '/revision_user' . $version_query_string, | |
264 ], | |
265 'self' => [ | |
266 'href' => $base_url->toString() . '/relationships/revision_user' . $version_query_string, | |
267 ], | |
268 ], | |
269 ], | |
270 ], | |
271 ], | |
272 ]; | |
273 } | |
274 | |
275 /** | |
276 * {@inheritdoc} | |
277 */ | |
278 protected function getPostDocument() { | |
279 $file = File::load(2); | |
280 return [ | |
281 'data' => [ | |
282 'type' => 'media--camelids', | |
283 'attributes' => [ | |
284 'name' => 'Dramallama', | |
285 ], | |
286 'relationships' => [ | |
287 'field_media_file' => [ | |
288 'data' => [ | |
289 'id' => $file->uuid(), | |
290 'meta' => [ | |
291 'description' => 'This file is better!', | |
292 'display' => NULL, | |
293 ], | |
294 'type' => 'file--file', | |
295 ], | |
296 ], | |
297 ], | |
298 ], | |
299 ]; | |
300 } | |
301 | |
302 /** | |
303 * {@inheritdoc} | |
304 */ | |
305 protected function getExpectedUnauthorizedAccessMessage($method) { | |
306 switch ($method) { | |
307 case 'GET'; | |
308 return "The 'view media' permission is required when the media item is published."; | |
309 | |
310 case 'POST': | |
311 return "The following permissions are required: 'administer media' OR 'create media' OR 'create camelids media'."; | |
312 | |
313 case 'PATCH': | |
314 return "The following permissions are required: 'update any media' OR 'update own media' OR 'camelids: edit any media' OR 'camelids: edit own media'."; | |
315 | |
316 case 'DELETE': | |
317 return "The following permissions are required: 'delete any media' OR 'delete own media' OR 'camelids: delete any media' OR 'camelids: delete own media'."; | |
318 | |
319 default: | |
320 return ''; | |
321 } | |
322 } | |
323 | |
324 /** | |
325 * {@inheritdoc} | |
326 */ | |
327 protected function getEditorialPermissions() { | |
328 return array_merge(parent::getEditorialPermissions(), ['view any unpublished content']); | |
329 } | |
330 | |
331 /** | |
332 * {@inheritdoc} | |
333 */ | |
334 protected function getExpectedUnauthorizedAccessCacheability() { | |
335 // @see \Drupal\media\MediaAccessControlHandler::checkAccess() | |
336 return parent::getExpectedUnauthorizedAccessCacheability() | |
337 ->addCacheTags(['media:1']); | |
338 } | |
339 | |
340 // @codingStandardsIgnoreStart | |
341 /** | |
342 * {@inheritdoc} | |
343 */ | |
344 public function testPostIndividual() { | |
345 // @todo Mimic \Drupal\Tests\rest\Functional\EntityResource\Media\MediaResourceTestBase::testPost() | |
346 // @todo Later, use https://www.drupal.org/project/jsonapi/issues/2958554 to upload files rather than the REST module. | |
347 parent::testPostIndividual(); | |
348 } | |
349 // @codingStandardsIgnoreEnd | |
350 | |
351 /** | |
352 * {@inheritdoc} | |
353 * | |
354 * @todo Determine if this override should be removed in https://www.drupal.org/project/jsonapi/issues/2952522 | |
355 */ | |
356 protected function getExpectedGetRelationshipDocumentData($relationship_field_name, EntityInterface $entity = NULL) { | |
357 $data = parent::getExpectedGetRelationshipDocumentData($relationship_field_name, $entity); | |
358 switch ($relationship_field_name) { | |
359 case 'thumbnail': | |
360 $data['meta'] = [ | |
361 'alt' => '', | |
362 'width' => 180, | |
363 'height' => 180, | |
364 'title' => NULL, | |
365 ]; | |
366 return $data; | |
367 | |
368 case 'field_media_file': | |
369 $data['meta'] = [ | |
370 'description' => NULL, | |
371 'display' => NULL, | |
372 ]; | |
373 return $data; | |
374 | |
375 default: | |
376 return $data; | |
377 } | |
378 } | |
379 | |
380 /** | |
381 * {@inheritdoc} | |
382 * | |
383 * @todo Remove this in https://www.drupal.org/node/2824851. | |
384 */ | |
385 protected function doTestRelationshipMutation(array $request_options) { | |
386 $this->grantPermissionsToTestedRole(['access content']); | |
387 parent::doTestRelationshipMutation($request_options); | |
388 } | |
389 | |
390 /** | |
391 * {@inheritdoc} | |
392 */ | |
393 public function testCollectionFilterAccess() { | |
394 $this->doTestCollectionFilterAccessForPublishableEntities('name', 'view media', 'administer media'); | |
395 } | |
396 | |
397 } |