1
0

AppConfigTest.php 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2024, Maxence Lange <maxence@artificial-owl.com>
  5. *
  6. * @author Maxence Lange <maxence@artificial-owl.com>
  7. *
  8. * @license AGPL-3.0 or later
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace Test;
  24. use InvalidArgumentException;
  25. use OC\AppConfig;
  26. use OCP\Exceptions\AppConfigTypeConflictException;
  27. use OCP\Exceptions\AppConfigUnknownKeyException;
  28. use OCP\IAppConfig;
  29. use OCP\IDBConnection;
  30. use Psr\Log\LoggerInterface;
  31. /**
  32. * Class AppConfigTest
  33. *
  34. * @group DB
  35. *
  36. * @package Test
  37. */
  38. class AppConfigTest extends TestCase {
  39. protected IAppConfig $appConfig;
  40. protected IDBConnection $connection;
  41. private LoggerInterface $logger;
  42. private array $originalConfig;
  43. /**
  44. * @var array<string, array<array<string, string, int, bool, bool>>>
  45. * [appId => [configKey, configValue, valueType, lazy, sensitive]]
  46. */
  47. private static array $baseStruct =
  48. [
  49. 'testapp' => [
  50. ['enabled', 'true'],
  51. ['installed_version', '1.2.3'],
  52. ['depends_on', 'someapp'],
  53. ['deletethis', 'deletethis'],
  54. ['key', 'value']
  55. ],
  56. 'someapp' => [
  57. ['key', 'value'],
  58. ['otherkey', 'othervalue']
  59. ],
  60. '123456' => [
  61. ['enabled', 'true'],
  62. ['key', 'value']
  63. ],
  64. 'anotherapp' => [
  65. ['enabled', 'false'],
  66. ['key', 'value']
  67. ],
  68. 'non-sensitive-app' => [
  69. ['lazy-key', 'value', IAppConfig::VALUE_STRING, true, false],
  70. ['non-lazy-key', 'value', IAppConfig::VALUE_STRING, false, false],
  71. ],
  72. 'sensitive-app' => [
  73. ['lazy-key', 'value', IAppConfig::VALUE_STRING, true, true],
  74. ['non-lazy-key', 'value', IAppConfig::VALUE_STRING, false, true],
  75. ],
  76. 'only-lazy' => [
  77. ['lazy-key', 'value', IAppConfig::VALUE_STRING, true]
  78. ],
  79. 'typed' => [
  80. ['mixed', 'mix', IAppConfig::VALUE_MIXED],
  81. ['string', 'value', IAppConfig::VALUE_STRING],
  82. ['int', '42', IAppConfig::VALUE_INT],
  83. ['float', '3.14', IAppConfig::VALUE_FLOAT],
  84. ['bool', '1', IAppConfig::VALUE_BOOL],
  85. ['array', '{"test": 1}', IAppConfig::VALUE_ARRAY],
  86. ],
  87. 'prefix-app' => [
  88. ['key1', 'value'],
  89. ['prefix1', 'value'],
  90. ['prefix-2', 'value'],
  91. ['key-2', 'value'],
  92. ]
  93. ];
  94. protected function setUp(): void {
  95. parent::setUp();
  96. $this->connection = \OC::$server->get(IDBConnection::class);
  97. $this->logger = \OC::$server->get(LoggerInterface::class);
  98. // storing current config and emptying the data table
  99. $sql = $this->connection->getQueryBuilder();
  100. $sql->select('*')
  101. ->from('appconfig');
  102. $result = $sql->executeQuery();
  103. $this->originalConfig = $result->fetchAll();
  104. $result->closeCursor();
  105. $sql = $this->connection->getQueryBuilder();
  106. $sql->delete('appconfig');
  107. $sql->executeStatement();
  108. // $this->overwriteService(AppConfig::class, new \OC\AppConfig($this->connection, $this->logger));
  109. $sql = $this->connection->getQueryBuilder();
  110. $sql->insert('appconfig')
  111. ->values(
  112. [
  113. 'appid' => $sql->createParameter('appid'),
  114. 'configkey' => $sql->createParameter('configkey'),
  115. 'configvalue' => $sql->createParameter('configvalue'),
  116. 'type' => $sql->createParameter('type'),
  117. 'lazy' => $sql->createParameter('lazy')
  118. ]
  119. );
  120. foreach (self::$baseStruct as $appId => $appData) {
  121. foreach ($appData as $row) {
  122. $type = $row[2] ?? IAppConfig::VALUE_MIXED;
  123. if (($row[4] ?? false) === true) {
  124. $type = $type | IAppConfig::VALUE_SENSITIVE;
  125. }
  126. $sql->setParameters(
  127. [
  128. 'appid' => $appId,
  129. 'configkey' => $row[0],
  130. 'configvalue' => $row[1],
  131. 'type' => $type,
  132. 'lazy' => (($row[3] ?? false) === true) ? 1 : 0
  133. ]
  134. )->executeStatement();
  135. }
  136. }
  137. }
  138. protected function tearDown(): void {
  139. $sql = $this->connection->getQueryBuilder();
  140. $sql->delete('appconfig');
  141. $sql->executeStatement();
  142. $sql = $this->connection->getQueryBuilder();
  143. $sql->insert('appconfig')
  144. ->values(
  145. [
  146. 'appid' => $sql->createParameter('appid'),
  147. 'configkey' => $sql->createParameter('configkey'),
  148. 'configvalue' => $sql->createParameter('configvalue'),
  149. 'lazy' => $sql->createParameter('lazy'),
  150. 'type' => $sql->createParameter('type'),
  151. ]
  152. );
  153. foreach ($this->originalConfig as $configs) {
  154. $sql->setParameter('appid', $configs['appid'])
  155. ->setParameter('configkey', $configs['configkey'])
  156. ->setParameter('configvalue', $configs['configvalue'])
  157. ->setParameter('lazy', ($configs['lazy'] === '1') ? '1' : '0')
  158. ->setParameter('type', $configs['type']);
  159. $sql->executeStatement();
  160. }
  161. // $this->restoreService(AppConfig::class);
  162. parent::tearDown();
  163. }
  164. /**
  165. * @param bool $preLoading TRUE will preload the 'fast' cache, which is the normal behavior of usual
  166. * IAppConfig
  167. *
  168. * @return IAppConfig
  169. */
  170. private function generateAppConfig(bool $preLoading = true): IAppConfig {
  171. /** @var AppConfig $config */
  172. $config = new \OC\AppConfig($this->connection, $this->logger);
  173. $msg = ' generateAppConfig() failed to confirm cache status';
  174. // confirm cache status
  175. $status = $config->statusCache();
  176. $this->assertSame(false, $status['fastLoaded'], $msg);
  177. $this->assertSame(false, $status['lazyLoaded'], $msg);
  178. $this->assertSame([], $status['fastCache'], $msg);
  179. $this->assertSame([], $status['lazyCache'], $msg);
  180. if ($preLoading) {
  181. // simple way to initiate the load of non-lazy config values in cache
  182. $config->getValueString('core', 'preload', '');
  183. // confirm cache status
  184. $status = $config->statusCache();
  185. $this->assertSame(true, $status['fastLoaded'], $msg);
  186. $this->assertSame(false, $status['lazyLoaded'], $msg);
  187. $apps = array_values(array_diff(array_keys(self::$baseStruct), ['only-lazy']));
  188. $this->assertEqualsCanonicalizing($apps, array_keys($status['fastCache']), $msg);
  189. $this->assertSame([], array_keys($status['lazyCache']), $msg);
  190. }
  191. return $config;
  192. }
  193. public function testGetApps(): void {
  194. $config = $this->generateAppConfig(false);
  195. $this->assertEqualsCanonicalizing(array_keys(self::$baseStruct), $config->getApps());
  196. }
  197. /**
  198. * returns list of app and their keys
  199. *
  200. * @return array<string, string[]> ['appId' => ['key1', 'key2', ]]
  201. * @see testGetKeys
  202. */
  203. public function providerGetAppKeys(): array {
  204. $appKeys = [];
  205. foreach (self::$baseStruct as $appId => $appData) {
  206. $keys = [];
  207. foreach ($appData as $row) {
  208. $keys[] = $row[0];
  209. }
  210. $appKeys[] = [(string)$appId, $keys];
  211. }
  212. return $appKeys;
  213. }
  214. /**
  215. * returns list of config keys
  216. *
  217. * @return array<string, string, string, int, bool, bool> [appId, key, value, type, lazy, sensitive]
  218. * @see testIsSensitive
  219. * @see testIsLazy
  220. * @see testGetKeys
  221. */
  222. public function providerGetKeys(): array {
  223. $appKeys = [];
  224. foreach (self::$baseStruct as $appId => $appData) {
  225. foreach ($appData as $row) {
  226. $appKeys[] = [
  227. (string)$appId, $row[0], $row[1], $row[2] ?? IAppConfig::VALUE_MIXED, $row[3] ?? false,
  228. $row[4] ?? false
  229. ];
  230. }
  231. }
  232. return $appKeys;
  233. }
  234. /**
  235. * @dataProvider providerGetAppKeys
  236. *
  237. * @param string $appId
  238. * @param array $expectedKeys
  239. */
  240. public function testGetKeys(string $appId, array $expectedKeys): void {
  241. $config = $this->generateAppConfig();
  242. $this->assertEqualsCanonicalizing($expectedKeys, $config->getKeys($appId));
  243. }
  244. public function testGetKeysOnUnknownAppShouldReturnsEmptyArray(): void {
  245. $config = $this->generateAppConfig();
  246. $this->assertEqualsCanonicalizing([], $config->getKeys('unknown-app'));
  247. }
  248. /**
  249. * @dataProvider providerGetKeys
  250. *
  251. * @param string $appId
  252. * @param string $configKey
  253. * @param string $value
  254. * @param bool $lazy
  255. */
  256. public function testHasKey(string $appId, string $configKey, string $value, int $type, bool $lazy): void {
  257. $config = $this->generateAppConfig();
  258. $this->assertEquals(true, $config->hasKey($appId, $configKey, $lazy));
  259. }
  260. public function testHasKeyOnNonExistentKeyReturnsFalse(): void {
  261. $config = $this->generateAppConfig();
  262. $this->assertEquals(false, $config->hasKey(array_keys(self::$baseStruct)[0], 'inexistant-key'));
  263. }
  264. public function testHasKeyOnUnknownAppReturnsFalse(): void {
  265. $config = $this->generateAppConfig();
  266. $this->assertEquals(false, $config->hasKey('inexistant-app', 'inexistant-key'));
  267. }
  268. public function testHasKeyOnMistypedAsLazyReturnsFalse(): void {
  269. $config = $this->generateAppConfig();
  270. $this->assertSame(false, $config->hasKey('non-sensitive-app', 'non-lazy-key', true));
  271. }
  272. public function testHasKeyOnMistypeAsNonLazyReturnsFalse(): void {
  273. $config = $this->generateAppConfig();
  274. $this->assertSame(false, $config->hasKey('non-sensitive-app', 'lazy-key', false));
  275. }
  276. public function testHasKeyOnMistypeAsNonLazyReturnsTrueWithLazyArgumentIsNull(): void {
  277. $config = $this->generateAppConfig();
  278. $this->assertSame(true, $config->hasKey('non-sensitive-app', 'lazy-key', null));
  279. }
  280. /**
  281. * @dataProvider providerGetKeys
  282. */
  283. public function testIsSensitive(
  284. string $appId, string $configKey, string $configValue, int $type, bool $lazy, bool $sensitive
  285. ): void {
  286. $config = $this->generateAppConfig();
  287. $this->assertEquals($sensitive, $config->isSensitive($appId, $configKey, $lazy));
  288. }
  289. public function testIsSensitiveOnNonExistentKeyThrowsException(): void {
  290. $config = $this->generateAppConfig();
  291. $this->expectException(AppConfigUnknownKeyException::class);
  292. $config->isSensitive(array_keys(self::$baseStruct)[0], 'inexistant-key');
  293. }
  294. public function testIsSensitiveOnUnknownAppThrowsException(): void {
  295. $config = $this->generateAppConfig();
  296. $this->expectException(AppConfigUnknownKeyException::class);
  297. $config->isSensitive('unknown-app', 'inexistant-key');
  298. }
  299. public function testIsSensitiveOnSensitiveMistypedAsLazy(): void {
  300. $config = $this->generateAppConfig();
  301. $this->assertSame(true, $config->isSensitive('sensitive-app', 'non-lazy-key', true));
  302. }
  303. public function testIsSensitiveOnNonSensitiveMistypedAsLazy(): void {
  304. $config = $this->generateAppConfig();
  305. $this->assertSame(false, $config->isSensitive('non-sensitive-app', 'non-lazy-key', true));
  306. }
  307. public function testIsSensitiveOnSensitiveMistypedAsNonLazyThrowsException(): void {
  308. $config = $this->generateAppConfig();
  309. $this->expectException(AppConfigUnknownKeyException::class);
  310. $config->isSensitive('sensitive-app', 'lazy-key', false);
  311. }
  312. public function testIsSensitiveOnNonSensitiveMistypedAsNonLazyThrowsException(): void {
  313. $config = $this->generateAppConfig();
  314. $this->expectException(AppConfigUnknownKeyException::class);
  315. $config->isSensitive('non-sensitive-app', 'lazy-key', false);
  316. }
  317. /**
  318. * @dataProvider providerGetKeys
  319. */
  320. public function testIsLazy(string $appId, string $configKey, string $configValue, int $type, bool $lazy
  321. ): void {
  322. $config = $this->generateAppConfig();
  323. $this->assertEquals($lazy, $config->isLazy($appId, $configKey));
  324. }
  325. public function testIsLazyOnNonExistentKeyThrowsException(): void {
  326. $config = $this->generateAppConfig();
  327. $this->expectException(AppConfigUnknownKeyException::class);
  328. $config->isLazy(array_keys(self::$baseStruct)[0], 'inexistant-key');
  329. }
  330. public function testIsLazyOnUnknownAppThrowsException(): void {
  331. $config = $this->generateAppConfig();
  332. $this->expectException(AppConfigUnknownKeyException::class);
  333. $config->isLazy('unknown-app', 'inexistant-key');
  334. }
  335. public function testGetAllValuesWithEmptyApp(): void {
  336. $config = $this->generateAppConfig();
  337. $this->expectException(InvalidArgumentException::class);
  338. $config->getAllValues('');
  339. }
  340. /**
  341. * @dataProvider providerGetAppKeys
  342. *
  343. * @param string $appId
  344. * @param array $keys
  345. */
  346. public function testGetAllValuesWithEmptyKey(string $appId, array $keys): void {
  347. $config = $this->generateAppConfig();
  348. $this->assertEqualsCanonicalizing($keys, array_keys($config->getAllValues($appId, '')));
  349. }
  350. public function testGetAllValuesWithPrefix(): void {
  351. $config = $this->generateAppConfig();
  352. $this->assertEqualsCanonicalizing(['prefix1', 'prefix-2'], array_keys($config->getAllValues('prefix-app', 'prefix')));
  353. }
  354. public function testSearchValues(): void {
  355. $config = $this->generateAppConfig();
  356. $this->assertEqualsCanonicalizing(['testapp' => 'true', '123456' => 'true', 'anotherapp' => 'false'], $config->searchValues('enabled'));
  357. }
  358. public function testGetValueString(): void {
  359. $config = $this->generateAppConfig();
  360. $this->assertSame('value', $config->getValueString('typed', 'string', ''));
  361. }
  362. public function testGetValueStringOnUnknownAppReturnsDefault(): void {
  363. $config = $this->generateAppConfig();
  364. $this->assertSame('default-1', $config->getValueString('typed-1', 'string', 'default-1'));
  365. }
  366. public function testGetValueStringOnNonExistentKeyReturnsDefault(): void {
  367. $config = $this->generateAppConfig();
  368. $this->assertSame('default-2', $config->getValueString('typed', 'string-2', 'default-2'));
  369. }
  370. public function testGetValueStringOnWrongType(): void {
  371. $config = $this->generateAppConfig();
  372. $this->expectException(AppConfigTypeConflictException::class);
  373. $config->getValueString('typed', 'int');
  374. }
  375. public function testGetNonLazyValueStringAsLazy(): void {
  376. $config = $this->generateAppConfig();
  377. $this->assertSame('value', $config->getValueString('non-sensitive-app', 'non-lazy-key', 'default', lazy: true));
  378. }
  379. public function testGetValueInt() {
  380. $config = $this->generateAppConfig();
  381. $this->assertSame(42, $config->getValueInt('typed', 'int', 0));
  382. }
  383. public function testGetValueIntOnUnknownAppReturnsDefault(): void {
  384. $config = $this->generateAppConfig();
  385. $this->assertSame(1, $config->getValueInt('typed-1', 'int', 1));
  386. }
  387. public function testGetValueIntOnNonExistentKeyReturnsDefault() {
  388. $config = $this->generateAppConfig();
  389. $this->assertSame(2, $config->getValueInt('typed', 'int-2', 2));
  390. }
  391. public function testGetValueIntOnWrongType(): void {
  392. $config = $this->generateAppConfig();
  393. $this->expectException(AppConfigTypeConflictException::class);
  394. $config->getValueInt('typed', 'float');
  395. }
  396. public function testGetValueFloat() {
  397. $config = $this->generateAppConfig();
  398. $this->assertSame(3.14, $config->getValueFloat('typed', 'float', 0));
  399. }
  400. public function testGetValueFloatOnNonUnknownAppReturnsDefault(): void {
  401. $config = $this->generateAppConfig();
  402. $this->assertSame(1.11, $config->getValueFloat('typed-1', 'float', 1.11));
  403. }
  404. public function testGetValueFloatOnNonExistentKeyReturnsDefault() {
  405. $config = $this->generateAppConfig();
  406. $this->assertSame(2.22, $config->getValueFloat('typed', 'float-2', 2.22));
  407. }
  408. public function testGetValueFloatOnWrongType(): void {
  409. $config = $this->generateAppConfig();
  410. $this->expectException(AppConfigTypeConflictException::class);
  411. $config->getValueFloat('typed', 'bool');
  412. }
  413. public function testGetValueBool(): void {
  414. $config = $this->generateAppConfig();
  415. $this->assertSame(true, $config->getValueBool('typed', 'bool'));
  416. }
  417. public function testGetValueBoolOnUnknownAppReturnsDefault(): void {
  418. $config = $this->generateAppConfig();
  419. $this->assertSame(false, $config->getValueBool('typed-1', 'bool', false));
  420. }
  421. public function testGetValueBoolOnNonExistentKeyReturnsDefault(): void {
  422. $config = $this->generateAppConfig();
  423. $this->assertSame(false, $config->getValueBool('typed', 'bool-2'));
  424. }
  425. public function testGetValueBoolOnWrongType(): void {
  426. $config = $this->generateAppConfig();
  427. $this->expectException(AppConfigTypeConflictException::class);
  428. $config->getValueBool('typed', 'array');
  429. }
  430. public function testGetValueArray(): void {
  431. $config = $this->generateAppConfig();
  432. $this->assertEqualsCanonicalizing(['test' => 1], $config->getValueArray('typed', 'array', []));
  433. }
  434. public function testGetValueArrayOnUnknownAppReturnsDefault(): void {
  435. $config = $this->generateAppConfig();
  436. $this->assertSame([1], $config->getValueArray('typed-1', 'array', [1]));
  437. }
  438. public function testGetValueArrayOnNonExistentKeyReturnsDefault(): void {
  439. $config = $this->generateAppConfig();
  440. $this->assertSame([1, 2], $config->getValueArray('typed', 'array-2', [1, 2]));
  441. }
  442. public function testGetValueArrayOnWrongType(): void {
  443. $config = $this->generateAppConfig();
  444. $this->expectException(AppConfigTypeConflictException::class);
  445. $config->getValueArray('typed', 'string');
  446. }
  447. /**
  448. * @return array
  449. * @see testGetValueType
  450. *
  451. * @see testGetValueMixed
  452. */
  453. public function providerGetValueMixed(): array {
  454. return [
  455. // key, value, type
  456. ['mixed', 'mix', IAppConfig::VALUE_MIXED],
  457. ['string', 'value', IAppConfig::VALUE_STRING],
  458. ['int', '42', IAppConfig::VALUE_INT],
  459. ['float', '3.14', IAppConfig::VALUE_FLOAT],
  460. ['bool', '1', IAppConfig::VALUE_BOOL],
  461. ['array', '{"test": 1}', IAppConfig::VALUE_ARRAY],
  462. ];
  463. }
  464. /**
  465. * @dataProvider providerGetValueMixed
  466. *
  467. * @param string $key
  468. * @param string $value
  469. */
  470. public function testGetValueMixed(string $key, string $value): void {
  471. $config = $this->generateAppConfig();
  472. $this->assertSame($value, $config->getValueMixed('typed', $key));
  473. }
  474. /**
  475. * @dataProvider providerGetValueMixed
  476. *
  477. * @param string $key
  478. * @param string $value
  479. * @param int $type
  480. */
  481. public function testGetValueType(string $key, string $value, int $type): void {
  482. $config = $this->generateAppConfig();
  483. $this->assertSame($type, $config->getValueType('typed', $key));
  484. }
  485. public function testGetValueTypeOnUnknownApp(): void {
  486. $config = $this->generateAppConfig();
  487. $this->expectException(AppConfigUnknownKeyException::class);
  488. $config->getValueType('typed-1', 'string');
  489. }
  490. public function testGetValueTypeOnNonExistentKey(): void {
  491. $config = $this->generateAppConfig();
  492. $this->expectException(AppConfigUnknownKeyException::class);
  493. $config->getValueType('typed', 'string-2');
  494. }
  495. public function testSetValueString(): void {
  496. $config = $this->generateAppConfig();
  497. $config->setValueString('feed', 'string', 'value-1');
  498. $this->assertSame('value-1', $config->getValueString('feed', 'string', ''));
  499. }
  500. public function testSetValueStringCache(): void {
  501. $config = $this->generateAppConfig();
  502. $config->setValueString('feed', 'string', 'value-1');
  503. $status = $config->statusCache();
  504. $this->assertSame('value-1', $status['fastCache']['feed']['string']);
  505. }
  506. public function testSetValueStringDatabase(): void {
  507. $config = $this->generateAppConfig();
  508. $config->setValueString('feed', 'string', 'value-1');
  509. $config->clearCache();
  510. $this->assertSame('value-1', $config->getValueString('feed', 'string', ''));
  511. }
  512. public function testSetValueStringIsUpdated(): void {
  513. $config = $this->generateAppConfig();
  514. $config->setValueString('feed', 'string', 'value-1');
  515. $this->assertSame(true, $config->setValueString('feed', 'string', 'value-2'));
  516. }
  517. public function testSetValueStringIsNotUpdated(): void {
  518. $config = $this->generateAppConfig();
  519. $config->setValueString('feed', 'string', 'value-1');
  520. $this->assertSame(false, $config->setValueString('feed', 'string', 'value-1'));
  521. }
  522. public function testSetValueStringIsUpdatedCache(): void {
  523. $config = $this->generateAppConfig();
  524. $config->setValueString('feed', 'string', 'value-1');
  525. $config->setValueString('feed', 'string', 'value-2');
  526. $status = $config->statusCache();
  527. $this->assertSame('value-2', $status['fastCache']['feed']['string']);
  528. }
  529. public function testSetValueStringIsUpdatedDatabase(): void {
  530. $config = $this->generateAppConfig();
  531. $config->setValueString('feed', 'string', 'value-1');
  532. $config->setValueString('feed', 'string', 'value-2');
  533. $config->clearCache();
  534. $this->assertSame('value-2', $config->getValueString('feed', 'string', ''));
  535. }
  536. public function testSetValueInt(): void {
  537. $config = $this->generateAppConfig();
  538. $config->setValueInt('feed', 'int', 42);
  539. $this->assertSame(42, $config->getValueInt('feed', 'int', 0));
  540. }
  541. public function testSetValueIntCache(): void {
  542. $config = $this->generateAppConfig();
  543. $config->setValueInt('feed', 'int', 42);
  544. $status = $config->statusCache();
  545. $this->assertSame('42', $status['fastCache']['feed']['int']);
  546. }
  547. public function testSetValueIntDatabase(): void {
  548. $config = $this->generateAppConfig();
  549. $config->setValueInt('feed', 'int', 42);
  550. $config->clearCache();
  551. $this->assertSame(42, $config->getValueInt('feed', 'int', 0));
  552. }
  553. public function testSetValueIntIsUpdated(): void {
  554. $config = $this->generateAppConfig();
  555. $config->setValueInt('feed', 'int', 42);
  556. $this->assertSame(true, $config->setValueInt('feed', 'int', 17));
  557. }
  558. public function testSetValueIntIsNotUpdated(): void {
  559. $config = $this->generateAppConfig();
  560. $config->setValueInt('feed', 'int', 42);
  561. $this->assertSame(false, $config->setValueInt('feed', 'int', 42));
  562. }
  563. public function testSetValueIntIsUpdatedCache(): void {
  564. $config = $this->generateAppConfig();
  565. $config->setValueInt('feed', 'int', 42);
  566. $config->setValueInt('feed', 'int', 17);
  567. $status = $config->statusCache();
  568. $this->assertSame('17', $status['fastCache']['feed']['int']);
  569. }
  570. public function testSetValueIntIsUpdatedDatabase(): void {
  571. $config = $this->generateAppConfig();
  572. $config->setValueInt('feed', 'int', 42);
  573. $config->setValueInt('feed', 'int', 17);
  574. $config->clearCache();
  575. $this->assertSame(17, $config->getValueInt('feed', 'int', 0));
  576. }
  577. public function testSetValueFloat(): void {
  578. $config = $this->generateAppConfig();
  579. $config->setValueFloat('feed', 'float', 3.14);
  580. $this->assertSame(3.14, $config->getValueFloat('feed', 'float', 0));
  581. }
  582. public function testSetValueFloatCache(): void {
  583. $config = $this->generateAppConfig();
  584. $config->setValueFloat('feed', 'float', 3.14);
  585. $status = $config->statusCache();
  586. $this->assertSame('3.14', $status['fastCache']['feed']['float']);
  587. }
  588. public function testSetValueFloatDatabase(): void {
  589. $config = $this->generateAppConfig();
  590. $config->setValueFloat('feed', 'float', 3.14);
  591. $config->clearCache();
  592. $this->assertSame(3.14, $config->getValueFloat('feed', 'float', 0));
  593. }
  594. public function testSetValueFloatIsUpdated(): void {
  595. $config = $this->generateAppConfig();
  596. $config->setValueFloat('feed', 'float', 3.14);
  597. $this->assertSame(true, $config->setValueFloat('feed', 'float', 1.23));
  598. }
  599. public function testSetValueFloatIsNotUpdated(): void {
  600. $config = $this->generateAppConfig();
  601. $config->setValueFloat('feed', 'float', 3.14);
  602. $this->assertSame(false, $config->setValueFloat('feed', 'float', 3.14));
  603. }
  604. public function testSetValueFloatIsUpdatedCache(): void {
  605. $config = $this->generateAppConfig();
  606. $config->setValueFloat('feed', 'float', 3.14);
  607. $config->setValueFloat('feed', 'float', 1.23);
  608. $status = $config->statusCache();
  609. $this->assertSame('1.23', $status['fastCache']['feed']['float']);
  610. }
  611. public function testSetValueFloatIsUpdatedDatabase(): void {
  612. $config = $this->generateAppConfig();
  613. $config->setValueFloat('feed', 'float', 3.14);
  614. $config->setValueFloat('feed', 'float', 1.23);
  615. $config->clearCache();
  616. $this->assertSame(1.23, $config->getValueFloat('feed', 'float', 0));
  617. }
  618. public function testSetValueBool(): void {
  619. $config = $this->generateAppConfig();
  620. $config->setValueBool('feed', 'bool', true);
  621. $this->assertSame(true, $config->getValueBool('feed', 'bool', false));
  622. }
  623. public function testSetValueBoolCache(): void {
  624. $config = $this->generateAppConfig();
  625. $config->setValueBool('feed', 'bool', true);
  626. $status = $config->statusCache();
  627. $this->assertSame('1', $status['fastCache']['feed']['bool']);
  628. }
  629. public function testSetValueBoolDatabase(): void {
  630. $config = $this->generateAppConfig();
  631. $config->setValueBool('feed', 'bool', true);
  632. $config->clearCache();
  633. $this->assertSame(true, $config->getValueBool('feed', 'bool', false));
  634. }
  635. public function testSetValueBoolIsUpdated(): void {
  636. $config = $this->generateAppConfig();
  637. $config->setValueBool('feed', 'bool', true);
  638. $this->assertSame(true, $config->setValueBool('feed', 'bool', false));
  639. }
  640. public function testSetValueBoolIsNotUpdated(): void {
  641. $config = $this->generateAppConfig();
  642. $config->setValueBool('feed', 'bool', true);
  643. $this->assertSame(false, $config->setValueBool('feed', 'bool', true));
  644. }
  645. public function testSetValueBoolIsUpdatedCache(): void {
  646. $config = $this->generateAppConfig();
  647. $config->setValueBool('feed', 'bool', true);
  648. $config->setValueBool('feed', 'bool', false);
  649. $status = $config->statusCache();
  650. $this->assertSame('0', $status['fastCache']['feed']['bool']);
  651. }
  652. public function testSetValueBoolIsUpdatedDatabase(): void {
  653. $config = $this->generateAppConfig();
  654. $config->setValueBool('feed', 'bool', true);
  655. $config->setValueBool('feed', 'bool', false);
  656. $config->clearCache();
  657. $this->assertSame(false, $config->getValueBool('feed', 'bool', true));
  658. }
  659. public function testSetValueArray(): void {
  660. $config = $this->generateAppConfig();
  661. $config->setValueArray('feed', 'array', ['test' => 1]);
  662. $this->assertSame(['test' => 1], $config->getValueArray('feed', 'array', []));
  663. }
  664. public function testSetValueArrayCache(): void {
  665. $config = $this->generateAppConfig();
  666. $config->setValueArray('feed', 'array', ['test' => 1]);
  667. $status = $config->statusCache();
  668. $this->assertSame('{"test":1}', $status['fastCache']['feed']['array']);
  669. }
  670. public function testSetValueArrayDatabase(): void {
  671. $config = $this->generateAppConfig();
  672. $config->setValueArray('feed', 'array', ['test' => 1]);
  673. $config->clearCache();
  674. $this->assertSame(['test' => 1], $config->getValueArray('feed', 'array', []));
  675. }
  676. public function testSetValueArrayIsUpdated(): void {
  677. $config = $this->generateAppConfig();
  678. $config->setValueArray('feed', 'array', ['test' => 1]);
  679. $this->assertSame(true, $config->setValueArray('feed', 'array', ['test' => 2]));
  680. }
  681. public function testSetValueArrayIsNotUpdated(): void {
  682. $config = $this->generateAppConfig();
  683. $config->setValueArray('feed', 'array', ['test' => 1]);
  684. $this->assertSame(false, $config->setValueArray('feed', 'array', ['test' => 1]));
  685. }
  686. public function testSetValueArrayIsUpdatedCache(): void {
  687. $config = $this->generateAppConfig();
  688. $config->setValueArray('feed', 'array', ['test' => 1]);
  689. $config->setValueArray('feed', 'array', ['test' => 2]);
  690. $status = $config->statusCache();
  691. $this->assertSame('{"test":2}', $status['fastCache']['feed']['array']);
  692. }
  693. public function testSetValueArrayIsUpdatedDatabase(): void {
  694. $config = $this->generateAppConfig();
  695. $config->setValueArray('feed', 'array', ['test' => 1]);
  696. $config->setValueArray('feed', 'array', ['test' => 2]);
  697. $config->clearCache();
  698. $this->assertSame(['test' => 2], $config->getValueArray('feed', 'array', []));
  699. }
  700. public function testSetLazyValueString(): void {
  701. $config = $this->generateAppConfig();
  702. $config->setValueString('feed', 'string', 'value-1', true);
  703. $this->assertSame('value-1', $config->getValueString('feed', 'string', '', true));
  704. }
  705. public function testSetLazyValueStringCache(): void {
  706. $config = $this->generateAppConfig();
  707. $config->setValueString('feed', 'string', 'value-1', true);
  708. $status = $config->statusCache();
  709. $this->assertSame('value-1', $status['lazyCache']['feed']['string']);
  710. }
  711. public function testSetLazyValueStringDatabase(): void {
  712. $config = $this->generateAppConfig();
  713. $config->setValueString('feed', 'string', 'value-1', true);
  714. $config->clearCache();
  715. $this->assertSame('value-1', $config->getValueString('feed', 'string', '', true));
  716. }
  717. public function testSetLazyValueStringAsNonLazy(): void {
  718. $config = $this->generateAppConfig();
  719. $config->setValueString('feed', 'string', 'value-1', true);
  720. $config->setValueString('feed', 'string', 'value-1', false);
  721. $this->assertSame('value-1', $config->getValueString('feed', 'string', ''));
  722. }
  723. public function testSetNonLazyValueStringAsLazy(): void {
  724. $config = $this->generateAppConfig();
  725. $config->setValueString('feed', 'string', 'value-1', false);
  726. $config->setValueString('feed', 'string', 'value-1', true);
  727. $this->assertSame('value-1', $config->getValueString('feed', 'string', '', true));
  728. }
  729. public function testSetSensitiveValueString(): void {
  730. $config = $this->generateAppConfig();
  731. $config->setValueString('feed', 'string', 'value-1', sensitive: true);
  732. $this->assertSame('value-1', $config->getValueString('feed', 'string', ''));
  733. }
  734. public function testSetSensitiveValueStringCache(): void {
  735. $config = $this->generateAppConfig();
  736. $config->setValueString('feed', 'string', 'value-1', sensitive: true);
  737. $status = $config->statusCache();
  738. // TODO: this value will be encrypted with #43114
  739. $this->assertSame('value-1', $status['fastCache']['feed']['string']);
  740. }
  741. public function testSetSensitiveValueStringDatabase(): void {
  742. $config = $this->generateAppConfig();
  743. $config->setValueString('feed', 'string', 'value-1', sensitive: true);
  744. $config->clearCache();
  745. $this->assertSame('value-1', $config->getValueString('feed', 'string', ''));
  746. }
  747. public function testSetNonSensitiveValueStringAsSensitive(): void {
  748. $config = $this->generateAppConfig();
  749. $config->setValueString('feed', 'string', 'value-1', sensitive: false);
  750. $config->setValueString('feed', 'string', 'value-1', sensitive: true);
  751. $this->assertSame(true, $config->isSensitive('feed', 'string'));
  752. }
  753. public function testSetSensitiveValueStringAsNonSensitiveStaysSensitive(): void {
  754. $config = $this->generateAppConfig();
  755. $config->setValueString('feed', 'string', 'value-1', sensitive: true);
  756. $config->setValueString('feed', 'string', 'value-2', sensitive: false);
  757. $this->assertSame(true, $config->isSensitive('feed', 'string'));
  758. }
  759. public function testSetSensitiveValueStringAsNonSensitiveAreStillUpdated(): void {
  760. $config = $this->generateAppConfig();
  761. $config->setValueString('feed', 'string', 'value-1', sensitive: true);
  762. $config->setValueString('feed', 'string', 'value-2', sensitive: false);
  763. $this->assertSame('value-2', $config->getValueString('feed', 'string', ''));
  764. }
  765. public function testSetSensitiveValueStringAsNonSensitiveWouldFailForSameValue(): void {
  766. $config = $this->generateAppConfig();
  767. $config->setValueString('feed', 'string', 'value-1', sensitive: true);
  768. $this->assertSame(false, $config->setValueString('feed', 'string', 'value-1'));
  769. }
  770. public function testSetLazyValueInt(): void {
  771. $config = $this->generateAppConfig();
  772. $config->setValueInt('feed', 'int', 42, true);
  773. $this->assertSame(42, $config->getValueInt('feed', 'int', 0, true));
  774. }
  775. public function testSetLazyValueIntCache(): void {
  776. $config = $this->generateAppConfig();
  777. $config->setValueInt('feed', 'int', 42, true);
  778. $status = $config->statusCache();
  779. $this->assertSame('42', $status['lazyCache']['feed']['int']);
  780. }
  781. public function testSetLazyValueIntDatabase(): void {
  782. $config = $this->generateAppConfig();
  783. $config->setValueInt('feed', 'int', 42, true);
  784. $config->clearCache();
  785. $this->assertSame(42, $config->getValueInt('feed', 'int', 0, true));
  786. }
  787. public function testSetLazyValueIntAsNonLazy(): void {
  788. $config = $this->generateAppConfig();
  789. $config->setValueInt('feed', 'int', 42, true);
  790. $config->setValueInt('feed', 'int', 42, false);
  791. $this->assertSame(42, $config->getValueInt('feed', 'int', 0));
  792. }
  793. public function testSetNonLazyValueIntAsLazy(): void {
  794. $config = $this->generateAppConfig();
  795. $config->setValueInt('feed', 'int', 42, false);
  796. $config->setValueInt('feed', 'int', 42, true);
  797. $this->assertSame(42, $config->getValueInt('feed', 'int', 0, true));
  798. }
  799. public function testSetSensitiveValueInt(): void {
  800. $config = $this->generateAppConfig();
  801. $config->setValueInt('feed', 'int', 42, sensitive: true);
  802. $this->assertSame(42, $config->getValueInt('feed', 'int', 0));
  803. }
  804. public function testSetSensitiveValueIntCache(): void {
  805. $config = $this->generateAppConfig();
  806. $config->setValueInt('feed', 'int', 42, sensitive: true);
  807. $status = $config->statusCache();
  808. // TODO: this value will be encrypted with #43114
  809. $this->assertSame('42', $status['fastCache']['feed']['int']);
  810. }
  811. public function testSetSensitiveValueIntDatabase(): void {
  812. $config = $this->generateAppConfig();
  813. $config->setValueInt('feed', 'int', 42, sensitive: true);
  814. $config->clearCache();
  815. $this->assertSame(42, $config->getValueInt('feed', 'int', 0));
  816. }
  817. public function testSetNonSensitiveValueIntAsSensitive(): void {
  818. $config = $this->generateAppConfig();
  819. $config->setValueInt('feed', 'int', 42);
  820. $config->setValueInt('feed', 'int', 42, sensitive: true);
  821. $this->assertSame(true, $config->isSensitive('feed', 'int'));
  822. }
  823. public function testSetSensitiveValueIntAsNonSensitiveStaysSensitive(): void {
  824. $config = $this->generateAppConfig();
  825. $config->setValueInt('feed', 'int', 42, sensitive: true);
  826. $config->setValueInt('feed', 'int', 17);
  827. $this->assertSame(true, $config->isSensitive('feed', 'int'));
  828. }
  829. public function testSetSensitiveValueIntAsNonSensitiveAreStillUpdated(): void {
  830. $config = $this->generateAppConfig();
  831. $config->setValueInt('feed', 'int', 42, sensitive: true);
  832. $config->setValueInt('feed', 'int', 17);
  833. $this->assertSame(17, $config->getValueInt('feed', 'int', 0));
  834. }
  835. public function testSetSensitiveValueIntAsNonSensitiveWouldFailForSameValue(): void {
  836. $config = $this->generateAppConfig();
  837. $config->setValueInt('feed', 'int', 42, sensitive: true);
  838. $this->assertSame(false, $config->setValueInt('feed', 'int', 42));
  839. }
  840. public function testSetLazyValueFloat(): void {
  841. $config = $this->generateAppConfig();
  842. $config->setValueFloat('feed', 'float', 3.14, true);
  843. $this->assertSame(3.14, $config->getValueFloat('feed', 'float', 0, true));
  844. }
  845. public function testSetLazyValueFloatCache(): void {
  846. $config = $this->generateAppConfig();
  847. $config->setValueFloat('feed', 'float', 3.14, true);
  848. $status = $config->statusCache();
  849. $this->assertSame('3.14', $status['lazyCache']['feed']['float']);
  850. }
  851. public function testSetLazyValueFloatDatabase(): void {
  852. $config = $this->generateAppConfig();
  853. $config->setValueFloat('feed', 'float', 3.14, true);
  854. $config->clearCache();
  855. $this->assertSame(3.14, $config->getValueFloat('feed', 'float', 0, true));
  856. }
  857. public function testSetLazyValueFloatAsNonLazy(): void {
  858. $config = $this->generateAppConfig();
  859. $config->setValueFloat('feed', 'float', 3.14, true);
  860. $config->setValueFloat('feed', 'float', 3.14, false);
  861. $this->assertSame(3.14, $config->getValueFloat('feed', 'float', 0));
  862. }
  863. public function testSetNonLazyValueFloatAsLazy(): void {
  864. $config = $this->generateAppConfig();
  865. $config->setValueFloat('feed', 'float', 3.14, false);
  866. $config->setValueFloat('feed', 'float', 3.14, true);
  867. $this->assertSame(3.14, $config->getValueFloat('feed', 'float', 0, true));
  868. }
  869. public function testSetSensitiveValueFloat(): void {
  870. $config = $this->generateAppConfig();
  871. $config->setValueFloat('feed', 'float', 3.14, sensitive: true);
  872. $this->assertSame(3.14, $config->getValueFloat('feed', 'float', 0));
  873. }
  874. public function testSetSensitiveValueFloatCache(): void {
  875. $config = $this->generateAppConfig();
  876. $config->setValueFloat('feed', 'float', 3.14, sensitive: true);
  877. $status = $config->statusCache();
  878. // TODO: this value will be encrypted with #43114
  879. $this->assertSame('3.14', $status['fastCache']['feed']['float']);
  880. }
  881. public function testSetSensitiveValueFloatDatabase(): void {
  882. $config = $this->generateAppConfig();
  883. $config->setValueFloat('feed', 'float', 3.14, sensitive: true);
  884. $config->clearCache();
  885. $this->assertSame(3.14, $config->getValueFloat('feed', 'float', 0));
  886. }
  887. public function testSetNonSensitiveValueFloatAsSensitive(): void {
  888. $config = $this->generateAppConfig();
  889. $config->setValueFloat('feed', 'float', 3.14);
  890. $config->setValueFloat('feed', 'float', 3.14, sensitive: true);
  891. $this->assertSame(true, $config->isSensitive('feed', 'float'));
  892. }
  893. public function testSetSensitiveValueFloatAsNonSensitiveStaysSensitive(): void {
  894. $config = $this->generateAppConfig();
  895. $config->setValueFloat('feed', 'float', 3.14, sensitive: true);
  896. $config->setValueFloat('feed', 'float', 1.23);
  897. $this->assertSame(true, $config->isSensitive('feed', 'float'));
  898. }
  899. public function testSetSensitiveValueFloatAsNonSensitiveAreStillUpdated(): void {
  900. $config = $this->generateAppConfig();
  901. $config->setValueFloat('feed', 'float', 3.14, sensitive: true);
  902. $config->setValueFloat('feed', 'float', 1.23);
  903. $this->assertSame(1.23, $config->getValueFloat('feed', 'float', 0));
  904. }
  905. public function testSetSensitiveValueFloatAsNonSensitiveWouldFailForSameValue(): void {
  906. $config = $this->generateAppConfig();
  907. $config->setValueFloat('feed', 'float', 3.14, sensitive: true);
  908. $this->assertSame(false, $config->setValueFloat('feed', 'float', 3.14));
  909. }
  910. public function testSetLazyValueBool(): void {
  911. $config = $this->generateAppConfig();
  912. $config->setValueBool('feed', 'bool', true, true);
  913. $this->assertSame(true, $config->getValueBool('feed', 'bool', false, true));
  914. }
  915. public function testSetLazyValueBoolCache(): void {
  916. $config = $this->generateAppConfig();
  917. $config->setValueBool('feed', 'bool', true, true);
  918. $status = $config->statusCache();
  919. $this->assertSame('1', $status['lazyCache']['feed']['bool']);
  920. }
  921. public function testSetLazyValueBoolDatabase(): void {
  922. $config = $this->generateAppConfig();
  923. $config->setValueBool('feed', 'bool', true, true);
  924. $config->clearCache();
  925. $this->assertSame(true, $config->getValueBool('feed', 'bool', false, true));
  926. }
  927. public function testSetLazyValueBoolAsNonLazy(): void {
  928. $config = $this->generateAppConfig();
  929. $config->setValueBool('feed', 'bool', true, true);
  930. $config->setValueBool('feed', 'bool', true, false);
  931. $this->assertSame(true, $config->getValueBool('feed', 'bool', false));
  932. }
  933. public function testSetNonLazyValueBoolAsLazy(): void {
  934. $config = $this->generateAppConfig();
  935. $config->setValueBool('feed', 'bool', true, false);
  936. $config->setValueBool('feed', 'bool', true, true);
  937. $this->assertSame(true, $config->getValueBool('feed', 'bool', false, true));
  938. }
  939. public function testSetLazyValueArray(): void {
  940. $config = $this->generateAppConfig();
  941. $config->setValueArray('feed', 'array', ['test' => 1], true);
  942. $this->assertSame(['test' => 1], $config->getValueArray('feed', 'array', [], true));
  943. }
  944. public function testSetLazyValueArrayCache(): void {
  945. $config = $this->generateAppConfig();
  946. $config->setValueArray('feed', 'array', ['test' => 1], true);
  947. $status = $config->statusCache();
  948. $this->assertSame('{"test":1}', $status['lazyCache']['feed']['array']);
  949. }
  950. public function testSetLazyValueArrayDatabase(): void {
  951. $config = $this->generateAppConfig();
  952. $config->setValueArray('feed', 'array', ['test' => 1], true);
  953. $config->clearCache();
  954. $this->assertSame(['test' => 1], $config->getValueArray('feed', 'array', [], true));
  955. }
  956. public function testSetLazyValueArrayAsNonLazy(): void {
  957. $config = $this->generateAppConfig();
  958. $config->setValueArray('feed', 'array', ['test' => 1], true);
  959. $config->setValueArray('feed', 'array', ['test' => 1], false);
  960. $this->assertSame(['test' => 1], $config->getValueArray('feed', 'array', []));
  961. }
  962. public function testSetNonLazyValueArrayAsLazy(): void {
  963. $config = $this->generateAppConfig();
  964. $config->setValueArray('feed', 'array', ['test' => 1], false);
  965. $config->setValueArray('feed', 'array', ['test' => 1], true);
  966. $this->assertSame(['test' => 1], $config->getValueArray('feed', 'array', [], true));
  967. }
  968. public function testSetSensitiveValueArray(): void {
  969. $config = $this->generateAppConfig();
  970. $config->setValueArray('feed', 'array', ['test' => 1], sensitive: true);
  971. $this->assertEqualsCanonicalizing(['test' => 1], $config->getValueArray('feed', 'array', []));
  972. }
  973. public function testSetSensitiveValueArrayCache(): void {
  974. $config = $this->generateAppConfig();
  975. $config->setValueArray('feed', 'array', ['test' => 1], sensitive: true);
  976. $status = $config->statusCache();
  977. // TODO: this value will be encrypted with #43114
  978. $this->assertSame('{"test":1}', $status['fastCache']['feed']['array']);
  979. }
  980. public function testSetSensitiveValueArrayDatabase(): void {
  981. $config = $this->generateAppConfig();
  982. $config->setValueArray('feed', 'array', ['test' => 1], sensitive: true);
  983. $config->clearCache();
  984. $this->assertEqualsCanonicalizing(['test' => 1], $config->getValueArray('feed', 'array', []));
  985. }
  986. public function testSetNonSensitiveValueArrayAsSensitive(): void {
  987. $config = $this->generateAppConfig();
  988. $config->setValueArray('feed', 'array', ['test' => 1]);
  989. $config->setValueArray('feed', 'array', ['test' => 1], sensitive: true);
  990. $this->assertSame(true, $config->isSensitive('feed', 'array'));
  991. }
  992. public function testSetSensitiveValueArrayAsNonSensitiveStaysSensitive(): void {
  993. $config = $this->generateAppConfig();
  994. $config->setValueArray('feed', 'array', ['test' => 1], sensitive: true);
  995. $config->setValueArray('feed', 'array', ['test' => 2]);
  996. $this->assertSame(true, $config->isSensitive('feed', 'array'));
  997. }
  998. public function testSetSensitiveValueArrayAsNonSensitiveAreStillUpdated(): void {
  999. $config = $this->generateAppConfig();
  1000. $config->setValueArray('feed', 'array', ['test' => 1], sensitive: true);
  1001. $config->setValueArray('feed', 'array', ['test' => 2]);
  1002. $this->assertEqualsCanonicalizing(['test' => 2], $config->getValueArray('feed', 'array', []));
  1003. }
  1004. public function testSetSensitiveValueArrayAsNonSensitiveWouldFailForSameValue(): void {
  1005. $config = $this->generateAppConfig();
  1006. $config->setValueArray('feed', 'array', ['test' => 1], sensitive: true);
  1007. $this->assertSame(false, $config->setValueArray('feed', 'array', ['test' => 1]));
  1008. }
  1009. public function testUpdateNotSensitiveToSensitive(): void {
  1010. $config = $this->generateAppConfig();
  1011. $config->updateSensitive('non-sensitive-app', 'lazy-key', true);
  1012. $this->assertSame(true, $config->isSensitive('non-sensitive-app', 'lazy-key', true));
  1013. }
  1014. public function testUpdateSensitiveToNotSensitive(): void {
  1015. $config = $this->generateAppConfig();
  1016. $config->updateSensitive('sensitive-app', 'lazy-key', false);
  1017. $this->assertSame(false, $config->isSensitive('sensitive-app', 'lazy-key', true));
  1018. }
  1019. public function testUpdateSensitiveToSensitiveReturnsFalse(): void {
  1020. $config = $this->generateAppConfig();
  1021. $this->assertSame(false, $config->updateSensitive('sensitive-app', 'lazy-key', true));
  1022. }
  1023. public function testUpdateNotSensitiveToNotSensitiveReturnsFalse(): void {
  1024. $config = $this->generateAppConfig();
  1025. $this->assertSame(false, $config->updateSensitive('non-sensitive-app', 'lazy-key', false));
  1026. }
  1027. public function testUpdateSensitiveOnUnknownKeyReturnsFalse(): void {
  1028. $config = $this->generateAppConfig();
  1029. $this->assertSame(false, $config->updateSensitive('non-sensitive-app', 'unknown-key', true));
  1030. }
  1031. public function testUpdateNotLazyToLazy(): void {
  1032. $config = $this->generateAppConfig();
  1033. $config->updateLazy('non-sensitive-app', 'non-lazy-key', true);
  1034. $this->assertSame(true, $config->isLazy('non-sensitive-app', 'non-lazy-key'));
  1035. }
  1036. public function testUpdateLazyToNotLazy(): void {
  1037. $config = $this->generateAppConfig();
  1038. $config->updateLazy('non-sensitive-app', 'lazy-key', false);
  1039. $this->assertSame(false, $config->isLazy('non-sensitive-app', 'lazy-key'));
  1040. }
  1041. public function testUpdateLazyToLazyReturnsFalse(): void {
  1042. $config = $this->generateAppConfig();
  1043. $this->assertSame(false, $config->updateLazy('non-sensitive-app', 'lazy-key', true));
  1044. }
  1045. public function testUpdateNotLazyToNotLazyReturnsFalse(): void {
  1046. $config = $this->generateAppConfig();
  1047. $this->assertSame(false, $config->updateLazy('non-sensitive-app', 'non-lazy-key', false));
  1048. }
  1049. public function testUpdateLazyOnUnknownKeyReturnsFalse(): void {
  1050. $config = $this->generateAppConfig();
  1051. $this->assertSame(false, $config->updateLazy('non-sensitive-app', 'unknown-key', true));
  1052. }
  1053. public function testGetDetails(): void {
  1054. $config = $this->generateAppConfig();
  1055. $this->assertEqualsCanonicalizing(
  1056. [
  1057. 'app' => 'sensitive-app',
  1058. 'key' => 'lazy-key',
  1059. 'value' => 'value',
  1060. 'type' => 4,
  1061. 'lazy' => true,
  1062. 'typeString' => 'string',
  1063. 'sensitive' => true
  1064. ],
  1065. $config->getDetails('sensitive-app', 'lazy-key')
  1066. );
  1067. }
  1068. public function testGetDetailsInt(): void {
  1069. $config = $this->generateAppConfig();
  1070. $this->assertEqualsCanonicalizing(
  1071. [
  1072. 'app' => 'typed',
  1073. 'key' => 'int',
  1074. 'value' => '42',
  1075. 'type' => 8,
  1076. 'lazy' => false,
  1077. 'typeString' => 'integer',
  1078. 'sensitive' => false
  1079. ],
  1080. $config->getDetails('typed', 'int')
  1081. );
  1082. }
  1083. public function testGetDetailsFloat(): void {
  1084. $config = $this->generateAppConfig();
  1085. $this->assertEqualsCanonicalizing(
  1086. [
  1087. 'app' => 'typed',
  1088. 'key' => 'float',
  1089. 'value' => '3.14',
  1090. 'type' => 16,
  1091. 'lazy' => false,
  1092. 'typeString' => 'float',
  1093. 'sensitive' => false
  1094. ],
  1095. $config->getDetails('typed', 'float')
  1096. );
  1097. }
  1098. public function testGetDetailsBool(): void {
  1099. $config = $this->generateAppConfig();
  1100. $this->assertEqualsCanonicalizing(
  1101. [
  1102. 'app' => 'typed',
  1103. 'key' => 'bool',
  1104. 'value' => '1',
  1105. 'type' => 32,
  1106. 'lazy' => false,
  1107. 'typeString' => 'boolean',
  1108. 'sensitive' => false
  1109. ],
  1110. $config->getDetails('typed', 'bool')
  1111. );
  1112. }
  1113. public function testGetDetailsArray(): void {
  1114. $config = $this->generateAppConfig();
  1115. $this->assertEqualsCanonicalizing(
  1116. [
  1117. 'app' => 'typed',
  1118. 'key' => 'array',
  1119. 'value' => '{"test": 1}',
  1120. 'type' => 64,
  1121. 'lazy' => false,
  1122. 'typeString' => 'array',
  1123. 'sensitive' => false
  1124. ],
  1125. $config->getDetails('typed', 'array')
  1126. );
  1127. }
  1128. public function testDeleteKey(): void {
  1129. $config = $this->generateAppConfig();
  1130. $config->deleteKey('anotherapp', 'key');
  1131. $this->assertSame('default', $config->getValueString('anotherapp', 'key', 'default'));
  1132. }
  1133. public function testDeleteKeyCache(): void {
  1134. $config = $this->generateAppConfig();
  1135. $config->deleteKey('anotherapp', 'key');
  1136. $status = $config->statusCache();
  1137. $this->assertEqualsCanonicalizing(['enabled' => 'false'], $status['fastCache']['anotherapp']);
  1138. }
  1139. public function testDeleteKeyDatabase(): void {
  1140. $config = $this->generateAppConfig();
  1141. $config->deleteKey('anotherapp', 'key');
  1142. $config->clearCache();
  1143. $this->assertSame('default', $config->getValueString('anotherapp', 'key', 'default'));
  1144. }
  1145. public function testDeleteApp(): void {
  1146. $config = $this->generateAppConfig();
  1147. $config->deleteApp('anotherapp');
  1148. $this->assertSame('default', $config->getValueString('anotherapp', 'key', 'default'));
  1149. $this->assertSame('default', $config->getValueString('anotherapp', 'enabled', 'default'));
  1150. }
  1151. public function testDeleteAppCache(): void {
  1152. $config = $this->generateAppConfig();
  1153. $status = $config->statusCache();
  1154. $this->assertSame(true, isset($status['fastCache']['anotherapp']));
  1155. $config->deleteApp('anotherapp');
  1156. $status = $config->statusCache();
  1157. $this->assertSame(false, isset($status['fastCache']['anotherapp']));
  1158. }
  1159. public function testDeleteAppDatabase(): void {
  1160. $config = $this->generateAppConfig();
  1161. $config->deleteApp('anotherapp');
  1162. $config->clearCache();
  1163. $this->assertSame('default', $config->getValueString('anotherapp', 'key', 'default'));
  1164. $this->assertSame('default', $config->getValueString('anotherapp', 'enabled', 'default'));
  1165. }
  1166. public function testClearCache(): void {
  1167. $config = $this->generateAppConfig();
  1168. $config->setValueString('feed', 'string', '123454');
  1169. $config->clearCache();
  1170. $status = $config->statusCache();
  1171. $this->assertSame([], $status['fastCache']);
  1172. }
  1173. }