9206153d5b8fa05439b71302dad3b367c57c5d60
[yaffs-website] / web / core / modules / file / tests / src / Kernel / Migrate / d7 / FileMigrationSetupTrait.php
1 <?php
2
3 namespace Drupal\Tests\file\Kernel\Migrate\d7;
4
5 use Drupal\file\Entity\File;
6 use Drupal\file\FileInterface;
7
8 /**
9  * A trait to setup the file migration.
10  */
11 trait FileMigrationSetupTrait {
12
13   /**
14    * Returns information about the file to be migrated.
15    *
16    * @return array
17    *   Array with keys 'path', 'size', 'base_path', and 'plugin_id'.
18    */
19   abstract protected function getFileMigrationInfo();
20
21   /**
22    * Prepare the file migration for running.
23    */
24   protected function fileMigrationSetup() {
25     $this->installEntitySchema('file');
26     $this->installSchema('file', ['file_usage']);
27
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']));
34
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);
43   }
44
45   /**
46    * Tests a single file entity.
47    *
48    * @param int $id
49    *   The file ID.
50    * @param string $name
51    *   The expected file name.
52    * @param string $uri
53    *   The expected URI.
54    * @param string $mime
55    *   The expected MIME type.
56    * @param string $size
57    *   The expected file size.
58    * @param string $created
59    *   The expected creation time.
60    * @param string $changed
61    *   The expected modification time.
62    * @param string $uid
63    *   The expected owner ID.
64    */
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());
79   }
80
81 }