aboutsummaryrefslogtreecommitdiffstats
path: root/tests/lib/config.php
blob: 1a1d062d6882626154f81c47c8540580425505fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?php
/**
 * Copyright (c) 2013 Bart Visscher <bartv@thisnet.nl>
 * This file is licensed under the Affero General Public License version 3 or
 * later.
 * See the COPYING-README file.
 */

class Test_Config extends PHPUnit_Framework_TestCase {
	const CONFIG_FILE = 'static://config.php';
	const CONFIG_DIR = 'static://';
	const TESTCONTENT = '<?php $CONFIG=array("foo"=>"bar");';

	/**
	 * @var \OC\Config
	 */
	private $config;

	function setUp() {
		file_put_contents(self::CONFIG_FILE, self::TESTCONTENT);
		$this->config = new OC\Config(self::CONFIG_DIR);
	}

	public function testReadData() {
		$config = new OC\Config('/non-existing');
		$this->assertAttributeEquals(array(), 'cache', $config);

		$this->assertAttributeEquals(array('foo' => 'bar'), 'cache', $this->config);
	}

	public function testGetKeys() {
		$this->assertEquals(array('foo'), $this->config->getKeys());
	}

	public function testGetValue() {
		$this->assertEquals('bar', $this->config->getValue('foo'));
		$this->assertEquals(null, $this->config->getValue('bar'));
		$this->assertEquals('moo', $this->config->getValue('bar', 'moo'));
	}

	public function testSetValue() {
		$this->config->setDebugMode(false);
		$this->config->setValue('foo', 'moo');
		$this->assertAttributeEquals(array('foo' => 'moo'), 'cache', $this->config);
		$content = file_get_contents(self::CONFIG_FILE);

		$expected = "<?php\n\$CONFIG = array (\n  'foo' => 'moo',\n);\n";
		$this->assertEquals($expected, $content);
		$this->config->setValue('bar', 'red');
		$this->assertAttributeEquals(array('foo' => 'moo', 'bar' => 'red'), 'cache', $this->config);
		$content = file_get_contents(self::CONFIG_FILE);

		$expected = "<?php\n\$CONFIG = array (\n  'foo' => 'moo',\n  'bar' => 'red',\n);\n";
		$this->assertEquals($expected, $content);
	}

	public function testDeleteKey() {
		$this->config->setDebugMode(false);
		$this->config->deleteKey('foo');
		$this->assertAttributeEquals(array(), 'cache', $this->config);
		$content = file_get_contents(self::CONFIG_FILE);

		$expected = "<?php\n\$CONFIG = array (\n);\n";
		$this->assertEquals($expected, $content);
	}

	public function testSavingDebugMode() {
		$this->config->setDebugMode(true);
		$this->config->deleteKey('foo'); // change something so we save to the config file
		$this->assertAttributeEquals(array(), 'cache', $this->config);
		$this->assertAttributeEquals(true, 'debugMode', $this->config);
		$content = file_get_contents(self::CONFIG_FILE);

		$expected = "<?php\ndefine('DEBUG',true);\n\$CONFIG = array (\n);\n";
		$this->assertEquals($expected, $content);
	}

	/**
	 * @expectedException \OC\HintException
	 */
	public function testWriteData() {
		$config = new OC\Config('/non-writable');
		// TODO never get's called, because the previous call throws the exception
		// maybe include some more logic to create a readable dir and then try to
		// write to this dir
		//
		// console commands:
		// $ sudo touch /non-writableconfig.php
		// $ sudo chmod go-rwx /non-writableconfig.php
		// ---- call the tests now -> above statemant throws the exception
		//
		// $ sudo chmod go+r /non-writableconfig.php
		// ---- call the tests now -> bellow statemant throws the exception
		$config->setValue('foo', 'bar');
	}
}