diff options
author | Maxence Lange <maxence@artificial-owl.com> | 2025-04-09 09:50:36 -0100 |
---|---|---|
committer | Maxence Lange <maxence@artificial-owl.com> | 2025-04-09 09:50:53 -0100 |
commit | 46508549a9b19f4473fc691dcb2e4966f300d67f (patch) | |
tree | 9a75d16b49fdba8d2012eadbfd5f616911d70a4d | |
parent | 455fb740d085c5649b0892b1185989451f7a14fa (diff) | |
download | nextcloud-server-enh/noid/return-default-value-from-lexicon.tar.gz nextcloud-server-enh/noid/return-default-value-from-lexicon.zip |
feat(config): returns default value set in lexicon if unknownenh/noid/return-default-value-from-lexicon
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
-rw-r--r-- | core/Command/Config/App/GetConfig.php | 17 | ||||
-rw-r--r-- | lib/private/AppConfig.php | 5 | ||||
-rw-r--r-- | tests/Core/Command/Config/App/GetConfigTest.php | 63 |
3 files changed, 59 insertions, 26 deletions
diff --git a/core/Command/Config/App/GetConfig.php b/core/Command/Config/App/GetConfig.php index f64efd3feaa..35a2fccc6fd 100644 --- a/core/Command/Config/App/GetConfig.php +++ b/core/Command/Config/App/GetConfig.php @@ -17,6 +17,7 @@ use Symfony\Component\Console\Output\OutputInterface; class GetConfig extends Base { public function __construct( + /** @var \OC\AppConfig */ protected IAppConfig $appConfig, ) { parent::__construct(); @@ -45,6 +46,12 @@ class GetConfig extends Base { 'returns complete details about the app config value' ) ->addOption( + 'default-if-none', + null, + InputOption::VALUE_NONE, + 'returns default if no value is set' + ) + ->addOption( 'default-value', null, InputOption::VALUE_OPTIONAL, @@ -76,10 +83,14 @@ class GetConfig extends Base { try { $configValue = $this->appConfig->getDetails($appName, $configName)['value']; } catch (AppConfigUnknownKeyException $e) { - if (!$input->hasParameterOption('--default-value')) { - return 1; + if ($input->getOption('default-if-none')) { + $configValue = $this->appConfig->getValueMixed($appName, $configName, lazy: null); + } else { + if (!$input->hasParameterOption('--default-value')) { + return 1; + } + $configValue = $defaultValue; } - $configValue = $defaultValue; } $this->writeMixedInOutputFormat($input, $output, $configValue); diff --git a/lib/private/AppConfig.php b/lib/private/AppConfig.php index 1228b9c20e3..28ed3785c59 100644 --- a/lib/private/AppConfig.php +++ b/lib/private/AppConfig.php @@ -284,7 +284,10 @@ class AppConfig implements IAppConfig { ): string { try { $lazy = ($lazy === null) ? $this->isLazy($app, $key) : $lazy; - } catch (AppConfigUnknownKeyException $e) { + } catch (AppConfigUnknownKeyException) { + $lazy = false; + $type = self::VALUE_MIXED; + $this->matchAndApplyLexiconDefinition($app, $key, $lazy, $type, $default); return $default; } diff --git a/tests/Core/Command/Config/App/GetConfigTest.php b/tests/Core/Command/Config/App/GetConfigTest.php index c1a6265db40..f4b7dd7bc21 100644 --- a/tests/Core/Command/Config/App/GetConfigTest.php +++ b/tests/Core/Command/Config/App/GetConfigTest.php @@ -8,11 +8,15 @@ namespace Tests\Core\Command\Config\App; use OC\AppConfig; +use OC\AppFramework\Bootstrap\Coordinator; use OC\Core\Command\Config\App\GetConfig; use OCP\Exceptions\AppConfigUnknownKeyException; +use OCP\IAppConfig; +use OCP\Server; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Test\TestCase; +use Tests\lib\Config\TestConfigLexicon_I; class GetConfigTest extends TestCase { /** @var \PHPUnit\Framework\MockObject\MockObject */ @@ -43,41 +47,44 @@ class GetConfigTest extends TestCase { public function getData() { return [ // String output as json - ['name', 'newvalue', true, null, false, 'json', 0, json_encode('newvalue')], + ['name', 'newvalue', true, null, false, 'json', 0, json_encode('newvalue'), false], // String output as plain text - ['name', 'newvalue', true, null, false, 'plain', 0, 'newvalue'], + ['name', 'newvalue', true, null, false, 'plain', 0, 'newvalue', false], // String falling back to default output as json - ['name', null, false, 'newvalue', true, 'json', 0, json_encode('newvalue')], - // String falling back without default: error - ['name', null, false, null, false, 'json', 1, null], + ['name', null, false, 'newvalue', true, 'json', 0, json_encode('newvalue'), false], + // String falling back without default: errorw + ['name', null, false, null, false, 'json', 1, null, false], + + // testing default value if set in lexicon + ['key1', '123', false, 'newvalssue', false, 'plain', 0, 'newvalsadsdae', true], // Int "0" output as json/plain - ['name', 0, true, null, false, 'json', 0, json_encode(0)], - ['name', 0, true, null, false, 'plain', 0, '0'], + ['name', 0, true, null, false, 'json', 0, json_encode(0), false], + ['name', 0, true, null, false, 'plain', 0, '0', false], // Int "1" output as json/plain - ['name', 1, true, null, false, 'json', 0, json_encode(1)], - ['name', 1, true, null, false, 'plain', 0, '1'], + ['name', 1, true, null, false, 'json', 0, json_encode(1), false], + ['name', 1, true, null, false, 'plain', 0, '1', false], // Bool "true" output as json/plain - ['name', true, true, null, false, 'json', 0, json_encode(true)], - ['name', true, true, null, false, 'plain', 0, 'true'], + ['name', true, true, null, false, 'json', 0, json_encode(true), false], + ['name', true, true, null, false, 'plain', 0, 'true', false], // Bool "false" output as json/plain - ['name', false, true, null, false, 'json', 0, json_encode(false)], - ['name', false, true, null, false, 'plain', 0, 'false'], + ['name', false, true, null, false, 'json', 0, json_encode(false), false], + ['name', false, true, null, false, 'plain', 0, 'false', false], // Null output as json/plain - ['name', null, true, null, false, 'json', 0, json_encode(null)], - ['name', null, true, null, false, 'plain', 0, 'null'], + ['name', null, true, null, false, 'json', 0, json_encode(null), false], + ['name', null, true, null, false, 'plain', 0, 'null', false], // Array output as json/plain - ['name', ['a', 'b'], true, null, false, 'json', 0, json_encode(['a', 'b'])], - ['name', ['a', 'b'], true, null, false, 'plain', 0, "a\nb"], + ['name', ['a', 'b'], true, null, false, 'json', 0, json_encode(['a', 'b']), false], + ['name', ['a', 'b'], true, null, false, 'plain', 0, "a\nb", false], // Key array output as json/plain - ['name', [0 => 'a', 1 => 'b'], true, null, false, 'json', 0, json_encode(['a', 'b'])], - ['name', [0 => 'a', 1 => 'b'], true, null, false, 'plain', 0, "a\nb"], + ['name', [0 => 'a', 1 => 'b'], true, null, false, 'json', 0, json_encode(['a', 'b']), false], + ['name', [0 => 'a', 1 => 'b'], true, null, false, 'plain', 0, "a\nb", false], // Associative array output as json/plain - ['name', ['a' => 1, 'b' => 2], true, null, false, 'json', 0, json_encode(['a' => 1, 'b' => 2])], - ['name', ['a' => 1, 'b' => 2], true, null, false, 'plain', 0, "a: 1\nb: 2"], + ['name', ['a' => 1, 'b' => 2], true, null, false, 'json', 0, json_encode(['a' => 1, 'b' => 2]), false], + ['name', ['a' => 1, 'b' => 2], true, null, false, 'plain', 0, "a: 1\nb: 2", false], ]; } @@ -93,8 +100,9 @@ class GetConfigTest extends TestCase { * @param string $outputFormat * @param int $expectedReturn * @param string $expectedMessage + * @param bool $fromLexicon */ - public function testGet($configName, $value, $configExists, $defaultValue, $hasDefault, $outputFormat, $expectedReturn, $expectedMessage): void { + public function testGet($configName, $value, $configExists, $defaultValue, $hasDefault, $outputFormat, $expectedReturn, $expectedMessage, $fromLexicon): void { if (!$expectedReturn) { if ($configExists) { $this->config->expects($this->once()) @@ -104,6 +112,17 @@ class GetConfigTest extends TestCase { } } + // testing default value extracted from a test lexicon assigned to app-name + if ($fromLexicon) { + $bootstrapCoordinator = Server::get(Coordinator::class); + $bootstrapCoordinator->getRegistrationContext()?->registerConfigLexicon('app-name', TestConfigLexicon_I::class); + + $appConfig = Server::get(IAppConfig::class); + $this->assertSame('abcde', $appConfig->getValueString('app-name', 'key1')); + return; + } + + if (!$configExists) { $this->config->expects($this->once()) ->method('getDetails') |