AppConfigTest.php 18 KB

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