123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363 |
- <?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;
- use OCP\IConfig;
- /**
- * 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->overwriteService('AppConfig', 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->restoreService('AppConfig');
- parent::tearDown();
- }
- 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 testGetFilteredValues() {
- /** @var \OC\AppConfig|\PHPUnit_Framework_MockObject_MockObject $config */
- $config = $this->getMockBuilder(\OC\AppConfig::class)
- ->setConstructorArgs([\OC::$server->getDatabaseConnection()])
- ->setMethods(['getValues'])
- ->getMock();
- $config->expects($this->once())
- ->method('getValues')
- ->with('user_ldap', false)
- ->willReturn([
- 'ldap_agent_password' => 'secret',
- 'ldap_dn' => 'dn',
- ]);
- $values = $config->getFilteredValues('user_ldap');
- $this->assertEquals([
- 'ldap_agent_password' => IConfig::SENSITIVE_VALUE,
- 'ldap_dn' => 'dn',
- ], $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']);
- }
- }
|