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