class ConnectionTest

Same name in this branch
  1. 8.9.x core/tests/Drupal/KernelTests/Core/Database/ConnectionTest.php \Drupal\KernelTests\Core\Database\ConnectionTest
  2. 8.9.x core/tests/Drupal/Tests/Core/Database/Driver/sqlite/ConnectionTest.php \Drupal\Tests\Core\Database\Driver\sqlite\ConnectionTest
Same name and namespace in other branches
  1. 9 core/modules/sqlite/tests/src/Unit/ConnectionTest.php \Drupal\Tests\sqlite\Unit\ConnectionTest
  2. 9 core/modules/mysql/tests/src/Unit/ConnectionTest.php \Drupal\Tests\mysql\Unit\ConnectionTest
  3. 9 core/modules/mysql/tests/src/Kernel/mysql/ConnectionTest.php \Drupal\Tests\mysql\Kernel\mysql\ConnectionTest
  4. 9 core/tests/Drupal/KernelTests/Core/Database/ConnectionTest.php \Drupal\KernelTests\Core\Database\ConnectionTest
  5. 9 core/tests/Drupal/Tests/Core/Database/ConnectionTest.php \Drupal\Tests\Core\Database\ConnectionTest
  6. 10 core/modules/sqlite/tests/src/Unit/ConnectionTest.php \Drupal\Tests\sqlite\Unit\ConnectionTest
  7. 10 core/modules/sqlite/tests/src/Kernel/sqlite/ConnectionTest.php \Drupal\Tests\sqlite\Kernel\sqlite\ConnectionTest
  8. 10 core/modules/mysql/tests/src/Unit/ConnectionTest.php \Drupal\Tests\mysql\Unit\ConnectionTest
  9. 10 core/modules/mysql/tests/src/Kernel/mysql/ConnectionTest.php \Drupal\Tests\mysql\Kernel\mysql\ConnectionTest
  10. 10 core/tests/Drupal/KernelTests/Core/Database/ConnectionTest.php \Drupal\KernelTests\Core\Database\ConnectionTest
  11. 10 core/tests/Drupal/Tests/Core/Database/ConnectionTest.php \Drupal\Tests\Core\Database\ConnectionTest
  12. 11.x core/modules/sqlite/tests/src/Unit/ConnectionTest.php \Drupal\Tests\sqlite\Unit\ConnectionTest
  13. 11.x core/modules/mysql/tests/src/Unit/ConnectionTest.php \Drupal\Tests\mysql\Unit\ConnectionTest
  14. 11.x core/modules/mysql/tests/src/Kernel/mysql/ConnectionTest.php \Drupal\Tests\mysql\Kernel\mysql\ConnectionTest
  15. 11.x core/tests/Drupal/KernelTests/Core/Database/ConnectionTest.php \Drupal\KernelTests\Core\Database\ConnectionTest
  16. 11.x core/tests/Drupal/Tests/Core/Database/ConnectionTest.php \Drupal\Tests\Core\Database\ConnectionTest

Tests the Connection class.

@group Database

Hierarchy

Expanded class hierarchy of ConnectionTest

File

core/tests/Drupal/Tests/Core/Database/ConnectionTest.php, line 14

Namespace

Drupal\Tests\Core\Database
View source
class ConnectionTest extends UnitTestCase {
    
    /**
     * Dataprovider for testPrefixRoundTrip().
     *
     * @return array
     *   Array of arrays with the following elements:
     *   - Arguments to pass to Connection::setPrefix().
     *   - Expected result from Connection::tablePrefix().
     */
    public function providerPrefixRoundTrip() {
        return [
            [
                [
                    '' => 'test_',
                ],
                'test_',
            ],
            [
                [
                    'fooTable' => 'foo_',
                    'barTable' => 'bar_',
                ],
                [
                    'fooTable' => 'foo_',
                    'barTable' => 'bar_',
                ],
            ],
        ];
    }
    
