3 namespace Drupal\KernelTests\Core\Database;
5 use Drupal\Core\Database\Database;
8 * Tests that the prefix info for a database schema is correct.
12 class PrefixInfoTest extends DatabaseTestBase {
15 * Tests that DatabaseSchema::getPrefixInfo() returns the right database.
17 * We are testing if the return array of the method
18 * \Drupal\Core\Database\Driver\mysql\Schema::getPrefixInfo(). This return
19 * array is a keyed array with info about amongst other things the database.
20 * The other two by Drupal core supported databases do not have this variable
21 * set in the return array.
23 public function testGetPrefixInfo() {
24 $connection_info = Database::getConnectionInfo('default');
25 if ($connection_info['default']['driver'] == 'mysql') {
26 // Copy the default connection info to the 'extra' key.
27 Database::addConnectionInfo('extra', 'default', $connection_info['default']);
29 $db1_connection = Database::getConnection('default', 'default');
30 $db1_schema = $db1_connection->schema();
31 $db2_connection = Database::getConnection('default', 'extra');
33 // Get the prefix info for the first databse.
34 $method = new \ReflectionMethod($db1_schema, 'getPrefixInfo');
35 $method->setAccessible(TRUE);
36 $db1_info = $method->invoke($db1_schema);
38 // We change the database after opening the connection, so as to prevent
39 // connecting to a non-existent database.
40 $reflection = new \ReflectionObject($db2_connection);
41 $property = $reflection->getProperty('connectionOptions');
42 $property->setAccessible(TRUE);
43 $connection_info['default']['database'] = 'foobar';
44 $property->setValue($db2_connection, $connection_info['default']);
46 // For testing purposes, we also change the database info.
47 $reflection_class = new \ReflectionClass('Drupal\Core\Database\Database');
48 $property = $reflection_class->getProperty('databaseInfo');
49 $property->setAccessible(TRUE);
50 $info = $property->getValue();
51 $info['extra']['default']['database'] = 'foobar';
52 $property->setValue(NULL, $info);
54 $extra_info = Database::getConnectionInfo('extra');
55 $this->assertSame($extra_info['default']['database'], 'foobar');
56 $db2_schema = $db2_connection->schema();
57 $db2_info = $method->invoke($db2_schema);
59 $this->assertNotSame($db2_info['database'], $db1_info['database'], 'Each target connection has a different database.');
60 $this->assertSame($db2_info['database'], 'foobar', 'The new profile has a different database.');
62 Database::removeConnection('extra');