You are here

trait FileMigrationSetupTrait in Drupal 9

Same name and namespace in other branches
  1. 8 core/modules/file/tests/src/Kernel/Migrate/d7/FileMigrationSetupTrait.php \Drupal\Tests\file\Kernel\Migrate\d7\FileMigrationSetupTrait

A trait to setup the file migration.

Hierarchy

4 files declare their use of FileMigrationSetupTrait
FollowUpMigrationsTest.php in core/modules/migrate_drupal/tests/src/Kernel/d7/FollowUpMigrationsTest.php
MigrateNodeCompleteTest.php in core/modules/node/tests/src/Kernel/Migrate/d7/MigrateNodeCompleteTest.php
MigrateNodeRevisionTest.php in core/modules/node/tests/src/Kernel/Migrate/d7/MigrateNodeRevisionTest.php
MigrateNodeTest.php in core/modules/node/tests/src/Kernel/Migrate/d7/MigrateNodeTest.php

File

core/modules/file/tests/src/Kernel/Migrate/d7/FileMigrationSetupTrait.php, line 11

Namespace

Drupal\Tests\file\Kernel\Migrate\d7
View source
trait FileMigrationSetupTrait {

  /**
   * Returns information about the file to be migrated.
   *
   * @return array
   *   Array with keys 'path', 'size', 'base_path', and 'plugin_id'.
   */
  protected abstract function getFileMigrationInfo();

  /**
   * Prepare the file migration for running.
   */
  protected function fileMigrationSetup() {
    $this
      ->installEntitySchema('file');
    $this
      ->installSchema('file', [
      'file_usage',
    ]);
    $info = $this
      ->getFileMigrationInfo();
    $fs = $this->container
      ->get('file_system');

    // Ensure that the files directory exists.
    $fs
      ->mkdir(dirname($info['path']), NULL, TRUE);

    // Put test file in the source directory.
    file_put_contents($info['path'], str_repeat('*', $info['size']));

    /** @var \Drupal\migrate\Plugin\Migration $migration */
    $migration = $this
      ->getMigration($info['plugin_id']);

    // Set the source plugin's source_base_path configuration value, which
    // would normally be set by the user running the migration.
    $source = $migration
      ->getSourceConfiguration();
    $source['constants']['source_base_path'] = $fs
      ->realpath($info['base_path']);
    $migration
      ->set('source', $source);
    $this
      ->executeMigration($migration);
  }

  /**
   * Tests a single file entity.
   *
   * @param int $id
   *   The file ID.
   * @param string $name
   *   The expected file name.
   * @param string $uri
   *   The expected URI.
   * @param string $mime
   *   The expected MIME type.
   * @param string $size
   *   The expected file size.
   * @param string $created
   *   The expected creation time.
   * @param string $changed
   *   The expected modification time.
   * @param string $uid
   *   The expected owner ID.
   */
  protected function assertEntity($id, $name, $uri, $mime, $size, $created, $changed, $uid) {

    /** @var \Drupal\file\FileInterface $file */
    $file = File::load($id);
    $this
      ->assertInstanceOf(FileInterface::class, $file);
    $this
      ->assertSame($name, $file
      ->getFilename());
    $this
      ->assertSame($uri, $file
      ->getFileUri());
    $this
      ->assertFileExists($uri);
    $this
      ->assertSame($mime, $file
      ->getMimeType());
    $this
      ->assertSame($size, $file
      ->getSize());

    // isPermanent(), isTemporary(), etc. are determined by the status column.
    $this
      ->assertTrue($file
      ->isPermanent());
    $this
      ->assertSame($created, $file
      ->getCreatedTime());
    $this
      ->assertSame($changed, $file
      ->getChangedTime());
    $this
      ->assertSame($uid, $file
      ->getOwnerId());
  }

}

Members

Namesort descending Modifiers Type Description Overrides
FileMigrationSetupTrait::assertEntity protected function Tests a single file entity. 1
FileMigrationSetupTrait::fileMigrationSetup protected function Prepare the file migration for running.
FileMigrationSetupTrait::getFileMigrationInfo abstract protected function Returns information about the file to be migrated. 6