diff options
Diffstat (limited to 'tests/lib/ConfigTest.php')
-rw-r--r-- | tests/lib/ConfigTest.php | 69 |
1 files changed, 39 insertions, 30 deletions
diff --git a/tests/lib/ConfigTest.php b/tests/lib/ConfigTest.php index 7c289472abd..b2a52dbd6a4 100644 --- a/tests/lib/ConfigTest.php +++ b/tests/lib/ConfigTest.php @@ -1,14 +1,16 @@ <?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. + * 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);'; @@ -23,8 +25,8 @@ class ConfigTest extends TestCase { protected function setUp(): void { parent::setUp(); - $this->randomTmpDir = \OC::$server->getTempManager()->getTemporaryFolder(); - $this->configFile = $this->randomTmpDir.'testconfig.php'; + $this->randomTmpDir = Server::get(ITempManager::class)->getTemporaryFolder(); + $this->configFile = $this->randomTmpDir . 'testconfig.php'; file_put_contents($this->configFile, self::TESTCONTENT); } @@ -34,15 +36,22 @@ class ConfigTest extends TestCase { } private function getConfig(): Config { - return new \OC\Config($this->randomTmpDir, 'testconfig.php'); + return new Config($this->randomTmpDir, 'testconfig.php'); } - public function testGetKeys() { + public function testGetKeys(): void { $expectedConfig = ['foo', 'beers', 'alcohol_free']; $this->assertSame($expectedConfig, $this->getConfig()->getKeys()); } - public function testGetValue() { + public function testGetKeysReturnsEnvironmentKeysIfSet() { + $expectedConfig = ['foo', 'beers', 'alcohol_free', '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')); @@ -52,7 +61,7 @@ class ConfigTest extends TestCase { $this->assertSame(['Appenzeller', 'Guinness', 'Kölsch'], $config->getValue('beers')); } - public function testGetValueReturnsEnvironmentValueIfSet() { + public function testGetValueReturnsEnvironmentValueIfSet(): void { $config = $this->getConfig(); $this->assertEquals('bar', $config->getValue('foo')); @@ -62,7 +71,7 @@ class ConfigTest extends TestCase { putenv('NC_foo'); // unset the env variable } - public function testGetValueReturnsEnvironmentValueIfSetToZero() { + public function testGetValueReturnsEnvironmentValueIfSetToZero(): void { $config = $this->getConfig(); $this->assertEquals('bar', $config->getValue('foo')); @@ -72,7 +81,7 @@ class ConfigTest extends TestCase { putenv('NC_foo'); // unset the env variable } - public function testGetValueReturnsEnvironmentValueIfSetToFalse() { + public function testGetValueReturnsEnvironmentValueIfSetToFalse(): void { $config = $this->getConfig(); $this->assertEquals('bar', $config->getValue('foo')); @@ -82,14 +91,14 @@ class ConfigTest extends TestCase { putenv('NC_foo'); // unset the env variable } - public function testSetValue() { + 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\$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"; + $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"; $this->assertEquals($expected, $content); $config->setValue('bar', 'red'); @@ -99,13 +108,13 @@ class ConfigTest extends TestCase { $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"; + $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"; $this->assertEquals($expected, $content); } - public function testSetValues() { + public function testSetValues(): void { $config = $this->getConfig(); $content = file_get_contents($this->configFile); $this->assertEquals(self::TESTCONTENT, $content); @@ -130,30 +139,30 @@ class ConfigTest extends TestCase { $this->assertSame(null, $config->getValue('not_exists')); $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"; + $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); } - public function testDeleteKey() { + 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\$CONFIG = array (\n 'beers' => \n array (\n 0 => 'Appenzeller',\n " . - " 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n 'alcohol_free' => false,\n);\n"; + $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"; $this->assertEquals($expected, $content); } - public function testConfigMerge() { + public function testConfigMerge(): void { // Create additional config $additionalConfig = '<?php $CONFIG=array("php53"=>"totallyOutdated");'; - $additionalConfigPath = $this->randomTmpDir.'additionalConfig.testconfig.php'; + $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 \OC\Config($this->randomTmpDir, 'testconfig.php'); + $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')); @@ -161,9 +170,9 @@ class ConfigTest extends TestCase { // Write a new value to the config $config->setValue('CoolWebsites', ['demo.owncloud.org', 'owncloud.org', 'owncloud.com']); - $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"; + $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 |