diff -u b/core/modules/migrate/src/Plugin/migrate/process/FileCopy.php b/core/modules/migrate/src/Plugin/migrate/process/FileCopy.php --- b/core/modules/migrate/src/Plugin/migrate/process/FileCopy.php +++ b/core/modules/migrate/src/Plugin/migrate/process/FileCopy.php @@ -94,17 +94,19 @@ } $replace = $this->getOverwriteMode(); - // We attempt the copy first to avoid calling file_prepare_directory() any - // more than absolutely necessary. - if ($this->writeFile($source, $destination, $replace)) { - return $destination; + // We attempt the copy or move first to avoid calling file_prepare_directory() + // any more than absolutely necessary. + $final_destination = $this->writeFile($source, $destination, $replace); + if ($final_destination) { + return $final_destination; } $dir = $this->getDirectory($destination); if (!file_prepare_directory($dir, FILE_CREATE_DIRECTORY | FILE_MODIFY_PERMISSIONS)) { throw new MigrateException("Could not create or write to directory '$dir'"); } - if ($this->writeFile($source, $destination, $replace)) { - return $destination; + $final_destination = $this->writeFile($source, $destination, $replace); + if ($final_destination) { + return $final_destination; } throw new MigrateException("File $source could not be copied to $destination"); } @@ -119,15 +121,20 @@ * @param int $replace * (optional) FILE_EXISTS_REPLACE (default) or FILE_EXISTS_RENAME. * - * @return bool - * TRUE on success, FALSE on failure. + * @return bool|string + * FALSE on failure, destination path on success. */ protected function writeFile($source, $destination, $replace = FILE_EXISTS_REPLACE) { if ($this->configuration['move']) { - return (boolean) file_unmanaged_move($source, $destination, $replace); + if ((boolean) file_unmanaged_move($source, $destination, $replace)) { + return $destination; + } } - $destination = file_destination($destination, $replace); - return @copy($source, $destination); + $final_destination = file_destination($destination, $replace); + if (@copy($source, $final_destination)) { + return $final_destination; + } + return FALSE; } /** diff -u b/core/modules/migrate/tests/src/Unit/process/CopyFileTest.php b/core/modules/migrate/tests/src/Unit/process/CopyFileTest.php --- b/core/modules/migrate/tests/src/Unit/process/CopyFileTest.php +++ b/core/modules/migrate/tests/src/Unit/process/CopyFileTest.php @@ -60,9 +60,10 @@ ]; foreach ($data_sets as $data) { list($source_path, $destination_path) = $data; - $this->doImport($source_path, $destination_path); - $message = sprintf('File %s exists', $destination_path); - $this->assertTrue(is_file($destination_path), $message); + $final_destination = $this->doImport($source_path, $destination_path); + $this->assertSame($final_destination, $destination_path); + $message = sprintf('File %s exists', $final_destination); + $this->assertTrue(is_file($final_destination), $message); // Make sure we didn't accidentally do a move. $this->assertTrue(is_file($source_path), $message); } @@ -96,9 +97,10 @@ ]; foreach ($data_sets as $data) { list($source_path, $destination_path) = $data; - $this->doImport($source_path, $destination_path, ['move' => TRUE]); - $message = sprintf('File %s exists', $destination_path); - $this->assertTrue(is_file($destination_path), $message); + $final_destination = $this->doImport($source_path, $destination_path, ['move' => TRUE]); + $this->assertSame($final_destination, $destination_path); + $message = sprintf('File %s exists', $final_destination); + $this->assertTrue(is_file($final_destination), $message); $message = sprintf('File %s does not exist', $source_path); $this->assertTrue(!is_file($source_path), $message); } @@ -123,8 +125,9 @@ $source = $this->createUri(NULL, NULL, 'temporary'); $destination = $this->createUri('foo.txt', NULL, 'public'); $expected_destination = 'public://foo_0.txt'; - $this->doImport($source, $destination, ['rename' => TRUE]); - $this->assertFileExists($expected_destination); + $final_destination = $this->doImport($source, $destination, ['rename' => TRUE]); + $this->assertSame($expected_destination,$final_destination); + $this->assertFileExists($final_destination); } /** @@ -137,6 +140,9 @@ * @param array $configuration * Process plugin configuration settings. * + * @return string $final_destination + * The actual final destination path. + * * @throws \Drupal\migrate\MigrateException */ protected function doImport($source_path, $destination_path, $configuration = []) { @@ -144,11 +150,9 @@ $executable = $this->prophesize(MigrateExecutableInterface::class)->reveal(); $row = new Row([], []); - $result = $plugin->transform([$source_path, $destination_path], $executable, $row, 'foobaz'); - // The plugin should either throw an exception or return the destination // path. - $this->assertSame($result, $destination_path); + return $plugin->transform([$source_path, $destination_path], $executable, $row, 'foobaz'); } }