diff options
author | Joas Schilling <nickvergessen@gmx.de> | 2016-05-20 15:38:20 +0200 |
---|---|---|
committer | Thomas Müller <DeepDiver1975@users.noreply.github.com> | 2016-05-20 15:38:20 +0200 |
commit | 94ad54ec9b96d41a614fbbad4a97b34c41a6901f (patch) | |
tree | f3eb7cdda2704aaf0cd59d58efe66bcbd34cb67d /tests/lib/AppConfigTest.php | |
parent | 2ef751b1ec28f7b5c7113af60ec8c9fa0ae1cf87 (diff) | |
download | nextcloud-server-94ad54ec9b96d41a614fbbad4a97b34c41a6901f.tar.gz nextcloud-server-94ad54ec9b96d41a614fbbad4a97b34c41a6901f.zip |
Move tests/ to PSR-4 (#24731)
* Move a-b to PSR-4
* Move c-d to PSR-4
* Move e+g to PSR-4
* Move h-l to PSR-4
* Move m-r to PSR-4
* Move s-u to PSR-4
* Move files/ to PSR-4
* Move remaining tests to PSR-4
* Remove Test\ from old autoloader
Diffstat (limited to 'tests/lib/AppConfigTest.php')
-rw-r--r-- | tests/lib/AppConfigTest.php | 351 |
1 files changed, 351 insertions, 0 deletions
diff --git a/tests/lib/AppConfigTest.php b/tests/lib/AppConfigTest.php new file mode 100644 index 00000000000..520dbf66d36 --- /dev/null +++ b/tests/lib/AppConfigTest.php @@ -0,0 +1,351 @@ +<?php +/** + * Copyright (c) 2013 Christopher Schäpers <christopher@schaepers.it> + * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl> + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test; + +/** + * Class AppConfigTest + * + * @group DB + * + * @package Test + */ +class AppConfigTest extends TestCase { + /** @var \OCP\IAppConfig */ + protected $appConfig; + + /** @var \OCP\IDBConnection */ + protected $connection; + + protected $originalConfig; + + public function setUp() { + parent::setUp(); + + $this->connection = \OC::$server->getDatabaseConnection(); + $sql = $this->connection->getQueryBuilder(); + $sql->select('*') + ->from('appconfig'); + $result = $sql->execute(); + $this->originalConfig = $result->fetchAll(); + $result->closeCursor(); + + $sql = $this->connection->getQueryBuilder(); + $sql->delete('appconfig'); + $sql->execute(); + + $this->registerAppConfig(new \OC\AppConfig($this->connection)); + + $sql = $this->connection->getQueryBuilder(); + $sql->insert('appconfig') + ->values([ + 'appid' => $sql->createParameter('appid'), + 'configkey' => $sql->createParameter('configkey'), + 'configvalue' => $sql->createParameter('configvalue'), + ]); + + $sql->setParameters([ + 'appid' => 'testapp', + 'configkey' => 'enabled', + 'configvalue' => 'true', + ])->execute(); + $sql->setParameters([ + 'appid' => 'testapp', + 'configkey' => 'installed_version', + 'configvalue' => '1.2.3', + ])->execute(); + $sql->setParameters([ + 'appid' => 'testapp', + 'configkey' => 'depends_on', + 'configvalue' => 'someapp', + ])->execute(); + $sql->setParameters([ + 'appid' => 'testapp', + 'configkey' => 'deletethis', + 'configvalue' => 'deletethis', + ])->execute(); + $sql->setParameters([ + 'appid' => 'testapp', + 'configkey' => 'key', + 'configvalue' => 'value', + ])->execute(); + + $sql->setParameters([ + 'appid' => 'someapp', + 'configkey' => 'key', + 'configvalue' => 'value', + ])->execute(); + $sql->setParameters([ + 'appid' => 'someapp', + 'configkey' => 'otherkey', + 'configvalue' => 'othervalue', + ])->execute(); + + $sql->setParameters([ + 'appid' => '123456', + 'configkey' => 'key', + 'configvalue' => 'value', + ])->execute(); + $sql->setParameters([ + 'appid' => '123456', + 'configkey' => 'enabled', + 'configvalue' => 'false', + ])->execute(); + + $sql->setParameters([ + 'appid' => 'anotherapp', + 'configkey' => 'key', + 'configvalue' => 'value', + ])->execute(); + $sql->setParameters([ + 'appid' => 'anotherapp', + 'configkey' => 'enabled', + 'configvalue' => 'false', + ])->execute(); + } + + public function tearDown() { + $sql = $this->connection->getQueryBuilder(); + $sql->delete('appconfig'); + $sql->execute(); + + $sql = $this->connection->getQueryBuilder(); + $sql->insert('appconfig') + ->values([ + 'appid' => $sql->createParameter('appid'), + 'configkey' => $sql->createParameter('configkey'), + 'configvalue' => $sql->createParameter('configvalue'), + ]); + + foreach ($this->originalConfig as $configs) { + $sql->setParameter('appid', $configs['appid']) + ->setParameter('configkey', $configs['configkey']) + ->setParameter('configvalue', $configs['configvalue']); + $sql->execute(); + } + + $this->registerAppConfig(new \OC\AppConfig(\OC::$server->getDatabaseConnection())); + parent::tearDown(); + } + + /** + * Register an app config object for testing purposes. + * + * @param \OCP\IAppConfig $appConfig + */ + protected function registerAppConfig($appConfig) { + \OC::$server->registerService('AppConfig', function () use ($appConfig) { + return $appConfig; + }); + } + + public function testGetApps() { + $config = new \OC\AppConfig(\OC::$server->getDatabaseConnection()); + + $this->assertEquals([ + 'anotherapp', + 'someapp', + 'testapp', + '123456', + ], $config->getApps()); + } + + public function testGetKeys() { + $config = new \OC\AppConfig(\OC::$server->getDatabaseConnection()); + + $keys = $config->getKeys('testapp'); + $this->assertEquals([ + 'deletethis', + 'depends_on', + 'enabled', + 'installed_version', + 'key', + ], $keys); + } + + public function testGetValue() { + $config = new \OC\AppConfig(\OC::$server->getDatabaseConnection()); + + $value = $config->getValue('testapp', 'installed_version'); + $this->assertConfigKey('testapp', 'installed_version', $value); + + $value = $config->getValue('testapp', 'nonexistant'); + $this->assertNull($value); + + $value = $config->getValue('testapp', 'nonexistant', 'default'); + $this->assertEquals('default', $value); + } + + public function testHasKey() { + $config = new \OC\AppConfig(\OC::$server->getDatabaseConnection()); + + $this->assertTrue($config->hasKey('testapp', 'installed_version')); + $this->assertFalse($config->hasKey('testapp', 'nonexistant')); + $this->assertFalse($config->hasKey('nonexistant', 'nonexistant')); + } + + public function testSetValueUpdate() { + $config = new \OC\AppConfig(\OC::$server->getDatabaseConnection()); + + $this->assertEquals('1.2.3', $config->getValue('testapp', 'installed_version')); + $this->assertConfigKey('testapp', 'installed_version', '1.2.3'); + + $wasModified = $config->setValue('testapp', 'installed_version', '1.2.3'); + if (!(\OC::$server->getDatabaseConnection() instanceof \OC\DB\OracleConnection)) { + $this->assertFalse($wasModified); + } + + $this->assertEquals('1.2.3', $config->getValue('testapp', 'installed_version')); + $this->assertConfigKey('testapp', 'installed_version', '1.2.3'); + + $this->assertTrue($config->setValue('testapp', 'installed_version', '1.33.7')); + + + $this->assertEquals('1.33.7', $config->getValue('testapp', 'installed_version')); + $this->assertConfigKey('testapp', 'installed_version', '1.33.7'); + + $config->setValue('someapp', 'somekey', 'somevalue'); + $this->assertConfigKey('someapp', 'somekey', 'somevalue'); + } + + public function testSetValueInsert() { + $config = new \OC\AppConfig(\OC::$server->getDatabaseConnection()); + + $this->assertFalse($config->hasKey('someapp', 'somekey')); + $this->assertNull($config->getValue('someapp', 'somekey')); + + $this->assertTrue($config->setValue('someapp', 'somekey', 'somevalue')); + + $this->assertTrue($config->hasKey('someapp', 'somekey')); + $this->assertEquals('somevalue', $config->getValue('someapp', 'somekey')); + $this->assertConfigKey('someapp', 'somekey', 'somevalue'); + + $wasInserted = $config->setValue('someapp', 'somekey', 'somevalue'); + if (!(\OC::$server->getDatabaseConnection() instanceof \OC\DB\OracleConnection)) { + $this->assertFalse($wasInserted); + } + } + + public function testDeleteKey() { + $config = new \OC\AppConfig(\OC::$server->getDatabaseConnection()); + + $this->assertTrue($config->hasKey('testapp', 'deletethis')); + + $config->deleteKey('testapp', 'deletethis'); + + $this->assertFalse($config->hasKey('testapp', 'deletethis')); + + $sql = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + $sql->select('configvalue') + ->from('appconfig') + ->where($sql->expr()->eq('appid', $sql->createParameter('appid'))) + ->andWhere($sql->expr()->eq('configkey', $sql->createParameter('configkey'))) + ->setParameter('appid', 'testapp') + ->setParameter('configkey', 'deletethis'); + $query = $sql->execute(); + $result = $query->fetch(); + $query->closeCursor(); + $this->assertFalse($result); + } + + public function testDeleteApp() { + $config = new \OC\AppConfig(\OC::$server->getDatabaseConnection()); + + $this->assertTrue($config->hasKey('someapp', 'otherkey')); + + $config->deleteApp('someapp'); + + $this->assertFalse($config->hasKey('someapp', 'otherkey')); + + $sql = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + $sql->select('configvalue') + ->from('appconfig') + ->where($sql->expr()->eq('appid', $sql->createParameter('appid'))) + ->setParameter('appid', 'someapp'); + $query = $sql->execute(); + $result = $query->fetch(); + $query->closeCursor(); + $this->assertFalse($result); + } + + public function testGetValuesNotAllowed() { + $config = new \OC\AppConfig(\OC::$server->getDatabaseConnection()); + + $this->assertFalse($config->getValues('testapp', 'enabled')); + + $this->assertFalse($config->getValues(false, false)); + } + + public function testGetValues() { + $config = new \OC\AppConfig(\OC::$server->getDatabaseConnection()); + + $sql = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + $sql->select(['configkey', 'configvalue']) + ->from('appconfig') + ->where($sql->expr()->eq('appid', $sql->createParameter('appid'))) + ->setParameter('appid', 'testapp'); + $query = $sql->execute(); + $expected = []; + while ($row = $query->fetch()) { + $expected[$row['configkey']] = $row['configvalue']; + } + $query->closeCursor(); + + $values = $config->getValues('testapp', false); + $this->assertEquals($expected, $values); + + $sql = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + $sql->select(['appid', 'configvalue']) + ->from('appconfig') + ->where($sql->expr()->eq('configkey', $sql->createParameter('configkey'))) + ->setParameter('configkey', 'enabled'); + $query = $sql->execute(); + $expected = []; + while ($row = $query->fetch()) { + $expected[$row['appid']] = $row['configvalue']; + } + $query->closeCursor(); + + $values = $config->getValues(false, 'enabled'); + $this->assertEquals($expected, $values); + } + + public function testSettingConfigParallel() { + $appConfig1 = new \OC\AppConfig(\OC::$server->getDatabaseConnection()); + $appConfig2 = new \OC\AppConfig(\OC::$server->getDatabaseConnection()); + $appConfig1->getValue('testapp', 'foo', 'v1'); + $appConfig2->getValue('testapp', 'foo', 'v1'); + + $appConfig1->setValue('testapp', 'foo', 'v1'); + $this->assertConfigKey('testapp', 'foo', 'v1'); + + $appConfig2->setValue('testapp', 'foo', 'v2'); + $this->assertConfigKey('testapp', 'foo', 'v2'); + } + + /** + * @param string $app + * @param string $key + * @param string $expected + */ + protected function assertConfigKey($app, $key, $expected) { + $sql = \OC::$server->getDatabaseConnection()->getQueryBuilder(); + $sql->select('configvalue') + ->from('appconfig') + ->where($sql->expr()->eq('appid', $sql->createParameter('appid'))) + ->andWhere($sql->expr()->eq('configkey', $sql->createParameter('configkey'))) + ->setParameter('appid', $app) + ->setParameter('configkey', $key); + $query = $sql->execute(); + $actual = $query->fetch(); + $query->closeCursor(); + + $this->assertEquals($expected, $actual['configvalue']); + } +} |