aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/User/Backend.php
blob: 7efba2eaeb675056bf824fe01b010b11897ed6ac (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
<?php
/**
 * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

namespace Test\User;

/**
 * Abstract class to provide the basis of backend-specific unit test classes.
 *
 * All subclasses MUST assign a backend property in setUp() which implements
 * user operations (add, remove, etc.). Test methods in this class will then be
 * run on each separate subclass and backend therein.
 *
 * For an example see /tests/lib/user/dummy.php
 */

abstract class Backend extends \Test\TestCase {
	/**
	 * @var \OC\User\Backend $backend
	 */
	protected $backend;

	/**
	 * get a new unique user name
	 * test cases can override this in order to clean up created user
	 * @return string
	 */
	public function getUser() {
		return $this->getUniqueID('test_');
	}

	public function testAddRemove() {
		//get the number of groups we start with, in case there are exising groups
		$startCount = count($this->backend->getUsers());

		$name1 = $this->getUser();
		$name2 = $this->getUser();
		$this->backend->createUser($name1, '');
		$count = count($this->backend->getUsers()) - $startCount;
		$this->assertEquals(1, $count);
		$this->assertTrue((array_search($name1, $this->backend->getUsers()) !== false));
		$this->assertFalse((array_search($name2, $this->backend->getUsers()) !== false));
		$this->backend->createUser($name2, '');
		$count = count($this->backend->getUsers()) - $startCount;
		$this->assertEquals(2, $count);
		$this->assertTrue((array_search($name1, $this->backend->getUsers()) !== false));
		$this->assertTrue((array_search($name2, $this->backend->getUsers()) !== false));

		$this->backend->deleteUser($name2);
		$count = count($this->backend->getUsers()) - $startCount;
		$this->assertEquals(1, $count);
		$this->assertTrue((array_search($name1, $this->backend->getUsers()) !== false));
		$this->assertFalse((array_search($name2, $this->backend->getUsers()) !== false));
	}

	public function testLogin() {
		$name1 = $this->getUser();
		$name2 = $this->getUser();

		$this->assertFalse($this->backend->userExists($name1));
		$this->assertFalse($this->backend->userExists($name2));

		$this->backend->createUser($name1, 'pass1');
		$this->backend->createUser($name2, 'pass2');

		$this->assertTrue($this->backend->userExists($name1));
		$this->assertTrue($this->backend->userExists($name2));

		$this->assertSame($name1, $this->backend->checkPassword($name1, 'pass1'));
		$this->assertSame($name2, $this->backend->checkPassword($name2, 'pass2'));

		$this->assertFalse($this->backend->checkPassword($name1, 'pass2'));
		$this->assertFalse($this->backend->checkPassword($name2, 'pass1'));

		$this->assertFalse($this->backend->checkPassword($name1, 'dummy'));
		$this->assertFalse($this->backend->checkPassword($name2, 'foobar'));

		$this->backend->setPassword($name1, 'newpass1');
		$this->assertFalse($this->backend->checkPassword($name1, 'pass1'));
		$this->assertSame($name1, $this->backend->checkPassword($name1, 'newpass1'));
		$this->assertFalse($this->backend->checkPassword($name2, 'newpass1'));
	}

	public function testSearch() {
		$name1 = 'foobarbaz';
		$name2 = 'bazbarfoo';
		$name3 = 'notme';
		$name4 = 'under_score';

		$this->backend->createUser($name1, 'pass1');
		$this->backend->createUser($name2, 'pass2');
		$this->backend->createUser($name3, 'pass3');
		$this->backend->createUser($name4, 'pass4');

		$result = $this->backend->getUsers('bar');
		$this->assertCount(2, $result);

		$result = $this->backend->getDisplayNames('bar');
		$this->assertCount(2, $result);

		$result = $this->backend->getUsers('under_');
		$this->assertCount(1, $result);

		$result = $this->backend->getUsers('not_');
		$this->assertCount(0, $result);
	}
}
> 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463
<?php

declare(strict_types=1);
/**
 * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-only
 */
namespace Test;

use InvalidArgumentException;
use OC\AppConfig;
use OCP\Exceptions\AppConfigTypeConflictException;
use OCP\Exceptions\AppConfigUnknownKeyException;
use OCP\IAppConfig;
use OCP\IDBConnection;
use OCP\Security\ICrypto;
use Psr\Log\LoggerInterface;

/**
 * Class AppConfigTest
 *
 * @group DB
 *
 * @package Test
 */
class AppConfigTest extends TestCase {
	protected IAppConfig $appConfig;
	protected IDBConnection $connection;
	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]]
	 */
	private array $baseStruct =
		[
			'testapp' => [
				'enabled' => ['enabled', 'true'],
				'installed_version' => ['installed_version', '1.2.3'],
				'depends_on' => ['depends_on', 'someapp'],
				'deletethis' => ['deletethis', 'deletethis'],
				'key' => ['key', 'value']
			],
			'someapp' => [
				'key' => ['key', 'value'],
				'otherkey' => ['otherkey', 'othervalue']
			],
			'123456' => [
				'enabled' => ['enabled', 'true'],
				'key' => ['key', 'value']
			],
			'anotherapp' => [
				'enabled' => ['enabled', 'false'],
				'key' => ['key', 'value']
			],
			'non-sensitive-app' => [
				'lazy-key' => ['lazy-key', 'value', IAppConfig::VALUE_STRING, true, false],
				'non-lazy-key' => ['non-lazy-key', 'value', IAppConfig::VALUE_STRING, false, false],
			],
			'sensitive-app' => [
				'lazy-key' => ['lazy-key', 'value', IAppConfig::VALUE_STRING, true, true],
				'non-lazy-key' => ['non-lazy-key', 'value', IAppConfig::VALUE_STRING, false, true],
			],
			'only-lazy' => [
				'lazy-key' => ['lazy-key', 'value', IAppConfig::VALUE_STRING, true]
			],
			'typed' => [
				'mixed' => ['mixed', 'mix', IAppConfig::VALUE_MIXED],
				'string' => ['string', 'value', IAppConfig::VALUE_STRING],
				'int' => ['int', '42', IAppConfig::VALUE_INT],
				'float' => ['float', '3.14', IAppConfig::VALUE_FLOAT],
				'bool' => ['bool', '1', IAppConfig::VALUE_BOOL],
				'array' => ['array', '{"test": 1}', IAppConfig::VALUE_ARRAY],
			],
			'prefix-app' => [
				'key1' => ['key1', 'value'],
				'prefix1' => ['prefix1', 'value'],
				'prefix-2' => ['prefix-2', 'value'],
				'key-2' => ['key-2', 'value'],
			]
		];

	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);

		// storing current config and emptying the data table
		$sql = $this->connection->getQueryBuilder();
		$sql->select('*')
			->from('appconfig');
		$result = $sql->executeQuery();
		$this->originalConfig = $result->fetchAll();
		$result->closeCursor();

		$sql = $this->connection->getQueryBuilder();
		$sql->delete('appconfig');
		$sql->executeStatement();

		$sql = $this->connection->getQueryBuilder();
		$sql->insert('appconfig')
			->values(
				[
					'appid' => $sql->createParameter('appid'),
					'configkey' => $sql->createParameter('configkey'),
					'configvalue' => $sql->createParameter('configvalue'),
					'type' => $sql->createParameter('type'),
					'lazy' => $sql->createParameter('lazy')
				]
			);

		foreach ($this->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;
				}

				$sql->setParameters(
					[
						'appid' => $appId,
						'configkey' => $row[0],
						'configvalue' => $value,
						'type' => $type,
						'lazy' => (($row[3] ?? false) === true) ? 1 : 0
					]
				)->executeStatement();
			}
		}
	}

	protected function tearDown(): void {
		$sql = $this->connection->getQueryBuilder();
		$sql->delete('appconfig');
		$sql->executeStatement();

		$sql = $this->connection->getQueryBuilder();
		$sql->insert('appconfig')
			->values(
				[
					'appid' => $sql->createParameter('appid'),
					'configkey' => $sql->createParameter('configkey'),
					'configvalue' => $sql->createParameter('configvalue'),
					'lazy' => $sql->createParameter('lazy'),
					'type' => $sql->createParameter('type'),
				]
			);

		foreach ($this->originalConfig as $key => $configs) {
			$sql->setParameter('appid', $configs['appid'])
				->setParameter('configkey', $configs['configkey'])
				->setParameter('configvalue', $configs['configvalue'])
				->setParameter('lazy', ($configs['lazy'] === '1') ? '1' : '0')
				->setParameter('type', $configs['type']);
			$sql->executeStatement();
		}

		//		$this->restoreService(AppConfig::class);
		parent::tearDown();
	}

	/**
	 * @param bool $preLoading TRUE will preload the 'fast' cache, which is the normal behavior of usual
	 *                         IAppConfig
	 *
	 * @return IAppConfig
	 */
	private function generateAppConfig(bool $preLoading = true): IAppConfig {
		/** @var AppConfig $config */
		$config = new \OC\AppConfig(
			$this->connection,
			$this->logger,
			$this->crypto,
		);
		$msg = ' generateAppConfig() failed to confirm cache status';

		// confirm cache status
		$status = $config->statusCache();
		$this->assertSame(false, $status['fastLoaded'], $msg);
		$this->assertSame(false, $status['lazyLoaded'], $msg);
		$this->assertSame([], $status['fastCache'], $msg);
		$this->assertSame([], $status['lazyCache'], $msg);
		if ($preLoading) {
			// simple way to initiate the load of non-lazy config values in cache
			$config->getValueString('core', 'preload', '');

			// confirm cache status
			$status = $config->statusCache();
			$this->assertSame(true, $status['fastLoaded'], $msg);
			$this->assertSame(false, $status['lazyLoaded'], $msg);

			$apps = array_values(array_diff(array_keys($this->baseStruct), ['only-lazy']));
			$this->assertEqualsCanonicalizing($apps, array_keys($status['fastCache']), $msg);
			$this->assertSame([], array_keys($status['lazyCache']), $msg);
		}

		return $config;
	}

	public function testGetApps(): void {
		$config = $this->generateAppConfig(false);

		$this->assertEqualsCanonicalizing(array_keys($this->baseStruct), $config->getApps());
	}

	/**
	 * returns list of app and their keys
	 *
	 * @return array<string, string[]> ['appId' => ['key1', 'key2', ]]
	 * @see testGetKeys
	 */
	public function providerGetAppKeys(): array {
		$appKeys = [];
		foreach ($this->baseStruct as $appId => $appData) {
			$keys = [];
			foreach ($appData as $row) {
				$keys[] = $row[0];
			}
			$appKeys[] = [(string)$appId, $keys];
		}

		return $appKeys;
	}

	/**
	 * returns list of config keys
	 *
	 * @return array<string, string, string, int, bool, bool> [appId, key, value, type, lazy, sensitive]
	 * @see testIsSensitive
	 * @see testIsLazy
	 * @see testGetKeys
	 */
	public function providerGetKeys(): array {
		$appKeys = [];
		foreach ($this->baseStruct as $appId => $appData) {
			foreach ($appData as $row) {
				$appKeys[] = [
					(string)$appId, $row[0], $row[1], $row[2] ?? IAppConfig::VALUE_MIXED, $row[3] ?? false,
					$row[4] ?? false
				];
			}
		}

		return $appKeys;
	}

	/**
	 * @dataProvider providerGetAppKeys
	 *
	 * @param string $appId
	 * @param array $expectedKeys
	 */
	public function testGetKeys(string $appId, array $expectedKeys): void {
		$config = $this->generateAppConfig();
		$this->assertEqualsCanonicalizing($expectedKeys, $config->getKeys($appId));
	}

	public function testGetKeysOnUnknownAppShouldReturnsEmptyArray(): void {
		$config = $this->generateAppConfig();
		$this->assertEqualsCanonicalizing([], $config->getKeys('unknown-app'));
	}

	/**
	 * @dataProvider providerGetKeys
	 *
	 * @param string $appId
	 * @param string $configKey
	 * @param string $value
	 * @param bool $lazy
	 */
	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));
	}

	public function testHasKeyOnNonExistentKeyReturnsFalse(): void {
		$config = $this->generateAppConfig();
		$this->assertEquals(false, $config->hasKey(array_keys($this->baseStruct)[0], 'inexistant-key'));
	}

	public function testHasKeyOnUnknownAppReturnsFalse(): void {
		$config = $this->generateAppConfig();
		$this->assertEquals(false, $config->hasKey('inexistant-app', 'inexistant-key'));
	}

	public function testHasKeyOnMistypedAsLazyReturnsFalse(): void {
		$config = $this->generateAppConfig();
		$this->assertSame(false, $config->hasKey('non-sensitive-app', 'non-lazy-key', true));
	}

	public function testHasKeyOnMistypeAsNonLazyReturnsFalse(): void {
		$config = $this->generateAppConfig();
		$this->assertSame(false, $config->hasKey('non-sensitive-app', 'lazy-key', false));
	}

	public function testHasKeyOnMistypeAsNonLazyReturnsTrueWithLazyArgumentIsNull(): void {
		$config = $this->generateAppConfig();
		$this->assertSame(true, $config->hasKey('non-sensitive-app', 'lazy-key', null));
	}

	/**
	 * @dataProvider providerGetKeys
	 */
	public function testIsSensitive(
		string $appId, string $configKey, string $configValue, int $type, bool $lazy, bool $sensitive
	): void {
		$config = $this->generateAppConfig();
		$this->assertEquals($sensitive, $config->isSensitive($appId, $configKey, $lazy));
	}

	public function testIsSensitiveOnNonExistentKeyThrowsException(): void {
		$config = $this->generateAppConfig();
		$this->expectException(AppConfigUnknownKeyException::class);
		$config->isSensitive(array_keys($this->baseStruct)[0], 'inexistant-key');
	}

	public function testIsSensitiveOnUnknownAppThrowsException(): void {
		$config = $this->generateAppConfig();
		$this->expectException(AppConfigUnknownKeyException::class);
		$config->isSensitive('unknown-app', 'inexistant-key');
	}

	public function testIsSensitiveOnSensitiveMistypedAsLazy(): void {
		$config = $this->generateAppConfig();
		$this->assertSame(true, $config->isSensitive('sensitive-app', 'non-lazy-key', true));
	}

	public function testIsSensitiveOnNonSensitiveMistypedAsLazy(): void {
		$config = $this->generateAppConfig();
		$this->assertSame(false, $config->isSensitive('non-sensitive-app', 'non-lazy-key', true));
	}

	public function testIsSensitiveOnSensitiveMistypedAsNonLazyThrowsException(): void {
		$config = $this->generateAppConfig();
		$this->expectException(AppConfigUnknownKeyException::class);
		$config->isSensitive('sensitive-app', 'lazy-key', false);
	}

	public function testIsSensitiveOnNonSensitiveMistypedAsNonLazyThrowsException(): void {
		$config = $this->generateAppConfig();
		$this->expectException(AppConfigUnknownKeyException::class);
		$config->isSensitive('non-sensitive-app', 'lazy-key', false);
	}

	/**
	 * @dataProvider providerGetKeys
	 */
	public function testIsLazy(string $appId, string $configKey, string $configValue, int $type, bool $lazy
	): void {
		$config = $this->generateAppConfig();
		$this->assertEquals($lazy, $config->isLazy($appId, $configKey));
	}

	public function testIsLazyOnNonExistentKeyThrowsException(): void {
		$config = $this->generateAppConfig();
		$this->expectException(AppConfigUnknownKeyException::class);
		$config->isLazy(array_keys($this->baseStruct)[0], 'inexistant-key');
	}

	public function testIsLazyOnUnknownAppThrowsException(): void {
		$config = $this->generateAppConfig();
		$this->expectException(AppConfigUnknownKeyException::class);
		$config->isLazy('unknown-app', 'inexistant-key');
	}

	public function testGetAllValues(): void {
		$config = $this->generateAppConfig();
		$this->assertEquals(
			[
				'array' => ['test' => 1],
				'bool' => true,
				'float' => 3.14,
				'int' => 42,
				'mixed' => 'mix',
				'string' => 'value',
			],
			$config->getAllValues('typed')
		);
	}

	public function testGetAllValuesWithEmptyApp(): void {
		$config = $this->generateAppConfig();
		$this->expectException(InvalidArgumentException::class);
		$config->getAllValues('');
	}

	/**
	 * @dataProvider providerGetAppKeys
	 *
	 * @param string $appId
	 * @param array $keys
	 */
	public function testGetAllValuesWithEmptyKey(string $appId, array $keys): void {
		$config = $this->generateAppConfig();
		$this->assertEqualsCanonicalizing($keys, array_keys($config->getAllValues($appId, '')));
	}

	public function testGetAllValuesWithPrefix(): void {
		$config = $this->generateAppConfig();
		$this->assertEqualsCanonicalizing(['prefix1', 'prefix-2'], array_keys($config->getAllValues('prefix-app', 'prefix')));
	}

	public function testSearchValues(): void {
		$config = $this->generateAppConfig();
		$this->assertEqualsCanonicalizing(['testapp' => 'true', '123456' => 'true', 'anotherapp' => 'false'], $config->searchValues('enabled'));
	}

	public function testGetValueString(): void {
		$config = $this->generateAppConfig();
		$this->assertSame('value', $config->getValueString('typed', 'string', ''));
	}

	public function testGetValueStringOnUnknownAppReturnsDefault(): void {
		$config = $this->generateAppConfig();
		$this->assertSame('default-1', $config->getValueString('typed-1', 'string', 'default-1'));
	}

	public function testGetValueStringOnNonExistentKeyReturnsDefault(): void {
		$config = $this->generateAppConfig();
		$this->assertSame('default-2', $config->getValueString('typed', 'string-2', 'default-2'));
	}

	public function testGetValueStringOnWrongType(): void {
		$config = $this->generateAppConfig();
		$this->expectException(AppConfigTypeConflictException::class);
		$config->getValueString('typed', 'int');
	}

	public function testGetNonLazyValueStringAsLazy(): void {
		$config = $this->generateAppConfig();
		$this->assertSame('value', $config->getValueString('non-sensitive-app', 'non-lazy-key', 'default', lazy: true));
	}

	public function testGetValueInt(): void {
		$config = $this->generateAppConfig();
		$this->assertSame(42, $config->getValueInt('typed', 'int', 0));
	}

	public function testGetValueIntOnUnknownAppReturnsDefault(): void {
		$config = $this->generateAppConfig();
		$this->assertSame(1, $config->getValueInt('typed-1', 'int', 1));
	}

	public function testGetValueIntOnNonExistentKeyReturnsDefault(): void {
		$config = $this->generateAppConfig();
		$this->assertSame(2, $config->getValueInt('typed', 'int-2', 2));
	}

	public function testGetValueIntOnWrongType(): void {
		$config = $this->generateAppConfig();
		$this->expectException(AppConfigTypeConflictException::class);
		$config->getValueInt('typed', 'float');
	}

	public function testGetValueFloat(): void {
		$config = $this->generateAppConfig();
		$this->assertSame(3.14, $config->getValueFloat('typed', 'float', 0));
	}

	public function testGetValueFloatOnNonUnknownAppReturnsDefault(): void {
		$config = $this->generateAppConfig();
		$this->assertSame(1.11, $config->getValueFloat('typed-1', 'float', 1.11));
	}

	public function testGetValueFloatOnNonExistentKeyReturnsDefault(): void {
		$config = $this->generateAppConfig();
		$this->assertSame(2.22, $config->getValueFloat('typed', 'float-2', 2.22));
	}

	public function testGetValueFloatOnWrongType(): void {
		$config = $this->generateAppConfig();
		$this->expectException(AppConfigTypeConflictException::class);
		$config->getValueFloat('typed', 'bool');
	}

	public function testGetValueBool(): void {
		$config = $this->generateAppConfig();
		$this->assertSame(true, $config->getValueBool('typed', 'bool'));
	}

	public function testGetValueBoolOnUnknownAppReturnsDefault(): void {
		$config = $this->generateAppConfig();
		$this->assertSame(false, $config->getValueBool('typed-1', 'bool', false));
	}

	public function testGetValueBoolOnNonExistentKeyReturnsDefault(): void {
		$config = $this->generateAppConfig();
		$this->assertSame(false, $config->getValueBool('typed', 'bool-2'));
	}

	public function testGetValueBoolOnWrongType(): void {
		$config = $this->generateAppConfig();
		$this->expectException(AppConfigTypeConflictException::class);
		$config->getValueBool('typed', 'array');
	}

	public function testGetValueArray(): void {
		$config = $this->generateAppConfig();
		$this->assertEqualsCanonicalizing(['test' => 1], $config->getValueArray('typed', 'array', []));
	}

	public function testGetValueArrayOnUnknownAppReturnsDefault(): void {
		$config = $this->generateAppConfig();
		$this->assertSame([1], $config->getValueArray('typed-1', 'array', [1]));
	}

	public function testGetValueArrayOnNonExistentKeyReturnsDefault(): void {
		$config = $this->generateAppConfig();
		$this->assertSame([1, 2], $config->getValueArray('typed', 'array-2', [1, 2]));
	}

	public function testGetValueArrayOnWrongType(): void {
		$config = $this->generateAppConfig();
		$this->expectException(AppConfigTypeConflictException::class);
		$config->getValueArray('typed', 'string');
	}


	/**
	 * @return array
	 * @see testGetValueType
	 *
	 * @see testGetValueMixed
	 */
	public function providerGetValueMixed(): array {
		return [
			// key, value, type
			['mixed', 'mix', IAppConfig::VALUE_MIXED],
			['string', 'value', IAppConfig::VALUE_STRING],
			['int', '42', IAppConfig::VALUE_INT],
			['float', '3.14', IAppConfig::VALUE_FLOAT],
			['bool', '1', IAppConfig::VALUE_BOOL],
			['array', '{"test": 1}', IAppConfig::VALUE_ARRAY],
		];
	}

	/**
	 * @dataProvider providerGetValueMixed
	 *
	 * @param string $key
	 * @param string $value
	 */
	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
	 */
	public function testGetValueType(string $key, string $value, int $type): void {
		$config = $this->generateAppConfig();
		$this->assertSame($type, $config->getValueType('typed', $key));
	}

	public function testGetValueTypeOnUnknownApp(): void {
		$config = $this->generateAppConfig();
		$this->expectException(AppConfigUnknownKeyException::class);
		$config->getValueType('typed-1', 'string');
	}

	public function testGetValueTypeOnNonExistentKey(): void {
		$config = $this->generateAppConfig();
		$this->expectException(AppConfigUnknownKeyException::class);
		$config->getValueType('typed', 'string-2');
	}

	public function testSetValueString(): void {
		$config = $this->generateAppConfig();
		$config->setValueString('feed', 'string', 'value-1');
		$this->assertSame('value-1', $config->getValueString('feed', 'string', ''));
	}

	public function testSetValueStringCache(): void {
		$config = $this->generateAppConfig();
		$config->setValueString('feed', 'string', 'value-1');
		$status = $config->statusCache();
		$this->assertSame('value-1', $status['fastCache']['feed']['string']);
	}

	public function testSetValueStringDatabase(): void {
		$config = $this->generateAppConfig();
		$config->setValueString('feed', 'string', 'value-1');
		$config->clearCache();
		$this->assertSame('value-1', $config->getValueString('feed', 'string', ''));
	}

	public function testSetValueStringIsUpdated(): void {
		$config = $this->generateAppConfig();
		$config->setValueString('feed', 'string', 'value-1');
		$this->assertSame(true, $config->setValueString('feed', 'string', 'value-2'));
	}

	public function testSetValueStringIsNotUpdated(): void {
		$config = $this->generateAppConfig();
		$config->setValueString('feed', 'string', 'value-1');
		$this->assertSame(false, $config->setValueString('feed', 'string', 'value-1'));
	}

	public function testSetValueStringIsUpdatedCache(): void {
		$config = $this->generateAppConfig();
		$config->setValueString('feed', 'string', 'value-1');
		$config->setValueString('feed', 'string', 'value-2');
		$status = $config->statusCache();
		$this->assertSame('value-2', $status['fastCache']['feed']['string']);
	}

	public function testSetValueStringIsUpdatedDatabase(): void {
		$config = $this->generateAppConfig();
		$config->setValueString('feed', 'string', 'value-1');
		$config->setValueString('feed', 'string', 'value-2');
		$config->clearCache();
		$this->assertSame('value-2', $config->getValueString('feed', 'string', ''));
	}

	public function testSetValueInt(): void {
		$config = $this->generateAppConfig();
		$config->setValueInt('feed', 'int', 42);
		$this->assertSame(42, $config->getValueInt('feed', 'int', 0));
	}

	public function testSetValueIntCache(): void {
		$config = $this->generateAppConfig();
		$config->setValueInt('feed', 'int', 42);
		$status = $config->statusCache();
		$this->assertSame('42', $status['fastCache']['feed']['int']);
	}

	public function testSetValueIntDatabase(): void {
		$config = $this->generateAppConfig();
		$config->setValueInt('feed', 'int', 42);
		$config->clearCache();
		$this->assertSame(42, $config->getValueInt('feed', 'int', 0));
	}

	public function testSetValueIntIsUpdated(): void {
		$config = $this->generateAppConfig();
		$config->setValueInt('feed', 'int', 42);
		$this->assertSame(true, $config->setValueInt('feed', 'int', 17));
	}

	public function testSetValueIntIsNotUpdated(): void {
		$config = $this->generateAppConfig();
		$config->setValueInt('feed', 'int', 42);
		$this->assertSame(false, $config->setValueInt('feed', 'int', 42));
	}

	public function testSetValueIntIsUpdatedCache(): void {
		$config = $this->generateAppConfig();
		$config->setValueInt('feed', 'int', 42);
		$config->setValueInt('feed', 'int', 17);
		$status = $config->statusCache();
		$this->assertSame('17', $status['fastCache']['feed']['int']);
	}

	public function testSetValueIntIsUpdatedDatabase(): void {
		$config = $this->generateAppConfig();
		$config->setValueInt('feed', 'int', 42);
		$config->setValueInt('feed', 'int', 17);
		$config->clearCache();
		$this->assertSame(17, $config->getValueInt('feed', 'int', 0));
	}

	public function testSetValueFloat(): void {
		$config = $this->generateAppConfig();
		$config->setValueFloat('feed', 'float', 3.14);
		$this->assertSame(3.14, $config->getValueFloat('feed', 'float', 0));
	}

	public function testSetValueFloatCache(): void {
		$config = $this->generateAppConfig();
		$config->setValueFloat('feed', 'float', 3.14);
		$status = $config->statusCache();
		$this->assertSame('3.14', $status['fastCache']['feed']['float']);
	}

	public function testSetValueFloatDatabase(): void {
		$config = $this->generateAppConfig();
		$config->setValueFloat('feed', 'float', 3.14);
		$config->clearCache();
		$this->assertSame(3.14, $config->getValueFloat('feed', 'float', 0));
	}

	public function testSetValueFloatIsUpdated(): void {
		$config = $this->generateAppConfig();
		$config->setValueFloat('feed', 'float', 3.14);
		$this->assertSame(true, $config->setValueFloat('feed', 'float', 1.23));
	}

	public function testSetValueFloatIsNotUpdated(): void {
		$config = $this->generateAppConfig();
		$config->setValueFloat('feed', 'float', 3.14);
		$this->assertSame(false, $config->setValueFloat('feed', 'float', 3.14));
	}

	public function testSetValueFloatIsUpdatedCache(): void {
		$config = $this->generateAppConfig();
		$config->setValueFloat('feed', 'float', 3.14);
		$config->setValueFloat('feed', 'float', 1.23);
		$status = $config->statusCache();
		$this->assertSame('1.23', $status['fastCache']['feed']['float']);
	}

	public function testSetValueFloatIsUpdatedDatabase(): void {
		$config = $this->generateAppConfig();
		$config->setValueFloat('feed', 'float', 3.14);
		$config->setValueFloat('feed', 'float', 1.23);
		$config->clearCache();
		$this->assertSame(1.23, $config->getValueFloat('feed', 'float', 0));
	}

	public function testSetValueBool(): void {
		$config = $this->generateAppConfig();
		$config->setValueBool('feed', 'bool', true);
		$this->assertSame(true, $config->getValueBool('feed', 'bool', false));
	}

	public function testSetValueBoolCache(): void {
		$config = $this->generateAppConfig();
		$config->setValueBool('feed', 'bool', true);
		$status = $config->statusCache();
		$this->assertSame('1', $status['fastCache']['feed']['bool']);
	}

	public function testSetValueBoolDatabase(): void {
		$config = $this->generateAppConfig();
		$config->setValueBool('feed', 'bool', true);
		$config->clearCache();
		$this->assertSame(true, $config->getValueBool('feed', 'bool', false));
	}

	public function testSetValueBoolIsUpdated(): void {
		$config = $this->generateAppConfig();
		$config->setValueBool('feed', 'bool', true);
		$this->assertSame(true, $config->setValueBool('feed', 'bool', false));
	}

	public function testSetValueBoolIsNotUpdated(): void {
		$config = $this->generateAppConfig();
		$config->setValueBool('feed', 'bool', true);
		$this->assertSame(false, $config->setValueBool('feed', 'bool', true));
	}

	public function testSetValueBoolIsUpdatedCache(): void {
		$config = $this->generateAppConfig();
		$config->setValueBool('feed', 'bool', true);
		$config->setValueBool('feed', 'bool', false);
		$status = $config->statusCache();
		$this->assertSame('0', $status['fastCache']['feed']['bool']);
	}

	public function testSetValueBoolIsUpdatedDatabase(): void {
		$config = $this->generateAppConfig();
		$config->setValueBool('feed', 'bool', true);
		$config->setValueBool('feed', 'bool', false);
		$config->clearCache();
		$this->assertSame(false, $config->getValueBool('feed', 'bool', true));
	}


	public function testSetValueArray(): void {
		$config = $this->generateAppConfig();
		$config->setValueArray('feed', 'array', ['test' => 1]);
		$this->assertSame(['test' => 1], $config->getValueArray('feed', 'array', []));
	}

	public function testSetValueArrayCache(): void {
		$config = $this->generateAppConfig();
		$config->setValueArray('feed', 'array', ['test' => 1]);
		$status = $config->statusCache();
		$this->assertSame('{"test":1}', $status['fastCache']['feed']['array']);
	}

	public function testSetValueArrayDatabase(): void {
		$config = $this->generateAppConfig();
		$config->setValueArray('feed', 'array', ['test' => 1]);
		$config->clearCache();
		$this->assertSame(['test' => 1], $config->getValueArray('feed', 'array', []));
	}

	public function testSetValueArrayIsUpdated(): void {
		$config = $this->generateAppConfig();
		$config->setValueArray('feed', 'array', ['test' => 1]);
		$this->assertSame(true, $config->setValueArray('feed', 'array', ['test' => 2]));
	}

	public function testSetValueArrayIsNotUpdated(): void {
		$config = $this->generateAppConfig();
		$config->setValueArray('feed', 'array', ['test' => 1]);
		$this->assertSame(false, $config->setValueArray('feed', 'array', ['test' => 1]));
	}

	public function testSetValueArrayIsUpdatedCache(): void {
		$config = $this->generateAppConfig();
		$config->setValueArray('feed', 'array', ['test' => 1]);
		$config->setValueArray('feed', 'array', ['test' => 2]);
		$status = $config->statusCache();
		$this->assertSame('{"test":2}', $status['fastCache']['feed']['array']);
	}

	public function testSetValueArrayIsUpdatedDatabase(): void {
		$config = $this->generateAppConfig();
		$config->setValueArray('feed', 'array', ['test' => 1]);
		$config->setValueArray('feed', 'array', ['test' => 2]);
		$config->clearCache();
		$this->assertSame(['test' => 2], $config->getValueArray('feed', 'array', []));
	}

	public function testSetLazyValueString(): void {
		$config = $this->generateAppConfig();
		$config->setValueString('feed', 'string', 'value-1', true);
		$this->assertSame('value-1', $config->getValueString('feed', 'string', '', true));
	}

	public function testSetLazyValueStringCache(): void {
		$config = $this->generateAppConfig();
		$config->setValueString('feed', 'string', 'value-1', true);
		$status = $config->statusCache();
		$this->assertSame('value-1', $status['lazyCache']['feed']['string']);
	}

	public function testSetLazyValueStringDatabase(): void {
		$config = $this->generateAppConfig();
		$config->setValueString('feed', 'string', 'value-1', true);
		$config->clearCache();
		$this->assertSame('value-1', $config->getValueString('feed', 'string', '', true));
	}

	public function testSetLazyValueStringAsNonLazy(): void {
		$config = $this->generateAppConfig();
		$config->setValueString('feed', 'string', 'value-1', true);
		$config->setValueString('feed', 'string', 'value-1', false);
		$this->assertSame('value-1', $config->getValueString('feed', 'string', ''));
	}

	public function testSetNonLazyValueStringAsLazy(): void {
		$config = $this->generateAppConfig();
		$config->setValueString('feed', 'string', 'value-1', false);
		$config->setValueString('feed', 'string', 'value-1', true);
		$this->assertSame('value-1', $config->getValueString('feed', 'string', '', true));
	}

	public function testSetSensitiveValueString(): void {
		$config = $this->generateAppConfig();
		$config->setValueString('feed', 'string', 'value-1', sensitive: true);
		$this->assertSame('value-1', $config->getValueString('feed', 'string', ''));
	}

	public function testSetSensitiveValueStringCache(): void {
		$config = $this->generateAppConfig();
		$config->setValueString('feed', 'string', 'value-1', sensitive: true);
		$status = $config->statusCache();
		$this->assertStringStartsWith(self::invokePrivate(AppConfig::class, 'ENCRYPTION_PREFIX'), $status['fastCache']['feed']['string']);
	}

	public function testSetSensitiveValueStringDatabase(): void {
		$config = $this->generateAppConfig();
		$config->setValueString('feed', 'string', 'value-1', sensitive: true);
		$config->clearCache();
		$this->assertSame('value-1', $config->getValueString('feed', 'string', ''));
	}

	public function testSetNonSensitiveValueStringAsSensitive(): void {
		$config = $this->generateAppConfig();
		$config->setValueString('feed', 'string', 'value-1', sensitive: false);
		$config->setValueString('feed', 'string', 'value-1', sensitive: true);
		$this->assertSame(true, $config->isSensitive('feed', 'string'));

		$this->assertConfigValueNotEquals('feed', 'string', 'value-1');
		$this->assertConfigValueNotEquals('feed', 'string', 'value-2');
	}

	public function testSetSensitiveValueStringAsNonSensitiveStaysSensitive(): void {
		$config = $this->generateAppConfig();
		$config->setValueString('feed', 'string', 'value-1', sensitive: true);
		$config->setValueString('feed', 'string', 'value-2', sensitive: false);
		$this->assertSame(true, $config->isSensitive('feed', 'string'));

		$this->assertConfigValueNotEquals('feed', 'string', 'value-1');
		$this->assertConfigValueNotEquals('feed', 'string', 'value-2');
	}

	public function testSetSensitiveValueStringAsNonSensitiveAreStillUpdated(): void {
		$config = $this->generateAppConfig();
		$config->setValueString('feed', 'string', 'value-1', sensitive: true);
		$config->setValueString('feed', 'string', 'value-2', sensitive: false);
		$this->assertSame('value-2', $config->getValueString('feed', 'string', ''));

		$this->assertConfigValueNotEquals('feed', 'string', 'value-1');
		$this->assertConfigValueNotEquals('feed', 'string', 'value-2');
	}

	public function testSetLazyValueInt(): void {
		$config = $this->generateAppConfig();
		$config->setValueInt('feed', 'int', 42, true);
		$this->assertSame(42, $config->getValueInt('feed', 'int', 0, true));
	}

	public function testSetLazyValueIntCache(): void {
		$config = $this->generateAppConfig();
		$config->setValueInt('feed', 'int', 42, true);
		$status = $config->statusCache();
		$this->assertSame('42', $status['lazyCache']['feed']['int']);
	}

	public function testSetLazyValueIntDatabase(): void {
		$config = $this->generateAppConfig();
		$config->setValueInt('feed', 'int', 42, true);
		$config->clearCache();
		$this->assertSame(42, $config->getValueInt('feed', 'int', 0, true));
	}

	public function testSetLazyValueIntAsNonLazy(): void {
		$config = $this->generateAppConfig();
		$config->setValueInt('feed', 'int', 42, true);
		$config->setValueInt('feed', 'int', 42, false);
		$this->assertSame(42, $config->getValueInt('feed', 'int', 0));
	}

	public function testSetNonLazyValueIntAsLazy(): void {
		$config = $this->generateAppConfig();
		$config->setValueInt('feed', 'int', 42, false);
		$config->setValueInt('feed', 'int', 42, true);
		$this->assertSame(42, $config->getValueInt('feed', 'int', 0, true));
	}

	public function testSetSensitiveValueInt(): void {
		$config = $this->generateAppConfig();
		$config->setValueInt('feed', 'int', 42, sensitive: true);
		$this->assertSame(42, $config->getValueInt('feed', 'int', 0));
	}

	public function testSetSensitiveValueIntCache(): void {
		$config = $this->generateAppConfig();
		$config->setValueInt('feed', 'int', 42, sensitive: true);
		$status = $config->statusCache();
		$this->assertStringStartsWith(self::invokePrivate(AppConfig::class, 'ENCRYPTION_PREFIX'), $status['fastCache']['feed']['int']);
	}

	public function testSetSensitiveValueIntDatabase(): void {
		$config = $this->generateAppConfig();
		$config->setValueInt('feed', 'int', 42, sensitive: true);
		$config->clearCache();
		$this->assertSame(42, $config->getValueInt('feed', 'int', 0));
	}

	public function testSetNonSensitiveValueIntAsSensitive(): void {
		$config = $this->generateAppConfig();
		$config->setValueInt('feed', 'int', 42);
		$config->setValueInt('feed', 'int', 42, sensitive: true);
		$this->assertSame(true, $config->isSensitive('feed', 'int'));
	}

	public function testSetSensitiveValueIntAsNonSensitiveStaysSensitive(): void {
		$config = $this->generateAppConfig();
		$config->setValueInt('feed', 'int', 42, sensitive: true);
		$config->setValueInt('feed', 'int', 17);
		$this->assertSame(true, $config->isSensitive('feed', 'int'));
	}

	public function testSetSensitiveValueIntAsNonSensitiveAreStillUpdated(): void {
		$config = $this->generateAppConfig();
		$config->setValueInt('feed', 'int', 42, sensitive: true);
		$config->setValueInt('feed', 'int', 17);
		$this->assertSame(17, $config->getValueInt('feed', 'int', 0));
	}

	public function testSetLazyValueFloat(): void {
		$config = $this->generateAppConfig();
		$config->setValueFloat('feed', 'float', 3.14, true);
		$this->assertSame(3.14, $config->getValueFloat('feed', 'float', 0, true));
	}

	public function testSetLazyValueFloatCache(): void {
		$config = $this->generateAppConfig();
		$config->setValueFloat('feed', 'float', 3.14, true);
		$status = $config->statusCache();
		$this->assertSame('3.14', $status['lazyCache']['feed']['float']);
	}

	public function testSetLazyValueFloatDatabase(): void {
		$config = $this->generateAppConfig();
		$config->setValueFloat('feed', 'float', 3.14, true);
		$config->clearCache();
		$this->assertSame(3.14, $config->getValueFloat('feed', 'float', 0, true));
	}

	public function testSetLazyValueFloatAsNonLazy(): void {
		$config = $this->generateAppConfig();
		$config->setValueFloat('feed', 'float', 3.14, true);
		$config->setValueFloat('feed', 'float', 3.14, false);
		$this->assertSame(3.14, $config->getValueFloat('feed', 'float', 0));
	}

	public function testSetNonLazyValueFloatAsLazy(): void {
		$config = $this->generateAppConfig();
		$config->setValueFloat('feed', 'float', 3.14, false);
		$config->setValueFloat('feed', 'float', 3.14, true);
		$this->assertSame(3.14, $config->getValueFloat('feed', 'float', 0, true));
	}

	public function testSetSensitiveValueFloat(): void {
		$config = $this->generateAppConfig();
		$config->setValueFloat('feed', 'float', 3.14, sensitive: true);
		$this->assertSame(3.14, $config->getValueFloat('feed', 'float', 0));
	}

	public function testSetSensitiveValueFloatCache(): void {
		$config = $this->generateAppConfig();
		$config->setValueFloat('feed', 'float', 3.14, sensitive: true);
		$status = $config->statusCache();
		$this->assertStringStartsWith(self::invokePrivate(AppConfig::class, 'ENCRYPTION_PREFIX'), $status['fastCache']['feed']['float']);
	}

	public function testSetSensitiveValueFloatDatabase(): void {
		$config = $this->generateAppConfig();
		$config->setValueFloat('feed', 'float', 3.14, sensitive: true);
		$config->clearCache();
		$this->assertSame(3.14, $config->getValueFloat('feed', 'float', 0));
	}

	public function testSetNonSensitiveValueFloatAsSensitive(): void {
		$config = $this->generateAppConfig();
		$config->setValueFloat('feed', 'float', 3.14);
		$config->setValueFloat('feed', 'float', 3.14, sensitive: true);
		$this->assertSame(true, $config->isSensitive('feed', 'float'));
	}

	public function testSetSensitiveValueFloatAsNonSensitiveStaysSensitive(): void {
		$config = $this->generateAppConfig();
		$config->setValueFloat('feed', 'float', 3.14, sensitive: true);
		$config->setValueFloat('feed', 'float', 1.23);
		$this->assertSame(true, $config->isSensitive('feed', 'float'));
	}

	public function testSetSensitiveValueFloatAsNonSensitiveAreStillUpdated(): void {
		$config = $this->generateAppConfig();
		$config->setValueFloat('feed', 'float', 3.14, sensitive: true);
		$config->setValueFloat('feed', 'float', 1.23);
		$this->assertSame(1.23, $config->getValueFloat('feed', 'float', 0));
	}

	public function testSetLazyValueBool(): void {
		$config = $this->generateAppConfig();
		$config->setValueBool('feed', 'bool', true, true);
		$this->assertSame(true, $config->getValueBool('feed', 'bool', false, true));
	}

	public function testSetLazyValueBoolCache(): void {
		$config = $this->generateAppConfig();
		$config->setValueBool('feed', 'bool', true, true);
		$status = $config->statusCache();
		$this->assertSame('1', $status['lazyCache']['feed']['bool']);
	}

	public function testSetLazyValueBoolDatabase(): void {
		$config = $this->generateAppConfig();
		$config->setValueBool('feed', 'bool', true, true);
		$config->clearCache();
		$this->assertSame(true, $config->getValueBool('feed', 'bool', false, true));
	}

	public function testSetLazyValueBoolAsNonLazy(): void {
		$config = $this->generateAppConfig();
		$config->setValueBool('feed', 'bool', true, true);
		$config->setValueBool('feed', 'bool', true, false);
		$this->assertSame(true, $config->getValueBool('feed', 'bool', false));
	}

	public function testSetNonLazyValueBoolAsLazy(): void {
		$config = $this->generateAppConfig();
		$config->setValueBool('feed', 'bool', true, false);
		$config->setValueBool('feed', 'bool', true, true);
		$this->assertSame(true, $config->getValueBool('feed', 'bool', false, true));
	}

	public function testSetLazyValueArray(): void {
		$config = $this->generateAppConfig();
		$config->setValueArray('feed', 'array', ['test' => 1], true);
		$this->assertSame(['test' => 1], $config->getValueArray('feed', 'array', [], true));
	}

	public function testSetLazyValueArrayCache(): void {
		$config = $this->generateAppConfig();
		$config->setValueArray('feed', 'array', ['test' => 1], true);
		$status = $config->statusCache();
		$this->assertSame('{"test":1}', $status['lazyCache']['feed']['array']);
	}

	public function testSetLazyValueArrayDatabase(): void {
		$config = $this->generateAppConfig();
		$config->setValueArray('feed', 'array', ['test' => 1], true);
		$config->clearCache();
		$this->assertSame(['test' => 1], $config->getValueArray('feed', 'array', [], true));
	}

	public function testSetLazyValueArrayAsNonLazy(): void {
		$config = $this->generateAppConfig();
		$config->setValueArray('feed', 'array', ['test' => 1], true);
		$config->setValueArray('feed', 'array', ['test' => 1], false);
		$this->assertSame(['test' => 1], $config->getValueArray('feed', 'array', []));
	}

	public function testSetNonLazyValueArrayAsLazy(): void {
		$config = $this->generateAppConfig();
		$config->setValueArray('feed', 'array', ['test' => 1], false);
		$config->setValueArray('feed', 'array', ['test' => 1], true);
		$this->assertSame(['test' => 1], $config->getValueArray('feed', 'array', [], true));
	}


	public function testSetSensitiveValueArray(): void {
		$config = $this->generateAppConfig();
		$config->setValueArray('feed', 'array', ['test' => 1], sensitive: true);
		$this->assertEqualsCanonicalizing(['test' => 1], $config->getValueArray('feed', 'array', []));
	}

	public function testSetSensitiveValueArrayCache(): void {
		$config = $this->generateAppConfig();
		$config->setValueArray('feed', 'array', ['test' => 1], sensitive: true);
		$status = $config->statusCache();
		$this->assertStringStartsWith(self::invokePrivate(AppConfig::class, 'ENCRYPTION_PREFIX'), $status['fastCache']['feed']['array']);
	}

	public function testSetSensitiveValueArrayDatabase(): void {
		$config = $this->generateAppConfig();
		$config->setValueArray('feed', 'array', ['test' => 1], sensitive: true);
		$config->clearCache();
		$this->assertEqualsCanonicalizing(['test' => 1], $config->getValueArray('feed', 'array', []));
	}

	public function testSetNonSensitiveValueArrayAsSensitive(): void {
		$config = $this->generateAppConfig();
		$config->setValueArray('feed', 'array', ['test' => 1]);
		$config->setValueArray('feed', 'array', ['test' => 1], sensitive: true);
		$this->assertSame(true, $config->isSensitive('feed', 'array'));
	}

	public function testSetSensitiveValueArrayAsNonSensitiveStaysSensitive(): void {
		$config = $this->generateAppConfig();
		$config->setValueArray('feed', 'array', ['test' => 1], sensitive: true);
		$config->setValueArray('feed', 'array', ['test' => 2]);
		$this->assertSame(true, $config->isSensitive('feed', 'array'));
	}

	public function testSetSensitiveValueArrayAsNonSensitiveAreStillUpdated(): void {
		$config = $this->generateAppConfig();
		$config->setValueArray('feed', 'array', ['test' => 1], sensitive: true);
		$config->setValueArray('feed', 'array', ['test' => 2]);
		$this->assertEqualsCanonicalizing(['test' => 2], $config->getValueArray('feed', 'array', []));
	}

	public function testUpdateNotSensitiveToSensitive(): void {
		$config = $this->generateAppConfig();
		$config->updateSensitive('non-sensitive-app', 'lazy-key', true);
		$this->assertSame(true, $config->isSensitive('non-sensitive-app', 'lazy-key', true));
	}

	public function testUpdateSensitiveToNotSensitive(): void {
		$config = $this->generateAppConfig();
		$config->updateSensitive('sensitive-app', 'lazy-key', false);
		$this->assertSame(false, $config->isSensitive('sensitive-app', 'lazy-key', true));
	}

	public function testUpdateSensitiveToSensitiveReturnsFalse(): void {
		$config = $this->generateAppConfig();
		$this->assertSame(false, $config->updateSensitive('sensitive-app', 'lazy-key', true));
	}

	public function testUpdateNotSensitiveToNotSensitiveReturnsFalse(): void {
		$config = $this->generateAppConfig();
		$this->assertSame(false, $config->updateSensitive('non-sensitive-app', 'lazy-key', false));
	}

	public function testUpdateSensitiveOnUnknownKeyReturnsFalse(): void {
		$config = $this->generateAppConfig();
		$this->assertSame(false, $config->updateSensitive('non-sensitive-app', 'unknown-key', true));
	}

	public function testUpdateNotLazyToLazy(): void {
		$config = $this->generateAppConfig();
		$config->updateLazy('non-sensitive-app', 'non-lazy-key', true);
		$this->assertSame(true, $config->isLazy('non-sensitive-app', 'non-lazy-key'));
	}

	public function testUpdateLazyToNotLazy(): void {
		$config = $this->generateAppConfig();
		$config->updateLazy('non-sensitive-app', 'lazy-key', false);
		$this->assertSame(false, $config->isLazy('non-sensitive-app', 'lazy-key'));
	}

	public function testUpdateLazyToLazyReturnsFalse(): void {
		$config = $this->generateAppConfig();
		$this->assertSame(false, $config->updateLazy('non-sensitive-app', 'lazy-key', true));
	}

	public function testUpdateNotLazyToNotLazyReturnsFalse(): void {
		$config = $this->generateAppConfig();
		$this->assertSame(false, $config->updateLazy('non-sensitive-app', 'non-lazy-key', false));
	}

	public function testUpdateLazyOnUnknownKeyReturnsFalse(): void {
		$config = $this->generateAppConfig();
		$this->assertSame(false, $config->updateLazy('non-sensitive-app', 'unknown-key', true));
	}

	public function testGetDetails(): void {
		$config = $this->generateAppConfig();
		$this->assertEquals(
			[
				'app' => 'non-sensitive-app',
				'key' => 'lazy-key',
				'value' => 'value',
				'type' => 4,
				'lazy' => true,
				'typeString' => 'string',
				'sensitive' => false,
			],
			$config->getDetails('non-sensitive-app', 'lazy-key')
		);
	}

	public function testGetDetailsSensitive(): void {
		$config = $this->generateAppConfig();
		$this->assertEquals(
			[
				'app' => 'sensitive-app',
				'key' => 'lazy-key',
				'value' => 'value',
				'type' => 4,
				'lazy' => true,
				'typeString' => 'string',
				'sensitive' => true,
			],
			$config->getDetails('sensitive-app', 'lazy-key')
		);
	}

	public function testGetDetailsInt(): void {
		$config = $this->generateAppConfig();
		$this->assertEquals(
			[
				'app' => 'typed',
				'key' => 'int',
				'value' => '42',
				'type' => 8,
				'lazy' => false,
				'typeString' => 'integer',
				'sensitive' => false
			],
			$config->getDetails('typed', 'int')
		);
	}

	public function testGetDetailsFloat(): void {
		$config = $this->generateAppConfig();
		$this->assertEquals(
			[
				'app' => 'typed',
				'key' => 'float',
				'value' => '3.14',
				'type' => 16,
				'lazy' => false,
				'typeString' => 'float',
				'sensitive' => false
			],
			$config->getDetails('typed', 'float')
		);
	}

	public function testGetDetailsBool(): void {
		$config = $this->generateAppConfig();
		$this->assertEquals(
			[
				'app' => 'typed',
				'key' => 'bool',
				'value' => '1',
				'type' => 32,
				'lazy' => false,
				'typeString' => 'boolean',
				'sensitive' => false
			],
			$config->getDetails('typed', 'bool')
		);
	}

	public function testGetDetailsArray(): void {
		$config = $this->generateAppConfig();
		$this->assertEquals(
			[
				'app' => 'typed',
				'key' => 'array',
				'value' => '{"test": 1}',
				'type' => 64,
				'lazy' => false,
				'typeString' => 'array',
				'sensitive' => false
			],
			$config->getDetails('typed', 'array')
		);
	}

	public function testDeleteKey(): void {
		$config = $this->generateAppConfig();
		$config->deleteKey('anotherapp', 'key');
		$this->assertSame('default', $config->getValueString('anotherapp', 'key', 'default'));
	}

	public function testDeleteKeyCache(): void {
		$config = $this->generateAppConfig();
		$config->deleteKey('anotherapp', 'key');
		$status = $config->statusCache();
		$this->assertEqualsCanonicalizing(['enabled' => 'false'], $status['fastCache']['anotherapp']);
	}

	public function testDeleteKeyDatabase(): void {
		$config = $this->generateAppConfig();
		$config->deleteKey('anotherapp', 'key');
		$config->clearCache();
		$this->assertSame('default', $config->getValueString('anotherapp', 'key', 'default'));
	}

	public function testDeleteApp(): void {
		$config = $this->generateAppConfig();
		$config->deleteApp('anotherapp');
		$this->assertSame('default', $config->getValueString('anotherapp', 'key', 'default'));
		$this->assertSame('default', $config->getValueString('anotherapp', 'enabled', 'default'));
	}

	public function testDeleteAppCache(): void {
		$config = $this->generateAppConfig();
		$status = $config->statusCache();
		$this->assertSame(true, isset($status['fastCache']['anotherapp']));
		$config->deleteApp('anotherapp');
		$status = $config->statusCache();
		$this->assertSame(false, isset($status['fastCache']['anotherapp']));
	}

	public function testDeleteAppDatabase(): void {
		$config = $this->generateAppConfig();
		$config->deleteApp('anotherapp');
		$config->clearCache();
		$this->assertSame('default', $config->getValueString('anotherapp', 'key', 'default'));
		$this->assertSame('default', $config->getValueString('anotherapp', 'enabled', 'default'));
	}

	public function testClearCache(): void {
		$config = $this->generateAppConfig();
		$config->setValueString('feed', 'string', '123454');
		$config->clearCache();
		$status = $config->statusCache();
		$this->assertSame([], $status['fastCache']);
	}

	public function testSensitiveValuesAreEncrypted(): void {
		$key = self::getUniqueID('secret');

		$appConfig = $this->generateAppConfig();
		$secret = md5((string)time());
		$appConfig->setValueString('testapp', $key, $secret, sensitive: true);

		$this->assertConfigValueNotEquals('testapp', $key, $secret);

		// Can get in same run
		$actualSecret = $appConfig->getValueString('testapp', $key);
		$this->assertEquals($secret, $actualSecret);

		// Can get freshly decrypted from DB
		$newAppConfig = $this->generateAppConfig();
		$actualSecret = $newAppConfig->getValueString('testapp', $key);
		$this->assertEquals($secret, $actualSecret);
	}

	public function testMigratingNonSensitiveValueToSensitiveWithSetValue(): void {
		$key = self::getUniqueID('secret');
		$appConfig = $this->generateAppConfig();
		$secret = sha1((string)time());

		// Unencrypted
		$appConfig->setValueString('testapp', $key, $secret);
		$this->assertConfigKey('testapp', $key, $secret);

		// Can get freshly decrypted from DB
		$newAppConfig = $this->generateAppConfig();
		$actualSecret = $newAppConfig->getValueString('testapp', $key);
		$this->assertEquals($secret, $actualSecret);

		// Encrypting on change
		$appConfig->setValueString('testapp', $key, $secret, sensitive: true);
		$this->assertConfigValueNotEquals('testapp', $key, $secret);

		// Can get in same run
		$actualSecret = $appConfig->getValueString('testapp', $key);
		$this->assertEquals($secret, $actualSecret);

		// Can get freshly decrypted from DB
		$newAppConfig = $this->generateAppConfig();
		$actualSecret = $newAppConfig->getValueString('testapp', $key);
		$this->assertEquals($secret, $actualSecret);
	}

	public function testUpdateSensitiveValueToNonSensitiveWithUpdateSensitive(): void {
		$key = self::getUniqueID('secret');
		$appConfig = $this->generateAppConfig();
		$secret = sha1((string)time());

		// Encrypted
		$appConfig->setValueString('testapp', $key, $secret, sensitive: true);
		$this->assertConfigValueNotEquals('testapp', $key, $secret);

		// Migrate to non-sensitive / non-encrypted
		$appConfig->updateSensitive('testapp', $key, false);
		$this->assertConfigKey('testapp', $key, $secret);
	}

	public function testUpdateNonSensitiveValueToSensitiveWithUpdateSensitive(): void {
		$key = self::getUniqueID('secret');
		$appConfig = $this->generateAppConfig();
		$secret = sha1((string)time());

		// Unencrypted
		$appConfig->setValueString('testapp', $key, $secret);
		$this->assertConfigKey('testapp', $key, $secret);

		// Migrate to sensitive / encrypted
		$appConfig->updateSensitive('testapp', $key, true);
		$this->assertConfigValueNotEquals('testapp', $key, $secret);
	}

	protected function loadConfigValueFromDatabase(string $app, string $key): string|false {
		$sql = $this->connection->getQueryBuilder();
		$sql->select('configvalue')
			->from('appconfig')
			->where($sql->expr()->eq('appid', $sql->createParameter('appid')))
			->andWhere($sql->expr()->eq('configkey', $sql->createParameter('configkey')))
			->setParameter('appid', $app)
			->setParameter('configkey', $key);
		$query = $sql->executeQuery();
		$actual = $query->fetchOne();
		$query->closeCursor();

		return $actual;
	}

	protected function assertConfigKey(string $app, string $key, string|false $expected): void {
		$this->assertEquals($expected, $this->loadConfigValueFromDatabase($app, $key));
	}

	protected function assertConfigValueNotEquals(string $app, string $key, string|false $expected): void {
		$this->assertNotEquals($expected, $this->loadConfigValueFromDatabase($app, $key));
	}
}