AllConfigTest.php 15 KB

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