1
0

AppConfigTest.php 11 KB

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