ConfigTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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() {
  31. $expectedConfig = ['foo', 'beers', 'alcohol_free'];
  32. $this->assertSame($expectedConfig, $this->getConfig()->getKeys());
  33. }
  34. public function testGetValue() {
  35. $config = $this->getConfig();
  36. $this->assertSame('bar', $config->getValue('foo'));
  37. $this->assertSame(null, $config->getValue('bar'));
  38. $this->assertSame('moo', $config->getValue('bar', 'moo'));
  39. $this->assertSame(false, $config->getValue('alcohol_free', 'someBogusValue'));
  40. $this->assertSame(['Appenzeller', 'Guinness', 'Kölsch'], $config->getValue('beers', 'someBogusValue'));
  41. $this->assertSame(['Appenzeller', 'Guinness', 'Kölsch'], $config->getValue('beers'));
  42. }
  43. public function testGetValueReturnsEnvironmentValueIfSet() {
  44. $config = $this->getConfig();
  45. $this->assertEquals('bar', $config->getValue('foo'));
  46. putenv('NC_foo=baz');
  47. $config = $this->getConfig();
  48. $this->assertEquals('baz', $config->getValue('foo'));
  49. putenv('NC_foo'); // unset the env variable
  50. }
  51. public function testGetValueReturnsEnvironmentValueIfSetToZero() {
  52. $config = $this->getConfig();
  53. $this->assertEquals('bar', $config->getValue('foo'));
  54. putenv('NC_foo=0');
  55. $config = $this->getConfig();
  56. $this->assertEquals('0', $config->getValue('foo'));
  57. putenv('NC_foo'); // unset the env variable
  58. }
  59. public function testGetValueReturnsEnvironmentValueIfSetToFalse() {
  60. $config = $this->getConfig();
  61. $this->assertEquals('bar', $config->getValue('foo'));
  62. putenv('NC_foo=false');
  63. $config = $this->getConfig();
  64. $this->assertEquals('false', $config->getValue('foo'));
  65. putenv('NC_foo'); // unset the env variable
  66. }
  67. public function testSetValue() {
  68. $config = $this->getConfig();
  69. $config->setValue('foo', 'moo');
  70. $this->assertSame('moo', $config->getValue('foo'));
  71. $content = file_get_contents($this->configFile);
  72. $expected = "<?php\n\$CONFIG = array (\n 'foo' => 'moo',\n 'beers' => \n array (\n 0 => 'Appenzeller',\n " .
  73. " 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n 'alcohol_free' => false,\n);\n";
  74. $this->assertEquals($expected, $content);
  75. $config->setValue('bar', 'red');
  76. $config->setValue('apps', ['files', 'gallery']);
  77. $this->assertSame('red', $config->getValue('bar'));
  78. $this->assertSame(['files', 'gallery'], $config->getValue('apps'));
  79. $content = file_get_contents($this->configFile);
  80. $expected = "<?php\n\$CONFIG = array (\n 'foo' => 'moo',\n 'beers' => \n array (\n 0 => 'Appenzeller',\n " .
  81. " 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n 'alcohol_free' => false,\n 'bar' => 'red',\n 'apps' => \n " .
  82. " array (\n 0 => 'files',\n 1 => 'gallery',\n ),\n);\n";
  83. $this->assertEquals($expected, $content);
  84. }
  85. public function testSetValues() {
  86. $config = $this->getConfig();
  87. $content = file_get_contents($this->configFile);
  88. $this->assertEquals(self::TESTCONTENT, $content);
  89. // Changing configs to existing values and deleting non-existing once
  90. // should not rewrite the config.php
  91. $config->setValues([
  92. 'foo' => 'bar',
  93. 'not_exists' => null,
  94. ]);
  95. $this->assertSame('bar', $config->getValue('foo'));
  96. $this->assertSame(null, $config->getValue('not_exists'));
  97. $content = file_get_contents($this->configFile);
  98. $this->assertEquals(self::TESTCONTENT, $content);
  99. $config->setValues([
  100. 'foo' => 'moo',
  101. 'alcohol_free' => null,
  102. ]);
  103. $this->assertSame('moo', $config->getValue('foo'));
  104. $this->assertSame(null, $config->getValue('not_exists'));
  105. $content = file_get_contents($this->configFile);
  106. $expected = "<?php\n\$CONFIG = array (\n 'foo' => 'moo',\n 'beers' => \n array (\n 0 => 'Appenzeller',\n " .
  107. " 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n);\n";
  108. $this->assertEquals($expected, $content);
  109. }
  110. public function testDeleteKey() {
  111. $config = $this->getConfig();
  112. $config->deleteKey('foo');
  113. $this->assertSame('this_was_clearly_not_set_before', $config->getValue('foo', 'this_was_clearly_not_set_before'));
  114. $content = file_get_contents($this->configFile);
  115. $expected = "<?php\n\$CONFIG = array (\n 'beers' => \n array (\n 0 => 'Appenzeller',\n " .
  116. " 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n 'alcohol_free' => false,\n);\n";
  117. $this->assertEquals($expected, $content);
  118. }
  119. public function testConfigMerge() {
  120. // Create additional config
  121. $additionalConfig = '<?php $CONFIG=array("php53"=>"totallyOutdated");';
  122. $additionalConfigPath = $this->randomTmpDir.'additionalConfig.testconfig.php';
  123. file_put_contents($additionalConfigPath, $additionalConfig);
  124. // Reinstantiate the config to force a read-in of the additional configs
  125. $config = new \OC\Config($this->randomTmpDir, 'testconfig.php');
  126. // Ensure that the config value can be read and the config has not been modified
  127. $this->assertSame('totallyOutdated', $config->getValue('php53', 'bogusValue'));
  128. $this->assertEquals(self::TESTCONTENT, file_get_contents($this->configFile));
  129. // Write a new value to the config
  130. $config->setValue('CoolWebsites', ['demo.owncloud.org', 'owncloud.org', 'owncloud.com']);
  131. $expected = "<?php\n\$CONFIG = array (\n 'foo' => 'bar',\n 'beers' => \n array (\n 0 => 'Appenzeller',\n " .
  132. " 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n 'alcohol_free' => false,\n 'php53' => 'totallyOutdated',\n 'CoolWebsites' => \n array (\n " .
  133. " 0 => 'demo.owncloud.org',\n 1 => 'owncloud.org',\n 2 => 'owncloud.com',\n ),\n);\n";
  134. $this->assertEquals($expected, file_get_contents($this->configFile));
  135. // Cleanup
  136. unlink($additionalConfigPath);
  137. }
  138. }