-
-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Expand file tree
/
Copy pathAppConfigTest.php
More file actions
234 lines (209 loc) · 6.87 KB
/
Copy pathAppConfigTest.php
File metadata and controls
234 lines (209 loc) · 6.87 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
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test;
use OC\AppConfig;
use OC\Config\ConfigManager;
use OC\Config\PresetManager;
use OC\Memcache\Factory as CacheFactory;
use OCP\DB\IResult;
use OCP\DB\QueryBuilder\IExpressionBuilder;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\ICache;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Security\ICrypto;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
/**
* Class AppConfigTest
*
* @package Test
*/
class AppConfigTest extends TestCase {
private IConfig&MockObject $config;
private IDBConnection&MockObject $connection;
private ConfigManager&MockObject $configManager;
private PresetManager&MockObject $presetManager;
private LoggerInterface&MockObject $logger;
private ICrypto&MockObject $crypto;
private CacheFactory&MockObject $cacheFactory;
private ICache&MockObject $localCache;
#[\Override]
protected function setUp(): void {
parent::setUp();
$this->connection = $this->createMock(IDBConnection::class);
$this->config = $this->createMock(IConfig::class);
$this->configManager = $this->createMock(ConfigManager::class);
$this->presetManager = $this->createMock(PresetManager::class);
$this->logger = $this->createMock(LoggerInterface::class);
$this->crypto = $this->createMock(ICrypto::class);
$this->cacheFactory = $this->createMock(CacheFactory::class);
$this->localCache = $this->createMock(ICache::class);
}
protected function getAppConfig($cached = false): AppConfig {
$this->config->method('getSystemValueBool')
->with('cache_app_config', true)
->willReturn(true);
$this->cacheFactory->method('isLocalCacheAvailable')->willReturn($cached);
if ($cached) {
$this->cacheFactory->method('withServerVersionPrefix')->willReturnCallback(function (\Closure $closure): void {
$closure($this->cacheFactory);
});
$this->cacheFactory->method('createLocal')->willReturn($this->localCache);
}
return new AppConfig(
$this->connection,
$this->config,
$this->configManager,
$this->presetManager,
$this->logger,
$this->crypto,
$this->cacheFactory,
);
}
public function testCachedRead(): void {
$this->localCache->expects(self::once())
->method('get')
->with('OC\\AppConfig')
->willReturn([
'fastCache' => [
'appid' => [
'some-key' => 'some-value',
'other-key' => 'other value'
],
],
'valueTypes' => [
'appid' => [
'some-key' => AppConfig::VALUE_STRING,
'other-key' => AppConfig::VALUE_STRING,
],
],
]);
$this->connection->expects(self::never())->method('getQueryBuilder');
$config = $this->getAppConfig(true);
$this->assertSame('some-value', $config->getValueString('appid', 'some-key'));
$this->assertSame('other value', $config->getValueString('appid', 'other-key'));
$this->assertSame(AppConfig::VALUE_STRING, $config->getValueType('appid', 'some-key', false));
}
public function testCachedLazyRead(): void {
$this->localCache->expects(self::once())
->method('get')
->with('OC\\AppConfig')
->willReturn([
'fastCache' => [
'appid' => [
'fast-key' => 'fast value',
],
],
'lazyCache' => [
'appid' => [
'lazy-key' => 'lazy value',
],
],
'valueTypes' => [
'appid' => [
'some-key' => AppConfig::VALUE_STRING,
'lazy-key' => AppConfig::VALUE_STRING,
],
],
]);
$this->connection->expects(self::never())->method('getQueryBuilder');
$config = $this->getAppConfig(true);
$this->assertSame('fast value', $config->getValueString('appid', 'fast-key'));
$this->assertSame('lazy value', $config->getValueString('appid', 'lazy-key', '', true));
}
public function testOnlyFastKeyCached(): void {
$this->localCache->expects(self::atLeastOnce())
->method('get')
->with('OC\\AppConfig')
->willReturn([
'fastCache' => [
'appid' => [
'fast-key' => 'fast value',
],
],
'valueTypes' => [
'appid' => [
'fast-key' => AppConfig::VALUE_STRING,
],
],
]);
$result = $this->createMock(IResult::class);
$result->method('fetchAllAssociative')->willReturn([
['lazy' => 1, 'appid' => 'appid', 'configkey' => 'lazy-key', 'configvalue' => 'lazy value'],
]);
$expression = $this->createMock(IExpressionBuilder::class);
$queryBuilder = $this->createMock(IQueryBuilder::class);
$queryBuilder->method('from')->willReturn($queryBuilder);
$queryBuilder->method('expr')->willReturn($expression);
$queryBuilder->method('executeQuery')->willReturn($result);
$this->connection->expects(self::once())->method('getQueryBuilder')->willReturn($queryBuilder);
$config = $this->getAppConfig(true);
$this->assertSame('fast value', $config->getValueString('appid', 'fast-key'));
$this->assertSame('lazy value', $config->getValueString('appid', 'lazy-key', '', true));
}
public function testWritesAreCached(): void {
$this->localCache->expects(self::atLeastOnce())
->method('get')
->with('OC\\AppConfig')
->willReturn([
'fastCache' => [
'appid' => [
'first-key' => 'first value',
],
],
'valueTypes' => [
'appid' => [
'first-key' => AppConfig::VALUE_STRING,
],
],
]);
$expression = $this->createMock(IExpressionBuilder::class);
$queryBuilder = $this->createMock(IQueryBuilder::class);
$queryBuilder->expects(self::once())
->method('update')
->with('appconfig', null)
->willReturn($queryBuilder);
$queryBuilder->method('set')->willReturn($queryBuilder);
$queryBuilder->method('where')->willReturn($queryBuilder);
$queryBuilder->method('andWhere')->willReturn($queryBuilder);
$queryBuilder->method('expr')->willReturn($expression);
$this->connection->expects(self::once())->method('getQueryBuilder')->willReturn($queryBuilder);
$config = $this->getAppConfig(true);
$this->assertSame('first value', $config->getValueString('appid', 'first-key'));
$config->setValueString('appid', 'first-key', 'new value');
$this->assertSame('new value', $config->getValueString('appid', 'first-key'));
}
public function testFilteredValuesMaskTheEuroOfficeSecret(): void {
$this->localCache->expects(self::atLeastOnce())
->method('get')
->with('OC\\AppConfig')
->willReturn([
'fastCache' => [
'eurooffice' => [
'jwt_secret' => 'a-document-server-signing-key',
'jwt_header' => 'AuthorizationJwt',
],
],
'lazyCache' => [
'eurooffice' => [],
],
'valueTypes' => [
'eurooffice' => [
'jwt_secret' => AppConfig::VALUE_STRING,
'jwt_header' => AppConfig::VALUE_STRING,
],
],
]);
$this->connection->expects(self::never())->method('getQueryBuilder');
$config = $this->getAppConfig(true);
$this->assertSame([
'jwt_secret' => IConfig::SENSITIVE_VALUE,
'jwt_header' => 'AuthorizationJwt',
], $config->getFilteredValues('eurooffice'));
}
}