1
0

AllConfigTest.php 17 KB

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