ConfigTest.php 5.7 KB

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