Unverified Commit 65656ac6 authored by alexpott's avatar alexpott
Browse files

Issue #3152398 by Hardik_Patel_12, ravi.shankar, rajandro, siddhant.bhosale,...

Issue #3152398 by Hardik_Patel_12, ravi.shankar, rajandro, siddhant.bhosale, daffie, mondrake, mondrake, longwave, voleger, andypost, catch, Berdir: Change static queries to dynamic queries in core/tests/Drupal
parent 46da61e3
......@@ -24,7 +24,17 @@ protected function setUpSettings() {
// 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 @@ protected function setUpSettings() {
// 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 @@ protected function setUpSettings() {
// 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 @@ public function testDatabaseLoaded() {
// 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 @@ public function testScriptLoad() {
}
// 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 @@ protected function setUp(): void {
}
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 @@ protected function assertCRUD($entity_type, UserInterface $user1) {
// 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 @@ protected function assertCRUD($entity_type, UserInterface $user1) {
// 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 @@ public function testWasDefaultRevision() {
*/
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 @@ public function testGarbageCollection() {
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 @@ public function testDump() {
$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);
......
......@@ -297,11 +297,11 @@ protected function tearDown(): void {
$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", [
':type' => 'table',
':table_name' => '%',
':pattern' => 'sqlite_%',
])->fetchAllKeyed(0, 0);
$result = $connection->select($this->databasePrefix . "sqlite_master", 'sql_m')->fields('sql_m', ['name'])
->condition('type', 'table')
->condition('name', '%', 'LIKE')
->condition('name', 'sqlite_%', 'NOT LIKE')
->execute()->fetchAllKeyed(0, 0);
$this->assertTrue(empty($result), 'All test tables have been removed.');
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment