2013-08-02 13:46:47 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2013 Christopher Schäpers <christopher@schaepers.it>
|
2013-03-02 20:17:11 +04:00
|
|
|
* Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl>
|
2013-08-02 13:46:47 +04:00
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
2016-05-19 10:27:21 +03:00
|
|
|
namespace Test;
|
2020-04-09 12:48:10 +03:00
|
|
|
|
2020-11-03 19:24:06 +03:00
|
|
|
use OC\AppConfig;
|
2021-01-03 17:28:31 +03:00
|
|
|
use OC\DB\Connection;
|
2017-01-12 12:49:22 +03:00
|
|
|
use OCP\IConfig;
|
2015-09-02 19:56:17 +03:00
|
|
|
|
2015-11-03 03:52:41 +03:00
|
|
|
/**
|
2016-05-20 16:38:20 +03:00
|
|
|
* Class AppConfigTest
|
2015-11-03 03:52:41 +03:00
|
|
|
*
|
|
|
|
* @group DB
|
|
|
|
*
|
2016-05-19 10:27:21 +03:00
|
|
|
* @package Test
|
2015-11-03 03:52:41 +03:00
|
|
|
*/
|
2016-05-20 16:38:20 +03:00
|
|
|
class AppConfigTest extends TestCase {
|
2015-05-11 13:22:23 +03:00
|
|
|
/** @var \OCP\IAppConfig */
|
|
|
|
protected $appConfig;
|
2014-11-11 00:59:50 +03:00
|
|
|
|
2021-01-03 17:28:31 +03:00
|
|
|
/** @var Connection */
|
2015-05-11 13:22:23 +03:00
|
|
|
protected $connection;
|
|
|
|
|
2015-09-02 19:56:17 +03:00
|
|
|
protected $originalConfig;
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2015-05-11 13:22:23 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
2021-01-03 17:28:31 +03:00
|
|
|
$this->connection = \OC::$server->get(Connection::class);
|
2015-09-02 19:56:17 +03:00
|
|
|
$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();
|
|
|
|
|
2020-11-03 19:24:06 +03:00
|
|
|
$this->overwriteService(AppConfig::class, new \OC\AppConfig($this->connection));
|
2015-09-02 19:56:17 +03:00
|
|
|
|
|
|
|
$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();
|
2013-08-02 13:46:47 +04:00
|
|
|
}
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function tearDown(): void {
|
2015-09-02 19:56:17 +03:00
|
|
|
$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();
|
|
|
|
}
|
2014-11-11 00:59:50 +03:00
|
|
|
|
2020-11-03 19:24:06 +03:00
|
|
|
$this->restoreService(AppConfig::class);
|
2015-05-11 13:22:23 +03:00
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2015-09-02 19:56:17 +03:00
|
|
|
public function testGetApps() {
|
2021-01-03 17:28:31 +03:00
|
|
|
$config = new \OC\AppConfig(\OC::$server->get(Connection::class));
|
2015-05-11 13:22:23 +03:00
|
|
|
|
2020-12-03 18:33:38 +03:00
|
|
|
$this->assertEqualsCanonicalizing([
|
2015-09-02 19:56:17 +03:00
|
|
|
'anotherapp',
|
|
|
|
'someapp',
|
|
|
|
'testapp',
|
2020-12-03 18:33:38 +03:00
|
|
|
123456,
|
2015-09-02 19:56:17 +03:00
|
|
|
], $config->getApps());
|
2013-08-02 13:46:47 +04:00
|
|
|
}
|
|
|
|
|
2015-09-02 19:56:17 +03:00
|
|
|
public function testGetKeys() {
|
2021-01-03 17:28:31 +03:00
|
|
|
$config = new \OC\AppConfig(\OC::$server->get(Connection::class));
|
2015-09-02 19:56:17 +03:00
|
|
|
|
|
|
|
$keys = $config->getKeys('testapp');
|
2020-12-03 18:33:38 +03:00
|
|
|
$this->assertEqualsCanonicalizing([
|
2015-09-02 19:56:17 +03:00
|
|
|
'deletethis',
|
|
|
|
'depends_on',
|
|
|
|
'enabled',
|
|
|
|
'installed_version',
|
|
|
|
'key',
|
|
|
|
], $keys);
|
2013-08-02 13:46:47 +04:00
|
|
|
}
|
|
|
|
|
2015-09-02 19:56:17 +03:00
|
|
|
public function testGetValue() {
|
2021-01-03 17:28:31 +03:00
|
|
|
$config = new \OC\AppConfig(\OC::$server->get(Connection::class));
|
2015-09-02 19:56:17 +03:00
|
|
|
|
|
|
|
$value = $config->getValue('testapp', 'installed_version');
|
2015-05-11 13:29:28 +03:00
|
|
|
$this->assertConfigKey('testapp', 'installed_version', $value);
|
2013-08-02 13:46:47 +04:00
|
|
|
|
2015-09-02 19:56:17 +03:00
|
|
|
$value = $config->getValue('testapp', 'nonexistant');
|
2013-08-02 13:46:47 +04:00
|
|
|
$this->assertNull($value);
|
|
|
|
|
2015-09-02 19:56:17 +03:00
|
|
|
$value = $config->getValue('testapp', 'nonexistant', 'default');
|
2013-08-02 13:46:47 +04:00
|
|
|
$this->assertEquals('default', $value);
|
|
|
|
}
|
|
|
|
|
2015-09-02 19:56:17 +03:00
|
|
|
public function testHasKey() {
|
2021-01-03 17:28:31 +03:00
|
|
|
$config = new \OC\AppConfig(\OC::$server->get(Connection::class));
|
2013-08-02 13:46:47 +04:00
|
|
|
|
2015-09-02 19:56:17 +03:00
|
|
|
$this->assertTrue($config->hasKey('testapp', 'installed_version'));
|
|
|
|
$this->assertFalse($config->hasKey('testapp', 'nonexistant'));
|
|
|
|
$this->assertFalse($config->hasKey('nonexistant', 'nonexistant'));
|
2013-08-02 13:46:47 +04:00
|
|
|
}
|
|
|
|
|
2015-09-02 19:56:17 +03:00
|
|
|
public function testSetValueUpdate() {
|
2021-01-03 17:28:31 +03:00
|
|
|
$config = new \OC\AppConfig(\OC::$server->get(Connection::class));
|
2015-09-02 19:56:17 +03:00
|
|
|
|
|
|
|
$this->assertEquals('1.2.3', $config->getValue('testapp', 'installed_version'));
|
|
|
|
$this->assertConfigKey('testapp', 'installed_version', '1.2.3');
|
|
|
|
|
2015-09-14 16:27:06 +03:00
|
|
|
$wasModified = $config->setValue('testapp', 'installed_version', '1.2.3');
|
2021-01-03 17:28:31 +03:00
|
|
|
if (!(\OC::$server->get(Connection::class) instanceof \OC\DB\OracleConnection)) {
|
2015-09-14 16:27:06 +03:00
|
|
|
$this->assertFalse($wasModified);
|
|
|
|
}
|
2015-09-02 19:56:17 +03:00
|
|
|
|
|
|
|
$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'));
|
2015-05-11 13:29:28 +03:00
|
|
|
$this->assertConfigKey('testapp', 'installed_version', '1.33.7');
|
2013-08-02 13:46:47 +04:00
|
|
|
|
2015-09-02 19:56:17 +03:00
|
|
|
$config->setValue('someapp', 'somekey', 'somevalue');
|
2015-05-11 13:29:28 +03:00
|
|
|
$this->assertConfigKey('someapp', 'somekey', 'somevalue');
|
2013-08-02 13:46:47 +04:00
|
|
|
}
|
|
|
|
|
2015-09-02 19:56:17 +03:00
|
|
|
public function testSetValueInsert() {
|
2021-01-03 17:28:31 +03:00
|
|
|
$config = new \OC\AppConfig(\OC::$server->get(Connection::class));
|
2015-09-02 19:56:17 +03:00
|
|
|
|
|
|
|
$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');
|
|
|
|
|
2015-09-14 16:27:06 +03:00
|
|
|
$wasInserted = $config->setValue('someapp', 'somekey', 'somevalue');
|
2021-01-03 17:28:31 +03:00
|
|
|
if (!(\OC::$server->get(Connection::class) instanceof \OC\DB\OracleConnection)) {
|
2015-09-14 16:27:06 +03:00
|
|
|
$this->assertFalse($wasInserted);
|
|
|
|
}
|
2015-09-02 19:56:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDeleteKey() {
|
2021-01-03 17:28:31 +03:00
|
|
|
$config = new \OC\AppConfig(\OC::$server->get(Connection::class));
|
2015-09-02 19:56:17 +03:00
|
|
|
|
|
|
|
$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();
|
2015-05-11 13:22:23 +03:00
|
|
|
$this->assertFalse($result);
|
|
|
|
}
|
|
|
|
|
2015-09-02 19:56:17 +03:00
|
|
|
public function testDeleteApp() {
|
2021-01-03 17:28:31 +03:00
|
|
|
$config = new \OC\AppConfig(\OC::$server->get(Connection::class));
|
2015-09-02 19:56:17 +03:00
|
|
|
|
|
|
|
$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();
|
2015-05-11 13:22:23 +03:00
|
|
|
$this->assertFalse($result);
|
|
|
|
}
|
|
|
|
|
2015-09-02 19:56:17 +03:00
|
|
|
public function testGetValuesNotAllowed() {
|
2021-01-03 17:28:31 +03:00
|
|
|
$config = new \OC\AppConfig(\OC::$server->get(Connection::class));
|
2015-05-11 13:22:23 +03:00
|
|
|
|
2015-09-02 19:56:17 +03:00
|
|
|
$this->assertFalse($config->getValues('testapp', 'enabled'));
|
|
|
|
|
|
|
|
$this->assertFalse($config->getValues(false, false));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetValues() {
|
2021-01-03 17:28:31 +03:00
|
|
|
$config = new \OC\AppConfig(\OC::$server->get(Connection::class));
|
2015-09-02 19:56:17 +03:00
|
|
|
|
|
|
|
$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()) {
|
2015-05-11 13:22:23 +03:00
|
|
|
$expected[$row['configkey']] = $row['configvalue'];
|
|
|
|
}
|
2015-09-02 19:56:17 +03:00
|
|
|
$query->closeCursor();
|
|
|
|
|
|
|
|
$values = $config->getValues('testapp', false);
|
2015-05-11 13:22:23 +03:00
|
|
|
$this->assertEquals($expected, $values);
|
|
|
|
|
2015-09-02 19:56:17 +03:00
|
|
|
$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()) {
|
2015-05-11 13:22:23 +03:00
|
|
|
$expected[$row['appid']] = $row['configvalue'];
|
|
|
|
}
|
2015-09-02 19:56:17 +03:00
|
|
|
$query->closeCursor();
|
2015-05-11 13:22:23 +03:00
|
|
|
|
2015-09-02 19:56:17 +03:00
|
|
|
$values = $config->getValues(false, 'enabled');
|
|
|
|
$this->assertEquals($expected, $values);
|
2014-07-08 02:19:17 +04:00
|
|
|
}
|
2015-05-11 13:29:28 +03:00
|
|
|
|
2017-01-12 12:49:22 +03:00
|
|
|
public function testGetFilteredValues() {
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \OC\AppConfig|\PHPUnit\Framework\MockObject\MockObject $config */
|
2017-01-12 12:49:22 +03:00
|
|
|
$config = $this->getMockBuilder(\OC\AppConfig::class)
|
2021-01-03 17:28:31 +03:00
|
|
|
->setConstructorArgs([\OC::$server->get(Connection::class)])
|
2017-01-12 12:49:22 +03:00
|
|
|
->setMethods(['getValues'])
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$config->expects($this->once())
|
|
|
|
->method('getValues')
|
|
|
|
->with('user_ldap', false)
|
|
|
|
->willReturn([
|
|
|
|
'ldap_agent_password' => 'secret',
|
2019-07-26 14:31:14 +03:00
|
|
|
's42ldap_agent_password' => 'secret',
|
2017-01-12 12:49:22 +03:00
|
|
|
'ldap_dn' => 'dn',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$values = $config->getFilteredValues('user_ldap');
|
|
|
|
$this->assertEquals([
|
|
|
|
'ldap_agent_password' => IConfig::SENSITIVE_VALUE,
|
2019-07-26 14:31:14 +03:00
|
|
|
's42ldap_agent_password' => IConfig::SENSITIVE_VALUE,
|
2017-01-12 12:49:22 +03:00
|
|
|
'ldap_dn' => 'dn',
|
|
|
|
], $values);
|
|
|
|
}
|
|
|
|
|
2015-05-11 13:29:28 +03:00
|
|
|
public function testSettingConfigParallel() {
|
2021-01-03 17:28:31 +03:00
|
|
|
$appConfig1 = new \OC\AppConfig(\OC::$server->get(Connection::class));
|
|
|
|
$appConfig2 = new \OC\AppConfig(\OC::$server->get(Connection::class));
|
2015-05-11 13:29:28 +03:00
|
|
|
$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) {
|
2015-09-02 19:56:17 +03:00
|
|
|
$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();
|
|
|
|
|
2015-05-11 13:29:28 +03:00
|
|
|
$this->assertEquals($expected, $actual['configvalue']);
|
|
|
|
}
|
2013-08-02 13:46:47 +04:00
|
|
|
}
|