3 namespace Drupal\system\Tests\Database;
6 * Tests the temporary query functionality.
10 class TemporaryQueryTest extends DatabaseWebTestBase {
17 public static $modules = ['database_test'];
20 * Returns the number of rows of a table.
22 public function countTableRows($table_name) {
23 return db_select($table_name)->countQuery()->execute()->fetchField();
27 * Confirms that temporary tables work and are limited to one request.
29 public function testTemporaryQuery() {
30 $this->drupalGet('database_test/db_query_temporary');
31 $data = json_decode($this->getRawContent());
33 $this->assertEqual($this->countTableRows('test'), $data->row_count, 'The temporary table contains the correct amount of rows.');
34 $this->assertFalse(db_table_exists($data->table_name), 'The temporary table is, indeed, temporary.');
37 $this->fail('The creation of the temporary table failed.');
40 // Now try to run two db_query_temporary() in the same request.
41 $table_name_test = db_query_temporary('SELECT name FROM {test}', []);
42 $table_name_task = db_query_temporary('SELECT pid FROM {test_task}', []);
44 $this->assertEqual($this->countTableRows($table_name_test), $this->countTableRows('test'), 'A temporary table was created successfully in this request.');
45 $this->assertEqual($this->countTableRows($table_name_task), $this->countTableRows('test_task'), 'A second temporary table was created successfully in this request.');
47 // Check that leading whitespace and comments do not cause problems
48 // in the modified query.
50 -- Let's select some rows into a temporary table
51 SELECT name FROM {test}
53 $table_name_test = db_query_temporary($sql, []);
54 $this->assertEqual($this->countTableRows($table_name_test), $this->countTableRows('test'), 'Leading white space and comments do not interfere with temporary table creation.');