diff --git a/core/modules/file/src/Plugin/migrate/source/d6/File.php b/core/modules/file/src/Plugin/migrate/source/d6/File.php index 9ec4887671ee5cafdadeae4eeb03ec208647277d..14a0a6d4cb51e449abcd5d626abe76770804c53b 100644 --- a/core/modules/file/src/Plugin/migrate/source/d6/File.php +++ b/core/modules/file/src/Plugin/migrate/source/d6/File.php @@ -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) diff --git a/core/modules/file/src/Plugin/migrate/source/d7/File.php b/core/modules/file/src/Plugin/migrate/source/d7/File.php index 5e88151229ade20e0f5710a10bb80b61574bfec4..c0da770b70a769ed3ee85ed21418985e54bdc536 100644 --- a/core/modules/file/src/Plugin/migrate/source/d7/File.php +++ b/core/modules/file/src/Plugin/migrate/source/d7/File.php @@ -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'); diff --git a/core/modules/file/tests/src/Kernel/Migrate/d6/MigrateFileTest.php b/core/modules/file/tests/src/Kernel/Migrate/d6/MigrateFileTest.php index 989eb15900c1846cf95ba074ed080c798d3a3b78..a3d4ba5e7c2e48b277ecf98e56047aef8e275d4e 100644 --- a/core/modules/file/tests/src/Kernel/Migrate/d6/MigrateFileTest.php +++ b/core/modules/file/tests/src/Kernel/Migrate/d6/MigrateFileTest.php @@ -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_drupal\Kernel\d6\MigrateDrupal6TestBase; /** - * file migration. + * Test file migration. * * @group migrate_drupal_6 */ @@ -68,8 +67,10 @@ protected function assertEntity($fid, $name, $size, $uri, $type, $uid) { 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 @@ public function testFiles() { // 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 @@ public function testFiles() { // 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 @@ public function testFiles() { $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(); diff --git a/core/modules/file/tests/src/Kernel/Migrate/d7/MigrateFileTest.php b/core/modules/file/tests/src/Kernel/Migrate/d7/MigrateFileTest.php index 38ef4ff32008c02fe5c4bed4fa43647deb17d251..ea32df5e3483eeeb1ad5e074be17252bc53cfc75 100644 --- a/core/modules/file/tests/src/Kernel/Migrate/d7/MigrateFileTest.php +++ b/core/modules/file/tests/src/Kernel/Migrate/d7/MigrateFileTest.php @@ -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 @@ protected function getFileMigrationInfo() { */ 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)); } } diff --git a/core/modules/file/tests/src/Kernel/Plugin/migrate/source/d6/FileTest.php b/core/modules/file/tests/src/Kernel/Plugin/migrate/source/d6/FileTest.php index cc8c9b36faf048ea4fd0c64564a34236de915ea0..33c430e2fdf276d11a5fde7883030b53c5d77eee 100644 --- a/core/modules/file/tests/src/Kernel/Plugin/migrate/source/d6/FileTest.php +++ b/core/modules/file/tests/src/Kernel/Plugin/migrate/source/d6/FileTest.php @@ -46,11 +46,42 @@ public function providerSource() { '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; } diff --git a/core/modules/file/tests/src/Kernel/Plugin/migrate/source/d7/FileTest.php b/core/modules/file/tests/src/Kernel/Plugin/migrate/source/d7/FileTest.php index 8ae0ec41fa16257c0c684b4aa43f217199304309..b588ad789ce5ad19fc99c6be45f4b8dd7b8a57d1 100644 --- a/core/modules/file/tests/src/Kernel/Plugin/migrate/source/d7/FileTest.php +++ b/core/modules/file/tests/src/Kernel/Plugin/migrate/source/d7/FileTest.php @@ -84,15 +84,14 @@ public function providerSource() { ], ]; - // 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 @@ public function providerSource() { '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; } diff --git a/core/modules/migrate_drupal/tests/fixtures/drupal7.php b/core/modules/migrate_drupal/tests/fixtures/drupal7.php index e76ee20f236dafedd5d55ce68f3987f707e494c1..3624a4a92fba806a62e03a7c692558b7eeb757f3 100644 --- a/core/modules/migrate_drupal/tests/fixtures/drupal7.php +++ b/core/modules/migrate_drupal/tests/fixtures/drupal7.php @@ -13373,6 +13373,17 @@ '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( diff --git a/core/modules/migrate_drupal_ui/tests/src/Functional/d6/MigrateUpgrade6Test.php b/core/modules/migrate_drupal_ui/tests/src/Functional/d6/MigrateUpgrade6Test.php index 96aa5579177540ef91e7989a413eb1b87daf35b4..d8ffd0296437255a4a8a806ee7fb09973e9c9fbe 100644 --- a/core/modules/migrate_drupal_ui/tests/src/Functional/d6/MigrateUpgrade6Test.php +++ b/core/modules/migrate_drupal_ui/tests/src/Functional/d6/MigrateUpgrade6Test.php @@ -69,7 +69,7 @@ protected function getEntityCounts() { '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 @@ protected function getEntityCountsIncremental() { $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; diff --git a/core/modules/simpletest/files/image-3.jpg b/core/modules/simpletest/files/image-3.jpg new file mode 100644 index 0000000000000000000000000000000000000000..ace07d078a00bee3b0ab02f7f077e39c23f2aa03 --- /dev/null +++ b/core/modules/simpletest/files/image-3.jpg @@ -0,0 +1,19 @@ +JFIFHHC + + + + %# , #&')*)-0-(0%()(C + + + +(((((((((((((((((((((((((((((((((((((((((((((((((((<P"8!1AQaq"#2B4Cb#!1"A4a ?E |US az>4~LS A:!b\%g8sЬTf`8S Wǂˆ#:!1rOp1  fAwfµr˶䒖Z@Ȏ:۪ᖤB9I\QbMC Yqaq~*1@jϳzheycbe .جZiҸ0 H鷎{?5JH3#r$N@Datq,pF K>2Ű32zAp@iDU. +}xP5Dzs!PsN;km|ƅcs&VI:%92!|F"p]0GjUφF?}ctQ5Tj + ƠU:cQsb]':u0M +pE&NDgf_oN ZTGީif}Q].")̢|Ct t;m}|Έ8)Yi+bhnAd|ƌad</9fR?sK^%BS \rtt5+(x1klLr\YMi +vupA<zXjj'$.1Ч_/uxk֖3n2GQ7 .yzTFrD3 l}B DMw ƚ}[ymQ$QH F7 `ȗX@ӂH3~Q>AJ] + c軳S6?ԺCԿ +tIp:fRA-\UAS$?v2gFq;tnwo zDNVU.Ĝw +}l.~$RV0#n޸37;|7 \ +R+xiKut Edn9x4sOc f|_~GF2TP<[o zx"2]䨅,SSͦgg5–EtIp]#؝0OnUUwo1wG.~l +D %$G