allconfig.php 14 KB

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