Issue #2706405 by heddn, edysmp, davidparedes21, mikeryan, phenaproxima: File migrations *always* copy in files, even if the files were off-line copied to destination

8.3.x
Nathaniel Catchpole 2016-09-27 22:17:47 +01:00
parent f0d2de33c3
commit 527bf02662
2 changed files with 40 additions and 4 deletions

View File

@ -53,6 +53,7 @@ class FileCopy extends ProcessPluginBase implements ContainerFactoryPluginInterf
$configuration += array(
'move' => FALSE,
'rename' => FALSE,
'reuse' => FALSE,
);
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->streamWrapperManager = $stream_wrappers;
@ -130,8 +131,13 @@ class FileCopy extends ProcessPluginBase implements ContainerFactoryPluginInterf
if ($this->configuration['move']) {
return file_unmanaged_move($source, $destination, $replace);
}
// Check if there is a destination available for copying. If there isn't,
// it already exists at the destination and the replace flag tells us to not
// replace it. In that case, return the original destination.
if (!($final_destination = file_destination($destination, $replace))) {
return $destination;
}
// We can't use file_unmanaged_copy because it will break with remote Urls.
$final_destination = file_destination($destination, $replace);
if (@copy($source, $final_destination)) {
return $final_destination;
}
@ -142,13 +148,17 @@ class FileCopy extends ProcessPluginBase implements ContainerFactoryPluginInterf
* Determines how to handle file conflicts.
*
* @return int
* Either FILE_EXISTS_REPLACE (default) or FILE_EXISTS_RENAME, depending
* on the current configuration.
* FILE_EXISTS_REPLACE (default), FILE_EXISTS_RENAME, or FILE_EXISTS_ERROR
* depending on the current configuration.
*/
protected function getOverwriteMode() {
if (!empty($this->configuration['rename'])) {
return FILE_EXISTS_RENAME;
}
if (!empty($this->configuration['reuse'])) {
return FILE_EXISTS_ERROR;
}
return FILE_EXISTS_REPLACE;
}

View File

@ -4,8 +4,8 @@ namespace Drupal\Tests\migrate\Kernel\process;
use Drupal\Core\StreamWrapper\StreamWrapperInterface;
use Drupal\KernelTests\Core\File\FileTestBase;
use Drupal\migrate\Plugin\migrate\process\FileCopy;
use Drupal\migrate\MigrateExecutableInterface;
use Drupal\migrate\Plugin\migrate\process\FileCopy;
use Drupal\migrate\Row;
/**
@ -70,6 +70,32 @@ class CopyFileTest extends FileTestBase {
}
}
/**
* Test successful file reuse.
*/
public function testSuccessfulReuse() {
$source_path = $this->root . '/core/modules/simpletest/files/image-test.jpg';
$destination_path = 'public://file1.jpg';
$file_reuse = file_unmanaged_copy($source_path, $destination_path);
$timestamp = (new \SplFileInfo($file_reuse))->getMTime();
$this->assertInternalType('int', $timestamp);
// We need to make sure the modified timestamp on the file is sooner than
// the attempted migration.
sleep(1);
$configuration = ['reuse' => TRUE];
$this->doImport($source_path, $destination_path, $configuration);
clearstatcache(TRUE, $destination_path);
$modified_timestamp = (new \SplFileInfo($destination_path))->getMTime();
$this->assertEquals($timestamp, $modified_timestamp);
$configuration = ['reuse' => FALSE];
$this->doImport($source_path, $destination_path, $configuration);
clearstatcache(TRUE, $destination_path);
$modified_timestamp = (new \SplFileInfo($destination_path))->getMTime();
$this->assertGreaterThan($timestamp, $modified_timestamp);
}
/**
* Test successful moves.
*/