f5bfa1429147d2ddec1df94c4c769c48ca2e5bd6
[yaffs-website] / web / core / modules / rest / tests / src / Functional / EntityResource / Media / MediaResourceTestBase.php
1 <?php
2
3 namespace Drupal\Tests\rest\Functional\EntityResource\Media;
4
5 use Drupal\file\Entity\File;
6 use Drupal\media\Entity\Media;
7 use Drupal\media\Entity\MediaType;
8 use Drupal\Tests\rest\Functional\BcTimestampNormalizerUnixTestTrait;
9 use Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase;
10 use Drupal\user\Entity\User;
11
12 abstract class MediaResourceTestBase extends EntityResourceTestBase {
13
14   use BcTimestampNormalizerUnixTestTrait;
15
16   /**
17    * {@inheritdoc}
18    */
19   public static $modules = ['media'];
20
21   /**
22    * {@inheritdoc}
23    */
24   protected static $entityTypeId = 'media';
25
26   /**
27    * @var \Drupal\media\MediaInterface
28    */
29   protected $entity;
30
31   /**
32    * {@inheritdoc}
33    */
34   protected static $patchProtectedFieldNames = [
35     'changed',
36   ];
37
38   /**
39    * {@inheritdoc}
40    */
41   protected function setUpAuthorization($method) {
42     switch ($method) {
43       case 'GET':
44         $this->grantPermissionsToTestedRole(['view media']);
45         break;
46
47       case 'POST':
48         $this->grantPermissionsToTestedRole(['create media']);
49         break;
50
51       case 'PATCH':
52         $this->grantPermissionsToTestedRole(['update any media']);
53         break;
54
55       case 'DELETE':
56         $this->grantPermissionsToTestedRole(['delete any media']);
57         break;
58     }
59   }
60
61   /**
62    * {@inheritdoc}
63    */
64   protected function createEntity() {
65     if (!MediaType::load('camelids')) {
66       // Create a "Camelids" media type.
67       $media_type = MediaType::create([
68         'name' => 'Camelids',
69         'id' => 'camelids',
70         'description' => 'Camelids are large, strictly herbivorous animals with slender necks and long legs.',
71         'source' => 'file',
72       ]);
73       $media_type->save();
74       // Create the source field.
75       $source_field = $media_type->getSource()->createSourceField($media_type);
76       $source_field->getFieldStorageDefinition()->save();
77       $source_field->save();
78       $media_type
79         ->set('source_configuration', [
80           'source_field' => $source_field->getName(),
81         ])
82         ->save();
83     }
84
85     // Create a file to upload.
86     $file = File::create([
87       'uri' => 'public://llama.txt',
88     ]);
89     $file->setPermanent();
90     $file->save();
91
92     // Create a "Llama" media item.
93     $media = Media::create([
94       'bundle' => 'camelids',
95       'field_media_file_1' => [
96         'target_id' => $file->id(),
97       ],
98     ]);
99     $media
100       ->setName('Llama')
101       ->setPublished(TRUE)
102       ->setCreatedTime(123456789)
103       ->setOwnerId(static::$auth ? $this->account->id() : 0)
104       ->setRevisionUserId(static::$auth ? $this->account->id() : 0)
105       ->save();
106
107     return $media;
108   }
109
110   /**
111    * {@inheritdoc}
112    */
113   protected function getExpectedNormalizedEntity() {
114     $file = File::load(1);
115     $thumbnail = File::load(2);
116     $author = User::load($this->entity->getOwnerId());
117     return [
118       'mid' => [
119         [
120           'value' => 1,
121         ],
122       ],
123       'uuid' => [
124         [
125           'value' => $this->entity->uuid(),
126         ],
127       ],
128       'vid' => [
129         [
130           'value' => 1,
131         ],
132       ],
133       'langcode' => [
134         [
135           'value' => 'en',
136         ],
137       ],
138       'bundle' => [
139         [
140           'target_id' => 'camelids',
141           'target_type' => 'media_type',
142           'target_uuid' => MediaType::load('camelids')->uuid(),
143         ],
144       ],
145       'name' => [
146         [
147           'value' => 'Llama',
148         ],
149       ],
150       'field_media_file_1' => [
151         [
152           'description' => NULL,
153           'display' => NULL,
154           'target_id' => (int) $file->id(),
155           'target_type' => 'file',
156           'target_uuid' => $file->uuid(),
157           'url' => $file->url(),
158         ],
159       ],
160       'thumbnail' => [
161         [
162           'alt' => 'Thumbnail',
163           'width' => 180,
164           'height' => 180,
165           'target_id' => (int) $thumbnail->id(),
166           'target_type' => 'file',
167           'target_uuid' => $thumbnail->uuid(),
168           'title' => 'Llama',
169           'url' => $thumbnail->url(),
170         ],
171       ],
172       'status' => [
173         [
174           'value' => TRUE,
175         ],
176       ],
177       'created' => [
178         $this->formatExpectedTimestampItemValues(123456789),
179       ],
180       'changed' => [
181         $this->formatExpectedTimestampItemValues($this->entity->getChangedTime()),
182       ],
183       'revision_created' => [
184         $this->formatExpectedTimestampItemValues((int) $this->entity->getRevisionCreationTime()),
185       ],
186       'default_langcode' => [
187         [
188           'value' => TRUE,
189         ],
190       ],
191       'uid' => [
192         [
193           'target_id' => (int) $author->id(),
194           'target_type' => 'user',
195           'target_uuid' => $author->uuid(),
196           'url' => base_path() . 'user/' . $author->id(),
197         ],
198       ],
199       'revision_user' => [
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_log_message' => [],
208       'revision_translation_affected' => [
209         [
210           'value' => TRUE,
211         ],
212       ],
213     ];
214   }
215
216   /**
217    * {@inheritdoc}
218    */
219   protected function getNormalizedPostEntity() {
220     return [
221       'bundle' => [
222         [
223           'target_id' => 'camelids',
224         ],
225       ],
226       'name' => [
227         [
228           'value' => 'Dramallama',
229         ],
230       ],
231     ];
232   }
233
234   /**
235    * {@inheritdoc}
236    */
237   protected function getExpectedUnauthorizedAccessMessage($method) {
238     if ($this->config('rest.settings')->get('bc_entity_resource_permissions')) {
239       return parent::getExpectedUnauthorizedAccessMessage($method);
240     }
241
242     switch ($method) {
243       case 'GET';
244         return "The 'view media' permission is required and the media item must be published.";
245
246       case 'PATCH':
247         return 'You are not authorized to update this media entity of bundle camelids.';
248
249       case 'DELETE':
250         return 'You are not authorized to delete this media entity of bundle camelids.';
251
252       default:
253         return parent::getExpectedUnauthorizedAccessMessage($method);
254     }
255   }
256
257   /**
258    * {@inheritdoc}
259    */
260   public function testPost() {
261     $this->markTestSkipped('POSTing File Media items is not supported until https://www.drupal.org/node/1927648 is solved.');
262   }
263
264 }