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.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. const TESTCONTENT = '<?php $CONFIG=array("foo"=>"bar", "beers" => array("Appenzeller", "Guinness", "Kölsch"), "alcohol_free" => false);';
  11. /** @var array */
  12. private $initialConfig = array('foo' => 'bar', 'beers' => array('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() {
  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() {
  27. unlink($this->configFile);
  28. parent::tearDown();
  29. }
  30. public function testGetKeys() {
  31. $expectedConfig = array('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(array('Appenzeller', 'Guinness', 'Kölsch'), $this->config->getValue('beers', 'someBogusValue'));
  40. $this->assertSame(array('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. $expectedConfig = $this->initialConfig;
  63. $expectedConfig['foo'] = 'moo';
  64. $this->assertAttributeEquals($expectedConfig, 'cache', $this->config);
  65. $content = file_get_contents($this->configFile);
  66. $expected = "<?php\n\$CONFIG = array (\n 'foo' => 'moo',\n 'beers' => \n array (\n 0 => 'Appenzeller',\n " .
  67. " 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n 'alcohol_free' => false,\n);\n";
  68. $this->assertEquals($expected, $content);
  69. $this->config->setValue('bar', 'red');
  70. $this->config->setValue('apps', array('files', 'gallery'));
  71. $expectedConfig['bar'] = 'red';
  72. $expectedConfig['apps'] = array('files', 'gallery');
  73. $this->assertAttributeEquals($expectedConfig, 'cache', $this->config);
  74. $content = file_get_contents($this->configFile);
  75. $expected = "<?php\n\$CONFIG = array (\n 'foo' => 'moo',\n 'beers' => \n array (\n 0 => 'Appenzeller',\n " .
  76. " 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n 'alcohol_free' => false,\n 'bar' => 'red',\n 'apps' => \n " .
  77. " array (\n 0 => 'files',\n 1 => 'gallery',\n ),\n);\n";
  78. $this->assertEquals($expected, $content);
  79. }
  80. public function testSetValues() {
  81. $content = file_get_contents($this->configFile);
  82. $this->assertEquals(self::TESTCONTENT, $content);
  83. // Changing configs to existing values and deleting non-existing once
  84. // should not rewrite the config.php
  85. $this->config->setValues([
  86. 'foo' => 'bar',
  87. 'not_exists' => null,
  88. ]);
  89. $this->assertAttributeEquals($this->initialConfig, 'cache', $this->config);
  90. $content = file_get_contents($this->configFile);
  91. $this->assertEquals(self::TESTCONTENT, $content);
  92. $this->config->setValues([
  93. 'foo' => 'moo',
  94. 'alcohol_free' => null,
  95. ]);
  96. $expectedConfig = $this->initialConfig;
  97. $expectedConfig['foo'] = 'moo';
  98. unset($expectedConfig['alcohol_free']);
  99. $this->assertAttributeEquals($expectedConfig, 'cache', $this->config);
  100. $content = file_get_contents($this->configFile);
  101. $expected = "<?php\n\$CONFIG = array (\n 'foo' => 'moo',\n 'beers' => \n array (\n 0 => 'Appenzeller',\n " .
  102. " 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n);\n";
  103. $this->assertEquals($expected, $content);
  104. }
  105. public function testDeleteKey() {
  106. $this->config->deleteKey('foo');
  107. $expectedConfig = $this->initialConfig;
  108. unset($expectedConfig['foo']);
  109. $this->assertAttributeEquals($expectedConfig, 'cache', $this->config);
  110. $content = file_get_contents($this->configFile);
  111. $expected = "<?php\n\$CONFIG = array (\n 'beers' => \n array (\n 0 => 'Appenzeller',\n " .
  112. " 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n 'alcohol_free' => false,\n);\n";
  113. $this->assertEquals($expected, $content);
  114. }
  115. public function testConfigMerge() {
  116. // Create additional config
  117. $additionalConfig = '<?php $CONFIG=array("php53"=>"totallyOutdated");';
  118. $additionalConfigPath = $this->randomTmpDir.'additionalConfig.testconfig.php';
  119. file_put_contents($additionalConfigPath, $additionalConfig);
  120. // Reinstantiate the config to force a read-in of the additional configs
  121. $this->config = new \OC\Config($this->randomTmpDir, 'testconfig.php');
  122. // Ensure that the config value can be read and the config has not been modified
  123. $this->assertSame('totallyOutdated', $this->config->getValue('php53', 'bogusValue'));
  124. $this->assertEquals(self::TESTCONTENT, file_get_contents($this->configFile));
  125. // Write a new value to the config
  126. $this->config->setValue('CoolWebsites', array('demo.owncloud.org', 'owncloud.org', 'owncloud.com'));
  127. $expected = "<?php\n\$CONFIG = array (\n 'foo' => 'bar',\n 'beers' => \n array (\n 0 => 'Appenzeller',\n " .
  128. " 1 => 'Guinness',\n 2 => 'Kölsch',\n ),\n 'alcohol_free' => false,\n 'php53' => 'totallyOutdated',\n 'CoolWebsites' => \n array (\n " .
  129. " 0 => 'demo.owncloud.org',\n 1 => 'owncloud.org',\n 2 => 'owncloud.com',\n ),\n);\n";
  130. $this->assertEquals($expected, file_get_contents($this->configFile));
  131. // Cleanup
  132. unlink($additionalConfigPath);
  133. }
  134. }