1
0

AppConfigTest.php 10 KB

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