View source
<?php
namespace Drupal\Tests\file\Kernel\Migrate\d6;
use Drupal\file\Entity\File;
use Drupal\file\FileInterface;
use Drupal\KernelTests\KernelTestBase;
use Drupal\Core\Database\Database;
use Drupal\Tests\migrate\Kernel\MigrateDumpAlterInterface;
use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
class MigrateFileTest extends MigrateDrupal6TestBase implements MigrateDumpAlterInterface {
use FileMigrationTestTrait;
protected static $tempFilename;
protected function setUp() : void {
parent::setUp();
Database::getConnection('default', 'migrate')
->delete('variable')
->condition('name', 'file_directory_path')
->execute();
$this
->setUpMigratedFiles();
}
protected function assertEntity(int $fid, string $name, int $size, string $uri, string $type, int $uid) : void {
$file = File::load($fid);
$this
->assertInstanceOf(FileInterface::class, $file);
$this
->assertSame($name, $file
->getFilename());
$this
->assertSame($size, (int) $file
->getSize());
$this
->assertSame($uri, $file
->getFileUri());
$this
->assertSame($type, $file
->getMimeType());
$this
->assertSame($uid, (int) $file
->getOwnerId());
}
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-3.jpg', 1831, 'public://image-3.jpg', 'image/jpeg', 1);
$this
->assertEntity(4, 'html-1.txt', 19, 'public://html-1.txt', 'text/plain', 1);
$this
->assertNull(File::load(6));
$map_table = $this
->getMigration('d6_file')
->getIdMap()
->mapTableName();
$map = \Drupal::database()
->select($map_table, 'm')
->fields('m', [
'sourceid1',
'destid1',
])
->execute()
->fetchAllKeyed();
$map_expected = [
1 => '1',
2 => '2',
3 => '3',
5 => '4',
7 => '4',
];
$this
->assertEquals($map_expected, $map);
\Drupal::database()
->truncate($map_table)
->execute();
Database::getConnection('default', 'migrate')
->insert('variable')
->fields([
'name',
'value',
])
->values([
'name' => 'file_directory_path',
'value' => serialize('files/test'),
])
->execute();
$this
->executeMigration('d6_file');
$file = File::load(6);
$this
->assertSame('public://core/tests/fixtures/files/image-2.jpg', $file
->getFileUri());
$map_table = $this
->getMigration('d6_file')
->getIdMap()
->mapTableName();
$map = \Drupal::database()
->select($map_table, 'm')
->fields('m', [
'sourceid1',
'destid1',
])
->execute()
->fetchAllKeyed();
$map_expected = [
1 => '5',
2 => '6',
3 => '7',
5 => '8',
7 => '8',
8 => '8',
];
$this
->assertEquals($map_expected, $map);
$this
->assertNull(File::load(9));
$this
->assertCount(8, File::loadMultiple());
}
public static function migrateDumpAlter(KernelTestBase $test) {
$db = Database::getConnection('default', 'migrate');
$db
->update('files')
->condition('fid', 3)
->fields([
'filename' => 'image-3.jpg',
'filepath' => 'core/tests/fixtures/files/image-3.jpg',
])
->execute();
$file = (array) $db
->select('files')
->fields('files')
->condition('fid', 5)
->execute()
->fetchObject();
unset($file['fid']);
$db
->insert('files')
->fields($file)
->execute();
return static::$tempFilename;
}
}