1
0

ConfigTest.php 6.7 KB

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