allconfig.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  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. class TestAllConfig extends \Test\TestCase {
  10. /** @var \OCP\IDBConnection */
  11. protected $connection;
  12. protected function getConfig($systemConfig = null, $connection = null) {
  13. if($this->connection === null) {
  14. $this->connection = \OC::$server->getDatabaseConnection();
  15. }
  16. if($connection === null) {
  17. $connection = $this->connection;
  18. }
  19. if($systemConfig === null) {
  20. $systemConfig = $this->getMock('\OC\SystemConfig');
  21. }
  22. return new \OC\AllConfig($systemConfig, $connection);
  23. }
  24. public function testDeleteUserValue() {
  25. $config = $this->getConfig();
  26. // preparation - add something to the database
  27. $this->connection->executeUpdate(
  28. 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' .
  29. '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)',
  30. array('userDelete', 'appDelete', 'keyDelete', 'valueDelete')
  31. );
  32. $config->deleteUserValue('userDelete', 'appDelete', 'keyDelete');
  33. $result = $this->connection->executeQuery(
  34. 'SELECT COUNT(*) AS `count` FROM `*PREFIX*preferences` WHERE `userid` = ?',
  35. array('userDelete')
  36. )->fetch();
  37. $actualCount = $result['count'];
  38. $this->assertEquals(0, $actualCount, 'There was one value in the database and after the tests there should be no entry left.');
  39. }
  40. public function testSetUserValue() {
  41. $selectAllSQL = 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?';
  42. $config = $this->getConfig();
  43. $config->setUserValue('userSet', 'appSet', 'keySet', 'valueSet');
  44. $result = $this->connection->executeQuery($selectAllSQL, array('userSet'))->fetchAll();
  45. $this->assertEquals(1, count($result));
  46. $this->assertEquals(array(
  47. 'userid' => 'userSet',
  48. 'appid' => 'appSet',
  49. 'configkey' => 'keySet',
  50. 'configvalue' => 'valueSet'
  51. ), $result[0]);
  52. // test if the method overwrites existing database entries
  53. $config->setUserValue('userSet', 'appSet', 'keySet', 'valueSet2');
  54. $result = $this->connection->executeQuery($selectAllSQL, array('userSet'))->fetchAll();
  55. $this->assertEquals(1, count($result));
  56. $this->assertEquals(array(
  57. 'userid' => 'userSet',
  58. 'appid' => 'appSet',
  59. 'configkey' => 'keySet',
  60. 'configvalue' => 'valueSet2'
  61. ), $result[0]);
  62. // cleanup - it therefore relies on the successful execution of the previous test
  63. $config->deleteUserValue('userSet', 'appSet', 'keySet');
  64. }
  65. public function testSetUserValueWithPreCondition() {
  66. // mock the check for the database to run the correct SQL statements for each database type
  67. $systemConfig = $this->getMock('\OC\SystemConfig');
  68. $systemConfig->expects($this->once())
  69. ->method('getValue')
  70. ->with($this->equalTo('dbtype'),
  71. $this->equalTo('sqlite'))
  72. ->will($this->returnValue(\OC::$server->getConfig()->getSystemValue('dbtype', 'sqlite')));
  73. $config = $this->getConfig($systemConfig);
  74. $selectAllSQL = 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?';
  75. $config->setUserValue('userPreCond', 'appPreCond', 'keyPreCond', 'valuePreCond');
  76. $result = $this->connection->executeQuery($selectAllSQL, array('userPreCond'))->fetchAll();
  77. $this->assertEquals(1, count($result));
  78. $this->assertEquals(array(
  79. 'userid' => 'userPreCond',
  80. 'appid' => 'appPreCond',
  81. 'configkey' => 'keyPreCond',
  82. 'configvalue' => 'valuePreCond'
  83. ), $result[0]);
  84. // test if the method overwrites existing database entries with valid precond
  85. $config->setUserValue('userPreCond', 'appPreCond', 'keyPreCond', 'valuePreCond2', 'valuePreCond');
  86. $result = $this->connection->executeQuery($selectAllSQL, array('userPreCond'))->fetchAll();
  87. $this->assertEquals(1, count($result));
  88. $this->assertEquals(array(
  89. 'userid' => 'userPreCond',
  90. 'appid' => 'appPreCond',
  91. 'configkey' => 'keyPreCond',
  92. 'configvalue' => 'valuePreCond2'
  93. ), $result[0]);
  94. // cleanup
  95. $config->deleteUserValue('userPreCond', 'appPreCond', 'keyPreCond');
  96. }
  97. /**
  98. * @expectedException \OCP\PreConditionNotMetException
  99. */
  100. public function testSetUserValueWithPreConditionFailure() {
  101. // mock the check for the database to run the correct SQL statements for each database type
  102. $systemConfig = $this->getMock('\OC\SystemConfig');
  103. $systemConfig->expects($this->once())
  104. ->method('getValue')
  105. ->with($this->equalTo('dbtype'),
  106. $this->equalTo('sqlite'))
  107. ->will($this->returnValue(\OC::$server->getConfig()->getSystemValue('dbtype', 'sqlite')));
  108. $config = $this->getConfig($systemConfig);
  109. $selectAllSQL = 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?';
  110. $config->setUserValue('userPreCond1', 'appPreCond', 'keyPreCond', 'valuePreCond');
  111. $result = $this->connection->executeQuery($selectAllSQL, array('userPreCond1'))->fetchAll();
  112. $this->assertEquals(1, count($result));
  113. $this->assertEquals(array(
  114. 'userid' => 'userPreCond1',
  115. 'appid' => 'appPreCond',
  116. 'configkey' => 'keyPreCond',
  117. 'configvalue' => 'valuePreCond'
  118. ), $result[0]);
  119. // test if the method overwrites existing database entries with valid precond
  120. $config->setUserValue('userPreCond1', 'appPreCond', 'keyPreCond', 'valuePreCond2', 'valuePreCond3');
  121. $result = $this->connection->executeQuery($selectAllSQL, array('userPreCond1'))->fetchAll();
  122. $this->assertEquals(1, count($result));
  123. $this->assertEquals(array(
  124. 'userid' => 'userPreCond1',
  125. 'appid' => 'appPreCond',
  126. 'configkey' => 'keyPreCond',
  127. 'configvalue' => 'valuePreCond'
  128. ), $result[0]);
  129. // cleanup
  130. $config->deleteUserValue('userPreCond1', 'appPreCond', 'keyPreCond');
  131. }
  132. public function testSetUserValueUnchanged() {
  133. // TODO - FIXME until the dependency injection is handled properly (in AllConfig)
  134. $this->markTestSkipped('Skipped because this is just testable if database connection can be injected');
  135. $resultMock = $this->getMockBuilder('\Doctrine\DBAL\Driver\Statement')
  136. ->disableOriginalConstructor()->getMock();
  137. $resultMock->expects($this->once())
  138. ->method('fetchColumn')
  139. ->will($this->returnValue('valueSetUnchanged'));
  140. $connectionMock = $this->getMock('\OCP\IDBConnection');
  141. $connectionMock->expects($this->once())
  142. ->method('executeQuery')
  143. ->with($this->equalTo('SELECT `configvalue` FROM `*PREFIX*preferences` '.
  144. 'WHERE `userid` = ? AND `appid` = ? AND `configkey` = ?'),
  145. $this->equalTo(array('userSetUnchanged', 'appSetUnchanged', 'keySetUnchanged')))
  146. ->will($this->returnValue($resultMock));
  147. $connectionMock->expects($this->never())
  148. ->method('executeUpdate');
  149. $config = $this->getConfig(null, $connectionMock);
  150. $config->setUserValue('userSetUnchanged', 'appSetUnchanged', 'keySetUnchanged', 'valueSetUnchanged');
  151. }
  152. public function testGetUserValue() {
  153. $config = $this->getConfig();
  154. // setup - it therefore relies on the successful execution of the previous test
  155. $config->setUserValue('userGet', 'appGet', 'keyGet', 'valueGet');
  156. $value = $config->getUserValue('userGet', 'appGet', 'keyGet');
  157. $this->assertEquals('valueGet', $value);
  158. $result = $this->connection->executeQuery(
  159. 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?',
  160. array('userGet')
  161. )->fetchAll();
  162. $this->assertEquals(1, count($result));
  163. $this->assertEquals(array(
  164. 'userid' => 'userGet',
  165. 'appid' => 'appGet',
  166. 'configkey' => 'keyGet',
  167. 'configvalue' => 'valueGet'
  168. ), $result[0]);
  169. // drop data from database - but the config option should be cached in the config object
  170. $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences` WHERE `userid` = ?', array('userGet'));
  171. // testing the caching mechanism
  172. $value = $config->getUserValue('userGet', 'appGet', 'keyGet');
  173. $this->assertEquals('valueGet', $value);
  174. $result = $this->connection->executeQuery(
  175. 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?',
  176. array('userGet')
  177. )->fetchAll();
  178. $this->assertEquals(0, count($result));
  179. }
  180. public function testGetUserKeys() {
  181. $config = $this->getConfig();
  182. // preparation - add something to the database
  183. $data = array(
  184. array('userFetch', 'appFetch1', 'keyFetch1', 'value1'),
  185. array('userFetch', 'appFetch1', 'keyFetch2', 'value2'),
  186. array('userFetch', 'appFetch2', 'keyFetch3', 'value3'),
  187. array('userFetch', 'appFetch1', 'keyFetch4', 'value4'),
  188. array('userFetch', 'appFetch4', 'keyFetch1', 'value5'),
  189. array('userFetch', 'appFetch5', 'keyFetch1', 'value6'),
  190. array('userFetch2', 'appFetch', 'keyFetch1', 'value7')
  191. );
  192. foreach ($data as $entry) {
  193. $this->connection->executeUpdate(
  194. 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' .
  195. '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)',
  196. $entry
  197. );
  198. }
  199. $value = $config->getUserKeys('userFetch', 'appFetch1');
  200. $this->assertEquals(array('keyFetch1', 'keyFetch2', 'keyFetch4'), $value);
  201. $value = $config->getUserKeys('userFetch2', 'appFetch');
  202. $this->assertEquals(array('keyFetch1'), $value);
  203. // cleanup
  204. $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`');
  205. }
  206. public function testGetUserValueDefault() {
  207. $config = $this->getConfig();
  208. $this->assertEquals('', $config->getUserValue('userGetUnset', 'appGetUnset', 'keyGetUnset'));
  209. $this->assertEquals(null, $config->getUserValue('userGetUnset', 'appGetUnset', 'keyGetUnset', null));
  210. $this->assertEquals('foobar', $config->getUserValue('userGetUnset', 'appGetUnset', 'keyGetUnset', 'foobar'));
  211. }
  212. public function testGetUserValueForUsers() {
  213. $config = $this->getConfig();
  214. // preparation - add something to the database
  215. $data = array(
  216. array('userFetch1', 'appFetch2', 'keyFetch1', 'value1'),
  217. array('userFetch2', 'appFetch2', 'keyFetch1', 'value2'),
  218. array('userFetch3', 'appFetch2', 'keyFetch1', 3),
  219. array('userFetch4', 'appFetch2', 'keyFetch1', 'value4'),
  220. array('userFetch5', 'appFetch2', 'keyFetch1', 'value5'),
  221. array('userFetch6', 'appFetch2', 'keyFetch1', 'value6'),
  222. array('userFetch7', 'appFetch2', 'keyFetch1', 'value7')
  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->getUserValueForUsers('appFetch2', 'keyFetch1',
  232. array('userFetch1', 'userFetch2', 'userFetch3', 'userFetch5'));
  233. $this->assertEquals(array(
  234. 'userFetch1' => 'value1',
  235. 'userFetch2' => 'value2',
  236. 'userFetch3' => 3,
  237. 'userFetch5' => 'value5'
  238. ), $value);
  239. $value = $config->getUserValueForUsers('appFetch2', 'keyFetch1',
  240. array('userFetch1', 'userFetch4', 'userFetch9'));
  241. $this->assertEquals(array(
  242. 'userFetch1' => 'value1',
  243. 'userFetch4' => 'value4'
  244. ), $value, 'userFetch9 is an non-existent user and should not be shown.');
  245. // cleanup
  246. $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`');
  247. }
  248. public function testDeleteAllUserValues() {
  249. $config = $this->getConfig();
  250. // preparation - add something to the database
  251. $data = array(
  252. array('userFetch3', 'appFetch1', 'keyFetch1', 'value1'),
  253. array('userFetch3', 'appFetch1', 'keyFetch2', 'value2'),
  254. array('userFetch3', 'appFetch2', 'keyFetch3', 'value3'),
  255. array('userFetch3', 'appFetch1', 'keyFetch4', 'value4'),
  256. array('userFetch3', 'appFetch4', 'keyFetch1', 'value5'),
  257. array('userFetch3', 'appFetch5', 'keyFetch1', 'value6'),
  258. array('userFetch4', 'appFetch2', 'keyFetch1', 'value7')
  259. );
  260. foreach ($data as $entry) {
  261. $this->connection->executeUpdate(
  262. 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' .
  263. '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)',
  264. $entry
  265. );
  266. }
  267. $config->deleteAllUserValues('userFetch3');
  268. $result = $this->connection->executeQuery(
  269. 'SELECT COUNT(*) AS `count` FROM `*PREFIX*preferences`'
  270. )->fetch();
  271. $actualCount = $result['count'];
  272. $this->assertEquals(1, $actualCount, 'After removing `userFetch3` there should be exactly 1 entry left.');
  273. // cleanup
  274. $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`');
  275. }
  276. public function testDeleteAppFromAllUsers() {
  277. $config = $this->getConfig();
  278. // preparation - add something to the database
  279. $data = array(
  280. array('userFetch5', 'appFetch1', 'keyFetch1', 'value1'),
  281. array('userFetch5', 'appFetch1', 'keyFetch2', 'value2'),
  282. array('userFetch5', 'appFetch2', 'keyFetch3', 'value3'),
  283. array('userFetch5', 'appFetch1', 'keyFetch4', 'value4'),
  284. array('userFetch5', 'appFetch4', 'keyFetch1', 'value5'),
  285. array('userFetch5', 'appFetch5', 'keyFetch1', 'value6'),
  286. array('userFetch6', 'appFetch2', 'keyFetch1', 'value7')
  287. );
  288. foreach ($data as $entry) {
  289. $this->connection->executeUpdate(
  290. 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' .
  291. '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)',
  292. $entry
  293. );
  294. }
  295. $config->deleteAppFromAllUsers('appFetch1');
  296. $result = $this->connection->executeQuery(
  297. 'SELECT COUNT(*) AS `count` FROM `*PREFIX*preferences`'
  298. )->fetch();
  299. $actualCount = $result['count'];
  300. $this->assertEquals(4, $actualCount, 'After removing `appFetch1` there should be exactly 4 entries left.');
  301. $config->deleteAppFromAllUsers('appFetch2');
  302. $result = $this->connection->executeQuery(
  303. 'SELECT COUNT(*) AS `count` FROM `*PREFIX*preferences`'
  304. )->fetch();
  305. $actualCount = $result['count'];
  306. $this->assertEquals(2, $actualCount, 'After removing `appFetch2` there should be exactly 2 entries left.');
  307. // cleanup
  308. $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`');
  309. }
  310. public function testGetUsersForUserValue() {
  311. // mock the check for the database to run the correct SQL statements for each database type
  312. $systemConfig = $this->getMock('\OC\SystemConfig');
  313. $systemConfig->expects($this->once())
  314. ->method('getValue')
  315. ->with($this->equalTo('dbtype'),
  316. $this->equalTo('sqlite'))
  317. ->will($this->returnValue(\OC::$server->getConfig()->getSystemValue('dbtype', 'sqlite')));
  318. $config = $this->getConfig($systemConfig);
  319. // preparation - add something to the database
  320. $data = array(
  321. array('user1', 'appFetch9', 'keyFetch9', 'value9'),
  322. array('user2', 'appFetch9', 'keyFetch9', 'value9'),
  323. array('user3', 'appFetch9', 'keyFetch9', 'value8'),
  324. array('user4', 'appFetch9', 'keyFetch8', 'value9'),
  325. array('user5', 'appFetch8', 'keyFetch9', 'value9'),
  326. array('user6', 'appFetch9', 'keyFetch9', 'value9'),
  327. );
  328. foreach ($data as $entry) {
  329. $this->connection->executeUpdate(
  330. 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' .
  331. '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)',
  332. $entry
  333. );
  334. }
  335. $value = $config->getUsersForUserValue('appFetch9', 'keyFetch9', 'value9');
  336. $this->assertEquals(array('user1', 'user2', 'user6'), $value);
  337. // cleanup
  338. $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`');
  339. }
  340. }