AllConfigTest.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460
  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 OC\SystemConfig;
  17. use OCP\IDBConnection;
  18. class AllConfigTest extends \Test\TestCase {
  19. /** @var \OCP\IDBConnection */
  20. protected $connection;
  21. protected function getConfig($systemConfig = null, $connection = null) {
  22. if ($this->connection === null) {
  23. $this->connection = \OC::$server->getDatabaseConnection();
  24. }
  25. if ($connection === null) {
  26. $connection = $this->connection;
  27. }
  28. if ($systemConfig === null) {
  29. $systemConfig = $this->getMockBuilder('\OC\SystemConfig')
  30. ->disableOriginalConstructor()
  31. ->getMock();
  32. }
  33. return new \OC\AllConfig($systemConfig, $connection);
  34. }
  35. public function testDeleteUserValue() {
  36. $config = $this->getConfig();
  37. // preparation - add something to the database
  38. $this->connection->executeUpdate(
  39. 'INSERT INTO `*PREFIX*preferences` (`userid`, `appid`, ' .
  40. '`configkey`, `configvalue`) VALUES (?, ?, ?, ?)',
  41. ['userDelete', 'appDelete', 'keyDelete', 'valueDelete']
  42. );
  43. $config->deleteUserValue('userDelete', 'appDelete', 'keyDelete');
  44. $result = $this->connection->executeQuery(
  45. 'SELECT COUNT(*) AS `count` FROM `*PREFIX*preferences` WHERE `userid` = ?',
  46. ['userDelete']
  47. )->fetch();
  48. $actualCount = $result['count'];
  49. $this->assertEquals(0, $actualCount, 'There was one value in the database and after the tests there should be no entry left.');
  50. }
  51. public function testSetUserValue() {
  52. $selectAllSQL = 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?';
  53. $config = $this->getConfig();
  54. $config->setUserValue('userSet', 'appSet', 'keySet', 'valueSet');
  55. $result = $this->connection->executeQuery($selectAllSQL, ['userSet'])->fetchAll();
  56. $this->assertEquals(1, count($result));
  57. $this->assertEquals([
  58. 'userid' => 'userSet',
  59. 'appid' => 'appSet',
  60. 'configkey' => 'keySet',
  61. 'configvalue' => 'valueSet'
  62. ], $result[0]);
  63. // test if the method overwrites existing database entries
  64. $config->setUserValue('userSet', 'appSet', 'keySet', 'valueSet2');
  65. $result = $this->connection->executeQuery($selectAllSQL, ['userSet'])->fetchAll();
  66. $this->assertEquals(1, count($result));
  67. $this->assertEquals([
  68. 'userid' => 'userSet',
  69. 'appid' => 'appSet',
  70. 'configkey' => 'keySet',
  71. 'configvalue' => 'valueSet2'
  72. ], $result[0]);
  73. // cleanup - it therefore relies on the successful execution of the previous test
  74. $config->deleteUserValue('userSet', 'appSet', 'keySet');
  75. }
  76. public function testSetUserValueWithPreCondition() {
  77. $config = $this->getConfig();
  78. $selectAllSQL = 'SELECT `userid`, `appid`, `configkey`, `configvalue` FROM `*PREFIX*preferences` WHERE `userid` = ?';
  79. $config->setUserValue('userPreCond', 'appPreCond', 'keyPreCond', 'valuePreCond');
  80. $result = $this->connection->executeQuery($selectAllSQL, ['userPreCond'])->fetchAll();
  81. $this->assertEquals(1, count($result));
  82. $this->assertEquals([
  83. 'userid' => 'userPreCond',
  84. 'appid' => 'appPreCond',
  85. 'configkey' => 'keyPreCond',
  86. 'configvalue' => 'valuePreCond'
  87. ], $result[0]);
  88. // test if the method overwrites existing database entries with valid precond
  89. $config->setUserValue('userPreCond', 'appPreCond', 'keyPreCond', 'valuePreCond2', 'valuePreCond');
  90. $result = $this->connection->executeQuery($selectAllSQL, ['userPreCond'])->fetchAll();
  91. $this->assertEquals(1, count($result));
  92. $this->assertEquals([
  93. 'userid' => 'userPreCond',
  94. 'appid' => 'appPreCond',
  95. 'configkey' => 'keyPreCond',
  96. 'configvalue' => 'valuePreCond2'
  97. ], $result[0]);
  98. // cleanup
  99. $config->deleteUserValue('userPreCond', 'appPreCond', 'keyPreCond');
  100. }
  101. public function dataSetUserValueUnexpectedValue() {
  102. return [
  103. [true],
  104. [false],
  105. [null],
  106. [new \stdClass()],
  107. ];
  108. }
  109. /**
  110. * @dataProvider dataSetUserValueUnexpectedValue
  111. * @param mixed $value
  112. */
  113. public function testSetUserValueUnexpectedValue($value) {
  114. $this->expectException(\UnexpectedValueException::class);
  115. $config = $this->getConfig();
  116. $config->setUserValue('userSetBool', 'appSetBool', 'keySetBool', $value);
  117. }
  118. public function testSetUserValueWithPreConditionFailure() {
  119. $this->expectException(\OCP\PreConditionNotMetException::class);
  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, ['userPreCond1'])->fetchAll();
  124. $this->assertEquals(1, count($result));
  125. $this->assertEquals([
  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, ['userPreCond1'])->fetchAll();
  134. $this->assertEquals(1, count($result));
  135. $this->assertEquals([
  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. ->willReturn('valueSetUnchanged');
  152. $connectionMock = $this->createMock(IDBConnection::class);
  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(['userSetUnchanged', 'appSetUnchanged', 'keySetUnchanged']))
  158. ->willReturn($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. ['userGet']
  173. )->fetchAll();
  174. $this->assertEquals(1, count($result));
  175. $this->assertEquals([
  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` = ?', ['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. ['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 = [
  196. ['userFetch', 'appFetch1', 'keyFetch1', 'value1'],
  197. ['userFetch', 'appFetch1', 'keyFetch2', 'value2'],
  198. ['userFetch', 'appFetch2', 'keyFetch3', 'value3'],
  199. ['userFetch', 'appFetch1', 'keyFetch4', 'value4'],
  200. ['userFetch', 'appFetch4', 'keyFetch1', 'value5'],
  201. ['userFetch', 'appFetch5', 'keyFetch1', 'value6'],
  202. ['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(['keyFetch1', 'keyFetch2', 'keyFetch4'], $value);
  213. $value = $config->getUserKeys('userFetch2', 'appFetch');
  214. $this->assertEquals(['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 = [
  228. ['userFetch1', 'appFetch2', 'keyFetch1', 'value1'],
  229. ['userFetch2', 'appFetch2', 'keyFetch1', 'value2'],
  230. ['userFetch3', 'appFetch2', 'keyFetch1', 3],
  231. ['userFetch4', 'appFetch2', 'keyFetch1', 'value4'],
  232. ['userFetch5', 'appFetch2', 'keyFetch1', 'value5'],
  233. ['userFetch6', 'appFetch2', 'keyFetch1', 'value6'],
  234. ['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. ['userFetch1', 'userFetch2', 'userFetch3', 'userFetch5']);
  245. $this->assertEquals([
  246. 'userFetch1' => 'value1',
  247. 'userFetch2' => 'value2',
  248. 'userFetch3' => 3,
  249. 'userFetch5' => 'value5'
  250. ], $value);
  251. $value = $config->getUserValueForUsers('appFetch2', 'keyFetch1',
  252. ['userFetch1', 'userFetch4', 'userFetch9']);
  253. $this->assertEquals([
  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 = [
  264. ['userFetch3', 'appFetch1', 'keyFetch1', 'value1'],
  265. ['userFetch3', 'appFetch1', 'keyFetch2', 'value2'],
  266. ['userFetch3', 'appFetch2', 'keyFetch3', 'value3'],
  267. ['userFetch3', 'appFetch1', 'keyFetch4', 'value4'],
  268. ['userFetch3', 'appFetch4', 'keyFetch1', 'value5'],
  269. ['userFetch3', 'appFetch5', 'keyFetch1', 'value6'],
  270. ['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 = [
  292. ['userFetch5', 'appFetch1', 'keyFetch1', 'value1'],
  293. ['userFetch5', 'appFetch1', 'keyFetch2', 'value2'],
  294. ['userFetch5', 'appFetch2', 'keyFetch3', 'value3'],
  295. ['userFetch5', 'appFetch1', 'keyFetch4', 'value4'],
  296. ['userFetch5', 'appFetch4', 'keyFetch1', 'value5'],
  297. ['userFetch5', 'appFetch5', 'keyFetch1', 'value6'],
  298. ['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. ->willReturn(\OC::$server->getConfig()->getSystemValue('dbtype', 'sqlite'));
  332. $config = $this->getConfig($systemConfig);
  333. // preparation - add something to the database
  334. $data = [
  335. ['user1', 'appFetch9', 'keyFetch9', 'value9'],
  336. ['user2', 'appFetch9', 'keyFetch9', 'value9'],
  337. ['user3', 'appFetch9', 'keyFetch9', 'value8'],
  338. ['user4', 'appFetch9', 'keyFetch8', 'value9'],
  339. ['user5', 'appFetch8', 'keyFetch9', 'value9'],
  340. ['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(['user1', 'user2', 'user6'], $value);
  351. // cleanup
  352. $this->connection->executeUpdate('DELETE FROM `*PREFIX*preferences`');
  353. }
  354. public function testGetUsersForUserValueCaseInsensitive() {
  355. // mock the check for the database to run the correct SQL statements for each database type
  356. $systemConfig = $this->createMock(SystemConfig::class);
  357. $systemConfig->expects($this->once())
  358. ->method('getValue')
  359. ->with($this->equalTo('dbtype'), $this->equalTo('sqlite'))
  360. ->willReturn(\OC::$server->getConfig()->getSystemValue('dbtype', 'sqlite'));
  361. $config = $this->getConfig($systemConfig);
  362. $config->setUserValue('user1', 'myApp', 'myKey', 'test123');
  363. $config->setUserValue('user2', 'myApp', 'myKey', 'TEST123');
  364. $config->setUserValue('user3', 'myApp', 'myKey', 'test12345');
  365. $users = $config->getUsersForUserValueCaseInsensitive('myApp', 'myKey', 'test123');
  366. $this->assertSame(2, count($users));
  367. $this->assertSame(['user1', 'user2'], $users);
  368. }
  369. }