From cf3ae0f90dbbb6090eb989aebda76e87af6f9c24 Mon Sep 17 00:00:00 2001 From: catch <catch56@gmail.com> Date: Wed, 2 Nov 2022 17:36:27 +0000 Subject: [PATCH] Issue #3316923 by mondrake, daffie: Sort out more driver specific database kernel tests --- .../tests/src/Kernel/mysql/SchemaTest.php | 18 +++++ .../tests/src/Kernel/mysql/SyntaxTest.php | 13 ++++ .../src/Kernel/mysql/TransactionTest.php | 13 ++++ .../tests/src/Kernel/pgsql/SchemaTest.php | 18 +++++ .../tests/src/Kernel/pgsql/SyntaxTest.php | 13 ++++ .../src/Kernel/pgsql/TransactionTest.php | 13 ++++ .../tests/src/Kernel/sqlite/SchemaTest.php | 20 +++++- .../tests/src/Kernel/sqlite/SyntaxTest.php | 13 ++++ .../src/Kernel/sqlite/TransactionTest.php | 13 ++++ ...atabase_statement_monitoring_test.info.yml | 2 + .../src/LoggedStatementsTrait.php | 7 ++ .../src/mysql/Connection.php | 7 ++ .../src/mysql/Install/Tasks.php | 8 +++ .../src/pgsql/Connection.php | 7 ++ .../src/pgsql/Install/Tasks.php | 8 +++ .../src/sqlite/Connection.php | 7 ++ .../src/sqlite/Install/Tasks.php | 8 +++ .../Cache/EndOfTransactionQueriesTest.php | 70 +++++++------------ .../Core/Database/BasicSyntaxTest.php | 29 -------- .../Database/DriverSpecificKernelTestBase.php | 21 ++++-- .../Database/DriverSpecificSchemaTestBase.php | 25 ++----- .../Database/DriverSpecificSyntaxTestBase.php | 44 ++++++++++++ ... => DriverSpecificTransactionTestBase.php} | 8 ++- 23 files changed, 284 insertions(+), 101 deletions(-) create mode 100644 core/modules/mysql/tests/src/Kernel/mysql/SyntaxTest.php create mode 100644 core/modules/mysql/tests/src/Kernel/mysql/TransactionTest.php create mode 100644 core/modules/pgsql/tests/src/Kernel/pgsql/SyntaxTest.php create mode 100644 core/modules/pgsql/tests/src/Kernel/pgsql/TransactionTest.php create mode 100644 core/modules/sqlite/tests/src/Kernel/sqlite/SyntaxTest.php create mode 100644 core/modules/sqlite/tests/src/Kernel/sqlite/TransactionTest.php create mode 100644 core/tests/Drupal/KernelTests/Core/Database/DriverSpecificSyntaxTestBase.php rename core/tests/Drupal/KernelTests/Core/Database/{TransactionTest.php => DriverSpecificTransactionTestBase.php} (98%) diff --git a/core/modules/mysql/tests/src/Kernel/mysql/SchemaTest.php b/core/modules/mysql/tests/src/Kernel/mysql/SchemaTest.php index f08a04df893d..f219b373280d 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 000000000000..b5ad81267abe --- /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 000000000000..d9d5dd513cc7 --- /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 b32335e4b872..b7fee8694a83 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 000000000000..2da93a315c47 --- /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 000000000000..f9d7d0ed7309 --- /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 07b662419657..a3aec37553fd 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 000000000000..fd495ffefec7 --- /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 000000000000..1c69ab15929c --- /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 894715d976a7..33b576e85455 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 677f4fd89182..553755104a46 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 caa96940090d..2af9c20b7c9f 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 338e136cd5c0..a1d0c30d392d 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 43995d325a84..227673100d8f 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 0b95ddf53d4f..5ddc4b106eed 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 2b4201a9763a..0760cbc02448 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 4827f8c18b92..5694f5684a2a 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 1d735d511def..46f5a37638a7 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 773ec6a99b72..b1b3bb7a4599 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 a84046e7d971..eb459eb3a151 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 bbf5de225c6c..de74ae5437f1 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 000000000000..b40d8786e357 --- /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 8adc5840631e..ebbf60826ad1 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". -- GitLab