aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/AppConfigTest.php
diff options
context:
space:
mode:
Diffstat (limited to 'tests/lib/AppConfigTest.php')
-rw-r--r--tests/lib/AppConfigTest.php119
1 files changed, 83 insertions, 36 deletions
diff --git a/tests/lib/AppConfigTest.php b/tests/lib/AppConfigTest.php
index 518d7909d70..0ae917a1d9f 100644
--- a/tests/lib/AppConfigTest.php
+++ b/tests/lib/AppConfigTest.php
@@ -9,11 +9,15 @@ namespace Test;
use InvalidArgumentException;
use OC\AppConfig;
+use OC\Config\ConfigManager;
+use OC\Config\PresetManager;
use OCP\Exceptions\AppConfigTypeConflictException;
use OCP\Exceptions\AppConfigUnknownKeyException;
use OCP\IAppConfig;
+use OCP\IConfig;
use OCP\IDBConnection;
use OCP\Security\ICrypto;
+use OCP\Server;
use Psr\Log\LoggerInterface;
/**
@@ -26,34 +30,45 @@ use Psr\Log\LoggerInterface;
class AppConfigTest extends TestCase {
protected IAppConfig $appConfig;
protected IDBConnection $connection;
+ private IConfig $config;
+ private ConfigManager $configManager;
+ private PresetManager $presetManager;
private LoggerInterface $logger;
private ICrypto $crypto;
private array $originalConfig;
/**
- * @var array<string, array<array<string, string, int, bool, bool>>>
- * [appId => [configKey, configValue, valueType, lazy, sensitive]]
+ * @var array<string, array<string, array<string, string, int, bool, bool>>>
+ * [appId => [configKey, configValue, valueType, lazy, sensitive]]
*/
- private array $baseStruct =
- [
+ private static array $baseStruct
+ = [
'testapp' => [
- 'enabled' => ['enabled', 'true'],
+ 'enabled' => ['enabled', 'yes'],
'installed_version' => ['installed_version', '1.2.3'],
'depends_on' => ['depends_on', 'someapp'],
'deletethis' => ['deletethis', 'deletethis'],
'key' => ['key', 'value']
],
+ 'searchtest' => [
+ 'search_key1' => ['search_key1', 'key1', IAppConfig::VALUE_STRING],
+ 'search_key2' => ['search_key2', 'key2', IAppConfig::VALUE_STRING],
+ 'search_key3' => ['search_key3', 'key3', IAppConfig::VALUE_STRING],
+ 'searchnot_key4' => ['searchnot_key4', 'key4', IAppConfig::VALUE_STRING],
+ 'search_key5_lazy' => ['search_key5_lazy', 'key5', IAppConfig::VALUE_STRING, true],
+ ],
'someapp' => [
'key' => ['key', 'value'],
'otherkey' => ['otherkey', 'othervalue']
],
'123456' => [
- 'enabled' => ['enabled', 'true'],
+ 'enabled' => ['enabled', 'yes'],
'key' => ['key', 'value']
],
'anotherapp' => [
- 'enabled' => ['enabled', 'false'],
+ 'enabled' => ['enabled', 'no'],
+ 'installed_version' => ['installed_version', '3.2.1'],
'key' => ['key', 'value']
],
'non-sensitive-app' => [
@@ -86,9 +101,12 @@ class AppConfigTest extends TestCase {
protected function setUp(): void {
parent::setUp();
- $this->connection = \OCP\Server::get(IDBConnection::class);
- $this->logger = \OCP\Server::get(LoggerInterface::class);
- $this->crypto = \OCP\Server::get(ICrypto::class);
+ $this->connection = Server::get(IDBConnection::class);
+ $this->config = Server::get(IConfig::class);
+ $this->configManager = Server::get(ConfigManager::class);
+ $this->presetManager = Server::get(PresetManager::class);
+ $this->logger = Server::get(LoggerInterface::class);
+ $this->crypto = Server::get(ICrypto::class);
// storing current config and emptying the data table
$sql = $this->connection->getQueryBuilder();
@@ -114,14 +132,14 @@ class AppConfigTest extends TestCase {
]
);
- foreach ($this->baseStruct as $appId => $appData) {
+ foreach (self::$baseStruct as $appId => $appData) {
foreach ($appData as $key => $row) {
$value = $row[1];
$type = $row[2] ?? IAppConfig::VALUE_MIXED;
if (($row[4] ?? false) === true) {
$type |= IAppConfig::VALUE_SENSITIVE;
$value = self::invokePrivate(AppConfig::class, 'ENCRYPTION_PREFIX') . $this->crypto->encrypt($value);
- $this->baseStruct[$appId][$key]['encrypted'] = $value;
+ self::$baseStruct[$appId][$key]['encrypted'] = $value;
}
$sql->setParameters(
@@ -175,8 +193,11 @@ class AppConfigTest extends TestCase {
*/
private function generateAppConfig(bool $preLoading = true): IAppConfig {
/** @var AppConfig $config */
- $config = new \OC\AppConfig(
+ $config = new AppConfig(
$this->connection,
+ $this->config,
+ $this->configManager,
+ $this->presetManager,
$this->logger,
$this->crypto,
);
@@ -197,7 +218,7 @@ class AppConfigTest extends TestCase {
$this->assertSame(true, $status['fastLoaded'], $msg);
$this->assertSame(false, $status['lazyLoaded'], $msg);
- $apps = array_values(array_diff(array_keys($this->baseStruct), ['only-lazy']));
+ $apps = array_values(array_diff(array_keys(self::$baseStruct), ['only-lazy']));
$this->assertEqualsCanonicalizing($apps, array_keys($status['fastCache']), $msg);
$this->assertSame([], array_keys($status['lazyCache']), $msg);
}
@@ -208,7 +229,20 @@ class AppConfigTest extends TestCase {
public function testGetApps(): void {
$config = $this->generateAppConfig(false);
- $this->assertEqualsCanonicalizing(array_keys($this->baseStruct), $config->getApps());
+ $this->assertEqualsCanonicalizing(array_keys(self::$baseStruct), $config->getApps());
+ }
+
+ public function testGetAppInstalledVersions(): void {
+ $config = $this->generateAppConfig(false);
+
+ $this->assertEquals(
+ ['testapp' => '1.2.3', 'anotherapp' => '3.2.1'],
+ $config->getAppInstalledVersions(false)
+ );
+ $this->assertEquals(
+ ['testapp' => '1.2.3'],
+ $config->getAppInstalledVersions(true)
+ );
}
/**
@@ -217,9 +251,9 @@ class AppConfigTest extends TestCase {
* @return array<string, string[]> ['appId' => ['key1', 'key2', ]]
* @see testGetKeys
*/
- public function providerGetAppKeys(): array {
+ public static function providerGetAppKeys(): array {
$appKeys = [];
- foreach ($this->baseStruct as $appId => $appData) {
+ foreach (self::$baseStruct as $appId => $appData) {
$keys = [];
foreach ($appData as $row) {
$keys[] = $row[0];
@@ -238,9 +272,9 @@ class AppConfigTest extends TestCase {
* @see testIsLazy
* @see testGetKeys
*/
- public function providerGetKeys(): array {
+ public static function providerGetKeys(): array {
$appKeys = [];
- foreach ($this->baseStruct as $appId => $appData) {
+ foreach (self::$baseStruct as $appId => $appData) {
foreach ($appData as $row) {
$appKeys[] = [
(string)$appId, $row[0], $row[1], $row[2] ?? IAppConfig::VALUE_MIXED, $row[3] ?? false,
@@ -253,11 +287,11 @@ class AppConfigTest extends TestCase {
}
/**
- * @dataProvider providerGetAppKeys
*
* @param string $appId
* @param array $expectedKeys
*/
+ #[\PHPUnit\Framework\Attributes\DataProvider('providerGetAppKeys')]
public function testGetKeys(string $appId, array $expectedKeys): void {
$config = $this->generateAppConfig();
$this->assertEqualsCanonicalizing($expectedKeys, $config->getKeys($appId));
@@ -269,13 +303,13 @@ class AppConfigTest extends TestCase {
}
/**
- * @dataProvider providerGetKeys
*
* @param string $appId
* @param string $configKey
* @param string $value
* @param bool $lazy
*/
+ #[\PHPUnit\Framework\Attributes\DataProvider('providerGetKeys')]
public function testHasKey(string $appId, string $configKey, string $value, int $type, bool $lazy): void {
$config = $this->generateAppConfig();
$this->assertEquals(true, $config->hasKey($appId, $configKey, $lazy));
@@ -283,7 +317,7 @@ class AppConfigTest extends TestCase {
public function testHasKeyOnNonExistentKeyReturnsFalse(): void {
$config = $this->generateAppConfig();
- $this->assertEquals(false, $config->hasKey(array_keys($this->baseStruct)[0], 'inexistant-key'));
+ $this->assertEquals(false, $config->hasKey(array_keys(self::$baseStruct)[0], 'inexistant-key'));
}
public function testHasKeyOnUnknownAppReturnsFalse(): void {
@@ -306,9 +340,7 @@ class AppConfigTest extends TestCase {
$this->assertSame(true, $config->hasKey('non-sensitive-app', 'lazy-key', null));
}
- /**
- * @dataProvider providerGetKeys
- */
+ #[\PHPUnit\Framework\Attributes\DataProvider('providerGetKeys')]
public function testIsSensitive(
string $appId, string $configKey, string $configValue, int $type, bool $lazy, bool $sensitive,
): void {
@@ -319,7 +351,7 @@ class AppConfigTest extends TestCase {
public function testIsSensitiveOnNonExistentKeyThrowsException(): void {
$config = $this->generateAppConfig();
$this->expectException(AppConfigUnknownKeyException::class);
- $config->isSensitive(array_keys($this->baseStruct)[0], 'inexistant-key');
+ $config->isSensitive(array_keys(self::$baseStruct)[0], 'inexistant-key');
}
public function testIsSensitiveOnUnknownAppThrowsException(): void {
@@ -350,9 +382,7 @@ class AppConfigTest extends TestCase {
$config->isSensitive('non-sensitive-app', 'lazy-key', false);
}
- /**
- * @dataProvider providerGetKeys
- */
+ #[\PHPUnit\Framework\Attributes\DataProvider('providerGetKeys')]
public function testIsLazy(string $appId, string $configKey, string $configValue, int $type, bool $lazy,
): void {
$config = $this->generateAppConfig();
@@ -362,7 +392,7 @@ class AppConfigTest extends TestCase {
public function testIsLazyOnNonExistentKeyThrowsException(): void {
$config = $this->generateAppConfig();
$this->expectException(AppConfigUnknownKeyException::class);
- $config->isLazy(array_keys($this->baseStruct)[0], 'inexistant-key');
+ $config->isLazy(array_keys(self::$baseStruct)[0], 'inexistant-key');
}
public function testIsLazyOnUnknownAppThrowsException(): void {
@@ -393,11 +423,11 @@ class AppConfigTest extends TestCase {
}
/**
- * @dataProvider providerGetAppKeys
*
* @param string $appId
* @param array $keys
*/
+ #[\PHPUnit\Framework\Attributes\DataProvider('providerGetAppKeys')]
public function testGetAllValuesWithEmptyKey(string $appId, array $keys): void {
$config = $this->generateAppConfig();
$this->assertEqualsCanonicalizing($keys, array_keys($config->getAllValues($appId, '')));
@@ -410,7 +440,7 @@ class AppConfigTest extends TestCase {
public function testSearchValues(): void {
$config = $this->generateAppConfig();
- $this->assertEqualsCanonicalizing(['testapp' => 'true', '123456' => 'true', 'anotherapp' => 'false'], $config->searchValues('enabled'));
+ $this->assertEqualsCanonicalizing(['testapp' => 'yes', '123456' => 'yes', 'anotherapp' => 'no'], $config->searchValues('enabled'));
}
public function testGetValueString(): void {
@@ -530,7 +560,7 @@ class AppConfigTest extends TestCase {
*
* @see testGetValueMixed
*/
- public function providerGetValueMixed(): array {
+ public static function providerGetValueMixed(): array {
return [
// key, value, type
['mixed', 'mix', IAppConfig::VALUE_MIXED],
@@ -543,23 +573,23 @@ class AppConfigTest extends TestCase {
}
/**
- * @dataProvider providerGetValueMixed
*
* @param string $key
* @param string $value
*/
+ #[\PHPUnit\Framework\Attributes\DataProvider('providerGetValueMixed')]
public function testGetValueMixed(string $key, string $value): void {
$config = $this->generateAppConfig();
$this->assertSame($value, $config->getValueMixed('typed', $key));
}
/**
- * @dataProvider providerGetValueMixed
*
* @param string $key
* @param string $value
* @param int $type
*/
+ #[\PHPUnit\Framework\Attributes\DataProvider('providerGetValueMixed')]
public function testGetValueType(string $key, string $value, int $type): void {
$config = $this->generateAppConfig();
$this->assertSame($type, $config->getValueType('typed', $key));
@@ -1322,7 +1352,7 @@ class AppConfigTest extends TestCase {
$config = $this->generateAppConfig();
$config->deleteKey('anotherapp', 'key');
$status = $config->statusCache();
- $this->assertEqualsCanonicalizing(['enabled' => 'false'], $status['fastCache']['anotherapp']);
+ $this->assertEqualsCanonicalizing(['enabled' => 'no', 'installed_version' => '3.2.1'], $status['fastCache']['anotherapp']);
}
public function testDeleteKeyDatabase(): void {
@@ -1439,6 +1469,23 @@ class AppConfigTest extends TestCase {
$this->assertConfigValueNotEquals('testapp', $key, $secret);
}
+ public function testSearchKeyNoLazyLoading(): void {
+ $appConfig = $this->generateAppConfig();
+ $appConfig->searchKeys('searchtest', 'search_');
+ $status = $appConfig->statusCache();
+ $this->assertFalse($status['lazyLoaded'], 'searchKeys() loaded lazy config');
+ }
+
+ public function testSearchKeyFast(): void {
+ $appConfig = $this->generateAppConfig();
+ $this->assertEquals(['search_key1', 'search_key2', 'search_key3'], $appConfig->searchKeys('searchtest', 'search_'));
+ }
+
+ public function testSearchKeyLazy(): void {
+ $appConfig = $this->generateAppConfig();
+ $this->assertEquals(['search_key5_lazy'], $appConfig->searchKeys('searchtest', 'search_', true));
+ }
+
protected function loadConfigValueFromDatabase(string $app, string $key): string|false {
$sql = $this->connection->getQueryBuilder();
$sql->select('configvalue')