AppConfigTest.php 11 KB

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