3 namespace Drupal\Tests\file\Kernel\Migrate\d7;
5 use Drupal\Core\DependencyInjection\ContainerBuilder;
6 use Drupal\file\Entity\File;
7 use Drupal\file\FileInterface;
8 use Drupal\Tests\migrate_drupal\Kernel\d7\MigrateDrupal7TestBase;
11 * Tests private files migration.
15 class MigratePrivateFileTest extends MigrateDrupal7TestBase {
20 public static $modules = ['file'];
25 protected function setUp() {
27 $this->setSetting('file_private_path', $this->container->get('site.path') . '/private');
28 $this->installEntitySchema('file');
29 $fs = $this->container->get('file_system');
31 // Ensure that the private files directory exists.
32 $fs->mkdir('private://sites/default/private/', NULL, TRUE);
33 // Put test file in the source directory.
34 file_put_contents('private://sites/default/private/Babylon5.txt', str_repeat('*', 3));
36 /** @var \Drupal\migrate\Plugin\Migration $migration */
37 $migration = $this->getMigration('d7_file_private');
38 // Set the source plugin's source_file_private_path configuration value,
39 // which would normally be set by the user running the migration.
40 $source = $migration->getSourceConfiguration();
41 $source['constants']['source_base_path'] = $fs->realpath('private://');
42 $migration->set('source', $source);
43 $this->executeMigration($migration);
49 public function register(ContainerBuilder $container) {
50 parent::register($container);
51 $container->register('stream_wrapper.private', 'Drupal\Core\StreamWrapper\PrivateStream')
52 ->addTag('stream_wrapper', ['scheme' => 'private']);
56 * Tests a single file entity.
61 * The expected file name.
65 * The expected MIME type.
67 * The expected file size.
69 * The expected creation time.
71 * The expected modification time.
73 * The expected owner ID.
75 protected function assertEntity($id, $name, $uri, $mime, $size, $created, $changed, $uid) {
76 /** @var \Drupal\file\FileInterface $file */
77 $file = File::load($id);
78 $this->assertInstanceOf(FileInterface::class, $file);
79 $this->assertSame($name, $file->getFilename());
80 $this->assertSame($uri, $file->getFileUri());
81 $this->assertFileExists($uri);
82 $this->assertSame($mime, $file->getMimeType());
83 $this->assertSame($size, $file->getSize());
84 // isPermanent(), isTemporary(), etc. are determined by the status column.
85 $this->assertTrue($file->isPermanent());
86 $this->assertSame($created, $file->getCreatedTime());
87 $this->assertSame($changed, $file->getChangedTime());
88 $this->assertSame($uid, $file->getOwnerId());
92 * Tests that all expected files are migrated.
94 public function testFileMigration() {
95 $this->assertEntity(3, 'Babylon5.txt', 'private://Babylon5.txt', 'text/plain', '3', '1486104045', '1486104045', '1');