Security update for Core, with self-updated composer
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Database / LoggingTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Database;
4
5 use Drupal\Core\Database\Database;
6
7 /**
8  * Tests the query logging facility.
9  *
10  * @group Database
11  */
12 class LoggingTest extends DatabaseTestBase {
13
14   /**
15    * Tests that we can log the existence of a query.
16    */
17   public function testEnableLogging() {
18     Database::startLog('testing');
19
20     db_query('SELECT name FROM {test} WHERE age > :age', [':age' => 25])->fetchCol();
21     db_query('SELECT age FROM {test} WHERE name = :name', [':name' => 'Ringo'])->fetchCol();
22
23     // Trigger a call that does not have file in the backtrace.
24     call_user_func_array('db_query', ['SELECT age FROM {test} WHERE name = :name', [':name' => 'Ringo']])->fetchCol();
25
26     $queries = Database::getLog('testing', 'default');
27
28     $this->assertEqual(count($queries), 3, 'Correct number of queries recorded.');
29
30     foreach ($queries as $query) {
31       $this->assertEqual($query['caller']['function'], __FUNCTION__, 'Correct function in query log.');
32     }
33   }
34
35   /**
36    * Tests that we can run two logs in parallel.
37    */
38   public function testEnableMultiLogging() {
39     Database::startLog('testing1');
40
41     db_query('SELECT name FROM {test} WHERE age > :age', [':age' => 25])->fetchCol();
42
43     Database::startLog('testing2');
44
45     db_query('SELECT age FROM {test} WHERE name = :name', [':name' => 'Ringo'])->fetchCol();
46
47     $queries1 = Database::getLog('testing1');
48     $queries2 = Database::getLog('testing2');
49
50     $this->assertEqual(count($queries1), 2, 'Correct number of queries recorded for log 1.');
51     $this->assertEqual(count($queries2), 1, 'Correct number of queries recorded for log 2.');
52   }
53
54   /**
55    * Tests logging queries against multiple targets on the same connection.
56    */
57   public function testEnableTargetLogging() {
58     // Clone the primary credentials to a replica connection and to another fake
59     // connection.
60     $connection_info = Database::getConnectionInfo('default');
61     Database::addConnectionInfo('default', 'replica', $connection_info['default']);
62
63     Database::startLog('testing1');
64
65     db_query('SELECT name FROM {test} WHERE age > :age', [':age' => 25])->fetchCol();
66
67     db_query('SELECT age FROM {test} WHERE name = :name', [':name' => 'Ringo'], ['target' => 'replica'])->fetchCol();
68
69     $queries1 = Database::getLog('testing1');
70
71     $this->assertEqual(count($queries1), 2, 'Recorded queries from all targets.');
72     $this->assertEqual($queries1[0]['target'], 'default', 'First query used default target.');
73     $this->assertEqual($queries1[1]['target'], 'replica', 'Second query used replica target.');
74   }
75
76   /**
77    * Tests that logs to separate targets use the same connection properly.
78    *
79    * This test is identical to the one above, except that it doesn't create
80    * a fake target so the query should fall back to running on the default
81    * target.
82    */
83   public function testEnableTargetLoggingNoTarget() {
84     Database::startLog('testing1');
85
86     db_query('SELECT name FROM {test} WHERE age > :age', [':age' => 25])->fetchCol();
87
88     // We use "fake" here as a target because any non-existent target will do.
89     // However, because all of the tests in this class share a single page
90     // request there is likely to be a target of "replica" from one of the other
91     // unit tests, so we use a target here that we know with absolute certainty
92     // does not exist.
93     db_query('SELECT age FROM {test} WHERE name = :name', [':name' => 'Ringo'], ['target' => 'fake'])->fetchCol();
94
95     $queries1 = Database::getLog('testing1');
96
97     $this->assertEqual(count($queries1), 2, 'Recorded queries from all targets.');
98     $this->assertEqual($queries1[0]['target'], 'default', 'First query used default target.');
99     $this->assertEqual($queries1[1]['target'], 'default', 'Second query used default target as fallback.');
100   }
101
102   /**
103    * Tests that we can log queries separately on different connections.
104    */
105   public function testEnableMultiConnectionLogging() {
106     // Clone the primary credentials to a fake connection.
107     // That both connections point to the same physical database is irrelevant.
108     $connection_info = Database::getConnectionInfo('default');
109     Database::addConnectionInfo('test2', 'default', $connection_info['default']);
110
111     Database::startLog('testing1');
112     Database::startLog('testing1', 'test2');
113
114     db_query('SELECT name FROM {test} WHERE age > :age', [':age' => 25])->fetchCol();
115
116     $old_key = db_set_active('test2');
117
118     db_query('SELECT age FROM {test} WHERE name = :name', [':name' => 'Ringo'], ['target' => 'replica'])->fetchCol();
119
120     db_set_active($old_key);
121
122     $queries1 = Database::getLog('testing1');
123     $queries2 = Database::getLog('testing1', 'test2');
124
125     $this->assertEqual(count($queries1), 1, 'Correct number of queries recorded for first connection.');
126     $this->assertEqual(count($queries2), 1, 'Correct number of queries recorded for second connection.');
127   }
128
129   /**
130    * Tests that getLog with a wrong key return an empty array.
131    */
132   public function testGetLoggingWrongKey() {
133     $result = Database::getLog('wrong');
134
135     $this->assertEqual($result, [], 'The function getLog with a wrong key returns an empty array.');
136   }
137
138 }