diff --git a/core/modules/file/src/FileAccessControlHandler.php b/core/modules/file/src/FileAccessControlHandler.php index 1faaa84..6745969 100644 --- a/core/modules/file/src/FileAccessControlHandler.php +++ b/core/modules/file/src/FileAccessControlHandler.php @@ -81,7 +81,8 @@ protected function checkCreateAccess(AccountInterface $account, array $context, * {@inheritdoc} */ protected function checkFieldAccess($operation, FieldDefinitionInterface $field_definition, AccountInterface $account, FieldItemListInterface $items = NULL) { - // No user can add the status of a file to avoid to save as persistent. + // No user can edit the status of a file. Prevents saving a new file as + // persistent before even validating it. if ($field_definition->getName() == 'status' && $operation == 'edit') { return AccessResult::forbidden(); } diff --git a/core/modules/file/src/Plugin/Validation/Constraint/FileValidationConstraint.php b/core/modules/file/src/Plugin/Validation/Constraint/FileValidationConstraint.php index 7d2ad53..3b14fee 100644 --- a/core/modules/file/src/Plugin/Validation/Constraint/FileValidationConstraint.php +++ b/core/modules/file/src/Plugin/Validation/Constraint/FileValidationConstraint.php @@ -17,4 +17,6 @@ * label = @Translation("File Validation", context = "Validation") * ) */ -class FileValidationConstraint extends Constraint {} +class FileValidationConstraint extends Constraint { + +} diff --git a/core/modules/hal/hal.services.yml b/core/modules/hal/hal.services.yml index a4df304..58cc6cd 100644 --- a/core/modules/hal/hal.services.yml +++ b/core/modules/hal/hal.services.yml @@ -16,7 +16,7 @@ services: class: Drupal\hal\Normalizer\FileEntityNormalizer tags: - { name: normalizer, priority: 20 } - arguments: ['@rest.link_manager', '@entity.manager', '@module_handler'] + arguments: ['@rest.link_manager', '@entity.manager', '@module_handler', '@file_system'] serializer.normalizer.entity.hal: class: Drupal\hal\Normalizer\ContentEntityNormalizer arguments: ['@rest.link_manager', '@entity.manager', '@module_handler'] diff --git a/core/modules/hal/src/Normalizer/FileEntityNormalizer.php b/core/modules/hal/src/Normalizer/FileEntityNormalizer.php index 17efebf..e3f5df1 100644 --- a/core/modules/hal/src/Normalizer/FileEntityNormalizer.php +++ b/core/modules/hal/src/Normalizer/FileEntityNormalizer.php @@ -7,6 +7,10 @@ namespace Drupal\hal\Normalizer; +use Drupal\Core\Entity\EntityManagerInterface; +use Drupal\Core\Extension\ModuleHandlerInterface; +use Drupal\rest\LinkManager\LinkManagerInterface; +use Drupal\Core\File\FileSystemInterface; use Symfony\Component\Serializer\Exception\RuntimeException; /** @@ -22,6 +26,22 @@ class FileEntityNormalizer extends ContentEntityNormalizer { protected $supportedInterfaceOrClass = 'Drupal\file\FileInterface'; /** + * @var \Drupal\Core\File\FileSystemInterface + */ + protected $fileSystem; + + /** + * Constructs an FileEntityNormalizer object. + * + * @param \Drupal\Core\File\FileSystemInterface $file_system + * The file system. + */ + public function __construct(LinkManagerInterface $link_manager, EntityManagerInterface $entity_manager, ModuleHandlerInterface $module_handler, FileSystemInterface $file_system) { + $this->fileSystem = $file_system; + parent::__construct($link_manager, $entity_manager, $module_handler); + } + + /** * {@inheritdoc} */ public function normalize($entity, $format = NULL, array $context = array()) { @@ -34,7 +54,9 @@ public function normalize($entity, $format = NULL, array $context = array()) { * {@inheritdoc} */ public function denormalize($data, $class, $format = NULL, array $context = array()) { - // Avoid 'data' being treated as a field. + // File content can be passed base64 encoded in a special "data" property. + // That property is not a field, so we remove it before denormalizing the + // rest of the file entity. $file_data = $data['data'][0]['value']; unset($data['data']); @@ -43,7 +65,7 @@ public function denormalize($data, $class, $format = NULL, array $context = arra // Decode and save to file if it's a new file. if (!isset($context['request_method']) || $context['request_method'] != 'patch') { $file_contents = base64_decode($file_data); - $dirname = \Drupal::service('file_system')->dirname($entity->getFileUri()); + $dirname = $this->fileSystem->dirname($entity->getFileUri()); file_prepare_directory($dirname, FILE_CREATE_DIRECTORY); if ($uri = file_unmanaged_save_data($file_contents, $entity->getFileUri())) { $entity->setFileUri($uri); diff --git a/core/modules/hal/src/Tests/EntityTest.php b/core/modules/hal/src/Tests/EntityTest.php index b227eae..a088b1c 100644 --- a/core/modules/hal/src/Tests/EntityTest.php +++ b/core/modules/hal/src/Tests/EntityTest.php @@ -230,16 +230,14 @@ public function testComment() { */ public function testFile() { $user = User::create([ - 'name' => $this->randomMachineName(), + 'name' => 'fileTestingUser', ]); $user->save(); - $file_uri = 'public://' . $this->randomMachineName(); - file_put_contents($file_uri, 'hello world'); - - $data = file_get_contents($file_uri); - $data = base64_encode($data); - file_put_contents($file_uri, 'hello world'); + $file_uri = 'public://normalization_test_file'; + $file_contents = 'hello world'; + $data = base64_encode($file_content); + file_put_contents($file_uri, $file_content); $file = File::create([ 'uid' => $user->id(), 'uri' => $file_uri, diff --git a/core/modules/hal/src/Tests/FileDenormalizeTest.php b/core/modules/hal/src/Tests/FileDenormalizeTest.php index a2345e3..2482788 100644 --- a/core/modules/hal/src/Tests/FileDenormalizeTest.php +++ b/core/modules/hal/src/Tests/FileDenormalizeTest.php @@ -47,7 +47,7 @@ public function testFileDenormalize() { // Adding data to the entity. $normalized_data['data'][0]['value'] = $data; // Use 'patch' to avoid trying to recreate the file. - $denormalized = $serializer->denormalize($normalized_data, 'Drupal\file\Entity\File', 'hal_json', array('request_method' => 'patch')); + $denormalized = $serializer->denormalize($normalized_data, File::class, 'hal_json', array('request_method' => 'patch')); $this->assertTrue($denormalized instanceof File, 'A File instance was created.'); $this->assertIdentical('public://' . $file->getFilename(), $denormalized->getFileUri()); diff --git a/core/modules/hal/src/Tests/FileFieldNormalizeTest.php b/core/modules/hal/src/Tests/FileFieldNormalizeTest.php index b0cfa22..d03b088 100644 --- a/core/modules/hal/src/Tests/FileFieldNormalizeTest.php +++ b/core/modules/hal/src/Tests/FileFieldNormalizeTest.php @@ -42,8 +42,8 @@ public function setUp() { */ public function testFileFieldNormalize() { // Create a file. - $file_name = $this->randomMachineName() . '.txt'; - file_put_contents("public://$file_name", $this->randomString()); + $file_name = 'test_file_field_normalize.txt'; + file_put_contents("public://$file_name", 'hello world'); $file = File::create([ 'uri' => "public://$file_name", )); diff --git a/core/modules/hal/src/Tests/FileNormalizeTest.php b/core/modules/hal/src/Tests/FileNormalizeTest.php index 0a74f3c..82c3200 100644 --- a/core/modules/hal/src/Tests/FileNormalizeTest.php +++ b/core/modules/hal/src/Tests/FileNormalizeTest.php @@ -46,7 +46,7 @@ protected function setUp() { $normalizers = array( new FieldNormalizer(), new FieldItemNormalizer(), - new FileEntityNormalizer($link_manager, $entity_manager, \Drupal::moduleHandler()), + new FileEntityNormalizer($link_manager, $entity_manager, \Drupal::moduleHandler(), $this->container->get('file_system')), ); $encoders = array( diff --git a/core/modules/hal/src/Tests/NormalizerTestBase.php b/core/modules/hal/src/Tests/NormalizerTestBase.php index a95cf09..675a251 100644 --- a/core/modules/hal/src/Tests/NormalizerTestBase.php +++ b/core/modules/hal/src/Tests/NormalizerTestBase.php @@ -142,7 +142,7 @@ protected function setUp() { // Set up the mock serializer. $normalizers = array( - new FileEntityNormalizer($link_manager, $this->container->get('entity.manager'), $this->container->get('module_handler')), + new FileEntityNormalizer($link_manager, $this->container->get('entity.manager'), $this->container->get('module_handler'), $this->container->get('file_system')), new ContentEntityNormalizer($link_manager, $this->container->get('entity.manager'), $this->container->get('module_handler')), new EntityReferenceItemNormalizer($link_manager, $chain_resolver), new FieldItemNormalizer(),