|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | +/** |
| 5 | + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors |
| 6 | + * SPDX-License-Identifier: AGPL-3.0-or-later |
| 7 | + */ |
| 8 | +namespace Test; |
| 9 | + |
| 10 | +use Doctrine\DBAL\Exception\InvalidFieldNameException; |
| 11 | +use OC\AppConfig; |
| 12 | +use OC\Config\ConfigManager; |
| 13 | +use OC\Config\PresetManager; |
| 14 | +use OC\DB\Exceptions\DbalException; |
| 15 | +use OC\Memcache\Factory as CacheFactory; |
| 16 | +use OCP\DB\Exception as DBException; |
| 17 | +use OCP\DB\IResult; |
| 18 | +use OCP\DB\QueryBuilder\IExpressionBuilder; |
| 19 | +use OCP\DB\QueryBuilder\IQueryBuilder; |
| 20 | +use OCP\IConfig; |
| 21 | +use OCP\IDBConnection; |
| 22 | +use OCP\Security\ICrypto; |
| 23 | +use PHPUnit\Framework\MockObject\MockObject; |
| 24 | +use Psr\Log\LoggerInterface; |
| 25 | + |
| 26 | +/** |
| 27 | + * Tests the ownCloud migration fallback in AppConfig. |
| 28 | + * |
| 29 | + * When migrating from ownCloud, the appconfig table lacks 'type' and 'lazy' |
| 30 | + * columns. AppConfig::loadConfig() must catch the resulting DBException and |
| 31 | + * retry with a query that only selects columns present in ownCloud's schema. |
| 32 | + */ |
| 33 | +class AppConfigMigrationFallbackTest extends TestCase { |
| 34 | + private IConfig&MockObject $config; |
| 35 | + private IDBConnection&MockObject $connection; |
| 36 | + private ConfigManager&MockObject $configManager; |
| 37 | + private PresetManager&MockObject $presetManager; |
| 38 | + private LoggerInterface&MockObject $logger; |
| 39 | + private ICrypto&MockObject $crypto; |
| 40 | + private CacheFactory&MockObject $cacheFactory; |
| 41 | + |
| 42 | + protected function setUp(): void { |
| 43 | + parent::setUp(); |
| 44 | + |
| 45 | + $this->connection = $this->createMock(IDBConnection::class); |
| 46 | + $this->config = $this->createMock(IConfig::class); |
| 47 | + $this->configManager = $this->createMock(ConfigManager::class); |
| 48 | + $this->presetManager = $this->createMock(PresetManager::class); |
| 49 | + $this->logger = $this->createMock(LoggerInterface::class); |
| 50 | + $this->crypto = $this->createMock(ICrypto::class); |
| 51 | + $this->cacheFactory = $this->createMock(CacheFactory::class); |
| 52 | + } |
| 53 | + |
| 54 | + private function getAppConfig(): AppConfig { |
| 55 | + $this->config->method('getSystemValueBool') |
| 56 | + ->with('cache_app_config', true) |
| 57 | + ->willReturn(true); |
| 58 | + $this->cacheFactory->method('isLocalCacheAvailable')->willReturn(false); |
| 59 | + |
| 60 | + return new AppConfig( |
| 61 | + $this->connection, |
| 62 | + $this->config, |
| 63 | + $this->configManager, |
| 64 | + $this->presetManager, |
| 65 | + $this->logger, |
| 66 | + $this->crypto, |
| 67 | + $this->cacheFactory, |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + private function createInvalidFieldNameException(): DBException { |
| 72 | + $driverException = $this->createMock(\Doctrine\DBAL\Driver\Exception::class); |
| 73 | + $dbalException = new InvalidFieldNameException($driverException, null); |
| 74 | + return DbalException::wrap($dbalException); |
| 75 | + } |
| 76 | + |
| 77 | + private function createMockQueryBuilder(): IQueryBuilder&MockObject { |
| 78 | + $expression = $this->createMock(IExpressionBuilder::class); |
| 79 | + $qb = $this->createMock(IQueryBuilder::class); |
| 80 | + $qb->method('from')->willReturn($qb); |
| 81 | + $qb->method('select')->willReturn($qb); |
| 82 | + $qb->method('addSelect')->willReturn($qb); |
| 83 | + $qb->method('where')->willReturn($qb); |
| 84 | + $qb->method('andWhere')->willReturn($qb); |
| 85 | + $qb->method('set')->willReturn($qb); |
| 86 | + $qb->method('expr')->willReturn($expression); |
| 87 | + $qb->method('insert')->willReturn($qb); |
| 88 | + $qb->method('setValue')->willReturn($qb); |
| 89 | + $qb->method('createNamedParameter')->willReturn('?'); |
| 90 | + return $qb; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Test that loadConfig retries without type/lazy columns on InvalidFieldNameException. |
| 95 | + */ |
| 96 | + public function testLoadConfigFallsBackOnMissingColumns(): void { |
| 97 | + $exception = $this->createInvalidFieldNameException(); |
| 98 | + |
| 99 | + $successResult = $this->createMock(IResult::class); |
| 100 | + $successResult->method('fetchAll')->willReturn([ |
| 101 | + ['appid' => 'core', 'configkey' => 'vendor', 'configvalue' => 'owncloud'], |
| 102 | + ['appid' => 'core', 'configkey' => 'installedat', 'configvalue' => '1234567890'], |
| 103 | + ]); |
| 104 | + |
| 105 | + $qb = $this->createMockQueryBuilder(); |
| 106 | + // First call throws (columns missing), second call succeeds (fallback query) |
| 107 | + $qb->method('executeQuery') |
| 108 | + ->willReturnOnConsecutiveCalls( |
| 109 | + $this->throwException($exception), |
| 110 | + $successResult, |
| 111 | + ); |
| 112 | + |
| 113 | + $this->connection->method('getQueryBuilder')->willReturn($qb); |
| 114 | + |
| 115 | + $appConfig = $this->getAppConfig(); |
| 116 | + |
| 117 | + // getValueString triggers loadConfig internally |
| 118 | + $value = $appConfig->getValueString('core', 'vendor'); |
| 119 | + $this->assertSame('owncloud', $value); |
| 120 | + } |
| 121 | + |
| 122 | + /** |
| 123 | + * Test that non-INVALID_FIELD_NAME exceptions are re-thrown, not swallowed. |
| 124 | + */ |
| 125 | + public function testLoadConfigRethrowsOtherExceptions(): void { |
| 126 | + $driverException = $this->createMock(\Doctrine\DBAL\Driver\Exception::class); |
| 127 | + $dbalException = new \Doctrine\DBAL\Exception\SyntaxErrorException($driverException, null); |
| 128 | + $exception = DbalException::wrap($dbalException); |
| 129 | + |
| 130 | + $qb = $this->createMockQueryBuilder(); |
| 131 | + $qb->method('executeQuery')->willThrowException($exception); |
| 132 | + |
| 133 | + $this->connection->method('getQueryBuilder')->willReturn($qb); |
| 134 | + |
| 135 | + $appConfig = $this->getAppConfig(); |
| 136 | + |
| 137 | + $this->expectException(DBException::class); |
| 138 | + $appConfig->getValueString('core', 'vendor'); |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Test that insert omits lazy/type columns when migration is not completed. |
| 143 | + */ |
| 144 | + public function testInsertOmitsNewColumnsInFallbackMode(): void { |
| 145 | + $exception = $this->createInvalidFieldNameException(); |
| 146 | + |
| 147 | + $loadResult = $this->createMock(IResult::class); |
| 148 | + $loadResult->method('fetchAll')->willReturn([]); |
| 149 | + |
| 150 | + $qb = $this->createMockQueryBuilder(); |
| 151 | + |
| 152 | + $callCount = 0; |
| 153 | + $qb->method('executeQuery') |
| 154 | + ->willReturnCallback(function () use ($exception, $loadResult, &$callCount) { |
| 155 | + $callCount++; |
| 156 | + if ($callCount === 1) { |
| 157 | + throw $exception; |
| 158 | + } |
| 159 | + return $loadResult; |
| 160 | + }); |
| 161 | + |
| 162 | + // Verify insert() is called (meaning we reached the insert path) |
| 163 | + $qb->expects(self::once())->method('insert')->with('appconfig')->willReturn($qb); |
| 164 | + $qb->method('executeStatement')->willReturn(1); |
| 165 | + |
| 166 | + // Track which columns are set via setValue |
| 167 | + $setColumns = []; |
| 168 | + $qb->method('setValue') |
| 169 | + ->willReturnCallback(function (string $column) use ($qb, &$setColumns) { |
| 170 | + $setColumns[] = $column; |
| 171 | + return $qb; |
| 172 | + }); |
| 173 | + |
| 174 | + $this->connection->method('getQueryBuilder')->willReturn($qb); |
| 175 | + |
| 176 | + $appConfig = $this->getAppConfig(); |
| 177 | + $appConfig->setValueString('core', 'vendor', 'owncloud'); |
| 178 | + |
| 179 | + $this->assertContains('appid', $setColumns); |
| 180 | + $this->assertContains('configkey', $setColumns); |
| 181 | + $this->assertContains('configvalue', $setColumns); |
| 182 | + $this->assertNotContains('lazy', $setColumns, 'lazy column should be omitted in fallback mode'); |
| 183 | + $this->assertNotContains('type', $setColumns, 'type column should be omitted in fallback mode'); |
| 184 | + } |
| 185 | +} |
0 commit comments