config.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 PHPUnit_Framework_TestCase {
  9. const CONFIG_FILE = 'static://config.php';
  10. const CONFIG_DIR = 'static://';
  11. const TESTCONTENT = '<?php $CONFIG=array("foo"=>"bar");';
  12. /**
  13. * @var \OC\Config
  14. */
  15. private $config;
  16. function setUp() {
  17. file_put_contents(self::CONFIG_FILE, self::TESTCONTENT);
  18. $this->config = new OC\Config(self::CONFIG_DIR);
  19. }
  20. public function testReadData() {
  21. $config = new OC\Config('/non-existing');
  22. $this->assertAttributeEquals(array(), 'cache', $config);
  23. $this->assertAttributeEquals(array('foo' => 'bar'), 'cache', $this->config);
  24. }
  25. public function testGetKeys() {
  26. $this->assertEquals(array('foo'), $this->config->getKeys());
  27. }
  28. public function testGetValue() {
  29. $this->assertEquals('bar', $this->config->getValue('foo'));
  30. $this->assertEquals(null, $this->config->getValue('bar'));
  31. $this->assertEquals('moo', $this->config->getValue('bar', 'moo'));
  32. }
  33. public function testSetValue() {
  34. $this->config->setDebugMode(false);
  35. $this->config->setValue('foo', 'moo');
  36. $this->assertAttributeEquals(array('foo' => 'moo'), 'cache', $this->config);
  37. $content = file_get_contents(self::CONFIG_FILE);
  38. $expected = "<?php\n\$CONFIG = array (\n 'foo' => 'moo',\n);\n";
  39. $this->assertEquals($expected, $content);
  40. $this->config->setValue('bar', 'red');
  41. $this->assertAttributeEquals(array('foo' => 'moo', 'bar' => 'red'), 'cache', $this->config);
  42. $content = file_get_contents(self::CONFIG_FILE);
  43. $expected = "<?php\n\$CONFIG = array (\n 'foo' => 'moo',\n 'bar' => 'red',\n);\n";
  44. $this->assertEquals($expected, $content);
  45. }
  46. public function testDeleteKey() {
  47. $this->config->setDebugMode(false);
  48. $this->config->deleteKey('foo');
  49. $this->assertAttributeEquals(array(), 'cache', $this->config);
  50. $content = file_get_contents(self::CONFIG_FILE);
  51. $expected = "<?php\n\$CONFIG = array (\n);\n";
  52. $this->assertEquals($expected, $content);
  53. }
  54. public function testSavingDebugMode() {
  55. $this->config->setDebugMode(true);
  56. $this->config->deleteKey('foo'); // change something so we save to the config file
  57. $this->assertAttributeEquals(array(), 'cache', $this->config);
  58. $this->assertAttributeEquals(true, 'debugMode', $this->config);
  59. $content = file_get_contents(self::CONFIG_FILE);
  60. $expected = "<?php\ndefine('DEBUG',true);\n\$CONFIG = array (\n);\n";
  61. $this->assertEquals($expected, $content);
  62. }
  63. /**
  64. * @expectedException \OC\HintException
  65. */
  66. public function testWriteData() {
  67. $config = new OC\Config('/non-writable');
  68. $config->setValue('foo', 'bar');
  69. }
  70. }