MigrateFileTest.php 4.72 KB
Newer Older
1 2
<?php

3
namespace Drupal\Tests\file\Kernel\Migrate\d6;
4

5 6
use Drupal\file\Entity\File;
use Drupal\file\FileInterface;
7
use Drupal\KernelTests\KernelTestBase;
8
use Drupal\Core\Database\Database;
9 10
use Drupal\Tests\migrate\Kernel\MigrateDumpAlterInterface;
use Drupal\Tests\migrate_drupal\Kernel\d6\MigrateDrupal6TestBase;
11 12

/**
13
 * Test file migration.
14
 *
15
 * @group migrate_drupal_6
16
 */
17 18
class MigrateFileTest extends MigrateDrupal6TestBase implements MigrateDumpAlterInterface {

19 20
  use FileMigrationTestTrait;

21 22 23 24 25 26
  /**
   * The filename of a file used to test temporary file migration.
   *
   * @var string
   */
  protected static $tempFilename;
27 28 29 30

  /**
   * {@inheritdoc}
   */
31
  protected function setUp(): void {
32
    parent::setUp();
33

34
    $this->setUpMigratedFiles();
35 36
  }

37 38 39
  /**
   * Asserts a file entity.
   *
40 41
   * @param int $fid
   *   The file ID.
42
   * @param string $name
43 44 45
   *   The expected file name.
   * @param int $size
   *   The expected file size.
46
   * @param string $uri
47
   *   The expected file URI.
48
   * @param string $type
49 50 51
   *   The expected MIME type.
   * @param int $uid
   *   The expected file owner ID.
52 53 54 55
   */
  protected function assertEntity($fid, $name, $size, $uri, $type, $uid) {
    /** @var \Drupal\file\FileInterface $file */
    $file = File::load($fid);
56
    $this->assertInstanceOf(FileInterface::class, $file);
57 58 59 60 61 62 63
    $this->assertIdentical($name, $file->getFilename());
    $this->assertIdentical($size, $file->getSize());
    $this->assertIdentical($uri, $file->getFileUri());
    $this->assertIdentical($type, $file->getMimeType());
    $this->assertIdentical($uid, $file->getOwnerId());
  }

64 65 66 67
  /**
   * Tests the Drupal 6 files to Drupal 8 migration.
   */
  public function testFiles() {
68 69
    $this->assertEntity(1, 'Image1.png', '39325', 'public://image-1.png', 'image/png', '1');
    $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 73
    // Ensure temporary file was not migrated.
    $this->assertNull(File::load(6));
74 75 76 77 78 79 80 81 82 83 84

    $map_table = $this->getMigration('d6_file')->getIdMap()->mapTableName();
    $map = \Drupal::database()
      ->select($map_table, 'm')
      ->fields('m', ['sourceid1', 'destid1'])
      ->execute()
      ->fetchAllKeyed();
    $map_expected = [
      // The 4 files from the fixture.
      1 => '1',
      2 => '2',
85
      // The file updated in migrateDumpAlter().
86 87 88 89 90 91
      3 => '3',
      5 => '4',
      // The file created in migrateDumpAlter().
      7 => '4',
    ];
    $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 99 100
    // Update the file_directory_path.
    Database::getConnection('default', 'migrate')
      ->update('variable')
101
      ->fields(['value' => serialize('files/test')])
102 103
      ->condition('name', 'file_directory_path')
      ->execute();
104

105
    $this->executeMigration('d6_file');
106

107 108 109
    // File 2, when migrated for the second time, is treated as a different file
    // (due to having a different uri this time) and is given fid 6.
    $file = File::load(6);
110
    $this->assertIdentical('public://core/tests/fixtures/files/image-2.jpg', $file->getFileUri());
111

112 113 114 115 116 117 118 119 120 121
    $map_table = $this->getMigration('d6_file')->getIdMap()->mapTableName();
    $map = \Drupal::database()
      ->select($map_table, 'm')
      ->fields('m', ['sourceid1', 'destid1'])
      ->execute()
      ->fetchAllKeyed();
    $map_expected = [
      // The 4 files from the fixture.
      1 => '5',
      2 => '6',
122
      // The file updated in migrateDumpAlter().
123 124 125 126 127 128 129 130 131 132 133 134 135 136
      3 => '7',
      5 => '8',
      // The files created in migrateDumpAlter().
      7 => '8',
      8 => '8',
    ];
    $this->assertEquals($map_expected, $map);

    // File 6, created in static::migrateDumpAlter(), shares a path with
    // file 4, which means it should be skipped entirely. If it was migrated
    // then it would have an fid of 9.
    $this->assertNull(File::load(9));

    $this->assertEquals(8, count(File::loadMultiple()));
137 138 139 140 141
  }

  /**
   * {@inheritdoc}
   */
142
  public static function migrateDumpAlter(KernelTestBase $test) {
143 144 145
    $db = Database::getConnection('default', 'migrate');

    $db->update('files')
146
      ->condition('fid', 3)
147
      ->fields([
148
        'filename' => 'image-3.jpg',
149
        'filepath' => 'core/tests/fixtures/files/image-3.jpg',
150
      ])
151 152
      ->execute();

153 154 155 156 157 158 159 160
    $file = (array) $db->select('files')
      ->fields('files')
      ->condition('fid', 5)
      ->execute()
      ->fetchObject();
    unset($file['fid']);
    $db->insert('files')->fields($file)->execute();

161
    return static::$tempFilename;
162 163 164
  }

}