AllConfigTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  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() {
  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() {
  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() {
  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) {
  113. $this->expectException(\UnexpectedValueException::class);
  114. $config = $this->getConfig();
  115. $config->setUserValue('userSetBool', 'appSetBool', 'keySetBool', $value);
  116. }
  117. public function testSetUserValueWithPreConditionFailure() {
  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 testSetUserValueUnchanged() {
  144. // TODO - FIXME until the dependency injection is handled properly (in AllConfig)
  145. $this->markTestSkipped('Skipped because this is just testable if database connection can be injected');
  146. $resultMock = $this->getMockBuilder('\Doctrine\DBAL\Driver\Statement')
  147. ->disableOriginalConstructor()->getMock();
  148. $resultMock->expects($this->once())
  149. ->method('fetchColumn')
  150. ->willReturn('valueSetUnchanged');
  151. $connectionMock = $this->createMock(IDBConnection::class);
  152. $connectionMock->expects($this->once())
  153. ->method('executeQuery')
  154. ->with($this->equalTo('SELECT `configvalue` FROM `*PREFIX*preferences` '.
  155. 'WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?'),
  156. $this->equalTo(['userSetUnchanged', 'appSetUnchanged', 'keySetUnchanged']))
  157. ->willReturn($resultMock);
  158. $connectionMock->expects($this->never())
  159. ->method('executeUpdate');
  160. $config = $this->getConfig(null, $connectionMock);
  161. $config->setUserValue('userSetUnchanged', 'appSetUnchanged', 'keySetUnchanged', 'valueSetUnchanged');
  162. }
  163. public function testGetUserValue() {
  164. $config = $this->getConfig();
  165. // setup - it therefore relies on the successful execution of the previous test
  166. $config->setUserValue('userGet', 'appGet', 'keyGet', 'valueGet');
  167. $value = $config->getUserValue('userGet', 'appGet', 'keyGet');
  168. $this->assertEquals('valueGet', $value);
  169. $result = $this->connection->executeQuery(
  170. 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?',
  171. ['userGet']
  172. )->fetchAll();
  173. $this->assertEquals(1, count($result));
  174. $this->assertEquals([
  175. 'userid' => 'userGet',
  176. 'appid' => 'appGet',
  177. 'configkey' => 'keyGet',
  178. 'configvalue' => 'valueGet'
  179. ], $result[0]);
  180. // drop data from database - but the config option should be cached in the config object
  181. $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences` WHERE `userid` = ?', ['userGet']);
  182. // testing the caching mechanism
  183. $value = $config->getUserValue('userGet', 'appGet', 'keyGet');
  184. $this->assertEquals('valueGet', $value);
  185. $result = $this->connection->executeQuery(
  186. 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?',
  187. ['userGet']
  188. )->fetchAll();
  189. $this->assertEquals(0, count($result));
  190. }
  191. public function testGetUserKeys() {
  192. $config = $this->getConfig();
  193. // preparation - add something to the database
  194. $data = [
  195. ['userFetch', 'appFetch1', 'keyFetch1', 'value1'],
  196. ['userFetch', 'appFetch1', 'keyFetch2', 'value2'],
  197. ['userFetch', 'appFetch2', 'keyFetch3', 'value3'],
  198. ['userFetch', 'appFetch1', 'keyFetch4', 'value4'],
  199. ['userFetch', 'appFetch4', 'keyFetch1', 'value5'],
  200. ['userFetch', 'appFetch5', 'keyFetch1', 'value6'],
  201. ['userFetch2', 'appFetch', 'keyFetch1', 'value7']
  202. ];
  203. foreach ($data as $entry) {
  204. $this->connection->executeUpdate(
  205. 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' .
  206. '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)',
  207. $entry
  208. );
  209. }
  210. $value = $config->getUserKeys('userFetch', 'appFetch1');
  211. $this->assertEquals(['keyFetch1', 'keyFetch2', 'keyFetch4'], $value);
  212. $value = $config->getUserKeys('userFetch2', 'appFetch');
  213. $this->assertEquals(['keyFetch1'], $value);
  214. // cleanup
  215. $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`');
  216. }
  217. public function testGetUserKeysAllInts() {
  218. $config = $this->getConfig();
  219. // preparation - add something to the database
  220. $data = [
  221. ['userFetch', 'appFetch1', '123', 'value'],
  222. ['userFetch', 'appFetch1', '456', 'value'],
  223. ];
  224. foreach ($data as $entry) {
  225. $this->connection->executeUpdate(
  226. 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' .
  227. '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)',
  228. $entry
  229. );
  230. }
  231. $value = $config->getUserKeys('userFetch', 'appFetch1');
  232. $this->assertEquals(['123', '456'], $value);
  233. $this->assertIsString($value[0]);
  234. $this->assertIsString($value[1]);
  235. // cleanup
  236. $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`');
  237. }
  238. public function testGetUserValueDefault() {
  239. $config = $this->getConfig();
  240. $this->assertEquals('', $config->getUserValue('userGetUnset', 'appGetUnset', 'keyGetUnset'));
  241. $this->assertEquals(null, $config->getUserValue('userGetUnset', 'appGetUnset', 'keyGetUnset', null));
  242. $this->assertEquals('foobar', $config->getUserValue('userGetUnset', 'appGetUnset', 'keyGetUnset', 'foobar'));
  243. }
  244. public function testGetUserValueForUsers() {
  245. $config = $this->getConfig();
  246. // preparation - add something to the database
  247. $data = [
  248. ['userFetch1', 'appFetch2', 'keyFetch1', 'value1'],
  249. ['userFetch2', 'appFetch2', 'keyFetch1', 'value2'],
  250. ['userFetch3', 'appFetch2', 'keyFetch1', 3],
  251. ['userFetch4', 'appFetch2', 'keyFetch1', 'value4'],
  252. ['userFetch5', 'appFetch2', 'keyFetch1', 'value5'],
  253. ['userFetch6', 'appFetch2', 'keyFetch1', 'value6'],
  254. ['userFetch7', 'appFetch2', 'keyFetch1', 'value7']
  255. ];
  256. foreach ($data as $entry) {
  257. $this->connection->executeUpdate(
  258. 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' .
  259. '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)',
  260. $entry
  261. );
  262. }
  263. $value = $config->getUserValueForUsers('appFetch2', 'keyFetch1',
  264. ['userFetch1', 'userFetch2', 'userFetch3', 'userFetch5']);
  265. $this->assertEquals([
  266. 'userFetch1' => 'value1',
  267. 'userFetch2' => 'value2',
  268. 'userFetch3' => 3,
  269. 'userFetch5' => 'value5'
  270. ], $value);
  271. $value = $config->getUserValueForUsers('appFetch2', 'keyFetch1',
  272. ['userFetch1', 'userFetch4', 'userFetch9']);
  273. $this->assertEquals([
  274. 'userFetch1' => 'value1',
  275. 'userFetch4' => 'value4'
  276. ], $value, 'userFetch9 is an non-existent user and should not be shown.');
  277. // cleanup
  278. $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`');
  279. }
  280. public function testDeleteAllUserValues() {
  281. $config = $this->getConfig();
  282. // preparation - add something to the database
  283. $data = [
  284. ['userFetch3', 'appFetch1', 'keyFetch1', 'value1'],
  285. ['userFetch3', 'appFetch1', 'keyFetch2', 'value2'],
  286. ['userFetch3', 'appFetch2', 'keyFetch3', 'value3'],
  287. ['userFetch3', 'appFetch1', 'keyFetch4', 'value4'],
  288. ['userFetch3', 'appFetch4', 'keyFetch1', 'value5'],
  289. ['userFetch3', 'appFetch5', 'keyFetch1', 'value6'],
  290. ['userFetch4', 'appFetch2', 'keyFetch1', 'value7']
  291. ];
  292. foreach ($data as $entry) {
  293. $this->connection->executeUpdate(
  294. 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' .
  295. '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)',
  296. $entry
  297. );
  298. }
  299. $config->deleteAllUserValues('userFetch3');
  300. $result = $this->connection->executeQuery(
  301. 'SELECT COUNT(*) AS `count` FROM `*PREFIX*preferences`'
  302. )->fetch();
  303. $actualCount = $result['count'];
  304. $this->assertEquals(1, $actualCount, 'After removing `userFetch3` there should be exactly 1 entry left.');
  305. // cleanup
  306. $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`');
  307. }
  308. public function testDeleteAppFromAllUsers() {
  309. $config = $this->getConfig();
  310. // preparation - add something to the database
  311. $data = [
  312. ['userFetch5', 'appFetch1', 'keyFetch1', 'value1'],
  313. ['userFetch5', 'appFetch1', 'keyFetch2', 'value2'],
  314. ['userFetch5', 'appFetch2', 'keyFetch3', 'value3'],
  315. ['userFetch5', 'appFetch1', 'keyFetch4', 'value4'],
  316. ['userFetch5', 'appFetch4', 'keyFetch1', 'value5'],
  317. ['userFetch5', 'appFetch5', 'keyFetch1', 'value6'],
  318. ['userFetch6', 'appFetch2', 'keyFetch1', 'value7']
  319. ];
  320. foreach ($data as $entry) {
  321. $this->connection->executeUpdate(
  322. 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' .
  323. '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)',
  324. $entry
  325. );
  326. }
  327. $config->deleteAppFromAllUsers('appFetch1');
  328. $result = $this->connection->executeQuery(
  329. 'SELECT COUNT(*) AS `count` FROM `*PREFIX*preferences`'
  330. )->fetch();
  331. $actualCount = $result['count'];
  332. $this->assertEquals(4, $actualCount, 'After removing `appFetch1` there should be exactly 4 entries left.');
  333. $config->deleteAppFromAllUsers('appFetch2');
  334. $result = $this->connection->executeQuery(
  335. 'SELECT COUNT(*) AS `count` FROM `*PREFIX*preferences`'
  336. )->fetch();
  337. $actualCount = $result['count'];
  338. $this->assertEquals(2, $actualCount, 'After removing `appFetch2` there should be exactly 2 entries left.');
  339. // cleanup
  340. $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`');
  341. }
  342. public function testGetUsersForUserValue() {
  343. // mock the check for the database to run the correct SQL statements for each database type
  344. $systemConfig = $this->getMockBuilder('\OC\SystemConfig')
  345. ->disableOriginalConstructor()
  346. ->getMock();
  347. $config = $this->getConfig($systemConfig);
  348. // preparation - add something to the database
  349. $data = [
  350. ['user1', 'appFetch9', 'keyFetch9', 'value9'],
  351. ['user2', 'appFetch9', 'keyFetch9', 'value9'],
  352. ['user3', 'appFetch9', 'keyFetch9', 'value8'],
  353. ['user4', 'appFetch9', 'keyFetch8', 'value9'],
  354. ['user5', 'appFetch8', 'keyFetch9', 'value9'],
  355. ['user6', 'appFetch9', 'keyFetch9', 'value9'],
  356. ];
  357. foreach ($data as $entry) {
  358. $this->connection->executeUpdate(
  359. 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' .
  360. '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)',
  361. $entry
  362. );
  363. }
  364. $value = $config->getUsersForUserValue('appFetch9', 'keyFetch9', 'value9');
  365. $this->assertEquals(['user1', 'user2', 'user6'], $value);
  366. // cleanup
  367. $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`');
  368. }
  369. public function testGetUsersForUserValueCaseInsensitive() {
  370. // mock the check for the database to run the correct SQL statements for each database type
  371. $systemConfig = $this->createMock(SystemConfig::class);
  372. $config = $this->getConfig($systemConfig);
  373. $config->setUserValue('user1', 'myApp', 'myKey', 'test123');
  374. $config->setUserValue('user2', 'myApp', 'myKey', 'TEST123');
  375. $config->setUserValue('user3', 'myApp', 'myKey', 'test12345');
  376. $users = $config->getUsersForUserValueCaseInsensitive('myApp', 'myKey', 'test123');
  377. $this->assertSame(2, count($users));
  378. $this->assertSame(['user1', 'user2'], $users);
  379. }
  380. }