ConfigTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test;
  8. use OC\Config;
  9. class ConfigTest extends TestCase {
  10. public const TESTCONTENT = '<?php $CONFIG=array("foo"=>"bar", "beers" => array("Appenzeller", "Guinness", "Kölsch"), "alcohol_free" => false);';
  11. /** @var array */
  12. private $initialConfig = ['foo' => 'bar', 'beers' => ['Appenzeller', 'Guinness', 'Kölsch'], 'alcohol_free' => false];
  13. /** @var string */
  14. private $configFile;
  15. /** @var string */
  16. private $randomTmpDir;
  17. protected function setUp(): void {
  18. parent::setUp();
  19. $this->randomTmpDir = \OC::$server->getTempManager()->getTemporaryFolder();
  20. $this->configFile = $this->randomTmpDir.'testconfig.php';
  21. file_put_contents($this->configFile, self::TESTCONTENT);
  22. }
  23. protected function tearDown(): void {
  24. unlink($this->configFile);
  25. parent::tearDown();
  26. }
  27. private function getConfig(): Config {
  28. return new \OC\Config($this->randomTmpDir, 'testconfig.php');
  29. }
  30. public function testGetKeys(): void {
  31. $expectedConfig = ['foo', 'beers', 'alcohol_free'];
  32. $this->assertSame($expectedConfig, $this->getConfig()->getKeys());
  33. }
  34. public function testGetKeysReturnsEnvironmentKeysIfSet() {
  35. $expectedConfig = ['foo', 'beers', 'alcohol_free', 'taste'];
  36. putenv('NC_taste=great');
  37. $this->assertSame($expectedConfig, $this->getConfig()->getKeys());
  38. putenv('NC_taste');
  39. }
  40. public function testGetValue(): void {
  41. $config = $this->getConfig();
  42. $this->assertSame('bar', $config->getValue('foo'));
  43. $this->assertSame(null, $config->getValue('bar'));
  44. $this->assertSame('moo', $config->getValue('bar', 'moo'));
  45. $this->assertSame(false, $config->getValue('alcohol_free', 'someBogusValue'));
  46. $this->assertSame(['Appenzeller', 'Guinness', 'Kölsch'], $config->getValue('beers', 'someBogusValue'));
  47. $this->assertSame(['Appenzeller', 'Guinness', 'Kölsch'], $config->getValue('beers'));
  48. }
  49. public function testGetValueReturnsEnvironmentValueIfSet(): void {
  50. $config = $this->getConfig();
  51. $this->assertEquals('bar', $config->getValue('foo'));
  52. putenv('NC_foo=baz');
  53. $config = $this->getConfig();
  54. $this->assertEquals('baz', $config->getValue('foo'));
  55. putenv('NC_foo'); // unset the env variable
  56. }
  57. public function testGetValueReturnsEnvironmentValueIfSetToZero(): void {
  58. $config = $this->getConfig();
  59. $this->assertEquals('bar', $config->getValue('foo'));
  60. putenv('NC_foo=0');
  61. $config = $this->getConfig();
  62. $this->assertEquals('0', $config->getValue('foo'));
  63. putenv('NC_foo'); // unset the env variable
  64. }
  65. public function testGetValueReturnsEnvironmentValueIfSetToFalse(): void {
  66. $config = $this->getConfig();
  67. $this->assertEquals('bar', $config->getValue('foo'));
  68. putenv('NC_foo=false');
  69. $config = $this->getConfig();
  70. $this->assertEquals('false', $config->getValue('foo'));
  71. putenv('NC_foo'); // unset the env variable
  72. }
  73. public function testSetValue(): void {
  74. $config = $this->getConfig();
  75. $config->setValue('foo', 'moo');
  76. $this->assertSame('moo', $config->getValue('foo'));
  77. $content = file_get_contents($this->configFile);
  78. $expected = "<?php\n\$CONFIG = array (\n 'foo' => 'moo',\n 'beers' => \n array (\n 0 => 'Appenzeller',\n " .
  79. " 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n 'alcohol_free' => false,\n);\n";
  80. $this->assertEquals($expected, $content);
  81. $config->setValue('bar', 'red');
  82. $config->setValue('apps', ['files', 'gallery']);
  83. $this->assertSame('red', $config->getValue('bar'));
  84. $this->assertSame(['files', 'gallery'], $config->getValue('apps'));
  85. $content = file_get_contents($this->configFile);
  86. $expected = "<?php\n\$CONFIG = array (\n 'foo' => 'moo',\n 'beers' => \n array (\n 0 => 'Appenzeller',\n " .
  87. " 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n 'alcohol_free' => false,\n 'bar' => 'red',\n 'apps' => \n " .
  88. " array (\n 0 => 'files',\n 1 => 'gallery',\n ),\n);\n";
  89. $this->assertEquals($expected, $content);
  90. }
  91. public function testSetValues(): void {
  92. $config = $this->getConfig();
  93. $content = file_get_contents($this->configFile);
  94. $this->assertEquals(self::TESTCONTENT, $content);
  95. // Changing configs to existing values and deleting non-existing once
  96. // should not rewrite the config.php
  97. $config->setValues([
  98. 'foo' => 'bar',
  99. 'not_exists' => null,
  100. ]);
  101. $this->assertSame('bar', $config->getValue('foo'));
  102. $this->assertSame(null, $config->getValue('not_exists'));
  103. $content = file_get_contents($this->configFile);
  104. $this->assertEquals(self::TESTCONTENT, $content);
  105. $config->setValues([
  106. 'foo' => 'moo',
  107. 'alcohol_free' => null,
  108. ]);
  109. $this->assertSame('moo', $config->getValue('foo'));
  110. $this->assertSame(null, $config->getValue('not_exists'));
  111. $content = file_get_contents($this->configFile);
  112. $expected = "<?php\n\$CONFIG = array (\n 'foo' => 'moo',\n 'beers' => \n array (\n 0 => 'Appenzeller',\n " .
  113. " 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n);\n";
  114. $this->assertEquals($expected, $content);
  115. }
  116. public function testDeleteKey(): void {
  117. $config = $this->getConfig();
  118. $config->deleteKey('foo');
  119. $this->assertSame('this_was_clearly_not_set_before', $config->getValue('foo', 'this_was_clearly_not_set_before'));
  120. $content = file_get_contents($this->configFile);
  121. $expected = "<?php\n\$CONFIG = array (\n 'beers' => \n array (\n 0 => 'Appenzeller',\n " .
  122. " 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n 'alcohol_free' => false,\n);\n";
  123. $this->assertEquals($expected, $content);
  124. }
  125. public function testConfigMerge(): void {
  126. // Create additional config
  127. $additionalConfig = '<?php $CONFIG=array("php53"=>"totallyOutdated");';
  128. $additionalConfigPath = $this->randomTmpDir.'additionalConfig.testconfig.php';
  129. file_put_contents($additionalConfigPath, $additionalConfig);
  130. // Reinstantiate the config to force a read-in of the additional configs
  131. $config = new \OC\Config($this->randomTmpDir, 'testconfig.php');
  132. // Ensure that the config value can be read and the config has not been modified
  133. $this->assertSame('totallyOutdated', $config->getValue('php53', 'bogusValue'));
  134. $this->assertEquals(self::TESTCONTENT, file_get_contents($this->configFile));
  135. // Write a new value to the config
  136. $config->setValue('CoolWebsites', ['demo.owncloud.org', 'owncloud.org', 'owncloud.com']);
  137. $expected = "<?php\n\$CONFIG = array (\n 'foo' => 'bar',\n 'beers' => \n array (\n 0 => 'Appenzeller',\n " .
  138. " 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n 'alcohol_free' => false,\n 'php53' => 'totallyOutdated',\n 'CoolWebsites' => \n array (\n " .
  139. " 0 => 'demo.owncloud.org',\n 1 => 'owncloud.org',\n 2 => 'owncloud.com',\n ),\n);\n";
  140. $this->assertEquals($expected, file_get_contents($this->configFile));
  141. // Cleanup
  142. unlink($additionalConfigPath);
  143. }
  144. }