AppConfigTest.php 11 KB

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