Security update for Core, with self-updated composer
[yaffs-website] / web / core / tests / Drupal / Tests / Core / Database / UrlConversionTest.php
1 <?php
2
3 namespace Drupal\Tests\Core\Database;
4
5 use Drupal\Core\Database\Database;
6 use Drupal\Tests\UnitTestCase;
7
8 /**
9  * @coversDefaultClass \Drupal\Core\Database\Database
10  *
11  * @group Database
12  */
13 class UrlConversionTest extends UnitTestCase {
14
15   /**
16    * @covers ::convertDbUrlToConnectionInfo
17    *
18    * @dataProvider providerConvertDbUrlToConnectionInfo
19    */
20   public function testDbUrltoConnectionConversion($root, $url, $database_array) {
21     $result = Database::convertDbUrlToConnectionInfo($url, $root);
22     $this->assertEquals($database_array, $result);
23   }
24
25   /**
26    * Dataprovider for testDbUrltoConnectionConversion().
27    *
28    * @return array
29    *   Array of arrays with the following elements:
30    *   - root: The baseroot string, only used with sqlite drivers.
31    *   - url: The full URL string to be tested.
32    *   - database_array: An array containing the expected results.
33    */
34   public function providerConvertDbUrlToConnectionInfo() {
35     // Some valid datasets.
36     $root1 = '';
37     $url1 = 'mysql://test_user:test_pass@test_host:3306/test_database';
38     $database_array1 = [
39       'driver' => 'mysql',
40       'username' => 'test_user',
41       'password' => 'test_pass',
42       'host' => 'test_host',
43       'database' => 'test_database',
44       'port' => '3306',
45     ];
46     $root2 = '/var/www/d8';
47     $url2 = 'sqlite://test_user:test_pass@test_host:3306/test_database';
48     $database_array2 = [
49       'driver' => 'sqlite',
50       'username' => 'test_user',
51       'password' => 'test_pass',
52       'host' => 'test_host',
53       'database' => $root2 . '/test_database',
54       'port' => 3306,
55     ];
56     return [
57       [$root1, $url1, $database_array1],
58       [$root2, $url2, $database_array2],
59     ];
60   }
61
62   /**
63    * Test ::convertDbUrlToConnectionInfo() exception for invalid arguments.
64    *
65    * @dataProvider providerInvalidArgumentsUrlConversion
66    */
67   public function testGetInvalidArgumentExceptionInUrlConversion($url, $root) {
68     $this->setExpectedException(\InvalidArgumentException::class);
69     Database::convertDbUrlToConnectionInfo($url, $root);
70   }
71
72   /**
73    * Dataprovider for testGetInvalidArgumentExceptionInUrlConversion().
74    *
75    * @return array
76    *   Array of arrays with the following elements:
77    *   - An invalid Url string.
78    *   - Drupal root string.
79    */
80   public function providerInvalidArgumentsUrlConversion() {
81     return [
82       ['foo', ''],
83       ['foo', 'bar'],
84       ['foo://', 'bar'],
85       ['foo://bar', 'baz'],
86       ['foo://bar:port', 'baz'],
87       ['foo/bar/baz', 'bar2'],
88       ['foo://bar:baz@test1', 'test2'],
89     ];
90   }
91
92   /**
93    * @covers ::convertDbUrlToConnectionInfo
94    *
95    * @dataProvider providerGetConnectionInfoAsUrl
96    */
97   public function testGetConnectionInfoAsUrl(array $info, $expected_url) {
98
99     Database::addConnectionInfo('default', 'default', $info);
100     $url = Database::getConnectionInfoAsUrl();
101
102     // Remove the connection to not pollute subsequent datasets being tested.
103     Database::removeConnection('default');
104
105     $this->assertEquals($expected_url, $url);
106   }
107
108   /**
109    * Dataprovider for testGetConnectionInfoAsUrl().
110    *
111    * @return array
112    *   Array of arrays with the following elements:
113    *   - An array mocking the database connection info. Possible keys are
114    *     database, username, password, prefix, host, port, namespace and driver.
115    *   - The expected URL after conversion.
116    */
117   public function providerGetConnectionInfoAsUrl() {
118     $info1 = [
119       'database' => 'test_database',
120       'username' => 'test_user',
121       'password' => 'test_pass',
122       'prefix' => '',
123       'host' => 'test_host',
124       'port' => '3306',
125       'namespace' => 'Drupal\\Core\\Database\\Driver\\mysql',
126       'driver' => 'mysql',
127     ];
128     $expected_url1 = 'mysql://test_user:test_pass@test_host:3306/test_database';
129
130     $info2 = [
131       'database' => 'test_database',
132       'username' => 'test_user',
133       'password' => 'test_pass',
134       'prefix' => 'pre',
135       'host' => 'test_host',
136       'port' => '3306',
137       'driver' => 'mysql',
138     ];
139     $expected_url2 = 'mysql://test_user:test_pass@test_host:3306/test_database#pre';
140
141     $info3 = [
142       'database' => 'test_database',
143       'driver' => 'sqlite',
144     ];
145     $expected_url3 = 'sqlite://localhost/test_database';
146
147     return [
148       [$info1, $expected_url1],
149       [$info2, $expected_url2],
150       [$info3, $expected_url3],
151     ];
152   }
153
154 }