Pull merge.
[yaffs-website] / web / core / modules / media / tests / src / Functional / Rest / MediaResourceTestBase.php
1 <?php
2
3 namespace Drupal\Tests\media\Functional\Rest;
4
5 use Drupal\Component\Utility\NestedArray;
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\rest\RestResourceConfigInterface;
11 use Drupal\Tests\rest\Functional\BcTimestampNormalizerUnixTestTrait;
12 use Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase;
13 use Drupal\user\Entity\Role;
14 use Drupal\user\Entity\User;
15 use Drupal\user\RoleInterface;
16 use GuzzleHttp\RequestOptions;
17
18 abstract class MediaResourceTestBase extends EntityResourceTestBase {
19
20   use BcTimestampNormalizerUnixTestTrait;
21
22   /**
23    * {@inheritdoc}
24    */
25   public static $modules = ['media'];
26
27   /**
28    * {@inheritdoc}
29    */
30   protected static $entityTypeId = 'media';
31
32   /**
33    * @var \Drupal\media\MediaInterface
34    */
35   protected $entity;
36
37   /**
38    * {@inheritdoc}
39    */
40   protected static $patchProtectedFieldNames = [
41     'changed' => NULL,
42   ];
43
44   /**
45    * {@inheritdoc}
46    */
47   protected function setUpAuthorization($method) {
48     switch ($method) {
49       case 'GET':
50         $this->grantPermissionsToTestedRole(['view media']);
51         break;
52
53       case 'POST':
54         $this->grantPermissionsToTestedRole(['create camelids media', 'access content']);
55         break;
56
57       case 'PATCH':
58         $this->grantPermissionsToTestedRole(['edit any camelids media']);
59         // @todo Remove this in https://www.drupal.org/node/2824851.
60         $this->grantPermissionsToTestedRole(['access content']);
61         break;
62
63       case 'DELETE':
64         $this->grantPermissionsToTestedRole(['delete any camelids media']);
65         break;
66     }
67   }
68
69   /**
70    * {@inheritdoc}
71    */
72   protected function createEntity() {
73     if (!MediaType::load('camelids')) {
74       // Create a "Camelids" media type.
75       $media_type = MediaType::create([
76         'name' => 'Camelids',
77         'id' => 'camelids',
78         'description' => 'Camelids are large, strictly herbivorous animals with slender necks and long legs.',
79         'source' => 'file',
80       ]);
81       $media_type->save();
82       // Create the source field.
83       $source_field = $media_type->getSource()->createSourceField($media_type);
84       $source_field->getFieldStorageDefinition()->save();
85       $source_field->save();
86       $media_type
87         ->set('source_configuration', [
88           'source_field' => $source_field->getName(),
89         ])
90         ->save();
91     }
92
93     // Create a file to upload.
94     $file = File::create([
95       'uri' => 'public://llama.txt',
96     ]);
97     $file->setPermanent();
98     $file->save();
99
100     // Create a "Llama" media item.
101     $media = Media::create([
102       'bundle' => 'camelids',
103       'field_media_file' => [
104         'target_id' => $file->id(),
105       ],
106     ]);
107     $media
108       ->setName('Llama')
109       ->setPublished()
110       ->setCreatedTime(123456789)
111       ->setOwnerId(static::$auth ? $this->account->id() : 0)
112       ->setRevisionUserId(static::$auth ? $this->account->id() : 0)
113       ->save();
114
115     return $media;
116   }
117
118   /**
119    * {@inheritdoc}
120    */
121   protected function getExpectedNormalizedEntity() {
122     $file = File::load(1);
123     $thumbnail = File::load(2);
124     $author = User::load($this->entity->getOwnerId());
125     return [
126       'mid' => [
127         [
128           'value' => 1,
129         ],
130       ],
131       'uuid' => [
132         [
133           'value' => $this->entity->uuid(),
134         ],
135       ],
136       'vid' => [
137         [
138           'value' => 1,
139         ],
140       ],
141       'langcode' => [
142         [
143           'value' => 'en',
144         ],
145       ],
146       'bundle' => [
147         [
148           'target_id' => 'camelids',
149           'target_type' => 'media_type',
150           'target_uuid' => MediaType::load('camelids')->uuid(),
151         ],
152       ],
153       'name' => [
154         [
155           'value' => 'Llama',
156         ],
157       ],
158       'field_media_file' => [
159         [
160           'description' => NULL,
161           'display' => NULL,
162           'target_id' => (int) $file->id(),
163           'target_type' => 'file',
164           'target_uuid' => $file->uuid(),
165           'url' => $file->url(),
166         ],
167       ],
168       'thumbnail' => [
169         [
170           'alt' => '',
171           'width' => 180,
172           'height' => 180,
173           'target_id' => (int) $thumbnail->id(),
174           'target_type' => 'file',
175           'target_uuid' => $thumbnail->uuid(),
176           'title' => NULL,
177           'url' => $thumbnail->url(),
178         ],
179       ],
180       'status' => [
181         [
182           'value' => TRUE,
183         ],
184       ],
185       'created' => [
186         $this->formatExpectedTimestampItemValues(123456789),
187       ],
188       'changed' => [
189         $this->formatExpectedTimestampItemValues($this->entity->getChangedTime()),
190       ],
191       'revision_created' => [
192         $this->formatExpectedTimestampItemValues((int) $this->entity->getRevisionCreationTime()),
193       ],
194       'default_langcode' => [
195         [
196           'value' => TRUE,
197         ],
198       ],
199       'uid' => [
200         [
201           'target_id' => (int) $author->id(),
202           'target_type' => 'user',
203           'target_uuid' => $author->uuid(),
204           'url' => base_path() . 'user/' . $author->id(),
205         ],
206       ],
207       'revision_user' => [
208         [
209           'target_id' => (int) $author->id(),
210           'target_type' => 'user',
211           'target_uuid' => $author->uuid(),
212           'url' => base_path() . 'user/' . $author->id(),
213         ],
214       ],
215       'revision_log_message' => [],
216       'revision_translation_affected' => [
217         [
218           'value' => TRUE,
219         ],
220       ],
221     ];
222   }
223
224   /**
225    * {@inheritdoc}
226    */
227   protected function getNormalizedPostEntity() {
228     return [
229       'bundle' => [
230         [
231           'target_id' => 'camelids',
232         ],
233       ],
234       'name' => [
235         [
236           'value' => 'Dramallama',
237         ],
238       ],
239       'field_media_file' => [
240         [
241           'description' => NULL,
242           'display' => NULL,
243           'target_id' => 3,
244         ],
245       ],
246     ];
247   }
248
249   /**
250    * {@inheritdoc}
251    */
252   protected function getNormalizedPatchEntity() {
253     return array_diff_key($this->getNormalizedPostEntity(), ['field_media_file' => TRUE]);
254   }
255
256   /**
257    * {@inheritdoc}
258    */
259   protected function getExpectedUnauthorizedAccessMessage($method) {
260     if ($this->config('rest.settings')->get('bc_entity_resource_permissions')) {
261       return parent::getExpectedUnauthorizedAccessMessage($method);
262     }
263
264     switch ($method) {
265       case 'GET';
266         return "The 'view media' permission is required and the media item must be published.";
267
268       case 'POST':
269         return "The following permissions are required: 'administer media' OR 'create media' OR 'create camelids media'.";
270
271       case 'PATCH':
272         return "The following permissions are required: 'update any media' OR 'update own media' OR 'camelids: edit any media' OR 'camelids: edit own media'.";
273
274       case 'DELETE':
275         return "The following permissions are required: 'delete any media' OR 'delete own media' OR 'camelids: delete any media' OR 'camelids: delete own media'.";
276
277       default:
278         return parent::getExpectedUnauthorizedAccessMessage($method);
279     }
280   }
281
282   /**
283    * {@inheritdoc}
284    */
285   public function testPost() {
286     $file_storage = $this->container->get('entity_type.manager')->getStorage('file');
287
288     // Step 1: upload file, results in File entity marked temporary.
289     $this->uploadFile();
290     $file = $file_storage->loadUnchanged(3);
291     $this->assertTrue($file->isTemporary());
292     $this->assertFalse($file->isPermanent());
293
294     // Step 2: create Media entity using the File, makes File entity permanent.
295     parent::testPost();
296     $file = $file_storage->loadUnchanged(3);
297     $this->assertFalse($file->isTemporary());
298     $this->assertTrue($file->isPermanent());
299   }
300
301   /**
302    * This duplicates some of the 'file_upload' REST resource plugin test
303    * coverage, to be able to test it on a concrete use case.
304    */
305   protected function uploadFile() {
306     // Enable the 'file_upload' REST resource for the current format + auth.
307     $this->resourceConfigStorage->create([
308       'id' => 'file.upload',
309       'granularity' => RestResourceConfigInterface::RESOURCE_GRANULARITY,
310       'configuration' => [
311         'methods' => ['POST'],
312         'formats' => [static::$format],
313         'authentication' => isset(static::$auth) ? [static::$auth] : [],
314       ],
315       'status' => TRUE,
316     ])->save();
317     $this->refreshTestStateAfterRestConfigChange();
318
319     $this->initAuthentication();
320
321     // POST to create a File entity.
322     $url = Url::fromUri('base:file/upload/media/camelids/field_media_file');
323     $url->setOption('query', ['_format' => static::$format]);
324     $request_options = [];
325     $request_options[RequestOptions::HEADERS] = [
326       // Set the required (and only accepted) content type for the request.
327       'Content-Type' => 'application/octet-stream',
328       // Set the required Content-Disposition header for the file name.
329       'Content-Disposition' => 'file; filename="drupal rocks ðŸ¤˜.txt"',
330     ];
331     $request_options[RequestOptions::BODY] = 'Drupal is the best!';
332     $request_options = NestedArray::mergeDeep($request_options, $this->getAuthenticationRequestOptions('POST'));
333     $response = $this->request('POST', $url, $request_options);
334     $this->assertResourceErrorResponse(403, $this->getExpectedUnauthorizedAccessMessage('POST'), $response);
335
336     // Grant necessary permission, retry.
337     $this->grantPermissionsToTestedRole(['create camelids media']);
338     $response = $this->request('POST', $url, $request_options);
339     $this->assertSame(201, $response->getStatusCode());
340     $expected = $this->getExpectedNormalizedFileEntity();
341     static::recursiveKSort($expected);
342     $actual = $this->serializer->decode((string) $response->getBody(), static::$format);
343     static::recursiveKSort($actual);
344     $this->assertSame($expected, $actual);
345
346     // To still run the complete test coverage for POSTing a Media entity, we
347     // must revoke the additional permissions that we granted.
348     $role = Role::load(static::$auth ? RoleInterface::AUTHENTICATED_ID : RoleInterface::AUTHENTICATED_ID);
349     $role->revokePermission('create camelids media');
350     $role->trustData()->save();
351   }
352
353   /**
354    * Gets the expected file entity.
355    *
356    * @return array
357    *   The expected normalized data array.
358    */
359   protected function getExpectedNormalizedFileEntity() {
360     $file = File::load(3);
361     $owner = static::$auth ? $this->account : User::load(0);
362
363     return [
364       'fid' => [
365         [
366           'value' => 3,
367         ],
368       ],
369       'uuid' => [
370         [
371           'value' => $file->uuid(),
372         ],
373       ],
374       'langcode' => [
375         [
376           'value' => 'en',
377         ],
378       ],
379       'uid' => [
380         [
381           'target_id' => (int) $owner->id(),
382           'target_type' => 'user',
383           'target_uuid' => $owner->uuid(),
384           'url' => base_path() . 'user/' . $owner->id(),
385         ],
386       ],
387       'filename' => [
388         [
389           'value' => 'drupal rocks ðŸ¤˜.txt',
390         ],
391       ],
392       'uri' => [
393         [
394           'value' => 'public://' . date('Y-m') . '/drupal rocks ðŸ¤˜.txt',
395           'url' => base_path() . $this->siteDirectory . '/files/' . date('Y-m') . '/drupal%20rocks%20%F0%9F%A4%98.txt',
396         ],
397       ],
398       'filemime' => [
399         [
400           'value' => 'text/plain',
401         ],
402       ],
403       'filesize' => [
404         [
405           'value' => 19,
406         ],
407       ],
408       'status' => [
409         [
410           'value' => FALSE,
411         ],
412       ],
413       'created' => [
414         $this->formatExpectedTimestampItemValues($file->getCreatedTime()),
415       ],
416       'changed' => [
417         $this->formatExpectedTimestampItemValues($file->getChangedTime()),
418       ],
419     ];
420   }
421
422   /**
423    * {@inheritdoc}
424    */
425   protected function getExpectedUnauthorizedAccessCacheability() {
426     // @see \Drupal\media\MediaAccessControlHandler::checkAccess()
427     return parent::getExpectedUnauthorizedAccessCacheability()
428       ->addCacheTags(['media:1']);
429   }
430
431 }