View source
<?php
namespace Drupal\Tests\file\Functional\Rest;
use Drupal\file\Entity\File;
use Drupal\Tests\rest\Functional\EntityResource\EntityResourceTestBase;
use Drupal\user\Entity\User;
abstract class FileResourceTestBase extends EntityResourceTestBase {
protected static $modules = [
'file',
'user',
];
protected static $entityTypeId = 'file';
protected $entity;
protected static $patchProtectedFieldNames = [
'uri' => NULL,
'filemime' => NULL,
'filesize' => NULL,
'status' => NULL,
'changed' => NULL,
];
protected $author;
protected function setUpAuthorization($method) {
switch ($method) {
case 'GET':
$this
->grantPermissionsToTestedRole([
'access content',
]);
break;
case 'PATCH':
case 'DELETE':
$this
->makeCurrentUserFileOwner();
break;
}
}
protected function makeCurrentUserFileOwner() {
$account = static::$auth ? User::load(2) : User::load(0);
$this->entity
->setOwnerId($account
->id());
$this->entity
->setOwner($account);
$this->entity
->save();
}
protected function createEntity() {
$this->author = User::load(1);
$file = File::create();
$file
->setOwnerId($this->author
->id());
$file
->setFilename('drupal.txt');
$file
->setMimeType('text/plain');
$file
->setFileUri('public://drupal.txt');
$file
->setPermanent();
$file
->save();
file_put_contents($file
->getFileUri(), 'Drupal');
return $file;
}
protected function getExpectedNormalizedEntity() {
return [
'changed' => [
[
'value' => (new \DateTime())
->setTimestamp($this->entity
->getChangedTime())
->setTimezone(new \DateTimeZone('UTC'))
->format(\DateTime::RFC3339),
'format' => \DateTime::RFC3339,
],
],
'created' => [
[
'value' => (new \DateTime())
->setTimestamp((int) $this->entity
->getCreatedTime())
->setTimezone(new \DateTimeZone('UTC'))
->format(\DateTime::RFC3339),
'format' => \DateTime::RFC3339,
],
],
'fid' => [
[
'value' => 1,
],
],
'filemime' => [
[
'value' => 'text/plain',
],
],
'filename' => [
[
'value' => 'drupal.txt',
],
],
'filesize' => [
[
'value' => (int) $this->entity
->getSize(),
],
],
'langcode' => [
[
'value' => 'en',
],
],
'status' => [
[
'value' => TRUE,
],
],
'uid' => [
[
'target_id' => (int) $this->author
->id(),
'target_type' => 'user',
'target_uuid' => $this->author
->uuid(),
'url' => base_path() . 'user/' . $this->author
->id(),
],
],
'uri' => [
[
'url' => base_path() . $this->siteDirectory . '/files/drupal.txt',
'value' => 'public://drupal.txt',
],
],
'uuid' => [
[
'value' => $this->entity
->uuid(),
],
],
];
}
protected function getNormalizedPostEntity() {
return [
'uid' => [
[
'target_id' => $this->author
->id(),
],
],
'filename' => [
[
'value' => 'drupal.txt',
],
],
];
}
protected function getNormalizedPatchEntity() {
return array_diff_key($this
->getNormalizedPostEntity(), [
'uid' => TRUE,
]);
}
protected function getExpectedCacheContexts() {
return [
'user.permissions',
];
}
public function testPost() {
$this
->markTestSkipped();
}
protected function getExpectedUnauthorizedAccessMessage($method) {
if ($method === 'GET') {
return "The 'access content' permission is required.";
}
if ($method === 'PATCH' || $method === 'DELETE') {
return 'Only the file owner can update or delete the file entity.';
}
return parent::getExpectedUnauthorizedAccessMessage($method);
}
}