AllConfigTest.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test;
  8. /**
  9. * Class AllConfigTest
  10. *
  11. * @group DB
  12. *
  13. * @package Test
  14. */
  15. use OC\SystemConfig;
  16. use OCP\IDBConnection;
  17. class AllConfigTest extends \Test\TestCase {
  18. /** @var \OCP\IDBConnection */
  19. protected $connection;
  20. protected function getConfig($systemConfig = null, $connection = null) {
  21. if ($this->connection === null) {
  22. $this->connection = \OC::$server->getDatabaseConnection();
  23. }
  24. if ($connection === null) {
  25. $connection = $this->connection;
  26. }
  27. if ($systemConfig === null) {
  28. $systemConfig = $this->getMockBuilder('\OC\SystemConfig')
  29. ->disableOriginalConstructor()
  30. ->getMock();
  31. }
  32. return new \OC\AllConfig($systemConfig, $connection);
  33. }
  34. public function testDeleteUserValue(): void {
  35. $config = $this->getConfig();
  36. // preparation - add something to the database
  37. $this->connection->executeUpdate(
  38. 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' .
  39. '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)',
  40. ['userDelete', 'appDelete', 'keyDelete', 'valueDelete']
  41. );
  42. $config->deleteUserValue('userDelete', 'appDelete', 'keyDelete');
  43. $result = $this->connection->executeQuery(
  44. 'SELECT COUNT(*) AS `count` FROM `*PREFIX*preferences` WHERE `userid` = ?',
  45. ['userDelete']
  46. )->fetch();
  47. $actualCount = $result['count'];
  48. $this->assertEquals(0, $actualCount, 'There was one value in the database and after the tests there should be no entry left.');
  49. }
  50. public function testSetUserValue(): void {
  51. $selectAllSQL = 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?';
  52. $config = $this->getConfig();
  53. $config->setUserValue('userSet', 'appSet', 'keySet', 'valueSet');
  54. $result = $this->connection->executeQuery($selectAllSQL, ['userSet'])->fetchAll();
  55. $this->assertEquals(1, count($result));
  56. $this->assertEquals([
  57. 'userid' => 'userSet',
  58. 'appid' => 'appSet',
  59. 'configkey' => 'keySet',
  60. 'configvalue' => 'valueSet'
  61. ], $result[0]);
  62. // test if the method overwrites existing database entries
  63. $config->setUserValue('userSet', 'appSet', 'keySet', 'valueSet2');
  64. $result = $this->connection->executeQuery($selectAllSQL, ['userSet'])->fetchAll();
  65. $this->assertEquals(1, count($result));
  66. $this->assertEquals([
  67. 'userid' => 'userSet',
  68. 'appid' => 'appSet',
  69. 'configkey' => 'keySet',
  70. 'configvalue' => 'valueSet2'
  71. ], $result[0]);
  72. // cleanup - it therefore relies on the successful execution of the previous test
  73. $config->deleteUserValue('userSet', 'appSet', 'keySet');
  74. }
  75. public function testSetUserValueWithPreCondition(): void {
  76. $config = $this->getConfig();
  77. $selectAllSQL = 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?';
  78. $config->setUserValue('userPreCond', 'appPreCond', 'keyPreCond', 'valuePreCond');
  79. $result = $this->connection->executeQuery($selectAllSQL, ['userPreCond'])->fetchAll();
  80. $this->assertEquals(1, count($result));
  81. $this->assertEquals([
  82. 'userid' => 'userPreCond',
  83. 'appid' => 'appPreCond',
  84. 'configkey' => 'keyPreCond',
  85. 'configvalue' => 'valuePreCond'
  86. ], $result[0]);
  87. // test if the method overwrites existing database entries with valid precond
  88. $config->setUserValue('userPreCond', 'appPreCond', 'keyPreCond', 'valuePreCond2', 'valuePreCond');
  89. $result = $this->connection->executeQuery($selectAllSQL, ['userPreCond'])->fetchAll();
  90. $this->assertEquals(1, count($result));
  91. $this->assertEquals([
  92. 'userid' => 'userPreCond',
  93. 'appid' => 'appPreCond',
  94. 'configkey' => 'keyPreCond',
  95. 'configvalue' => 'valuePreCond2'
  96. ], $result[0]);
  97. // cleanup
  98. $config->deleteUserValue('userPreCond', 'appPreCond', 'keyPreCond');
  99. }
  100. public function dataSetUserValueUnexpectedValue() {
  101. return [
  102. [true],
  103. [false],
  104. [null],
  105. [new \stdClass()],
  106. ];
  107. }
  108. /**
  109. * @dataProvider dataSetUserValueUnexpectedValue
  110. * @param mixed $value
  111. */
  112. public function testSetUserValueUnexpectedValue($value): void {
  113. $this->expectException(\UnexpectedValueException::class);
  114. $config = $this->getConfig();
  115. $config->setUserValue('userSetBool', 'appSetBool', 'keySetBool', $value);
  116. }
  117. public function testSetUserValueWithPreConditionFailure(): void {
  118. $this->expectException(\OCP\PreConditionNotMetException::class);
  119. $config = $this->getConfig();
  120. $selectAllSQL = 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?';
  121. $config->setUserValue('userPreCond1', 'appPreCond', 'keyPreCond', 'valuePreCond');
  122. $result = $this->connection->executeQuery($selectAllSQL, ['userPreCond1'])->fetchAll();
  123. $this->assertEquals(1, count($result));
  124. $this->assertEquals([
  125. 'userid' => 'userPreCond1',
  126. 'appid' => 'appPreCond',
  127. 'configkey' => 'keyPreCond',
  128. 'configvalue' => 'valuePreCond'
  129. ], $result[0]);
  130. // test if the method overwrites existing database entries with valid precond
  131. $config->setUserValue('userPreCond1', 'appPreCond', 'keyPreCond', 'valuePreCond2', 'valuePreCond3');
  132. $result = $this->connection->executeQuery($selectAllSQL, ['userPreCond1'])->fetchAll();
  133. $this->assertEquals(1, count($result));
  134. $this->assertEquals([
  135. 'userid' => 'userPreCond1',
  136. 'appid' => 'appPreCond',
  137. 'configkey' => 'keyPreCond',
  138. 'configvalue' => 'valuePreCond'
  139. ], $result[0]);
  140. // cleanup
  141. $config->deleteUserValue('userPreCond1', 'appPreCond', 'keyPreCond');
  142. }
  143. public function testSetUserValueWithPreConditionFailureWhenResultStillMatches(): void {
  144. $this->expectException(\OCP\PreConditionNotMetException::class);
  145. $config = $this->getConfig();
  146. $selectAllSQL = 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?';
  147. $config->setUserValue('userPreCond1', 'appPreCond', 'keyPreCond', 'valuePreCond');
  148. $result = $this->connection->executeQuery($selectAllSQL, ['userPreCond1'])->fetchAll();
  149. $this->assertCount(1, $result);
  150. $this->assertEquals([
  151. 'userid' => 'userPreCond1',
  152. 'appid' => 'appPreCond',
  153. 'configkey' => 'keyPreCond',
  154. 'configvalue' => 'valuePreCond'
  155. ], $result[0]);
  156. // test if the method throws with invalid precondition when the value is the same
  157. $config->setUserValue('userPreCond1', 'appPreCond', 'keyPreCond', 'valuePreCond', 'valuePreCond3');
  158. $result = $this->connection->executeQuery($selectAllSQL, ['userPreCond1'])->fetchAll();
  159. $this->assertCount(1, $result);
  160. $this->assertEquals([
  161. 'userid' => 'userPreCond1',
  162. 'appid' => 'appPreCond',
  163. 'configkey' => 'keyPreCond',
  164. 'configvalue' => 'valuePreCond'
  165. ], $result[0]);
  166. // cleanup
  167. $config->deleteUserValue('userPreCond1', 'appPreCond', 'keyPreCond');
  168. }
  169. public function testSetUserValueUnchanged(): void {
  170. // TODO - FIXME until the dependency injection is handled properly (in AllConfig)
  171. $this->markTestSkipped('Skipped because this is just testable if database connection can be injected');
  172. $resultMock = $this->getMockBuilder('\Doctrine\DBAL\Driver\Statement')
  173. ->disableOriginalConstructor()->getMock();
  174. $resultMock->expects($this->once())
  175. ->method('fetchColumn')
  176. ->willReturn('valueSetUnchanged');
  177. $connectionMock = $this->createMock(IDBConnection::class);
  178. $connectionMock->expects($this->once())
  179. ->method('executeQuery')
  180. ->with($this->equalTo('SELECT `configvalue` FROM `*PREFIX*preferences` '.
  181. 'WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?'),
  182. $this->equalTo(['userSetUnchanged', 'appSetUnchanged', 'keySetUnchanged']))
  183. ->willReturn($resultMock);
  184. $connectionMock->expects($this->never())
  185. ->method('executeUpdate');
  186. $config = $this->getConfig(null, $connectionMock);
  187. $config->setUserValue('userSetUnchanged', 'appSetUnchanged', 'keySetUnchanged', 'valueSetUnchanged');
  188. }
  189. public function testGetUserValue(): void {
  190. $config = $this->getConfig();
  191. // setup - it therefore relies on the successful execution of the previous test
  192. $config->setUserValue('userGet', 'appGet', 'keyGet', 'valueGet');
  193. $value = $config->getUserValue('userGet', 'appGet', 'keyGet');
  194. $this->assertEquals('valueGet', $value);
  195. $result = $this->connection->executeQuery(
  196. 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?',
  197. ['userGet']
  198. )->fetchAll();
  199. $this->assertEquals(1, count($result));
  200. $this->assertEquals([
  201. 'userid' => 'userGet',
  202. 'appid' => 'appGet',
  203. 'configkey' => 'keyGet',
  204. 'configvalue' => 'valueGet'
  205. ], $result[0]);
  206. // drop data from database - but the config option should be cached in the config object
  207. $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences` WHERE `userid` = ?', ['userGet']);
  208. // testing the caching mechanism
  209. $value = $config->getUserValue('userGet', 'appGet', 'keyGet');
  210. $this->assertEquals('valueGet', $value);
  211. $result = $this->connection->executeQuery(
  212. 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?',
  213. ['userGet']
  214. )->fetchAll();
  215. $this->assertEquals(0, count($result));
  216. }
  217. public function testGetUserKeys(): void {
  218. $config = $this->getConfig();
  219. // preparation - add something to the database
  220. $data = [
  221. ['userFetch', 'appFetch1', 'keyFetch1', 'value1'],
  222. ['userFetch', 'appFetch1', 'keyFetch2', 'value2'],
  223. ['userFetch', 'appFetch2', 'keyFetch3', 'value3'],
  224. ['userFetch', 'appFetch1', 'keyFetch4', 'value4'],
  225. ['userFetch', 'appFetch4', 'keyFetch1', 'value5'],
  226. ['userFetch', 'appFetch5', 'keyFetch1', 'value6'],
  227. ['userFetch2', 'appFetch', 'keyFetch1', 'value7']
  228. ];
  229. foreach ($data as $entry) {
  230. $this->connection->executeUpdate(
  231. 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' .
  232. '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)',
  233. $entry
  234. );
  235. }
  236. $value = $config->getUserKeys('userFetch', 'appFetch1');
  237. $this->assertEquals(['keyFetch1', 'keyFetch2', 'keyFetch4'], $value);
  238. $value = $config->getUserKeys('userFetch2', 'appFetch');
  239. $this->assertEquals(['keyFetch1'], $value);
  240. // cleanup
  241. $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`');
  242. }
  243. public function testGetUserKeysAllInts(): void {
  244. $config = $this->getConfig();
  245. // preparation - add something to the database
  246. $data = [
  247. ['userFetch', 'appFetch1', '123', 'value'],
  248. ['userFetch', 'appFetch1', '456', 'value'],
  249. ];
  250. foreach ($data as $entry) {
  251. $this->connection->executeUpdate(
  252. 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' .
  253. '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)',
  254. $entry
  255. );
  256. }
  257. $value = $config->getUserKeys('userFetch', 'appFetch1');
  258. $this->assertEquals(['123', '456'], $value);
  259. $this->assertIsString($value[0]);
  260. $this->assertIsString($value[1]);
  261. // cleanup
  262. $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`');
  263. }
  264. public function testGetUserValueDefault(): void {
  265. $config = $this->getConfig();
  266. $this->assertEquals('', $config->getUserValue('userGetUnset', 'appGetUnset', 'keyGetUnset'));
  267. $this->assertEquals(null, $config->getUserValue('userGetUnset', 'appGetUnset', 'keyGetUnset', null));
  268. $this->assertEquals('foobar', $config->getUserValue('userGetUnset', 'appGetUnset', 'keyGetUnset', 'foobar'));
  269. }
  270. public function testGetUserValueForUsers(): void {
  271. $config = $this->getConfig();
  272. // preparation - add something to the database
  273. $data = [
  274. ['userFetch1', 'appFetch2', 'keyFetch1', 'value1'],
  275. ['userFetch2', 'appFetch2', 'keyFetch1', 'value2'],
  276. ['userFetch3', 'appFetch2', 'keyFetch1', 3],
  277. ['userFetch4', 'appFetch2', 'keyFetch1', 'value4'],
  278. ['userFetch5', 'appFetch2', 'keyFetch1', 'value5'],
  279. ['userFetch6', 'appFetch2', 'keyFetch1', 'value6'],
  280. ['userFetch7', 'appFetch2', 'keyFetch1', 'value7']
  281. ];
  282. foreach ($data as $entry) {
  283. $this->connection->executeUpdate(
  284. 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' .
  285. '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)',
  286. $entry
  287. );
  288. }
  289. $value = $config->getUserValueForUsers('appFetch2', 'keyFetch1',
  290. ['userFetch1', 'userFetch2', 'userFetch3', 'userFetch5']);
  291. $this->assertEquals([
  292. 'userFetch1' => 'value1',
  293. 'userFetch2' => 'value2',
  294. 'userFetch3' => 3,
  295. 'userFetch5' => 'value5'
  296. ], $value);
  297. $value = $config->getUserValueForUsers('appFetch2', 'keyFetch1',
  298. ['userFetch1', 'userFetch4', 'userFetch9']);
  299. $this->assertEquals([
  300. 'userFetch1' => 'value1',
  301. 'userFetch4' => 'value4'
  302. ], $value, 'userFetch9 is an non-existent user and should not be shown.');
  303. // cleanup
  304. $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`');
  305. }
  306. public function testDeleteAllUserValues(): void {
  307. $config = $this->getConfig();
  308. // preparation - add something to the database
  309. $data = [
  310. ['userFetch3', 'appFetch1', 'keyFetch1', 'value1'],
  311. ['userFetch3', 'appFetch1', 'keyFetch2', 'value2'],
  312. ['userFetch3', 'appFetch2', 'keyFetch3', 'value3'],
  313. ['userFetch3', 'appFetch1', 'keyFetch4', 'value4'],
  314. ['userFetch3', 'appFetch4', 'keyFetch1', 'value5'],
  315. ['userFetch3', 'appFetch5', 'keyFetch1', 'value6'],
  316. ['userFetch4', 'appFetch2', 'keyFetch1', 'value7']
  317. ];
  318. foreach ($data as $entry) {
  319. $this->connection->executeUpdate(
  320. 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' .
  321. '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)',
  322. $entry
  323. );
  324. }
  325. $config->deleteAllUserValues('userFetch3');
  326. $result = $this->connection->executeQuery(
  327. 'SELECT COUNT(*) AS `count` FROM `*PREFIX*preferences`'
  328. )->fetch();
  329. $actualCount = $result['count'];
  330. $this->assertEquals(1, $actualCount, 'After removing `userFetch3` there should be exactly 1 entry left.');
  331. // cleanup
  332. $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`');
  333. }
  334. public function testDeleteAppFromAllUsers(): void {
  335. $config = $this->getConfig();
  336. // preparation - add something to the database
  337. $data = [
  338. ['userFetch5', 'appFetch1', 'keyFetch1', 'value1'],
  339. ['userFetch5', 'appFetch1', 'keyFetch2', 'value2'],
  340. ['userFetch5', 'appFetch2', 'keyFetch3', 'value3'],
  341. ['userFetch5', 'appFetch1', 'keyFetch4', 'value4'],
  342. ['userFetch5', 'appFetch4', 'keyFetch1', 'value5'],
  343. ['userFetch5', 'appFetch5', 'keyFetch1', 'value6'],
  344. ['userFetch6', 'appFetch2', 'keyFetch1', 'value7']
  345. ];
  346. foreach ($data as $entry) {
  347. $this->connection->executeUpdate(
  348. 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' .
  349. '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)',
  350. $entry
  351. );
  352. }
  353. $config->deleteAppFromAllUsers('appFetch1');
  354. $result = $this->connection->executeQuery(
  355. 'SELECT COUNT(*) AS `count` FROM `*PREFIX*preferences`'
  356. )->fetch();
  357. $actualCount = $result['count'];
  358. $this->assertEquals(4, $actualCount, 'After removing `appFetch1` there should be exactly 4 entries left.');
  359. $config->deleteAppFromAllUsers('appFetch2');
  360. $result = $this->connection->executeQuery(
  361. 'SELECT COUNT(*) AS `count` FROM `*PREFIX*preferences`'
  362. )->fetch();
  363. $actualCount = $result['count'];
  364. $this->assertEquals(2, $actualCount, 'After removing `appFetch2` there should be exactly 2 entries left.');
  365. // cleanup
  366. $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`');
  367. }
  368. public function testGetUsersForUserValue(): void {
  369. // mock the check for the database to run the correct SQL statements for each database type
  370. $systemConfig = $this->getMockBuilder('\OC\SystemConfig')
  371. ->disableOriginalConstructor()
  372. ->getMock();
  373. $config = $this->getConfig($systemConfig);
  374. // preparation - add something to the database
  375. $data = [
  376. ['user1', 'appFetch9', 'keyFetch9', 'value9'],
  377. ['user2', 'appFetch9', 'keyFetch9', 'value9'],
  378. ['user3', 'appFetch9', 'keyFetch9', 'value8'],
  379. ['user4', 'appFetch9', 'keyFetch8', 'value9'],
  380. ['user5', 'appFetch8', 'keyFetch9', 'value9'],
  381. ['user6', 'appFetch9', 'keyFetch9', 'value9'],
  382. ];
  383. foreach ($data as $entry) {
  384. $this->connection->executeUpdate(
  385. 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' .
  386. '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)',
  387. $entry
  388. );
  389. }
  390. $value = $config->getUsersForUserValue('appFetch9', 'keyFetch9', 'value9');
  391. $this->assertEquals(['user1', 'user2', 'user6'], $value);
  392. // cleanup
  393. $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`');
  394. }
  395. public function testGetUsersForUserValueCaseInsensitive(): void {
  396. // mock the check for the database to run the correct SQL statements for each database type
  397. $systemConfig = $this->createMock(SystemConfig::class);
  398. $config = $this->getConfig($systemConfig);
  399. $config->setUserValue('user1', 'myApp', 'myKey', 'test123');
  400. $config->setUserValue('user2', 'myApp', 'myKey', 'TEST123');
  401. $config->setUserValue('user3', 'myApp', 'myKey', 'test12345');
  402. $users = $config->getUsersForUserValueCaseInsensitive('myApp', 'myKey', 'test123');
  403. $this->assertSame(2, count($users));
  404. $this->assertSame(['user1', 'user2'], $users);
  405. }
  406. }