You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

AllConfigTest.php 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. class AllConfigTest 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. public function dataSetUserValueUnexpectedValue() {
  100. return [
  101. [true],
  102. [false],
  103. [null],
  104. [new \stdClass()],
  105. ];
  106. }
  107. /**
  108. * @dataProvider dataSetUserValueUnexpectedValue
  109. * @param mixed $value
  110. * @expectedException \UnexpectedValueException
  111. */
  112. public function testSetUserValueUnexpectedValue($value) {
  113. $config = $this->getConfig();
  114. $config->setUserValue('userSetBool', 'appSetBool', 'keySetBool', $value);
  115. }
  116. /**
  117. * @expectedException \OCP\PreConditionNotMetException
  118. */
  119. public function testSetUserValueWithPreConditionFailure() {
  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, array('userPreCond1'))->fetchAll();
  124. $this->assertEquals(1, count($result));
  125. $this->assertEquals(array(
  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, array('userPreCond1'))->fetchAll();
  134. $this->assertEquals(1, count($result));
  135. $this->assertEquals(array(
  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. ->will($this->returnValue('valueSetUnchanged'));
  152. $connectionMock = $this->getMock('\OCP\IDBConnection');
  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(array('userSetUnchanged', 'appSetUnchanged', 'keySetUnchanged')))
  158. ->will($this->returnValue($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. array('userGet')
  173. )->fetchAll();
  174. $this->assertEquals(1, count($result));
  175. $this->assertEquals(array(
  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` = ?', array('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. array('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 = array(
  196. array('userFetch', 'appFetch1', 'keyFetch1', 'value1'),
  197. array('userFetch', 'appFetch1', 'keyFetch2', 'value2'),
  198. array('userFetch', 'appFetch2', 'keyFetch3', 'value3'),
  199. array('userFetch', 'appFetch1', 'keyFetch4', 'value4'),
  200. array('userFetch', 'appFetch4', 'keyFetch1', 'value5'),
  201. array('userFetch', 'appFetch5', 'keyFetch1', 'value6'),
  202. array('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(array('keyFetch1', 'keyFetch2', 'keyFetch4'), $value);
  213. $value = $config->getUserKeys('userFetch2', 'appFetch');
  214. $this->assertEquals(array('keyFetch1'), $value);
  215. // cleanup
  216. $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`');
  217. }
  218. public function testGetUserValueDefault() {
  219. $config = $this->getConfig();
  220. $this->assertEquals('', $config->getUserValue('userGetUnset', 'appGetUnset', 'keyGetUnset'));
  221. $this->assertEquals(null, $config->getUserValue('userGetUnset', 'appGetUnset', 'keyGetUnset', null));
  222. $this->assertEquals('foobar', $config->getUserValue('userGetUnset', 'appGetUnset', 'keyGetUnset', 'foobar'));
  223. }
  224. public function testGetUserValueForUsers() {
  225. $config = $this->getConfig();
  226. // preparation - add something to the database
  227. $data = array(
  228. array('userFetch1', 'appFetch2', 'keyFetch1', 'value1'),
  229. array('userFetch2', 'appFetch2', 'keyFetch1', 'value2'),
  230. array('userFetch3', 'appFetch2', 'keyFetch1', 3),
  231. array('userFetch4', 'appFetch2', 'keyFetch1', 'value4'),
  232. array('userFetch5', 'appFetch2', 'keyFetch1', 'value5'),
  233. array('userFetch6', 'appFetch2', 'keyFetch1', 'value6'),
  234. array('userFetch7', 'appFetch2', 'keyFetch1', 'value7')
  235. );
  236. foreach ($data as $entry) {
  237. $this->connection->executeUpdate(
  238. 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' .
  239. '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)',
  240. $entry
  241. );
  242. }
  243. $value = $config->getUserValueForUsers('appFetch2', 'keyFetch1',
  244. array('userFetch1', 'userFetch2', 'userFetch3', 'userFetch5'));
  245. $this->assertEquals(array(
  246. 'userFetch1' => 'value1',
  247. 'userFetch2' => 'value2',
  248. 'userFetch3' => 3,
  249. 'userFetch5' => 'value5'
  250. ), $value);
  251. $value = $config->getUserValueForUsers('appFetch2', 'keyFetch1',
  252. array('userFetch1', 'userFetch4', 'userFetch9'));
  253. $this->assertEquals(array(
  254. 'userFetch1' => 'value1',
  255. 'userFetch4' => 'value4'
  256. ), $value, 'userFetch9 is an non-existent user and should not be shown.');
  257. // cleanup
  258. $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`');
  259. }
  260. public function testDeleteAllUserValues() {
  261. $config = $this->getConfig();
  262. // preparation - add something to the database
  263. $data = array(
  264. array('userFetch3', 'appFetch1', 'keyFetch1', 'value1'),
  265. array('userFetch3', 'appFetch1', 'keyFetch2', 'value2'),
  266. array('userFetch3', 'appFetch2', 'keyFetch3', 'value3'),
  267. array('userFetch3', 'appFetch1', 'keyFetch4', 'value4'),
  268. array('userFetch3', 'appFetch4', 'keyFetch1', 'value5'),
  269. array('userFetch3', 'appFetch5', 'keyFetch1', 'value6'),
  270. array('userFetch4', 'appFetch2', 'keyFetch1', 'value7')
  271. );
  272. foreach ($data as $entry) {
  273. $this->connection->executeUpdate(
  274. 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' .
  275. '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)',
  276. $entry
  277. );
  278. }
  279. $config->deleteAllUserValues('userFetch3');
  280. $result = $this->connection->executeQuery(
  281. 'SELECT COUNT(*) AS `count` FROM `*PREFIX*preferences`'
  282. )->fetch();
  283. $actualCount = $result['count'];
  284. $this->assertEquals(1, $actualCount, 'After removing `userFetch3` there should be exactly 1 entry left.');
  285. // cleanup
  286. $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`');
  287. }
  288. public function testDeleteAppFromAllUsers() {
  289. $config = $this->getConfig();
  290. // preparation - add something to the database
  291. $data = array(
  292. array('userFetch5', 'appFetch1', 'keyFetch1', 'value1'),
  293. array('userFetch5', 'appFetch1', 'keyFetch2', 'value2'),
  294. array('userFetch5', 'appFetch2', 'keyFetch3', 'value3'),
  295. array('userFetch5', 'appFetch1', 'keyFetch4', 'value4'),
  296. array('userFetch5', 'appFetch4', 'keyFetch1', 'value5'),
  297. array('userFetch5', 'appFetch5', 'keyFetch1', 'value6'),
  298. array('userFetch6', 'appFetch2', 'keyFetch1', 'value7')
  299. );
  300. foreach ($data as $entry) {
  301. $this->connection->executeUpdate(
  302. 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' .
  303. '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)',
  304. $entry
  305. );
  306. }
  307. $config->deleteAppFromAllUsers('appFetch1');
  308. $result = $this->connection->executeQuery(
  309. 'SELECT COUNT(*) AS `count` FROM `*PREFIX*preferences`'
  310. )->fetch();
  311. $actualCount = $result['count'];
  312. $this->assertEquals(4, $actualCount, 'After removing `appFetch1` there should be exactly 4 entries left.');
  313. $config->deleteAppFromAllUsers('appFetch2');
  314. $result = $this->connection->executeQuery(
  315. 'SELECT COUNT(*) AS `count` FROM `*PREFIX*preferences`'
  316. )->fetch();
  317. $actualCount = $result['count'];
  318. $this->assertEquals(2, $actualCount, 'After removing `appFetch2` there should be exactly 2 entries left.');
  319. // cleanup
  320. $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`');
  321. }
  322. public function testGetUsersForUserValue() {
  323. // mock the check for the database to run the correct SQL statements for each database type
  324. $systemConfig = $this->getMockBuilder('\OC\SystemConfig')
  325. ->disableOriginalConstructor()
  326. ->getMock();
  327. $systemConfig->expects($this->once())
  328. ->method('getValue')
  329. ->with($this->equalTo('dbtype'),
  330. $this->equalTo('sqlite'))
  331. ->will($this->returnValue(\OC::$server->getConfig()->getSystemValue('dbtype', 'sqlite')));
  332. $config = $this->getConfig($systemConfig);
  333. // preparation - add something to the database
  334. $data = array(
  335. array('user1', 'appFetch9', 'keyFetch9', 'value9'),
  336. array('user2', 'appFetch9', 'keyFetch9', 'value9'),
  337. array('user3', 'appFetch9', 'keyFetch9', 'value8'),
  338. array('user4', 'appFetch9', 'keyFetch8', 'value9'),
  339. array('user5', 'appFetch8', 'keyFetch9', 'value9'),
  340. array('user6', 'appFetch9', 'keyFetch9', 'value9'),
  341. );
  342. foreach ($data as $entry) {
  343. $this->connection->executeUpdate(
  344. 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' .
  345. '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)',
  346. $entry
  347. );
  348. }
  349. $value = $config->getUsersForUserValue('appFetch9', 'keyFetch9', 'value9');
  350. $this->assertEquals(array('user1', 'user2', 'user6'), $value);
  351. // cleanup
  352. $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`');
  353. }
  354. }