3 namespace Drupal\Tests\file\Kernel\Migrate\d6;
5 use Drupal\Component\Utility\Random;
6 use Drupal\file\Entity\File;
7 use Drupal\file\FileInterface;
8 use Drupal\KernelTests\KernelTestBase;
9 use Drupal\Core\Database\Database;
10 use Drupal\Tests\migrate\Kernel\MigrateDumpAlterInterface;
11 use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
16 * @group migrate_drupal_6
18 class MigrateFileTest extends MigrateDrupal6TestBase implements MigrateDumpAlterInterface {
20 use FileMigrationTestTrait;
23 * The filename of a file used to test temporary file migration.
27 protected static $tempFilename;
32 protected function setUp() {
35 $this->setUpMigratedFiles();
39 * Asserts a file entity.
44 * The expected file name.
46 * The expected file size.
48 * The expected file URI.
50 * The expected MIME type.
52 * The expected file owner ID.
54 protected function assertEntity($fid, $name, $size, $uri, $type, $uid) {
55 /** @var \Drupal\file\FileInterface $file */
56 $file = File::load($fid);
57 $this->assertTrue($file instanceof FileInterface);
58 $this->assertIdentical($name, $file->getFilename());
59 $this->assertIdentical($size, $file->getSize());
60 $this->assertIdentical($uri, $file->getFileUri());
61 $this->assertIdentical($type, $file->getMimeType());
62 $this->assertIdentical($uid, $file->getOwnerId());
66 * Tests the Drupal 6 files to Drupal 8 migration.
68 public function testFiles() {
69 $this->assertEntity(1, 'Image1.png', '39325', 'public://image-1.png', 'image/png', '1');
70 $this->assertEntity(2, 'Image2.jpg', '1831', 'public://image-2.jpg', 'image/jpeg', '1');
71 $this->assertEntity(3, 'Image-test.gif', '183', 'public://image-test.gif', 'image/jpeg', '1');
72 $this->assertEntity(4, 'html-1.txt', '24', 'public://html-1.txt', 'text/plain', '1');
74 $map_table = $this->getMigration('d6_file')->getIdMap()->mapTableName();
75 $map = \Drupal::database()
76 ->select($map_table, 'm')
77 ->fields('m', ['sourceid1', 'destid1'])
81 // The 4 files from the fixture.
86 // The file updated in migrateDumpAlter().
88 // The file created in migrateDumpAlter().
91 $this->assertEquals($map_expected, $map);
93 // Test that we can re-import and also test with file_directory_path set.
95 ->truncate($map_table)
98 // Update the file_directory_path.
99 Database::getConnection('default', 'migrate')
101 ->fields(['value' => serialize('files/test')])
102 ->condition('name', 'file_directory_path')
104 Database::getConnection('default', 'migrate')
106 ->fields(['value' => serialize(file_directory_temp())])
107 ->condition('name', 'file_directory_temp')
110 $this->executeMigration('d6_file');
112 // File 2, when migrated for the second time, is treated as a different file
113 // (due to having a different uri this time) and is given fid 6.
114 $file = File::load(6);
115 $this->assertIdentical('public://core/modules/simpletest/files/image-2.jpg', $file->getFileUri());
117 $map_table = $this->getMigration('d6_file')->getIdMap()->mapTableName();
118 $map = \Drupal::database()
119 ->select($map_table, 'm')
120 ->fields('m', ['sourceid1', 'destid1'])
124 // The 4 files from the fixture.
129 // The file updated in migrateDumpAlter().
131 // The files created in migrateDumpAlter().
135 $this->assertEquals($map_expected, $map);
137 // File 6, created in static::migrateDumpAlter(), shares a path with
138 // file 4, which means it should be skipped entirely. If it was migrated
139 // then it would have an fid of 9.
140 $this->assertNull(File::load(9));
142 $this->assertEquals(8, count(File::loadMultiple()));
147 * A filename based upon the test.
149 public static function getUniqueFilename() {
150 return static::$tempFilename;
156 public static function migrateDumpAlter(KernelTestBase $test) {
157 // Creates a random filename and updates the source database.
158 $random = new Random();
159 $temp_directory = file_directory_temp();
160 file_prepare_directory($temp_directory, FILE_CREATE_DIRECTORY);
161 static::$tempFilename = $test->getDatabasePrefix() . $random->name() . '.jpg';
162 $file_path = $temp_directory . '/' . static::$tempFilename;
163 file_put_contents($file_path, '');
165 $db = Database::getConnection('default', 'migrate');
168 ->condition('fid', 6)
170 'filename' => static::$tempFilename,
171 'filepath' => $file_path,
175 $file = (array) $db->select('files')
177 ->condition('fid', 5)
181 $db->insert('files')->fields($file)->execute();
183 return static::$tempFilename;