reverted: --- b/core/includes/file.inc +++ a/core/includes/file.inc @@ -524,7 +524,7 @@ else { // Perhaps $destination is a dir/file? $dirname = drupal_dirname($destination); + if (!file_prepare_directory($dirname)) { - if (!file_prepare_directory($dirname, FILE_CREATE_DIRECTORY)) { // The destination is not valid. $logger->notice('File %file could not be moved/copied because the destination directory %destination is not configured correctly.', array('%file' => $original_source, '%destination' => $dirname)); drupal_set_message(t('The specified file %file could not be moved/copied because the destination directory is not properly configured. This may be caused by a problem with file or directory permissions. More information is available in the system log.', array('%file' => $original_source)), 'error'); diff -u b/core/tests/Drupal/KernelTests/Core/File/UnmanagedCopyTest.php b/core/tests/Drupal/KernelTests/Core/File/UnmanagedCopyTest.php --- b/core/tests/Drupal/KernelTests/Core/File/UnmanagedCopyTest.php +++ b/core/tests/Drupal/KernelTests/Core/File/UnmanagedCopyTest.php @@ -37,7 +37,23 @@ $this->assertTrue(file_exists($newer_filepath), 'New file exists.'); $this->assertFilePermissions($newer_filepath, Settings::get('file_chmod_file', FileSystem::CHMOD_FILE)); - // TODO: test copying to a directory (rather than full directory/file path) + // Copying to an existing directory. + $desired_directory = 'public://' . $this->randomMachineName(); + $this->assertFalse(file_exists($desired_directory), 'Directory does not exist.'); + $this->assertTrue(file_prepare_directory($desired_directory, FILE_CREATE_DIRECTORY), 'Directory was created.'); + $desired_filepath = $desired_directory . DIRECTORY_SEPARATOR . $this->randomMachineName(); + $newer_filepath = file_unmanaged_copy($uri, $desired_filepath, FILE_EXISTS_ERROR); + $this->assertTrue($newer_filepath, 'Copy was successful.'); + $this->assertTrue(file_exists($newer_filepath), 'New file exists.'); + + // Copying to a missing directory. + $desired_directory = 'public://' . $this->randomMachineName(); + $this->assertFalse(file_exists($desired_directory), 'Directory does not exist.'); + $desired_filepath = $desired_directory . DIRECTORY_SEPARATOR . $this->randomMachineName(); + $newer_filepath = file_unmanaged_copy($uri, $desired_filepath, FILE_EXISTS_ERROR); + $this->assertFalse($newer_filepath, 'Copy was not successful.'); + $this->assertFalse(file_exists($newer_filepath), 'New file does not exist.'); + // TODO: test copying normal files using normal paths (rather than only streams) } @@ -84,14 +100,6 @@ $this->assertTrue(file_exists($uri), 'Original file exists after copying onto itself.'); $this->assertTrue(file_exists($new_filepath), 'Copied file exists after copying onto itself.'); $this->assertFilePermissions($new_filepath, Settings::get('file_chmod_file', FileSystem::CHMOD_FILE)); - - // Copy the file into a subdirectory that does not exist. - $new_filepath = file_unmanaged_copy($uri, \Drupal::service('file_system')->dirname($uri) . '/sub/' . \Drupal::service('file_system')->basename($uri), FILE_EXISTS_ERROR); - $this->assertTrue($new_filepath, 'Copying into same directory works.'); - self::assertNotEquals($new_filepath, $uri, 'Copied file has a new name.'); - $this->assertTrue(file_exists($uri), 'Original file exists after copying onto itself.'); - $this->assertTrue(file_exists($new_filepath), 'Copied file exists after copying onto itself.'); - $this->assertFilePermissions($new_filepath, Settings::get('file_chmod_file', FileSystem::CHMOD_FILE)); } } diff -u b/core/tests/Drupal/KernelTests/Core/File/UnmanagedMoveTest.php b/core/tests/Drupal/KernelTests/Core/File/UnmanagedMoveTest.php --- b/core/tests/Drupal/KernelTests/Core/File/UnmanagedMoveTest.php +++ b/core/tests/Drupal/KernelTests/Core/File/UnmanagedMoveTest.php @@ -21,30 +21,46 @@ // Moving to a new name. $desired_filepath = 'public://' . $this->randomMachineName(); $new_filepath = file_unmanaged_move($uri, $desired_filepath, FILE_EXISTS_ERROR); + $this->assertTrue($new_filepath, 'Move was successful.'); $this->assertEqual($new_filepath, $desired_filepath, 'Returned expected filepath.'); - $this->assertFileExists($new_filepath, 'File exists at the new location.'); - $this->assertFileNotExists($uri, 'No file remains at the old location.'); + $this->assertTrue(file_exists($new_filepath), 'File exists at the new location.'); + $this->assertFalse(file_exists($uri), 'No file remains at the old location.'); $this->assertFilePermissions($new_filepath, Settings::get('file_chmod_file', FileSystem::CHMOD_FILE)); // Moving with rename. + $uri = $new_filepath; $desired_filepath = 'public://' . $this->randomMachineName(); - $this->assertTrue(file_exists($new_filepath), 'File exists before moving.'); + $this->assertTrue(file_exists($uri), 'File exists before moving.'); $this->assertTrue(file_put_contents($desired_filepath, ' '), 'Created a file so a rename will have to happen.'); - $newer_filepath = file_unmanaged_move($new_filepath, $desired_filepath, FILE_EXISTS_RENAME); - $this->assertNotEqual($newer_filepath, $desired_filepath, 'Returned expected filepath.'); - $this->assertFileExists($newer_filepath, 'File exists at the new location.'); - $this->assertFileNotExists($new_filepath, 'No file remains at the old location.'); - $this->assertFilePermissions($newer_filepath, Settings::get('file_chmod_file', FileSystem::CHMOD_FILE)); + $new_filepath = file_unmanaged_move($uri, $desired_filepath, FILE_EXISTS_RENAME); + $this->assertTrue($new_filepath, 'Move was successful.'); + $this->assertNotEqual($new_filepath, $desired_filepath, 'Returned expected filepath.'); + $this->assertTrue(file_exists($new_filepath), 'File exists at the new location.'); + $this->assertFalse(file_exists($uri), 'No file remains at the old location.'); + $this->assertFilePermissions($new_filepath, Settings::get('file_chmod_file', FileSystem::CHMOD_FILE)); + + // Moving to an existing directory. + $uri = $new_filepath; + $desired_directory = 'public://' . $this->randomMachineName(); + $this->assertFalse(file_exists($desired_directory), 'Directory does not exist.'); + $this->assertTrue(file_prepare_directory($desired_directory, FILE_CREATE_DIRECTORY), 'Directory was created.'); + $desired_filepath = $desired_directory . DIRECTORY_SEPARATOR . $this->randomMachineName(); + $new_filepath = file_unmanaged_move($uri, $desired_filepath, FILE_EXISTS_ERROR); + $this->assertTrue($new_filepath, 'Move was successful.'); + $this->assertEqual($new_filepath, $desired_filepath, 'Returned expected filepath.'); + $this->assertTrue(file_exists($new_filepath), 'File exists at the new location.'); + $this->assertFalse(file_exists($uri), 'No file remains at the old location.'); - // Moving the file into a subdirectory that does not exist. - $desired_filepath = 'public://sub/' . $this->randomMachineName(); - $newest_filepath = file_unmanaged_move($newer_filepath, $desired_filepath, FILE_EXISTS_ERROR); - self::assertEquals($newest_filepath, $desired_filepath, 'Returned expected filepath.'); - $this->assertFileExists($newest_filepath, 'File exists at the new location.'); - $this->assertFileNotExists($newer_filepath, 'No file remains at the old location.'); - $this->assertFilePermissions($newest_filepath, Settings::get('file_chmod_file', FileSystem::CHMOD_FILE)); + // Moving to a missing directory. + $uri = $new_filepath; + $desired_directory = 'public://' . $this->randomMachineName(); + $this->assertFalse(file_exists($desired_directory), 'Directory does not exist.'); + $desired_filepath = $desired_directory . DIRECTORY_SEPARATOR . $this->randomMachineName(); + $new_filepath = file_unmanaged_copy($uri, $desired_filepath, FILE_EXISTS_ERROR); + $this->assertFalse($new_filepath, 'Move was not successful.'); + $this->assertFalse(file_exists($new_filepath), 'New file does not exist.'); + $this->assertTrue(file_exists($uri), 'Original file remains.'); - // TODO: test moving to a directory (rather than full directory/file path) // TODO: test creating and moving normal files (rather than streams) }