View source
<?php
namespace Drupal\Tests\Core\Database;
use Drupal\Tests\Core\Database\Stub\StubConnection;
use Drupal\Tests\Core\Database\Stub\StubPDO;
use Drupal\Tests\UnitTestCase;
class ConnectionTest extends UnitTestCase {
public function providerPrefixRoundTrip() {
return [
[
[
'' => 'test_',
],
'test_',
],
[
[
'fooTable' => 'foo_',
'barTable' => 'bar_',
],
[
'fooTable' => 'foo_',
'barTable' => 'bar_',
],
],
];
}
public function testPrefixRoundTrip($expected, $prefix_info) {
$mock_pdo = $this
->createMock('Drupal\\Tests\\Core\\Database\\Stub\\StubPDO');
$connection = new StubConnection($mock_pdo, []);
$reflection = new \ReflectionClass('Drupal\\Tests\\Core\\Database\\Stub\\StubConnection');
$set_prefix = $reflection
->getMethod('setPrefix');
$set_prefix
->setAccessible(TRUE);
$set_prefix
->invokeArgs($connection, [
$prefix_info,
]);
foreach ($expected as $table => $prefix) {
$this
->assertEquals($prefix, $connection
->tablePrefix($table));
}
}
public function providerTestPrefixTables() {
return [
[
'SELECT * FROM test_table',
'test_',
'SELECT * FROM {table}',
],
[
'SELECT * FROM first_table JOIN second_thingie',
[
'table' => 'first_',
'thingie' => 'second_',
],
'SELECT * FROM {table} JOIN {thingie}',
],
];
}
public function testPrefixTables($expected, $prefix_info, $query) {
$mock_pdo = $this
->createMock('Drupal\\Tests\\Core\\Database\\Stub\\StubPDO');
$connection = new StubConnection($mock_pdo, [
'prefix' => $prefix_info,
]);
$this
->assertEquals($expected, $connection
->prefixTables($query));
}
public function providerEscapeMethods() {
return [
[
'thing',
'thing',
],
[
'_item',
'_item',
],
[
'item_',
'item_',
],
[
'_item_',
'_item_',
],
[
'',
'!@#$%^&*()-=+',
],
[
'123',
'!1@2#3',
],
];
}
public function testEscapeMethods($expected, $name) {
$mock_pdo = $this
->createMock('Drupal\\Tests\\Core\\Database\\Stub\\StubPDO');
$connection = new StubConnection($mock_pdo, []);
$this
->assertEquals($expected, $connection
->escapeDatabase($name));
$this
->assertEquals($expected, $connection
->escapeTable($name));
$this
->assertEquals($expected, $connection
->escapeField($name));
$this
->assertEquals($expected, $connection
->escapeAlias($name));
}
public function providerGetDriverClass() {
return [
[
'nonexistent_class',
'\\',
'nonexistent_class',
],
[
'Drupal\\Tests\\Core\\Database\\Stub\\Select',
NULL,
'Select',
],
[
'Drupal\\Tests\\Core\\Database\\Stub\\Driver\\Schema',
'Drupal\\Tests\\Core\\Database\\Stub\\Driver',
'Schema',
],
];
}
public function testGetDriverClass($expected, $namespace, $class) {
$mock_pdo = $this
->createMock('Drupal\\Tests\\Core\\Database\\Stub\\StubPDO');
$connection = new StubConnection($mock_pdo, [
'namespace' => $namespace,
]);
$this
->assertEquals($expected, $connection
->getDriverClass($class));
}
public function providerSchema() {
return [
[
'Drupal\\Tests\\Core\\Database\\Stub\\Driver\\Schema',
'stub',
'Drupal\\Tests\\Core\\Database\\Stub\\Driver',
],
];
}
public function testSchema($expected, $driver, $namespace) {
$mock_pdo = $this
->createMock('Drupal\\Tests\\Core\\Database\\Stub\\StubPDO');
$connection = new StubConnection($mock_pdo, [
'namespace' => $namespace,
]);
$connection->driver = $driver;
$this
->assertInstanceOf($expected, $connection
->schema());
}
public function testDestroy() {
$mock_pdo = $this
->createMock('Drupal\\Tests\\Core\\Database\\Stub\\StubPDO');
$connection = new StubConnection($mock_pdo, [
'namespace' => 'Drupal\\Tests\\Core\\Database\\Stub\\Driver',
]);
$this
->assertInstanceOf('Drupal\\Tests\\Core\\Database\\Stub\\Driver\\Schema', $connection
->schema());
$connection
->destroy();
$this
->assertAttributeEquals(NULL, 'schema', $connection);
}
public function providerMakeComments() {
return [
[
'/* */ ',
[
'',
],
],
[
'/* Exploit * / DROP TABLE node. -- */ ',
[
'Exploit * / DROP TABLE node; --',
],
],
[
'/* Exploit * / DROP TABLE node. --. another comment */ ',
[
'Exploit * / DROP TABLE node; --',
'another comment',
],
],
];
}
public function testMakeComments($expected, $comment_array) {
$mock_pdo = $this
->createMock('Drupal\\Tests\\Core\\Database\\Stub\\StubPDO');
$connection = new StubConnection($mock_pdo, []);
$this
->assertEquals($expected, $connection
->makeComment($comment_array));
}
public function providerFilterComments() {
return [
[
'',
'',
],
[
'Exploit * / DROP TABLE node. --',
'Exploit * / DROP TABLE node; --',
],
[
'Exploit * / DROP TABLE node. --',
'Exploit */ DROP TABLE node; --',
],
];
}
public function testFilterComments($expected, $comment) {
$mock_pdo = $this
->createMock('Drupal\\Tests\\Core\\Database\\Stub\\StubPDO');
$connection = new StubConnection($mock_pdo, []);
$reflection = new \ReflectionClass('Drupal\\Tests\\Core\\Database\\Stub\\StubConnection');
$filter_comment = $reflection
->getMethod('filterComment');
$filter_comment
->setAccessible(TRUE);
$this
->assertEquals($expected, $filter_comment
->invokeArgs($connection, [
$comment,
]));
}
public function testQueryTrim($expected, $query, $options) {
$mock_pdo = $this
->getMockBuilder(StubPdo::class)
->setMethods([
'execute',
'prepare',
'setAttribute',
])
->getMock();
$mock_pdo
->expects($this
->once())
->method('prepare')
->with($expected)
->willReturnSelf();
$connection = new StubConnection($mock_pdo, []);
$connection
->query($query, [], $options);
}
public function provideQueriesToTrim() {
return [
'remove_semicolon' => [
'SELECT * FROM test',
'SELECT * FROM test;',
[],
],
'keep_trailing_semicolon' => [
'SELECT * FROM test;',
'SELECT * FROM test;',
[
'allow_delimiter_in_query' => TRUE,
],
],
'remove_semicolon_with_whitespace' => [
'SELECT * FROM test',
'SELECT * FROM test; ',
[],
],
'keep_trailing_semicolon_with_whitespace' => [
'SELECT * FROM test;',
'SELECT * FROM test; ',
[
'allow_delimiter_in_query' => TRUE,
],
],
];
}
}