diff --git a/core/lib/Drupal/Core/File/Event/FileUploadSanitizeNameEvent.php b/core/lib/Drupal/Core/File/Event/FileUploadSanitizeNameEvent.php index 41b520121fef21fa1e6158eb360c3e007106153b..3d766e5073e17c17b35c2e7b6c5131ec1d147f7b 100644 --- a/core/lib/Drupal/Core/File/Event/FileUploadSanitizeNameEvent.php +++ b/core/lib/Drupal/Core/File/Event/FileUploadSanitizeNameEvent.php @@ -7,7 +7,7 @@ /** * An event during file upload that lets subscribers sanitize the filename. * - * @see _file_save_upload_single() + * @see \Drupal\file\Upload\FileUploadHandler::handleFileUpload() * @see \Drupal\file\Plugin\rest\resource\FileUploadResource::prepareFilename() * @see \Drupal\system\EventSubscriber\SecurityFileUploadEventSubscriber::sanitizeName() */ diff --git a/core/lib/Drupal/Core/File/FileSystemInterface.php b/core/lib/Drupal/Core/File/FileSystemInterface.php index c1781b6944f4943906feefdbd6ae8eefc42edcb5..d0d305161f0647db11496e7a2c06076a0eed87d3 100644 --- a/core/lib/Drupal/Core/File/FileSystemInterface.php +++ b/core/lib/Drupal/Core/File/FileSystemInterface.php @@ -361,7 +361,7 @@ public function move($source, $destination, $replace = self::EXISTS_RENAME); /** * Saves a file to the specified destination without invoking file API. * - * This function is identical to file_save_data() except the file will not be + * This function is identical to writeData() except the file will not be * saved to the {file_managed} table and none of the file_* hooks will be * called. * diff --git a/core/modules/file/file.module b/core/modules/file/file.module index 97c25bfdafca186f0c2be97e3563ecf71799e137..f136985529f5cc55df3c251362891310e5dea739 100644 --- a/core/modules/file/file.module +++ b/core/modules/file/file.module @@ -10,7 +10,6 @@ use Drupal\Core\Datetime\Entity\DateFormat; use Drupal\Core\Entity\EntityStorageInterface; use Drupal\Core\Field\FieldDefinitionInterface; -use Drupal\Core\File\Event\FileUploadSanitizeNameEvent; use Drupal\Core\File\Exception\FileException; use Drupal\Core\File\Exception\FileExistsException; use Drupal\Core\File\Exception\FileWriteException; @@ -36,16 +35,6 @@ // cspell:ignore abiword -/** - * The regex pattern used when checking for insecure file types. - * - * @deprecated in drupal:9.2.0 and is removed from drupal:10.0.0. Use - * \Drupal\Core\File\FileSystemInterface::INSECURE_EXTENSION_REGEX. - * - * @see https://www.drupal.org/node/3032541 - */ -define('FILE_INSECURE_EXTENSION_REGEX', FileSystemInterface::INSECURE_EXTENSION_REGEX); - // Load all Field module hooks for File. require_once __DIR__ . '/file.field.inc'; @@ -85,143 +74,6 @@ function file_field_widget_info_alter(array &$info) { $info['uri']['field_types'][] = 'file_uri'; } -/** - * Copies a file to a new location and adds a file record to the database. - * - * This function should be used when manipulating files that have records - * stored in the database. This is a powerful function that in many ways - * performs like an advanced version of copy(). - * - Checks if $source and $destination are valid and readable/writable. - * - If file already exists in $destination either the call will error out, - * replace the file or rename the file based on the $replace parameter. - * - If the $source and $destination are equal, the behavior depends on the - * $replace parameter. FileSystemInterface::EXISTS_REPLACE will error out. - * FileSystemInterface::EXISTS_RENAME will rename the file until the - * $destination is unique. - * - Adds the new file to the files database. If the source file is a - * temporary file, the resulting file will also be a temporary file. See - * file_save_upload() for details on temporary files. - * - * @param \Drupal\file\FileInterface $source - * A file entity. - * @param string $destination - * A string containing the destination that $source should be - * copied to. This must be a stream wrapper URI. - * @param int $replace - * (optional) Replace behavior when the destination file already exists. - * Possible values include: - * - FileSystemInterface::EXISTS_REPLACE: Replace the existing file. If a - * managed file with the destination name exists, then its database entry - * will be updated. If no database entry is found, then a new one will be - * created. - * - FileSystemInterface::EXISTS_RENAME: (default) Append - * _{incrementing number} until the filename is unique. - * - FileSystemInterface::EXISTS_ERROR: Do nothing and return FALSE. - * - * @return \Drupal\file\FileInterface|false - * File entity if the copy is successful, or FALSE in the event of an error. - * - * @throws \Drupal\Core\Entity\EntityStorageException - * Thrown when there is an error updating the file storage. - * - * @deprecated in drupal:9.3.0 and is removed from drupal:10.0.0. Use - * \Drupal\file\FileRepositoryInterface::copy() instead. - * - * @see https://www.drupal.org/node/3223520 - * @see \Drupal\file\FileRepositoryInterface::copy() - * @see \Drupal\Core\File\FileSystemInterface::copy() - * @see hook_file_copy() - */ -function file_copy(FileInterface $source, $destination = NULL, $replace = FileSystemInterface::EXISTS_RENAME) { - @trigger_error(__FUNCTION__ . ' is deprecated in drupal:9.3.0 and will be removed in drupal:10.0.0. Use \Drupal\file\FileRepositoryInterface::copy() instead. See https://www.drupal.org/node/3223520', E_USER_DEPRECATED); - if (empty($destination)) { - $destination = \Drupal::config('system.file')->get('default_scheme') . '://'; - } - /** @var \Drupal\file\FileRepositoryInterface $file_repository */ - $file_repository = \Drupal::service('file.repository'); - try { - return $file_repository->copy($source, $destination, $replace); - } - catch (InvalidStreamWrapperException $e) { - if (($realpath = \Drupal::service('file_system')->realpath($source->getFileUri())) !== FALSE) { - \Drupal::logger('file')->notice('File %file (%realpath) could not be copied because the destination %destination is invalid. This is often caused by improper use of file_copy() or a missing stream wrapper.', ['%file' => $source->getFileUri(), '%realpath' => $realpath, '%destination' => $destination]); - } - else { - \Drupal::logger('file')->notice('File %file could not be copied because the destination %destination is invalid. This is often caused by improper use of file_copy() or a missing stream wrapper.', ['%file' => $source->getFileUri(), '%destination' => $destination]); - } - \Drupal::messenger()->addError(t('The specified file %file could not be copied because the destination is invalid. More information is available in the system log.', ['%file' => $source->getFileUri()])); - return FALSE; - } - catch (FileException $e) { - return FALSE; - } -} - -/** - * Moves a file to a new location and update the file's database entry. - * - * - Checks if $source and $destination are valid and readable/writable. - * - Performs a file move if $source is not equal to $destination. - * - If file already exists in $destination either the call will error out, - * replace the file or rename the file based on the $replace parameter. - * - Adds the new file to the files database. - * - * @param \Drupal\file\FileInterface $source - * A file entity. - * @param string $destination - * A string containing the destination that $source should be moved - * to. This must be a stream wrapper URI. - * @param int $replace - * (optional) The replace behavior when the destination file already exists. - * Possible values include: - * - FileSystemInterface::EXISTS_REPLACE: Replace the existing file. If a - * managed file with the destination name exists then its database entry - * will be updated and $source->delete() called after invoking - * hook_file_move(). If no database entry is found, then the source files - * record will be updated. - * - FileSystemInterface::EXISTS_RENAME: (default) Append - * _{incrementing number} until the filename is unique. - * - FileSystemInterface::EXISTS_ERROR: Do nothing and return FALSE. - * - * @return \Drupal\file\FileInterface|false - * Resulting file entity for success, or FALSE in the event of an error. - * - * @throws \Drupal\Core\Entity\EntityStorageException - * Thrown when there is an error updating the file storage. - * - * @deprecated in drupal:9.3.0 and is removed from drupal:10.0.0. Use - * \Drupal\file\FileRepositoryInterface::move() instead. - * - * @see https://www.drupal.org/node/3223520 - * @see \Drupal\file\FileRepositoryInterface::move() - * @see \Drupal\Core\File\FileSystemInterface::move() - * @see hook_file_move() - */ -function file_move(FileInterface $source, $destination = NULL, $replace = FileSystemInterface::EXISTS_RENAME) { - @trigger_error(__FUNCTION__ . ' is deprecated in drupal:9.3.0 and will be removed in drupal:10.0.0. Use \Drupal\file\FileRepositoryInterface::move() instead. See https://www.drupal.org/node/3223520', E_USER_DEPRECATED); - if (empty($destination)) { - $destination = \Drupal::config('system.file')->get('default_scheme') . '://'; - } - /** @var \Drupal\file\FileRepositoryInterface $file_repository */ - $file_repository = \Drupal::service('file.repository'); - try { - return $file_repository->move($source, $destination, $replace); - } - catch (InvalidStreamWrapperException $e) { - if (($realpath = \Drupal::service('file_system')->realpath($source->getFileUri())) !== FALSE) { - \Drupal::logger('file')->notice('File %file (%realpath) could not be moved because the destination %destination is invalid. This may be caused by improper use of file_move() or a missing stream wrapper.', ['%file' => $source->getFileUri(), '%realpath' => $realpath, '%destination' => $destination]); - } - else { - \Drupal::logger('file')->notice('File %file could not be moved because the destination %destination is invalid. This may be caused by improper use of file_move() or a missing stream wrapper.', ['%file' => $source->getFileUri(), '%destination' => $destination]); - } - \Drupal::messenger()->addError(t('The specified file %file could not be moved because the destination is invalid. More information is available in the system log.', ['%file' => $source->getFileUri()])); - return FALSE; - } - catch (FileException $e) { - return FALSE; - } -} - /** * Checks that a file meets the criteria specified by the validators. * @@ -258,10 +110,10 @@ function file_validate(FileInterface $file, $validators = []) { $errors = array_merge($errors, \Drupal::moduleHandler()->invokeAll('file_validate', [$file])); // Ensure the file does not contain a malicious extension. At this point - // _file_save_upload_single() will have munged the file so it does not contain - // a malicious extension. Contributed and custom code that calls this method - // needs to take similar steps if they need to permit files with malicious - // extensions to be uploaded. + // \Drupal\file\Upload\FileUploadHandler::handleFileUpload() will have munged + // the file so it does not contain a malicious extension. Contributed and + // custom code that calls this method needs to take similar steps if they need + // to permit files with malicious extensions to be uploaded. if (empty($errors) && !\Drupal::config('system.file')->get('allow_insecure_uploads') && preg_match(FileSystemInterface::INSECURE_EXTENSION_REGEX, $file->getFilename())) { $errors[] = t('For security reasons, your upload has been rejected.'); } @@ -482,59 +334,6 @@ function file_validate_image_resolution(FileInterface $file, $maximum_dimensions return $errors; } -/** - * Saves a file to the specified destination and creates a database entry. - * - * @param string $data - * A string containing the contents of the file. - * @param string|null $destination - * (optional) A string containing the destination URI. This must be a stream - * wrapper URI. If no value or NULL is provided, a randomized name will be - * generated and the file will be saved using Drupal's default files scheme, - * usually "public://". - * @param int $replace - * (optional) The replace behavior when the destination file already exists. - * Possible values include: - * - FileSystemInterface::EXISTS_REPLACE: Replace the existing file. If a - * managed file with the destination name exists, then its database entry - * will be updated. If no database entry is found, then a new one will be - * created. - * - FileSystemInterface::EXISTS_RENAME: (default) Append - * _{incrementing number} until the filename is unique. - * - FileSystemInterface::EXISTS_ERROR: Do nothing and return FALSE. - * - * @return \Drupal\file\FileInterface|false - * A file entity, or FALSE on error. - * - * @throws \Drupal\Core\Entity\EntityStorageException - * Thrown when there is an error updating the file storage. - * - * @deprecated in drupal:9.3.0 and is removed from drupal:10.0.0. Use - * \Drupal\file\FileRepositoryInterface::writeData() instead. - * - * @see https://www.drupal.org/node/3223520 - * @see \Drupal\Core\File\FileSystemInterface::saveData() - */ -function file_save_data($data, $destination = NULL, $replace = FileSystemInterface::EXISTS_RENAME) { - @trigger_error(__FUNCTION__ . ' is deprecated in drupal:9.3.0 and will be removed in drupal:10.0.0. Use \Drupal\file\FileRepositoryInterface::writeData() instead. See https://www.drupal.org/node/3223520', E_USER_DEPRECATED); - if (empty($destination)) { - $destination = \Drupal::config('system.file')->get('default_scheme') . '://'; - } - /** @var \Drupal\file\FileRepositoryInterface $fileRepository */ - $fileRepository = \Drupal::service('file.repository'); - try { - return $fileRepository->writeData($data, $destination, $replace); - } - catch (InvalidStreamWrapperException $e) { - \Drupal::logger('file')->notice('The data could not be saved because the destination %destination is invalid. This may be caused by improper use of file_save_data() or a missing stream wrapper.', ['%destination' => $destination]); - \Drupal::messenger()->addError(t('The data could not be saved because the destination is invalid. More information is available in the system log.')); - return FALSE; - } - catch (FileException $e) { - return FALSE; - } -} - /** * Examines a file entity and returns appropriate content headers for download. * @@ -914,252 +713,6 @@ function file_save_upload($form_field_name, $validators = [], $destination = FAL return isset($delta) ? $files[$delta] : $files; } -/** - * Saves a file upload to a new location. - * - * @param \SplFileInfo $file_info - * The file upload to save. - * @param string $form_field_name - * A string that is the associative array key of the upload form element in - * the form array. - * @param array $validators - * (optional) An associative array of callback functions used to validate the - * file. - * @param bool $destination - * (optional) A string containing the URI that the file should be copied to. - * @param int $replace - * (optional) The replace behavior when the destination file already exists. - * - * @return \Drupal\file\FileInterface|false - * The created file entity or FALSE if the uploaded file not saved. - * - * @throws \Drupal\Core\Entity\EntityStorageException - * - * @internal - * This method should only be called from file_save_upload(). Use that method - * instead. - * - * @deprecated in drupal:9.3.0 and is removed from drupal:10.0.0. Use - * \Drupal\file\Upload\FileUploadHandler::handleFileUpload() instead. - * - * @see https://www.drupal.org/node/3239547 - * @see file_save_upload() - */ -function _file_save_upload_single(\SplFileInfo $file_info, $form_field_name, $validators = [], $destination = FALSE, $replace = FileSystemInterface::EXISTS_REPLACE) { - @trigger_error(__METHOD__ . '() is deprecated in drupal:9.3.0 and is removed from drupal:10.0.0. Use \Drupal\file\Upload\FileUploadHandler::handleFileUpload() instead. See https://www.drupal.org/node/3239547', E_USER_DEPRECATED); - $user = \Drupal::currentUser(); - // Remember the original filename so we can print a message if it changes. - $original_file_name = $file_info->getClientOriginalName(); - // Check for file upload errors and return FALSE for this file if a lower - // level system error occurred. For a complete list of errors: - // See http://php.net/manual/features.file-upload.errors.php. - switch ($file_info->getError()) { - case UPLOAD_ERR_INI_SIZE: - case UPLOAD_ERR_FORM_SIZE: - \Drupal::messenger()->addError(t('The file %file could not be saved because it exceeds %maxsize, the maximum allowed size for uploads.', ['%file' => $original_file_name, '%maxsize' => format_size(Environment::getUploadMaxSize())])); - return FALSE; - - case UPLOAD_ERR_PARTIAL: - case UPLOAD_ERR_NO_FILE: - \Drupal::messenger()->addError(t('The file %file could not be saved because the upload did not complete.', ['%file' => $original_file_name])); - return FALSE; - - case UPLOAD_ERR_OK: - // Final check that this is a valid upload, if it isn't, use the - // default error handler. - if (is_uploaded_file($file_info->getRealPath())) { - break; - } - - default: - // Unknown error - \Drupal::messenger()->addError(t('The file %file could not be saved. An unknown error has occurred.', ['%file' => $original_file_name])); - return FALSE; - - } - - // Build a list of allowed extensions. - $extensions = ''; - if (isset($validators['file_validate_extensions'])) { - if (isset($validators['file_validate_extensions'][0])) { - // Build the list of non-munged extensions if the caller provided them. - $extensions = $validators['file_validate_extensions'][0]; - } - else { - // If 'file_validate_extensions' is set and the list is empty then the - // caller wants to allow any extension. In this case we have to remove the - // validator or else it will reject all extensions. - unset($validators['file_validate_extensions']); - } - } - else { - // No validator was provided, so add one using the default list. - // Build a default non-munged safe list for - // \Drupal\system\EventSubscriber\SecurityFileUploadEventSubscriber::sanitizeName(). - $extensions = 'jpg jpeg gif png txt doc xls pdf ppt pps odt ods odp'; - $validators['file_validate_extensions'] = []; - $validators['file_validate_extensions'][0] = $extensions; - } - - // If the destination is not provided, use the temporary directory. - if (empty($destination)) { - $destination = 'temporary://'; - } - - /** @var \Drupal\Core\StreamWrapper\StreamWrapperManagerInterface $stream_wrapper_manager */ - $stream_wrapper_manager = \Drupal::service('stream_wrapper_manager'); - - // Assert that the destination contains a valid stream. - $destination_scheme = $stream_wrapper_manager::getScheme($destination); - if (!$stream_wrapper_manager->isValidScheme($destination_scheme)) { - \Drupal::messenger()->addError(t('The file could not be uploaded because the destination %destination is invalid.', ['%destination' => $destination])); - return FALSE; - } - - // A file URI may already have a trailing slash or look like "public://". - if (substr($destination, -1) != '/') { - $destination .= '/'; - } - - // Call an event to sanitize the filename and to attempt to address security - // issues caused by common server setups. - $event = new FileUploadSanitizeNameEvent($original_file_name, $extensions); - \Drupal::service('event_dispatcher')->dispatch($event); - - // Begin building the file entity. - $values = [ - 'uid' => $user->id(), - 'status' => 0, - // This will be replaced later with a filename based on the destination. - 'filename' => $event->getFilename(), - 'uri' => $file_info->getRealPath(), - 'filesize' => $file_info->getSize(), - ]; - $file = File::create($values); - - /** @var \Drupal\Core\File\FileSystemInterface $file_system */ - $file_system = \Drupal::service('file_system'); - try { - // Use the result of the sanitization event as the destination name. - $file->destination = $file_system->getDestinationFilename($destination . $event->getFilename(), $replace); - } - catch (FileException $e) { - \Drupal::messenger()->addError(t('The file %filename could not be uploaded because the name is invalid.', ['%filename' => $file->getFilename()])); - return FALSE; - } - - $guesser = \Drupal::service('file.mime_type.guesser'); - $file->setMimeType($guesser->guessMimeType($values['filename'])); - $file->source = $form_field_name; - - // If the destination is FALSE then $replace === FILE_EXISTS_ERROR and - // there's an existing file, so we need to bail. - if ($file->destination === FALSE) { - \Drupal::messenger()->addError(t('The file %source could not be uploaded because a file by that name already exists in the destination %directory.', ['%source' => $form_field_name, '%directory' => $destination])); - return FALSE; - } - - // Add in our check of the file name length. - $validators['file_validate_name_length'] = []; - - // Call the validation functions specified by this function's caller. - $errors = file_validate($file, $validators); - - // Check for errors. - if (!empty($errors)) { - $message = [ - 'error' => [ - '#markup' => t('The specified file %name could not be uploaded.', ['%name' => $file->getFilename()]), - ], - 'item_list' => [ - '#theme' => 'item_list', - '#items' => $errors, - ], - ]; - // @todo Add support for render arrays in - // \Drupal\Core\Messenger\MessengerInterface::addMessage()? - // @see https://www.drupal.org/node/2505497. - \Drupal::messenger()->addError(\Drupal::service('renderer')->renderPlain($message)); - return FALSE; - } - - $file->setFileUri($file->destination); - if (!$file_system->moveUploadedFile($file_info->getRealPath(), $file->getFileUri())) { - \Drupal::messenger()->addError(t('File upload error. Could not move uploaded file.')); - \Drupal::logger('file')->notice('Upload error. Could not move uploaded file %file to destination %destination.', ['%file' => $file->getFilename(), '%destination' => $file->getFileUri()]); - return FALSE; - } - - // Update the filename with any changes as a result of the renaming due to an - // existing file. - $file->setFilename(\Drupal::service('file_system')->basename($file->destination)); - - // If the filename has been modified, let the user know. - if ($file->getFilename() !== $original_file_name) { - if ($event->isSecurityRename()) { - $message = t('For security reasons, your upload has been renamed to %filename.', ['%filename' => $file->getFilename()]); - } - else { - $message = t('Your upload has been renamed to %filename.', ['%filename' => $file->getFilename()]); - } - \Drupal::messenger()->addStatus($message); - } - - // Set the permissions on the new file. - $file_system->chmod($file->getFileUri()); - - // If we are replacing an existing file re-use its database record. - // @todo Do not create a new entity in order to update it. See - // https://www.drupal.org/node/2241865. - if ($replace == FileSystemInterface::EXISTS_REPLACE) { - $existing_files = \Drupal::entityTypeManager()->getStorage('file')->loadByProperties(['uri' => $file->getFileUri()]); - if (count($existing_files)) { - $existing = reset($existing_files); - $file->fid = $existing->id(); - $file->setOriginalId($existing->id()); - } - } - - // Update the filename with any changes as a result of security or renaming - // due to an existing file. - $file->setFilename(\Drupal::service('file_system')->basename($file->destination)); - - // We can now validate the file object itself before it's saved. - $violations = $file->validate(); - foreach ($violations as $violation) { - $errors[] = $violation->getMessage(); - } - if (!empty($errors)) { - $message = [ - 'error' => [ - '#markup' => t('The specified file %name could not be uploaded.', ['%name' => $file->getFilename()]), - ], - 'item_list' => [ - '#theme' => 'item_list', - '#items' => $errors, - ], - ]; - // @todo Add support for render arrays in - // \Drupal\Core\Messenger\MessengerInterface::addMessage()? - // @see https://www.drupal.org/node/2505497. - \Drupal::messenger()->addError(\Drupal::service('renderer')->renderPlain($message)); - return FALSE; - } - - // If we made it this far it's safe to record this file in the database. - $file->save(); - - // Allow an anonymous user who creates a non-public file to see it. See - // \Drupal\file\FileAccessControlHandler::checkAccess(). - if ($user->isAnonymous() && $destination_scheme !== 'public') { - $session = \Drupal::request()->getSession(); - $allowed_temp_files = $session->get('anonymous_allowed_file_ids', []); - $allowed_temp_files[$file->id()] = $file->id(); - $session->set('anonymous_allowed_file_ids', $allowed_temp_files); - } - return $file; -} - /** * Determines the preferred upload progress implementation. * @@ -1778,32 +1331,3 @@ function file_get_file_references(FileInterface $file, FieldDefinitionInterface } return $return; } - -/** - * Formats human-readable version of file status. - * - * @param int|null $choice - * (optional) An integer status code. If not set, all statuses are returned. - * Defaults to NULL. - * - * @return \Drupal\Core\StringTranslation\TranslatableMarkup|\Drupal\Core\StringTranslation\TranslatableMarkup[] - * An array of file statuses or a specified status if $choice is set. - * - * @deprecated in drupal:9.3.0 and is removed from drupal:10.0.0. - * There is no replacement. - * - * @see https://www.drupal.org/node/3227228 - */ -function _views_file_status($choice = NULL) { - @trigger_error('_views_file_status() is deprecated in drupal:9.3.0 and is removed from drupal:10.0.0. There is no replacement. See https://www.drupal.org/node/3227228', E_USER_DEPRECATED); - $status = [ - 0 => t('Temporary'), - FileInterface::STATUS_PERMANENT => t('Permanent'), - ]; - - if (isset($choice)) { - return $status[$choice] ?? t('Unknown'); - } - - return $status; -} diff --git a/core/modules/file/src/Plugin/rest/resource/FileUploadResource.php b/core/modules/file/src/Plugin/rest/resource/FileUploadResource.php index 16f7683b9c91c2cef956f4507a4dee2ee3d546f8..32fdd9ac8a0ff3449fcd8a3762b7429ca85457c1 100644 --- a/core/modules/file/src/Plugin/rest/resource/FileUploadResource.php +++ b/core/modules/file/src/Plugin/rest/resource/FileUploadResource.php @@ -278,7 +278,7 @@ public function post(Request $request, $entity_type_id, $bundle, $field_name) { // Validate the file against field-level validators first while the file is // still a temporary file. Validation is split up in 2 steps to be the same - // as in _file_save_upload_single(). + // as in \Drupal\file\Upload\FileUploadHandler::handleFileUpload(). // For backwards compatibility this part is copied from ::validate() to // leave that method behavior unchanged. // @todo Improve this with a file uploader service in diff --git a/core/modules/file/tests/src/Kernel/LegacyFileTest.php b/core/modules/file/tests/src/Kernel/LegacyFileTest.php deleted file mode 100644 index 67370850bcb97de8f2cb7c23d1a30498fb4d94af..0000000000000000000000000000000000000000 --- a/core/modules/file/tests/src/Kernel/LegacyFileTest.php +++ /dev/null @@ -1,90 +0,0 @@ -<?php - -namespace Drupal\Tests\file\Kernel; - -use Drupal\Component\Utility\Environment; -use Drupal\Core\File\FileSystemInterface; -use Drupal\Core\StringTranslation\TranslatableMarkup; -use Symfony\Component\HttpFoundation\File\UploadedFile; - -/** - * Tests deprecated file functions. - * - * @group file - * @group legacy - */ -class LegacyFileTest extends FileManagedUnitTestBase { - - /** - * Tests file size upload errors. - * - * @throws \Drupal\Core\Entity\EntityStorageException - */ - public function testFileSaveUploadSingleErrorFormSize() { - $file_name = $this->randomMachineName(); - $file_info = $this->createMock(UploadedFile::class); - $file_info->expects($this->once())->method('getError')->willReturn(UPLOAD_ERR_FORM_SIZE); - $file_info->expects($this->once())->method('getClientOriginalName')->willReturn($file_name); - $this->expectDeprecation('_file_save_upload_single() is deprecated in drupal:9.3.0 and is removed from drupal:10.0.0. Use \Drupal\file\Upload\FileUploadHandler::handleFileUpload() instead. See https://www.drupal.org/node/3239547'); - $this->assertFalse(\_file_save_upload_single($file_info, 'name')); - $expected_message = new TranslatableMarkup('The file %file could not be saved because it exceeds %maxsize, the maximum allowed size for uploads.', ['%file' => $file_name, '%maxsize' => format_size(Environment::getUploadMaxSize())]); - $this->assertEquals($expected_message, \Drupal::messenger()->all()['error'][0]); - } - - /** - * Tests the deprecation of _views_file_status(). - * - * @group legacy - */ - public function testViewsFileStatus() { - $this->expectDeprecation('_views_file_status() is deprecated in drupal:9.3.0 and is removed from drupal:10.0.0. There is no replacement. See https://www.drupal.org/node/3227228'); - $this->assertIsArray(_views_file_status()); - } - - /** - * Tests file_save_data deprecation and that it works without a destination. - */ - public function testSaveData() { - $this->expectDeprecation('file_save_data is deprecated in drupal:9.3.0 and will be removed in drupal:10.0.0. Use \Drupal\file\FileRepositoryInterface::writeData() instead. See https://www.drupal.org/node/3223520'); - $contents = $this->randomMachineName(8); - $result = file_save_data($contents); - $this->assertNotFalse($result, 'Unnamed file saved correctly.'); - } - - /** - * Tests the file_copy deprecation and legacy behavior. - */ - public function testCopy() { - $this->expectDeprecation('file_copy is deprecated in drupal:9.3.0 and will be removed in drupal:10.0.0. Use \Drupal\file\FileRepositoryInterface::copy() instead. See https://www.drupal.org/node/3223520'); - $contents = $this->randomMachineName(10); - $source = $this->createFile(NULL, $contents); - $desired_uri = 'public://' . $this->randomMachineName(); - - // Clone the object so we don't have to worry about the function changing - // our reference copy. - $result = file_copy(clone $source, $desired_uri, FileSystemInterface::EXISTS_ERROR); - - // Check the return status and that the contents have not changed. - $this->assertNotFalse($result, 'File copied successfully.'); - $this->assertEquals($contents, file_get_contents($result->getFileUri())); - - } - - /** - * Tests the file_copy deprecation and legacy behavior. - */ - public function testMove() { - $this->expectDeprecation('file_move is deprecated in drupal:9.3.0 and will be removed in drupal:10.0.0. Use \Drupal\file\FileRepositoryInterface::move() instead. See https://www.drupal.org/node/3223520'); - $contents = $this->randomMachineName(10); - $source = $this->createFile(NULL, $contents); - $desired_uri = 'public://' . $this->randomMachineName(); - // Clone the object so we don't have to worry about the function changing - // our reference copy. - $result = file_move(clone $source, $desired_uri, FileSystemInterface::EXISTS_ERROR); - - // Check the return status and that the contents have not changed. - $this->assertFileNotExists($source->getFileUri()); - $this->assertEquals($contents, file_get_contents($result->getFileUri())); - } - -} diff --git a/core/modules/jsonapi/src/Controller/TemporaryJsonapiFileFieldUploader.php b/core/modules/jsonapi/src/Controller/TemporaryJsonapiFileFieldUploader.php index 3b8641f7bc359fecdb6310be105e72e2cf7eff04..62e96a57129bb724892cf408e030f87fb64791fb 100644 --- a/core/modules/jsonapi/src/Controller/TemporaryJsonapiFileFieldUploader.php +++ b/core/modules/jsonapi/src/Controller/TemporaryJsonapiFileFieldUploader.php @@ -200,7 +200,7 @@ public function handleFileUploadForField(FieldDefinitionInterface $field_definit // Validate the file against field-level validators first while the file is // still a temporary file. Validation is split up in 2 steps to be the same - // as in _file_save_upload_single(). + // as in \Drupal\file\Upload\FileUploadHandler::handleFileUpload(). // For backwards compatibility this part is copied from ::validate() to // leave that method behavior unchanged. // @todo Improve this with a file uploader service in