-
-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Expand file tree
/
Copy pathAbstractDatabaseTest.php
More file actions
254 lines (215 loc) · 7.23 KB
/
Copy pathAbstractDatabaseTest.php
File metadata and controls
254 lines (215 loc) · 7.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test\Setup;
use OC\DB\Connection;
use OC\DB\ConnectionFactory;
use OC\SystemConfig;
use OCP\IL10N;
use OCP\Security\ISecureRandom;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
use Test\TestCase;
class AbstractDatabaseTest extends TestCase {
/**
* Numeric literal instead of PDO::MYSQL_ATTR_SSL_CA: the constant is deprecated
* since PHP 8.5 and only defined when the MySQL driver is available.
*/
private const MYSQL_ATTR_SSL_CA = 1008;
private SystemConfig&MockObject $config;
private ConnectionFactory&MockObject $connectionFactory;
private Connection&MockObject $connection;
private LoggerInterface&MockObject $logger;
private TestDatabase $database;
#[\Override]
protected function setUp(): void {
parent::setUp();
$this->config = $this->createMock(SystemConfig::class);
$this->connectionFactory = $this->createMock(ConnectionFactory::class);
$this->connection = $this->createMock(Connection::class);
$this->logger = $this->createMock(LoggerInterface::class);
$l10n = $this->createMock(IL10N::class);
$l10n->method('t')
->willReturnCallback(fn (string $text, array $parameters = []) => vsprintf($text, $parameters));
$this->database = new TestDatabase(
$l10n,
$this->config,
$this->logger,
$this->createMock(ISecureRandom::class),
);
$this->database->connectionFactory = $this->connectionFactory;
}
public function testInitializeWritesConnectionConfig(): void {
$this->config->expects($this->once())
->method('setValues')
->with([
'dbname' => 'nextcloud',
'dbhost' => 'db.example.org:5432',
'dbtableprefix' => 'nc_',
]);
$this->database->initialize([
'dbuser' => 'admin',
'dbpass' => 'admin-password',
'dbname' => 'nextcloud',
'dbhost' => 'db.example.org:5432',
'dbtableprefix' => 'nc_',
]);
}
public function testInitializeFallsBackToLocalhost(): void {
$this->config->expects($this->once())
->method('setValues')
->with([
'dbname' => 'nextcloud',
'dbhost' => 'localhost',
'dbtableprefix' => 'oc_',
]);
$this->database->initialize([
'dbuser' => 'admin',
'dbpass' => 'admin-password',
'dbname' => 'nextcloud',
'dbhost' => '',
]);
}
/**
* The connection encryption options are only read from the system config, so they have
* to be persisted by initialize() - before any connection is opened by setupDatabase().
*/
public function testInitializePersistsDriverOptions(): void {
$this->config->expects($this->once())
->method('setValues')
->with([
'dbname' => 'nextcloud',
'dbhost' => 'db.example.org',
'dbtableprefix' => 'oc_',
'dbdriveroptions' => [self::MYSQL_ATTR_SSL_CA => '/ca.pem'],
]);
$this->database->initialize($this->options([
'dbdriveroptions' => [self::MYSQL_ATTR_SSL_CA => '/ca.pem'],
]));
}
/**
* Only the options of the database being set up may be persisted, every database
* configures an encrypted connection differently.
*/
public function testInitializeSkipsOptionsOfOtherDatabases(): void {
$this->config->expects($this->once())
->method('setValues')
->with([
'dbname' => 'nextcloud',
'dbhost' => 'db.example.org',
'dbtableprefix' => 'oc_',
]);
$this->database->initialize($this->options([
'pgsql_ssl' => ['mode' => 'verify-full'],
]));
}
public static function emptyEncryptionOptions(): array {
return [
'not provided' => [[]],
'empty array' => [['dbdriveroptions' => []]],
'null' => [['dbdriveroptions' => null]],
];
}
#[\PHPUnit\Framework\Attributes\DataProvider('emptyEncryptionOptions')]
public function testInitializeSkipsEmptyEncryptionOptions(array $additional): void {
$this->config->expects($this->once())
->method('setValues')
->with([
'dbname' => 'nextcloud',
'dbhost' => 'db.example.org',
'dbtableprefix' => 'oc_',
]);
$this->database->initialize($this->options($additional));
}
/**
* A malformed option must never be persisted, as that would end up configuring an
* unencrypted connection while the admin expects an encrypted one.
*/
public function testInitializeRejectsMalformedEncryptionOptions(): void {
$this->config->expects($this->once())
->method('setValues')
->with([
'dbname' => 'nextcloud',
'dbhost' => 'db.example.org',
'dbtableprefix' => 'oc_',
]);
$this->logger->expects($this->once())
->method('error');
$this->database->initialize($this->options(['dbdriveroptions' => '/ca.pem']));
}
public function testValidateRejectsMalformedEncryptionOptions(): void {
$errors = $this->database->validate($this->options(['dbdriveroptions' => '/ca.pem']));
$this->assertEquals([
'The database option "dbdriveroptions" for Test has to be a list of values',
], $errors);
}
public function testValidateAcceptsEncryptionOptions(): void {
$errors = $this->database->validate($this->options([
'dbdriveroptions' => [self::MYSQL_ATTR_SSL_CA => '/ca.pem'],
// not an option of this database, so it is not validated either
'pgsql_ssl' => 'verify-full',
]));
$this->assertEquals([], $errors);
}
/**
* Host, database name and table prefix must not be passed as additional parameters:
* they are resolved from the system config by the connection factory, so that setup
* connects exactly like the installed instance will.
*/
public function testConnectOnlyPassesCredentials(): void {
$this->expectConnection('mysql', [
'user' => 'admin',
'password' => 'admin-password',
]);
$this->database->initialize($this->options());
$this->assertSame($this->connection, $this->database->connectForTest());
}
public function testConnectPassesPort(): void {
$this->expectConnection('pgsql', [
'user' => 'admin',
'password' => 'admin-password',
'port' => 5432,
]);
$this->database->initialize($this->options(['dbport' => '5432']));
$this->database->connectForTest();
}
public function testConnectPassesSocket(): void {
$this->expectConnection('mysql', [
'user' => 'admin',
'password' => 'admin-password',
'unix_socket' => '/var/run/mysqld/mysqld.sock',
]);
$this->database->initialize($this->options(['dbport' => '/var/run/mysqld/mysqld.sock']));
$this->database->connectForTest();
}
public function testConnectAppliesConfigOverwrite(): void {
$this->expectConnection('pgsql', [
'user' => 'admin',
'password' => 'admin-password',
'dbname' => 'postgres',
]);
$this->database->initialize($this->options());
$this->database->connectForTest(['dbname' => 'postgres']);
}
private function options(array $additional = []): array {
return array_merge([
'dbuser' => 'admin',
'dbpass' => 'admin-password',
'dbname' => 'nextcloud',
'dbhost' => 'db.example.org',
], $additional);
}
private function expectConnection(string $dbType, array $expectedParams): void {
$this->config->method('getValue')
->willReturnCallback(fn ($key, $default = '') => $key === 'dbtype' ? $dbType : $default);
$this->connectionFactory->expects($this->once())
->method('getConnection')
->with($dbType, $expectedParams)
->willReturn($this->connection);
$this->connection->expects($this->once())
->method('ensureConnectedToPrimary');
}
}