You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConfigTest.php 6.3KB

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