From f68d4f7300af493d52c85a36561551ca6d6e63de Mon Sep 17 00:00:00 2001 From: Côme Chilliet Date: Thu, 21 Sep 2023 17:25:52 +0200 Subject: Remove deprecated methods Util::writeLog and DIContainer::log MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Côme Chilliet --- .../DependencyInjection/DIContainer.php | 27 ---------------------- 1 file changed, 27 deletions(-) (limited to 'lib/private/AppFramework/DependencyInjection') diff --git a/lib/private/AppFramework/DependencyInjection/DIContainer.php b/lib/private/AppFramework/DependencyInjection/DIContainer.php index a012d1e8ea6..c342ea236e2 100644 --- a/lib/private/AppFramework/DependencyInjection/DIContainer.php +++ b/lib/private/AppFramework/DependencyInjection/DIContainer.php @@ -403,33 +403,6 @@ class DIContainer extends SimpleContainer implements IAppContainer { return $this->getServer()->getSession()->get('user_id'); } - /** - * @deprecated use the ILogger instead - * @param string $message - * @param string $level - * @return mixed - */ - public function log($message, $level) { - switch ($level) { - case 'debug': - $level = ILogger::DEBUG; - break; - case 'info': - $level = ILogger::INFO; - break; - case 'warn': - $level = ILogger::WARN; - break; - case 'fatal': - $level = ILogger::FATAL; - break; - default: - $level = ILogger::ERROR; - break; - } - \OCP\Util::writeLog($this->getAppName(), $message, $level); - } - /** * Register a capability * -- cgit v1.2.3 From 51fa22dc26a9200c80202e92d49873a0c8ead455 Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Wed, 31 Jan 2024 10:40:46 -0100 Subject: fix psalm Signed-off-by: Maxence Lange --- lib/private/AppFramework/DependencyInjection/DIContainer.php | 3 ++- lib/private/AppFramework/Services/AppConfig.php | 12 ++++++++---- lib/public/AppFramework/Services/IAppConfig.php | 4 ++-- 3 files changed, 12 insertions(+), 7 deletions(-) (limited to 'lib/private/AppFramework/DependencyInjection') diff --git a/lib/private/AppFramework/DependencyInjection/DIContainer.php b/lib/private/AppFramework/DependencyInjection/DIContainer.php index c342ea236e2..75eb7890ee2 100644 --- a/lib/private/AppFramework/DependencyInjection/DIContainer.php +++ b/lib/private/AppFramework/DependencyInjection/DIContainer.php @@ -345,7 +345,8 @@ class DIContainer extends SimpleContainer implements IAppContainer { $this->registerService(IAppConfig::class, function (ContainerInterface $c) { return new OC\AppFramework\Services\AppConfig( $c->get(IConfig::class), - $c->get('AppName') + $c->get('AppName'), + $c->get(\OCP\IAppConfig::class) ); }); $this->registerService(IInitialState::class, function (ContainerInterface $c) { diff --git a/lib/private/AppFramework/Services/AppConfig.php b/lib/private/AppFramework/Services/AppConfig.php index 054ce5f145f..a90e38f065f 100644 --- a/lib/private/AppFramework/Services/AppConfig.php +++ b/lib/private/AppFramework/Services/AppConfig.php @@ -37,6 +37,7 @@ class AppConfig implements IAppConfig { public function __construct( private IConfig $config, private string $appName, + /** @var \OC\AppConfig */ private \OCP\IAppConfig $appConfig, ) { } @@ -61,7 +62,7 @@ class AppConfig implements IAppConfig { * @since 29.0.0 */ public function hasAppKey(string $key, ?bool $lazy = false): bool { - return $this->appConfig->hasKey($this->appName, $key); + return $this->appConfig->hasKey($this->appName, $key, $lazy); } /** @@ -73,7 +74,7 @@ class AppConfig implements IAppConfig { * @since 29.0.0 */ public function isSensitive(string $key, ?bool $lazy = false): bool { - return $this->appConfig->isSensitive($this->appName, $key); + return $this->appConfig->isSensitive($this->appName, $key, $lazy); } /** @@ -109,9 +110,10 @@ class AppConfig implements IAppConfig { * @param string $key the key of the value, under which will be saved * @param string $value the value that should be stored * @since 20.0.0 - * @deprecated use {@see setAppValueString()} + * @deprecated 29.0.0 use {@see setAppValueString()} */ public function setAppValue(string $key, string $value): void { + /** @psalm-suppress InternalMethod */ $this->appConfig->setValueMixed($this->appName, $key, $value); } @@ -229,10 +231,12 @@ class AppConfig implements IAppConfig { * @param string $default * * @since 20.0.0 - * @deprecated use {@see getAppValueString()} + * @deprecated 29.0.0 use {@see getAppValueString()} * @return string */ public function getAppValue(string $key, string $default = ''): string { + /** @psalm-suppress InternalMethod */ + /** @psalm-suppress UndefinedInterfaceMethod */ return $this->appConfig->getValueMixed($this->appName, $key, $default); } diff --git a/lib/public/AppFramework/Services/IAppConfig.php b/lib/public/AppFramework/Services/IAppConfig.php index 48c54d80531..9340fdd3c32 100644 --- a/lib/public/AppFramework/Services/IAppConfig.php +++ b/lib/public/AppFramework/Services/IAppConfig.php @@ -100,7 +100,7 @@ interface IAppConfig { * @param string $value the value that should be stored * @return void * @since 20.0.0 - * @deprecated use {@see setAppValueString()} + * @deprecated 29.0.0 use {@see setAppValueString()} */ public function setAppValue(string $key, string $value): void; @@ -211,7 +211,7 @@ interface IAppConfig { * * @return string the saved value * @since 20.0.0 - * @deprecated use {@see getAppValueString()} + * @deprecated 29.0.0 use {@see getAppValueString()} */ public function getAppValue(string $key, string $default = ''): string; -- cgit v1.2.3 From e1d7328bb2962b3789f6a7fa5c0977d7551fefcf Mon Sep 17 00:00:00 2001 From: Maxence Lange Date: Wed, 31 Jan 2024 16:53:47 -0100 Subject: adding test Signed-off-by: Maxence Lange --- .../DependencyInjection/DIContainer.php | 4 +- lib/private/AppFramework/Services/AppConfig.php | 2 +- tests/lib/AppFramework/Services/AppConfigTest.php | 214 +++++++++++++++++++++ 3 files changed, 217 insertions(+), 3 deletions(-) create mode 100644 tests/lib/AppFramework/Services/AppConfigTest.php (limited to 'lib/private/AppFramework/DependencyInjection') diff --git a/lib/private/AppFramework/DependencyInjection/DIContainer.php b/lib/private/AppFramework/DependencyInjection/DIContainer.php index 75eb7890ee2..93365582864 100644 --- a/lib/private/AppFramework/DependencyInjection/DIContainer.php +++ b/lib/private/AppFramework/DependencyInjection/DIContainer.php @@ -345,8 +345,8 @@ class DIContainer extends SimpleContainer implements IAppContainer { $this->registerService(IAppConfig::class, function (ContainerInterface $c) { return new OC\AppFramework\Services\AppConfig( $c->get(IConfig::class), - $c->get('AppName'), - $c->get(\OCP\IAppConfig::class) + $c->get(\OCP\IAppConfig::class), + $c->get('AppName') ); }); $this->registerService(IInitialState::class, function (ContainerInterface $c) { diff --git a/lib/private/AppFramework/Services/AppConfig.php b/lib/private/AppFramework/Services/AppConfig.php index a90e38f065f..1d18baef9ed 100644 --- a/lib/private/AppFramework/Services/AppConfig.php +++ b/lib/private/AppFramework/Services/AppConfig.php @@ -36,9 +36,9 @@ use OCP\IConfig; class AppConfig implements IAppConfig { public function __construct( private IConfig $config, - private string $appName, /** @var \OC\AppConfig */ private \OCP\IAppConfig $appConfig, + private string $appName, ) { } diff --git a/tests/lib/AppFramework/Services/AppConfigTest.php b/tests/lib/AppFramework/Services/AppConfigTest.php new file mode 100644 index 00000000000..5fc0da6a5f3 --- /dev/null +++ b/tests/lib/AppFramework/Services/AppConfigTest.php @@ -0,0 +1,214 @@ + + * + * @license AGPL-3.0 + * + * This code is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License, version 3, + * as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License, version 3, + * along with this program. If not, see + * + */ + +namespace Test\AppFramework\Services; + +use OC\AppFramework\Services\AppConfig; +use OCP\Exceptions\AppConfigUnknownKeyException; +use OCP\IAppConfig as IAppConfigCore; +use OCP\IConfig; +use PHPUnit\Framework\MockObject\MockObject; +use Test\TestCase; + +class AppConfigTest extends TestCase { + private IConfig|MockObject $config; + private IAppConfigCore $appConfigCore; + private AppConfig $appConfig; + + private const TEST_APPID = 'appconfig-test'; + + protected function setUp(): void { + parent::setUp(); + $this->config = $this->createMock(IConfig::class); + + $this->appConfigCore = \OCP\Server::get(IAppConfigCore::class); + + // we reset previous test entries and initiate some value in core appconfig + $this->appConfigCore->deleteApp(self::TEST_APPID); + $this->appConfigCore->setValueString(self::TEST_APPID, 'key1', 'value1'); + $this->appConfigCore->setValueString(self::TEST_APPID, 'key2', 'value0', sensitive: true); + $this->appConfigCore->setValueString(self::TEST_APPID, 'key3', 'value0', true, true); + $this->appConfigCore->setValueInt(self::TEST_APPID, 'key4', 3, true); + $this->appConfigCore->setValueFloat(self::TEST_APPID, 'key5', 3.14, true); + $this->appConfigCore->setValueBool(self::TEST_APPID, 'key6', true); + $this->appConfigCore->setValueArray(self::TEST_APPID, 'key7', ['test1' => 1, 'test2' => 2]); + $this->appConfigCore->setValueBool(self::TEST_APPID, 'test8', true); + + $this->appConfigCore->clearCache(); + $this->appConfig = new AppConfig($this->config, $this->appConfigCore, self::TEST_APPID); + } + + public function testGetAppKeys(): void { + $expected = ['key1', 'key2', 'key3', 'key4', 'key5', 'key6', 'key7', 'test8']; + $this->assertSame($expected, $this->appConfig->getAppKeys()); + } + + public function testHasExistingAppKey(): void { + $this->assertSame(true, $this->appConfig->hasAppKey('key1')); + $this->assertSame(true, $this->appConfig->hasAppKey('key4', true)); + $this->appConfigCore->clearCache(); + $this->assertSame(true, $this->appConfig->hasAppKey('key5', null)); + $this->assertSame(true, $this->appConfig->hasAppKey('test8')); + $this->assertSame(false, $this->appConfig->hasAppKey('inexisting-key')); + } + + public function testIsSensitive(): void { + $this->assertSame(false, $this->appConfig->isSensitive('key1')); + $this->assertSame(true, $this->appConfig->isSensitive('key2')); + + try { + $this->assertSame( + false, $this->appConfig->isSensitive('key3'), + 'should throw exception, or call clearCache before this test' + ); + $this->assertSame(true, false, 'not supposed to happen, key3 is set as lazy and sensitive'); + } catch (AppConfigUnknownKeyException $e) { + $this->assertSame(true, true); + } + + $this->assertSame(true, $this->appConfig->isSensitive('key3', true)); + $this->assertSame(false, $this->appConfig->isSensitive('key4', true)); + } + + public function testIsLazy(): void { + $this->assertSame(false, $this->appConfig->isLazy('key1')); + $this->assertSame(false, $this->appConfig->isLazy('key2')); + $this->assertSame(true, $this->appConfig->isLazy('key3')); + $this->assertSame(true, $this->appConfig->isLazy('key4')); + $this->assertSame(true, $this->appConfig->isLazy('key5')); + $this->assertSame(false, $this->appConfig->isLazy('key6')); + $this->assertSame(false, $this->appConfig->isLazy('key7')); + $this->assertSame(false, $this->appConfig->isLazy('test8')); + } + + + // TODO: fix this in core: getAllAppValues() should returns values based on their types instead of all string +// public function testGetAllAppValues(): void { +// $this->assertSame( +// [ +// 'key1' => 'value1', +// 'key2' => 'value0', +// 'key6' => 1, +// 'key7' => [ +// 'test1' => 1, +// 'test2' => 2 +// ], +// 'test8' => 1, +// 'key3' => 'value0', +// 'key4' => 3, +// 'key5' => 3.14 +// ], +// $this->appConfig->getAllAppValues() +// ); +// } + + + public function testSetAppValue(): void { + $this->appConfig->setAppValue('old1', 'newvalue'); + $this->assertSame('newvalue', $this->appConfigCore->getValueString(self::TEST_APPID, 'old1', 'default')); + } + + public function testSetAppValueString(): void { + $this->assertSame('value1', $this->appConfigCore->getValueString(self::TEST_APPID, 'key1', 'default')); + $this->assertSame(false, $this->appConfig->setAppValueString('key1', 'value1')); + $this->assertSame(true, $this->appConfig->setAppValueString('key1', 'newvalue1')); + $this->assertSame('newvalue1', $this->appConfigCore->getValueString(self::TEST_APPID, 'key1', 'default')); + } + + public function testSetAppValueInt(): void { + $this->assertSame(3, $this->appConfigCore->getValueInt(self::TEST_APPID, 'key4', lazy: true)); + $this->assertSame(false, $this->appConfig->setAppValueInt('key4', 3, true)); + $this->assertSame(true, $this->appConfig->setAppValueInt('key4', 4, true)); + $this->assertSame(4, $this->appConfigCore->getValueInt(self::TEST_APPID, 'key4', lazy: true)); + } + + public function testSetAppValueFloat(): void { + $this->assertSame(3.14, $this->appConfigCore->getValueFloat(self::TEST_APPID, 'key5', lazy: true)); + $this->assertSame(false, $this->appConfig->setAppValueFloat('key5', 3.14, true)); + $this->assertSame(true, $this->appConfig->setAppValueFloat('key5', 4.17, true)); + $this->assertSame(4.17, $this->appConfigCore->getValueFloat(self::TEST_APPID, 'key5', lazy: true)); + } + + public function testSetAppValueBool(): void { + $this->assertSame(true, $this->appConfigCore->getValueBool(self::TEST_APPID, 'key6', false)); + $this->assertSame(false, $this->appConfig->setAppValueBool('key6', true)); + $this->assertSame(true, $this->appConfig->setAppValueBool('key6', false)); + $this->assertSame(false, $this->appConfigCore->getValueBool(self::TEST_APPID, 'key6', true)); + } + + public function testSetAppValueArray(): void { + $this->assertSame(['test1' => 1, 'test2' => 2], $this->appConfigCore->getValueArray(self::TEST_APPID, 'key7', [])); + $this->assertSame(false, $this->appConfig->setAppValueArray('key7', ['test1' => 1, 'test2' => 2])); + $this->assertSame(true, $this->appConfig->setAppValueArray('key7', ['test3' => 0])); + $this->assertSame(['test3' => 0], $this->appConfigCore->getValueArray(self::TEST_APPID, 'key7', [])); + } + + public function testGetAppValue(): void { + $this->appConfigCore->setValueMixed(self::TEST_APPID, 'old1', 'newvalue1'); + $this->assertSame('newvalue1', $this->appConfig->getAppValue('old1', 'default')); + } + + public function testGetAppValueString(): void { + $this->appConfigCore->setValueString(self::TEST_APPID, 'key1', 'value1new'); + $this->appConfigCore->setValueString(self::TEST_APPID, 'key3', 'value3new', lazy: true); + $this->assertSame('value1new', $this->appConfig->getAppValueString('key1', 'default')); + $this->assertSame('default', $this->appConfig->getAppValueString('key3', 'default')); + $this->assertSame('value3new', $this->appConfig->getAppValueString('key3', 'default', lazy: true)); + } + + public function testGetAppValueInt(): void { + $this->appConfigCore->setValueInt(self::TEST_APPID, 'key4', 14, true); + $this->assertSame(14, $this->appConfig->getAppValueInt('key4', 0, true)); + } + + public function testGetAppValueFloat(): void { + $this->appConfigCore->setValueFloat(self::TEST_APPID, 'key5', 12.34, true); + $this->assertSame(12.34, $this->appConfig->getAppValueFloat('key5', 0, true)); + } + + public function testGetAppValueBool(): void { + $this->appConfigCore->setValueBool(self::TEST_APPID, 'key6', true); + $this->assertSame(true, $this->appConfig->getAppValueBool('key6', false)); + $this->appConfigCore->setValueBool(self::TEST_APPID, 'key6', false); + $this->assertSame(false, $this->appConfig->getAppValueBool('key6', true)); + } + + public function testGetAppValueArray(): void { + $this->appConfigCore->setValueArray(self::TEST_APPID, 'key7', ['test' => 'done']); + $this->assertSame(['test' => 'done'], $this->appConfig->getAppValueArray('key7', [])); + } + + public function testDeleteAppValue(): void { + $this->assertSame(true, $this->appConfigCore->hasKey(self::TEST_APPID, 'test8')); + $this->appConfig->deleteAppValue('test8'); + $this->assertSame(false, $this->appConfigCore->hasKey(self::TEST_APPID, 'test8')); + $this->assertSame(['key1', 'key2', 'key3', 'key4', 'key5', 'key6', 'key7'], $this->appConfigCore->getKeys(self::TEST_APPID)); + } + + public function testDeleteAppValues(): void { + $this->assertSame(['key1', 'key2', 'key3', 'key4', 'key5', 'key6', 'key7', 'test8'], $this->appConfigCore->getKeys(self::TEST_APPID)); + $this->appConfig->deleteAppValues(); + $this->assertSame([], $this->appConfigCore->getKeys(self::TEST_APPID)); + } +} -- cgit v1.2.3