AllConfigTest.php 15 KB

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