private function DrushTestBase::createMigrationConnection in Media Migration 8
Changes the database connection to the prefixed one.
This method is the modified copy of Drupal\Tests\migrate\Kernel\MigrateTestBase::createMigrationConnection().
@todo Remove when we don't use global. https://www.drupal.org/node/2552791
1 call to DrushTestBase::createMigrationConnection()
- DrushTestBase::setUp in tests/
src/ Functional/ DrushTestBase.php
File
- tests/
src/ Functional/ DrushTestBase.php, line 125
Class
- DrushTestBase
- Base class for testing media migrations executed with Drush.
Namespace
Drupal\Tests\media_migration\FunctionalCode
private function createMigrationConnection(string $source_db_key = 'migrate') {
// If the backup already exists, something went terribly wrong.
// This case is possible, because database connection info is a static
// global state construct on the Database class, which at least persists
// for all test methods executed in one PHP process.
if (Database::getConnectionInfo('simpletest_original_migrate')) {
throw new \RuntimeException("Bad Database connection state: 'simpletest_original_migrate' connection key already exists. Broken test?");
}
// Clone the current connection and replace the current prefix.
$connection_info = Database::getConnectionInfo('migrate');
if ($connection_info) {
Database::renameConnection('migrate', 'simpletest_original_migrate');
}
$connection_info = Database::getConnectionInfo('default');
foreach ($connection_info as $target => $value) {
$prefix = is_array($value['prefix']) ? $value['prefix']['default'] : $value['prefix'];
// Simpletest uses 7 character prefixes at most so this can't cause
// collisions.
$connection_info[$target]['prefix']['default'] = $prefix . '0';
// Add the original simpletest prefix so SQLite can attach its database.
// @see \Drupal\Core\Database\Driver\sqlite\Connection::init()
$connection_info[$target]['prefix'][$value['prefix']['default']] = $value['prefix']['default'];
}
Database::addConnectionInfo($source_db_key, 'default', $connection_info['default']);
$this
->writeSettings([
'databases' => [
"{$source_db_key}']['default" => (object) [
'value' => $connection_info['default'],
'required' => TRUE,
],
],
]);
\Drupal::state()
->set('media_migration.test_db', [
'key' => $source_db_key,
'target' => 'default',
]);
\Drupal::state()
->set('migrate.fallback_state_key', 'media_migration.test_db');
}