2013-08-02 09:46:47 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Copyright (c) 2013 Christopher Schäpers <christopher@schaepers.it>
|
2013-03-02 16:17:11 +00:00
|
|
|
* Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl>
|
2013-08-02 09:46:47 +00:00
|
|
|
* This file is licensed under the Affero General Public License version 3 or
|
|
|
|
* later.
|
|
|
|
* See the COPYING-README file.
|
|
|
|
*/
|
|
|
|
|
2014-11-10 21:59:50 +00:00
|
|
|
class Test_Appconfig extends \Test\TestCase {
|
2015-05-11 10:22:23 +00:00
|
|
|
/** @var \OCP\IAppConfig */
|
|
|
|
protected $appConfig;
|
2014-11-10 21:59:50 +00:00
|
|
|
|
2015-05-11 10:22:23 +00:00
|
|
|
/** @var \OCP\IDBConnection */
|
|
|
|
protected $connection;
|
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->connection = \OC::$server->getDatabaseConnection();
|
|
|
|
$this->registerAppConfig(new \OC\AppConfig(\OC::$server->getDatabaseConnection()));
|
|
|
|
|
|
|
|
$query = $this->connection->prepare('DELETE FROM `*PREFIX*appconfig` WHERE `appid` = ?');
|
2015-02-02 14:47:54 +00:00
|
|
|
$query->execute(array('testapp'));
|
|
|
|
$query->execute(array('someapp'));
|
|
|
|
$query->execute(array('123456'));
|
|
|
|
$query->execute(array('anotherapp'));
|
|
|
|
|
2015-05-11 10:22:23 +00:00
|
|
|
$query = $this->connection->prepare('INSERT INTO `*PREFIX*appconfig` VALUES (?, ?, ?)');
|
2013-08-02 09:46:47 +00:00
|
|
|
|
|
|
|
$query->execute(array('testapp', 'enabled', 'true'));
|
|
|
|
$query->execute(array('testapp', 'installed_version', '1.2.3'));
|
|
|
|
$query->execute(array('testapp', 'depends_on', 'someapp'));
|
|
|
|
$query->execute(array('testapp', 'deletethis', 'deletethis'));
|
|
|
|
$query->execute(array('testapp', 'key', 'value'));
|
|
|
|
|
|
|
|
$query->execute(array('someapp', 'key', 'value'));
|
|
|
|
$query->execute(array('someapp', 'otherkey', 'othervalue'));
|
|
|
|
|
|
|
|
$query->execute(array('123456', 'key', 'value'));
|
|
|
|
$query->execute(array('123456', 'enabled', 'false'));
|
|
|
|
|
|
|
|
$query->execute(array('anotherapp', 'key', 'value'));
|
|
|
|
$query->execute(array('anotherapp', 'enabled', 'false'));
|
|
|
|
}
|
|
|
|
|
2015-05-11 10:22:23 +00:00
|
|
|
public function tearDown() {
|
|
|
|
$query = $this->connection->prepare('DELETE FROM `*PREFIX*appconfig` WHERE `appid` = ?');
|
2013-08-06 15:30:58 +00:00
|
|
|
$query->execute(array('testapp'));
|
|
|
|
$query->execute(array('someapp'));
|
|
|
|
$query->execute(array('123456'));
|
|
|
|
$query->execute(array('anotherapp'));
|
2014-11-10 21:59:50 +00:00
|
|
|
|
2015-05-11 10:22:23 +00:00
|
|
|
$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 ($c) use ($appConfig) {
|
|
|
|
return $appConfig;
|
|
|
|
});
|
2013-08-06 15:30:58 +00:00
|
|
|
}
|
2013-08-02 09:46:47 +00:00
|
|
|
|
2015-05-11 10:22:23 +00:00
|
|
|
public function getAppConfigs() {
|
|
|
|
return [
|
|
|
|
[new \OC\AppConfig(\OC::$server->getDatabaseConnection())],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getAppConfigs
|
|
|
|
*
|
|
|
|
* @param mixed $callable
|
|
|
|
*/
|
|
|
|
public function testGetApps($callable) {
|
2013-12-16 16:22:44 +00:00
|
|
|
$query = \OC_DB::prepare('SELECT DISTINCT `appid` FROM `*PREFIX*appconfig` ORDER BY `appid`');
|
2013-08-02 09:46:47 +00:00
|
|
|
$result = $query->execute();
|
|
|
|
$expected = array();
|
|
|
|
while ($row = $result->fetchRow()) {
|
|
|
|
$expected[] = $row['appid'];
|
|
|
|
}
|
2014-02-13 12:56:02 +00:00
|
|
|
sort($expected);
|
2015-05-11 10:22:23 +00:00
|
|
|
$apps = call_user_func([$callable, 'getApps']);
|
2013-08-02 09:46:47 +00:00
|
|
|
$this->assertEquals($expected, $apps);
|
|
|
|
}
|
|
|
|
|
2015-05-11 10:22:23 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider getAppConfigs
|
|
|
|
*
|
|
|
|
* @param mixed $callable
|
|
|
|
*/
|
|
|
|
public function testGetKeys($callable) {
|
2013-08-02 09:46:47 +00:00
|
|
|
$query = \OC_DB::prepare('SELECT `configkey` FROM `*PREFIX*appconfig` WHERE `appid` = ?');
|
|
|
|
$result = $query->execute(array('testapp'));
|
|
|
|
$expected = array();
|
|
|
|
while($row = $result->fetchRow()) {
|
|
|
|
$expected[] = $row["configkey"];
|
|
|
|
}
|
2014-02-13 12:56:02 +00:00
|
|
|
sort($expected);
|
2015-05-11 10:22:23 +00:00
|
|
|
$keys = call_user_func([$callable, 'getKeys'], 'testapp');
|
2013-08-02 09:46:47 +00:00
|
|
|
$this->assertEquals($expected, $keys);
|
|
|
|
}
|
|
|
|
|
2015-05-11 10:22:23 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider getAppConfigs
|
|
|
|
*
|
|
|
|
* @param mixed $callable
|
|
|
|
*/
|
|
|
|
public function testGetValue($callable) {
|
|
|
|
$value = call_user_func([$callable, 'getValue'], 'testapp', 'installed_version');
|
2015-05-11 10:29:28 +00:00
|
|
|
$this->assertConfigKey('testapp', 'installed_version', $value);
|
2013-08-02 09:46:47 +00:00
|
|
|
|
2015-05-11 10:22:23 +00:00
|
|
|
$value = call_user_func([$callable, 'getValue'], 'testapp', 'nonexistant');
|
2013-08-02 09:46:47 +00:00
|
|
|
$this->assertNull($value);
|
|
|
|
|
2015-05-11 10:22:23 +00:00
|
|
|
$value = call_user_func([$callable, 'getValue'], 'testapp', 'nonexistant', 'default');
|
2013-08-02 09:46:47 +00:00
|
|
|
$this->assertEquals('default', $value);
|
|
|
|
}
|
|
|
|
|
2015-05-11 10:22:23 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider getAppConfigs
|
|
|
|
*
|
|
|
|
* @param mixed $callable
|
|
|
|
*/
|
|
|
|
public function testHasKey($callable) {
|
|
|
|
$value = call_user_func([$callable, 'hasKey'], 'testapp', 'installed_version');
|
2013-08-02 09:46:47 +00:00
|
|
|
$this->assertTrue($value);
|
|
|
|
|
2015-05-11 10:22:23 +00:00
|
|
|
$value = call_user_func([$callable, 'hasKey'], 'nonexistant', 'nonexistant');
|
2013-08-02 09:46:47 +00:00
|
|
|
$this->assertFalse($value);
|
|
|
|
}
|
|
|
|
|
2015-05-11 10:22:23 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider getAppConfigs
|
|
|
|
*
|
|
|
|
* @param mixed $callable
|
|
|
|
*/
|
|
|
|
public function testSetValue($callable) {
|
|
|
|
call_user_func([$callable, 'setValue'], 'testapp', 'installed_version', '1.33.7');
|
2015-05-11 10:29:28 +00:00
|
|
|
$this->assertConfigKey('testapp', 'installed_version', '1.33.7');
|
2013-08-02 09:46:47 +00:00
|
|
|
|
2015-05-11 10:22:23 +00:00
|
|
|
call_user_func([$callable, 'setValue'], 'someapp', 'somekey', 'somevalue');
|
2015-05-11 10:29:28 +00:00
|
|
|
$this->assertConfigKey('someapp', 'somekey', 'somevalue');
|
2013-08-02 09:46:47 +00:00
|
|
|
}
|
|
|
|
|
2015-05-11 10:22:23 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider getAppConfigs
|
|
|
|
*
|
|
|
|
* @param mixed $callable
|
|
|
|
*/
|
|
|
|
public function testDeleteKey($callable) {
|
|
|
|
call_user_func([$callable, 'deleteKey'], 'testapp', 'deletethis');
|
|
|
|
$query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*appconfig` WHERE `appid` = ? AND `configkey` = ?');
|
|
|
|
$query->execute(array('testapp', 'deletethis'));
|
|
|
|
$result = (bool)$query->fetchRow();
|
|
|
|
$this->assertFalse($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getAppConfigs
|
|
|
|
*
|
|
|
|
* @param mixed $callable
|
|
|
|
*/
|
|
|
|
public function testDeleteApp($callable) {
|
|
|
|
call_user_func([$callable, 'deleteApp'], 'someapp');
|
|
|
|
$query = \OC_DB::prepare('SELECT `configkey` FROM `*PREFIX*appconfig` WHERE `appid` = ?');
|
|
|
|
$query->execute(array('someapp'));
|
|
|
|
$result = (bool)$query->fetchRow();
|
|
|
|
$this->assertFalse($result);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider getAppConfigs
|
|
|
|
*
|
|
|
|
* @param mixed $callable
|
|
|
|
*/
|
|
|
|
public function testGetValues($callable) {
|
|
|
|
$this->assertFalse(call_user_func([$callable, 'getValues'], 'testapp', 'enabled'));
|
|
|
|
|
|
|
|
$query = \OC_DB::prepare('SELECT `configkey`, `configvalue` FROM `*PREFIX*appconfig` WHERE `appid` = ?');
|
|
|
|
$query->execute(array('testapp'));
|
|
|
|
$expected = array();
|
|
|
|
while ($row = $query->fetchRow()) {
|
|
|
|
$expected[$row['configkey']] = $row['configvalue'];
|
|
|
|
}
|
|
|
|
$values = call_user_func([$callable, 'getValues'], 'testapp', false);
|
|
|
|
$this->assertEquals($expected, $values);
|
|
|
|
|
|
|
|
$query = \OC_DB::prepare('SELECT `appid`, `configvalue` FROM `*PREFIX*appconfig` WHERE `configkey` = ?');
|
|
|
|
$query->execute(array('enabled'));
|
|
|
|
$expected = array();
|
|
|
|
while ($row = $query->fetchRow()) {
|
|
|
|
$expected[$row['appid']] = $row['configvalue'];
|
|
|
|
}
|
|
|
|
$values = call_user_func([$callable, 'getValues'], false, 'enabled');
|
|
|
|
$this->assertEquals($expected, $values);
|
|
|
|
}
|
|
|
|
|
2014-07-07 22:19:17 +00:00
|
|
|
public function testSetValueUnchanged() {
|
|
|
|
$statementMock = $this->getMock('\Doctrine\DBAL\Statement', array(), array(), '', false);
|
|
|
|
$statementMock->expects($this->once())
|
|
|
|
->method('fetch')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
|
|
|
$connectionMock = $this->getMock('\OC\DB\Connection', array(), array(), '', false);
|
|
|
|
$connectionMock->expects($this->once())
|
|
|
|
->method('executeQuery')
|
|
|
|
->with($this->equalTo('SELECT `configvalue`, `configkey` FROM `*PREFIX*appconfig`'
|
|
|
|
.' WHERE `appid` = ?'), $this->equalTo(array('bar')))
|
|
|
|
->will($this->returnValue($statementMock));
|
|
|
|
$connectionMock->expects($this->once())
|
2015-05-11 10:37:14 +00:00
|
|
|
->method('insertIfNotExist')
|
2014-07-07 22:19:17 +00:00
|
|
|
->with($this->equalTo('*PREFIX*appconfig'),
|
|
|
|
$this->equalTo(
|
|
|
|
array(
|
|
|
|
'appid' => 'bar',
|
|
|
|
'configkey' => 'foo',
|
|
|
|
'configvalue' => 'v1',
|
|
|
|
)
|
2015-05-11 10:37:14 +00:00
|
|
|
), $this->equalTo(['appid', 'configkey']))
|
|
|
|
->willReturn(1);
|
2014-07-07 22:19:17 +00:00
|
|
|
$connectionMock->expects($this->never())
|
|
|
|
->method('update');
|
|
|
|
|
|
|
|
$appconfig = new OC\AppConfig($connectionMock);
|
|
|
|
$appconfig->setValue('bar', 'foo', 'v1');
|
|
|
|
$appconfig->setValue('bar', 'foo', 'v1');
|
|
|
|
$appconfig->setValue('bar', 'foo', 'v1');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetValueUnchanged2() {
|
|
|
|
$statementMock = $this->getMock('\Doctrine\DBAL\Statement', array(), array(), '', false);
|
|
|
|
$statementMock->expects($this->once())
|
|
|
|
->method('fetch')
|
|
|
|
->will($this->returnValue(false));
|
|
|
|
|
|
|
|
$connectionMock = $this->getMock('\OC\DB\Connection', array(), array(), '', false);
|
|
|
|
$connectionMock->expects($this->once())
|
|
|
|
->method('executeQuery')
|
|
|
|
->with($this->equalTo('SELECT `configvalue`, `configkey` FROM `*PREFIX*appconfig`'
|
|
|
|
.' WHERE `appid` = ?'), $this->equalTo(array('bar')))
|
|
|
|
->will($this->returnValue($statementMock));
|
|
|
|
$connectionMock->expects($this->once())
|
2015-05-11 10:37:14 +00:00
|
|
|
->method('insertIfNotExist')
|
2014-07-07 22:19:17 +00:00
|
|
|
->with($this->equalTo('*PREFIX*appconfig'),
|
|
|
|
$this->equalTo(
|
|
|
|
array(
|
|
|
|
'appid' => 'bar',
|
|
|
|
'configkey' => 'foo',
|
|
|
|
'configvalue' => 'v1',
|
|
|
|
)
|
2015-05-11 10:37:14 +00:00
|
|
|
), $this->equalTo(['appid', 'configkey']))
|
|
|
|
->willReturn(1);
|
2014-07-07 22:19:17 +00:00
|
|
|
$connectionMock->expects($this->once())
|
|
|
|
->method('update')
|
|
|
|
->with($this->equalTo('*PREFIX*appconfig'),
|
|
|
|
$this->equalTo(array('configvalue' => 'v2')),
|
|
|
|
$this->equalTo(array('appid' => 'bar', 'configkey' => 'foo'))
|
|
|
|
);
|
|
|
|
|
|
|
|
$appconfig = new OC\AppConfig($connectionMock);
|
|
|
|
$appconfig->setValue('bar', 'foo', 'v1');
|
|
|
|
$appconfig->setValue('bar', 'foo', 'v2');
|
|
|
|
$appconfig->setValue('bar', 'foo', 'v2');
|
|
|
|
}
|
2015-05-11 10:29:28 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
* @throws \OC\DatabaseException
|
|
|
|
*/
|
|
|
|
protected function assertConfigKey($app, $key, $expected) {
|
|
|
|
$query = \OC_DB::prepare('SELECT `configvalue` FROM `*PREFIX*appconfig` WHERE `appid` = ? AND `configkey` = ?');
|
|
|
|
$result = $query->execute([$app, $key]);
|
|
|
|
$actual = $result->fetchRow();
|
|
|
|
$this->assertEquals($expected, $actual['configvalue']);
|
|
|
|
}
|
2013-08-02 09:46:47 +00:00
|
|
|
}
|