public function TemporaryQueryTest::testTemporaryQuery in Drupal 9
Same name and namespace in other branches
- 8 core/modules/system/tests/src/Functional/Database/TemporaryQueryTest.php \Drupal\Tests\system\Functional\Database\TemporaryQueryTest::testTemporaryQuery()
Confirms that temporary tables work and are limited to one request.
File
- core/
modules/ system/ tests/ src/ Functional/ Database/ TemporaryQueryTest.php, line 35
Class
- TemporaryQueryTest
- Tests the temporary query functionality.
Namespace
Drupal\Tests\system\Functional\DatabaseCode
public function testTemporaryQuery() {
$this
->expectDeprecation('Connection::queryTemporary() is deprecated in drupal:9.3.0 and is removed from drupal:10.0.0. There is no replacement. See https://www.drupal.org/node/3211781');
$this
->expectDeprecation('Connection::generateTemporaryTableName() is deprecated in drupal:9.3.0 and is removed from drupal:10.0.0. There is no replacement. See https://www.drupal.org/node/3211781');
$connection = Database::getConnection();
$this
->drupalGet('database_test/db_query_temporary');
$data = json_decode($this
->getSession()
->getPage()
->getContent());
if ($data) {
$this
->assertEquals($this
->countTableRows('test'), $data->row_count, 'The temporary table contains the correct amount of rows.');
$this
->assertFalse($connection
->schema()
->tableExists($data->table_name), 'The temporary table is, indeed, temporary.');
}
else {
$this
->fail('The creation of the temporary table failed.');
}
// Now try to run two temporary queries in the same request.
$table_name_test = $connection
->queryTemporary('SELECT [name] FROM {test}', []);
$table_name_task = $connection
->queryTemporary('SELECT [pid] FROM {test_task}', []);
$this
->assertEquals($this
->countTableRows('test'), $this
->countTableRows($table_name_test), 'A temporary table was created successfully in this request.');
$this
->assertEquals($this
->countTableRows('test_task'), $this
->countTableRows($table_name_task), 'A second temporary table was created successfully in this request.');
// Check that leading whitespace and comments do not cause problems
// in the modified query.
$sql = "\n -- Let's select some rows into a temporary table\n SELECT [name] FROM {test}\n ";
$table_name_test = $connection
->queryTemporary($sql, []);
$this
->assertEquals($this
->countTableRows('test'), $this
->countTableRows($table_name_test), 'Leading white space and comments do not interfere with temporary table creation.');
}