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 14KB

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