Issue #2729369 by quietone, Jo Fitzgerald, maxocub, mikeryan, heddn: Remove support for migrating temporary files

8.7.x
Nathaniel Catchpole 2018-08-10 15:24:42 +09:00
parent 237b3f4112
commit 953b7c7302
9 changed files with 121 additions and 39 deletions

View File

@ -42,6 +42,7 @@ class File extends DrupalSqlBase {
public function query() {
return $this->select('files', 'f')
->fields('f')
->condition('filepath', '/tmp%', 'NOT LIKE')
->orderBy('timestamp')
// If two or more files have the same timestamp, they'll end up in a
// non-deterministic order. Ordering by fid (or any other unique field)

View File

@ -43,18 +43,21 @@ class File extends DrupalSqlBase {
public function query() {
$query = $this->select('file_managed', 'f')
->fields('f')
->condition('uri', 'temporary://%', 'NOT LIKE')
->orderBy('f.timestamp');
// Filter by scheme(s), if configured.
if (isset($this->configuration['scheme'])) {
$schemes = [];
// Remove 'temporary' scheme.
$valid_schemes = array_diff((array) $this->configuration['scheme'], ['temporary']);
// Accept either a single scheme, or a list.
foreach ((array) $this->configuration['scheme'] as $scheme) {
foreach ((array) $valid_schemes as $scheme) {
$schemes[] = rtrim($scheme) . '://';
}
$schemes = array_map([$this->getDatabase(), 'escapeLike'], $schemes);
// uri LIKE 'public://%' OR uri LIKE 'private://%'
// Add conditions, uri LIKE 'public://%' OR uri LIKE 'private://%'.
$conditions = new Condition('OR');
foreach ($schemes as $scheme) {
$conditions->condition('uri', $scheme . '%', 'LIKE');

View File

@ -2,7 +2,6 @@
namespace Drupal\Tests\file\Kernel\Migrate\d6;
use Drupal\Component\Utility\Random;
use Drupal\file\Entity\File;
use Drupal\file\FileInterface;
use Drupal\KernelTests\KernelTestBase;
@ -11,7 +10,7 @@ use Drupal\Tests\migrate\Kernel\MigrateDumpAlterInterface;
use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
/**
* file migration.
* Test file migration.
*
* @group migrate_drupal_6
*/
@ -68,8 +67,10 @@ class MigrateFileTest extends MigrateDrupal6TestBase implements MigrateDumpAlter
public function testFiles() {
$this->assertEntity(1, 'Image1.png', '39325', 'public://image-1.png', 'image/png', '1');
$this->assertEntity(2, 'Image2.jpg', '1831', 'public://image-2.jpg', 'image/jpeg', '1');
$this->assertEntity(3, 'Image-test.gif', '183', 'public://image-test.gif', 'image/jpeg', '1');
$this->assertEntity(3, 'image-3.jpg', '1831', 'public://image-3.jpg', 'image/jpeg', '1');
$this->assertEntity(4, 'html-1.txt', '24', 'public://html-1.txt', 'text/plain', '1');
// Ensure temporary file was not migrated.
$this->assertNull(File::load(6));
$map_table = $this->getMigration('d6_file')->getIdMap()->mapTableName();
$map = \Drupal::database()
@ -81,10 +82,9 @@ class MigrateFileTest extends MigrateDrupal6TestBase implements MigrateDumpAlter
// The 4 files from the fixture.
1 => '1',
2 => '2',
// The file updated in migrateDumpAlter().
3 => '3',
5 => '4',
// The file updated in migrateDumpAlter().
6 => NULL,
// The file created in migrateDumpAlter().
7 => '4',
];
@ -124,10 +124,9 @@ class MigrateFileTest extends MigrateDrupal6TestBase implements MigrateDumpAlter
// The 4 files from the fixture.
1 => '5',
2 => '6',
// The file updated in migrateDumpAlter().
3 => '7',
5 => '8',
// The file updated in migrateDumpAlter().
6 => NULL,
// The files created in migrateDumpAlter().
7 => '8',
8 => '8',
@ -142,33 +141,17 @@ class MigrateFileTest extends MigrateDrupal6TestBase implements MigrateDumpAlter
$this->assertEquals(8, count(File::loadMultiple()));
}
/**
* @return string
* A filename based upon the test.
*/
public static function getUniqueFilename() {
return static::$tempFilename;
}
/**
* {@inheritdoc}
*/
public static function migrateDumpAlter(KernelTestBase $test) {
// Creates a random filename and updates the source database.
$random = new Random();
$temp_directory = file_directory_temp();
file_prepare_directory($temp_directory, FILE_CREATE_DIRECTORY);
static::$tempFilename = $test->getDatabasePrefix() . $random->name() . '.jpg';
$file_path = $temp_directory . '/' . static::$tempFilename;
file_put_contents($file_path, '');
$db = Database::getConnection('default', 'migrate');
$db->update('files')
->condition('fid', 6)
->condition('fid', 3)
->fields([
'filename' => static::$tempFilename,
'filepath' => $file_path,
'filename' => 'image-3.jpg',
'filepath' => 'core/modules/simpletest/files/image-3.jpg',
])
->execute();

View File

@ -2,6 +2,7 @@
namespace Drupal\Tests\file\Kernel\Migrate\d7;
use Drupal\file\Entity\File;
use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
/**
@ -43,6 +44,8 @@ class MigrateFileTest extends MigrateDrupal7TestBase {
*/
public function testFileMigration() {
$this->assertEntity(1, 'cube.jpeg', 'public://cube.jpeg', 'image/jpeg', '3620', '1421727515', '1421727515', '1');
// Ensure temporary file was not migrated.
$this->assertNull(File::load(4));
}
}

View File

@ -46,11 +46,42 @@ class FileTest extends MigrateSqlSourceTestBase {
'status' => 1,
'timestamp' => 1382255662,
],
[
'fid' => 3,
'uid' => 1,
'filename' => 'migrate-test-file-3.pdf',
'filepath' => '/tmp/migrate-test-file-3.pdf',
'filemime' => 'application/pdf',
'filesize' => 304124,
'status' => 1,
'timestamp' => 1382277662,
],
];
// The expected results are identical to the source data.
$tests[0]['expected_data'] = $tests[0]['source_data']['files'];
// The expected results are the same as the source data but excluding
// the temporary file.
$tests[0]['expected_data'] = [
[
'fid' => 1,
'uid' => 1,
'filename' => 'migrate-test-file-1.pdf',
'filepath' => 'sites/default/files/migrate-test-file-1.pdf',
'filemime' => 'application/pdf',
'filesize' => 890404,
'status' => 1,
'timestamp' => 1382255613,
],
[
'fid' => 2,
'uid' => 1,
'filename' => 'migrate-test-file-2.pdf',
'filepath' => 'sites/default/files/migrate-test-file-2.pdf',
'filemime' => 'application/pdf',
'filesize' => 204124,
'status' => 1,
'timestamp' => 1382255662,
],
];
return $tests;
}

View File

@ -84,15 +84,14 @@ class FileTest extends MigrateSqlSourceTestBase {
],
];
// The expected results will include only the first three files, since we
// are configuring the plugin to filter out the file with the null URI
// scheme.
$tests[0]['expected_data'] = array_slice($tests[0]['source_data']['file_managed'], 0, 3);
// The expected results will include only the first two files, since the
// plugin will filter out files with either the null URI scheme or the
// temporary scheme.
$tests[0]['expected_data'] = array_slice($tests[0]['source_data']['file_managed'], 0, 2);
// The filepath property will vary by URI scheme.
$tests[0]['expected_data'][0]['filepath'] = 'sites/default/files/cube.jpeg';
$tests[0]['expected_data'][1]['filepath'] = '/path/to/private/files/cube.jpeg';
$tests[0]['expected_data'][2]['filepath'] = '/tmp/cube.jpeg';
// Do an automatic count.
$tests[0]['expected_count'] = NULL;
@ -102,10 +101,61 @@ class FileTest extends MigrateSqlSourceTestBase {
'constants' => [
'source_base_path' => '/path/to/files',
],
// Only return files which use one of these URI schemes.
'scheme' => ['public', 'private', 'temporary'],
];
// Test getting only public files.
$tests[1]['source_data'] = $tests[0]['source_data'];
$tests[1]['expected_data'] = [
[
'fid' => '1',
'uid' => '1',
'filename' => 'cube.jpeg',
'uri' => 'public://cube.jpeg',
'filemime' => 'image/jpeg',
'filesize' => '3620',
'status' => '1',
'timestamp' => '1421727515',
],
];
// Do an automatic count.
$tests[1]['expected_count'] = NULL;
// Set up plugin configuration.
$tests[1]['configuration'] = [
'constants' => [
'source_base_path' => '/path/to/files',
],
'scheme' => ['public'],
];
// Test getting only public files when configuration scheme is not an array.
$tests[2]['source_data'] = $tests[0]['source_data'];
$tests[2]['expected_data'] = [
[
'fid' => '1',
'uid' => '1',
'filename' => 'cube.jpeg',
'uri' => 'public://cube.jpeg',
'filemime' => 'image/jpeg',
'filesize' => '3620',
'status' => '1',
'timestamp' => '1421727515',
],
];
// Do an automatic count.
$tests[2]['expected_count'] = NULL;
// Set up plugin configuration.
$tests[2]['configuration'] = [
'constants' => [
'source_base_path' => '/path/to/files',
],
'scheme' => 'public',
];
return $tests;
}

View File

@ -13373,6 +13373,17 @@ $connection->insert('file_managed')
'status' => '1',
'timestamp' => '1486104045',
))
->values(array(
'fid' => '4',
'uid' => '1',
'filename' => 'TerokNor.txt',
'uri' => 'temporary://TerokNor.txt',
'filemime' => 'text/plain',
'filesize' => '2369',
'status' => '1',
'timestamp' => '1421747516',
))
->execute();
$connection->schema()->createTable('file_usage', array(

View File

@ -69,7 +69,7 @@ class MigrateUpgrade6Test extends MigrateUpgradeExecuteTestBase {
'editor' => 2,
'field_config' => 89,
'field_storage_config' => 63,
'file' => 8,
'file' => 7,
'filter_format' => 7,
'image_style' => 5,
'language_content_settings' => 3,
@ -109,7 +109,7 @@ class MigrateUpgrade6Test extends MigrateUpgradeExecuteTestBase {
$counts['comment'] = 7;
$counts['entity_view_display'] = 55;
$counts['entity_view_mode'] = 14;
$counts['file'] = 9;
$counts['file'] = 8;
$counts['menu_link_content'] = 11;
$counts['node'] = 18;
$counts['taxonomy_term'] = 9;

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.8 KiB