ConfigTest.php 6.5 KB

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