    /**
     * Exercise setPrefix() and tablePrefix().
     *
     * @dataProvider providerPrefixRoundTrip
     */
    public function testPrefixRoundTrip($expected, $prefix_info) {
        $mock_pdo = $this->createMock('Drupal\\Tests\\Core\\Database\\Stub\\StubPDO');
        $connection = new StubConnection($mock_pdo, []);
        // setPrefix() is protected, so we make it accessible with reflection.
        $reflection = new \ReflectionClass('Drupal\\Tests\\Core\\Database\\Stub\\StubConnection');
        $set_prefix = $reflection->getMethod('setPrefix');
        $set_prefix->setAccessible(TRUE);
        // Set the prefix data.
        $set_prefix->invokeArgs($connection, [
            $prefix_info,
        ]);
        // Check the round-trip.
        foreach ($expected as $table => $prefix) {
            $this->assertEquals($prefix, $connection->tablePrefix($table));
        }
    }
    
    /**
     * Dataprovider for testPrefixTables().
     *
     * @return array
     *   Array of arrays with the following elements:
     *   - Expected result.
     *   - Table prefix.
     *   - Query to be prefixed.
     */
    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}',
            ],
        ];
    }
    
    /**
     * Exercise the prefixTables() method.
     *
     * @dataProvider providerTestPrefixTables
     */
    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));
    }
    
    /**
     * Dataprovider for testEscapeMethods().
     *
     * @return array
     *   Array of arrays with the following elements:
     *   - Expected escaped string.
     *   - String to escape.
     */
    public function providerEscapeMethods() {
        return [
            [
                'thing',
                'thing',
            ],
            [
                '_item',
                '_item',
            ],
            [
                'item_',
                'item_',
            ],
            [
                '_item_',
                '_item_',
            ],
            [
                '',
                '!@#$%^&*()-=+',
            ],
            [
                '123',
                '!1@2#3',
            ],
        ];
    }
    
    /**
     * Test the various escaping methods.
     *
     * All tested together since they're basically the same method
     * with different names.
     *
     * @dataProvider providerEscapeMethods
     * @todo Separate test method for each escape method?
     */
    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));
    }
    
    /**
     * Dataprovider for testGetDriverClass().
     *
     * @return array
     *   Array of arrays with the following elements:
     *   - Expected namespaced class name.
     *   - Driver.
     *   - Namespace.
     *   - Class name without namespace.
     */
    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',
            ],
        ];
    }
    
    /**
     * Test getDriverClass().
     *
     * @dataProvider providerGetDriverClass
     */
    public function testGetDriverClass($expected, $namespace, $class) {
        $mock_pdo = $this->createMock('Drupal\\Tests\\Core\\Database\\Stub\\StubPDO');
        $connection = new StubConnection($mock_pdo, [
            'namespace' => $namespace,
        ]);
        // Set the driver using our stub class' public property.
        $this->assertEquals($expected, $connection->getDriverClass($class));
    }
    
    /**
     * Dataprovider for testSchema().
     *
     * @return array
     *   Array of arrays with the following elements:
     *   - Expected namespaced class of schema object.
     *   - Driver for PDO connection.
     *   - Namespace for connection.
     */
    public function providerSchema() {
        return [
            [
                'Drupal\\Tests\\Core\\Database\\Stub\\Driver\\Schema',
                'stub',
                'Drupal\\Tests\\Core\\Database\\Stub\\Driver',
            ],
        ];
    }
    
    /**
     * Test Connection::schema().
     *
     * @dataProvider providerSchema
     */
    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());
    }
    
    /**
     * Test Connection::destroy().
     */
    public function testDestroy() {
        $mock_pdo = $this->createMock('Drupal\\Tests\\Core\\Database\\Stub\\StubPDO');
        // Mocking StubConnection gives us access to the $schema attribute.
        $connection = new StubConnection($mock_pdo, [
            'namespace' => 'Drupal\\Tests\\Core\\Database\\Stub\\Driver',
        ]);
        // Generate a schema object in order to verify that we've NULLed it later.
        $this->assertInstanceOf('Drupal\\Tests\\Core\\Database\\Stub\\Driver\\Schema', $connection->schema());
        $connection->destroy();
        $this->assertAttributeEquals(NULL, 'schema', $connection);
    }
    
    /**
     * Dataprovider for testMakeComments().
     *
     * @return array
     *   Array of arrays with the following elements:
     *   - Expected filtered comment.
     *   - Arguments for Connection::makeComment().
     */
    public function providerMakeComments() {
        return [
            [
                '/*  */ ',
                [
                    '',
                ],
            ],
            [
                '/* Exploit  *  / DROP TABLE node. -- */ ',
                [
                    'Exploit * / DROP TABLE node; --',
                ],
            ],
            [
                '/* Exploit  *  / DROP TABLE node. --. another comment */ ',
                [
                    'Exploit * / DROP TABLE node; --',
                    'another comment',
                ],
            ],
        ];
    }
    
    /**
     * Test Connection::makeComments().
     *
     * @dataProvider providerMakeComments
     */
    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));
    }
    
    /**
     * Dataprovider for testFilterComments().
     *
     * @return array
     *   Array of arrays with the following elements:
     *   - Expected filtered comment.
     *   - Comment to filter.
     */
    public function providerFilterComments() {
        return [
            [
                '',
                '',
            ],
            [
                'Exploit  *  / DROP TABLE node. --',
                'Exploit * / DROP TABLE node; --',
            ],
            [
                'Exploit  * / DROP TABLE node. --',
                'Exploit */ DROP TABLE node; --',
            ],
        ];
    }
    
    /**
     * Test Connection::filterComments().
     *
     * @dataProvider providerFilterComments
     */
    public function testFilterComments($expected, $comment) {
        $mock_pdo = $this->createMock('Drupal\\Tests\\Core\\Database\\Stub\\StubPDO');
        $connection = new StubConnection($mock_pdo, []);
        // filterComment() is protected, so we make it accessible with reflection.
        $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,
        ]));
    }
    
    /**
     * Test rtrim() of query strings.
     *
     * @dataProvider provideQueriesToTrim
     */
    public function testQueryTrim($expected, $query, $options) {
        $mock_pdo = $this->getMockBuilder(StubPdo::class)
            ->setMethods([
            'execute',
            'prepare',
            'setAttribute',
        ])
            ->getMock();
        // Ensure that PDO::prepare() is called only once, and with the
        // correctly trimmed query string.
        $mock_pdo->expects($this->once())
            ->method('prepare')
            ->with($expected)
            ->willReturnSelf();
        $connection = new StubConnection($mock_pdo, []);
        $connection->query($query, [], $options);
    }
    
    /**
     * Dataprovider for testQueryTrim().
     *
     * @return array
     *   Array of arrays with the following elements:
     *   - Expected trimmed query.
     *   - Padded 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,
                ],
            ],
        ];
    }

}

Members

Title Sort descending Deprecated Modifiers Object type Summary Overrides
ConnectionTest::provideQueriesToTrim public function Dataprovider for testQueryTrim().
ConnectionTest::providerEscapeMethods public function Dataprovider for testEscapeMethods().
ConnectionTest::providerFilterComments public function Dataprovider for testFilterComments().
ConnectionTest::providerGetDriverClass public function Dataprovider for testGetDriverClass().
ConnectionTest::providerMakeComments public function Dataprovider for testMakeComments().
ConnectionTest::providerPrefixRoundTrip public function Dataprovider for testPrefixRoundTrip().
ConnectionTest::providerSchema public function Dataprovider for testSchema().
ConnectionTest::providerTestPrefixTables public function Dataprovider for testPrefixTables().
ConnectionTest::testDestroy public function Test Connection::destroy().
ConnectionTest::testEscapeMethods public function Test the various escaping methods.
ConnectionTest::testFilterComments public function Test Connection::filterComments().
ConnectionTest::testGetDriverClass public function Test getDriverClass().
ConnectionTest::testMakeComments public function Test Connection::makeComments().
ConnectionTest::testPrefixRoundTrip public function Exercise setPrefix() and tablePrefix().
ConnectionTest::testPrefixTables public function Exercise the prefixTables() method.
ConnectionTest::testQueryTrim public function Test rtrim() of query strings.
ConnectionTest::testSchema public function Test Connection::schema().
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.
UnitTestCase::setUp protected function 340

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.