3 namespace Drupal\Tests\Core\Database;
5 use Drupal\Tests\Core\Database\Stub\StubConnection;
6 use Drupal\Tests\UnitTestCase;
9 * Tests the Connection class.
13 class ConnectionTest extends UnitTestCase {
16 * Dataprovider for testPrefixRoundTrip().
19 * Array of arrays with the following elements:
20 * - Arguments to pass to Connection::setPrefix().
21 * - Expected result from Connection::tablePrefix().
23 public function providerPrefixRoundTrip() {
43 * Exercise setPrefix() and tablePrefix().
45 * @dataProvider providerPrefixRoundTrip
47 public function testPrefixRoundTrip($expected, $prefix_info) {
48 $mock_pdo = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO');
49 $connection = new StubConnection($mock_pdo, []);
51 // setPrefix() is protected, so we make it accessible with reflection.
52 $reflection = new \ReflectionClass('Drupal\Tests\Core\Database\Stub\StubConnection');
53 $set_prefix = $reflection->getMethod('setPrefix');
54 $set_prefix->setAccessible(TRUE);
56 // Set the prefix data.
57 $set_prefix->invokeArgs($connection, [$prefix_info]);
58 // Check the round-trip.
59 foreach ($expected as $table => $prefix) {
60 $this->assertEquals($prefix, $connection->tablePrefix($table));
65 * Dataprovider for testPrefixTables().
68 * Array of arrays with the following elements:
71 * - Query to be prefixed.
73 public function providerTestPrefixTables() {
76 'SELECT * FROM test_table',
78 'SELECT * FROM {table}',
81 'SELECT * FROM first_table JOIN second_thingie',
84 'thingie' => 'second_',
86 'SELECT * FROM {table} JOIN {thingie}',
92 * Exercise the prefixTables() method.
94 * @dataProvider providerTestPrefixTables
96 public function testPrefixTables($expected, $prefix_info, $query) {
97 $mock_pdo = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO');
98 $connection = new StubConnection($mock_pdo, ['prefix' => $prefix_info]);
99 $this->assertEquals($expected, $connection->prefixTables($query));
103 * Dataprovider for testEscapeMethods().
106 * Array of arrays with the following elements:
107 * - Expected escaped string.
108 * - String to escape.
110 public function providerEscapeMethods() {
115 ['_item_', '_item_'],
116 ['', '!@#$%^&*()-=+'],
122 * Test the various escaping methods.
124 * All tested together since they're basically the same method
125 * with different names.
127 * @dataProvider providerEscapeMethods
128 * @todo Separate test method for each escape method?
130 public function testEscapeMethods($expected, $name) {
131 $mock_pdo = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO');
132 $connection = new StubConnection($mock_pdo, []);
133 $this->assertEquals($expected, $connection->escapeDatabase($name));
134 $this->assertEquals($expected, $connection->escapeTable($name));
135 $this->assertEquals($expected, $connection->escapeField($name));
136 $this->assertEquals($expected, $connection->escapeAlias($name));
140 * Dataprovider for testGetDriverClass().
143 * Array of arrays with the following elements:
144 * - Expected namespaced class name.
147 * - Class name without namespace.
149 public function providerGetDriverClass() {
157 'Drupal\Tests\Core\Database\Stub\Select',
162 'Drupal\\Tests\\Core\\Database\\Stub\\Driver\\Schema',
163 'Drupal\\Tests\\Core\\Database\\Stub\\Driver',
170 * Test getDriverClass().
172 * @dataProvider providerGetDriverClass
174 public function testGetDriverClass($expected, $namespace, $class) {
175 $mock_pdo = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO');
176 $connection = new StubConnection($mock_pdo, ['namespace' => $namespace]);
177 // Set the driver using our stub class' public property.
178 $this->assertEquals($expected, $connection->getDriverClass($class));
182 * Dataprovider for testSchema().
185 * Array of arrays with the following elements:
186 * - Expected namespaced class of schema object.
187 * - Driver for PDO connection.
188 * - Namespace for connection.
190 public function providerSchema() {
193 'Drupal\\Tests\\Core\\Database\\Stub\\Driver\\Schema',
195 'Drupal\\Tests\\Core\\Database\\Stub\\Driver',
201 * Test Connection::schema().
203 * @dataProvider providerSchema
205 public function testSchema($expected, $driver, $namespace) {
206 $mock_pdo = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO');
207 $connection = new StubConnection($mock_pdo, ['namespace' => $namespace]);
208 $connection->driver = $driver;
209 $this->assertInstanceOf($expected, $connection->schema());
213 * Test Connection::destroy().
215 public function testDestroy() {
216 $mock_pdo = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO');
217 // Mocking StubConnection gives us access to the $schema attribute.
218 $connection = $this->getMock(
219 'Drupal\Tests\Core\Database\Stub\StubConnection',
221 [$mock_pdo, ['namespace' => 'Drupal\\Tests\\Core\\Database\\Stub\\Driver']]
223 // Generate a schema object in order to verify that we've NULLed it later.
224 $this->assertInstanceOf(
225 'Drupal\\Tests\\Core\\Database\\Stub\\Driver\\Schema',
226 $connection->schema()
228 $connection->destroy();
229 $this->assertAttributeEquals(NULL, 'schema', $connection);
233 * Dataprovider for testMakeComments().
236 * Array of arrays with the following elements:
237 * - Expected filtered comment.
238 * - Arguments for Connection::makeComment().
240 public function providerMakeComments() {
247 '/* Exploit * / DROP TABLE node. -- */ ',
248 ['Exploit * / DROP TABLE node; --'],
251 '/* Exploit * / DROP TABLE node. --. another comment */ ',
252 ['Exploit * / DROP TABLE node; --', 'another comment'],
258 * Test Connection::makeComments().
260 * @dataProvider providerMakeComments
262 public function testMakeComments($expected, $comment_array) {
263 $mock_pdo = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO');
264 $connection = new StubConnection($mock_pdo, []);
265 $this->assertEquals($expected, $connection->makeComment($comment_array));
269 * Dataprovider for testFilterComments().
272 * Array of arrays with the following elements:
273 * - Expected filtered comment.
274 * - Comment to filter.
276 public function providerFilterComments() {
279 ['Exploit * / DROP TABLE node. --', 'Exploit * / DROP TABLE node; --'],
280 ['Exploit * / DROP TABLE node. --', 'Exploit */ DROP TABLE node; --'],
285 * Test Connection::filterComments().
287 * @dataProvider providerFilterComments
289 public function testFilterComments($expected, $comment) {
290 $mock_pdo = $this->getMock('Drupal\Tests\Core\Database\Stub\StubPDO');
291 $connection = new StubConnection($mock_pdo, []);
293 // filterComment() is protected, so we make it accessible with reflection.
294 $reflection = new \ReflectionClass('Drupal\Tests\Core\Database\Stub\StubConnection');
295 $filter_comment = $reflection->getMethod('filterComment');
296 $filter_comment->setAccessible(TRUE);
300 $filter_comment->invokeArgs($connection, [$comment])