From 07df94def66a78bda40560a5bdd31058f61e2238 Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Sun, 3 Mar 2013 12:06:00 +0100 Subject: Convert OC_Config to object interface --- tests/lib/config.php | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 tests/lib/config.php (limited to 'tests') diff --git a/tests/lib/config.php b/tests/lib/config.php new file mode 100644 index 00000000000..e22bf3fd7de --- /dev/null +++ b/tests/lib/config.php @@ -0,0 +1,113 @@ + + * 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 = '"bar");'; + + public function testReadData() + { + $config = new OC\Config(self::CONFIG_DIR, false); + $this->assertAttributeEquals(array(), 'cache', $config); + + file_put_contents(self::CONFIG_FILE, self::TESTCONTENT); + $config = new OC\Config(self::CONFIG_DIR, false); + $this->assertAttributeEquals(array('foo'=>'bar'), 'cache', $config); + } + + public function testGetKeys() + { + file_put_contents(self::CONFIG_FILE, self::TESTCONTENT); + $config = new OC\Config(self::CONFIG_DIR, false); + $this->assertEquals(array('foo'), $config->getKeys()); + } + + public function testGetValue() + { + file_put_contents(self::CONFIG_FILE, self::TESTCONTENT); + $config = new OC\Config(self::CONFIG_DIR, false); + $this->assertEquals('bar', $config->getValue('foo')); + $this->assertEquals(null, $config->getValue('bar')); + $this->assertEquals('moo', $config->getValue('bar', 'moo')); + } + + public function testSetValue() + { + file_put_contents(self::CONFIG_FILE, self::TESTCONTENT); + $config = new OC\Config(self::CONFIG_DIR, false); + $config->setValue('foo', 'moo'); + $this->assertAttributeEquals(array('foo'=>'moo'), 'cache', $config); + $content = file_get_contents(self::CONFIG_FILE); + $this->assertEquals(<< 'moo', +); + +EOL +, $content); + $config->setValue('bar', 'red'); + $this->assertAttributeEquals(array('foo'=>'moo', 'bar'=>'red'), 'cache', $config); + $content = file_get_contents(self::CONFIG_FILE); + $this->assertEquals(<< 'moo', + 'bar' => 'red', +); + +EOL +, $content); + } + + public function testDeleteKey() + { + file_put_contents(self::CONFIG_FILE, self::TESTCONTENT); + $config = new OC\Config(self::CONFIG_DIR, false); + $config->deleteKey('foo'); + $this->assertAttributeEquals(array(), 'cache', $config); + $content = file_get_contents(self::CONFIG_FILE); + $this->assertEquals(<<deleteKey('foo'); // change something so we save to the config file + $this->assertAttributeEquals(array(), 'cache', $config); + $this->assertAttributeEquals(true, 'debug_mode', $config); + $content = file_get_contents(self::CONFIG_FILE); + $this->assertEquals(<<setValue('foo', 'bar'); + } catch (\OC\HintException $e) { + return; + } + $this->fail(); + } +} -- cgit v1.2.3 From 969e43c87b7afb6184846fe27849167c9c6f5eab Mon Sep 17 00:00:00 2001 From: Michael Gapczynski Date: Mon, 10 Jun 2013 12:07:25 -0400 Subject: Can't determine if debug mode is defined until we read the config --- lib/config.php | 9 +++------ lib/legacy/config.php | 2 +- tests/lib/config.php | 18 +++++++++--------- 3 files changed, 13 insertions(+), 16 deletions(-) (limited to 'tests') diff --git a/lib/config.php b/lib/config.php index 4003339ea5c..a3663949d16 100644 --- a/lib/config.php +++ b/lib/config.php @@ -47,11 +47,8 @@ class Config { protected $configDir; protected $configFilename; - protected $debugMode; - - public function __construct($configDir, $debugMode) { + public function __construct($configDir) { $this->configDir = $configDir; - $this->debugMode = $debugMode; $this->configFilename = $this->configDir.'config.php'; $this->readData(); } @@ -152,7 +149,7 @@ class Config { private function writeData() { // Create a php file ... $content = "debugMode) { + if (defined('DEBUG') && DEBUG) { $content .= "define('DEBUG',true);\n"; } $content .= '$CONFIG = '; @@ -167,7 +164,7 @@ class Config { 'You can usually fix this by giving the webserver user write access' .' to the config directory in ownCloud'); } - // Prevent others not to read the config + // Prevent others from reading the config @chmod($this->configFilename, 0640); } } diff --git a/lib/legacy/config.php b/lib/legacy/config.php index 635f0af66f8..f68d7c31b25 100644 --- a/lib/legacy/config.php +++ b/lib/legacy/config.php @@ -38,7 +38,7 @@ * This class is responsible for reading and writing config.php, the very basic * configuration file of ownCloud. */ -OC_Config::$object = new \OC\Config(OC::$SERVERROOT.'/config/', defined('DEBUG') && DEBUG); +OC_Config::$object = new \OC\Config(OC::$SERVERROOT.'/config/'); class OC_Config { public static $object; diff --git a/tests/lib/config.php b/tests/lib/config.php index e22bf3fd7de..acc2a536fd0 100644 --- a/tests/lib/config.php +++ b/tests/lib/config.php @@ -13,25 +13,25 @@ class Test_Config extends PHPUnit_Framework_TestCase { public function testReadData() { - $config = new OC\Config(self::CONFIG_DIR, false); + $config = new OC\Config(self::CONFIG_DIR); $this->assertAttributeEquals(array(), 'cache', $config); file_put_contents(self::CONFIG_FILE, self::TESTCONTENT); - $config = new OC\Config(self::CONFIG_DIR, false); + $config = new OC\Config(self::CONFIG_DIR); $this->assertAttributeEquals(array('foo'=>'bar'), 'cache', $config); } public function testGetKeys() { file_put_contents(self::CONFIG_FILE, self::TESTCONTENT); - $config = new OC\Config(self::CONFIG_DIR, false); + $config = new OC\Config(self::CONFIG_DIR); $this->assertEquals(array('foo'), $config->getKeys()); } public function testGetValue() { file_put_contents(self::CONFIG_FILE, self::TESTCONTENT); - $config = new OC\Config(self::CONFIG_DIR, false); + $config = new OC\Config(self::CONFIG_DIR); $this->assertEquals('bar', $config->getValue('foo')); $this->assertEquals(null, $config->getValue('bar')); $this->assertEquals('moo', $config->getValue('bar', 'moo')); @@ -40,7 +40,7 @@ class Test_Config extends PHPUnit_Framework_TestCase { public function testSetValue() { file_put_contents(self::CONFIG_FILE, self::TESTCONTENT); - $config = new OC\Config(self::CONFIG_DIR, false); + $config = new OC\Config(self::CONFIG_DIR); $config->setValue('foo', 'moo'); $this->assertAttributeEquals(array('foo'=>'moo'), 'cache', $config); $content = file_get_contents(self::CONFIG_FILE); @@ -69,7 +69,7 @@ EOL public function testDeleteKey() { file_put_contents(self::CONFIG_FILE, self::TESTCONTENT); - $config = new OC\Config(self::CONFIG_DIR, false); + $config = new OC\Config(self::CONFIG_DIR); $config->deleteKey('foo'); $this->assertAttributeEquals(array(), 'cache', $config); $content = file_get_contents(self::CONFIG_FILE); @@ -84,11 +84,11 @@ EOL public function testSavingDebugMode() { + define('DEBUG',true); file_put_contents(self::CONFIG_FILE, self::TESTCONTENT); - $config = new OC\Config(self::CONFIG_DIR, true); + $config = new OC\Config(self::CONFIG_DIR); $config->deleteKey('foo'); // change something so we save to the config file $this->assertAttributeEquals(array(), 'cache', $config); - $this->assertAttributeEquals(true, 'debug_mode', $config); $content = file_get_contents(self::CONFIG_FILE); $this->assertEquals(<<setValue('foo', 'bar'); } catch (\OC\HintException $e) { -- cgit v1.2.3 From 194b61b4c507e58eab0750ab40ed6eb6f085c06a Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Thu, 27 Jun 2013 22:24:17 +0200 Subject: Revert "Can't determine if debug mode is defined until we read the config" This reverts commit 969e43c87b7afb6184846fe27849167c9c6f5eab. --- lib/config.php | 9 ++++++--- lib/legacy/config.php | 2 +- tests/lib/config.php | 18 +++++++++--------- 3 files changed, 16 insertions(+), 13 deletions(-) (limited to 'tests') diff --git a/lib/config.php b/lib/config.php index 7ccbc050508..adf70ac841a 100644 --- a/lib/config.php +++ b/lib/config.php @@ -47,8 +47,11 @@ class Config { protected $configDir; protected $configFilename; - public function __construct($configDir) { + protected $debugMode; + + public function __construct($configDir, $debugMode) { $this->configDir = $configDir; + $this->debugMode = $debugMode; $this->configFilename = $this->configDir.'config.php'; $this->readData(); } @@ -149,7 +152,7 @@ class Config { private function writeData() { // Create a php file ... $content = "debugMode) { $content .= "define('DEBUG',true);\n"; } $content .= '$CONFIG = '; @@ -164,7 +167,7 @@ class Config { 'You can usually fix this by giving the webserver user write access' .' to the config directory in ownCloud'); } - // Prevent others from reading the config + // Prevent others not to read the config @chmod($this->configFilename, 0640); \OC_Util::clearOpcodeCache(); } diff --git a/lib/legacy/config.php b/lib/legacy/config.php index f68d7c31b25..635f0af66f8 100644 --- a/lib/legacy/config.php +++ b/lib/legacy/config.php @@ -38,7 +38,7 @@ * This class is responsible for reading and writing config.php, the very basic * configuration file of ownCloud. */ -OC_Config::$object = new \OC\Config(OC::$SERVERROOT.'/config/'); +OC_Config::$object = new \OC\Config(OC::$SERVERROOT.'/config/', defined('DEBUG') && DEBUG); class OC_Config { public static $object; diff --git a/tests/lib/config.php b/tests/lib/config.php index acc2a536fd0..e22bf3fd7de 100644 --- a/tests/lib/config.php +++ b/tests/lib/config.php @@ -13,25 +13,25 @@ class Test_Config extends PHPUnit_Framework_TestCase { public function testReadData() { - $config = new OC\Config(self::CONFIG_DIR); + $config = new OC\Config(self::CONFIG_DIR, false); $this->assertAttributeEquals(array(), 'cache', $config); file_put_contents(self::CONFIG_FILE, self::TESTCONTENT); - $config = new OC\Config(self::CONFIG_DIR); + $config = new OC\Config(self::CONFIG_DIR, false); $this->assertAttributeEquals(array('foo'=>'bar'), 'cache', $config); } public function testGetKeys() { file_put_contents(self::CONFIG_FILE, self::TESTCONTENT); - $config = new OC\Config(self::CONFIG_DIR); + $config = new OC\Config(self::CONFIG_DIR, false); $this->assertEquals(array('foo'), $config->getKeys()); } public function testGetValue() { file_put_contents(self::CONFIG_FILE, self::TESTCONTENT); - $config = new OC\Config(self::CONFIG_DIR); + $config = new OC\Config(self::CONFIG_DIR, false); $this->assertEquals('bar', $config->getValue('foo')); $this->assertEquals(null, $config->getValue('bar')); $this->assertEquals('moo', $config->getValue('bar', 'moo')); @@ -40,7 +40,7 @@ class Test_Config extends PHPUnit_Framework_TestCase { public function testSetValue() { file_put_contents(self::CONFIG_FILE, self::TESTCONTENT); - $config = new OC\Config(self::CONFIG_DIR); + $config = new OC\Config(self::CONFIG_DIR, false); $config->setValue('foo', 'moo'); $this->assertAttributeEquals(array('foo'=>'moo'), 'cache', $config); $content = file_get_contents(self::CONFIG_FILE); @@ -69,7 +69,7 @@ EOL public function testDeleteKey() { file_put_contents(self::CONFIG_FILE, self::TESTCONTENT); - $config = new OC\Config(self::CONFIG_DIR); + $config = new OC\Config(self::CONFIG_DIR, false); $config->deleteKey('foo'); $this->assertAttributeEquals(array(), 'cache', $config); $content = file_get_contents(self::CONFIG_FILE); @@ -84,11 +84,11 @@ EOL public function testSavingDebugMode() { - define('DEBUG',true); file_put_contents(self::CONFIG_FILE, self::TESTCONTENT); - $config = new OC\Config(self::CONFIG_DIR); + $config = new OC\Config(self::CONFIG_DIR, true); $config->deleteKey('foo'); // change something so we save to the config file $this->assertAttributeEquals(array(), 'cache', $config); + $this->assertAttributeEquals(true, 'debug_mode', $config); $content = file_get_contents(self::CONFIG_FILE); $this->assertEquals(<<setValue('foo', 'bar'); } catch (\OC\HintException $e) { -- cgit v1.2.3 From 12976fb2e1f6a4d6a054ba2b620f0e7707ce2c69 Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Thu, 27 Jun 2013 22:50:28 +0200 Subject: Set debugMode after reading the config file --- lib/config.php | 9 +++++++-- lib/legacy/config.php | 2 +- tests/lib/config.php | 50 ++++++++++++++++++++++---------------------------- 3 files changed, 30 insertions(+), 31 deletions(-) (limited to 'tests') diff --git a/lib/config.php b/lib/config.php index adf70ac841a..afd74c56b40 100644 --- a/lib/config.php +++ b/lib/config.php @@ -49,12 +49,17 @@ class Config { protected $debugMode; - public function __construct($configDir, $debugMode) { + public function __construct($configDir) { $this->configDir = $configDir; - $this->debugMode = $debugMode; $this->configFilename = $this->configDir.'config.php'; $this->readData(); + $this->setDebugMode(defined('DEBUG') && DEBUG); } + + public function setDebugMode($enable) { + $this->debugMode = $enable; + } + /** * @brief Lists all available config keys * @return array with key names diff --git a/lib/legacy/config.php b/lib/legacy/config.php index 635f0af66f8..f68d7c31b25 100644 --- a/lib/legacy/config.php +++ b/lib/legacy/config.php @@ -38,7 +38,7 @@ * This class is responsible for reading and writing config.php, the very basic * configuration file of ownCloud. */ -OC_Config::$object = new \OC\Config(OC::$SERVERROOT.'/config/', defined('DEBUG') && DEBUG); +OC_Config::$object = new \OC\Config(OC::$SERVERROOT.'/config/'); class OC_Config { public static $object; diff --git a/tests/lib/config.php b/tests/lib/config.php index e22bf3fd7de..8f52cf4ae76 100644 --- a/tests/lib/config.php +++ b/tests/lib/config.php @@ -11,38 +11,35 @@ class Test_Config extends PHPUnit_Framework_TestCase { const CONFIG_DIR = 'static://'; const TESTCONTENT = '"bar");'; + 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(self::CONFIG_DIR, false); + $config = new OC\Config('/non-existing'); $this->assertAttributeEquals(array(), 'cache', $config); - file_put_contents(self::CONFIG_FILE, self::TESTCONTENT); - $config = new OC\Config(self::CONFIG_DIR, false); - $this->assertAttributeEquals(array('foo'=>'bar'), 'cache', $config); + $this->assertAttributeEquals(array('foo'=>'bar'), 'cache', $this->config); } public function testGetKeys() { - file_put_contents(self::CONFIG_FILE, self::TESTCONTENT); - $config = new OC\Config(self::CONFIG_DIR, false); - $this->assertEquals(array('foo'), $config->getKeys()); + $this->assertEquals(array('foo'), $this->config->getKeys()); } public function testGetValue() { - file_put_contents(self::CONFIG_FILE, self::TESTCONTENT); - $config = new OC\Config(self::CONFIG_DIR, false); - $this->assertEquals('bar', $config->getValue('foo')); - $this->assertEquals(null, $config->getValue('bar')); - $this->assertEquals('moo', $config->getValue('bar', 'moo')); + $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() { - file_put_contents(self::CONFIG_FILE, self::TESTCONTENT); - $config = new OC\Config(self::CONFIG_DIR, false); - $config->setValue('foo', 'moo'); - $this->assertAttributeEquals(array('foo'=>'moo'), 'cache', $config); + $this->config->setValue('foo', 'moo'); + $this->assertAttributeEquals(array('foo'=>'moo'), 'cache', $this->config); $content = file_get_contents(self::CONFIG_FILE); $this->assertEquals(<<setValue('bar', 'red'); - $this->assertAttributeEquals(array('foo'=>'moo', 'bar'=>'red'), 'cache', $config); + $this->config->setValue('bar', 'red'); + $this->assertAttributeEquals(array('foo'=>'moo', 'bar'=>'red'), 'cache', $this->config); $content = file_get_contents(self::CONFIG_FILE); $this->assertEquals(<<deleteKey('foo'); - $this->assertAttributeEquals(array(), 'cache', $config); + $this->config->deleteKey('foo'); + $this->assertAttributeEquals(array(), 'cache', $this->config); $content = file_get_contents(self::CONFIG_FILE); $this->assertEquals(<<deleteKey('foo'); // change something so we save to the config file - $this->assertAttributeEquals(array(), 'cache', $config); - $this->assertAttributeEquals(true, 'debug_mode', $config); + $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); $this->assertEquals(<<setValue('foo', 'bar'); } catch (\OC\HintException $e) { -- cgit v1.2.3 From e789e056759aedb93d9eba3a8598acea67c842c6 Mon Sep 17 00:00:00 2001 From: Thomas Müller Date: Tue, 2 Jul 2013 00:15:42 +0200 Subject: on unit test use @expectedException some phpdoc added --- lib/legacy/config.php | 3 +++ tests/lib/config.php | 15 +++++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) (limited to 'tests') diff --git a/lib/legacy/config.php b/lib/legacy/config.php index f68d7c31b25..5294a48ea44 100644 --- a/lib/legacy/config.php +++ b/lib/legacy/config.php @@ -41,6 +41,9 @@ OC_Config::$object = new \OC\Config(OC::$SERVERROOT.'/config/'); class OC_Config { + /** + * @var \OC\Config + */ public static $object; /** diff --git a/tests/lib/config.php b/tests/lib/config.php index 8f52cf4ae76..c17d2ae7eff 100644 --- a/tests/lib/config.php +++ b/tests/lib/config.php @@ -11,6 +11,11 @@ class Test_Config extends PHPUnit_Framework_TestCase { const CONFIG_DIR = 'static://'; const TESTCONTENT = '"bar");'; + /** + * @var \OC\Config + */ + private $config; + function setUp() { file_put_contents(self::CONFIG_FILE, self::TESTCONTENT); $this->config = new OC\Config(self::CONFIG_DIR); @@ -94,14 +99,12 @@ EOL , $content); } + /** + * @expectedException \OC\HintException + */ public function testWriteData() { $config = new OC\Config('/non-writable'); - try { - $config->setValue('foo', 'bar'); - } catch (\OC\HintException $e) { - return; - } - $this->fail(); + $config->setValue('foo', 'bar'); } } -- cgit v1.2.3 From f29dd1c784b736e5d5398f936733409c0db0160d Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Fri, 5 Jul 2013 15:25:53 +0200 Subject: fix test case whitespace --- tests/lib/config.php | 35 ++++++++++++++--------------------- 1 file changed, 14 insertions(+), 21 deletions(-) (limited to 'tests') diff --git a/tests/lib/config.php b/tests/lib/config.php index c17d2ae7eff..12473eb6676 100644 --- a/tests/lib/config.php +++ b/tests/lib/config.php @@ -21,30 +21,26 @@ class Test_Config extends PHPUnit_Framework_TestCase { $this->config = new OC\Config(self::CONFIG_DIR); } - public function testReadData() - { + public function testReadData() { $config = new OC\Config('/non-existing'); $this->assertAttributeEquals(array(), 'cache', $config); - $this->assertAttributeEquals(array('foo'=>'bar'), 'cache', $this->config); + $this->assertAttributeEquals(array('foo' => 'bar'), 'cache', $this->config); } - public function testGetKeys() - { + public function testGetKeys() { $this->assertEquals(array('foo'), $this->config->getKeys()); } - public function testGetValue() - { + 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() - { + public function testSetValue() { $this->config->setValue('foo', 'moo'); - $this->assertAttributeEquals(array('foo'=>'moo'), 'cache', $this->config); + $this->assertAttributeEquals(array('foo' => 'moo'), 'cache', $this->config); $content = file_get_contents(self::CONFIG_FILE); $this->assertEquals(<<config->setValue('bar', 'red'); - $this->assertAttributeEquals(array('foo'=>'moo', 'bar'=>'red'), 'cache', $this->config); + $this->assertAttributeEquals(array('foo' => 'moo', 'bar' => 'red'), 'cache', $this->config); $content = file_get_contents(self::CONFIG_FILE); $this->assertEquals(<<config->deleteKey('foo'); $this->assertAttributeEquals(array(), 'cache', $this->config); $content = file_get_contents(self::CONFIG_FILE); @@ -79,11 +74,10 @@ EOL ); EOL -, $content); + , $content); } - public function testSavingDebugMode() - { + 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); @@ -96,14 +90,13 @@ define('DEBUG',true); ); EOL -, $content); + , $content); } /** * @expectedException \OC\HintException */ - public function testWriteData() - { + public function testWriteData() { $config = new OC\Config('/non-writable'); $config->setValue('foo', 'bar'); } -- cgit v1.2.3 From 492a35737c634fee27b0eb9d3ea6425bc6d98396 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Fri, 5 Jul 2013 15:26:39 +0200 Subject: fix \OC\Config test cases when debug mode is enabled --- tests/lib/config.php | 2 ++ 1 file changed, 2 insertions(+) (limited to 'tests') diff --git a/tests/lib/config.php b/tests/lib/config.php index 12473eb6676..87ee2807c2d 100644 --- a/tests/lib/config.php +++ b/tests/lib/config.php @@ -39,6 +39,7 @@ class Test_Config extends PHPUnit_Framework_TestCase { } 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); @@ -65,6 +66,7 @@ EOL } public function testDeleteKey() { + $this->config->setDebugMode(false); $this->config->deleteKey('foo'); $this->assertAttributeEquals(array(), 'cache', $this->config); $content = file_get_contents(self::CONFIG_FILE); -- cgit v1.2.3