You are here

public function MigrateFileTest::testFiles in Drupal 10

Same name and namespace in other branches
  1. 8 core/modules/file/tests/src/Kernel/Migrate/d6/MigrateFileTest.php \Drupal\Tests\file\Kernel\Migrate\d6\MigrateFileTest::testFiles()
  2. 9 core/modules/file/tests/src/Kernel/Migrate/d6/MigrateFileTest.php \Drupal\Tests\file\Kernel\Migrate\d6\MigrateFileTest::testFiles()

Tests the Drupal 6 files to Drupal 8 migration.

File

core/modules/file/tests/src/Kernel/Migrate/d6/MigrateFileTest.php, line 76

Class

MigrateFileTest
Test file migration.

Namespace

Drupal\Tests\file\Kernel\Migrate\d6

Code

public function testFiles() {
  $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);
  $this
    ->assertEntity(3, 'image-3.jpg', 1831, 'public://image-3.jpg', 'image/jpeg', 1);
  $this
    ->assertEntity(4, 'html-1.txt', 19, 'public://html-1.txt', 'text/plain', 1);

  // Ensure temporary file was not migrated.
  $this
    ->assertNull(File::load(6));
  $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',
    // The file updated in migrateDumpAlter().
    3 => '3',
    5 => '4',
    // The file created in migrateDumpAlter().
    7 => '4',
  ];
  $this
    ->assertEquals($map_expected, $map);

  // Test that we can re-import and also test with file_directory_path set.
  \Drupal::database()
    ->truncate($map_table)
    ->execute();

  // Set the file_directory_path.
  Database::getConnection('default', 'migrate')
    ->insert('variable')
    ->fields([
    'name',
    'value',
  ])
    ->values([
    'name' => 'file_directory_path',
    'value' => serialize('files/test'),
  ])
    ->execute();
  $this
    ->executeMigration('d6_file');

  // 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);
  $this
    ->assertSame('public://core/tests/fixtures/files/image-2.jpg', $file
    ->getFileUri());
  $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',
    // The file updated in migrateDumpAlter().
    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
    ->assertCount(8, File::loadMultiple());
}