sync with new OCP\IAppConfigtags/v29.0.0beta1
@@ -345,6 +345,7 @@ 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(\OCP\IAppConfig::class), | |||
$c->get('AppName') | |||
); | |||
}); |
@@ -26,39 +26,322 @@ declare(strict_types=1); | |||
*/ | |||
namespace OC\AppFramework\Services; | |||
use InvalidArgumentException; | |||
use JsonException; | |||
use OCP\AppFramework\Services\IAppConfig; | |||
use OCP\Exceptions\AppConfigTypeConflictException; | |||
use OCP\Exceptions\AppConfigUnknownKeyException; | |||
use OCP\IConfig; | |||
class AppConfig implements IAppConfig { | |||
/** @var IConfig */ | |||
private $config; | |||
public function __construct( | |||
private IConfig $config, | |||
/** @var \OC\AppConfig */ | |||
private \OCP\IAppConfig $appConfig, | |||
private string $appName, | |||
) { | |||
} | |||
/** @var string */ | |||
private $appName; | |||
/** | |||
* @inheritDoc | |||
* | |||
* @return string[] list of stored config keys | |||
* @since 20.0.0 | |||
*/ | |||
public function getAppKeys(): array { | |||
return $this->appConfig->getKeys($this->appName); | |||
} | |||
public function __construct(IConfig $config, string $appName) { | |||
$this->config = $config; | |||
$this->appName = $appName; | |||
/** | |||
* @inheritDoc | |||
* | |||
* @param string $key config key | |||
* @param bool|null $lazy TRUE to search within lazy loaded config, NULL to search within all config | |||
* | |||
* @return bool TRUE if key exists | |||
* @since 29.0.0 | |||
*/ | |||
public function hasAppKey(string $key, ?bool $lazy = false): bool { | |||
return $this->appConfig->hasKey($this->appName, $key, $lazy); | |||
} | |||
public function getAppKeys(): array { | |||
return $this->config->getAppKeys($this->appName); | |||
/** | |||
* @param string $key config key | |||
* @param bool|null $lazy TRUE to search within lazy loaded config, NULL to search within all config | |||
* | |||
* @return bool | |||
* @throws AppConfigUnknownKeyException if config key is not known | |||
* @since 29.0.0 | |||
*/ | |||
public function isSensitive(string $key, ?bool $lazy = false): bool { | |||
return $this->appConfig->isSensitive($this->appName, $key, $lazy); | |||
} | |||
/** | |||
* @inheritDoc | |||
* | |||
* @param string $key config key | |||
* | |||
* @return bool TRUE if config is lazy loaded | |||
* @throws AppConfigUnknownKeyException if config key is not known | |||
* @see \OCP\IAppConfig for details about lazy loading | |||
* @since 29.0.0 | |||
*/ | |||
public function isLazy(string $key): bool { | |||
return $this->appConfig->isLazy($this->appName, $key); | |||
} | |||
/** | |||
* @inheritDoc | |||
* | |||
* @param string $key config keys prefix to search | |||
* @param bool $filtered TRUE to hide sensitive config values. Value are replaced by {@see IConfig::SENSITIVE_VALUE} | |||
* | |||
* @return array<string, string> [configKey => configValue] | |||
* @since 29.0.0 | |||
*/ | |||
public function getAllAppValues(string $key = '', bool $filtered = false): array { | |||
return $this->appConfig->getAllValues($this->appName, $key, $filtered); | |||
} | |||
/** | |||
* @inheritDoc | |||
* | |||
* @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 29.0.0 use {@see setAppValueString()} | |||
*/ | |||
public function setAppValue(string $key, string $value): void { | |||
$this->config->setAppValue($this->appName, $key, $value); | |||
/** @psalm-suppress InternalMethod */ | |||
$this->appConfig->setValueMixed($this->appName, $key, $value); | |||
} | |||
/** | |||
* @inheritDoc | |||
* | |||
* @param string $key config key | |||
* @param string $value config value | |||
* @param bool $lazy set config as lazy loaded | |||
* @param bool $sensitive if TRUE value will be hidden when listing config values. | |||
* | |||
* @return bool TRUE if value was different, therefor updated in database | |||
* @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one | |||
* @since 29.0.0 | |||
* @see \OCP\IAppConfig for explanation about lazy loading | |||
*/ | |||
public function setAppValueString( | |||
string $key, | |||
string $value, | |||
bool $lazy = false, | |||
bool $sensitive = false | |||
): bool { | |||
return $this->appConfig->setValueString($this->appName, $key, $value, $lazy, $sensitive); | |||
} | |||
/** | |||
* @inheritDoc | |||
* | |||
* @param string $key config key | |||
* @param int $value config value | |||
* @param bool $lazy set config as lazy loaded | |||
* @param bool $sensitive if TRUE value will be hidden when listing config values. | |||
* | |||
* @return bool TRUE if value was different, therefor updated in database | |||
* @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one | |||
* @since 29.0.0 | |||
* @see \OCP\IAppConfig for explanation about lazy loading | |||
*/ | |||
public function setAppValueInt( | |||
string $key, | |||
int $value, | |||
bool $lazy = false, | |||
bool $sensitive = false | |||
): bool { | |||
return $this->appConfig->setValueInt($this->appName, $key, $value, $lazy, $sensitive); | |||
} | |||
/** | |||
* @inheritDoc | |||
* | |||
* @param string $key config key | |||
* @param float $value config value | |||
* @param bool $lazy set config as lazy loaded | |||
* @param bool $sensitive if TRUE value will be hidden when listing config values. | |||
* | |||
* @return bool TRUE if value was different, therefor updated in database | |||
* @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one | |||
* @since 29.0.0 | |||
* @see \OCP\IAppConfig for explanation about lazy loading | |||
*/ | |||
public function setAppValueFloat( | |||
string $key, | |||
float $value, | |||
bool $lazy = false, | |||
bool $sensitive = false | |||
): bool { | |||
return $this->appConfig->setValueFloat($this->appName, $key, $value, $lazy, $sensitive); | |||
} | |||
/** | |||
* @inheritDoc | |||
* | |||
* @param string $key config key | |||
* @param bool $value config value | |||
* @param bool $lazy set config as lazy loaded | |||
* | |||
* @return bool TRUE if value was different, therefor updated in database | |||
* @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one | |||
* @since 29.0.0 | |||
* @see \OCP\IAppConfig for explanation about lazy loading | |||
*/ | |||
public function setAppValueBool( | |||
string $key, | |||
bool $value, | |||
bool $lazy = false | |||
): bool { | |||
return $this->appConfig->setValueBool($this->appName, $key, $value, $lazy); | |||
} | |||
/** | |||
* @inheritDoc | |||
* | |||
* @param string $key config key | |||
* @param array $value config value | |||
* @param bool $lazy set config as lazy loaded | |||
* @param bool $sensitive if TRUE value will be hidden when listing config values. | |||
* | |||
* @return bool TRUE if value was different, therefor updated in database | |||
* @throws AppConfigTypeConflictException if type from database is not VALUE_MIXED and different from the requested one | |||
* @throws JsonException | |||
* @since 29.0.0 | |||
* @see \OCP\IAppConfig for explanation about lazy loading | |||
*/ | |||
public function setAppValueArray( | |||
string $key, | |||
array $value, | |||
bool $lazy = false, | |||
bool $sensitive = false | |||
): bool { | |||
return $this->appConfig->setValueArray($this->appName, $key, $value, $lazy, $sensitive); | |||
} | |||
/** | |||
* @param string $key | |||
* @param string $default | |||
* | |||
* @since 20.0.0 | |||
* @deprecated 29.0.0 use {@see getAppValueString()} | |||
* @return string | |||
*/ | |||
public function getAppValue(string $key, string $default = ''): string { | |||
return $this->config->getAppValue($this->appName, $key, $default); | |||
/** @psalm-suppress InternalMethod */ | |||
/** @psalm-suppress UndefinedInterfaceMethod */ | |||
return $this->appConfig->getValueMixed($this->appName, $key, $default); | |||
} | |||
/** | |||
* @inheritDoc | |||
* | |||
* @param string $key config key | |||
* @param string $default default value | |||
* @param bool $lazy search within lazy loaded config | |||
* | |||
* @return string stored config value or $default if not set in database | |||
* @throws InvalidArgumentException if one of the argument format is invalid | |||
* @throws AppConfigTypeConflictException in case of conflict with the value type set in database | |||
* @since 29.0.0 | |||
* @see \OCP\IAppConfig for explanation about lazy loading | |||
*/ | |||
public function getAppValueString(string $key, string $default = '', bool $lazy = false): string { | |||
return $this->appConfig->getValueString($this->appName, $key, $default, $lazy); | |||
} | |||
/** | |||
* @inheritDoc | |||
* | |||
* @param string $key config key | |||
* @param int $default default value | |||
* @param bool $lazy search within lazy loaded config | |||
* | |||
* @return int stored config value or $default if not set in database | |||
* @throws InvalidArgumentException if one of the argument format is invalid | |||
* @throws AppConfigTypeConflictException in case of conflict with the value type set in database | |||
* @since 29.0.0 | |||
* @see \OCP\IAppConfig for explanation about lazy loading | |||
*/ | |||
public function getAppValueInt(string $key, int $default = 0, bool $lazy = false): int { | |||
return $this->appConfig->getValueInt($this->appName, $key, $default, $lazy); | |||
} | |||
/** | |||
* @inheritDoc | |||
* | |||
* @param string $key config key | |||
* @param float $default default value | |||
* @param bool $lazy search within lazy loaded config | |||
* | |||
* @return float stored config value or $default if not set in database | |||
* @throws InvalidArgumentException if one of the argument format is invalid | |||
* @throws AppConfigTypeConflictException in case of conflict with the value type set in database | |||
* @since 29.0.0 | |||
* @see \OCP\IAppConfig for explanation about lazy loading | |||
*/ | |||
public function getAppValueFloat(string $key, float $default = 0, bool $lazy = false): float { | |||
return $this->appConfig->getValueFloat($this->appName, $key, $default, $lazy); | |||
} | |||
/** | |||
* @inheritDoc | |||
* | |||
* @param string $key config key | |||
* @param bool $default default value | |||
* @param bool $lazy search within lazy loaded config | |||
* | |||
* @return bool stored config value or $default if not set in database | |||
* @throws InvalidArgumentException if one of the argument format is invalid | |||
* @throws AppConfigTypeConflictException in case of conflict with the value type set in database | |||
* @since 29.0.0 | |||
* @see \OCP\IAppConfig for explanation about lazy loading | |||
*/ | |||
public function getAppValueBool(string $key, bool $default = false, bool $lazy = false): bool { | |||
return $this->appConfig->getValueBool($this->appName, $key, $default, $lazy); | |||
} | |||
/** | |||
* @inheritDoc | |||
* | |||
* @param string $key config key | |||
* @param array $default default value | |||
* @param bool $lazy search within lazy loaded config | |||
* | |||
* @return array stored config value or $default if not set in database | |||
* @throws InvalidArgumentException if one of the argument format is invalid | |||
* @throws AppConfigTypeConflictException in case of conflict with the value type set in database | |||
* @since 29.0.0 | |||
* @see \OCP\IAppConfig for explanation about lazy loading | |||
*/ | |||
public function getAppValueArray(string $key, array $default = [], bool $lazy = false): array { | |||
return $this->appConfig->getValueArray($this->appName, $key, $default, $lazy); | |||
} | |||
/** | |||
* @inheritDoc | |||
* | |||
* @param string $key the key of the value, under which it was saved | |||
* @since 20.0.0 | |||
*/ | |||
public function deleteAppValue(string $key): void { | |||
$this->config->deleteAppValue($this->appName, $key); | |||
$this->appConfig->deleteKey($this->appName, $key); | |||
} | |||
/** | |||
* @inheritDoc | |||
* | |||
* @since 20.0.0 | |||
*/ | |||
public function deleteAppValues(): void { | |||
$this->config->deleteAppValues($this->appName); | |||
$this->appConfig->deleteApp($this->appName); | |||
} | |||
public function setUserValue(string $userId, string $key, string $value, ?string $preCondition = null): void { |
@@ -5,6 +5,7 @@ declare(strict_types=1); | |||
/** | |||
* @copyright Copyright (c) 2020, Roeland Jago Douma <roeland@famdouma.nl> | |||
* | |||
* @author Maxence Lange <maxence@artificial-owl.com> | |||
* @author Roeland Jago Douma <roeland@famdouma.nl> | |||
* | |||
* @license GNU AGPL version 3 or any later version | |||
@@ -25,6 +26,8 @@ declare(strict_types=1); | |||
*/ | |||
namespace OCP\AppFramework\Services; | |||
use OCP\Exceptions\AppConfigUnknownKeyException; | |||
/** | |||
* Wrapper for AppConfig for the AppFramework | |||
* | |||
@@ -37,7 +40,58 @@ interface IAppConfig { | |||
* @return string[] the keys stored for the app | |||
* @since 20.0.0 | |||
*/ | |||
public function getAppKeys(): array ; | |||
public function getAppKeys(): array; | |||
/** | |||
* Check if a key exists in the list of stored config values. | |||
* | |||
* @param string $key config key | |||
* @param bool $lazy search within lazy loaded config | |||
* | |||
* @return bool TRUE if key exists | |||
* @since 29.0.0 | |||
*/ | |||
public function hasAppKey(string $key, ?bool $lazy = false): bool; | |||
/** | |||
* best way to see if a value is set as sensitive (not displayed in report) | |||
* | |||
* @param string $key config key | |||
* @param bool|null $lazy search within lazy loaded config | |||
* | |||
* @return bool TRUE if value is sensitive | |||
* @throws AppConfigUnknownKeyException if config key is not known | |||
* @since 29.0.0 | |||
*/ | |||
public function isSensitive(string $key, ?bool $lazy = false): bool; | |||
/** | |||
* Returns if the config key stored in database is lazy loaded | |||
* | |||
* **WARNING:** ignore lazy filtering, all config values are loaded from database | |||
* | |||
* @param string $key config key | |||
* | |||
* @return bool TRUE if config is lazy loaded | |||
* @throws AppConfigUnknownKeyException if config key is not known | |||
* @see IAppConfig for details about lazy loading | |||
* @since 29.0.0 | |||
*/ | |||
public function isLazy(string $key): bool; | |||
/** | |||
* List all config values from an app with config key starting with $key. | |||
* Returns an array with config key as key, stored value as value. | |||
* | |||
* **WARNING:** ignore lazy filtering, all config values are loaded from database | |||
* | |||
* @param string $key config keys prefix to search, can be empty. | |||
* @param bool $filtered filter sensitive config values | |||
* | |||
* @return array<string, string> [configKey => configValue] | |||
* @since 29.0.0 | |||
*/ | |||
public function getAllAppValues(string $key = '', bool $filtered = false): array; | |||
/** | |||
* Writes a new app wide value | |||
@@ -46,19 +100,196 @@ interface IAppConfig { | |||
* @param string $value the value that should be stored | |||
* @return void | |||
* @since 20.0.0 | |||
* @deprecated 29.0.0 use {@see setAppValueString()} | |||
*/ | |||
public function setAppValue(string $key, string $value): void; | |||
/** | |||
* Store a config key and its value in database | |||
* | |||
* If config key is already known with the exact same config value, the database is not updated. | |||
* If config key is not supposed to be read during the boot of the cloud, it is advised to set it as lazy loaded. | |||
* | |||
* If config value was previously stored as sensitive or lazy loaded, status cannot be altered without using {@see deleteKey()} first | |||
* | |||
* @param string $key config key | |||
* @param string $value config value | |||
* @param bool $sensitive if TRUE value will be hidden when listing config values. | |||
* @param bool $lazy set config as lazy loaded | |||
* | |||
* @return bool TRUE if value was different, therefor updated in database | |||
* @since 29.0.0 | |||
* @see \OCP\IAppConfig for explanation about lazy loading | |||
*/ | |||
public function setAppValueString(string $key, string $value, bool $lazy = false, bool $sensitive = false): bool; | |||
/** | |||
* Store a config key and its value in database | |||
* | |||
* When handling huge value around and/or above 2,147,483,647, a debug log will be generated | |||
* on 64bits system, as php int type reach its limit (and throw an exception) on 32bits when using huge numbers. | |||
* | |||
* When using huge numbers, it is advised to use {@see \OCP\Util::numericToNumber()} and {@see setValueString()} | |||
* | |||
* If config key is already known with the exact same config value, the database is not updated. | |||
* If config key is not supposed to be read during the boot of the cloud, it is advised to set it as lazy loaded. | |||
* | |||
* If config value was previously stored as sensitive or lazy loaded, status cannot be altered without using {@see deleteKey()} first | |||
* | |||
* @param string $key config key | |||
* @param int $value config value | |||
* @param bool $sensitive if TRUE value will be hidden when listing config values. | |||
* @param bool $lazy set config as lazy loaded | |||
* | |||
* @return bool TRUE if value was different, therefor updated in database | |||
* @since 29.0.0 | |||
* @see \OCP\IAppConfig for explanation about lazy loading | |||
*/ | |||
public function setAppValueInt(string $key, int $value, bool $lazy = false, bool $sensitive = false): bool; | |||
/** | |||
* Store a config key and its value in database. | |||
* | |||
* If config key is already known with the exact same config value, the database is not updated. | |||
* If config key is not supposed to be read during the boot of the cloud, it is advised to set it as lazy loaded. | |||
* | |||
* If config value was previously stored as sensitive or lazy loaded, status cannot be altered without using {@see deleteKey()} first | |||
* | |||
* @param string $key config key | |||
* @param float $value config value | |||
* @param bool $sensitive if TRUE value will be hidden when listing config values. | |||
* @param bool $lazy set config as lazy loaded | |||
* | |||
* @return bool TRUE if value was different, therefor updated in database | |||
* @since 29.0.0 | |||
* @see \OCP\IAppConfig for explanation about lazy loading | |||
*/ | |||
public function setAppValueFloat(string $key, float $value, bool $lazy = false, bool $sensitive = false): bool; | |||
/** | |||
* Store a config key and its value in database | |||
* | |||
* If config key is already known with the exact same config value, the database is not updated. | |||
* If config key is not supposed to be read during the boot of the cloud, it is advised to set it as lazy loaded. | |||
* | |||
* If config value was previously stored as lazy loaded, status cannot be altered without using {@see deleteKey()} first | |||
* | |||
* @param string $key config key | |||
* @param bool $value config value | |||
* @param bool $lazy set config as lazy loaded | |||
* | |||
* @return bool TRUE if value was different, therefor updated in database | |||
* @since 29.0.0 | |||
* @see \OCP\IAppConfig for explanation about lazy loading | |||
*/ | |||
public function setAppValueBool(string $key, bool $value, bool $lazy = false): bool; | |||
/** | |||
* Store a config key and its value in database | |||
* | |||
* If config key is already known with the exact same config value, the database is not updated. | |||
* If config key is not supposed to be read during the boot of the cloud, it is advised to set it as lazy loaded. | |||
* | |||
* If config value was previously stored as sensitive or lazy loaded, status cannot be altered without using {@see deleteKey()} first | |||
* | |||
* @param string $key config key | |||
* @param array $value config value | |||
* @param bool $sensitive if TRUE value will be hidden when listing config values. | |||
* @param bool $lazy set config as lazy loaded | |||
* | |||
* @return bool TRUE if value was different, therefor updated in database | |||
* @since 29.0.0 | |||
* @see \OCP\IAppConfig for explanation about lazy loading | |||
*/ | |||
public function setAppValueArray(string $key, array $value, bool $lazy = false, bool $sensitive = false): bool; | |||
/** | |||
* Looks up an app wide defined value | |||
* | |||
* @param string $key the key of the value, under which it was saved | |||
* @param string $default the default value to be returned if the value isn't set | |||
* | |||
* @return string the saved value | |||
* @since 20.0.0 | |||
* @deprecated 29.0.0 use {@see getAppValueString()} | |||
*/ | |||
public function getAppValue(string $key, string $default = ''): string; | |||
/** | |||
* Get config value assigned to a config key. | |||
* If config key is not found in database, default value is returned. | |||
* If config key is set as lazy loaded, the $lazy argument needs to be set to TRUE. | |||
* | |||
* @param string $key config key | |||
* @param string $default default value | |||
* @param bool $lazy search within lazy loaded config | |||
* | |||
* @return string stored config value or $default if not set in database | |||
* @since 29.0.0 | |||
* @see \OCP\IAppConfig for explanation about lazy loading | |||
*/ | |||
public function getAppValueString(string $key, string $default = '', bool $lazy = false): string; | |||
/** | |||
* Get config value assigned to a config key. | |||
* If config key is not found in database, default value is returned. | |||
* If config key is set as lazy loaded, the $lazy argument needs to be set to TRUE. | |||
* | |||
* @param string $key config key | |||
* @param int $default default value | |||
* @param bool $lazy search within lazy loaded config | |||
* | |||
* @return int stored config value or $default if not set in database | |||
* @since 29.0.0 | |||
* @see \OCP\IAppConfig for explanation about lazy loading | |||
*/ | |||
public function getAppValueInt(string $key, int $default = 0, bool $lazy = false): int; | |||
/** | |||
* Get config value assigned to a config key. | |||
* If config key is not found in database, default value is returned. | |||
* If config key is set as lazy loaded, the $lazy argument needs to be set to TRUE. | |||
* | |||
* @param string $key config key | |||
* @param float $default default value | |||
* @param bool $lazy search within lazy loaded config | |||
* | |||
* @return float stored config value or $default if not set in database | |||
* @since 29.0.0 | |||
* @see \OCP\IAppConfig for explanation about lazy loading | |||
*/ | |||
public function getAppValueFloat(string $key, float $default = 0, bool $lazy = false): float; | |||
/** | |||
* Get config value assigned to a config key. | |||
* If config key is not found in database, default value is returned. | |||
* If config key is set as lazy loaded, the $lazy argument needs to be set to TRUE. | |||
* | |||
* @param string $key config key | |||
* @param bool $default default value | |||
* @param bool $lazy search within lazy loaded config | |||
* | |||
* @return bool stored config value or $default if not set in database | |||
* @since 29.0.0 | |||
* @see \OCP\IAppConfig for explanation about lazy loading | |||
*/ | |||
public function getAppValueBool(string $key, bool $default = false, bool $lazy = false): bool; | |||
/** | |||
* Get config value assigned to a config key. | |||
* If config key is not found in database, default value is returned. | |||
* If config key is set as lazy loaded, the $lazy argument needs to be set to TRUE. | |||
* | |||
* @param string $key config key | |||
* @param array $default default value | |||
* @param bool $lazy search within lazy loaded config | |||
* | |||
* @return array stored config value or $default if not set in database | |||
* @since 29.0.0 | |||
* @see \OCP\IAppConfig for explanation about lazy loading | |||
*/ | |||
public function getAppValueArray(string $key, array $default = [], bool $lazy = false): array; | |||
/** | |||
* Delete an app wide defined value | |||
* |
@@ -0,0 +1,691 @@ | |||
<?php | |||
declare(strict_types=1); | |||
/** | |||
* @copyright Copyright (c) 2024, Maxence Lange <maxence@artificial-owl.com | |||
* | |||
* @author Maxence Lange <maxence@artificial-owl.com> | |||
* | |||
* @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 <http://www.gnu.org/licenses/> | |||
* | |||
*/ | |||
namespace Test\AppFramework\Services; | |||
use OC\AppConfig as AppConfigCore; | |||
use OC\AppFramework\Services\AppConfig; | |||
use OCP\Exceptions\AppConfigTypeConflictException; | |||
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|MockObject $appConfigCore; | |||
private AppConfig $appConfig; | |||
private const TEST_APPID = 'appconfig-test'; | |||
protected function setUp(): void { | |||
parent::setUp(); | |||
$this->config = $this->createMock(IConfig::class); | |||
$this->appConfigCore = $this->createMock(AppConfigCore::class); | |||
$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->appConfigCore->expects($this->once()) | |||
->method('getKeys') | |||
->with(self::TEST_APPID) | |||
->willReturn($expected); | |||
$this->assertSame($expected, $this->appConfig->getAppKeys()); | |||
} | |||
/** | |||
* @return array | |||
* @see testHasAppKey | |||
*/ | |||
public function providerHasAppKey(): array { | |||
return [ | |||
// lazy, expected | |||
[false, true], | |||
[true, true], | |||
[false, false], | |||
[true, false], | |||
]; | |||
} | |||
/** | |||
* @dataProvider providerHasAppKey | |||
* | |||
* @param bool $lazy | |||
* @param bool $expected | |||
*/ | |||
public function testHasAppKey(bool $lazy, bool $expected): void { | |||
$key = 'key'; | |||
$this->appConfigCore->expects($this->once()) | |||
->method('hasKey') | |||
->with(self::TEST_APPID, $key, $lazy) | |||
->willReturn($expected); | |||
$this->assertSame($expected, $this->appConfig->hasAppKey($key, $lazy)); | |||
} | |||
/** | |||
* @return array | |||
* @see testIsSensitive | |||
*/ | |||
public function providerIsSensitive(): array { | |||
return [ | |||
// lazy, expected | |||
[false, true], | |||
[true, true], | |||
[false, false], | |||
[true, false], | |||
]; | |||
} | |||
/** | |||
* @dataProvider providerIsSensitive | |||
* | |||
* @param bool $lazy | |||
* @param bool $expected | |||
*/ | |||
public function testIsSensitive(bool $lazy, bool $expected): void { | |||
$key = 'key'; | |||
$this->appConfigCore->expects($this->once()) | |||
->method('isSensitive') | |||
->with(self::TEST_APPID, $key, $lazy) | |||
->willReturn($expected); | |||
$this->assertSame($expected, $this->appConfig->isSensitive($key, $lazy)); | |||
} | |||
/** | |||
* @dataProvider providerIsSensitive | |||
* | |||
* @param bool $lazy | |||
* @param bool $expected | |||
*/ | |||
public function testIsSensitiveException(bool $lazy, bool $expected): void { | |||
$key = 'unknown-key'; | |||
$this->appConfigCore->expects($this->once()) | |||
->method('isSensitive') | |||
->with(self::TEST_APPID, $key, $lazy) | |||
->willThrowException(new AppConfigUnknownKeyException()); | |||
$this->expectException(AppConfigUnknownKeyException::class); | |||
$this->appConfig->isSensitive($key, $lazy); | |||
} | |||
/** | |||
* @return array | |||
* @see testIsLazy | |||
*/ | |||
public function providerIsLazy(): array { | |||
return [ | |||
// expected | |||
[true], | |||
[false], | |||
]; | |||
} | |||
/** | |||
* @dataProvider providerIsLazy | |||
* | |||
* @param bool $expected | |||
*/ | |||
public function testIsLazy(bool $expected): void { | |||
$key = 'key'; | |||
$this->appConfigCore->expects($this->once()) | |||
->method('isLazy') | |||
->with(self::TEST_APPID, $key) | |||
->willReturn($expected); | |||
$this->assertSame($expected, $this->appConfig->isLazy($key)); | |||
} | |||
public function testIsLazyException(): void { | |||
$key = 'unknown-key'; | |||
$this->appConfigCore->expects($this->once()) | |||
->method('isLazy') | |||
->with(self::TEST_APPID, $key) | |||
->willThrowException(new AppConfigUnknownKeyException()); | |||
$this->expectException(AppConfigUnknownKeyException::class); | |||
$this->appConfig->isLazy($key); | |||
} | |||
/** | |||
* @return array | |||
* @see testGetAllAppValues | |||
*/ | |||
public function providerGetAllAppValues(): array { | |||
return [ | |||
// key, filtered | |||
['', false], | |||
['', true], | |||
['key', false], | |||
['key', true], | |||
]; | |||
} | |||
/** | |||
* @dataProvider providerGetAllAppValues | |||
* | |||
* @param string $key | |||
* @param bool $filtered | |||
*/ | |||
public function testGetAllAppValues(string $key, bool $filtered): void { | |||
$expected = [ | |||
'key1' => 'value1', | |||
'key2' => 3, | |||
'key3' => 3.14, | |||
'key4' => true | |||
]; | |||
$this->appConfigCore->expects($this->once()) | |||
->method('getAllValues') | |||
->with(self::TEST_APPID, $key, $filtered) | |||
->willReturn($expected); | |||
$this->assertSame($expected, $this->appConfig->getAllAppValues($key, $filtered)); | |||
} | |||
public function testSetAppValue(): void { | |||
$key = 'key'; | |||
$value = 'value'; | |||
$this->appConfigCore->expects($this->once()) | |||
->method('setValueMixed') | |||
->with(self::TEST_APPID, $key, $value); | |||
$this->appConfig->setAppValue($key, $value); | |||
} | |||
/** | |||
* @return array | |||
* @see testSetAppValueString | |||
* @see testSetAppValueStringException | |||
* @see testSetAppValueInt | |||
* @see testSetAppValueIntException | |||
* @see testSetAppValueFloat | |||
* @see testSetAppValueFloatException | |||
* @see testSetAppValueArray | |||
* @see testSetAppValueArrayException | |||
*/ | |||
public function providerSetAppValue(): array { | |||
return [ | |||
// lazy, sensitive, expected | |||
[false, false, true], | |||
[false, true, true], | |||
[true, true, true], | |||
[true, false, true], | |||
[false, false, false], | |||
[false, true, false], | |||
[true, true, false], | |||
[true, false, false], | |||
]; | |||
} | |||
/** | |||
* @dataProvider providerSetAppValue | |||
* | |||
* @param bool $lazy | |||
* @param bool $sensitive | |||
* @param bool $expected | |||
*/ | |||
public function testSetAppValueString(bool $lazy, bool $sensitive, bool $expected): void { | |||
$key = 'key'; | |||
$value = 'valueString'; | |||
$this->appConfigCore->expects($this->once()) | |||
->method('setValueString') | |||
->with(self::TEST_APPID, $key, $value, $lazy, $sensitive) | |||
->willReturn($expected); | |||
$this->assertSame($expected, $this->appConfig->setAppValueString($key, $value, $lazy, $sensitive)); | |||
} | |||
/** | |||
* @dataProvider providerSetAppValue | |||
* | |||
* @param bool $lazy | |||
* @param bool $sensitive | |||
*/ | |||
public function testSetAppValueStringException(bool $lazy, bool $sensitive): void { | |||
$key = 'key'; | |||
$value = 'valueString'; | |||
$this->appConfigCore->expects($this->once()) | |||
->method('setValueString') | |||
->with(self::TEST_APPID, $key, $value, $lazy, $sensitive) | |||
->willThrowException(new AppConfigTypeConflictException()); | |||
$this->expectException(AppConfigTypeConflictException::class); | |||
$this->appConfig->setAppValueString($key, $value, $lazy, $sensitive); | |||
} | |||
/** | |||
* @dataProvider providerSetAppValue | |||
* | |||
* @param bool $lazy | |||
* @param bool $sensitive | |||
* @param bool $expected | |||
*/ | |||
public function testSetAppValueInt(bool $lazy, bool $sensitive, bool $expected): void { | |||
$key = 'key'; | |||
$value = 42; | |||
$this->appConfigCore->expects($this->once()) | |||
->method('setValueInt') | |||
->with(self::TEST_APPID, $key, $value, $lazy, $sensitive) | |||
->willReturn($expected); | |||
$this->assertSame($expected, $this->appConfig->setAppValueInt($key, $value, $lazy, $sensitive)); | |||
} | |||
/** | |||
* @dataProvider providerSetAppValue | |||
* | |||
* @param bool $lazy | |||
* @param bool $sensitive | |||
*/ | |||
public function testSetAppValueIntException(bool $lazy, bool $sensitive): void { | |||
$key = 'key'; | |||
$value = 42; | |||
$this->appConfigCore->expects($this->once()) | |||
->method('setValueInt') | |||
->with(self::TEST_APPID, $key, $value, $lazy, $sensitive) | |||
->willThrowException(new AppConfigTypeConflictException()); | |||
$this->expectException(AppConfigTypeConflictException::class); | |||
$this->appConfig->setAppValueInt($key, $value, $lazy, $sensitive); | |||
} | |||
/** | |||
* @dataProvider providerSetAppValue | |||
* | |||
* @param bool $lazy | |||
* @param bool $sensitive | |||
* @param bool $expected | |||
*/ | |||
public function testSetAppValueFloat(bool $lazy, bool $sensitive, bool $expected): void { | |||
$key = 'key'; | |||
$value = 3.14; | |||
$this->appConfigCore->expects($this->once()) | |||
->method('setValueFloat') | |||
->with(self::TEST_APPID, $key, $value, $lazy, $sensitive) | |||
->willReturn($expected); | |||
$this->assertSame($expected, $this->appConfig->setAppValueFloat($key, $value, $lazy, $sensitive)); | |||
} | |||
/** | |||
* @dataProvider providerSetAppValue | |||
* | |||
* @param bool $lazy | |||
* @param bool $sensitive | |||
*/ | |||
public function testSetAppValueFloatException(bool $lazy, bool $sensitive): void { | |||
$key = 'key'; | |||
$value = 3.14; | |||
$this->appConfigCore->expects($this->once()) | |||
->method('setValueFloat') | |||
->with(self::TEST_APPID, $key, $value, $lazy, $sensitive) | |||
->willThrowException(new AppConfigTypeConflictException()); | |||
$this->expectException(AppConfigTypeConflictException::class); | |||
$this->appConfig->setAppValueFloat($key, $value, $lazy, $sensitive); | |||
} | |||
/** | |||
* @return array | |||
* @see testSetAppValueBool | |||
*/ | |||
public function providerSetAppValueBool(): array { | |||
return [ | |||
// lazy, expected | |||
[false, true], | |||
[false, false], | |||
[true, true], | |||
[true, false], | |||
]; | |||
} | |||
/** | |||
* @dataProvider providerSetAppValueBool | |||
* | |||
* @param bool $lazy | |||
* @param bool $expected | |||
*/ | |||
public function testSetAppValueBool(bool $lazy, bool $expected): void { | |||
$key = 'key'; | |||
$value = true; | |||
$this->appConfigCore->expects($this->once()) | |||
->method('setValueBool') | |||
->with(self::TEST_APPID, $key, $value, $lazy) | |||
->willReturn($expected); | |||
$this->assertSame($expected, $this->appConfig->setAppValueBool($key, $value, $lazy)); | |||
} | |||
/** | |||
* @dataProvider providerSetAppValueBool | |||
* | |||
* @param bool $lazy | |||
*/ | |||
public function testSetAppValueBoolException(bool $lazy): void { | |||
$key = 'key'; | |||
$value = true; | |||
$this->appConfigCore->expects($this->once()) | |||
->method('setValueBool') | |||
->with(self::TEST_APPID, $key, $value, $lazy) | |||
->willThrowException(new AppConfigTypeConflictException()); | |||
$this->expectException(AppConfigTypeConflictException::class); | |||
$this->appConfig->setAppValueBool($key, $value, $lazy); | |||
} | |||
/** | |||
* @dataProvider providerSetAppValue | |||
* | |||
* @param bool $lazy | |||
* @param bool $sensitive | |||
* @param bool $expected | |||
*/ | |||
public function testSetAppValueArray(bool $lazy, bool $sensitive, bool $expected): void { | |||
$key = 'key'; | |||
$value = ['item' => true]; | |||
$this->appConfigCore->expects($this->once()) | |||
->method('setValueArray') | |||
->with(self::TEST_APPID, $key, $value, $lazy, $sensitive) | |||
->willReturn($expected); | |||
$this->assertSame($expected, $this->appConfig->setAppValueArray($key, $value, $lazy, $sensitive)); | |||
} | |||
/** | |||
* @dataProvider providerSetAppValue | |||
* | |||
* @param bool $lazy | |||
* @param bool $sensitive | |||
*/ | |||
public function testSetAppValueArrayException(bool $lazy, bool $sensitive): void { | |||
$key = 'key'; | |||
$value = ['item' => true]; | |||
$this->appConfigCore->expects($this->once()) | |||
->method('setValueArray') | |||
->with(self::TEST_APPID, $key, $value, $lazy, $sensitive) | |||
->willThrowException(new AppConfigTypeConflictException()); | |||
$this->expectException(AppConfigTypeConflictException::class); | |||
$this->appConfig->setAppValueArray($key, $value, $lazy, $sensitive); | |||
} | |||
public function testGetAppValue(): void { | |||
$key = 'key'; | |||
$value = 'value'; | |||
$default = 'default'; | |||
$this->appConfigCore->expects($this->once()) | |||
->method('getValueMixed') | |||
->with(self::TEST_APPID, $key, $default) | |||
->willReturn($value); | |||
$this->assertSame($value, $this->appConfig->getAppValue($key, $default)); | |||
} | |||
public function testGetAppValueDefault(): void { | |||
$key = 'key'; | |||
$default = 'default'; | |||
$this->appConfigCore->expects($this->once()) | |||
->method('getValueMixed') | |||
->with(self::TEST_APPID, $key, $default) | |||
->willReturn($default); | |||
$this->assertSame($default, $this->appConfig->getAppValue($key, $default)); | |||
} | |||
/** | |||
* @return array | |||
* @see testGetAppValueString | |||
* @see testGetAppValueStringException | |||
* @see testGetAppValueInt | |||
* @see testGetAppValueIntException | |||
* @see testGetAppValueFloat | |||
* @see testGetAppValueFloatException | |||
* @see testGetAppValueBool | |||
* @see testGetAppValueBoolException | |||
* @see testGetAppValueArray | |||
* @see testGetAppValueArrayException | |||
*/ | |||
public function providerGetAppValue(): array { | |||
return [ | |||
// lazy, exist | |||
[false, false], | |||
[false, true], | |||
[true, true], | |||
[true, false] | |||
]; | |||
} | |||
/** | |||
* @dataProvider providerGetAppValue | |||
* | |||
* @param bool $lazy | |||
* @param bool $exist | |||
*/ | |||
public function testGetAppValueString(bool $lazy, bool $exist): void { | |||
$key = 'key'; | |||
$value = 'valueString'; | |||
$default = 'default'; | |||
$expected = ($exist) ? $value : $default; | |||
$this->appConfigCore->expects($this->once()) | |||
->method('getValueString') | |||
->with(self::TEST_APPID, $key, $default, $lazy) | |||
->willReturn($expected); | |||
$this->assertSame($expected, $this->appConfig->getAppValueString($key, $default, $lazy)); | |||
} | |||
/** | |||
* @dataProvider providerGetAppValue | |||
* | |||
* @param bool $lazy | |||
*/ | |||
public function testGetAppValueStringException(bool $lazy): void { | |||
$key = 'key'; | |||
$default = 'default'; | |||
$this->appConfigCore->expects($this->once()) | |||
->method('getValueString') | |||
->with(self::TEST_APPID, $key, $default, $lazy) | |||
->willThrowException(new AppConfigTypeConflictException()); | |||
$this->expectException(AppConfigTypeConflictException::class); | |||
$this->appConfig->getAppValueString($key, $default, $lazy); | |||
} | |||
/** | |||
* @dataProvider providerGetAppValue | |||
* | |||
* @param bool $lazy | |||
* @param bool $exist | |||
*/ | |||
public function testGetAppValueInt(bool $lazy, bool $exist): void { | |||
$key = 'key'; | |||
$value = 42; | |||
$default = 17; | |||
$expected = ($exist) ? $value : $default; | |||
$this->appConfigCore->expects($this->once()) | |||
->method('getValueInt') | |||
->with(self::TEST_APPID, $key, $default, $lazy) | |||
->willReturn($expected); | |||
$this->assertSame($expected, $this->appConfig->getAppValueInt($key, $default, $lazy)); | |||
} | |||
/** | |||
* @dataProvider providerGetAppValue | |||
* | |||
* @param bool $lazy | |||
*/ | |||
public function testGetAppValueIntException(bool $lazy): void { | |||
$key = 'key'; | |||
$default = 17; | |||
$this->appConfigCore->expects($this->once()) | |||
->method('getValueInt') | |||
->with(self::TEST_APPID, $key, $default, $lazy) | |||
->willThrowException(new AppConfigTypeConflictException()); | |||
$this->expectException(AppConfigTypeConflictException::class); | |||
$this->appConfig->getAppValueInt($key, $default, $lazy); | |||
} | |||
/** | |||
* @dataProvider providerGetAppValue | |||
* | |||
* @param bool $lazy | |||
* @param bool $exist | |||
*/ | |||
public function testGetAppValueFloat(bool $lazy, bool $exist): void { | |||
$key = 'key'; | |||
$value = 3.14; | |||
$default = 17.04; | |||
$expected = ($exist) ? $value : $default; | |||
$this->appConfigCore->expects($this->once()) | |||
->method('getValueFloat') | |||
->with(self::TEST_APPID, $key, $default, $lazy) | |||
->willReturn($expected); | |||
$this->assertSame($expected, $this->appConfig->getAppValueFloat($key, $default, $lazy)); | |||
} | |||
/** | |||
* @dataProvider providerGetAppValue | |||
* | |||
* @param bool $lazy | |||
*/ | |||
public function testGetAppValueFloatException(bool $lazy): void { | |||
$key = 'key'; | |||
$default = 17.04; | |||
$this->appConfigCore->expects($this->once()) | |||
->method('getValueFloat') | |||
->with(self::TEST_APPID, $key, $default, $lazy) | |||
->willThrowException(new AppConfigTypeConflictException()); | |||
$this->expectException(AppConfigTypeConflictException::class); | |||
$this->appConfig->getAppValueFloat($key, $default, $lazy); | |||
} | |||
/** | |||
* @dataProvider providerGetAppValue | |||
* | |||
* @param bool $lazy | |||
* @param bool $exist | |||
*/ | |||
public function testGetAppValueBool(bool $lazy, bool $exist): void { | |||
$key = 'key'; | |||
$value = true; | |||
$default = false; | |||
$expected = ($exist) ? $value : $default; // yes, it can be simplified | |||
$this->appConfigCore->expects($this->once()) | |||
->method('getValueBool') | |||
->with(self::TEST_APPID, $key, $default, $lazy) | |||
->willReturn($expected); | |||
$this->assertSame($expected, $this->appConfig->getAppValueBool($key, $default, $lazy)); | |||
} | |||
/** | |||
* @dataProvider providerGetAppValue | |||
* | |||
* @param bool $lazy | |||
*/ | |||
public function testGetAppValueBoolException(bool $lazy): void { | |||
$key = 'key'; | |||
$default = false; | |||
$this->appConfigCore->expects($this->once()) | |||
->method('getValueBool') | |||
->with(self::TEST_APPID, $key, $default, $lazy) | |||
->willThrowException(new AppConfigTypeConflictException()); | |||
$this->expectException(AppConfigTypeConflictException::class); | |||
$this->appConfig->getAppValueBool($key, $default, $lazy); | |||
} | |||
/** | |||
* @dataProvider providerGetAppValue | |||
* | |||
* @param bool $lazy | |||
* @param bool $exist | |||
*/ | |||
public function testGetAppValueArray(bool $lazy, bool $exist): void { | |||
$key = 'key'; | |||
$value = ['item' => true]; | |||
$default = []; | |||
$expected = ($exist) ? $value : $default; | |||
$this->appConfigCore->expects($this->once()) | |||
->method('getValueArray') | |||
->with(self::TEST_APPID, $key, $default, $lazy) | |||
->willReturn($expected); | |||
$this->assertSame($expected, $this->appConfig->getAppValueArray($key, $default, $lazy)); | |||
} | |||
/** | |||
* @dataProvider providerGetAppValue | |||
* | |||
* @param bool $lazy | |||
*/ | |||
public function testGetAppValueArrayException(bool $lazy): void { | |||
$key = 'key'; | |||
$default = []; | |||
$this->appConfigCore->expects($this->once()) | |||
->method('getValueArray') | |||
->with(self::TEST_APPID, $key, $default, $lazy) | |||
->willThrowException(new AppConfigTypeConflictException()); | |||
$this->expectException(AppConfigTypeConflictException::class); | |||
$this->appConfig->getAppValueArray($key, $default, $lazy); | |||
} | |||
public function testDeleteAppValue(): void { | |||
$key = 'key'; | |||
$this->appConfigCore->expects($this->once()) | |||
->method('deleteKey') | |||
->with(self::TEST_APPID, $key); | |||
$this->appConfig->deleteAppValue($key); | |||
} | |||
public function testDeleteAppValues(): void { | |||
$this->appConfigCore->expects($this->once()) | |||
->method('deleteApp') | |||
->with(self::TEST_APPID); | |||
$this->appConfig->deleteAppValues(); | |||
} | |||
} |