Version 1
[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\Component\Utility\Random;
6 use Drupal\file\Entity\File;
7 use Drupal\file\FileInterface;
8 use Drupal\KernelTests\KernelTestBase;
9 use Drupal\Core\Database\Database;
10 use Drupal\Tests\migrate\Kernel\MigrateDumpAlterInterface;
11 use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
12
13 /**
14  * file migration.
15  *
16  * @group migrate_drupal_6
17  */
18 class MigrateFileTest extends MigrateDrupal6TestBase implements MigrateDumpAlterInterface {
19
20   use FileMigrationTestTrait;
21
22   /**
23    * The filename of a file used to test temporary file migration.
24    *
25    * @var string
26    */
27   protected static $tempFilename;
28
29   /**
30    * {@inheritdoc}
31    */
32   protected function setUp() {
33     parent::setUp();
34
35     $this->setUpMigratedFiles();
36   }
37
38   /**
39    * Asserts a file entity.
40    *
41    * @param int $fid
42    *   The file ID.
43    * @param string $name
44    *   The expected file name.
45    * @param int $size
46    *   The expected file size.
47    * @param string $uri
48    *   The expected file URI.
49    * @param string $type
50    *   The expected MIME type.
51    * @param int $uid
52    *   The expected file owner ID.
53    */
54   protected function assertEntity($fid, $name, $size, $uri, $type, $uid) {
55     /** @var \Drupal\file\FileInterface $file */
56     $file = File::load($fid);
57     $this->assertTrue($file instanceof FileInterface);
58     $this->assertIdentical($name, $file->getFilename());
59     $this->assertIdentical($size, $file->getSize());
60     $this->assertIdentical($uri, $file->getFileUri());
61     $this->assertIdentical($type, $file->getMimeType());
62     $this->assertIdentical($uid, $file->getOwnerId());
63   }
64
65   /**
66    * Tests the Drupal 6 files to Drupal 8 migration.
67    */
68   public function testFiles() {
69     $this->assertEntity(1, 'Image1.png', '39325', 'public://image-1.png', 'image/png', '1');
70     $this->assertEntity(2, 'Image2.jpg', '1831', 'public://image-2.jpg', 'image/jpeg', '1');
71     $this->assertEntity(3, 'Image-test.gif', '183', 'public://image-test.gif', 'image/jpeg', '1');
72     $this->assertEntity(4, 'html-1.txt', '24', 'public://html-1.txt', 'text/plain', '1');
73
74     $map_table = $this->getMigration('d6_file')->getIdMap()->mapTableName();
75     $map = \Drupal::database()
76       ->select($map_table, 'm')
77       ->fields('m', ['sourceid1', 'destid1'])
78       ->execute()
79       ->fetchAllKeyed();
80     $map_expected = [
81       // The 4 files from the fixture.
82       1 => '1',
83       2 => '2',
84       3 => '3',
85       5 => '4',
86       // The file updated in migrateDumpAlter().
87       6 => NULL,
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       3 => '7',
128       5 => '8',
129       // The file updated in migrateDumpAlter().
130       6 => NULL,
131       // The files created in migrateDumpAlter().
132       7 => '8',
133       8 => '8',
134     ];
135     $this->assertEquals($map_expected, $map);
136
137     // File 6, created in static::migrateDumpAlter(), shares a path with
138     // file 4, which means it should be skipped entirely. If it was migrated
139     // then it would have an fid of 9.
140     $this->assertNull(File::load(9));
141
142     $this->assertEquals(8, count(File::loadMultiple()));
143   }
144
145   /**
146    * @return string
147    *   A filename based upon the test.
148    */
149   public static function getUniqueFilename() {
150     return static::$tempFilename;
151   }
152
153   /**
154    * {@inheritdoc}
155    */
156   public static function migrateDumpAlter(KernelTestBase $test) {
157     // Creates a random filename and updates the source database.
158     $random = new Random();
159     $temp_directory = file_directory_temp();
160     file_prepare_directory($temp_directory, FILE_CREATE_DIRECTORY);
161     static::$tempFilename = $test->getDatabasePrefix() . $random->name() . '.jpg';
162     $file_path = $temp_directory . '/' . static::$tempFilename;
163     file_put_contents($file_path, '');
164
165     $db = Database::getConnection('default', 'migrate');
166
167     $db->update('files')
168       ->condition('fid', 6)
169       ->fields([
170         'filename' => static::$tempFilename,
171         'filepath' => $file_path,
172       ])
173       ->execute();
174
175     $file = (array) $db->select('files')
176       ->fields('files')
177       ->condition('fid', 5)
178       ->execute()
179       ->fetchObject();
180     unset($file['fid']);
181     $db->insert('files')->fields($file)->execute();
182
183     return static::$tempFilename;
184   }
185
186 }