3 namespace Drupal\Tests\system\Functional\Database;
6 * Tests the temporary query functionality.
10 class TemporaryQueryTest extends DatabaseTestBase {
15 public static $modules = ['database_test'];
18 * Returns the number of rows of a table.
20 public function countTableRows($table_name) {
21 return db_select($table_name)->countQuery()->execute()->fetchField();
25 * Confirms that temporary tables work and are limited to one request.
27 public function testTemporaryQuery() {
28 $this->drupalGet('database_test/db_query_temporary');
29 $data = json_decode($this->getSession()->getPage()->getContent());
31 $this->assertEqual($this->countTableRows('test'), $data->row_count, 'The temporary table contains the correct amount of rows.');
32 $this->assertFalse(db_table_exists($data->table_name), 'The temporary table is, indeed, temporary.');
35 $this->fail('The creation of the temporary table failed.');
38 // Now try to run two db_query_temporary() in the same request.
39 $table_name_test = db_query_temporary('SELECT name FROM {test}', []);
40 $table_name_task = db_query_temporary('SELECT pid FROM {test_task}', []);
42 $this->assertEqual($this->countTableRows($table_name_test), $this->countTableRows('test'), 'A temporary table was created successfully in this request.');
43 $this->assertEqual($this->countTableRows($table_name_task), $this->countTableRows('test_task'), 'A second temporary table was created successfully in this request.');
45 // Check that leading whitespace and comments do not cause problems
46 // in the modified query.
48 -- Let's select some rows into a temporary table
49 SELECT name FROM {test}
51 $table_name_test = db_query_temporary($sql, []);
52 $this->assertEqual($this->countTableRows($table_name_test), $this->countTableRows('test'), 'Leading white space and comments do not interfere with temporary table creation.');