Security update for Core, with self-updated composer
[yaffs-website] / web / core / tests / Drupal / KernelTests / Core / Database / PrefixInfoTest.php
1 <?php
2
3 namespace Drupal\KernelTests\Core\Database;
4
5 use Drupal\Core\Database\Database;
6
7 /**
8  * Tests that the prefix info for a database schema is correct.
9  *
10  * @group Database
11  */
12 class PrefixInfoTest extends DatabaseTestBase {
13
14   /**
15    * Tests that DatabaseSchema::getPrefixInfo() returns the right database.
16    *
17    * We are testing if the return array of the method
18    * \Drupal\Core\Database\Driver\mysql\Schema::getPrefixInfo(). This return
19    * array is a keyed array with info about amongst other things the database.
20    * The other two by Drupal core supported databases do not have this variable
21    * set in the return array.
22    */
23   public function testGetPrefixInfo() {
24     $connection_info = Database::getConnectionInfo('default');
25     if ($connection_info['default']['driver'] == 'mysql') {
26       // Copy the default connection info to the 'extra' key.
27       Database::addConnectionInfo('extra', 'default', $connection_info['default']);
28
29       $db1_connection = Database::getConnection('default', 'default');
30       $db1_schema = $db1_connection->schema();
31       $db2_connection = Database::getConnection('default', 'extra');
32
33       // Get the prefix info for the first databse.
34       $method = new \ReflectionMethod($db1_schema, 'getPrefixInfo');
35       $method->setAccessible(TRUE);
36       $db1_info = $method->invoke($db1_schema);
37
38       // We change the database after opening the connection, so as to prevent
39       // connecting to a non-existent database.
40       $reflection = new \ReflectionObject($db2_connection);
41       $property = $reflection->getProperty('connectionOptions');
42       $property->setAccessible(TRUE);
43       $connection_info['default']['database'] = 'foobar';
44       $property->setValue($db2_connection, $connection_info['default']);
45
46       // For testing purposes, we also change the database info.
47       $reflection_class = new \ReflectionClass('Drupal\Core\Database\Database');
48       $property = $reflection_class->getProperty('databaseInfo');
49       $property->setAccessible(TRUE);
50       $info = $property->getValue();
51       $info['extra']['default']['database'] = 'foobar';
52       $property->setValue(NULL, $info);
53
54       $extra_info = Database::getConnectionInfo('extra');
55       $this->assertSame($extra_info['default']['database'], 'foobar');
56       $db2_schema = $db2_connection->schema();
57       $db2_info = $method->invoke($db2_schema);
58
59       $this->assertNotSame($db2_info['database'], $db1_info['database'], 'Each target connection has a different database.');
60       $this->assertSame($db2_info['database'], 'foobar', 'The new profile has a different database.');
61
62       Database::removeConnection('extra');
63     }
64   }
65
66 }