test: Fix overwriting and restoring config values

Assisted-by: ClaudeCode:claude-opus-5
Signed-off-by: Marcel Müller <marcel-mueller@gmx.de>
pull/63300/head
Marcel Müller 4 days ago
parent 35975adc66
commit b07428d460
  1. 9
      apps/settings/tests/UserMigration/AccountMigratorTest.php
  2. 37
      tests/lib/TestCase.php
  3. 91
      tests/lib/TestCaseTest.php

@ -13,9 +13,7 @@ use OCA\Settings\UserMigration\AccountMigrator;
use OCP\Accounts\IAccountManager;
use OCP\AppFramework\App;
use OCP\IAvatarManager;
use OCP\IConfig;
use OCP\IUserManager;
use OCP\Server;
use OCP\UserMigration\IExportDestination;
use OCP\UserMigration\IImportSource;
use PHPUnit\Framework\Constraint\JsonMatches;
@ -46,7 +44,7 @@ class AccountMigratorTest extends TestCase {
$app = new App(Application::APP_ID);
$container = $app->getContainer();
$container->get(IConfig::class)->setSystemValue('has_internet_connection', false);
$this->overwriteSystemConfig('has_internet_connection', false);
$this->userManager = $container->get(IUserManager::class);
$this->avatarManager = $container->get(IAvatarManager::class);
@ -57,11 +55,6 @@ class AccountMigratorTest extends TestCase {
$this->output = $this->createMock(OutputInterface::class);
}
protected function tearDown(): void {
Server::get(IConfig::class)->setSystemValue('has_internet_connection', true);
parent::tearDown();
}
public static function dataImportExportAccount(): array {
return array_map(
static function (string $filename): array {

@ -47,10 +47,13 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase {
/** @psalm-suppress ImpureStaticProperty */
private static bool $wasDatabaseAllowed = false;
protected array $services = [];
/** Original values keyed by config key; null means the key was unset. */
private array $systemConfigValues = [];
#[\Override]
protected function onNotSuccessfulTest(\Throwable $t): never {
$this->restoreAllServices();
$this->restoreAllSystemConfig();
// restore database connection
if (!$this->IsDatabaseAccessAllowed()) {
@ -114,6 +117,39 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase {
}
}
/**
* Sets a system config value for the duration of the test, restoring the
* previous one in tearDown. System config is persisted to config.php, so a
* leaked value would outlive the whole run.
*/
protected function overwriteSystemConfig(string $key, mixed $value): void {
$config = Server::get(IConfig::class);
if (!array_key_exists($key, $this->systemConfigValues)) {
$this->systemConfigValues[$key] = $config->getSystemValue($key, null);
}
$config->setSystemValue($key, $value);
}
public function restoreAllSystemConfig(): void {
if ($this->systemConfigValues === []) {
return;
}
$config = Server::get(IConfig::class);
foreach ($this->systemConfigValues as $key => $value) {
// null reads back as the default, so remove the key instead.
if ($value === null) {
$config->deleteSystemValue($key);
} else {
$config->setSystemValue($key, $value);
}
}
$this->systemConfigValues = [];
}
protected function getTestTraits(): array {
$traits = [];
$class = $this;
@ -161,6 +197,7 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase {
#[\Override]
protected function tearDown(): void {
$this->restoreAllServices();
$this->restoreAllSystemConfig();
// restore database connection
if (!$this->IsDatabaseAccessAllowed()) {

@ -0,0 +1,91 @@
<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
namespace Test;
use OCP\IConfig;
use OCP\Server;
/**
* Tests for TestCase::overwriteSystemConfig(). System config is persisted to
* config.php, so a value leaked by a test outlives the run and the next one.
*/
#[\PHPUnit\Framework\Attributes\Group('DB')]
class TestCaseTest extends TestCase {
private const KEY = 'testcase_overwrite_system_config';
private IConfig $config;
#[\Override]
protected function setUp(): void {
parent::setUp();
$this->config = Server::get(IConfig::class);
$this->config->deleteSystemValue(self::KEY);
}
#[\Override]
protected function tearDown(): void {
parent::tearDown();
$this->config->deleteSystemValue(self::KEY);
}
public function testOverwriteSetsTheValue(): void {
$this->overwriteSystemConfig(self::KEY, 'overwritten');
$this->assertSame('overwritten', $this->config->getSystemValue(self::KEY));
}
public function testRestoreRemovesAPreviouslyUnsetKey(): void {
$this->overwriteSystemConfig(self::KEY, 'overwritten');
$this->restoreAllSystemConfig();
$this->assertSame('fallback', $this->config->getSystemValue(self::KEY, 'fallback'));
}
public function testRestoreReturnsThePreviousValue(): void {
$this->config->setSystemValue(self::KEY, 'original');
$this->overwriteSystemConfig(self::KEY, 'overwritten');
$this->restoreAllSystemConfig();
$this->assertSame('original', $this->config->getSystemValue(self::KEY));
}
public function testRestoreReturnsThePreviousValueAfterRepeatedOverwrites(): void {
$this->config->setSystemValue(self::KEY, 'original');
$this->overwriteSystemConfig(self::KEY, 'first');
$this->overwriteSystemConfig(self::KEY, 'second');
$this->restoreAllSystemConfig();
$this->assertSame('original', $this->config->getSystemValue(self::KEY));
}
public function testRestoreIsIdempotent(): void {
$this->config->setSystemValue(self::KEY, 'original');
$this->overwriteSystemConfig(self::KEY, 'overwritten');
$this->restoreAllSystemConfig();
$this->config->setSystemValue(self::KEY, 'set afterwards');
$this->restoreAllSystemConfig();
$this->assertSame('set afterwards', $this->config->getSystemValue(self::KEY));
}
/** false is falsy but set: an isset-based check would wrongly delete the key. */
public function testRestoreReturnsAPreviousFalseValue(): void {
$this->config->setSystemValue(self::KEY, false);
$this->overwriteSystemConfig(self::KEY, true);
$this->restoreAllSystemConfig();
$this->assertFalse($this->config->getSystemValue(self::KEY, 'fallback'));
}
}
Loading…
Cancel
Save