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

merge-requests/2/head
Alex Pott 2020-07-20 10:16:11 +01:00
parent f884044d46
commit fe38a9c910
No known key found for this signature in database
GPG Key ID: 31905460D4A69276
10 changed files with 61 additions and 31 deletions

View File

@ -24,7 +24,17 @@ class InstallerDatabaseErrorMessagesTest extends InstallerTestBase {
// it will try and create the drupal_install_test table as this is part of // it will try and create the drupal_install_test table as this is part of
// the standard database tests performed by the installer in // the standard database tests performed by the installer in
// Drupal\Core\Database\Install\Tasks. // 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(); parent::setUpSettings();
} }

View File

@ -53,7 +53,17 @@ class InstallerTranslationTest extends InstallerTestBase {
// it will try and create the drupal_install_test table as this is part of // it will try and create the drupal_install_test table as this is part of
// the standard database tests performed by the installer in // the standard database tests performed by the installer in
// Drupal\Core\Database\Install\Tasks. // 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(); parent::setUpSettings();
// Ensure that the error message translation is working. // Ensure that the error message translation is working.
@ -63,7 +73,7 @@ class InstallerTranslationTest extends InstallerTestBase {
// cSpell:enable // cSpell:enable
// Now do it successfully. // Now do it successfully.
Database::getConnection('default')->query('DROP TABLE {drupal_install_test}'); Database::getConnection('default')->schema()->dropTable('drupal_install_test');
parent::setUpSettings(); parent::setUpSettings();
} }

View File

@ -41,7 +41,10 @@ class UpdatePathTestBaseTest extends UpdatePathTestBase {
// Ensure that all {router} entries can be unserialized. If they cannot be // Ensure that all {router} entries can be unserialized. If they cannot be
// unserialized a notice will be thrown by PHP. // 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 // For the purpose of fetching the notices and displaying more helpful error
// messages, let's override the error handler temporarily. // messages, let's override the error handler temporarily.
set_error_handler(function ($severity, $message, $filename, $lineno) { set_error_handler(function ($severity, $message, $filename, $lineno) {

View File

@ -213,7 +213,7 @@ class DbDumpTest extends KernelTestBase {
} }
// Ensure the test config has been replaced. // 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.'); $this->assertIdentical($config, $this->data, 'Script has properly restored the config table data.');
// Ensure the cache data was not exported. // Ensure the cache data was not exported.

View File

@ -26,7 +26,7 @@ class DatabaseStorageTest extends ConfigStorageTestBase {
} }
protected function read($name) { 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); return unserialize($data);
} }

View File

@ -97,15 +97,16 @@ class EntityApiTest extends EntityKernelTestBase {
// Verify that all data got deleted. // Verify that all data got deleted.
$definition = \Drupal::entityTypeManager()->getDefinition($entity_type); $definition = \Drupal::entityTypeManager()->getDefinition($entity_type);
$connection = Database::getConnection(); $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()) { 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()) { 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()) { 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. // 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. // Verify that all data got deleted from the tables.
$definition = \Drupal::entityTypeManager()->getDefinition($entity_type); $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()) { 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()) { 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()) { 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');
} }
} }

View File

@ -169,10 +169,11 @@ class RevisionableContentEntityBaseTest extends EntityKernelTestBase {
*/ */
protected function assertItemsTableCount($count, EntityTypeInterface $definition) { protected function assertItemsTableCount($count, EntityTypeInterface $definition) {
$connection = Database::getConnection(); $connection = Database::getConnection();
$this->assertEqual(1, $connection->query('SELECT COUNT(*) FROM {' . $definition->getBaseTable() . '}')->fetchField()); $this->assertEqual(1, (int) $connection->select($definition->getBaseTable())->countQuery()->execute()->fetchField());
$this->assertEqual(1, $connection->query('SELECT COUNT(*) FROM {' . $definition->getDataTable() . '}')->fetchField()); $this->assertEqual(1, (int) $connection->select($definition->getDataTable())->countQuery()->execute()->fetchField());
$this->assertEqual($count, $connection->query('SELECT COUNT(*) FROM {' . $definition->getRevisionTable() . '}')->fetchField()); $this->assertEqual($count, (int) $connection->select($definition->getRevisionTable())->countQuery()->execute()->fetchField());
$this->assertEqual($count, $connection->query('SELECT COUNT(*) FROM {' . $definition->getRevisionDataTable() . '}')->fetchField()); $this->assertEqual($count, (int) $connection->select($definition->getRevisionDataTable())->countQuery()->execute()->fetchField());
} }
/** /**

View File

@ -60,11 +60,11 @@ class GarbageCollectionTest extends KernelTestBase {
system_cron(); system_cron();
// Query the database and confirm that the stale records were deleted. // Query the database and confirm that the stale records were deleted.
$result = $connection->query( $result = $connection->select('key_value_expire', 'kvp')
'SELECT name, value FROM {key_value_expire} WHERE collection = :collection', ->fields('kvp', ['name'])
[ ->condition('collection', $collection)
':collection' => $collection, ->execute()
])->fetchAll(); ->fetchAll();
$this->assertCount(1, $result, 'Only one item remains after garbage collection'); $this->assertCount(1, $result, 'Only one item remains after garbage collection');
} }

View File

@ -118,7 +118,11 @@ class MatcherDumperTest extends KernelTestBase {
$dumper->dump(['provider' => 'test']); $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); $loaded_route = unserialize($record->route);

View File

@ -292,19 +292,19 @@ class KernelTestBaseTest extends KernelTestBase {
// point the original database connection is restored so we need to prefix // point the original database connection is restored so we need to prefix
// the tables. // the tables.
$connection = Database::getConnection(); $connection = Database::getConnection();
if ($connection->databaseType() != 'sqlite') { if ($connection->databaseType() === 'sqlite') {
$tables = $connection->schema()->findTables($this->databasePrefix . '%'); $result = $connection->query("SELECT name FROM " . $this->databasePrefix .
$this->assertTrue(empty($tables), 'All test tables have been removed.'); ".sqlite_master WHERE type = :type AND name LIKE :table_name AND name NOT LIKE :pattern", [
}
else {
$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', ':type' => 'table',
':table_name' => '%', ':table_name' => '%',
':pattern' => 'sqlite_%', ':pattern' => 'sqlite_%',
])->fetchAllKeyed(0, 0); ])->fetchAllKeyed(0, 0);
$this->assertTrue(empty($result), 'All test tables have been removed.'); $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.');
}
} }
/** /**