3 namespace Drupal\Tests\file\Kernel\Migrate\d6;
5 use Drupal\file\Entity\File;
6 use Drupal\file\FileInterface;
7 use Drupal\KernelTests\KernelTestBase;
8 use Drupal\Core\Database\Database;
9 use Drupal\Tests\migrate\Kernel\MigrateDumpAlterInterface;
10 use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
13 * Test file migration.
15 * @group migrate_drupal_6
17 class MigrateFileTest extends MigrateDrupal6TestBase implements MigrateDumpAlterInterface {
19 use FileMigrationTestTrait;
22 * The filename of a file used to test temporary file migration.
26 protected static $tempFilename;
31 protected function setUp() {
34 $this->setUpMigratedFiles();
38 * Asserts a file entity.
43 * The expected file name.
45 * The expected file size.
47 * The expected file URI.
49 * The expected MIME type.
51 * The expected file owner ID.
53 protected function assertEntity($fid, $name, $size, $uri, $type, $uid) {
54 /** @var \Drupal\file\FileInterface $file */
55 $file = File::load($fid);
56 $this->assertTrue($file instanceof FileInterface);
57 $this->assertIdentical($name, $file->getFilename());
58 $this->assertIdentical($size, $file->getSize());
59 $this->assertIdentical($uri, $file->getFileUri());
60 $this->assertIdentical($type, $file->getMimeType());
61 $this->assertIdentical($uid, $file->getOwnerId());
65 * Tests the Drupal 6 files to Drupal 8 migration.
67 public function testFiles() {
68 $this->assertEntity(1, 'Image1.png', '39325', 'public://image-1.png', 'image/png', '1');
69 $this->assertEntity(2, 'Image2.jpg', '1831', 'public://image-2.jpg', 'image/jpeg', '1');
70 $this->assertEntity(3, 'image-3.jpg', '1831', 'public://image-3.jpg', 'image/jpeg', '1');
71 $this->assertEntity(4, 'html-1.txt', '24', 'public://html-1.txt', 'text/plain', '1');
72 // Ensure temporary file was not migrated.
73 $this->assertNull(File::load(6));
75 $map_table = $this->getMigration('d6_file')->getIdMap()->mapTableName();
76 $map = \Drupal::database()
77 ->select($map_table, 'm')
78 ->fields('m', ['sourceid1', 'destid1'])
82 // The 4 files from the fixture.
85 // 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.
127 // The file updated in migrateDumpAlter().
130 // The files created in migrateDumpAlter().
134 $this->assertEquals($map_expected, $map);
136 // File 6, created in static::migrateDumpAlter(), shares a path with
137 // file 4, which means it should be skipped entirely. If it was migrated
138 // then it would have an fid of 9.
139 $this->assertNull(File::load(9));
141 $this->assertEquals(8, count(File::loadMultiple()));
147 public static function migrateDumpAlter(KernelTestBase $test) {
148 $db = Database::getConnection('default', 'migrate');
151 ->condition('fid', 3)
153 'filename' => 'image-3.jpg',
154 'filepath' => 'core/modules/simpletest/files/image-3.jpg',
158 $file = (array) $db->select('files')
160 ->condition('fid', 5)
164 $db->insert('files')->fields($file)->execute();
166 return static::$tempFilename;