View source  
  <?php
namespace Drupal\Tests\migrate_drupal_ui\Functional\d7;
use Drupal\Core\File\FileSystemInterface;
use Drupal\Tests\ExtensionListTestTrait;
use Drupal\Tests\migrate_drupal_ui\Functional\MigrateUpgradeTestBase;
class FilePathTest extends MigrateUpgradeTestBase {
  use ExtensionListTestTrait;
  
  protected $profile = 'testing';
  
  protected $defaultTheme = 'stark';
  
  protected $fs;
  
  protected $localDirectory = [];
  
  protected $sourceFileScheme = [];
  
  protected static $modules = [
    'file',
    'migrate',
    'migrate_drupal',
    'migrate_drupal_ui',
  ];
  
  protected function setUp() : void {
    parent::setUp();
    $this->fs = \Drupal::service('file_system');
    $this
      ->loadFixture($this
      ->getModulePath('migrate_drupal') . '/tests/fixtures/drupal7.php');
  }
  
  public function testFilePath(string $file_private_path, string $file_public_path, string $file_temporary_path, string $private, string $public, string $temporary) {
    $this->sourceFileScheme['private'] = $file_private_path;
    $this->sourceFileScheme['public'] = $file_public_path;
    $this->sourceFileScheme['temporary'] = $file_temporary_path;
    $this->localDirectory['private'] = $private;
    $this->localDirectory['public'] = $public;
    $this->localDirectory['temporary'] = $temporary;
    
    $this
      ->makeFiles();
    
    $this->sourceDatabase
      ->update('variable')
      ->fields([
      'value' => serialize($file_private_path),
    ])
      ->condition('name', 'file_private_path')
      ->execute();
    $this->sourceDatabase
      ->update('variable')
      ->fields([
      'value' => serialize($file_public_path),
    ])
      ->condition('name', 'file_public_path')
      ->execute();
    $this->sourceDatabase
      ->update('variable')
      ->fields([
      'value' => serialize($file_temporary_path),
    ])
      ->condition('name', 'file_temporary_path')
      ->execute();
    $connection_options = $this->sourceDatabase
      ->getConnectionOptions();
    $driver = $connection_options['driver'];
    
    $drivers = drupal_get_database_types();
    $form = $drivers[$driver]
      ->getFormOptions($connection_options);
    $connection_options = array_intersect_key($connection_options, $form + $form['advanced_options']);
    $edit = [
      $driver => $connection_options,
      'version' => '7',
    ];
    if (count($drivers) !== 1) {
      $edit['driver'] = $driver;
    }
    
    $edit['source_private_file_path'] = $this->fs
      ->realpath($this
      ->getSourcePath('private'));
    $edit['source_base_path'] = $this->fs
      ->realpath($this
      ->getSourcePath('public'));
    $edits = $this
      ->translatePostValues($edit);
    
    $this
      ->drupalGet('/upgrade');
    $this
      ->submitForm([], 'Continue');
    $this
      ->submitForm($edits, 'Review upgrade');
    
    $store = \Drupal::service('tempstore.private')
      ->get('migrate_drupal_ui');
    $migration_array = array_intersect_key($store
      ->get('migrations'), array_flip([
      'd7_file',
      'd7_file_private',
    ]));
    $store
      ->set('migrations', $migration_array);
    
    $this
      ->submitForm([], 'Perform upgrade');
    $this
      ->resetAll();
    $this
      ->assertFileMigrations();
  }
  
  public function providerTestFilePath() {
    return [
      'All source base paths are at temporary' => [
        'sites/default/private',
        'sites/default/files',
        '/tmp',
        '',
        '',
        '',
      ],
      'The private files are in a subdirectory' => [
        'sites/default/private',
        'sites/default/files',
        '/tmp',
        'abc',
        '',
        '',
      ],
      ' The public files are in a subdirectory' => [
        'sites/default/private',
        'sites/default/files',
        '/tmp',
        '',
        'def',
        '',
      ],
      'The private, public and temporary files are in separate subdirectories' => [
        'private',
        'files',
        '/tmp',
        'abc',
        'def',
        'xyz',
      ],
    ];
  }
  
  protected function makeFiles() {
    
    foreach ($this
      ->getManagedFiles() as $file) {
      $this
        ->assertSame(1, preg_match('/^(private|public|temporary):/', $file['uri'], $matches));
      $scheme = $matches[1];
      $path = $this->sourceFileScheme[$scheme] ?? '';
      $filepath = implode('/', [
        $this
          ->getSourcePath($scheme),
        $path,
        $file['filename'],
      ]);
      
      $source_file = @fopen($filepath, 'w');
      if (!$source_file) {
        
        $dir = $this->fs
          ->dirname($filepath);
        $this->fs
          ->prepareDirectory($dir, FileSystemInterface::CREATE_DIRECTORY | FileSystemInterface::MODIFY_PERMISSIONS);
        
        $source_file = @fopen($filepath, 'w');
      }
      fwrite($source_file, '42');
    }
  }
  
  public function getSourcePath($scheme) {
    $base_path = $this->localDirectory[$scheme] ?: '';
    
    return $this->tempFilesDirectory . '/' . $base_path;
  }
  
  public function getManagedFiles() {
    return [
      [
        'filename' => 'cube.jpeg',
        'uri' => 'public://cube.jpeg',
      ],
      [
        'filename' => 'ds9.txt',
        'uri' => 'public://ds9.txt',
      ],
      [
        'filename' => 'Babylon5.txt',
        'uri' => 'private://Babylon5.txt',
      ],
      [
        'filename' => 'TerokNor.txt',
        'uri' => 'temporary://TerokNor.txt',
      ],
    ];
  }
  
  protected function getEntityCounts() {
  }
  
  protected function getEntityCountsIncremental() {
  }
  
  protected function getAvailablePaths() {
  }
  
  protected function getMissingPaths() {
  }
  
  protected function getSourceBasePath() {
  }
}