diff --git a/core/modules/migrate/src/Tests/MigrateDumpAlterInterface.php b/core/modules/migrate/src/Tests/MigrateDumpAlterInterface.php new file mode 100644 index 0000000..94b8a3d --- /dev/null +++ b/core/modules/migrate/src/Tests/MigrateDumpAlterInterface.php @@ -0,0 +1,28 @@ +loadDumps($files); + if ($this instanceof MigrateDumpAlterInterface) { + static::migrateDumpAlter($this); + } } /** diff --git a/core/modules/migrate_drupal/src/Tests/MigrateFullDrupalTestBase.php b/core/modules/migrate_drupal/src/Tests/MigrateFullDrupalTestBase.php index 956a4e6..09dd3cd 100644 --- a/core/modules/migrate_drupal/src/Tests/MigrateFullDrupalTestBase.php +++ b/core/modules/migrate_drupal/src/Tests/MigrateFullDrupalTestBase.php @@ -54,6 +54,11 @@ public function testDrupal() { $this->loadDumps($dumps); $classes = $this->getTestClassesList(); + foreach ($classes as $class) { + if (is_subclass_of($class, '\Drupal\migrate\Tests\MigrateDumpAlterInterface')) { + $class::migrateDumpAlter($this); + } + } // Run every migration in the order specified by the storage controller. foreach (entity_load_multiple('migration', static::$migrations) as $migration) { diff --git a/core/modules/migrate_drupal/src/Tests/d6/MigrateDrupal6Test.php b/core/modules/migrate_drupal/src/Tests/d6/MigrateDrupal6Test.php index a644a99..33835a7 100644 --- a/core/modules/migrate_drupal/src/Tests/d6/MigrateDrupal6Test.php +++ b/core/modules/migrate_drupal/src/Tests/d6/MigrateDrupal6Test.php @@ -158,9 +158,6 @@ protected function setUp() { $config->set('default', 'bartik'); $config->set('admin', 'seven'); $config->save(); - - // We need a temp file for testing the MigrateFileTest. - file_put_contents('/tmp/some-temp-file.jpg', ''); } /** diff --git a/core/modules/migrate_drupal/src/Tests/d6/MigrateFileTest.php b/core/modules/migrate_drupal/src/Tests/d6/MigrateFileTest.php index 342c077..91560a4 100644 --- a/core/modules/migrate_drupal/src/Tests/d6/MigrateFileTest.php +++ b/core/modules/migrate_drupal/src/Tests/d6/MigrateFileTest.php @@ -7,16 +7,26 @@ namespace Drupal\migrate_drupal\Tests\d6; +use Drupal\Component\Utility\Random; use Drupal\migrate\MigrateExecutable; +use Drupal\migrate\Tests\MigrateDumpAlterInterface; use Drupal\migrate_drupal\Tests\d6\MigrateDrupal6TestBase; use Drupal\Core\Database\Database; +use Drupal\simpletest\TestBase; /** * file migration. * * @group migrate_drupal */ -class MigrateFileTest extends MigrateDrupal6TestBase { +class MigrateFileTest extends MigrateDrupal6TestBase implements MigrateDumpAlterInterface { + + /** + * The filename of a file used to test temporary file migration. + * + * @var string + */ + protected static $tempFilename; /** * Modules to enable. @@ -29,9 +39,6 @@ class MigrateFileTest extends MigrateDrupal6TestBase { * {@inheritdoc} */ protected function setUp() { - // Set the temp file of the site to the same as the D6 site, this allows us - // to test files which start and finish in the same place. - $this->tempFilesDirectory = '/tmp'; parent::setUp(); $dumps = array( $this->getDumpDirectory() . '/Files.php', @@ -45,7 +52,6 @@ protected function setUp() { $executable = new MigrateExecutable($migration, $this); $executable->import(); $this->standalone = TRUE; - file_put_contents('/tmp/some-temp-file.jpg', ''); } /** @@ -79,7 +85,7 @@ public function testFiles() { ->execute(); Database::getConnection('default', 'migrate') ->update('variable') - ->fields(array('value' => serialize('/tmp'))) + ->fields(array('value' => serialize($this->getTempFilesDirectory()))) ->condition('name', 'file_directory_temp') ->execute(); $executable = new MigrateExecutable($migration, $this); @@ -90,7 +96,37 @@ public function testFiles() { // Ensure that a temporary file has been migrated. $file = entity_load('file', 6); - $this->assertIdentical('temporary://some-temp-file.jpg', $file->getFileUri()); + $this->assertIdentical('temporary://' . static::getUniqueFilename(), $file->getFileUri()); + } + + /** + * @return string + * A filename based upon the test. + */ + public static function getUniqueFilename() { + return static::$tempFilename; + } + + /** + * {@inheritdoc} + */ + public static function migrateDumpAlter(TestBase $test) { + // Creates a random filename and updates the source database. + $random = new Random(); + $temp_directory = $test->getTempFilesDirectory(); + static::$tempFilename = $test->getDatabasePrefix() . $random->name() . '.jpg'; + $file_path = $temp_directory . '/' . static::$tempFilename; + file_put_contents($file_path, ''); + Database::getConnection('default', 'migrate') + ->update('files') + ->condition('fid', 6) + ->fields(array( + 'filename' => static::$tempFilename, + 'filepath' => $file_path, + )) + ->execute(); + + return static::$tempFilename; } } diff --git a/core/modules/simpletest/src/TestBase.php b/core/modules/simpletest/src/TestBase.php index a95bade..9a38774 100644 --- a/core/modules/simpletest/src/TestBase.php +++ b/core/modules/simpletest/src/TestBase.php @@ -1652,4 +1652,24 @@ protected function config($name) { return \Drupal::configFactory()->getEditable($name); } + /** + * Gets the database prefix. + * + * @return string + * The database prefix + */ + public function getDatabasePrefix() { + return $this->databasePrefix; + } + + /** + * Gets the temporary files directory. + * + * @return string + * The temporary files directory. + */ + public function getTempFilesDirectory() { + return $this->tempFilesDirectory; + } + }