From fe38a9c9108bd47af278ec1d5762431d8e0976f1 Mon Sep 17 00:00:00 2001 From: Alex Pott Date: Mon, 20 Jul 2020 10:16:11 +0100 Subject: [PATCH] Issue #3152398 by Hardik_Patel_12, alexpott, rajandro, ravi.shankar, ultrabob, siddhant.bhosale, daffie, andypost, voleger, Berdir, mondrake, catch, longwave: Change static queries to dynamic queries in core/tests/Drupal --- .../InstallerDatabaseErrorMessagesTest.php | 12 +++++++++++- .../Installer/InstallerTranslationTest.php | 14 ++++++++++++-- .../Update/UpdatePathTestBaseTest.php | 5 ++++- .../KernelTests/Core/Command/DbDumpTest.php | 2 +- .../Config/Storage/DatabaseStorageTest.php | 2 +- .../KernelTests/Core/Entity/EntityApiTest.php | 18 ++++++++++-------- .../RevisionableContentEntityBaseTest.php | 9 +++++---- .../KeyValueStore/GarbageCollectionTest.php | 10 +++++----- .../Core/Routing/MatcherDumperTest.php | 6 +++++- .../Drupal/KernelTests/KernelTestBaseTest.php | 14 +++++++------- 10 files changed, 61 insertions(+), 31 deletions(-) diff --git a/core/tests/Drupal/FunctionalTests/Installer/InstallerDatabaseErrorMessagesTest.php b/core/tests/Drupal/FunctionalTests/Installer/InstallerDatabaseErrorMessagesTest.php index 021fbc9a221c..036196471e11 100644 --- a/core/tests/Drupal/FunctionalTests/Installer/InstallerDatabaseErrorMessagesTest.php +++ b/core/tests/Drupal/FunctionalTests/Installer/InstallerDatabaseErrorMessagesTest.php @@ -24,7 +24,17 @@ class InstallerDatabaseErrorMessagesTest extends InstallerTestBase { // it will try and create the drupal_install_test table as this is part of // the standard database tests performed by the installer in // Drupal\Core\Database\Install\Tasks. - Database::getConnection('default')->query('CREATE TABLE {drupal_install_test} (id int NOT NULL PRIMARY KEY)'); + $spec = [ + 'fields' => [ + 'id' => [ + 'type' => 'int', + 'not null' => TRUE, + ], + ], + 'primary key' => ['id'], + ]; + + Database::getConnection('default')->schema()->createTable('drupal_install_test', $spec); parent::setUpSettings(); } diff --git a/core/tests/Drupal/FunctionalTests/Installer/InstallerTranslationTest.php b/core/tests/Drupal/FunctionalTests/Installer/InstallerTranslationTest.php index 5d86fc54fdb3..4ce80621bf07 100644 --- a/core/tests/Drupal/FunctionalTests/Installer/InstallerTranslationTest.php +++ b/core/tests/Drupal/FunctionalTests/Installer/InstallerTranslationTest.php @@ -53,7 +53,17 @@ class InstallerTranslationTest extends InstallerTestBase { // it will try and create the drupal_install_test table as this is part of // the standard database tests performed by the installer in // Drupal\Core\Database\Install\Tasks. - Database::getConnection('default')->query('CREATE TABLE {drupal_install_test} (id int NOT NULL PRIMARY KEY)'); + $spec = [ + 'fields' => [ + 'id' => [ + 'type' => 'int', + 'not null' => TRUE, + ], + ], + 'primary key' => ['id'], + ]; + + Database::getConnection('default')->schema()->createTable('drupal_install_test', $spec); parent::setUpSettings(); // Ensure that the error message translation is working. @@ -63,7 +73,7 @@ class InstallerTranslationTest extends InstallerTestBase { // cSpell:enable // Now do it successfully. - Database::getConnection('default')->query('DROP TABLE {drupal_install_test}'); + Database::getConnection('default')->schema()->dropTable('drupal_install_test'); parent::setUpSettings(); } diff --git a/core/tests/Drupal/FunctionalTests/Update/UpdatePathTestBaseTest.php b/core/tests/Drupal/FunctionalTests/Update/UpdatePathTestBaseTest.php index 34d2e8682974..689abd66767a 100644 --- a/core/tests/Drupal/FunctionalTests/Update/UpdatePathTestBaseTest.php +++ b/core/tests/Drupal/FunctionalTests/Update/UpdatePathTestBaseTest.php @@ -41,7 +41,10 @@ class UpdatePathTestBaseTest extends UpdatePathTestBase { // Ensure that all {router} entries can be unserialized. If they cannot be // unserialized a notice will be thrown by PHP. - $result = \Drupal::database()->query("SELECT name, route from {router}")->fetchAllKeyed(0, 1); + $result = \Drupal::database()->select('router', 'r') + ->fields('r', ['name', 'route']) + ->execute() + ->fetchAllKeyed(0, 1); // For the purpose of fetching the notices and displaying more helpful error // messages, let's override the error handler temporarily. set_error_handler(function ($severity, $message, $filename, $lineno) { diff --git a/core/tests/Drupal/KernelTests/Core/Command/DbDumpTest.php b/core/tests/Drupal/KernelTests/Core/Command/DbDumpTest.php index 2192d0ec73ca..3e16fff33316 100644 --- a/core/tests/Drupal/KernelTests/Core/Command/DbDumpTest.php +++ b/core/tests/Drupal/KernelTests/Core/Command/DbDumpTest.php @@ -213,7 +213,7 @@ class DbDumpTest extends KernelTestBase { } // Ensure the test config has been replaced. - $config = unserialize($connection->query("SELECT data FROM {config} WHERE name = 'test_config'")->fetchField()); + $config = unserialize($connection->select('config', 'c')->fields('c', ['data'])->condition('name', 'test_config')->execute()->fetchField()); $this->assertIdentical($config, $this->data, 'Script has properly restored the config table data.'); // Ensure the cache data was not exported. diff --git a/core/tests/Drupal/KernelTests/Core/Config/Storage/DatabaseStorageTest.php b/core/tests/Drupal/KernelTests/Core/Config/Storage/DatabaseStorageTest.php index 3bc7738d20bc..b26b3fd53c6b 100644 --- a/core/tests/Drupal/KernelTests/Core/Config/Storage/DatabaseStorageTest.php +++ b/core/tests/Drupal/KernelTests/Core/Config/Storage/DatabaseStorageTest.php @@ -26,7 +26,7 @@ class DatabaseStorageTest extends ConfigStorageTestBase { } protected function read($name) { - $data = Database::getConnection()->query('SELECT data FROM {config} WHERE name = :name', [':name' => $name])->fetchField(); + $data = Database::getConnection()->select('config', 'c')->fields('c', ['data'])->condition('name', $name)->execute()->fetchField(); return unserialize($data); } diff --git a/core/tests/Drupal/KernelTests/Core/Entity/EntityApiTest.php b/core/tests/Drupal/KernelTests/Core/Entity/EntityApiTest.php index 6b8e5149432c..f9ec4d59a1f7 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/EntityApiTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/EntityApiTest.php @@ -97,15 +97,16 @@ class EntityApiTest extends EntityKernelTestBase { // Verify that all data got deleted. $definition = \Drupal::entityTypeManager()->getDefinition($entity_type); $connection = Database::getConnection(); - $this->assertEqual(0, $connection->query('SELECT COUNT(*) FROM {' . $definition->getBaseTable() . '}')->fetchField(), 'Base table was emptied'); + $this->assertEqual(0, (int) $connection->select($definition->getBaseTable())->countQuery()->execute()->fetchField(), 'Base table was emptied'); + if ($data_table = $definition->getDataTable()) { - $this->assertEqual(0, $connection->query('SELECT COUNT(*) FROM {' . $data_table . '}')->fetchField(), 'Data table was emptied'); + $this->assertEqual(0, (int) $connection->select($data_table)->countQuery()->execute()->fetchField(), 'Data table was emptied'); } if ($revision_table = $definition->getRevisionTable()) { - $this->assertEqual(0, $connection->query('SELECT COUNT(*) FROM {' . $revision_table . '}')->fetchField(), 'Data table was emptied'); + $this->assertEqual(0, (int) $connection->select($revision_table)->countQuery()->execute()->fetchField(), 'Revision table was emptied'); } if ($revision_data_table = $definition->getRevisionDataTable()) { - $this->assertEqual(0, $connection->query('SELECT COUNT(*) FROM {' . $revision_data_table . '}')->fetchField(), 'Data table was emptied'); + $this->assertEqual(0, (int) $connection->select($revision_data_table)->countQuery()->execute()->fetchField(), 'Revision data table was emptied'); } // Test deleting a list of entities not indexed by entity id. @@ -125,15 +126,16 @@ class EntityApiTest extends EntityKernelTestBase { // Verify that all data got deleted from the tables. $definition = \Drupal::entityTypeManager()->getDefinition($entity_type); - $this->assertEqual(0, $connection->query('SELECT COUNT(*) FROM {' . $definition->getBaseTable() . '}')->fetchField(), 'Base table was emptied'); + $this->assertEqual(0, (int) $connection->select($definition->getBaseTable())->countQuery()->execute()->fetchField(), 'Base table was emptied'); + if ($data_table = $definition->getDataTable()) { - $this->assertEqual(0, $connection->query('SELECT COUNT(*) FROM {' . $data_table . '}')->fetchField(), 'Data table was emptied'); + $this->assertEqual(0, (int) $connection->select($data_table)->countQuery()->execute()->fetchField(), 'Data table was emptied'); } if ($revision_table = $definition->getRevisionTable()) { - $this->assertEqual(0, $connection->query('SELECT COUNT(*) FROM {' . $revision_table . '}')->fetchField(), 'Data table was emptied'); + $this->assertEqual(0, (int) $connection->select($revision_table)->countQuery()->execute()->fetchField(), 'Revision table was emptied'); } if ($revision_data_table = $definition->getRevisionDataTable()) { - $this->assertEqual(0, $connection->query('SELECT COUNT(*) FROM {' . $revision_data_table . '}')->fetchField(), 'Data table was emptied'); + $this->assertEqual(0, (int) $connection->select($revision_data_table)->countQuery()->execute()->fetchField(), 'Revision data table was emptied'); } } diff --git a/core/tests/Drupal/KernelTests/Core/Entity/RevisionableContentEntityBaseTest.php b/core/tests/Drupal/KernelTests/Core/Entity/RevisionableContentEntityBaseTest.php index c83a6bde4caf..6cb8c139969f 100644 --- a/core/tests/Drupal/KernelTests/Core/Entity/RevisionableContentEntityBaseTest.php +++ b/core/tests/Drupal/KernelTests/Core/Entity/RevisionableContentEntityBaseTest.php @@ -169,10 +169,11 @@ class RevisionableContentEntityBaseTest extends EntityKernelTestBase { */ protected function assertItemsTableCount($count, EntityTypeInterface $definition) { $connection = Database::getConnection(); - $this->assertEqual(1, $connection->query('SELECT COUNT(*) FROM {' . $definition->getBaseTable() . '}')->fetchField()); - $this->assertEqual(1, $connection->query('SELECT COUNT(*) FROM {' . $definition->getDataTable() . '}')->fetchField()); - $this->assertEqual($count, $connection->query('SELECT COUNT(*) FROM {' . $definition->getRevisionTable() . '}')->fetchField()); - $this->assertEqual($count, $connection->query('SELECT COUNT(*) FROM {' . $definition->getRevisionDataTable() . '}')->fetchField()); + $this->assertEqual(1, (int) $connection->select($definition->getBaseTable())->countQuery()->execute()->fetchField()); + $this->assertEqual(1, (int) $connection->select($definition->getDataTable())->countQuery()->execute()->fetchField()); + $this->assertEqual($count, (int) $connection->select($definition->getRevisionTable())->countQuery()->execute()->fetchField()); + $this->assertEqual($count, (int) $connection->select($definition->getRevisionDataTable())->countQuery()->execute()->fetchField()); + } /** diff --git a/core/tests/Drupal/KernelTests/Core/KeyValueStore/GarbageCollectionTest.php b/core/tests/Drupal/KernelTests/Core/KeyValueStore/GarbageCollectionTest.php index c702fb92967e..6d774687f970 100644 --- a/core/tests/Drupal/KernelTests/Core/KeyValueStore/GarbageCollectionTest.php +++ b/core/tests/Drupal/KernelTests/Core/KeyValueStore/GarbageCollectionTest.php @@ -60,11 +60,11 @@ class GarbageCollectionTest extends KernelTestBase { system_cron(); // Query the database and confirm that the stale records were deleted. - $result = $connection->query( - 'SELECT name, value FROM {key_value_expire} WHERE collection = :collection', - [ - ':collection' => $collection, - ])->fetchAll(); + $result = $connection->select('key_value_expire', 'kvp') + ->fields('kvp', ['name']) + ->condition('collection', $collection) + ->execute() + ->fetchAll(); $this->assertCount(1, $result, 'Only one item remains after garbage collection'); } diff --git a/core/tests/Drupal/KernelTests/Core/Routing/MatcherDumperTest.php b/core/tests/Drupal/KernelTests/Core/Routing/MatcherDumperTest.php index 41873110a43a..e0d3e8a59d2c 100644 --- a/core/tests/Drupal/KernelTests/Core/Routing/MatcherDumperTest.php +++ b/core/tests/Drupal/KernelTests/Core/Routing/MatcherDumperTest.php @@ -118,7 +118,11 @@ class MatcherDumperTest extends KernelTestBase { $dumper->dump(['provider' => 'test']); - $record = $connection->query("SELECT * FROM {test_routes} WHERE name= :name", [':name' => 'test_route'])->fetchObject(); + $record = $connection->select('test_routes', 'tr') + ->fields('tr') + ->condition('name', 'test_route') + ->execute() + ->fetchObject(); $loaded_route = unserialize($record->route); diff --git a/core/tests/Drupal/KernelTests/KernelTestBaseTest.php b/core/tests/Drupal/KernelTests/KernelTestBaseTest.php index 26e0d8ed4e9b..40d325b594bd 100644 --- a/core/tests/Drupal/KernelTests/KernelTestBaseTest.php +++ b/core/tests/Drupal/KernelTests/KernelTestBaseTest.php @@ -292,19 +292,19 @@ class KernelTestBaseTest extends KernelTestBase { // point the original database connection is restored so we need to prefix // the tables. $connection = Database::getConnection(); - if ($connection->databaseType() != 'sqlite') { - $tables = $connection->schema()->findTables($this->databasePrefix . '%'); - $this->assertTrue(empty($tables), 'All test tables have been removed.'); - } - else { - $result = $connection->query("SELECT name FROM " . $this->databasePrefix . ".sqlite_master WHERE type = :type AND name LIKE :table_name AND name NOT LIKE :pattern", [ + if ($connection->databaseType() === 'sqlite') { + $result = $connection->query("SELECT name FROM " . $this->databasePrefix . + ".sqlite_master WHERE type = :type AND name LIKE :table_name AND name NOT LIKE :pattern", [ ':type' => 'table', ':table_name' => '%', ':pattern' => 'sqlite_%', ])->fetchAllKeyed(0, 0); - $this->assertTrue(empty($result), 'All test tables have been removed.'); } + else { + $tables = $connection->schema()->findTables($this->databasePrefix . '%'); + $this->assertTrue(empty($tables), 'All test tables have been removed.'); + } } /**