Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / system / tests / src / Functional / Database / TemporaryQueryTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\Database;
4
5 /**
6  * Tests the temporary query functionality.
7  *
8  * @group Database
9  */
10 class TemporaryQueryTest extends DatabaseTestBase {
11
12   /**
13    * {@inheritdoc}
14    */
15   public static $modules = ['database_test'];
16
17   /**
18    * Returns the number of rows of a table.
19    */
20   public function countTableRows($table_name) {
21     return db_select($table_name)->countQuery()->execute()->fetchField();
22   }
23
24   /**
25    * Confirms that temporary tables work and are limited to one request.
26    */
27   public function testTemporaryQuery() {
28     $this->drupalGet('database_test/db_query_temporary');
29     $data = json_decode($this->getSession()->getPage()->getContent());
30     if ($data) {
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.');
33     }
34     else {
35       $this->fail('The creation of the temporary table failed.');
36     }
37
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}', []);
41
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.');
44
45     // Check that leading whitespace and comments do not cause problems
46     // in the modified query.
47     $sql = "
48       -- Let's select some rows into a temporary table
49       SELECT name FROM {test}
50     ";
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.');
53   }
54
55 }