diff --git a/core/modules/mysql/tests/src/Kernel/mysql/SchemaTest.php b/core/modules/mysql/tests/src/Kernel/mysql/SchemaTest.php index f08a04df893d888e3d2029e536370b2f4fcddd93..f219b373280d5da0a30c0deb2d77ae93c6913414 100644 --- a/core/modules/mysql/tests/src/Kernel/mysql/SchemaTest.php +++ b/core/modules/mysql/tests/src/Kernel/mysql/SchemaTest.php @@ -43,6 +43,24 @@ protected function assertCollation(): void { $this->assertSame('ascii_general_ci', $string_ascii_check, 'test_field_string_ascii should have a ascii_general_ci collation, but it has not.'); } + /** + * {@inheritdoc} + */ + public function testTableWithSpecificDataType(): void { + $table_specification = [ + 'description' => 'Schema table description.', + 'fields' => [ + 'timestamp' => [ + 'mysql_type' => 'timestamp', + 'not null' => FALSE, + 'default' => NULL, + ], + ], + ]; + $this->schema->createTable('test_timestamp', $table_specification); + $this->assertTrue($this->schema->tableExists('test_timestamp')); + } + /** * Tests that indexes on string fields are limited to 191 characters on MySQL. * diff --git a/core/modules/mysql/tests/src/Kernel/mysql/SyntaxTest.php b/core/modules/mysql/tests/src/Kernel/mysql/SyntaxTest.php new file mode 100644 index 0000000000000000000000000000000000000000..b5ad81267abe5b4b2e29e94419adae5cd3b41260 --- /dev/null +++ b/core/modules/mysql/tests/src/Kernel/mysql/SyntaxTest.php @@ -0,0 +1,13 @@ +<?php + +namespace Drupal\Tests\mysql\Kernel\mysql; + +use Drupal\KernelTests\Core\Database\DriverSpecificSyntaxTestBase; + +/** + * Tests MySql syntax interpretation. + * + * @group Database + */ +class SyntaxTest extends DriverSpecificSyntaxTestBase { +} diff --git a/core/modules/mysql/tests/src/Kernel/mysql/TransactionTest.php b/core/modules/mysql/tests/src/Kernel/mysql/TransactionTest.php new file mode 100644 index 0000000000000000000000000000000000000000..d9d5dd513cc73a3ef6066eb74d4e7bdfbe18ed99 --- /dev/null +++ b/core/modules/mysql/tests/src/Kernel/mysql/TransactionTest.php @@ -0,0 +1,13 @@ +<?php + +namespace Drupal\Tests\mysql\Kernel\mysql; + +use Drupal\KernelTests\Core\Database\DriverSpecificTransactionTestBase; + +/** + * Tests transaction for the MySQL driver. + * + * @group Database + */ +class TransactionTest extends DriverSpecificTransactionTestBase { +} diff --git a/core/modules/pgsql/tests/src/Kernel/pgsql/SchemaTest.php b/core/modules/pgsql/tests/src/Kernel/pgsql/SchemaTest.php index b32335e4b872af3b8c347ff58010ededf8122040..b7fee8694a838f1c19ccb3bf975cf14cae6ec2ef 100644 --- a/core/modules/pgsql/tests/src/Kernel/pgsql/SchemaTest.php +++ b/core/modules/pgsql/tests/src/Kernel/pgsql/SchemaTest.php @@ -39,6 +39,24 @@ protected function checkSequenceRenaming(string $tableName): void { $this->assertTrue($sequenceExists, 'Sequence was renamed.'); } + /** + * {@inheritdoc} + */ + public function testTableWithSpecificDataType(): void { + $table_specification = [ + 'description' => 'Schema table description.', + 'fields' => [ + 'timestamp' => [ + 'pgsql_type' => 'timestamp', + 'not null' => FALSE, + 'default' => NULL, + ], + ], + ]; + $this->schema->createTable('test_timestamp', $table_specification); + $this->assertTrue($this->schema->tableExists('test_timestamp')); + } + /** * @covers \Drupal\pgsql\Driver\Database\pgsql\Schema::introspectIndexSchema */ diff --git a/core/modules/pgsql/tests/src/Kernel/pgsql/SyntaxTest.php b/core/modules/pgsql/tests/src/Kernel/pgsql/SyntaxTest.php new file mode 100644 index 0000000000000000000000000000000000000000..2da93a315c479ea84cbcdfe90ef31bd835be9952 --- /dev/null +++ b/core/modules/pgsql/tests/src/Kernel/pgsql/SyntaxTest.php @@ -0,0 +1,13 @@ +<?php + +namespace Drupal\Tests\pgsql\Kernel\pgsql; + +use Drupal\KernelTests\Core\Database\DriverSpecificSyntaxTestBase; + +/** + * Tests PostgreSQL syntax interpretation. + * + * @group Database + */ +class SyntaxTest extends DriverSpecificSyntaxTestBase { +} diff --git a/core/modules/pgsql/tests/src/Kernel/pgsql/TransactionTest.php b/core/modules/pgsql/tests/src/Kernel/pgsql/TransactionTest.php new file mode 100644 index 0000000000000000000000000000000000000000..f9d7d0ed73093468e372fde53c31dd95b4db121e --- /dev/null +++ b/core/modules/pgsql/tests/src/Kernel/pgsql/TransactionTest.php @@ -0,0 +1,13 @@ +<?php + +namespace Drupal\Tests\pgsql\Kernel\pgsql; + +use Drupal\KernelTests\Core\Database\DriverSpecificTransactionTestBase; + +/** + * Tests transaction for the PostgreSQL driver. + * + * @group Database + */ +class TransactionTest extends DriverSpecificTransactionTestBase { +} diff --git a/core/modules/sqlite/tests/src/Kernel/sqlite/SchemaTest.php b/core/modules/sqlite/tests/src/Kernel/sqlite/SchemaTest.php index 07b6624196572f33543fb35f171c9cc469d27e3d..a3aec37553fd51866dbca285a4fe06e9088a3e4c 100644 --- a/core/modules/sqlite/tests/src/Kernel/sqlite/SchemaTest.php +++ b/core/modules/sqlite/tests/src/Kernel/sqlite/SchemaTest.php @@ -5,7 +5,7 @@ use Drupal\KernelTests\Core\Database\DriverSpecificSchemaTestBase; /** - * Tests schema API for the PostgreSQL driver. + * Tests schema API for the SQLite driver. * * @group Database */ @@ -26,6 +26,24 @@ protected function tryInsertExpectsIntegrityConstraintViolationException(string // Sqlite does not throw an IntegrityConstraintViolationException here. } + /** + * {@inheritdoc} + */ + public function testTableWithSpecificDataType(): void { + $table_specification = [ + 'description' => 'Schema table description.', + 'fields' => [ + 'timestamp' => [ + 'sqlite_type' => 'datetime', + 'not null' => FALSE, + 'default' => NULL, + ], + ], + ]; + $this->schema->createTable('test_timestamp', $table_specification); + $this->assertTrue($this->schema->tableExists('test_timestamp')); + } + /** * @covers \Drupal\sqlite\Driver\Database\sqlite\Schema::introspectIndexSchema */ diff --git a/core/modules/sqlite/tests/src/Kernel/sqlite/SyntaxTest.php b/core/modules/sqlite/tests/src/Kernel/sqlite/SyntaxTest.php new file mode 100644 index 0000000000000000000000000000000000000000..fd495ffefec742d2b601e21a1e601dbdd99e1b64 --- /dev/null +++ b/core/modules/sqlite/tests/src/Kernel/sqlite/SyntaxTest.php @@ -0,0 +1,13 @@ +<?php + +namespace Drupal\Tests\sqlite\Kernel\sqlite; + +use Drupal\KernelTests\Core\Database\DriverSpecificSyntaxTestBase; + +/** + * Tests SQLite syntax interpretation. + * + * @group Database + */ +class SyntaxTest extends DriverSpecificSyntaxTestBase { +} diff --git a/core/modules/sqlite/tests/src/Kernel/sqlite/TransactionTest.php b/core/modules/sqlite/tests/src/Kernel/sqlite/TransactionTest.php new file mode 100644 index 0000000000000000000000000000000000000000..1c69ab15929c05c18c9ba32fe41bcd38d0358dae --- /dev/null +++ b/core/modules/sqlite/tests/src/Kernel/sqlite/TransactionTest.php @@ -0,0 +1,13 @@ +<?php + +namespace Drupal\Tests\sqlite\Kernel\sqlite; + +use Drupal\KernelTests\Core\Database\DriverSpecificTransactionTestBase; + +/** + * Tests transaction for the SQLite driver. + * + * @group Database + */ +class TransactionTest extends DriverSpecificTransactionTestBase { +} diff --git a/core/modules/system/tests/modules/database_statement_monitoring_test/database_statement_monitoring_test.info.yml b/core/modules/system/tests/modules/database_statement_monitoring_test/database_statement_monitoring_test.info.yml index 894715d976a74780bf021c1f893fb78f4ba5b5bb..33b576e85455afd84522c8c6329fdc7909395d89 100644 --- a/core/modules/system/tests/modules/database_statement_monitoring_test/database_statement_monitoring_test.info.yml +++ b/core/modules/system/tests/modules/database_statement_monitoring_test/database_statement_monitoring_test.info.yml @@ -3,3 +3,5 @@ type: module description: 'Support module for Database layer tests that need to monitor executed database statements.' package: Testing version: VERSION +lifecycle: deprecated +lifecycle_link: 'https://www.drupal.org/node/3318162' diff --git a/core/modules/system/tests/modules/database_statement_monitoring_test/src/LoggedStatementsTrait.php b/core/modules/system/tests/modules/database_statement_monitoring_test/src/LoggedStatementsTrait.php index 677f4fd891825e3282071e9f00ee101573de325c..553755104a46ca17dc7589f20a28a4e228cace52 100644 --- a/core/modules/system/tests/modules/database_statement_monitoring_test/src/LoggedStatementsTrait.php +++ b/core/modules/system/tests/modules/database_statement_monitoring_test/src/LoggedStatementsTrait.php @@ -2,8 +2,15 @@ namespace Drupal\database_statement_monitoring_test; +@trigger_error('\Drupal\database_statement_monitoring_test\LoggedStatementsTrait is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. There is no replacement. See https://www.drupal.org/node/3318162', E_USER_DEPRECATED); + /** * Trait for Connection classes that can store logged statements. + * + * @deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. There is no + * replacement. + * + * @see https://www.drupal.org/node/3318162 */ trait LoggedStatementsTrait { diff --git a/core/modules/system/tests/modules/database_statement_monitoring_test/src/mysql/Connection.php b/core/modules/system/tests/modules/database_statement_monitoring_test/src/mysql/Connection.php index caa96940090dc481e90ea4190a1d62047f00c96e..2af9c20b7c9ffe214f60f84efd7b71b2a9d6f8af 100644 --- a/core/modules/system/tests/modules/database_statement_monitoring_test/src/mysql/Connection.php +++ b/core/modules/system/tests/modules/database_statement_monitoring_test/src/mysql/Connection.php @@ -5,8 +5,15 @@ use Drupal\mysql\Driver\Database\mysql\Connection as BaseConnection; use Drupal\database_statement_monitoring_test\LoggedStatementsTrait; +@trigger_error('\Drupal\database_statement_monitoring_test\mysql\Connection is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. There is no replacement. See https://www.drupal.org/node/3318162', E_USER_DEPRECATED); + /** * MySQL Connection class that can log executed queries. + * + * @deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. There is no + * replacement. + * + * @see https://www.drupal.org/node/3318162 */ class Connection extends BaseConnection { use LoggedStatementsTrait; diff --git a/core/modules/system/tests/modules/database_statement_monitoring_test/src/mysql/Install/Tasks.php b/core/modules/system/tests/modules/database_statement_monitoring_test/src/mysql/Install/Tasks.php index 338e136cd5c0cd344a593b659874a444dde046dd..a1d0c30d392ddf908a661abae7989c5e47ed691d 100644 --- a/core/modules/system/tests/modules/database_statement_monitoring_test/src/mysql/Install/Tasks.php +++ b/core/modules/system/tests/modules/database_statement_monitoring_test/src/mysql/Install/Tasks.php @@ -4,5 +4,13 @@ use Drupal\mysql\Driver\Database\mysql\Install\Tasks as BaseTasks; +@trigger_error('\Drupal\database_statement_monitoring_test\mysql\Install\Tasks is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. There is no replacement. See https://www.drupal.org/node/3318162', E_USER_DEPRECATED); + +/** + * @deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. There is no + * replacement. + * + * @see https://www.drupal.org/node/3318162 + */ class Tasks extends BaseTasks { } diff --git a/core/modules/system/tests/modules/database_statement_monitoring_test/src/pgsql/Connection.php b/core/modules/system/tests/modules/database_statement_monitoring_test/src/pgsql/Connection.php index 43995d325a84bbfb5376d02e06da34bf033bc642..227673100d8f2acb429afee002a3adb2c480ecf7 100644 --- a/core/modules/system/tests/modules/database_statement_monitoring_test/src/pgsql/Connection.php +++ b/core/modules/system/tests/modules/database_statement_monitoring_test/src/pgsql/Connection.php @@ -5,8 +5,15 @@ use Drupal\pgsql\Driver\Database\pgsql\Connection as BaseConnection; use Drupal\database_statement_monitoring_test\LoggedStatementsTrait; +@trigger_error('\Drupal\database_statement_monitoring_test\pgsql\Connection is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. There is no replacement. See https://www.drupal.org/node/3318162', E_USER_DEPRECATED); + /** * PostgreSQL Connection class that can log executed queries. + * + * @deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. There is no + * replacement. + * + * @see https://www.drupal.org/node/3318162 */ class Connection extends BaseConnection { use LoggedStatementsTrait; diff --git a/core/modules/system/tests/modules/database_statement_monitoring_test/src/pgsql/Install/Tasks.php b/core/modules/system/tests/modules/database_statement_monitoring_test/src/pgsql/Install/Tasks.php index 0b95ddf53d4f3ba7fdff48917a36494f3d64bf68..5ddc4b106eed4d6dedbfcee9a309bcef11033dc6 100644 --- a/core/modules/system/tests/modules/database_statement_monitoring_test/src/pgsql/Install/Tasks.php +++ b/core/modules/system/tests/modules/database_statement_monitoring_test/src/pgsql/Install/Tasks.php @@ -4,5 +4,13 @@ use Drupal\pgsql\Driver\Database\pgsql\Install\Tasks as BaseTasks; +@trigger_error('\Drupal\database_statement_monitoring_test\pgsql\Install\Tasks is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. There is no replacement. See https://www.drupal.org/node/3318162', E_USER_DEPRECATED); + +/** + * @deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. There is no + * replacement. + * + * @see https://www.drupal.org/node/3318162 + */ class Tasks extends BaseTasks { } diff --git a/core/modules/system/tests/modules/database_statement_monitoring_test/src/sqlite/Connection.php b/core/modules/system/tests/modules/database_statement_monitoring_test/src/sqlite/Connection.php index 2b4201a9763a771e554df793ad10ac623429fa9b..0760cbc024480eb496ba293c7002ce02417f2b9b 100644 --- a/core/modules/system/tests/modules/database_statement_monitoring_test/src/sqlite/Connection.php +++ b/core/modules/system/tests/modules/database_statement_monitoring_test/src/sqlite/Connection.php @@ -5,8 +5,15 @@ use Drupal\sqlite\Driver\Database\sqlite\Connection as BaseConnection; use Drupal\database_statement_monitoring_test\LoggedStatementsTrait; +@trigger_error('\Drupal\database_statement_monitoring_test\sqlite\Connection is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. There is no replacement. See https://www.drupal.org/node/3318162', E_USER_DEPRECATED); + /** * SQlite Connection class that can log executed queries. + * + * @deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. There is no + * replacement. + * + * @see https://www.drupal.org/node/3318162 */ class Connection extends BaseConnection { use LoggedStatementsTrait; diff --git a/core/modules/system/tests/modules/database_statement_monitoring_test/src/sqlite/Install/Tasks.php b/core/modules/system/tests/modules/database_statement_monitoring_test/src/sqlite/Install/Tasks.php index 4827f8c18b9202a75360c6889e081bc068e18ba2..5694f5684a2aaaae4dacd260e8e8fae1df82e205 100644 --- a/core/modules/system/tests/modules/database_statement_monitoring_test/src/sqlite/Install/Tasks.php +++ b/core/modules/system/tests/modules/database_statement_monitoring_test/src/sqlite/Install/Tasks.php @@ -4,5 +4,13 @@ use Drupal\sqlite\Driver\Database\sqlite\Install\Tasks as BaseTasks; +@trigger_error('\Drupal\database_statement_monitoring_test\sqlite\Install\Tasks is deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. There is no replacement. See https://www.drupal.org/node/3318162', E_USER_DEPRECATED); + +/** + * @deprecated in drupal:10.1.0 and is removed from drupal:11.0.0. There is no + * replacement. + * + * @see https://www.drupal.org/node/3318162 + */ class Tasks extends BaseTasks { } diff --git a/core/tests/Drupal/KernelTests/Core/Cache/EndOfTransactionQueriesTest.php b/core/tests/Drupal/KernelTests/Core/Cache/EndOfTransactionQueriesTest.php index 1d735d511def5552ddb76a1261c101c2c16782f2..46f5a37638a73fce65bb4749671be89d129c5918 100644 --- a/core/tests/Drupal/KernelTests/Core/Cache/EndOfTransactionQueriesTest.php +++ b/core/tests/Drupal/KernelTests/Core/Cache/EndOfTransactionQueriesTest.php @@ -4,6 +4,7 @@ use Drupal\Core\Cache\Cache; use Drupal\Core\Cache\DatabaseBackendFactory; +use Drupal\Core\Database\Database; use Drupal\Core\DependencyInjection\ContainerBuilder; use Drupal\entity_test\Entity\EntityTest; use Drupal\KernelTests\KernelTestBase; @@ -11,7 +12,7 @@ use Symfony\Component\DependencyInjection\Reference; /** - * Tests that cache tag invalidation queries are delayed to the end of transactions. + * Tests delaying of cache tag invalidation queries to the end of transactions. * * @group Cache */ @@ -33,12 +34,6 @@ class EndOfTransactionQueriesTest extends KernelTestBase { protected function setUp(): void { parent::setUp(); - // This can only be checked after installing Drupal as it requires functions - // from bootstrap.inc. - if (!class_exists($this->getDatabaseConnectionInfo()['default']['namespace'] . '\Connection')) { - $this->markTestSkipped(sprintf('No logging override exists for the %s database driver. Create it, subclass this test class and override ::getDatabaseConnectionInfo().', $this->getDatabaseConnectionInfo()['default']['driver'])); - } - $this->installSchema('system', 'sequences'); $this->installEntitySchema('entity_test'); $this->installEntitySchema('user'); @@ -61,21 +56,23 @@ public function register(ContainerBuilder $container) { } /** - * {@inheritdoc} + * Tests an entity save. */ - public function testEntitySave() { + public function testEntitySave(): void { \Drupal::cache()->set('test_cache_pretransaction_foobar', 'something', Cache::PERMANENT, ['foobar']); \Drupal::cache()->set('test_cache_pretransaction_entity_test_list', 'something', Cache::PERMANENT, ['entity_test_list']); $entity = EntityTest::create(['name' => $this->randomString()]); - \Drupal::database()->resetLoggedStatements(); + Database::startLog('testEntitySave'); $entity->save(); - $executed_statements = \Drupal::database()->getLoggedStatements(); + $executed_statements = []; + foreach (Database::getLog('testEntitySave') as $log) { + $executed_statements[] = $log['query']; + } $last_statement_index = max(array_keys($executed_statements)); - - $cachetag_statements = array_keys($this->getStatementsForTable(\Drupal::database()->getLoggedStatements(), 'cachetags')); + $cachetag_statements = array_keys($this->getStatementsForTable($executed_statements, 'cachetags')); $this->assertSame($last_statement_index - count($cachetag_statements) + 1, min($cachetag_statements), 'All of the last queries in the transaction are for the "cachetags" table.'); // Verify that a nested entity save occurred. @@ -103,9 +100,9 @@ public function testEntitySave() { } /** - * {@inheritdoc} + * Tests an entity save rollback. */ - public function testEntitySaveRollback() { + public function testEntitySaveRollback(): void { \Drupal::cache() ->set('test_cache_pretransaction_entity_test_list', 'something', Cache::PERMANENT, ['entity_test_list']); \Drupal::cache() @@ -148,44 +145,29 @@ public function testEntitySaveRollback() { * Filtered statement list. */ protected function getStatementsForTable(array $statements, $table_name) { - $tables = array_filter(array_map([$this, 'statementToTableName'], $statements)); - return array_filter($tables, function ($table_for_statement) use ($table_name) { - return $table_for_statement === $table_name; + return array_filter($statements, function ($statement) use ($table_name) { + return $this->isStatementRelatedToTable($statement, $table_name); }); } /** - * Returns the table name for a statement. + * Determines if a statement is relative to a specified table. + * + * Non-core database drivers can override this method if they have different + * patterns to identify table related statements. * * @param string $statement * The query statement. + * @param string $tableName + * The table name, Drupal style, without curly brackets or prefix. * - * @return string|null - * The name of the table or NULL if none was found. - */ - protected static function statementToTableName($statement) { - if (preg_match('/.*\{([^\}]+)\}.*/', $statement, $matches)) { - return $matches[1]; - } - else { - return NULL; - } - } - - /** - * {@inheritdoc} + * @return bool + * TRUE if the statement is relative to the table, FALSE otherwise. */ - protected function getDatabaseConnectionInfo() { - $info = parent::getDatabaseConnectionInfo(); - // Override default database driver to one that does logging. Third-party - // (non-core) database drivers can achieve the same test coverage by - // subclassing this test class and overriding only this method. - // @see \Drupal\database_statement_monitoring_test\LoggedStatementsTrait - // @see \Drupal\database_statement_monitoring_test\mysql\Connection - // @see \Drupal\database_statement_monitoring_test\pgsql\Connection - // @see \Drupal\database_statement_monitoring_test\sqlite\Connection - $info['default']['namespace'] = '\Drupal\database_statement_monitoring_test\\' . $info['default']['driver']; - return $info; + protected static function isStatementRelatedToTable(string $statement, string $tableName): bool { + $realTableIdentifier = Database::getConnection()->prefixTables('{' . $tableName . '}'); + $pattern = '/.*(INTO|FROM|UPDATE)( |\n)' . preg_quote($realTableIdentifier, '/') . '/'; + return preg_match($pattern, $statement) === 1 ? TRUE : FALSE; } } diff --git a/core/tests/Drupal/KernelTests/Core/Database/BasicSyntaxTest.php b/core/tests/Drupal/KernelTests/Core/Database/BasicSyntaxTest.php index 773ec6a99b7260333c2e0ef1af40afa76e22ffd0..b1b3bb7a45995dd9b45106227ef9535746c6eeb7 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/BasicSyntaxTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/BasicSyntaxTest.php @@ -141,33 +141,4 @@ public function testGetFullQualifiedTableName() { $this->assertSame('4', $num_matches, 'Found 4 records.'); } - /** - * Tests allowing square brackets in queries. - * - * @see \Drupal\Core\Database\Connection::prepareQuery() - */ - public function testAllowSquareBrackets() { - $this->connection->insert('test') - ->fields(['name']) - ->values([ - 'name' => '[square]', - ]) - ->execute(); - - // Note that this is a very bad example query because arguments should be - // passed in via the $args parameter. - $result = $this->connection->query("select name from {test} where name = '[square]'", [], ['allow_square_brackets' => TRUE]); - $this->assertSame('[square]', $result->fetchField()); - - // Test that allow_square_brackets has no effect on arguments. - $result = $this->connection->query("select [name] from {test} where [name] = :value", [':value' => '[square]']); - $this->assertSame('[square]', $result->fetchField()); - $result = $this->connection->query("select name from {test} where name = :value", [':value' => '[square]'], ['allow_square_brackets' => TRUE]); - $this->assertSame('[square]', $result->fetchField()); - - // Test square brackets using the query builder. - $result = $this->connection->select('test')->fields('test', ['name'])->condition('name', '[square]')->execute(); - $this->assertSame('[square]', $result->fetchField()); - } - } diff --git a/core/tests/Drupal/KernelTests/Core/Database/DriverSpecificKernelTestBase.php b/core/tests/Drupal/KernelTests/Core/Database/DriverSpecificKernelTestBase.php index a84046e7d971d7b77fe1c27c2cbad6b763f993cb..eb459eb3a151ad15621ac975d918160fc203eb9b 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/DriverSpecificKernelTestBase.php +++ b/core/tests/Drupal/KernelTests/Core/Database/DriverSpecificKernelTestBase.php @@ -27,11 +27,11 @@ abstract class DriverSpecificKernelTestBase extends KernelTestBase { * @inheritdoc */ protected function setUp(): void { - parent::setUp(); - $this->connection = Database::getConnection(); - - $running_provider = $this->connection->getProvider(); - $running_driver = $this->connection->driver(); + // Find the current SUT database driver from the connection info. If that + // is not the one the test requires, skip before test database + // initialization so to save cycles. + $this->root = static::getDrupalRoot(); + $connectionInfo = $this->getDatabaseConnectionInfo(); $test_class_parts = explode('\\', get_class($this)); $expected_provider = $test_class_parts[2] ?? ''; for ($i = 3; $i < count($test_class_parts); $i++) { @@ -40,6 +40,17 @@ protected function setUp(): void { break; } } + if ($connectionInfo['default']['driver'] !== $expected_driver) { + $this->markTestSkipped("This test only runs for the database driver '$expected_driver'. Current database driver is '{$connectionInfo['default']['driver']}'."); + } + + parent::setUp(); + $this->connection = Database::getConnection(); + + // After database initialization, the database driver may be not provided + // by the expected module; skip test in that case. + $running_provider = $this->connection->getProvider(); + $running_driver = $this->connection->driver(); if ($running_provider !== $expected_provider || $running_driver !== $expected_driver) { $this->markTestSkipped("This test only runs for the database driver '$expected_driver' provided by the '$expected_provider' module. Connected database driver is '$running_driver' provided by '$running_provider'."); } diff --git a/core/tests/Drupal/KernelTests/Core/Database/DriverSpecificSchemaTestBase.php b/core/tests/Drupal/KernelTests/Core/Database/DriverSpecificSchemaTestBase.php index bbf5de225c6cdb6312f7c137aed1957fd2cd90b8..de74ae5437f12baaa9f55d6f4ec2f32bd498ad81 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/DriverSpecificSchemaTestBase.php +++ b/core/tests/Drupal/KernelTests/Core/Database/DriverSpecificSchemaTestBase.php @@ -308,28 +308,13 @@ public function testSchema(): void { // Check that the ID sequence gets renamed when the table is renamed. $this->checkSequenceRenaming($new_table_name); - - // Use database specific data type and ensure that table is created. - $table_specification = [ - 'description' => 'Schema table description.', - 'fields' => [ - 'timestamp' => [ - 'mysql_type' => 'timestamp', - 'pgsql_type' => 'timestamp', - 'sqlite_type' => 'datetime', - 'not null' => FALSE, - 'default' => NULL, - ], - ], - ]; - try { - $this->schema->createTable('test_timestamp', $table_specification); - } - catch (\Exception $e) { - } - $this->assertTrue($this->schema->tableExists('test_timestamp'), 'Table with database specific datatype was created.'); } + /** + * Tests creating a table with database specific data type. + */ + abstract public function testTableWithSpecificDataType(): void; + /** * Tests creating unsigned columns and data integrity thereof. */ diff --git a/core/tests/Drupal/KernelTests/Core/Database/DriverSpecificSyntaxTestBase.php b/core/tests/Drupal/KernelTests/Core/Database/DriverSpecificSyntaxTestBase.php new file mode 100644 index 0000000000000000000000000000000000000000..b40d8786e3570fa6fcd3b1ab94f34060b4286294 --- /dev/null +++ b/core/tests/Drupal/KernelTests/Core/Database/DriverSpecificSyntaxTestBase.php @@ -0,0 +1,44 @@ +<?php + +namespace Drupal\KernelTests\Core\Database; + +/** + * Tests driver specific SQL syntax interpretation. + */ +abstract class DriverSpecificSyntaxTestBase extends DriverSpecificDatabaseTestBase { + + /** + * Tests allowing square brackets in queries. + * + * This method should be overridden if the SQL syntax of the test queries is + * not compatible with a non-core database driver. For example, the unquoted + * 'name' identifier in Oracle is a reserved keyword that would let the test + * query fail. + * + * @see \Drupal\Core\Database\Connection::prepareQuery() + */ + public function testAllowSquareBrackets() { + $this->connection->insert('test') + ->fields(['name']) + ->values([ + 'name' => '[square]', + ]) + ->execute(); + + // Note that this is a very bad example query because arguments should be + // passed in via the $args parameter. + $result = $this->connection->query("select name from {test} where name = '[square]'", [], ['allow_square_brackets' => TRUE]); + $this->assertSame('[square]', $result->fetchField()); + + // Test that allow_square_brackets has no effect on arguments. + $result = $this->connection->query("select [name] from {test} where [name] = :value", [':value' => '[square]']); + $this->assertSame('[square]', $result->fetchField()); + $result = $this->connection->query("select name from {test} where name = :value", [':value' => '[square]'], ['allow_square_brackets' => TRUE]); + $this->assertSame('[square]', $result->fetchField()); + + // Test square brackets using the query builder. + $result = $this->connection->select('test')->fields('test', ['name'])->condition('name', '[square]')->execute(); + $this->assertSame('[square]', $result->fetchField()); + } + +} diff --git a/core/tests/Drupal/KernelTests/Core/Database/TransactionTest.php b/core/tests/Drupal/KernelTests/Core/Database/DriverSpecificTransactionTestBase.php similarity index 98% rename from core/tests/Drupal/KernelTests/Core/Database/TransactionTest.php rename to core/tests/Drupal/KernelTests/Core/Database/DriverSpecificTransactionTestBase.php index 8adc5840631e6a8f5e2d2798a64f6bf95749148b..ebbf60826ad1400bdb6e4096a621a75c368a5fa8 100644 --- a/core/tests/Drupal/KernelTests/Core/Database/TransactionTest.php +++ b/core/tests/Drupal/KernelTests/Core/Database/DriverSpecificTransactionTestBase.php @@ -26,9 +26,13 @@ * Do more stuff * Should still be in transaction A * - * @group Database + * These method can be overridden by non-core database driver if their + * transaction behavior is different from core. For example, both oci8 (Oracle) + * and mysqli (MySql) clients do not have a solution to check if a transaction + * is active, and mysqli does not fail when rolling back and no transaction + * active. */ -class TransactionTest extends DatabaseTestBase { +class DriverSpecificTransactionTestBase extends DriverSpecificDatabaseTestBase { /** * Encapsulates a transaction's "inner layer" with an "outer layer".