|
| 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\lib\Config; |
| 9 | + |
| 10 | +use OC\Config\ConfigManager; |
| 11 | +use OC\Config\PresetManager; |
| 12 | +use OC\Config\UserConfig; |
| 13 | +use OC\DB\Exceptions\DbalException; |
| 14 | +use Doctrine\DBAL\Exception\InvalidFieldNameException; |
| 15 | +use OCP\DB\Exception as DBException; |
| 16 | +use OCP\DB\IResult; |
| 17 | +use OCP\DB\QueryBuilder\IExpressionBuilder; |
| 18 | +use OCP\DB\QueryBuilder\IQueryBuilder; |
| 19 | +use OCP\EventDispatcher\IEventDispatcher; |
| 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 | +use Test\TestCase; |
| 26 | + |
| 27 | +/** |
| 28 | + * Tests the ownCloud migration fallback in UserConfig. |
| 29 | + * |
| 30 | + * When migrating from ownCloud, the preferences table lacks 'type', 'lazy', |
| 31 | + * 'flags', and 'indexed' columns. UserConfig::loadConfig() must catch the |
| 32 | + * resulting DBException and retry with a query that only selects columns |
| 33 | + * present in ownCloud's schema. |
| 34 | + */ |
| 35 | +class UserConfigMigrationFallbackTest extends TestCase { |
| 36 | + private IDBConnection&MockObject $connection; |
| 37 | + private IConfig&MockObject $config; |
| 38 | + private ConfigManager&MockObject $configManager; |
| 39 | + private PresetManager&MockObject $presetManager; |
| 40 | + private LoggerInterface&MockObject $logger; |
| 41 | + private ICrypto&MockObject $crypto; |
| 42 | + private IEventDispatcher&MockObject $dispatcher; |
| 43 | + |
| 44 | + protected function setUp(): void { |
| 45 | + parent::setUp(); |
| 46 | + |
| 47 | + $this->connection = $this->createMock(IDBConnection::class); |
| 48 | + $this->config = $this->createMock(IConfig::class); |
| 49 | + $this->configManager = $this->createMock(ConfigManager::class); |
| 50 | + $this->presetManager = $this->createMock(PresetManager::class); |
| 51 | + $this->logger = $this->createMock(LoggerInterface::class); |
| 52 | + $this->crypto = $this->createMock(ICrypto::class); |
| 53 | + $this->dispatcher = $this->createMock(IEventDispatcher::class); |
| 54 | + } |
| 55 | + |
| 56 | + private function getUserConfig(): UserConfig { |
| 57 | + return new UserConfig( |
| 58 | + $this->connection, |
| 59 | + $this->config, |
| 60 | + $this->configManager, |
| 61 | + $this->presetManager, |
| 62 | + $this->logger, |
| 63 | + $this->crypto, |
| 64 | + $this->dispatcher, |
| 65 | + ); |
| 66 | + } |
| 67 | + |
| 68 | + private function createInvalidFieldNameException(): DBException { |
| 69 | + $driverException = $this->createMock(\Doctrine\DBAL\Driver\Exception::class); |
| 70 | + $dbalException = new InvalidFieldNameException($driverException, null); |
| 71 | + return DbalException::wrap($dbalException); |
| 72 | + } |
| 73 | + |
| 74 | + private function createMockQueryBuilder(): IQueryBuilder&MockObject { |
| 75 | + $expression = $this->createMock(IExpressionBuilder::class); |
| 76 | + $qb = $this->createMock(IQueryBuilder::class); |
| 77 | + $qb->method('from')->willReturn($qb); |
| 78 | + $qb->method('select')->willReturn($qb); |
| 79 | + $qb->method('addSelect')->willReturn($qb); |
| 80 | + $qb->method('where')->willReturn($qb); |
| 81 | + $qb->method('andWhere')->willReturn($qb); |
| 82 | + $qb->method('set')->willReturn($qb); |
| 83 | + $qb->method('expr')->willReturn($expression); |
| 84 | + $qb->method('insert')->willReturn($qb); |
| 85 | + $qb->method('setValue')->willReturn($qb); |
| 86 | + $qb->method('createNamedParameter')->willReturn('?'); |
| 87 | + return $qb; |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Test that loadConfig retries without new columns on InvalidFieldNameException. |
| 92 | + */ |
| 93 | + public function testLoadConfigFallsBackOnMissingColumns(): void { |
| 94 | + $exception = $this->createInvalidFieldNameException(); |
| 95 | + |
| 96 | + $successResult = $this->createMock(IResult::class); |
| 97 | + $successResult->method('fetchAll')->willReturn([ |
| 98 | + ['appid' => 'settings', 'configkey' => 'email', 'configvalue' => 'user@example.com'], |
| 99 | + ]); |
| 100 | + |
| 101 | + $qb = $this->createMockQueryBuilder(); |
| 102 | + $qb->method('executeQuery') |
| 103 | + ->willReturnOnConsecutiveCalls( |
| 104 | + $this->throwException($exception), |
| 105 | + $successResult, |
| 106 | + ); |
| 107 | + |
| 108 | + $this->connection->method('getQueryBuilder')->willReturn($qb); |
| 109 | + |
| 110 | + $userConfig = $this->getUserConfig(); |
| 111 | + |
| 112 | + $value = $userConfig->getValueString('user1', 'settings', 'email'); |
| 113 | + $this->assertSame('user@example.com', $value); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Test that non-INVALID_FIELD_NAME exceptions are re-thrown. |
| 118 | + */ |
| 119 | + public function testLoadConfigRethrowsOtherExceptions(): void { |
| 120 | + $driverException = $this->createMock(\Doctrine\DBAL\Driver\Exception::class); |
| 121 | + $dbalException = new \Doctrine\DBAL\Exception\SyntaxErrorException($driverException, null); |
| 122 | + $exception = DbalException::wrap($dbalException); |
| 123 | + |
| 124 | + $qb = $this->createMockQueryBuilder(); |
| 125 | + $qb->method('executeQuery')->willThrowException($exception); |
| 126 | + |
| 127 | + $this->connection->method('getQueryBuilder')->willReturn($qb); |
| 128 | + |
| 129 | + $userConfig = $this->getUserConfig(); |
| 130 | + |
| 131 | + $this->expectException(DBException::class); |
| 132 | + $userConfig->getValueString('user1', 'settings', 'email'); |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Test that insert omits new columns when migration is not completed. |
| 137 | + */ |
| 138 | + public function testInsertOmitsNewColumnsInFallbackMode(): void { |
| 139 | + $exception = $this->createInvalidFieldNameException(); |
| 140 | + |
| 141 | + $loadResult = $this->createMock(IResult::class); |
| 142 | + $loadResult->method('fetchAll')->willReturn([]); |
| 143 | + |
| 144 | + $qb = $this->createMockQueryBuilder(); |
| 145 | + |
| 146 | + $callCount = 0; |
| 147 | + $qb->method('executeQuery') |
| 148 | + ->willReturnCallback(function () use ($exception, $loadResult, &$callCount) { |
| 149 | + $callCount++; |
| 150 | + if ($callCount === 1) { |
| 151 | + throw $exception; |
| 152 | + } |
| 153 | + return $loadResult; |
| 154 | + }); |
| 155 | + |
| 156 | + $qb->expects(self::once())->method('insert')->with('preferences')->willReturn($qb); |
| 157 | + $qb->method('executeStatement')->willReturn(1); |
| 158 | + |
| 159 | + $setColumns = []; |
| 160 | + $qb->method('setValue') |
| 161 | + ->willReturnCallback(function (string $column) use ($qb, &$setColumns) { |
| 162 | + $setColumns[] = $column; |
| 163 | + return $qb; |
| 164 | + }); |
| 165 | + |
| 166 | + $this->connection->method('getQueryBuilder')->willReturn($qb); |
| 167 | + |
| 168 | + $userConfig = $this->getUserConfig(); |
| 169 | + $userConfig->setValueString('user1', 'settings', 'email', 'user@example.com'); |
| 170 | + |
| 171 | + $this->assertContains('userid', $setColumns); |
| 172 | + $this->assertContains('appid', $setColumns); |
| 173 | + $this->assertContains('configkey', $setColumns); |
| 174 | + $this->assertContains('configvalue', $setColumns); |
| 175 | + $this->assertNotContains('lazy', $setColumns, 'lazy column should be omitted in fallback mode'); |
| 176 | + $this->assertNotContains('type', $setColumns, 'type column should be omitted in fallback mode'); |
| 177 | + $this->assertNotContains('flags', $setColumns, 'flags column should be omitted in fallback mode'); |
| 178 | + $this->assertNotContains('indexed', $setColumns, 'indexed column should be omitted in fallback mode'); |
| 179 | + } |
| 180 | +} |
0 commit comments