config.php 5.7 KB

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