mirror of
https://github.com/nextcloud/server.git
synced 2026-06-15 11:41:20 -04:00
252 lines
10 KiB
PHP
252 lines
10 KiB
PHP
<?php
|
|
|
|
/**
|
|
* SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
|
|
* SPDX-FileCopyrightText: 2016 ownCloud, Inc.
|
|
* SPDX-License-Identifier: AGPL-3.0-or-later
|
|
*/
|
|
|
|
namespace Test;
|
|
|
|
use OC\Config;
|
|
use OCP\ITempManager;
|
|
use OCP\Server;
|
|
|
|
class ConfigTest extends TestCase {
|
|
public const TESTCONTENT = '<?php $CONFIG=array("foo"=>"bar", "beers" => array("Appenzeller", "Guinness", "Kölsch"), "alcohol_free" => false, "top" => ["bottom1" => "value1"]);';
|
|
|
|
/** @var array */
|
|
private $initialConfig = ['foo' => 'bar', 'beers' => ['Appenzeller', 'Guinness', 'Kölsch'], 'alcohol_free' => false];
|
|
/** @var string */
|
|
private $configFile;
|
|
/** @var string */
|
|
private $randomTmpDir;
|
|
|
|
#[\Override]
|
|
protected function setUp(): void {
|
|
parent::setUp();
|
|
|
|
$this->randomTmpDir = Server::get(ITempManager::class)->getTemporaryFolder();
|
|
$this->configFile = $this->randomTmpDir . 'testconfig.php';
|
|
file_put_contents($this->configFile, self::TESTCONTENT);
|
|
}
|
|
|
|
#[\Override]
|
|
protected function tearDown(): void {
|
|
unlink($this->configFile);
|
|
parent::tearDown();
|
|
}
|
|
|
|
private function getConfig(): Config {
|
|
return new Config($this->randomTmpDir, 'testconfig.php');
|
|
}
|
|
|
|
public function testGetKeys(): void {
|
|
$expectedConfig = ['foo', 'beers', 'alcohol_free', 'top'];
|
|
$this->assertSame($expectedConfig, $this->getConfig()->getKeys());
|
|
}
|
|
|
|
public function testGetKeysReturnsEnvironmentKeysIfSet() {
|
|
$expectedConfig = ['foo', 'beers', 'alcohol_free', 'top', 'taste'];
|
|
putenv('NC_taste=great');
|
|
$this->assertSame($expectedConfig, $this->getConfig()->getKeys());
|
|
putenv('NC_taste');
|
|
}
|
|
|
|
public function testGetValue(): void {
|
|
$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'));
|
|
}
|
|
|
|
public function testGetValueReturnsEnvironmentValueIfSet(): void {
|
|
$config = $this->getConfig();
|
|
$this->assertEquals('bar', $config->getValue('foo'));
|
|
|
|
putenv('NC_foo=baz');
|
|
$config = $this->getConfig();
|
|
$this->assertEquals('baz', $config->getValue('foo'));
|
|
putenv('NC_foo'); // unset the env variable
|
|
}
|
|
|
|
public function testGetValueReturnsEnvironmentValueIfSetToZero(): void {
|
|
$config = $this->getConfig();
|
|
$this->assertEquals('bar', $config->getValue('foo'));
|
|
|
|
putenv('NC_foo=0');
|
|
$config = $this->getConfig();
|
|
$this->assertEquals('0', $config->getValue('foo'));
|
|
putenv('NC_foo'); // unset the env variable
|
|
}
|
|
|
|
public function testGetValueReturnsEnvironmentValueIfSetToFalse(): void {
|
|
$config = $this->getConfig();
|
|
$this->assertEquals('bar', $config->getValue('foo'));
|
|
|
|
putenv('NC_foo=false');
|
|
$config = $this->getConfig();
|
|
$this->assertEquals('false', $config->getValue('foo'));
|
|
putenv('NC_foo'); // unset the env variable
|
|
}
|
|
|
|
public function testSetValue(): void {
|
|
$config = $this->getConfig();
|
|
$config->setValue('foo', 'moo');
|
|
$this->assertSame('moo', $config->getValue('foo'));
|
|
|
|
$content = file_get_contents($this->configFile);
|
|
$expected = "<?php\n";
|
|
$expected .= Config::CONF_WARNING;
|
|
$expected .= "\$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 'top' => \n array (\n 'bottom1' => 'value1',\n ),\n);\n";
|
|
$this->assertEquals($expected, $content);
|
|
|
|
$config->setValue('bar', 'red');
|
|
$config->setValue('apps', ['files', 'gallery']);
|
|
$this->assertSame('red', $config->getValue('bar'));
|
|
$this->assertSame(['files', 'gallery'], $config->getValue('apps'));
|
|
|
|
$content = file_get_contents($this->configFile);
|
|
|
|
$expected = "<?php\n";
|
|
$expected .= Config::CONF_WARNING;
|
|
$expected .= "\$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 'top' => \n array (\n 'bottom1' => 'value1',\n ),\n 'bar' => 'red',\n 'apps' => \n "
|
|
. " array (\n 0 => 'files',\n 1 => 'gallery',\n ),\n);\n";
|
|
$this->assertEquals($expected, $content);
|
|
}
|
|
|
|
public function testSetValues(): void {
|
|
$config = $this->getConfig();
|
|
$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
|
|
$config->setValues([
|
|
'foo' => 'bar',
|
|
'not_exists' => null,
|
|
]);
|
|
|
|
$this->assertSame('bar', $config->getValue('foo'));
|
|
$this->assertSame(null, $config->getValue('not_exists'));
|
|
$content = file_get_contents($this->configFile);
|
|
$this->assertEquals(self::TESTCONTENT, $content);
|
|
|
|
$config->setValues([
|
|
'foo' => 'moo',
|
|
'alcohol_free' => null,
|
|
]);
|
|
$this->assertSame('moo', $config->getValue('foo'));
|
|
$this->assertSame(null, $config->getValue('not_exists'));
|
|
|
|
$content = file_get_contents($this->configFile);
|
|
$expected = "<?php\n";
|
|
$expected .= Config::CONF_WARNING;
|
|
$expected .= "\$CONFIG = array (\n 'foo' => 'moo',\n 'beers' => \n array (\n 0 => 'Appenzeller',\n "
|
|
. " 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n 'top' => \n array (\n 'bottom1' => 'value1',\n ),\n);\n";
|
|
$this->assertEquals($expected, $content);
|
|
}
|
|
|
|
public function testDeleteKey(): void {
|
|
$config = $this->getConfig();
|
|
$config->deleteKey('foo');
|
|
$this->assertSame('this_was_clearly_not_set_before', $config->getValue('foo', 'this_was_clearly_not_set_before'));
|
|
$content = file_get_contents($this->configFile);
|
|
|
|
$expected = "<?php\n";
|
|
$expected .= Config::CONF_WARNING;
|
|
$expected .= "\$CONFIG = array (\n 'beers' => \n array (\n 0 => 'Appenzeller',\n "
|
|
. " 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n 'alcohol_free' => false,\n 'top' => \n array (\n 'bottom1' => 'value1',\n ),\n);\n";
|
|
$this->assertEquals($expected, $content);
|
|
}
|
|
|
|
public function testConfigMerge(): void {
|
|
// Create additional config
|
|
$additionalConfig = '<?php $CONFIG=array("php53"=>"totallyOutdated","alcohol_free"=>true);';
|
|
$additionalConfigPath = $this->randomTmpDir . 'additionalConfig.testconfig.php';
|
|
file_put_contents($additionalConfigPath, $additionalConfig);
|
|
|
|
// Reinstantiate the config to force a read-in of the additional configs
|
|
$config = new Config($this->randomTmpDir, 'testconfig.php');
|
|
|
|
// Ensure that the config value can be read and the config has not been modified
|
|
$this->assertSame('totallyOutdated', $config->getValue('php53', 'bogusValue'));
|
|
$this->assertEquals(self::TESTCONTENT, file_get_contents($this->configFile));
|
|
|
|
// Value is overwritten by additional config file, but not saved to the main config file.
|
|
$this->assertTrue($config->getValue('alcohol_free'));
|
|
|
|
// Set a new key that's saved to the main config file
|
|
$config->setValue('new_key', 'new_value');
|
|
|
|
// Write a new value to the config
|
|
$config->setValue('CoolWebsites', ['demo.owncloud.org', 'owncloud.org', 'owncloud.com']);
|
|
$expected = "<?php\n";
|
|
$expected .= Config::CONF_WARNING;
|
|
$expected .= "\$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 'top' => \n array (\n 'bottom1' => 'value1',\n ),\n 'new_key' => 'new_value',\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);
|
|
}
|
|
|
|
public function testConfigMergeRecursive(): void {
|
|
$additionalConfig = '<?php $CONFIG=["top" => ["bottom2" => "value2"]];';
|
|
$additionalConfigPath = $this->randomTmpDir . 'additionalConfig.testconfig.php';
|
|
file_put_contents($additionalConfigPath, $additionalConfig);
|
|
|
|
$config = new Config($this->randomTmpDir, 'testconfig.php');
|
|
|
|
// Values are merged correctly from the additional config file
|
|
$this->assertEquals(['bottom1' => 'value1', 'bottom2' => 'value2'], $config->getValue('top'));
|
|
|
|
self::invokePrivate($config, 'writeData');
|
|
|
|
// Values from the additional config file are not saved to the main config file
|
|
$expected = "<?php\n";
|
|
$expected .= Config::CONF_WARNING;
|
|
$expected .= "\$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 'top' => \n array (\n 'bottom1' => 'value1',\n ),\n);\n";
|
|
$this->assertEquals($expected, file_get_contents($this->configFile));
|
|
|
|
unlink($additionalConfigPath);
|
|
}
|
|
|
|
/**
|
|
* The current behavior of additional config files is broken.
|
|
* Setting or deleting a key in the current process will remove it from the main config file,
|
|
* but if the key is specified in an additional config file it will just be overwritten again.
|
|
*/
|
|
public function testConfigAdditionalSetDelete(): void {
|
|
$additionalConfig = '<?php $CONFIG=["key1" => "value1", "key2" => "value2"];';
|
|
$additionalConfigPath = $this->randomTmpDir . 'additionalConfig.testconfig.php';
|
|
file_put_contents($additionalConfigPath, $additionalConfig);
|
|
|
|
$config = new Config($this->randomTmpDir, 'testconfig.php');
|
|
|
|
$config->setValue('key1', 'value3');
|
|
$config->deleteKey('key2');
|
|
|
|
// The updated config is written to the main config file
|
|
$expected = "<?php\n";
|
|
$expected .= Config::CONF_WARNING;
|
|
$expected .= "\$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 'top' => \n array (\n 'bottom1' => 'value1',\n ),\n 'key1' => 'value3',\n);\n";
|
|
$this->assertEquals($expected, file_get_contents($this->configFile));
|
|
|
|
$config = new Config($this->randomTmpDir, 'testconfig.php');
|
|
|
|
// The additional config file overwrites the values again
|
|
$this->assertEquals('value1', $config->getValue('key1'));
|
|
$this->assertEquals('value2', $config->getValue('key2'));
|
|
|
|
unlink($additionalConfigPath);
|
|
}
|
|
}
|