1
0

AppConfigTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. <?php
  2. /**
  3. * Copyright (c) 2013 Christopher Schäpers <christopher@schaepers.it>
  4. * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl>
  5. * This file is licensed under the Affero General Public License version 3 or
  6. * later.
  7. * See the COPYING-README file.
  8. */
  9. namespace Test;
  10. use OCP\IConfig;
  11. /**
  12. * Class AppConfigTest
  13. *
  14. * @group DB
  15. *
  16. * @package Test
  17. */
  18. class AppConfigTest extends TestCase {
  19. /** @var \OCP\IAppConfig */
  20. protected $appConfig;
  21. /** @var \OCP\IDBConnection */
  22. protected $connection;
  23. protected $originalConfig;
  24. public function setUp() {
  25. parent::setUp();
  26. $this->connection = \OC::$server->getDatabaseConnection();
  27. $sql = $this->connection->getQueryBuilder();
  28. $sql->select('*')
  29. ->from('appconfig');
  30. $result = $sql->execute();
  31. $this->originalConfig = $result->fetchAll();
  32. $result->closeCursor();
  33. $sql = $this->connection->getQueryBuilder();
  34. $sql->delete('appconfig');
  35. $sql->execute();
  36. $this->overwriteService('AppConfig', new \OC\AppConfig($this->connection));
  37. $sql = $this->connection->getQueryBuilder();
  38. $sql->insert('appconfig')
  39. ->values([
  40. 'appid' => $sql->createParameter('appid'),
  41. 'configkey' => $sql->createParameter('configkey'),
  42. 'configvalue' => $sql->createParameter('configvalue'),
  43. ]);
  44. $sql->setParameters([
  45. 'appid' => 'testapp',
  46. 'configkey' => 'enabled',
  47. 'configvalue' => 'true',
  48. ])->execute();
  49. $sql->setParameters([
  50. 'appid' => 'testapp',
  51. 'configkey' => 'installed_version',
  52. 'configvalue' => '1.2.3',
  53. ])->execute();
  54. $sql->setParameters([
  55. 'appid' => 'testapp',
  56. 'configkey' => 'depends_on',
  57. 'configvalue' => 'someapp',
  58. ])->execute();
  59. $sql->setParameters([
  60. 'appid' => 'testapp',
  61. 'configkey' => 'deletethis',
  62. 'configvalue' => 'deletethis',
  63. ])->execute();
  64. $sql->setParameters([
  65. 'appid' => 'testapp',
  66. 'configkey' => 'key',
  67. 'configvalue' => 'value',
  68. ])->execute();
  69. $sql->setParameters([
  70. 'appid' => 'someapp',
  71. 'configkey' => 'key',
  72. 'configvalue' => 'value',
  73. ])->execute();
  74. $sql->setParameters([
  75. 'appid' => 'someapp',
  76. 'configkey' => 'otherkey',
  77. 'configvalue' => 'othervalue',
  78. ])->execute();
  79. $sql->setParameters([
  80. 'appid' => '123456',
  81. 'configkey' => 'key',
  82. 'configvalue' => 'value',
  83. ])->execute();
  84. $sql->setParameters([
  85. 'appid' => '123456',
  86. 'configkey' => 'enabled',
  87. 'configvalue' => 'false',
  88. ])->execute();
  89. $sql->setParameters([
  90. 'appid' => 'anotherapp',
  91. 'configkey' => 'key',
  92. 'configvalue' => 'value',
  93. ])->execute();
  94. $sql->setParameters([
  95. 'appid' => 'anotherapp',
  96. 'configkey' => 'enabled',
  97. 'configvalue' => 'false',
  98. ])->execute();
  99. }
  100. public function tearDown() {
  101. $sql = $this->connection->getQueryBuilder();
  102. $sql->delete('appconfig');
  103. $sql->execute();
  104. $sql = $this->connection->getQueryBuilder();
  105. $sql->insert('appconfig')
  106. ->values([
  107. 'appid' => $sql->createParameter('appid'),
  108. 'configkey' => $sql->createParameter('configkey'),
  109. 'configvalue' => $sql->createParameter('configvalue'),
  110. ]);
  111. foreach ($this->originalConfig as $configs) {
  112. $sql->setParameter('appid', $configs['appid'])
  113. ->setParameter('configkey', $configs['configkey'])
  114. ->setParameter('configvalue', $configs['configvalue']);
  115. $sql->execute();
  116. }
  117. $this->restoreService('AppConfig');
  118. parent::tearDown();
  119. }
  120. public function testGetApps() {
  121. $config = new \OC\AppConfig(\OC::$server->getDatabaseConnection());
  122. $this->assertEquals([
  123. 'anotherapp',
  124. 'someapp',
  125. 'testapp',
  126. '123456',
  127. ], $config->getApps());
  128. }
  129. public function testGetKeys() {
  130. $config = new \OC\AppConfig(\OC::$server->getDatabaseConnection());
  131. $keys = $config->getKeys('testapp');
  132. $this->assertEquals([
  133. 'deletethis',
  134. 'depends_on',
  135. 'enabled',
  136. 'installed_version',
  137. 'key',
  138. ], $keys);
  139. }
  140. public function testGetValue() {
  141. $config = new \OC\AppConfig(\OC::$server->getDatabaseConnection());
  142. $value = $config->getValue('testapp', 'installed_version');
  143. $this->assertConfigKey('testapp', 'installed_version', $value);
  144. $value = $config->getValue('testapp', 'nonexistant');
  145. $this->assertNull($value);
  146. $value = $config->getValue('testapp', 'nonexistant', 'default');
  147. $this->assertEquals('default', $value);
  148. }
  149. public function testHasKey() {
  150. $config = new \OC\AppConfig(\OC::$server->getDatabaseConnection());
  151. $this->assertTrue($config->hasKey('testapp', 'installed_version'));
  152. $this->assertFalse($config->hasKey('testapp', 'nonexistant'));
  153. $this->assertFalse($config->hasKey('nonexistant', 'nonexistant'));
  154. }
  155. public function testSetValueUpdate() {
  156. $config = new \OC\AppConfig(\OC::$server->getDatabaseConnection());
  157. $this->assertEquals('1.2.3', $config->getValue('testapp', 'installed_version'));
  158. $this->assertConfigKey('testapp', 'installed_version', '1.2.3');
  159. $wasModified = $config->setValue('testapp', 'installed_version', '1.2.3');
  160. if (!(\OC::$server->getDatabaseConnection() instanceof \OC\DB\OracleConnection)) {
  161. $this->assertFalse($wasModified);
  162. }
  163. $this->assertEquals('1.2.3', $config->getValue('testapp', 'installed_version'));
  164. $this->assertConfigKey('testapp', 'installed_version', '1.2.3');
  165. $this->assertTrue($config->setValue('testapp', 'installed_version', '1.33.7'));
  166. $this->assertEquals('1.33.7', $config->getValue('testapp', 'installed_version'));
  167. $this->assertConfigKey('testapp', 'installed_version', '1.33.7');
  168. $config->setValue('someapp', 'somekey', 'somevalue');
  169. $this->assertConfigKey('someapp', 'somekey', 'somevalue');
  170. }
  171. public function testSetValueInsert() {
  172. $config = new \OC\AppConfig(\OC::$server->getDatabaseConnection());
  173. $this->assertFalse($config->hasKey('someapp', 'somekey'));
  174. $this->assertNull($config->getValue('someapp', 'somekey'));
  175. $this->assertTrue($config->setValue('someapp', 'somekey', 'somevalue'));
  176. $this->assertTrue($config->hasKey('someapp', 'somekey'));
  177. $this->assertEquals('somevalue', $config->getValue('someapp', 'somekey'));
  178. $this->assertConfigKey('someapp', 'somekey', 'somevalue');
  179. $wasInserted = $config->setValue('someapp', 'somekey', 'somevalue');
  180. if (!(\OC::$server->getDatabaseConnection() instanceof \OC\DB\OracleConnection)) {
  181. $this->assertFalse($wasInserted);
  182. }
  183. }
  184. public function testDeleteKey() {
  185. $config = new \OC\AppConfig(\OC::$server->getDatabaseConnection());
  186. $this->assertTrue($config->hasKey('testapp', 'deletethis'));
  187. $config->deleteKey('testapp', 'deletethis');
  188. $this->assertFalse($config->hasKey('testapp', 'deletethis'));
  189. $sql = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  190. $sql->select('configvalue')
  191. ->from('appconfig')
  192. ->where($sql->expr()->eq('appid', $sql->createParameter('appid')))
  193. ->andWhere($sql->expr()->eq('configkey', $sql->createParameter('configkey')))
  194. ->setParameter('appid', 'testapp')
  195. ->setParameter('configkey', 'deletethis');
  196. $query = $sql->execute();
  197. $result = $query->fetch();
  198. $query->closeCursor();
  199. $this->assertFalse($result);
  200. }
  201. public function testDeleteApp() {
  202. $config = new \OC\AppConfig(\OC::$server->getDatabaseConnection());
  203. $this->assertTrue($config->hasKey('someapp', 'otherkey'));
  204. $config->deleteApp('someapp');
  205. $this->assertFalse($config->hasKey('someapp', 'otherkey'));
  206. $sql = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  207. $sql->select('configvalue')
  208. ->from('appconfig')
  209. ->where($sql->expr()->eq('appid', $sql->createParameter('appid')))
  210. ->setParameter('appid', 'someapp');
  211. $query = $sql->execute();
  212. $result = $query->fetch();
  213. $query->closeCursor();
  214. $this->assertFalse($result);
  215. }
  216. public function testGetValuesNotAllowed() {
  217. $config = new \OC\AppConfig(\OC::$server->getDatabaseConnection());
  218. $this->assertFalse($config->getValues('testapp', 'enabled'));
  219. $this->assertFalse($config->getValues(false, false));
  220. }
  221. public function testGetValues() {
  222. $config = new \OC\AppConfig(\OC::$server->getDatabaseConnection());
  223. $sql = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  224. $sql->select(['configkey', 'configvalue'])
  225. ->from('appconfig')
  226. ->where($sql->expr()->eq('appid', $sql->createParameter('appid')))
  227. ->setParameter('appid', 'testapp');
  228. $query = $sql->execute();
  229. $expected = [];
  230. while ($row = $query->fetch()) {
  231. $expected[$row['configkey']] = $row['configvalue'];
  232. }
  233. $query->closeCursor();
  234. $values = $config->getValues('testapp', false);
  235. $this->assertEquals($expected, $values);
  236. $sql = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  237. $sql->select(['appid', 'configvalue'])
  238. ->from('appconfig')
  239. ->where($sql->expr()->eq('configkey', $sql->createParameter('configkey')))
  240. ->setParameter('configkey', 'enabled');
  241. $query = $sql->execute();
  242. $expected = [];
  243. while ($row = $query->fetch()) {
  244. $expected[$row['appid']] = $row['configvalue'];
  245. }
  246. $query->closeCursor();
  247. $values = $config->getValues(false, 'enabled');
  248. $this->assertEquals($expected, $values);
  249. }
  250. public function testGetFilteredValues() {
  251. /** @var \OC\AppConfig|\PHPUnit_Framework_MockObject_MockObject $config */
  252. $config = $this->getMockBuilder(\OC\AppConfig::class)
  253. ->setConstructorArgs([\OC::$server->getDatabaseConnection()])
  254. ->setMethods(['getValues'])
  255. ->getMock();
  256. $config->expects($this->once())
  257. ->method('getValues')
  258. ->with('user_ldap', false)
  259. ->willReturn([
  260. 'ldap_agent_password' => 'secret',
  261. 'ldap_dn' => 'dn',
  262. ]);
  263. $values = $config->getFilteredValues('user_ldap');
  264. $this->assertEquals([
  265. 'ldap_agent_password' => IConfig::SENSITIVE_VALUE,
  266. 'ldap_dn' => 'dn',
  267. ], $values);
  268. }
  269. public function testSettingConfigParallel() {
  270. $appConfig1 = new \OC\AppConfig(\OC::$server->getDatabaseConnection());
  271. $appConfig2 = new \OC\AppConfig(\OC::$server->getDatabaseConnection());
  272. $appConfig1->getValue('testapp', 'foo', 'v1');
  273. $appConfig2->getValue('testapp', 'foo', 'v1');
  274. $appConfig1->setValue('testapp', 'foo', 'v1');
  275. $this->assertConfigKey('testapp', 'foo', 'v1');
  276. $appConfig2->setValue('testapp', 'foo', 'v2');
  277. $this->assertConfigKey('testapp', 'foo', 'v2');
  278. }
  279. /**
  280. * @param string $app
  281. * @param string $key
  282. * @param string $expected
  283. */
  284. protected function assertConfigKey($app, $key, $expected) {
  285. $sql = \OC::$server->getDatabaseConnection()->getQueryBuilder();
  286. $sql->select('configvalue')
  287. ->from('appconfig')
  288. ->where($sql->expr()->eq('appid', $sql->createParameter('appid')))
  289. ->andWhere($sql->expr()->eq('configkey', $sql->createParameter('configkey')))
  290. ->setParameter('appid', $app)
  291. ->setParameter('configkey', $key);
  292. $query = $sql->execute();
  293. $actual = $query->fetch();
  294. $query->closeCursor();
  295. $this->assertEquals($expected, $actual['configvalue']);
  296. }
  297. }