diff --git a/lib/AppConfigOverwrite.php b/lib/AppConfigOverwrite.php index da6ab2f8..1bb2ceab 100644 --- a/lib/AppConfigOverwrite.php +++ b/lib/AppConfigOverwrite.php @@ -30,4 +30,50 @@ public function getValue($app, $key, $default = '') { return parent::getValue($app, $key, $default); } + + #[\Override] + public function getValueString(string $app, string $key, string $default = '', bool $lazy = false): string { + if (isset($this->overWrite[$app]) && isset($this->overWrite[$app][$key])) { + return $this->overWrite[$app][$key]; + } + + return parent::getValueString($app, $key, $default, $lazy); + } + + #[\Override] + public function getValueBool(string $app, string $key, bool $default = false, bool $lazy = false): bool { + if (isset($this->overWrite[$app]) && isset($this->overWrite[$app][$key])) { + return in_array(strtolower($this->overWrite[$app][$key]), ['1', 'true', 'yes', 'on'], true); + } + + return parent::getValueBool($app, $key, $default, $lazy); + } + + #[\Override] + public function getValueInt(string $app, string $key, int $default = 0, bool $lazy = false): int { + if (isset($this->overWrite[$app]) && isset($this->overWrite[$app][$key])) { + return (int)$this->overWrite[$app][$key]; + } + + return parent::getValueInt($app, $key, $default, $lazy); + } + + #[\Override] + public function getValueFloat(string $app, string $key, float $default = 0, bool $lazy = false): float { + if (isset($this->overWrite[$app]) && isset($this->overWrite[$app][$key])) { + return (float)$this->overWrite[$app][$key]; + } + + return parent::getValueFloat($app, $key, $default, $lazy); + } + + #[\Override] + public function getValueArray(string $app, string $key, array $default = [], bool $lazy = false): array { + if (isset($this->overWrite[$app]) && isset($this->overWrite[$app][$key])) { + $value = json_decode($this->overWrite[$app][$key], true); + return is_array($value) ? $value : $default; + } + + return parent::getValueArray($app, $key, $default, $lazy); + } } diff --git a/tests/stub.php b/tests/stub.php index cbddd7d4..310dd4c7 100644 --- a/tests/stub.php +++ b/tests/stub.php @@ -133,6 +133,21 @@ public function __construct( */ public function getValue($app, $key, $default = '') { } + + public function getValueString(string $app, string $key, string $default = '', bool $lazy = false): string { + } + + public function getValueBool(string $app, string $key, bool $default = false, bool $lazy = false): bool { + } + + public function getValueInt(string $app, string $key, int $default = 0, bool $lazy = false): int { + } + + public function getValueFloat(string $app, string $key, float $default = 0, bool $lazy = false): float { + } + + public function getValueArray(string $app, string $key, array $default = [], bool $lazy = false): array { + } } } diff --git a/tests/unit/AppConfigOverwriteTest.php b/tests/unit/AppConfigOverwriteTest.php new file mode 100644 index 00000000..8f10c3a6 --- /dev/null +++ b/tests/unit/AppConfigOverwriteTest.php @@ -0,0 +1,55 @@ +appConfig = Server::get(AppConfigOverwrite::class); + $this->appConfig->setOverwrite(['core' => [self::KEY => 'yes']]); + } + + protected function tearDown(): void { + $this->appConfig->setOverwrite([]); + + parent::tearDown(); + } + + /** + * The server reads the overwritten values through several getters: + * IConfig::getAppValue() ends up in getValue(), the share recipient + * autocompletion uses getValueString() and OCA\Guests\Config uses + * getValueBool(). All of them have to see the overwrite. + */ + public function testOverwriteIsAppliedToAllGetters(): void { + $this->assertSame('yes', $this->appConfig->getValue('core', self::KEY, 'no')); + $this->assertSame('yes', $this->appConfig->getValueString('core', self::KEY, 'no')); + $this->assertTrue($this->appConfig->getValueBool('core', self::KEY)); + } + + public function testWithoutOverwriteTheStoredValueIsUsed(): void { + $this->appConfig->setOverwrite([]); + + $this->assertSame('no', $this->appConfig->getValue('core', self::KEY, 'no')); + $this->assertSame('no', $this->appConfig->getValueString('core', self::KEY, 'no')); + $this->assertFalse($this->appConfig->getValueBool('core', self::KEY)); + } +}