nextcloud/tests/lib/appconfig.php

299 lines
9.5 KiB
PHP
Raw Normal View History

2013-08-02 13:46:47 +04:00
<?php
/**
* Copyright (c) 2013 Christopher Schäpers <christopher@schaepers.it>
* 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.
*/
class Test_Appconfig extends \Test\TestCase {
/** @var \OCP\IAppConfig */
protected $appConfig;
/** @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 17:47:54 +03:00
$query->execute(array('testapp'));
$query->execute(array('someapp'));
$query->execute(array('123456'));
$query->execute(array('anotherapp'));
$query = $this->connection->prepare('INSERT INTO `*PREFIX*appconfig` VALUES (?, ?, ?)');
2013-08-02 13:46:47 +04: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'));
}
public function tearDown() {
$query = $this->connection->prepare('DELETE FROM `*PREFIX*appconfig` WHERE `appid` = ?');
$query->execute(array('testapp'));
$query->execute(array('someapp'));
$query->execute(array('123456'));
$query->execute(array('anotherapp'));
$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-02 13:46:47 +04:00
public function getAppConfigs() {
return [
['\OC_Appconfig'],
[new \OC\AppConfig(\OC::$server->getDatabaseConnection())],
];
}
/**
* @dataProvider getAppConfigs
*
* @param mixed $callable
*/
public function testGetApps($callable) {
$query = \OC_DB::prepare('SELECT DISTINCT `appid` FROM `*PREFIX*appconfig` ORDER BY `appid`');
2013-08-02 13:46:47 +04:00
$result = $query->execute();
$expected = array();
while ($row = $result->fetchRow()) {
$expected[] = $row['appid'];
}
2014-02-13 16:56:02 +04:00
sort($expected);
$apps = call_user_func([$callable, 'getApps']);
2013-08-02 13:46:47 +04:00
$this->assertEquals($expected, $apps);
}
/**
* @dataProvider getAppConfigs
*
* @param mixed $callable
*/
public function testGetKeys($callable) {
2013-08-02 13:46:47 +04: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 16:56:02 +04:00
sort($expected);
$keys = call_user_func([$callable, 'getKeys'], 'testapp');
2013-08-02 13:46:47 +04:00
$this->assertEquals($expected, $keys);
}
/**
* @dataProvider getAppConfigs
*
* @param mixed $callable
*/
public function testGetValue($callable) {
$value = call_user_func([$callable, '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
$value = call_user_func([$callable, 'getValue'], 'testapp', 'nonexistant');
2013-08-02 13:46:47 +04:00
$this->assertNull($value);
$value = call_user_func([$callable, 'getValue'], 'testapp', 'nonexistant', 'default');
2013-08-02 13:46:47 +04:00
$this->assertEquals('default', $value);
}
/**
* @dataProvider getAppConfigs
*
* @param mixed $callable
*/
public function testHasKey($callable) {
$value = call_user_func([$callable, 'hasKey'], 'testapp', 'installed_version');
2013-08-02 13:46:47 +04:00
$this->assertTrue($value);
$value = call_user_func([$callable, 'hasKey'], 'nonexistant', 'nonexistant');
2013-08-02 13:46:47 +04:00
$this->assertFalse($value);
}
/**
* @dataProvider getAppConfigs
*
* @param mixed $callable
*/
public function testSetValue($callable) {
call_user_func([$callable, 'setValue'], 'testapp', 'installed_version', '1.33.7');
2015-05-11 13:29:28 +03:00
$this->assertConfigKey('testapp', 'installed_version', '1.33.7');
2013-08-02 13:46:47 +04:00
call_user_func([$callable, '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
}
/**
* @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);
}
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())
->method('insertIfNotExist')
->with($this->equalTo('*PREFIX*appconfig'),
$this->equalTo(
array(
'appid' => 'bar',
'configkey' => 'foo',
'configvalue' => 'v1',
)
), $this->equalTo(['appid', 'configkey']))
->willReturn(1);
$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())
->method('insertIfNotExist')
->with($this->equalTo('*PREFIX*appconfig'),
$this->equalTo(
array(
'appid' => 'bar',
'configkey' => 'foo',
'configvalue' => 'v1',
)
), $this->equalTo(['appid', 'configkey']))
->willReturn(1);
$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 13:29:28 +03: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 13:46:47 +04:00
}