Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / file / tests / src / Kernel / Migrate / d7 / MigratePrivateFileTest.php
1 <?php
2
3 namespace Drupal\Tests\file\Kernel\Migrate\d7;
4
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;
9
10 /**
11  * Tests private files migration.
12  *
13  * @group file
14  */
15 class MigratePrivateFileTest extends MigrateDrupal7TestBase {
16
17   /**
18    * {@inheritdoc}
19    */
20   public static $modules = ['file'];
21
22   /**
23    * {@inheritdoc}
24    */
25   protected function setUp() {
26     parent::setUp();
27     $this->setSetting('file_private_path', $this->container->get('site.path') . '/private');
28     $this->installEntitySchema('file');
29     $fs = $this->container->get('file_system');
30
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));
35
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);
44   }
45
46   /**
47    * {@inheritdoc}
48    */
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']);
53   }
54
55   /**
56    * Tests a single file entity.
57    *
58    * @param int $id
59    *   The file ID.
60    * @param string $name
61    *   The expected file name.
62    * @param string $uri
63    *   The expected URI.
64    * @param string $mime
65    *   The expected MIME type.
66    * @param int $size
67    *   The expected file size.
68    * @param int $created
69    *   The expected creation time.
70    * @param int $changed
71    *   The expected modification time.
72    * @param int $uid
73    *   The expected owner ID.
74    */
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());
89   }
90
91   /**
92    * Tests that all expected files are migrated.
93    */
94   public function testFileMigration() {
95     $this->assertEntity(3, 'Babylon5.txt', 'private://Babylon5.txt', 'text/plain', '3', '1486104045', '1486104045', '1');
96   }
97
98 }