3 namespace Drupal\Tests\file\Kernel\Migrate\d7;
5 use Drupal\file\Entity\File;
6 use Drupal\file\FileInterface;
9 * A trait to setup the file migration.
11 trait FileMigrationSetupTrait {
14 * Returns information about the file to be migrated.
17 * Array with keys 'path', 'size', 'base_path', and 'plugin_id'.
19 abstract protected function getFileMigrationInfo();
22 * Prepare the file migration for running.
24 protected function fileMigrationSetup() {
25 $this->installEntitySchema('file');
26 $this->installSchema('file', ['file_usage']);
28 $info = $this->getFileMigrationInfo();
29 $fs = $this->container->get('file_system');
30 // Ensure that the files directory exists.
31 $fs->mkdir(dirname($info['path']), NULL, TRUE);
32 // Put test file in the source directory.
33 file_put_contents($info['path'], str_repeat('*', $info['size']));
35 /** @var \Drupal\migrate\Plugin\Migration $migration */
36 $migration = $this->getMigration($info['plugin_id']);
37 // Set the source plugin's source_base_path configuration value, which
38 // would normally be set by the user running the migration.
39 $source = $migration->getSourceConfiguration();
40 $source['constants']['source_base_path'] = $fs->realpath($info['base_path']);
41 $migration->set('source', $source);
42 $this->executeMigration($migration);
46 * Tests a single file entity.
51 * The expected file name.
55 * The expected MIME type.
57 * The expected file size.
58 * @param string $created
59 * The expected creation time.
60 * @param string $changed
61 * The expected modification time.
63 * The expected owner ID.
65 protected function assertEntity($id, $name, $uri, $mime, $size, $created, $changed, $uid) {
66 /** @var \Drupal\file\FileInterface $file */
67 $file = File::load($id);
68 $this->assertInstanceOf(FileInterface::class, $file);
69 $this->assertSame($name, $file->getFilename());
70 $this->assertSame($uri, $file->getFileUri());
71 $this->assertFileExists($uri);
72 $this->assertSame($mime, $file->getMimeType());
73 $this->assertSame($size, $file->getSize());
74 // isPermanent(), isTemporary(), etc. are determined by the status column.
75 $this->assertTrue($file->isPermanent());
76 $this->assertSame($created, $file->getCreatedTime());
77 $this->assertSame($changed, $file->getChangedTime());
78 $this->assertSame($uid, $file->getOwnerId());