View source
<?php
namespace Drupal\system\Tests\Database;
use Drupal\Core\Database\Database;
use Drupal\Core\Database\DatabaseExceptionWrapper;
class ConnectionTest extends DatabaseTestBase {
function testConnectionRouting() {
$connection_info = Database::getConnectionInfo('default');
Database::addConnectionInfo('default', 'replica', $connection_info['default']);
$db1 = Database::getConnection('default', 'default');
$db2 = Database::getConnection('replica', 'default');
$this
->assertNotNull($db1, 'default connection is a real connection object.');
$this
->assertNotNull($db2, 'replica connection is a real connection object.');
$this
->assertNotIdentical($db1, $db2, 'Each target refers to a different connection.');
$db1b = Database::getConnection('default', 'default');
$db2b = Database::getConnection('replica', 'default');
$this
->assertIdentical($db1, $db1b, 'A second call to getConnection() returns the same object.');
$this
->assertIdentical($db2, $db2b, 'A second call to getConnection() returns the same object.');
$unknown_target = $this
->randomMachineName();
$db3 = Database::getConnection($unknown_target, 'default');
$this
->assertNotNull($db3, 'Opening an unknown target returns a real connection object.');
$this
->assertIdentical($db1, $db3, 'An unknown target opens the default connection.');
$db3b = Database::getConnection($unknown_target, 'default');
$this
->assertIdentical($db3, $db3b, 'A second call to getConnection() returns the same object.');
}
function testConnectionRoutingOverride() {
$connection_info = Database::getConnectionInfo('default');
Database::addConnectionInfo('default', 'replica', $connection_info['default']);
Database::ignoreTarget('default', 'replica');
$db1 = Database::getConnection('default', 'default');
$db2 = Database::getConnection('replica', 'default');
$this
->assertIdentical($db1, $db2, 'Both targets refer to the same connection.');
}
function testConnectionClosing() {
$db1 = Database::getConnection('default', 'default');
Database::closeConnection('default', 'default');
$db2 = Database::getConnection('default', 'default');
$this
->assertNotIdentical($db1, $db2, 'Opening the default connection after it is closed returns a new object.');
}
function testConnectionOptions() {
$connection_info = Database::getConnectionInfo('default');
$db = Database::getConnection('default', 'default');
$connectionOptions = $db
->getConnectionOptions();
$this
->assertEqual($connection_info['default']['driver'], $connectionOptions['driver'], 'The default connection info driver matches the current connection options driver.');
$this
->assertEqual($connection_info['default']['database'], $connectionOptions['database'], 'The default connection info database matches the current connection options database.');
Database::addConnectionInfo('default', 'replica', $connection_info['default']);
$db2 = Database::getConnection('replica', 'default');
$connectionOptions2 = $db2
->getConnectionOptions();
$connectionOptions = $db
->getConnectionOptions();
$this
->assertIdentical($connectionOptions, $connectionOptions2, 'The default and replica connection options are identical.');
$test = $connection_info['default'];
$test['database'] .= 'test';
Database::addConnectionInfo('test', 'default', $test);
$connection_info = Database::getConnectionInfo('test');
$connectionOptions = $db
->getConnectionOptions();
$this
->assertNotEqual($connection_info['default']['database'], $connectionOptions['database'], 'The test connection info database does not match the current connection options database.');
}
public function testMultipleStatementsForNewPhp() {
if (Database::getConnection()
->databaseType() !== 'mysql' || !defined('\\PDO::MYSQL_ATTR_MULTI_STATEMENTS')) {
return;
}
$db = Database::getConnection('default', 'default');
try {
$db
->query('SELECT * FROM {test}; SELECT * FROM {test_people}', [], [
'allow_delimiter_in_query' => TRUE,
]);
$this
->fail('No PDO exception thrown for multiple statements.');
} catch (DatabaseExceptionWrapper $e) {
$this
->pass('PDO exception thrown for multiple statements.');
}
}
public function testMultipleStatements() {
$db = Database::getConnection('default', 'default');
try {
$db
->query('SELECT * FROM {test}; SELECT * FROM {test_people}');
$this
->fail('No exception thrown for multiple statements.');
} catch (\InvalidArgumentException $e) {
$this
->pass('Exception thrown for multiple statements.');
}
}
public function testPostgresqlReservedWords() {
if (Database::getConnection()
->databaseType() !== 'pgsql') {
return;
}
$db = Database::getConnection('default', 'default');
$stmt = $db
->query("SELECT word FROM pg_get_keywords() WHERE catcode IN ('R', 'T')");
$stmt
->execute();
foreach ($stmt
->fetchAllAssoc('word') as $word => $row) {
$expected = '"' . $word . '"';
$this
->assertIdentical($db
->escapeTable($word), $expected, format_string('The reserved word %word was correctly escaped when used as a table name.', array(
'%word' => $word,
)));
$this
->assertIdentical($db
->escapeField($word), $expected, format_string('The reserved word %word was correctly escaped when used as a column name.', array(
'%word' => $word,
)));
$this
->assertIdentical($db
->escapeAlias($word), $expected, format_string('The reserved word %word was correctly escaped when used as an alias.', array(
'%word' => $word,
)));
}
}
}