04b7c87ea37a4d0c2c14752675f48fefe46065a9
[yaffs-website] / web / core / modules / file / tests / src / Functional / Rest / FileResourceTestBase.php
1 <?php
2
3 namespace Drupal\Tests\file\Functional\Rest;
4
5 use Drupal\file\Entity\File;
6 use Drupal\Tests\rest\Functional\BcTimestampNormalizerUnixTestTrait;
7 use Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase;
8 use Drupal\user\Entity\User;
9
10 abstract class FileResourceTestBase extends EntityResourceTestBase {
11
12   use BcTimestampNormalizerUnixTestTrait;
13
14   /**
15    * {@inheritdoc}
16    */
17   public static $modules = ['file', 'user'];
18
19   /**
20    * {@inheritdoc}
21    */
22   protected static $entityTypeId = 'file';
23
24   /**
25    * @var \Drupal\file\FileInterface
26    */
27   protected $entity;
28
29   /**
30    * {@inheritdoc}
31    */
32   protected static $patchProtectedFieldNames = [
33     'uri' => NULL,
34     'filemime' => NULL,
35     'filesize' => NULL,
36     'status' => NULL,
37     'changed' => NULL,
38   ];
39
40   /**
41    * @var \Drupal\user\UserInterface
42    */
43   protected $author;
44
45   /**
46    * {@inheritdoc}
47    */
48   protected function setUpAuthorization($method) {
49     switch ($method) {
50       case 'GET':
51         $this->grantPermissionsToTestedRole(['access content']);
52         break;
53
54       case 'PATCH':
55       case 'DELETE':
56         // \Drupal\file\FileAccessControlHandler::checkAccess() grants 'update'
57         // and 'delete' access only to the user that owns the file. So there is
58         // no permission to grant: instead, the file owner must be changed from
59         // its default (user 1) to the current user.
60         $this->makeCurrentUserFileOwner();
61         break;
62     }
63   }
64
65   /**
66    * {@inheritdoc}
67    */
68   protected function grantPermissionsToTestedRole(array $permissions) {
69     // testPatch() and testDelete() test the 'bc_entity_resource_permissions' BC
70     // layer; also call makeCurrentUserFileOwner() then.
71     if ($permissions === ['restful patch entity:file'] || $permissions === ['restful delete entity:file']) {
72       $this->makeCurrentUserFileOwner();
73     }
74     parent::grantPermissionsToTestedRole($permissions);
75   }
76
77   /**
78    * Makes the current user the file owner.
79    */
80   protected function makeCurrentUserFileOwner() {
81     $account = static::$auth ? User::load(2) : User::load(0);
82     $this->entity->setOwnerId($account->id());
83     $this->entity->setOwner($account);
84     $this->entity->save();
85   }
86
87   /**
88    * {@inheritdoc}
89    */
90   protected function createEntity() {
91     $this->author = User::load(1);
92
93     $file = File::create();
94     $file->setOwnerId($this->author->id());
95     $file->setFilename('drupal.txt');
96     $file->setMimeType('text/plain');
97     $file->setFileUri('public://drupal.txt');
98     $file->set('status', FILE_STATUS_PERMANENT);
99     $file->save();
100
101     file_put_contents($file->getFileUri(), 'Drupal');
102
103     return $file;
104   }
105
106   /**
107    * {@inheritdoc}
108    */
109   protected function getExpectedNormalizedEntity() {
110     return [
111       'changed' => [
112         $this->formatExpectedTimestampItemValues($this->entity->getChangedTime()),
113       ],
114       'created' => [
115         $this->formatExpectedTimestampItemValues((int) $this->entity->getCreatedTime()),
116       ],
117       'fid' => [
118         [
119           'value' => 1,
120         ],
121       ],
122       'filemime' => [
123         [
124           'value' => 'text/plain',
125         ],
126       ],
127       'filename' => [
128         [
129           'value' => 'drupal.txt',
130         ],
131       ],
132       'filesize' => [
133         [
134           'value' => (int) $this->entity->getSize(),
135         ],
136       ],
137       'langcode' => [
138         [
139           'value' => 'en',
140         ],
141       ],
142       'status' => [
143         [
144           'value' => TRUE,
145         ],
146       ],
147       'uid' => [
148         [
149           'target_id' => (int) $this->author->id(),
150           'target_type' => 'user',
151           'target_uuid' => $this->author->uuid(),
152           'url' => base_path() . 'user/' . $this->author->id(),
153         ],
154       ],
155       'uri' => [
156         [
157           'url' => base_path() . $this->siteDirectory . '/files/drupal.txt',
158           'value' => 'public://drupal.txt',
159         ],
160       ],
161       'uuid' => [
162         [
163           'value' => $this->entity->uuid(),
164         ],
165       ],
166     ];
167   }
168
169   /**
170    * {@inheritdoc}
171    */
172   protected function getNormalizedPostEntity() {
173     return [
174       'uid' => [
175         [
176           'target_id' => $this->author->id(),
177         ],
178       ],
179       'filename' => [
180         [
181           'value' => 'drupal.txt',
182         ],
183       ],
184     ];
185   }
186
187   /**
188    * {@inheritdoc}
189    */
190   protected function getNormalizedPatchEntity() {
191     return array_diff_key($this->getNormalizedPostEntity(), ['uid' => TRUE]);
192   }
193
194   /**
195    * {@inheritdoc}
196    */
197   protected function getExpectedCacheContexts() {
198     return [
199       'user.permissions',
200     ];
201   }
202
203   /**
204    * {@inheritdoc}
205    */
206   public function testPost() {
207     // Drupal does not allow creating file entities independently. It allows you
208     // to create file entities that are referenced from another entity (e.g. an
209     // image for a node's image field).
210     // For that purpose, there is the "file_upload" REST resource plugin.
211     // @see \Drupal\file\FileAccessControlHandler::checkCreateAccess()
212     // @see \Drupal\file\Plugin\rest\resource\FileUploadResource
213     $this->markTestSkipped();
214   }
215
216   /**
217    * {@inheritdoc}
218    */
219   protected function getExpectedUnauthorizedAccessMessage($method) {
220     if ($this->config('rest.settings')->get('bc_entity_resource_permissions')) {
221       return parent::getExpectedUnauthorizedAccessMessage($method);
222     }
223
224     if ($method === 'GET') {
225       return "The 'access content' permission is required.";
226     }
227     if ($method === 'PATCH' || $method === 'DELETE') {
228       return 'Only the file owner can update or delete the file entity.';
229     }
230     return parent::getExpectedUnauthorizedAccessMessage($method);
231   }
232
233 }