@@ -4247,9 +4247,9 @@ class ConnectionUnitTest extends DrupalUnitTestCase {
4247
4247
}
4248
4248
}
4249
4249
4250
- /**
4251
- * Test reserved keyword handling (introduced for MySQL 8+)
4252
- */
4250
+ /**
4251
+ * Test reserved keyword handling (introduced for MySQL 8+)
4252
+ */
4253
4253
class DatabaseReservedKeywordTestCase extends DatabaseTestCase {
4254
4254
public static function getInfo() {
4255
4255
return array(
@@ -4373,5 +4373,50 @@ class DatabaseReservedKeywordTestCase extends DatabaseTestCase {
4373
4373
$num_records_after = db_query('SELECT COUNT(*) FROM {virtual}')->fetchField();
4374
4374
$this->assertIdentical($num_records_before, $num_records_after, 'Successful merge query on a table with a name and column which are reserved words.');
4375
4375
}
4376
+ }
4377
+
4378
+ /**
4379
+ * Test table prefix handling.
4380
+ */
4381
+ class DatabaseTablePrefixTestCase extends DatabaseTestCase {
4382
+ public static function getInfo() {
4383
+ return array(
4384
+ 'name' => 'Table prefixes',
4385
+ 'description' => 'Test handling of table prefixes.',
4386
+ 'group' => 'Database',
4387
+ );
4388
+ }
4376
4389
4390
+ public function testSchemaDotTablePrefixes() {
4391
+ // Get a copy of the default connection options.
4392
+ $db = Database::getConnection('default', 'default');
4393
+ $connection_options = $db->getConnectionOptions();
4394
+
4395
+ if ($connection_options['driver'] === 'sqlite') {
4396
+ // In SQLite simpletest's prefixed db tables exist in their own schema
4397
+ // (e.g. simpletest124904.system), so we cannot test the schema.table
4398
+ // prefix syntax here.
4399
+ $this->assert(TRUE, 'Skipping schema.table prefixed tables test for SQLite.');
4400
+ return;
4401
+ }
4402
+
4403
+ $db_name = $connection_options['database'];
4404
+ // This prefix is usually something like simpletest12345
4405
+ $test_prefix = $connection_options['prefix']['default'];
4406
+
4407
+ // Set up a new connection with table prefixes in the form "schema.table"
4408
+ $prefixed = $connection_options;
4409
+ $prefixed['prefix'] = array(
4410
+ 'default' => $test_prefix,
4411
+ 'users' => $db_name . '.' . $test_prefix,
4412
+ 'role' => $db_name . '.' . $test_prefix,
4413
+ );
4414
+ Database::addConnectionInfo('default', 'prefixed', $prefixed);
4415
+
4416
+ // Test that the prefixed database connection can query the prefixed tables.
4417
+ $num_users_prefixed = Database::getConnection('prefixed', 'default')->query('SELECT COUNT(1) FROM {users}')->fetchField();
4418
+ $this->assertTrue((int) $num_users_prefixed > 0, 'Successfully queried the users table using a schema.table prefix');
4419
+ $num_users_default = Database::getConnection('default', 'default')->query('SELECT COUNT(1) FROM {users}')->fetchField();
4420
+ $this->assertEqual($num_users_default, $num_users_prefixed, 'Verified results of query using a connection with schema.table prefixed tables');
4421
+ }
4377
4422
}
0 commit comments