-
-
Notifications
You must be signed in to change notification settings - Fork 5k
Expand file tree
/
Copy pathAppConfigMigrationFallbackTest.php
More file actions
185 lines (156 loc) · 6.23 KB
/
Copy pathAppConfigMigrationFallbackTest.php
File metadata and controls
185 lines (156 loc) · 6.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
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test;
use Doctrine\DBAL\Exception\InvalidFieldNameException;
use OC\AppConfig;
use OC\Config\ConfigManager;
use OC\Config\PresetManager;
use OC\DB\Exceptions\DbalException;
use OC\Memcache\Factory as CacheFactory;
use OCP\DB\Exception as DBException;
use OCP\DB\IResult;
use OCP\DB\QueryBuilder\IExpressionBuilder;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Security\ICrypto;
use PHPUnit\Framework\MockObject\MockObject;
use Psr\Log\LoggerInterface;
/**
* Tests the ownCloud migration fallback in AppConfig.
*
* When migrating from ownCloud, the appconfig table lacks 'type' and 'lazy'
* columns. AppConfig::loadConfig() must catch the resulting DBException and
* retry with a query that only selects columns present in ownCloud's schema.
*/
class AppConfigMigrationFallbackTest 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;
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);
}
private function getAppConfig(): AppConfig {
$this->config->method('getSystemValueBool')
->with('cache_app_config', true)
->willReturn(true);
$this->cacheFactory->method('isLocalCacheAvailable')->willReturn(false);
return new AppConfig(
$this->connection,
$this->config,
$this->configManager,
$this->presetManager,
$this->logger,
$this->crypto,
$this->cacheFactory,
);
}
private function createInvalidFieldNameException(): DBException {
$driverException = $this->createMock(\Doctrine\DBAL\Driver\Exception::class);
$dbalException = new InvalidFieldNameException($driverException, null);
return DbalException::wrap($dbalException);
}
private function createMockQueryBuilder(): IQueryBuilder&MockObject {
$expression = $this->createMock(IExpressionBuilder::class);
$qb = $this->createMock(IQueryBuilder::class);
$qb->method('from')->willReturn($qb);
$qb->method('select')->willReturn($qb);
$qb->method('addSelect')->willReturn($qb);
$qb->method('where')->willReturn($qb);
$qb->method('andWhere')->willReturn($qb);
$qb->method('set')->willReturn($qb);
$qb->method('expr')->willReturn($expression);
$qb->method('insert')->willReturn($qb);
$qb->method('setValue')->willReturn($qb);
$qb->method('createNamedParameter')->willReturn('?');
return $qb;
}
/**
* Test that loadConfig retries without type/lazy columns on InvalidFieldNameException.
*/
public function testLoadConfigFallsBackOnMissingColumns(): void {
$exception = $this->createInvalidFieldNameException();
$successResult = $this->createMock(IResult::class);
$successResult->method('fetchAll')->willReturn([
['appid' => 'core', 'configkey' => 'vendor', 'configvalue' => 'owncloud'],
['appid' => 'core', 'configkey' => 'installedat', 'configvalue' => '1234567890'],
]);
$qb = $this->createMockQueryBuilder();
// First call throws (columns missing), second call succeeds (fallback query)
$qb->method('executeQuery')
->willReturnOnConsecutiveCalls(
$this->throwException($exception),
$successResult,
);
$this->connection->method('getQueryBuilder')->willReturn($qb);
$appConfig = $this->getAppConfig();
// getValueString triggers loadConfig internally
$value = $appConfig->getValueString('core', 'vendor');
$this->assertSame('owncloud', $value);
}
/**
* Test that non-INVALID_FIELD_NAME exceptions are re-thrown, not swallowed.
*/
public function testLoadConfigRethrowsOtherExceptions(): void {
$driverException = $this->createMock(\Doctrine\DBAL\Driver\Exception::class);
$dbalException = new \Doctrine\DBAL\Exception\SyntaxErrorException($driverException, null);
$exception = DbalException::wrap($dbalException);
$qb = $this->createMockQueryBuilder();
$qb->method('executeQuery')->willThrowException($exception);
$this->connection->method('getQueryBuilder')->willReturn($qb);
$appConfig = $this->getAppConfig();
$this->expectException(DBException::class);
$appConfig->getValueString('core', 'vendor');
}
/**
* Test that insert omits lazy/type columns when migration is not completed.
*/
public function testInsertOmitsNewColumnsInFallbackMode(): void {
$exception = $this->createInvalidFieldNameException();
$loadResult = $this->createMock(IResult::class);
$loadResult->method('fetchAll')->willReturn([]);
$qb = $this->createMockQueryBuilder();
$callCount = 0;
$qb->method('executeQuery')
->willReturnCallback(function () use ($exception, $loadResult, &$callCount) {
$callCount++;
if ($callCount === 1) {
throw $exception;
}
return $loadResult;
});
// Verify insert() is called (meaning we reached the insert path)
$qb->expects(self::once())->method('insert')->with('appconfig')->willReturn($qb);
$qb->method('executeStatement')->willReturn(1);
// Track which columns are set via setValue
$setColumns = [];
$qb->method('setValue')
->willReturnCallback(function (string $column) use ($qb, &$setColumns) {
$setColumns[] = $column;
return $qb;
});
$this->connection->method('getQueryBuilder')->willReturn($qb);
$appConfig = $this->getAppConfig();
$appConfig->setValueString('core', 'vendor', 'owncloud');
$this->assertContains('appid', $setColumns);
$this->assertContains('configkey', $setColumns);
$this->assertContains('configvalue', $setColumns);
$this->assertNotContains('lazy', $setColumns, 'lazy column should be omitted in fallback mode');
$this->assertNotContains('type', $setColumns, 'type column should be omitted in fallback mode');
}
}