function PrefixInfoTest::testGetPrefixInfo

Same name in other branches
  1. 9 core/modules/mysql/tests/src/Kernel/mysql/PrefixInfoTest.php \Drupal\Tests\mysql\Kernel\mysql\PrefixInfoTest::testGetPrefixInfo()
  2. 8.9.x core/tests/Drupal/KernelTests/Core/Database/PrefixInfoTest.php \Drupal\KernelTests\Core\Database\PrefixInfoTest::testGetPrefixInfo()
  3. 10 core/modules/mysql/tests/src/Kernel/mysql/PrefixInfoTest.php \Drupal\Tests\mysql\Kernel\mysql\PrefixInfoTest::testGetPrefixInfo()

Tests that DatabaseSchema::getPrefixInfo() returns the right database.

We are testing if the return array of the method \Drupal\mysql\Driver\Database\mysql\Schema::getPrefixInfo(). This return array is a keyed array with info about amongst other things the database. The other two by Drupal core supported databases do not have this variable set in the return array.

File

core/modules/mysql/tests/src/Kernel/mysql/PrefixInfoTest.php, line 26

Class

PrefixInfoTest
Tests that the prefix info for a database schema is correct.

Namespace

Drupal\Tests\mysql\Kernel\mysql

Code

public function testGetPrefixInfo() : void {
    $connection_info = Database::getConnectionInfo('default');
    // Copy the default connection info to the 'extra' key.
    Database::addConnectionInfo('extra', 'default', $connection_info['default']);
    $db1_connection = Database::getConnection('default', 'default');
    $db1_schema = $db1_connection->schema();
    $db2_connection = Database::getConnection('default', 'extra');
    // Get the prefix info for the first database.
    $method = new \ReflectionMethod($db1_schema, 'getPrefixInfo');
    $db1_info = $method->invoke($db1_schema);
    // We change the database after opening the connection, so as to prevent
    // connecting to a non-existent database.
    $reflection = new \ReflectionObject($db2_connection);
    $property = $reflection->getProperty('connectionOptions');
    $connection_info['default']['database'] = 'foobar';
    $property->setValue($db2_connection, $connection_info['default']);
    // For testing purposes, we also change the database info.
    $reflection_class = new \ReflectionClass(Database::class);
    $property = $reflection_class->getProperty('databaseInfo');
    $info = $property->getValue();
    $info['extra']['default']['database'] = 'foobar';
    $property->setValue(NULL, $info);
    $extra_info = Database::getConnectionInfo('extra');
    $this->assertSame('foobar', $extra_info['default']['database']);
    $db2_schema = $db2_connection->schema();
    $db2_info = $method->invoke($db2_schema);
    // Each target connection has a different database.
    $this->assertNotSame($db2_info['database'], $db1_info['database']);
    // The new profile has a different database.
    $this->assertSame('foobar', $db2_info['database']);
    Database::removeConnection('extra');
}

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