2013-03-03 15:06:00 +04:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2015-09-25 17:00:36 +03:00
|
|
|
namespace Test;
|
|
|
|
|
2020-11-06 11:37:45 +03:00
|
|
|
use OC\Config;
|
|
|
|
|
2016-05-19 10:27:21 +03:00
|
|
|
class ConfigTest extends TestCase {
|
2020-04-10 17:54:27 +03:00
|
|
|
public const TESTCONTENT = '<?php $CONFIG=array("foo"=>"bar", "beers" => array("Appenzeller", "Guinness", "Kölsch"), "alcohol_free" => false);';
|
2013-03-03 15:06:00 +04:00
|
|
|
|
2014-09-25 20:43:04 +04:00
|
|
|
/** @var array */
|
2020-03-26 11:30:18 +03:00
|
|
|
private $initialConfig = ['foo' => 'bar', 'beers' => ['Appenzeller', 'Guinness', 'Kölsch'], 'alcohol_free' => false];
|
2014-09-25 20:43:04 +04:00
|
|
|
/** @var string */
|
|
|
|
private $configFile;
|
|
|
|
/** @var string */
|
|
|
|
private $randomTmpDir;
|
2013-07-02 02:15:42 +04:00
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2014-11-11 00:59:50 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
2015-12-18 13:19:53 +03:00
|
|
|
$this->randomTmpDir = \OC::$server->getTempManager()->getTemporaryFolder();
|
2014-09-25 20:43:04 +04:00
|
|
|
$this->configFile = $this->randomTmpDir.'testconfig.php';
|
|
|
|
file_put_contents($this->configFile, self::TESTCONTENT);
|
2013-06-28 00:50:28 +04:00
|
|
|
}
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function tearDown(): void {
|
2014-09-25 20:43:04 +04:00
|
|
|
unlink($this->configFile);
|
2014-11-11 00:59:50 +03:00
|
|
|
parent::tearDown();
|
2013-03-03 15:06:00 +04:00
|
|
|
}
|
|
|
|
|
2020-11-06 11:37:45 +03:00
|
|
|
private function getConfig(): Config {
|
|
|
|
return new \OC\Config($this->randomTmpDir, 'testconfig.php');
|
|
|
|
}
|
|
|
|
|
2013-07-05 17:25:53 +04:00
|
|
|
public function testGetKeys() {
|
2020-03-26 11:30:18 +03:00
|
|
|
$expectedConfig = ['foo', 'beers', 'alcohol_free'];
|
2020-11-06 11:37:45 +03:00
|
|
|
$this->assertSame($expectedConfig, $this->getConfig()->getKeys());
|
2013-03-03 15:06:00 +04:00
|
|
|
}
|
|
|
|
|
2013-07-05 17:25:53 +04:00
|
|
|
public function testGetValue() {
|
2020-11-06 11:37:45 +03:00
|
|
|
$config = $this->getConfig();
|
|
|
|
$this->assertSame('bar', $config->getValue('foo'));
|
|
|
|
$this->assertSame(null, $config->getValue('bar'));
|
|
|
|
$this->assertSame('moo', $config->getValue('bar', 'moo'));
|
|
|
|
$this->assertSame(false, $config->getValue('alcohol_free', 'someBogusValue'));
|
|
|
|
$this->assertSame(['Appenzeller', 'Guinness', 'Kölsch'], $config->getValue('beers', 'someBogusValue'));
|
|
|
|
$this->assertSame(['Appenzeller', 'Guinness', 'Kölsch'], $config->getValue('beers'));
|
2013-03-03 15:06:00 +04:00
|
|
|
}
|
|
|
|
|
2017-01-24 14:18:29 +03:00
|
|
|
public function testGetValueReturnsEnvironmentValueIfSet() {
|
2020-11-06 11:37:45 +03:00
|
|
|
$config = $this->getConfig();
|
|
|
|
$this->assertEquals('bar', $config->getValue('foo'));
|
|
|
|
|
2017-03-21 06:29:55 +03:00
|
|
|
putenv('NC_foo=baz');
|
2020-11-06 11:37:45 +03:00
|
|
|
$config = $this->getConfig();
|
|
|
|
$this->assertEquals('baz', $config->getValue('foo'));
|
2017-03-21 21:46:17 +03:00
|
|
|
putenv('NC_foo'); // unset the env variable
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetValueReturnsEnvironmentValueIfSetToZero() {
|
2020-11-06 11:37:45 +03:00
|
|
|
$config = $this->getConfig();
|
|
|
|
$this->assertEquals('bar', $config->getValue('foo'));
|
|
|
|
|
2017-03-21 21:46:17 +03:00
|
|
|
putenv('NC_foo=0');
|
2020-11-06 11:37:45 +03:00
|
|
|
$config = $this->getConfig();
|
|
|
|
$this->assertEquals('0', $config->getValue('foo'));
|
2017-03-21 21:46:17 +03:00
|
|
|
putenv('NC_foo'); // unset the env variable
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetValueReturnsEnvironmentValueIfSetToFalse() {
|
2020-11-06 11:37:45 +03:00
|
|
|
$config = $this->getConfig();
|
|
|
|
$this->assertEquals('bar', $config->getValue('foo'));
|
|
|
|
|
2017-03-21 21:46:17 +03:00
|
|
|
putenv('NC_foo=false');
|
2020-11-06 11:37:45 +03:00
|
|
|
$config = $this->getConfig();
|
|
|
|
$this->assertEquals('false', $config->getValue('foo'));
|
2017-03-21 21:46:17 +03:00
|
|
|
putenv('NC_foo'); // unset the env variable
|
2017-01-24 14:18:29 +03:00
|
|
|
}
|
|
|
|
|
2013-07-05 17:25:53 +04:00
|
|
|
public function testSetValue() {
|
2020-11-06 11:37:45 +03:00
|
|
|
$config = $this->getConfig();
|
|
|
|
$config->setValue('foo', 'moo');
|
|
|
|
$this->assertSame('moo', $config->getValue('foo'));
|
2013-03-03 15:06:00 +04:00
|
|
|
|
2014-09-25 20:43:04 +04:00
|
|
|
$content = file_get_contents($this->configFile);
|
|
|
|
$expected = "<?php\n\$CONFIG = array (\n 'foo' => 'moo',\n 'beers' => \n array (\n 0 => 'Appenzeller',\n " .
|
|
|
|
" 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n 'alcohol_free' => false,\n);\n";
|
2013-07-15 12:28:14 +04:00
|
|
|
$this->assertEquals($expected, $content);
|
2014-09-25 20:43:04 +04:00
|
|
|
|
2020-11-06 11:37:45 +03:00
|
|
|
$config->setValue('bar', 'red');
|
|
|
|
$config->setValue('apps', ['files', 'gallery']);
|
|
|
|
$this->assertSame('red', $config->getValue('bar'));
|
|
|
|
$this->assertSame(['files', 'gallery'], $config->getValue('apps'));
|
2013-03-03 15:06:00 +04:00
|
|
|
|
2014-09-25 20:43:04 +04:00
|
|
|
$content = file_get_contents($this->configFile);
|
|
|
|
|
|
|
|
$expected = "<?php\n\$CONFIG = array (\n 'foo' => 'moo',\n 'beers' => \n array (\n 0 => 'Appenzeller',\n " .
|
|
|
|
" 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n 'alcohol_free' => false,\n 'bar' => 'red',\n 'apps' => \n " .
|
|
|
|
" array (\n 0 => 'files',\n 1 => 'gallery',\n ),\n);\n";
|
2013-07-15 12:28:14 +04:00
|
|
|
$this->assertEquals($expected, $content);
|
2013-03-03 15:06:00 +04:00
|
|
|
}
|
|
|
|
|
2015-01-23 12:50:25 +03:00
|
|
|
public function testSetValues() {
|
2020-11-06 11:37:45 +03:00
|
|
|
$config = $this->getConfig();
|
2015-01-23 12:50:25 +03:00
|
|
|
$content = file_get_contents($this->configFile);
|
|
|
|
$this->assertEquals(self::TESTCONTENT, $content);
|
|
|
|
|
|
|
|
// Changing configs to existing values and deleting non-existing once
|
|
|
|
// should not rewrite the config.php
|
2020-11-06 11:37:45 +03:00
|
|
|
$config->setValues([
|
2020-10-05 16:12:57 +03:00
|
|
|
'foo' => 'bar',
|
|
|
|
'not_exists' => null,
|
2015-01-23 12:50:25 +03:00
|
|
|
]);
|
|
|
|
|
2020-11-06 11:37:45 +03:00
|
|
|
$this->assertSame('bar', $config->getValue('foo'));
|
|
|
|
$this->assertSame(null, $config->getValue('not_exists'));
|
2015-01-23 12:50:25 +03:00
|
|
|
$content = file_get_contents($this->configFile);
|
|
|
|
$this->assertEquals(self::TESTCONTENT, $content);
|
|
|
|
|
2020-11-06 11:37:45 +03:00
|
|
|
$config->setValues([
|
2020-10-05 16:12:57 +03:00
|
|
|
'foo' => 'moo',
|
|
|
|
'alcohol_free' => null,
|
2015-01-23 12:50:25 +03:00
|
|
|
]);
|
2020-11-06 11:37:45 +03:00
|
|
|
$this->assertSame('moo', $config->getValue('foo'));
|
|
|
|
$this->assertSame(null, $config->getValue('not_exists'));
|
2015-01-23 12:50:25 +03:00
|
|
|
|
|
|
|
$content = file_get_contents($this->configFile);
|
|
|
|
$expected = "<?php\n\$CONFIG = array (\n 'foo' => 'moo',\n 'beers' => \n array (\n 0 => 'Appenzeller',\n " .
|
|
|
|
" 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n);\n";
|
|
|
|
$this->assertEquals($expected, $content);
|
|
|
|
}
|
|
|
|
|
2013-07-05 17:25:53 +04:00
|
|
|
public function testDeleteKey() {
|
2020-11-06 11:37:45 +03:00
|
|
|
$config = $this->getConfig();
|
|
|
|
$config->deleteKey('foo');
|
|
|
|
$this->assertSame('this_was_clearly_not_set_before', $config->getValue('foo', 'this_was_clearly_not_set_before'));
|
2014-09-25 20:43:04 +04:00
|
|
|
$content = file_get_contents($this->configFile);
|
2013-03-03 15:06:00 +04:00
|
|
|
|
2014-09-25 20:43:04 +04:00
|
|
|
$expected = "<?php\n\$CONFIG = array (\n 'beers' => \n array (\n 0 => 'Appenzeller',\n " .
|
|
|
|
" 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n 'alcohol_free' => false,\n);\n";
|
2013-07-15 12:28:14 +04:00
|
|
|
$this->assertEquals($expected, $content);
|
2013-03-03 15:06:00 +04:00
|
|
|
}
|
|
|
|
|
2014-09-25 20:43:04 +04:00
|
|
|
public function testConfigMerge() {
|
|
|
|
// Create additional config
|
|
|
|
$additionalConfig = '<?php $CONFIG=array("php53"=>"totallyOutdated");';
|
|
|
|
$additionalConfigPath = $this->randomTmpDir.'additionalConfig.testconfig.php';
|
|
|
|
file_put_contents($additionalConfigPath, $additionalConfig);
|
|
|
|
|
|
|
|
// Reinstantiate the config to force a read-in of the additional configs
|
2020-11-06 11:37:45 +03:00
|
|
|
$config = new \OC\Config($this->randomTmpDir, 'testconfig.php');
|
2014-09-25 20:43:04 +04:00
|
|
|
|
|
|
|
// Ensure that the config value can be read and the config has not been modified
|
2020-11-06 11:37:45 +03:00
|
|
|
$this->assertSame('totallyOutdated', $config->getValue('php53', 'bogusValue'));
|
2014-09-25 20:43:04 +04:00
|
|
|
$this->assertEquals(self::TESTCONTENT, file_get_contents($this->configFile));
|
|
|
|
|
|
|
|
// Write a new value to the config
|
2020-11-06 11:37:45 +03:00
|
|
|
$config->setValue('CoolWebsites', ['demo.owncloud.org', 'owncloud.org', 'owncloud.com']);
|
2014-09-25 20:43:04 +04:00
|
|
|
$expected = "<?php\n\$CONFIG = array (\n 'foo' => 'bar',\n 'beers' => \n array (\n 0 => 'Appenzeller',\n " .
|
|
|
|
" 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n 'alcohol_free' => false,\n 'php53' => 'totallyOutdated',\n 'CoolWebsites' => \n array (\n " .
|
|
|
|
" 0 => 'demo.owncloud.org',\n 1 => 'owncloud.org',\n 2 => 'owncloud.com',\n ),\n);\n";
|
|
|
|
$this->assertEquals($expected, file_get_contents($this->configFile));
|
|
|
|
|
|
|
|
// Cleanup
|
|
|
|
unlink($additionalConfigPath);
|
2013-03-03 15:06:00 +04:00
|
|
|
}
|
|
|
|
}
|