Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / file / tests / src / Kernel / Migrate / d6 / MigrateFileTest.php
1 <?php
2
3 namespace Drupal\Tests\file\Kernel\Migrate\d6;
4
5 use Drupal\file\Entity\File;
6 use Drupal\file\FileInterface;
7 use Drupal\KernelTests\KernelTestBase;
8 use Drupal\Core\Database\Database;
9 use Drupal\Tests\migrate\Kernel\MigrateDumpAlterInterface;
10 use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
11
12 /**
13  * Test file migration.
14  *
15  * @group migrate_drupal_6
16  */
17 class MigrateFileTest extends MigrateDrupal6TestBase implements MigrateDumpAlterInterface {
18
19   use FileMigrationTestTrait;
20
21   /**
22    * The filename of a file used to test temporary file migration.
23    *
24    * @var string
25    */
26   protected static $tempFilename;
27
28   /**
29    * {@inheritdoc}
30    */
31   protected function setUp() {
32     parent::setUp();
33
34     $this->setUpMigratedFiles();
35   }
36
37   /**
38    * Asserts a file entity.
39    *
40    * @param int $fid
41    *   The file ID.
42    * @param string $name
43    *   The expected file name.
44    * @param int $size
45    *   The expected file size.
46    * @param string $uri
47    *   The expected file URI.
48    * @param string $type
49    *   The expected MIME type.
50    * @param int $uid
51    *   The expected file owner ID.
52    */
53   protected function assertEntity($fid, $name, $size, $uri, $type, $uid) {
54     /** @var \Drupal\file\FileInterface $file */
55     $file = File::load($fid);
56     $this->assertTrue($file instanceof FileInterface);
57     $this->assertIdentical($name, $file->getFilename());
58     $this->assertIdentical($size, $file->getSize());
59     $this->assertIdentical($uri, $file->getFileUri());
60     $this->assertIdentical($type, $file->getMimeType());
61     $this->assertIdentical($uid, $file->getOwnerId());
62   }
63
64   /**
65    * Tests the Drupal 6 files to Drupal 8 migration.
66    */
67   public function testFiles() {
68     $this->assertEntity(1, 'Image1.png', '39325', 'public://image-1.png', 'image/png', '1');
69     $this->assertEntity(2, 'Image2.jpg', '1831', 'public://image-2.jpg', 'image/jpeg', '1');
70     $this->assertEntity(3, 'image-3.jpg', '1831', 'public://image-3.jpg', 'image/jpeg', '1');
71     $this->assertEntity(4, 'html-1.txt', '24', 'public://html-1.txt', 'text/plain', '1');
72     // Ensure temporary file was not migrated.
73     $this->assertNull(File::load(6));
74
75     $map_table = $this->getMigration('d6_file')->getIdMap()->mapTableName();
76     $map = \Drupal::database()
77       ->select($map_table, 'm')
78       ->fields('m', ['sourceid1', 'destid1'])
79       ->execute()
80       ->fetchAllKeyed();
81     $map_expected = [
82       // The 4 files from the fixture.
83       1 => '1',
84       2 => '2',
85       // The file updated in migrateDumpAlter().
86       3 => '3',
87       5 => '4',
88       // The file created in migrateDumpAlter().
89       7 => '4',
90     ];
91     $this->assertEquals($map_expected, $map);
92
93     // Test that we can re-import and also test with file_directory_path set.
94     \Drupal::database()
95       ->truncate($map_table)
96       ->execute();
97
98     // Update the file_directory_path.
99     Database::getConnection('default', 'migrate')
100       ->update('variable')
101       ->fields(['value' => serialize('files/test')])
102       ->condition('name', 'file_directory_path')
103       ->execute();
104     Database::getConnection('default', 'migrate')
105       ->update('variable')
106       ->fields(['value' => serialize(file_directory_temp())])
107       ->condition('name', 'file_directory_temp')
108       ->execute();
109
110     $this->executeMigration('d6_file');
111
112     // File 2, when migrated for the second time, is treated as a different file
113     // (due to having a different uri this time) and is given fid 6.
114     $file = File::load(6);
115     $this->assertIdentical('public://core/modules/simpletest/files/image-2.jpg', $file->getFileUri());
116
117     $map_table = $this->getMigration('d6_file')->getIdMap()->mapTableName();
118     $map = \Drupal::database()
119       ->select($map_table, 'm')
120       ->fields('m', ['sourceid1', 'destid1'])
121       ->execute()
122       ->fetchAllKeyed();
123     $map_expected = [
124       // The 4 files from the fixture.
125       1 => '5',
126       2 => '6',
127       // The file updated in migrateDumpAlter().
128       3 => '7',
129       5 => '8',
130       // The files created in migrateDumpAlter().
131       7 => '8',
132       8 => '8',
133     ];
134     $this->assertEquals($map_expected, $map);
135
136     // File 6, created in static::migrateDumpAlter(), shares a path with
137     // file 4, which means it should be skipped entirely. If it was migrated
138     // then it would have an fid of 9.
139     $this->assertNull(File::load(9));
140
141     $this->assertEquals(8, count(File::loadMultiple()));
142   }
143
144   /**
145    * {@inheritdoc}
146    */
147   public static function migrateDumpAlter(KernelTestBase $test) {
148     $db = Database::getConnection('default', 'migrate');
149
150     $db->update('files')
151       ->condition('fid', 3)
152       ->fields([
153         'filename' => 'image-3.jpg',
154         'filepath' => 'core/modules/simpletest/files/image-3.jpg',
155       ])
156       ->execute();
157
158     $file = (array) $db->select('files')
159       ->fields('files')
160       ->condition('fid', 5)
161       ->execute()
162       ->fetchObject();
163     unset($file['fid']);
164     $db->insert('files')->fields($file)->execute();
165
166     return static::$tempFilename;
167   }
168
169 }