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
parent
f884044d46
commit
fe38a9c910
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
|
|
|
@ -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) {
|
||||
|
|
|
@ -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.
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
|
@ -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');
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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());
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -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');
|
||||
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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.');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in New Issue