diff -u b/core/lib/Drupal/Core/File/Exception/DirectoryNotReadyException.php b/core/lib/Drupal/Core/File/Exception/DirectoryNotReadyException.php --- b/core/lib/Drupal/Core/File/Exception/DirectoryNotReadyException.php +++ b/core/lib/Drupal/Core/File/Exception/DirectoryNotReadyException.php @@ -1,11 +1,12 @@ fileHandler = new UnmanagedFileHandler( + $this->container->get('file_system') + ); + } + + /** + * @covers ::copy + */ + public function testEnsureFileExistsBeforeCopy() { + // We need to compute the exception message here because it will include + // the 'real' path to the file, which varies with $this->siteDirectory. + $this->setExpectedException( + NonExistentFileException::class, + "File 'public://test.txt' ('{$this->siteDirectory}/files/test.txt') could not be copied because it does not exist" + ); + + $this->fileHandler->copy('public://test.txt', 'public://test-copy.txt'); + } + + /** + * @covers ::copy + * @expectedException \Drupal\Core\File\Exception\DirectoryNotReadyException + * @expectedExceptionMessage The specified file 'public://test.txt' could not be copied because the destination directory is not properly configured. This may be caused by a problem with file or directory permissions + */ + public function testDestinationDirectoryFailureOnCopy() { + touch('public://test.txt'); + // public://subdirectory has not been created, so file_prepare_directory() + // will fail, causing copy() to throw DirectoryNotReadyException. + $this->fileHandler->copy('public://test.txt', 'public://subdirectory/test.txt'); + } + + /** + * @covers ::copy + * @expectedException \Drupal\Core\File\Exception\FileExistsException + * @expectedExceptionMessage File 'public://test.txt' could not be copied because a file by that name already exists in the destination directory ('') + */ + public function testCopyFailureIfFileAlreadyExists() { + $uri = 'public://test.txt'; + touch($uri); + $this->fileHandler->copy($uri, $uri, FileHandlerInterface::FILE_EXISTS_ERROR); + } + + /** + * @covers ::copy + * @expectedException \Drupal\Core\File\Exception\FileException + * @expectedException File 'public://test.txt' cannot be copied because it would overwrite itself + */ + public function testCopyFailureIfSelfOverwrite() { + $uri = 'public://test.txt'; + $this->fileHandler->copy($uri, $uri); + } + + /** + * @covers ::copy + */ + public function testSuccessfulCopy() { + touch('public://test.txt'); + $this->fileHandler->copy('public://test.txt', 'public://test-copy.txt'); + $this->assertFileExists('public://test-copy.txt'); + } + +}