AppConfigTest.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace Test\AppFramework\Services;
  8. use OC\AppConfig as AppConfigCore;
  9. use OC\AppFramework\Services\AppConfig;
  10. use OCP\Exceptions\AppConfigTypeConflictException;
  11. use OCP\Exceptions\AppConfigUnknownKeyException;
  12. use OCP\IAppConfig as IAppConfigCore;
  13. use OCP\IConfig;
  14. use PHPUnit\Framework\MockObject\MockObject;
  15. use Test\TestCase;
  16. class AppConfigTest extends TestCase {
  17. private IConfig|MockObject $config;
  18. private IAppConfigCore|MockObject $appConfigCore;
  19. private AppConfig $appConfig;
  20. private const TEST_APPID = 'appconfig-test';
  21. protected function setUp(): void {
  22. parent::setUp();
  23. $this->config = $this->createMock(IConfig::class);
  24. $this->appConfigCore = $this->createMock(AppConfigCore::class);
  25. $this->appConfig = new AppConfig($this->config, $this->appConfigCore, self::TEST_APPID);
  26. }
  27. public function testGetAppKeys(): void {
  28. $expected = ['key1', 'key2', 'key3', 'key4', 'key5', 'key6', 'key7', 'test8'];
  29. $this->appConfigCore->expects($this->once())
  30. ->method('getKeys')
  31. ->with(self::TEST_APPID)
  32. ->willReturn($expected);
  33. $this->assertSame($expected, $this->appConfig->getAppKeys());
  34. }
  35. /**
  36. * @return array
  37. * @see testHasAppKey
  38. */
  39. public function providerHasAppKey(): array {
  40. return [
  41. // lazy, expected
  42. [false, true],
  43. [true, true],
  44. [false, false],
  45. [true, false],
  46. ];
  47. }
  48. /**
  49. * @dataProvider providerHasAppKey
  50. *
  51. * @param bool $lazy
  52. * @param bool $expected
  53. */
  54. public function testHasAppKey(bool $lazy, bool $expected): void {
  55. $key = 'key';
  56. $this->appConfigCore->expects($this->once())
  57. ->method('hasKey')
  58. ->with(self::TEST_APPID, $key, $lazy)
  59. ->willReturn($expected);
  60. $this->assertSame($expected, $this->appConfig->hasAppKey($key, $lazy));
  61. }
  62. /**
  63. * @return array
  64. * @see testIsSensitive
  65. */
  66. public function providerIsSensitive(): array {
  67. return [
  68. // lazy, expected
  69. [false, true],
  70. [true, true],
  71. [false, false],
  72. [true, false],
  73. ];
  74. }
  75. /**
  76. * @dataProvider providerIsSensitive
  77. *
  78. * @param bool $lazy
  79. * @param bool $expected
  80. */
  81. public function testIsSensitive(bool $lazy, bool $expected): void {
  82. $key = 'key';
  83. $this->appConfigCore->expects($this->once())
  84. ->method('isSensitive')
  85. ->with(self::TEST_APPID, $key, $lazy)
  86. ->willReturn($expected);
  87. $this->assertSame($expected, $this->appConfig->isSensitive($key, $lazy));
  88. }
  89. /**
  90. * @dataProvider providerIsSensitive
  91. *
  92. * @param bool $lazy
  93. * @param bool $expected
  94. */
  95. public function testIsSensitiveException(bool $lazy, bool $expected): void {
  96. $key = 'unknown-key';
  97. $this->appConfigCore->expects($this->once())
  98. ->method('isSensitive')
  99. ->with(self::TEST_APPID, $key, $lazy)
  100. ->willThrowException(new AppConfigUnknownKeyException());
  101. $this->expectException(AppConfigUnknownKeyException::class);
  102. $this->appConfig->isSensitive($key, $lazy);
  103. }
  104. /**
  105. * @return array
  106. * @see testIsLazy
  107. */
  108. public function providerIsLazy(): array {
  109. return [
  110. // expected
  111. [true],
  112. [false],
  113. ];
  114. }
  115. /**
  116. * @dataProvider providerIsLazy
  117. *
  118. * @param bool $expected
  119. */
  120. public function testIsLazy(bool $expected): void {
  121. $key = 'key';
  122. $this->appConfigCore->expects($this->once())
  123. ->method('isLazy')
  124. ->with(self::TEST_APPID, $key)
  125. ->willReturn($expected);
  126. $this->assertSame($expected, $this->appConfig->isLazy($key));
  127. }
  128. public function testIsLazyException(): void {
  129. $key = 'unknown-key';
  130. $this->appConfigCore->expects($this->once())
  131. ->method('isLazy')
  132. ->with(self::TEST_APPID, $key)
  133. ->willThrowException(new AppConfigUnknownKeyException());
  134. $this->expectException(AppConfigUnknownKeyException::class);
  135. $this->appConfig->isLazy($key);
  136. }
  137. /**
  138. * @return array
  139. * @see testGetAllAppValues
  140. */
  141. public function providerGetAllAppValues(): array {
  142. return [
  143. // key, filtered
  144. ['', false],
  145. ['', true],
  146. ['key', false],
  147. ['key', true],
  148. ];
  149. }
  150. /**
  151. * @dataProvider providerGetAllAppValues
  152. *
  153. * @param string $key
  154. * @param bool $filtered
  155. */
  156. public function testGetAllAppValues(string $key, bool $filtered): void {
  157. $expected = [
  158. 'key1' => 'value1',
  159. 'key2' => 3,
  160. 'key3' => 3.14,
  161. 'key4' => true
  162. ];
  163. $this->appConfigCore->expects($this->once())
  164. ->method('getAllValues')
  165. ->with(self::TEST_APPID, $key, $filtered)
  166. ->willReturn($expected);
  167. $this->assertSame($expected, $this->appConfig->getAllAppValues($key, $filtered));
  168. }
  169. public function testSetAppValue(): void {
  170. $key = 'key';
  171. $value = 'value';
  172. $this->appConfigCore->expects($this->once())
  173. ->method('setValueMixed')
  174. ->with(self::TEST_APPID, $key, $value);
  175. $this->appConfig->setAppValue($key, $value);
  176. }
  177. /**
  178. * @return array
  179. * @see testSetAppValueString
  180. * @see testSetAppValueStringException
  181. * @see testSetAppValueInt
  182. * @see testSetAppValueIntException
  183. * @see testSetAppValueFloat
  184. * @see testSetAppValueFloatException
  185. * @see testSetAppValueArray
  186. * @see testSetAppValueArrayException
  187. */
  188. public function providerSetAppValue(): array {
  189. return [
  190. // lazy, sensitive, expected
  191. [false, false, true],
  192. [false, true, true],
  193. [true, true, true],
  194. [true, false, true],
  195. [false, false, false],
  196. [false, true, false],
  197. [true, true, false],
  198. [true, false, false],
  199. ];
  200. }
  201. /**
  202. * @dataProvider providerSetAppValue
  203. *
  204. * @param bool $lazy
  205. * @param bool $sensitive
  206. * @param bool $expected
  207. */
  208. public function testSetAppValueString(bool $lazy, bool $sensitive, bool $expected): void {
  209. $key = 'key';
  210. $value = 'valueString';
  211. $this->appConfigCore->expects($this->once())
  212. ->method('setValueString')
  213. ->with(self::TEST_APPID, $key, $value, $lazy, $sensitive)
  214. ->willReturn($expected);
  215. $this->assertSame($expected, $this->appConfig->setAppValueString($key, $value, $lazy, $sensitive));
  216. }
  217. /**
  218. * @dataProvider providerSetAppValue
  219. *
  220. * @param bool $lazy
  221. * @param bool $sensitive
  222. */
  223. public function testSetAppValueStringException(bool $lazy, bool $sensitive): void {
  224. $key = 'key';
  225. $value = 'valueString';
  226. $this->appConfigCore->expects($this->once())
  227. ->method('setValueString')
  228. ->with(self::TEST_APPID, $key, $value, $lazy, $sensitive)
  229. ->willThrowException(new AppConfigTypeConflictException());
  230. $this->expectException(AppConfigTypeConflictException::class);
  231. $this->appConfig->setAppValueString($key, $value, $lazy, $sensitive);
  232. }
  233. /**
  234. * @dataProvider providerSetAppValue
  235. *
  236. * @param bool $lazy
  237. * @param bool $sensitive
  238. * @param bool $expected
  239. */
  240. public function testSetAppValueInt(bool $lazy, bool $sensitive, bool $expected): void {
  241. $key = 'key';
  242. $value = 42;
  243. $this->appConfigCore->expects($this->once())
  244. ->method('setValueInt')
  245. ->with(self::TEST_APPID, $key, $value, $lazy, $sensitive)
  246. ->willReturn($expected);
  247. $this->assertSame($expected, $this->appConfig->setAppValueInt($key, $value, $lazy, $sensitive));
  248. }
  249. /**
  250. * @dataProvider providerSetAppValue
  251. *
  252. * @param bool $lazy
  253. * @param bool $sensitive
  254. */
  255. public function testSetAppValueIntException(bool $lazy, bool $sensitive): void {
  256. $key = 'key';
  257. $value = 42;
  258. $this->appConfigCore->expects($this->once())
  259. ->method('setValueInt')
  260. ->with(self::TEST_APPID, $key, $value, $lazy, $sensitive)
  261. ->willThrowException(new AppConfigTypeConflictException());
  262. $this->expectException(AppConfigTypeConflictException::class);
  263. $this->appConfig->setAppValueInt($key, $value, $lazy, $sensitive);
  264. }
  265. /**
  266. * @dataProvider providerSetAppValue
  267. *
  268. * @param bool $lazy
  269. * @param bool $sensitive
  270. * @param bool $expected
  271. */
  272. public function testSetAppValueFloat(bool $lazy, bool $sensitive, bool $expected): void {
  273. $key = 'key';
  274. $value = 3.14;
  275. $this->appConfigCore->expects($this->once())
  276. ->method('setValueFloat')
  277. ->with(self::TEST_APPID, $key, $value, $lazy, $sensitive)
  278. ->willReturn($expected);
  279. $this->assertSame($expected, $this->appConfig->setAppValueFloat($key, $value, $lazy, $sensitive));
  280. }
  281. /**
  282. * @dataProvider providerSetAppValue
  283. *
  284. * @param bool $lazy
  285. * @param bool $sensitive
  286. */
  287. public function testSetAppValueFloatException(bool $lazy, bool $sensitive): void {
  288. $key = 'key';
  289. $value = 3.14;
  290. $this->appConfigCore->expects($this->once())
  291. ->method('setValueFloat')
  292. ->with(self::TEST_APPID, $key, $value, $lazy, $sensitive)
  293. ->willThrowException(new AppConfigTypeConflictException());
  294. $this->expectException(AppConfigTypeConflictException::class);
  295. $this->appConfig->setAppValueFloat($key, $value, $lazy, $sensitive);
  296. }
  297. /**
  298. * @return array
  299. * @see testSetAppValueBool
  300. */
  301. public function providerSetAppValueBool(): array {
  302. return [
  303. // lazy, expected
  304. [false, true],
  305. [false, false],
  306. [true, true],
  307. [true, false],
  308. ];
  309. }
  310. /**
  311. * @dataProvider providerSetAppValueBool
  312. *
  313. * @param bool $lazy
  314. * @param bool $expected
  315. */
  316. public function testSetAppValueBool(bool $lazy, bool $expected): void {
  317. $key = 'key';
  318. $value = true;
  319. $this->appConfigCore->expects($this->once())
  320. ->method('setValueBool')
  321. ->with(self::TEST_APPID, $key, $value, $lazy)
  322. ->willReturn($expected);
  323. $this->assertSame($expected, $this->appConfig->setAppValueBool($key, $value, $lazy));
  324. }
  325. /**
  326. * @dataProvider providerSetAppValueBool
  327. *
  328. * @param bool $lazy
  329. */
  330. public function testSetAppValueBoolException(bool $lazy): void {
  331. $key = 'key';
  332. $value = true;
  333. $this->appConfigCore->expects($this->once())
  334. ->method('setValueBool')
  335. ->with(self::TEST_APPID, $key, $value, $lazy)
  336. ->willThrowException(new AppConfigTypeConflictException());
  337. $this->expectException(AppConfigTypeConflictException::class);
  338. $this->appConfig->setAppValueBool($key, $value, $lazy);
  339. }
  340. /**
  341. * @dataProvider providerSetAppValue
  342. *
  343. * @param bool $lazy
  344. * @param bool $sensitive
  345. * @param bool $expected
  346. */
  347. public function testSetAppValueArray(bool $lazy, bool $sensitive, bool $expected): void {
  348. $key = 'key';
  349. $value = ['item' => true];
  350. $this->appConfigCore->expects($this->once())
  351. ->method('setValueArray')
  352. ->with(self::TEST_APPID, $key, $value, $lazy, $sensitive)
  353. ->willReturn($expected);
  354. $this->assertSame($expected, $this->appConfig->setAppValueArray($key, $value, $lazy, $sensitive));
  355. }
  356. /**
  357. * @dataProvider providerSetAppValue
  358. *
  359. * @param bool $lazy
  360. * @param bool $sensitive
  361. */
  362. public function testSetAppValueArrayException(bool $lazy, bool $sensitive): void {
  363. $key = 'key';
  364. $value = ['item' => true];
  365. $this->appConfigCore->expects($this->once())
  366. ->method('setValueArray')
  367. ->with(self::TEST_APPID, $key, $value, $lazy, $sensitive)
  368. ->willThrowException(new AppConfigTypeConflictException());
  369. $this->expectException(AppConfigTypeConflictException::class);
  370. $this->appConfig->setAppValueArray($key, $value, $lazy, $sensitive);
  371. }
  372. public function testGetAppValue(): void {
  373. $key = 'key';
  374. $value = 'value';
  375. $default = 'default';
  376. $this->appConfigCore->expects($this->once())
  377. ->method('getValueMixed')
  378. ->with(self::TEST_APPID, $key, $default)
  379. ->willReturn($value);
  380. $this->assertSame($value, $this->appConfig->getAppValue($key, $default));
  381. }
  382. public function testGetAppValueDefault(): void {
  383. $key = 'key';
  384. $default = 'default';
  385. $this->appConfigCore->expects($this->once())
  386. ->method('getValueMixed')
  387. ->with(self::TEST_APPID, $key, $default)
  388. ->willReturn($default);
  389. $this->assertSame($default, $this->appConfig->getAppValue($key, $default));
  390. }
  391. /**
  392. * @return array
  393. * @see testGetAppValueString
  394. * @see testGetAppValueStringException
  395. * @see testGetAppValueInt
  396. * @see testGetAppValueIntException
  397. * @see testGetAppValueFloat
  398. * @see testGetAppValueFloatException
  399. * @see testGetAppValueBool
  400. * @see testGetAppValueBoolException
  401. * @see testGetAppValueArray
  402. * @see testGetAppValueArrayException
  403. */
  404. public function providerGetAppValue(): array {
  405. return [
  406. // lazy, exist
  407. [false, false],
  408. [false, true],
  409. [true, true],
  410. [true, false]
  411. ];
  412. }
  413. /**
  414. * @dataProvider providerGetAppValue
  415. *
  416. * @param bool $lazy
  417. * @param bool $exist
  418. */
  419. public function testGetAppValueString(bool $lazy, bool $exist): void {
  420. $key = 'key';
  421. $value = 'valueString';
  422. $default = 'default';
  423. $expected = ($exist) ? $value : $default;
  424. $this->appConfigCore->expects($this->once())
  425. ->method('getValueString')
  426. ->with(self::TEST_APPID, $key, $default, $lazy)
  427. ->willReturn($expected);
  428. $this->assertSame($expected, $this->appConfig->getAppValueString($key, $default, $lazy));
  429. }
  430. /**
  431. * @dataProvider providerGetAppValue
  432. *
  433. * @param bool $lazy
  434. */
  435. public function testGetAppValueStringException(bool $lazy): void {
  436. $key = 'key';
  437. $default = 'default';
  438. $this->appConfigCore->expects($this->once())
  439. ->method('getValueString')
  440. ->with(self::TEST_APPID, $key, $default, $lazy)
  441. ->willThrowException(new AppConfigTypeConflictException());
  442. $this->expectException(AppConfigTypeConflictException::class);
  443. $this->appConfig->getAppValueString($key, $default, $lazy);
  444. }
  445. /**
  446. * @dataProvider providerGetAppValue
  447. *
  448. * @param bool $lazy
  449. * @param bool $exist
  450. */
  451. public function testGetAppValueInt(bool $lazy, bool $exist): void {
  452. $key = 'key';
  453. $value = 42;
  454. $default = 17;
  455. $expected = ($exist) ? $value : $default;
  456. $this->appConfigCore->expects($this->once())
  457. ->method('getValueInt')
  458. ->with(self::TEST_APPID, $key, $default, $lazy)
  459. ->willReturn($expected);
  460. $this->assertSame($expected, $this->appConfig->getAppValueInt($key, $default, $lazy));
  461. }
  462. /**
  463. * @dataProvider providerGetAppValue
  464. *
  465. * @param bool $lazy
  466. */
  467. public function testGetAppValueIntException(bool $lazy): void {
  468. $key = 'key';
  469. $default = 17;
  470. $this->appConfigCore->expects($this->once())
  471. ->method('getValueInt')
  472. ->with(self::TEST_APPID, $key, $default, $lazy)
  473. ->willThrowException(new AppConfigTypeConflictException());
  474. $this->expectException(AppConfigTypeConflictException::class);
  475. $this->appConfig->getAppValueInt($key, $default, $lazy);
  476. }
  477. /**
  478. * @dataProvider providerGetAppValue
  479. *
  480. * @param bool $lazy
  481. * @param bool $exist
  482. */
  483. public function testGetAppValueFloat(bool $lazy, bool $exist): void {
  484. $key = 'key';
  485. $value = 3.14;
  486. $default = 17.04;
  487. $expected = ($exist) ? $value : $default;
  488. $this->appConfigCore->expects($this->once())
  489. ->method('getValueFloat')
  490. ->with(self::TEST_APPID, $key, $default, $lazy)
  491. ->willReturn($expected);
  492. $this->assertSame($expected, $this->appConfig->getAppValueFloat($key, $default, $lazy));
  493. }
  494. /**
  495. * @dataProvider providerGetAppValue
  496. *
  497. * @param bool $lazy
  498. */
  499. public function testGetAppValueFloatException(bool $lazy): void {
  500. $key = 'key';
  501. $default = 17.04;
  502. $this->appConfigCore->expects($this->once())
  503. ->method('getValueFloat')
  504. ->with(self::TEST_APPID, $key, $default, $lazy)
  505. ->willThrowException(new AppConfigTypeConflictException());
  506. $this->expectException(AppConfigTypeConflictException::class);
  507. $this->appConfig->getAppValueFloat($key, $default, $lazy);
  508. }
  509. /**
  510. * @dataProvider providerGetAppValue
  511. *
  512. * @param bool $lazy
  513. * @param bool $exist
  514. */
  515. public function testGetAppValueBool(bool $lazy, bool $exist): void {
  516. $key = 'key';
  517. $value = true;
  518. $default = false;
  519. $expected = ($exist) ? $value : $default; // yes, it can be simplified
  520. $this->appConfigCore->expects($this->once())
  521. ->method('getValueBool')
  522. ->with(self::TEST_APPID, $key, $default, $lazy)
  523. ->willReturn($expected);
  524. $this->assertSame($expected, $this->appConfig->getAppValueBool($key, $default, $lazy));
  525. }
  526. /**
  527. * @dataProvider providerGetAppValue
  528. *
  529. * @param bool $lazy
  530. */
  531. public function testGetAppValueBoolException(bool $lazy): void {
  532. $key = 'key';
  533. $default = false;
  534. $this->appConfigCore->expects($this->once())
  535. ->method('getValueBool')
  536. ->with(self::TEST_APPID, $key, $default, $lazy)
  537. ->willThrowException(new AppConfigTypeConflictException());
  538. $this->expectException(AppConfigTypeConflictException::class);
  539. $this->appConfig->getAppValueBool($key, $default, $lazy);
  540. }
  541. /**
  542. * @dataProvider providerGetAppValue
  543. *
  544. * @param bool $lazy
  545. * @param bool $exist
  546. */
  547. public function testGetAppValueArray(bool $lazy, bool $exist): void {
  548. $key = 'key';
  549. $value = ['item' => true];
  550. $default = [];
  551. $expected = ($exist) ? $value : $default;
  552. $this->appConfigCore->expects($this->once())
  553. ->method('getValueArray')
  554. ->with(self::TEST_APPID, $key, $default, $lazy)
  555. ->willReturn($expected);
  556. $this->assertSame($expected, $this->appConfig->getAppValueArray($key, $default, $lazy));
  557. }
  558. /**
  559. * @dataProvider providerGetAppValue
  560. *
  561. * @param bool $lazy
  562. */
  563. public function testGetAppValueArrayException(bool $lazy): void {
  564. $key = 'key';
  565. $default = [];
  566. $this->appConfigCore->expects($this->once())
  567. ->method('getValueArray')
  568. ->with(self::TEST_APPID, $key, $default, $lazy)
  569. ->willThrowException(new AppConfigTypeConflictException());
  570. $this->expectException(AppConfigTypeConflictException::class);
  571. $this->appConfig->getAppValueArray($key, $default, $lazy);
  572. }
  573. public function testDeleteAppValue(): void {
  574. $key = 'key';
  575. $this->appConfigCore->expects($this->once())
  576. ->method('deleteKey')
  577. ->with(self::TEST_APPID, $key);
  578. $this->appConfig->deleteAppValue($key);
  579. }
  580. public function testDeleteAppValues(): void {
  581. $this->appConfigCore->expects($this->once())
  582. ->method('deleteApp')
  583. ->with(self::TEST_APPID);
  584. $this->appConfig->deleteAppValues();
  585. }
  586. }