function SqlBaseTest::testConnectionTypes

Same name in other branches
  1. 9 core/modules/migrate/tests/src/Kernel/SqlBaseTest.php \Drupal\Tests\migrate\Kernel\SqlBaseTest::testConnectionTypes()
  2. 8.9.x core/modules/migrate/tests/src/Kernel/SqlBaseTest.php \Drupal\Tests\migrate\Kernel\SqlBaseTest::testConnectionTypes()
  3. 11.x core/modules/migrate/tests/src/Kernel/SqlBaseTest.php \Drupal\Tests\migrate\Kernel\SqlBaseTest::testConnectionTypes()

Tests different connection types.

File

core/modules/migrate/tests/src/Kernel/SqlBaseTest.php, line 42

Class

SqlBaseTest
Tests the functionality of SqlBase.

Namespace

Drupal\Tests\migrate\Kernel

Code

public function testConnectionTypes() : void {
    $sql_base = new TestSqlBase([], $this->migration);
    // Verify that falling back to the default 'migrate' connection (defined in
    // the base class) works.
    $this->assertSame('default', $sql_base->getDatabase()
        ->getTarget());
    $this->assertSame('migrate', $sql_base->getDatabase()
        ->getKey());
    // Verify the fallback state key overrides the 'migrate' connection.
    $target = 'test_fallback_target';
    $key = 'test_fallback_key';
    $config = [
        'target' => $target,
        'key' => $key,
    ];
    $database_state_key = 'test_fallback_state';
    \Drupal::state()->set($database_state_key, $config);
    \Drupal::state()->set('migrate.fallback_state_key', $database_state_key);
    // Create a test connection using the default database configuration.
    Database::addConnectionInfo($key, $target, Database::getConnectionInfo('default')['default']);
    $this->assertSame($sql_base->getDatabase()
        ->getTarget(), $target);
    $this->assertSame($sql_base->getDatabase()
        ->getKey(), $key);
    // Verify that setting explicit connection information overrides fallbacks.
    $target = 'test_db_target';
    $key = 'test_migrate_connection';
    $config = [
        'target' => $target,
        'key' => $key,
    ];
    $sql_base->setConfiguration($config);
    Database::addConnectionInfo($key, $target, Database::getConnectionInfo('default')['default']);
    // Validate we have injected our custom key and target.
    $this->assertSame($sql_base->getDatabase()
        ->getTarget(), $target);
    $this->assertSame($sql_base->getDatabase()
        ->getKey(), $key);
    // Now test we can have SqlBase create the connection from an info array.
    $sql_base = new TestSqlBase([], $this->migration);
    $target = 'test_db_target2';
    $key = 'test_migrate_connection2';
    $database = Database::getConnectionInfo('default')['default'];
    $config = [
        'target' => $target,
        'key' => $key,
        'database' => $database,
    ];
    $sql_base->setConfiguration($config);
    // Call getDatabase() to get the connection defined.
    $sql_base->getDatabase();
    // Validate the connection has been created with the right values.
    $this->assertSame(Database::getConnectionInfo($key)[$target], $database);
    // Now, test this all works when using state to store db info.
    $target = 'test_state_db_target';
    $key = 'test_state_migrate_connection';
    $config = [
        'target' => $target,
        'key' => $key,
    ];
    $database_state_key = 'migrate_sql_base_test';
    \Drupal::state()->set($database_state_key, $config);
    $sql_base->setConfiguration([
        'database_state_key' => $database_state_key,
    ]);
    Database::addConnectionInfo($key, $target, Database::getConnectionInfo('default')['default']);
    // Validate we have injected our custom key and target.
    $this->assertSame($sql_base->getDatabase()
        ->getTarget(), $target);
    $this->assertSame($sql_base->getDatabase()
        ->getKey(), $key);
    // Now test we can have SqlBase create the connection from an info array.
    $sql_base = new TestSqlBase([], $this->migration);
    $target = 'test_state_db_target2';
    $key = 'test_state_migrate_connection2';
    $database = Database::getConnectionInfo('default')['default'];
    $config = [
        'target' => $target,
        'key' => $key,
        'database' => $database,
    ];
    $database_state_key = 'migrate_sql_base_test2';
    \Drupal::state()->set($database_state_key, $config);
    $sql_base->setConfiguration([
        'database_state_key' => $database_state_key,
    ]);
    // Call getDatabase() to get the connection defined.
    $sql_base->getDatabase();
    // Validate the connection has been created with the right values.
    $this->assertSame(Database::getConnectionInfo($key)[$target], $database);
    // Verify that falling back to 'migrate' when the connection is not defined
    // throws a RequirementsException.
    \Drupal::state()->delete('migrate.fallback_state_key');
    $sql_base->setConfiguration([]);
    Database::renameConnection('migrate', 'fallback_connection');
    $this->expectException(RequirementsException::class);
    $this->expectExceptionMessage('No database connection configured for source plugin');
    $sql_base->getDatabase();
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.