From 80a3f8d066b7deffe70a232ca956746db02db138 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Sun, 17 Mar 2013 16:00:39 +0100 Subject: Seperate memory based cache from OC_Cache --- tests/lib/cache/apc.php | 35 ----------------------------------- tests/lib/cache/xcache.php | 31 ------------------------------- tests/lib/memcache/apc.php | 31 +++++++++++++++++++++++++++++++ tests/lib/memcache/xcache.php | 31 +++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 66 deletions(-) delete mode 100644 tests/lib/cache/apc.php delete mode 100644 tests/lib/cache/xcache.php create mode 100644 tests/lib/memcache/apc.php create mode 100644 tests/lib/memcache/xcache.php (limited to 'tests') diff --git a/tests/lib/cache/apc.php b/tests/lib/cache/apc.php deleted file mode 100644 index bb5eb483dbf..00000000000 --- a/tests/lib/cache/apc.php +++ /dev/null @@ -1,35 +0,0 @@ -. -* -*/ - -class Test_Cache_APC extends Test_Cache { - public function setUp() { - if(!extension_loaded('apc')) { - $this->markTestSkipped('The apc extension is not available.'); - return; - } - if(!ini_get('apc.enable_cli') && OC::$CLI) { - $this->markTestSkipped('apc not available in CLI.'); - return; - } - $this->instance=new OC_Cache_APC(); - } -} diff --git a/tests/lib/cache/xcache.php b/tests/lib/cache/xcache.php deleted file mode 100644 index 43bed2db037..00000000000 --- a/tests/lib/cache/xcache.php +++ /dev/null @@ -1,31 +0,0 @@ -. -* -*/ - -class Test_Cache_XCache extends Test_Cache { - public function setUp() { - if(!function_exists('xcache_get')) { - $this->markTestSkipped('The xcache extension is not available.'); - return; - } - $this->instance=new OC_Cache_XCache(); - } -} diff --git a/tests/lib/memcache/apc.php b/tests/lib/memcache/apc.php new file mode 100644 index 00000000000..e3dccc09669 --- /dev/null +++ b/tests/lib/memcache/apc.php @@ -0,0 +1,31 @@ +. +* +*/ + +class Test_Memcache_APC extends Test_Cache { + public function setUp() { + if(!\OC\Memcache\APC::isAvailable()) { + $this->markTestSkipped('The apc extension is not available.'); + return; + } + $this->instance=new \OC\Memcache\APC(); + } +} diff --git a/tests/lib/memcache/xcache.php b/tests/lib/memcache/xcache.php new file mode 100644 index 00000000000..48773533c89 --- /dev/null +++ b/tests/lib/memcache/xcache.php @@ -0,0 +1,31 @@ +. + * + */ + +class Test_Memcache_XCache extends Test_Cache { + public function setUp() { + if (!\OC\Memcache\XCache::isAvailable()) { + $this->markTestSkipped('The xcache extension is not available.'); + return; + } + $this->instance = new \OC\Memcache\XCache(); + } +} -- cgit v1.2.3 From 5418c98a81caba2da00b2e81fb56fe373737a7c8 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Sun, 17 Mar 2013 16:01:10 +0100 Subject: Add memcached backend --- lib/memcache/cache.php | 6 ++- lib/memcache/memcached.php | 81 ++++++++++++++++++++++++++++++++++++++++ tests/lib/memcache/memcached.php | 17 +++++++++ 3 files changed, 103 insertions(+), 1 deletion(-) create mode 100644 lib/memcache/memcached.php create mode 100644 tests/lib/memcache/memcached.php (limited to 'tests') diff --git a/lib/memcache/cache.php b/lib/memcache/cache.php index d77ea27933f..331c689f065 100644 --- a/lib/memcache/cache.php +++ b/lib/memcache/cache.php @@ -20,6 +20,8 @@ abstract class Cache { return new XCache($global); } elseif (APC::isAvailable()) { return new APC($global); + } elseif (Memcached::isAvailable()) { + return new Memcached($global); } else { return null; } @@ -65,5 +67,7 @@ abstract class Cache { /** * @return bool */ - //static public function isAvailable(); + static public function isAvailable() { + return XCache::isAvailable() || APC::isAvailable() || Memcached::isAvailable(); + } } diff --git a/lib/memcache/memcached.php b/lib/memcache/memcached.php new file mode 100644 index 00000000000..ab35bd8bbac --- /dev/null +++ b/lib/memcache/memcached.php @@ -0,0 +1,81 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Memcache; + +class Memcached extends Cache { + protected $prefix; + + /** + * @var \Memcached $cache + */ + private static $cache = null; + + public function __construct($global = false) { + $this->prefix = \OC_Util::getInstanceId() . '/'; + if (!$global) { + $this->prefix .= \OC_User::getUser() . '/'; + } + if (is_null(self::$cache)) { + self::$cache = new \Memcached(); + list($host, $port) = \OC_Config::getValue('memcached_server', array('localhost', 11211)); + self::$cache->addServer($host, $port); + } + } + + /** + * entries in XCache gets namespaced to prevent collisions between owncloud instances and users + */ + protected function getNameSpace() { + return $this->prefix; + } + + public function get($key) { + $result = self::$cache->get($this->getNamespace() . $key); + if ($result === false and self::$cache->getResultCode() == \Memcached::RES_NOTFOUND) { + return null; + } else { + return $result; + } + } + + public function set($key, $value, $ttl = 0) { + if ($ttl > 0) { + return self::$cache->set($this->getNamespace() . $key, $value, $ttl); + } else { + return self::$cache->set($this->getNamespace() . $key, $value); + } + } + + public function hasKey($key) { + self::$cache->get($this->getNamespace() . $key); + return self::$cache->getResultCode() !== \Memcached::RES_NOTFOUND; + } + + public function remove($key) { + return self::$cache->delete($this->getNamespace() . $key); + } + + public function clear($prefix = '') { + $prefix = $this->getNamespace() . $prefix; + $allKeys = self::$cache->getAllKeys(); + $keys = array(); + $prefixLength = strlen($prefix); + foreach ($allKeys as $key) { + if (substr($key, 0, $prefixLength) === $prefix) { + $keys[] = $key; + } + } + self::$cache->deleteMulti($keys); + return true; + } + + static public function isAvailable() { + return extension_loaded('memcached'); + } +} diff --git a/tests/lib/memcache/memcached.php b/tests/lib/memcache/memcached.php new file mode 100644 index 00000000000..a0be047ed1f --- /dev/null +++ b/tests/lib/memcache/memcached.php @@ -0,0 +1,17 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +class Test_Memcache_Memcached extends Test_Cache { + public function setUp() { + if (!\OC\Memcache\Memcached::isAvailable()) { + $this->markTestSkipped('The memcached extension is not available.'); + return; + } + $this->instance = new \OC\Memcache\Memcached(); + } +} -- cgit v1.2.3 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 --- lib/config.php | 111 ++++++++++++++++++++++--------------------------- lib/hintexception.php | 27 ++++++++++++ lib/legacy/config.php | 98 +++++++++++++++++++++++++++++++++++++++++++ lib/setup.php | 16 +------ tests/lib/config.php | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 288 insertions(+), 77 deletions(-) create mode 100644 lib/hintexception.php create mode 100644 lib/legacy/config.php create mode 100644 tests/lib/config.php (limited to 'tests') diff --git a/lib/config.php b/lib/config.php index 9b87d4ce4e5..dcc659395a7 100644 --- a/lib/config.php +++ b/lib/config.php @@ -34,17 +34,27 @@ * */ +namespace OC; + /** * This class is responsible for reading and writing config.php, the very basic * configuration file of owncloud. */ -class OC_Config{ +class Config { // associative array key => value - private static $cache = array(); + protected $cache = array(); + + protected $config_dir; + protected $config_filename; - // Is the cache filled? - private static $init = false; + protected $debug_mode; + public function __construct($config_dir, $debug_mode) { + $this->config_dir = $config_dir; + $this->debug_mode = $debug_mode; + $this->config_filename = $this->config_dir.'config.php'; + $this->readData(); + } /** * @brief Lists all available config keys * @return array with key names @@ -52,10 +62,8 @@ class OC_Config{ * This function returns all keys saved in config.php. Please note that it * does not return the values. */ - public static function getKeys() { - self::readData(); - - return array_keys( self::$cache ); + public function getKeys() { + return array_keys( $this->cache ); } /** @@ -67,11 +75,9 @@ class OC_Config{ * This function gets the value from config.php. If it does not exist, * $default will be returned. */ - public static function getValue( $key, $default = null ) { - self::readData(); - - if( array_key_exists( $key, self::$cache )) { - return self::$cache[$key]; + public function getValue( $key, $default = null ) { + if( array_key_exists( $key, $this->cache )) { + return $this->cache[$key]; } return $default; @@ -81,57 +87,43 @@ class OC_Config{ * @brief Sets a value * @param string $key key * @param string $value value - * @return bool * * This function sets the value and writes the config.php. If the file can * not be written, false will be returned. */ - public static function setValue( $key, $value ) { - self::readData(); - + public function setValue( $key, $value ) { // Add change - self::$cache[$key] = $value; + $this->cache[$key] = $value; // Write changes - self::writeData(); - return true; + $this->writeData(); } /** * @brief Removes a key from the config * @param string $key key - * @return bool * * This function removes a key from the config.php. If owncloud has no * write access to config.php, the function will return false. */ - public static function deleteKey( $key ) { - self::readData(); - - if( array_key_exists( $key, self::$cache )) { + public function deleteKey( $key ) { + if( array_key_exists( $key, $this->cache )) { // Delete key from cache - unset( self::$cache[$key] ); + unset( $this->cache[$key] ); // Write changes - self::writeData(); + $this->writeData(); } - - return true; } /** * @brief Loads the config file - * @return bool * * Reads the config file and saves it to the cache */ - private static function readData() { - if( self::$init ) { - return true; - } - + private function readData() { // read all file in config dir ending by config.php - $config_files = glob( OC::$SERVERROOT."/config/*.config.php"); + $config_files = glob( $this->config_dir.'*.config.php'); //Filter only regular files $config_files = array_filter($config_files, 'is_file'); @@ -140,54 +132,49 @@ class OC_Config{ natsort($config_files); // Add default config - array_unshift($config_files,OC::$SERVERROOT."/config/config.php"); + array_unshift($config_files, $this->config_filename); //Include file and merge config - foreach($config_files as $file){ + foreach($config_files as $file) { + if( !file_exists( $file) ) { + continue; + } + unset($CONFIG); include $file; if( isset( $CONFIG ) && is_array( $CONFIG )) { - self::$cache = array_merge(self::$cache, $CONFIG); + $this->cache = array_merge($this->cache, $CONFIG); } } - - // We cached everything - self::$init = true; - - return true; } /** * @brief Writes the config file - * @return bool * * Saves the config to the config file. * */ - public static function writeData() { + private function writeData() { // Create a php file ... - $content = "debug_mode) { $content .= "define('DEBUG',true);\n"; } - $content .= "\$CONFIG = "; - $content .= var_export(self::$cache, true); + $content .= '$CONFIG = '; + $content .= var_export($this->cache, true); $content .= ";\n"; + //var_dump($content, $this); - $filename = OC::$SERVERROOT."/config/config.php"; // Write the file - $result=@file_put_contents( $filename, $content ); + $result=@file_put_contents( $this->config_filename, $content ); if(!$result) { - $tmpl = new OC_Template( '', 'error', 'guest' ); - $tmpl->assign('errors', array(1=>array( - 'error'=>"Can't write into config directory 'config'", - 'hint'=>'You can usually fix this by giving the webserver user write access' - .' to the config directory in owncloud'))); - $tmpl->printPage(); - exit; + throw new HintException( + "Can't write into config directory '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 - @chmod($filename, 0640); - - return true; + @chmod($this->config_filename, 0640); } } + +require_once __DIR__.'/legacy/'.basename(__FILE__); diff --git a/lib/hintexception.php b/lib/hintexception.php new file mode 100644 index 00000000000..8c64258435b --- /dev/null +++ b/lib/hintexception.php @@ -0,0 +1,27 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC; + +class HintException extends \Exception +{ + private $hint; + + public function __construct($message, $hint, $code = 0, Exception $previous = null) { + $this->hint = $hint; + parent::__construct($message, $code, $previous); + } + + public function __toString() { + return __CLASS__ . ": [{$this->code}]: {$this->message} ({$this->hint})\n"; + } + + public function getHint() { + return $this->hint; + } +} diff --git a/lib/legacy/config.php b/lib/legacy/config.php new file mode 100644 index 00000000000..d030bbe3676 --- /dev/null +++ b/lib/legacy/config.php @@ -0,0 +1,98 @@ +. + * + */ +/* + * + * An example of config.php + * + * "mysql", + * "firstrun" => false, + * "pi" => 3.14 + * ); + * ?> + * + */ + +/** + * 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); +class OC_Config{ + public static $object; + /** + * @brief Lists all available config keys + * @return array with key names + * + * This function returns all keys saved in config.php. Please note that it + * does not return the values. + */ + public static function getKeys() { + return self::$object->getKeys(); + } + + /** + * @brief Gets a value from config.php + * @param string $key key + * @param string $default = null default value + * @return string the value or $default + * + * This function gets the value from config.php. If it does not exist, + * $default will be returned. + */ + public static function getValue( $key, $default = null ) { + return self::$object->getValue( $key, $default ); + } + + /** + * @brief Sets a value + * @param string $key key + * @param string $value value + * + * This function sets the value and writes the config.php. If the file can + * not be written, false will be returned. + */ + public static function setValue( $key, $value ) { + try { + self::$object->setValue( $key, $value ); + } catch (\OC\HintException $e) { + \OC_Template::printErrorPage( $e->getMessage(), $e->getHint() ); + } + } + + /** + * @brief Removes a key from the config + * @param string $key key + * + * This function removes a key from the config.php. If owncloud has no + * write access to config.php, the function will return false. + */ + public static function deleteKey( $key ) { + try { + self::$object->deleteKey( $key ); + } catch (\OC\HintException $e) { + \OC_Template::printErrorPage( $e->getMessage(), $e->getHint() ); + } + } +} diff --git a/lib/setup.php b/lib/setup.php index d1197b3ebf3..e05db554320 100644 --- a/lib/setup.php +++ b/lib/setup.php @@ -1,21 +1,7 @@ hint = $hint; - parent::__construct($message, $code, $previous); - } - - public function __toString() { - return __CLASS__ . ": [{$this->code}]: {$this->message} ({$this->hint})\n"; - } - - public function getHint() { - return $this->hint; - } } class OC_Setup { 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 6887d7daf5f0dfcd2f1da0d9c8f796a4fcc29963 Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Mon, 24 Jun 2013 22:38:05 +0200 Subject: Skip Test_Archive_TAR in php 5.5 for now --- tests/lib/archive/tar.php | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'tests') diff --git a/tests/lib/archive/tar.php b/tests/lib/archive/tar.php index e66a8740879..d831487b16f 100644 --- a/tests/lib/archive/tar.php +++ b/tests/lib/archive/tar.php @@ -10,6 +10,12 @@ require_once 'archive.php'; if (!OC_Util::runningOnWindows()) { class Test_Archive_TAR extends Test_Archive { + public function setUp() { + if (floatval(phpversion())>=5.5) { + $this->markTestSkipped('php 5.5 changed unpack function.'); + return; + } + } protected function getExisting() { $dir = OC::$SERVERROOT . '/tests/data'; return new OC_Archive_TAR($dir . '/data.tar.gz'); -- cgit v1.2.3 From 23da0c7d188b4c0a119e16c5be48ce322df29068 Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Mon, 24 Jun 2013 17:46:51 +0200 Subject: Fix tableExists test function for Oracle --- tests/lib/dbschema.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'tests') diff --git a/tests/lib/dbschema.php b/tests/lib/dbschema.php index 59f203993ef..813112f1fe5 100644 --- a/tests/lib/dbschema.php +++ b/tests/lib/dbschema.php @@ -102,9 +102,10 @@ class Test_DBSchema extends PHPUnit_Framework_TestCase { $exists = $result && $result->fetchOne(); break; case 'oci': - $sql = 'SELECT table_name FROM user_tables WHERE table_name = ?'; - $result = \OC_DB::executeAudited($sql, array($table)); - $exists = (bool)$result->fetchOne(); //oracle uses MDB2 and returns null + $sql = "SELECT table_name FROM user_tables WHERE table_name = '{$table}'"; + $query = OC_DB::prepare($sql); + $result = $query->execute(array()); + $exists = $result && $result->fetchOne(); break; case 'mssql': $sql = "SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '{$table}'"; -- cgit v1.2.3 From eb9078407437f275687e31c9bd7486f469ffa978 Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Wed, 12 Jun 2013 18:51:59 +0200 Subject: Fix table change tests for OCI --- tests/data/db_structure2.xml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/data/db_structure2.xml b/tests/data/db_structure2.xml index fc6fe0bba7d..6f12f81f477 100644 --- a/tests/data/db_structure2.xml +++ b/tests/data/db_structure2.xml @@ -49,8 +49,9 @@ description - clob + text false + 1024 -- cgit v1.2.3 From b980987e32270fc416eefc87f7e163763cab2776 Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Fri, 21 Jun 2013 12:04:52 +0200 Subject: Doctrine only returns false --- tests/lib/db.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/lib/db.php b/tests/lib/db.php index afbdb413c3d..60331a23295 100644 --- a/tests/lib/db.php +++ b/tests/lib/db.php @@ -37,7 +37,7 @@ class Test_DB extends PHPUnit_Framework_TestCase { $result = $query->execute(array('uri_1')); $this->assertTrue((bool)$result); $row = $result->fetchRow(); - $this->assertFalse((bool)$row); //PDO returns false, MDB2 returns null + $this->assertFalse($row); $query = OC_DB::prepare('INSERT INTO `*PREFIX*'.$this->table2.'` (`fullname`,`uri`) VALUES (?,?)'); $result = $query->execute(array('fullname test', 'uri_1')); $this->assertTrue((bool)$result); -- cgit v1.2.3 From 769212a9a025a58a5b2189eb2461a01e0ece6d36 Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Mon, 24 Jun 2013 18:15:02 +0200 Subject: numRows doesn't work with Oracle --- tests/lib/db.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/lib/db.php b/tests/lib/db.php index 60331a23295..18b7340ec9b 100644 --- a/tests/lib/db.php +++ b/tests/lib/db.php @@ -94,7 +94,7 @@ class Test_DB extends PHPUnit_Framework_TestCase { $query = OC_DB::prepare('SELECT * FROM `*PREFIX*'.$this->table3.'`'); $result = $query->execute(); $this->assertTrue((bool)$result); - $this->assertEquals('4', $result->numRows()); + $this->assertEquals('4', count($result->fetchAll())); } public function testinsertIfNotExistDontOverwrite() { -- cgit v1.2.3 From 0c680b46cdfe5106d87ad807657c9d2e558b4a73 Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Wed, 26 Jun 2013 20:48:54 +0200 Subject: View test needs a dummy user --- tests/lib/files/view.php | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'tests') diff --git a/tests/lib/files/view.php b/tests/lib/files/view.php index 830913a91ad..3bac9e770aa 100644 --- a/tests/lib/files/view.php +++ b/tests/lib/files/view.php @@ -20,10 +20,19 @@ class View extends \PHPUnit_Framework_TestCase { private $storages = array(); public function setUp() { + \OC_User::clearBackends(); + \OC_User::useBackend(new \OC_User_Dummy()); + + //login + \OC_User::createUser('test', 'test'); + $this->user=\OC_User::getUser(); + \OC_User::setUserId('test'); + \OC\Files\Filesystem::clearMounts(); } public function tearDown() { + \OC_User::setUserId($this->user); foreach ($this->storages as $storage) { $cache = $storage->getCache(); $ids = $cache->getAll(); -- 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 21601fd78448ede37bc6f55c9880fc27a4df9ae3 Mon Sep 17 00:00:00 2001 From: Christian Berendt Date: Wed, 3 Jul 2013 18:08:19 +0200 Subject: increasing difference for modifcation time tests one second is sometimes not enough when using a slow storage connection, three seconds is working better (at least when testing against S3) --- tests/lib/files/storage/storage.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'tests') diff --git a/tests/lib/files/storage/storage.php b/tests/lib/files/storage/storage.php index fb3e05e66b3..155a99d8bab 100644 --- a/tests/lib/files/storage/storage.php +++ b/tests/lib/files/storage/storage.php @@ -168,10 +168,10 @@ abstract class Storage extends \PHPUnit_Framework_TestCase { $this->assertTrue($this->instance->isReadable('/lorem.txt')); $ctimeEnd = time(); $mTime = $this->instance->filemtime('/lorem.txt'); - $this->assertTrue($this->instance->hasUpdated('/lorem.txt', $ctimeStart - 1)); - $this->assertTrue($this->instance->hasUpdated('/', $ctimeStart - 1)); + $this->assertTrue($this->instance->hasUpdated('/lorem.txt', $ctimeStart - 3)); + $this->assertTrue($this->instance->hasUpdated('/', $ctimeStart - 3)); - $this->assertTrue(($ctimeStart - 1) <= $mTime); + $this->assertTrue(($ctimeStart - 3) <= $mTime); $this->assertTrue($mTime <= ($ctimeEnd + 1)); $this->assertEquals(filesize($textFile), $this->instance->filesize('/lorem.txt')); @@ -185,10 +185,10 @@ abstract class Storage extends \PHPUnit_Framework_TestCase { $mtimeEnd = time(); if ($supportsTouch !== false) { $mTime = $this->instance->filemtime('/lorem.txt'); - $this->assertTrue(($mtimeStart - 1) <= $mTime); - $this->assertTrue($mTime <= ($mtimeEnd + 1)); + $this->assertTrue(($mtimeStart - 3) <= $mTime); + $this->assertTrue($mTime <= ($mtimeEnd + 3)); - $this->assertTrue($this->instance->hasUpdated('/lorem.txt', $mtimeStart - 1)); + $this->assertTrue($this->instance->hasUpdated('/lorem.txt', $mtimeStart - 3)); if ($this->instance->touch('/lorem.txt', 100) !== false) { $mTime = $this->instance->filemtime('/lorem.txt'); @@ -203,11 +203,11 @@ abstract class Storage extends \PHPUnit_Framework_TestCase { clearstatcache(); $mtimeEnd = time(); $mTime = $this->instance->filemtime('/lorem.txt'); - $this->assertTrue(($mtimeStart - 1) <= $mTime); - $this->assertTrue($mTime <= ($mtimeEnd + 1)); + $this->assertTrue(($mtimeStart - 3) <= $mTime); + $this->assertTrue($mTime <= ($mtimeEnd + 3)); $this->instance->unlink('/lorem.txt'); - $this->assertTrue($this->instance->hasUpdated('/', $mtimeStart - 1)); + $this->assertTrue($this->instance->hasUpdated('/', $mtimeStart - 3)); } public function testSearch() { -- cgit v1.2.3 From 81acfc9498d1451d7266dbad024cd3d29d7d0b1a Mon Sep 17 00:00:00 2001 From: Christian Berendt Date: Wed, 3 Jul 2013 18:11:54 +0200 Subject: test copying and moving files in subdirectories --- tests/lib/files/storage/storage.php | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'tests') diff --git a/tests/lib/files/storage/storage.php b/tests/lib/files/storage/storage.php index 155a99d8bab..4a3a0c40e08 100644 --- a/tests/lib/files/storage/storage.php +++ b/tests/lib/files/storage/storage.php @@ -129,6 +129,16 @@ abstract class Storage extends \PHPUnit_Framework_TestCase { $this->assertTrue($this->instance->file_exists('/target2.txt')); $this->assertFalse($this->instance->file_exists('/source.txt')); $this->assertEquals(file_get_contents($textFile), $this->instance->file_get_contents('/target.txt')); + + // check if it's working inside directories as well + $this->instance->mkdir('/folder'); + $this->instance->file_put_contents('/folder/lorem.txt', $sourceText); + $this->assertFalse($this->instance->is_dir('/folder/lorem.txt')); + $this->assertEquals($sourceText, $this->instance->file_get_contents('/folder/lorem.txt'), 'data returned from file_get_contents is not equal to the source data'); + $this->instance->file_put_contents('/folder/lorem.txt', ''); + $this->assertEquals('', $this->instance->file_get_contents('/folder/lorem.txt'), 'file not emptied'); + $this->instance->rmdir('/folder'); + $this->assertFalse($this->instance->file_exists('/folder/lorem.txt')); } public function testLocal() { -- cgit v1.2.3 From e556b7ab5504a039553f92696be6124db51a2a39 Mon Sep 17 00:00:00 2001 From: Christian Berendt Date: Wed, 3 Jul 2013 18:16:50 +0200 Subject: test working with subdirectories --- tests/lib/files/storage/storage.php | 48 +++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) (limited to 'tests') diff --git a/tests/lib/files/storage/storage.php b/tests/lib/files/storage/storage.php index 4a3a0c40e08..e5cc9483871 100644 --- a/tests/lib/files/storage/storage.php +++ b/tests/lib/files/storage/storage.php @@ -79,6 +79,54 @@ abstract class Storage extends \PHPUnit_Framework_TestCase { } } $this->assertEquals(array(), $content); + + $this->assertTrue($this->instance->mkdir('/folder')); + $this->assertTrue($this->instance->mkdir('/folder/sub_a')); + $this->assertTrue($this->instance->mkdir('/folder/sub_b')); + $this->assertTrue($this->instance->mkdir('/folder/sub_b/sub_bb')); + $this->assertTrue($this->instance->touch('/folder/sub_b/sub_bb/file.txt')); + $this->assertTrue($this->instance->touch('/folder/sub_a/file.txt')); + $this->assertTrue($this->instance->is_dir('/folder/sub_b')); + $this->assertTrue($this->instance->is_dir('/folder/sub_b/sub_bb')); + $this->assertTrue($this->instance->file_exists('/folder/sub_a/file.txt')); + $this->assertTrue($this->instance->file_exists('/folder/sub_b/sub_bb/file.txt')); + + $dh = $this->instance->opendir('/folder'); + $content = array(); + while ($file = readdir($dh)) { + if ($file != '.' and $file != '..') { + $content[] = $file; + } + } + $this->assertEquals(array('sub_a', 'sub_b'), $content); + + $dh = $this->instance->opendir('/folder/sub_b/sub_bb'); + $content = array(); + while ($file = readdir($dh)) { + if ($file != '.' and $file != '..') { + $content[] = $file; + } + } + $this->assertEquals(array('file.txt'), $content); + + $this->assertTrue($this->instance->rmdir('/folder/sub_b')); + $this->assertFalse($this->instance->is_dir('/folder/sub_b')); + $this->assertFalse($this->instance->is_dir('/folder/sub_b/sub_bb')); + $this->assertFalse($this->instance->file_exists('/folder/sub_b/sub_bb/file.txt')); + + $dh = $this->instance->opendir('/folder'); + $content = array(); + while ($file = readdir($dh)) { + if ($file != '.' and $file != '..') { + $content[] = $file; + } + } + $this->assertEquals(array('sub_a'), $content); + + $this->assertTrue($this->instance->rmdir('/folder')); + $this->assertFalse($this->instance->is_dir('/folder')); + $this->assertFalse($this->instance->is_dir('/folder/sub_a')); + $this->assertFalse($this->instance->file_exists('/folder/sub_a/file.txt')); } /** -- cgit v1.2.3 From 0a5e18335ec4fef9fc0ea7b6778628b7471962bd Mon Sep 17 00:00:00 2001 From: Christian Berendt Date: Wed, 3 Jul 2013 18:23:09 +0200 Subject: test working with files in subdirectories --- tests/lib/files/storage/storage.php | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'tests') diff --git a/tests/lib/files/storage/storage.php b/tests/lib/files/storage/storage.php index e5cc9483871..8f6a8771ba8 100644 --- a/tests/lib/files/storage/storage.php +++ b/tests/lib/files/storage/storage.php @@ -127,6 +127,16 @@ abstract class Storage extends \PHPUnit_Framework_TestCase { $this->assertFalse($this->instance->is_dir('/folder')); $this->assertFalse($this->instance->is_dir('/folder/sub_a')); $this->assertFalse($this->instance->file_exists('/folder/sub_a/file.txt')); + + // check if it's working inside directories as well + $this->instance->mkdir('/folder'); + $this->instance->file_put_contents('/folder/lorem.txt', $sourceText); + $this->assertFalse($this->instance->is_dir('/folder/lorem.txt')); + $this->assertEquals($sourceText, $this->instance->file_get_contents('/folder/lorem.txt'), 'data returned from file_get_contents is not equal to the source data'); + $this->instance->file_put_contents('/folder/lorem.txt', ''); + $this->assertEquals('', $this->instance->file_get_contents('/folder/lorem.txt'), 'file not emptied'); + $this->instance->rmdir('/folder'); + $this->assertFalse($this->instance->file_exists('/folder/lorem.txt')); } /** -- cgit v1.2.3 From 407753f59402c7377db1d1badbf198616a1ac563 Mon Sep 17 00:00:00 2001 From: Christian Berendt Date: Wed, 3 Jul 2013 18:34:33 +0200 Subject: move new tests into the correct test method --- tests/lib/files/storage/storage.php | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) (limited to 'tests') diff --git a/tests/lib/files/storage/storage.php b/tests/lib/files/storage/storage.php index 8f6a8771ba8..771fad8c61b 100644 --- a/tests/lib/files/storage/storage.php +++ b/tests/lib/files/storage/storage.php @@ -154,6 +154,16 @@ abstract class Storage extends \PHPUnit_Framework_TestCase { //empty the file $this->instance->file_put_contents('/lorem.txt', ''); $this->assertEquals('', $this->instance->file_get_contents('/lorem.txt'), 'file not emptied'); + + // check if it's working inside directories as well + $this->instance->mkdir('/folder'); + $this->instance->file_put_contents('/folder/lorem.txt', $sourceText); + $this->assertFalse($this->instance->is_dir('/folder/lorem.txt')); + $this->assertEquals($sourceText, $this->instance->file_get_contents('/folder/lorem.txt'), 'data returned from file_get_contents is not equal to the source data'); + $this->instance->file_put_contents('/folder/lorem.txt', ''); + $this->assertEquals('', $this->instance->file_get_contents('/folder/lorem.txt'), 'file not emptied'); + $this->instance->rmdir('/folder'); + $this->assertFalse($this->instance->file_exists('/folder/lorem.txt')); } /** @@ -187,16 +197,6 @@ abstract class Storage extends \PHPUnit_Framework_TestCase { $this->assertTrue($this->instance->file_exists('/target2.txt')); $this->assertFalse($this->instance->file_exists('/source.txt')); $this->assertEquals(file_get_contents($textFile), $this->instance->file_get_contents('/target.txt')); - - // check if it's working inside directories as well - $this->instance->mkdir('/folder'); - $this->instance->file_put_contents('/folder/lorem.txt', $sourceText); - $this->assertFalse($this->instance->is_dir('/folder/lorem.txt')); - $this->assertEquals($sourceText, $this->instance->file_get_contents('/folder/lorem.txt'), 'data returned from file_get_contents is not equal to the source data'); - $this->instance->file_put_contents('/folder/lorem.txt', ''); - $this->assertEquals('', $this->instance->file_get_contents('/folder/lorem.txt'), 'file not emptied'); - $this->instance->rmdir('/folder'); - $this->assertFalse($this->instance->file_exists('/folder/lorem.txt')); } public function testLocal() { -- cgit v1.2.3 From 818e2a364a6ff3eae0e463fb117e6f17166b3dd1 Mon Sep 17 00:00:00 2001 From: Christian Berendt Date: Wed, 3 Jul 2013 18:38:34 +0200 Subject: test moving and copying of subdirectories --- tests/lib/files/storage/storage.php | 41 +++++++++++++++++++++++++++++++++++++ 1 file changed, 41 insertions(+) (limited to 'tests') diff --git a/tests/lib/files/storage/storage.php b/tests/lib/files/storage/storage.php index 771fad8c61b..b694a76ddfe 100644 --- a/tests/lib/files/storage/storage.php +++ b/tests/lib/files/storage/storage.php @@ -197,6 +197,47 @@ abstract class Storage extends \PHPUnit_Framework_TestCase { $this->assertTrue($this->instance->file_exists('/target2.txt')); $this->assertFalse($this->instance->file_exists('/source.txt')); $this->assertEquals(file_get_contents($textFile), $this->instance->file_get_contents('/target.txt')); + + $this->assertTrue($this->instance->mkdir('/folder')); + $this->assertTrue($this->instance->mkdir('/folder/sub_a')); + $this->assertTrue($this->instance->mkdir('/folder/sub_b')); + $this->assertTrue($this->instance->mkdir('/folder/sub_b/sub_bb')); + + $this->assertTrue($this->instance->rename('/folder/sub_b', '/folder/sub_c')); + $this->assertTrue($this->instance->is_dir('/folder/sub_c')); + $this->assertTrue($this->instance->is_dir('/folder/sub_c/sub_bb')); + $this->assertFalse($this->instance->is_dir('/folder/sub_b')); + $this->assertFalse($this->instance->is_dir('/folder/sub_b/sub_bb')); + + $this->assertTrue($this->instance->rename('/folder', '/folder_b')); + $this->assertTrue($this->instance->is_dir('/folder_b')); + $this->assertTrue($this->instance->is_dir('/folder_b/sub_c')); + $this->assertTrue($this->instance->is_dir('/folder_b/sub_c/sub_bb')); + $this->assertFalse($this->instance->is_dir('/folder')); + $this->assertFalse($this->instance->is_dir('/folder/sub_c')); + $this->assertFalse($this->instance->is_dir('/folder/sub_c/sub_bb')); + + $this->assertTrue($this->instance->copy('/folder_b', '/folder')); + $this->assertTrue($this->instance->is_dir('/folder_b')); + $this->assertTrue($this->instance->is_dir('/folder_b/sub_c')); + $this->assertTrue($this->instance->is_dir('/folder_b/sub_c/sub_bb')); + $this->assertTrue($this->instance->is_dir('/folder')); + $this->assertTrue($this->instance->is_dir('/folder/sub_c')); + $this->assertTrue($this->instance->is_dir('/folder/sub_c/sub_bb')); + + $this->assertTrue($this->instance->copy('/folder/sub_c', '/folder/sub_b')); + $this->assertTrue($this->instance->is_dir('/folder/sub_b')); + $this->assertTrue($this->instance->is_dir('/folder/sub_b/sub_bb')); + $this->assertTrue($this->instance->is_dir('/folder/sub_c')); + $this->assertTrue($this->instance->is_dir('/folder/sub_c/sub_bb')); + + $this->assertTrue($this->instance->rmdir('/folder')); + $this->assertFalse($this->instance->is_dir('/folder')); + $this->assertFalse($this->instance->is_dir('/folder/sub_a')); + $this->assertFalse($this->instance->is_dir('/folder/sub_b')); + $this->assertFalse($this->instance->is_dir('/folder/sub_c')); + $this->assertFalse($this->instance->is_dir('/folder/sub_b/sub_bb')); + $this->assertFalse($this->instance->is_dir('/folder/sub_c/sub_bb')); } public function testLocal() { -- cgit v1.2.3 From 37254744b5b1d0f5761837dee1e33ad63dcff101 Mon Sep 17 00:00:00 2001 From: Christian Berendt Date: Wed, 3 Jul 2013 18:41:14 +0200 Subject: remove tests from the wrong test method --- tests/lib/files/storage/storage.php | 10 ---------- 1 file changed, 10 deletions(-) (limited to 'tests') diff --git a/tests/lib/files/storage/storage.php b/tests/lib/files/storage/storage.php index b694a76ddfe..8db3aed1fa5 100644 --- a/tests/lib/files/storage/storage.php +++ b/tests/lib/files/storage/storage.php @@ -127,16 +127,6 @@ abstract class Storage extends \PHPUnit_Framework_TestCase { $this->assertFalse($this->instance->is_dir('/folder')); $this->assertFalse($this->instance->is_dir('/folder/sub_a')); $this->assertFalse($this->instance->file_exists('/folder/sub_a/file.txt')); - - // check if it's working inside directories as well - $this->instance->mkdir('/folder'); - $this->instance->file_put_contents('/folder/lorem.txt', $sourceText); - $this->assertFalse($this->instance->is_dir('/folder/lorem.txt')); - $this->assertEquals($sourceText, $this->instance->file_get_contents('/folder/lorem.txt'), 'data returned from file_get_contents is not equal to the source data'); - $this->instance->file_put_contents('/folder/lorem.txt', ''); - $this->assertEquals('', $this->instance->file_get_contents('/folder/lorem.txt'), 'file not emptied'); - $this->instance->rmdir('/folder'); - $this->assertFalse($this->instance->file_exists('/folder/lorem.txt')); } /** -- cgit v1.2.3 From 92e73928529f00aa1638777995fdd5c91ee05275 Mon Sep 17 00:00:00 2001 From: Christian Berendt Date: Thu, 4 Jul 2013 09:01:36 +0200 Subject: revoking additional tests --- tests/lib/files/storage/storage.php | 99 ------------------------------------- 1 file changed, 99 deletions(-) (limited to 'tests') diff --git a/tests/lib/files/storage/storage.php b/tests/lib/files/storage/storage.php index 8db3aed1fa5..155a99d8bab 100644 --- a/tests/lib/files/storage/storage.php +++ b/tests/lib/files/storage/storage.php @@ -79,54 +79,6 @@ abstract class Storage extends \PHPUnit_Framework_TestCase { } } $this->assertEquals(array(), $content); - - $this->assertTrue($this->instance->mkdir('/folder')); - $this->assertTrue($this->instance->mkdir('/folder/sub_a')); - $this->assertTrue($this->instance->mkdir('/folder/sub_b')); - $this->assertTrue($this->instance->mkdir('/folder/sub_b/sub_bb')); - $this->assertTrue($this->instance->touch('/folder/sub_b/sub_bb/file.txt')); - $this->assertTrue($this->instance->touch('/folder/sub_a/file.txt')); - $this->assertTrue($this->instance->is_dir('/folder/sub_b')); - $this->assertTrue($this->instance->is_dir('/folder/sub_b/sub_bb')); - $this->assertTrue($this->instance->file_exists('/folder/sub_a/file.txt')); - $this->assertTrue($this->instance->file_exists('/folder/sub_b/sub_bb/file.txt')); - - $dh = $this->instance->opendir('/folder'); - $content = array(); - while ($file = readdir($dh)) { - if ($file != '.' and $file != '..') { - $content[] = $file; - } - } - $this->assertEquals(array('sub_a', 'sub_b'), $content); - - $dh = $this->instance->opendir('/folder/sub_b/sub_bb'); - $content = array(); - while ($file = readdir($dh)) { - if ($file != '.' and $file != '..') { - $content[] = $file; - } - } - $this->assertEquals(array('file.txt'), $content); - - $this->assertTrue($this->instance->rmdir('/folder/sub_b')); - $this->assertFalse($this->instance->is_dir('/folder/sub_b')); - $this->assertFalse($this->instance->is_dir('/folder/sub_b/sub_bb')); - $this->assertFalse($this->instance->file_exists('/folder/sub_b/sub_bb/file.txt')); - - $dh = $this->instance->opendir('/folder'); - $content = array(); - while ($file = readdir($dh)) { - if ($file != '.' and $file != '..') { - $content[] = $file; - } - } - $this->assertEquals(array('sub_a'), $content); - - $this->assertTrue($this->instance->rmdir('/folder')); - $this->assertFalse($this->instance->is_dir('/folder')); - $this->assertFalse($this->instance->is_dir('/folder/sub_a')); - $this->assertFalse($this->instance->file_exists('/folder/sub_a/file.txt')); } /** @@ -144,16 +96,6 @@ abstract class Storage extends \PHPUnit_Framework_TestCase { //empty the file $this->instance->file_put_contents('/lorem.txt', ''); $this->assertEquals('', $this->instance->file_get_contents('/lorem.txt'), 'file not emptied'); - - // check if it's working inside directories as well - $this->instance->mkdir('/folder'); - $this->instance->file_put_contents('/folder/lorem.txt', $sourceText); - $this->assertFalse($this->instance->is_dir('/folder/lorem.txt')); - $this->assertEquals($sourceText, $this->instance->file_get_contents('/folder/lorem.txt'), 'data returned from file_get_contents is not equal to the source data'); - $this->instance->file_put_contents('/folder/lorem.txt', ''); - $this->assertEquals('', $this->instance->file_get_contents('/folder/lorem.txt'), 'file not emptied'); - $this->instance->rmdir('/folder'); - $this->assertFalse($this->instance->file_exists('/folder/lorem.txt')); } /** @@ -187,47 +129,6 @@ abstract class Storage extends \PHPUnit_Framework_TestCase { $this->assertTrue($this->instance->file_exists('/target2.txt')); $this->assertFalse($this->instance->file_exists('/source.txt')); $this->assertEquals(file_get_contents($textFile), $this->instance->file_get_contents('/target.txt')); - - $this->assertTrue($this->instance->mkdir('/folder')); - $this->assertTrue($this->instance->mkdir('/folder/sub_a')); - $this->assertTrue($this->instance->mkdir('/folder/sub_b')); - $this->assertTrue($this->instance->mkdir('/folder/sub_b/sub_bb')); - - $this->assertTrue($this->instance->rename('/folder/sub_b', '/folder/sub_c')); - $this->assertTrue($this->instance->is_dir('/folder/sub_c')); - $this->assertTrue($this->instance->is_dir('/folder/sub_c/sub_bb')); - $this->assertFalse($this->instance->is_dir('/folder/sub_b')); - $this->assertFalse($this->instance->is_dir('/folder/sub_b/sub_bb')); - - $this->assertTrue($this->instance->rename('/folder', '/folder_b')); - $this->assertTrue($this->instance->is_dir('/folder_b')); - $this->assertTrue($this->instance->is_dir('/folder_b/sub_c')); - $this->assertTrue($this->instance->is_dir('/folder_b/sub_c/sub_bb')); - $this->assertFalse($this->instance->is_dir('/folder')); - $this->assertFalse($this->instance->is_dir('/folder/sub_c')); - $this->assertFalse($this->instance->is_dir('/folder/sub_c/sub_bb')); - - $this->assertTrue($this->instance->copy('/folder_b', '/folder')); - $this->assertTrue($this->instance->is_dir('/folder_b')); - $this->assertTrue($this->instance->is_dir('/folder_b/sub_c')); - $this->assertTrue($this->instance->is_dir('/folder_b/sub_c/sub_bb')); - $this->assertTrue($this->instance->is_dir('/folder')); - $this->assertTrue($this->instance->is_dir('/folder/sub_c')); - $this->assertTrue($this->instance->is_dir('/folder/sub_c/sub_bb')); - - $this->assertTrue($this->instance->copy('/folder/sub_c', '/folder/sub_b')); - $this->assertTrue($this->instance->is_dir('/folder/sub_b')); - $this->assertTrue($this->instance->is_dir('/folder/sub_b/sub_bb')); - $this->assertTrue($this->instance->is_dir('/folder/sub_c')); - $this->assertTrue($this->instance->is_dir('/folder/sub_c/sub_bb')); - - $this->assertTrue($this->instance->rmdir('/folder')); - $this->assertFalse($this->instance->is_dir('/folder')); - $this->assertFalse($this->instance->is_dir('/folder/sub_a')); - $this->assertFalse($this->instance->is_dir('/folder/sub_b')); - $this->assertFalse($this->instance->is_dir('/folder/sub_c')); - $this->assertFalse($this->instance->is_dir('/folder/sub_b/sub_bb')); - $this->assertFalse($this->instance->is_dir('/folder/sub_c/sub_bb')); } public function testLocal() { -- cgit v1.2.3 From 424ec94680e69a3082a3d3f7c1ceefd7eeae15d2 Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Fri, 8 Mar 2013 14:22:04 +0100 Subject: Make buildNotExistingFileName testable and write unittests --- lib/helper.php | 14 +++++++++++++- tests/lib/helper.php | 42 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/lib/helper.php b/lib/helper.php index 1860a55fc8f..017221cef77 100644 --- a/lib/helper.php +++ b/lib/helper.php @@ -636,6 +636,18 @@ class OC_Helper { * @return string */ public static function buildNotExistingFileName($path, $filename) { + $view = \OC\Files\Filesystem::getView(); + return self::buildNotExistingFileNameForView($path, $filename, $view); + } + + /** + * Adds a suffix to the name in case the file exists + * + * @param $path + * @param $filename + * @return string + */ + public static function buildNotExistingFileNameForView($path, $filename, \OC\Files\View $view) { if($path==='/') { $path=''; } @@ -649,7 +661,7 @@ class OC_Helper { $newpath = $path . '/' . $filename; $counter = 2; - while (\OC\Files\Filesystem::file_exists($newpath)) { + while ($view->file_exists($newpath)) { $newname = $name . ' (' . $counter . ')' . $ext; $newpath = $path . '/' . $newname; $counter++; diff --git a/tests/lib/helper.php b/tests/lib/helper.php index 6acb0dfaa6b..410117a9e67 100644 --- a/tests/lib/helper.php +++ b/tests/lib/helper.php @@ -146,4 +146,46 @@ class Test_Helper extends PHPUnit_Framework_TestCase { $result = OC_Helper::recursiveArraySearch($haystack, "NotFound"); $this->assertFalse($result); } + + function testBuildNotExistingFileNameForView() { + $viewMock = $this->getMock('\OC\Files\View', array(), array(), '', false); + $this->assertEquals('/filename', OC_Helper::buildNotExistingFileNameForView('/', 'filename', $viewMock)); + $this->assertEquals('dir/filename.ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename.ext', $viewMock)); + + $viewMock->expects($this->at(0)) + ->method('file_exists') + ->will($this->returnValue(true)); + $this->assertEquals('dir/filename (2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename.ext', $viewMock)); + + $viewMock->expects($this->at(0)) + ->method('file_exists') + ->will($this->returnValue(true)); + $viewMock->expects($this->at(1)) + ->method('file_exists') + ->will($this->returnValue(true)); + $this->assertEquals('dir/filename (3).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename.ext', $viewMock)); + + $viewMock->expects($this->at(0)) + ->method('file_exists') + ->will($this->returnValue(true)); + $this->assertEquals('dir/filename (2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename (1).ext', $viewMock)); + + $viewMock->expects($this->at(0)) + ->method('file_exists') + ->will($this->returnValue(true)); + $viewMock->expects($this->at(1)) + ->method('file_exists') + ->will($this->returnValue(true)); + $this->assertEquals('dir/filename (3).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename (2).ext', $viewMock)); + + $viewMock->expects($this->at(0)) + ->method('file_exists') + ->will($this->returnValue(true)); + $this->assertEquals('dir/filename(2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1).ext', $viewMock)); + + $viewMock->expects($this->at(0)) + ->method('file_exists') + ->will($this->returnValue(true)); + $this->assertEquals('dir/filename(1) (2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1) (1).ext', $viewMock)); + } } -- 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 From 22c29eb64b67ee18beacf126eecb78fd4a02608c Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Fri, 5 Jul 2013 15:38:09 +0200 Subject: Fix renaming using parenthesis --- lib/helper.php | 26 +++++++++++++++++++++----- tests/lib/helper.php | 36 +++++++++++++++++++++++++++--------- 2 files changed, 48 insertions(+), 14 deletions(-) (limited to 'tests') diff --git a/lib/helper.php b/lib/helper.php index 017221cef77..df0d120976d 100644 --- a/lib/helper.php +++ b/lib/helper.php @@ -660,11 +660,27 @@ class OC_Helper { } $newpath = $path . '/' . $filename; - $counter = 2; - while ($view->file_exists($newpath)) { - $newname = $name . ' (' . $counter . ')' . $ext; - $newpath = $path . '/' . $newname; - $counter++; + if ($view->file_exists($newpath)) { + if(preg_match_all('/\((\d+)\)/', $name, $matches, PREG_OFFSET_CAPTURE)) { + //Replace the last "(number)" with "(number+1)" + $last_match = count($matches[0])-1; + $counter = $matches[1][$last_match][0]+1; + $offset = $matches[0][$last_match][1]; + $match_length = strlen($matches[0][$last_match][0]); + } else { + $counter = 2; + $offset = false; + } + do { + if($offset) { + //Replace the last "(number)" with "(number+1)" + $newname = substr_replace($name, '('.$counter.')', $offset, $match_length); + } else { + $newname = $name . ' (' . $counter . ')'; + } + $newpath = $path . '/' . $newname . $ext; + $counter++; + } while ($view->file_exists($newpath)); } return $newpath; diff --git a/tests/lib/helper.php b/tests/lib/helper.php index 410117a9e67..67b5a3d43ec 100644 --- a/tests/lib/helper.php +++ b/tests/lib/helper.php @@ -154,38 +154,56 @@ class Test_Helper extends PHPUnit_Framework_TestCase { $viewMock->expects($this->at(0)) ->method('file_exists') - ->will($this->returnValue(true)); + ->will($this->returnValue(true)); // filename.ext exists $this->assertEquals('dir/filename (2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename.ext', $viewMock)); $viewMock->expects($this->at(0)) ->method('file_exists') - ->will($this->returnValue(true)); + ->will($this->returnValue(true)); // filename.ext exists $viewMock->expects($this->at(1)) ->method('file_exists') - ->will($this->returnValue(true)); + ->will($this->returnValue(true)); // filename (2).ext exists $this->assertEquals('dir/filename (3).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename.ext', $viewMock)); $viewMock->expects($this->at(0)) ->method('file_exists') - ->will($this->returnValue(true)); + ->will($this->returnValue(true)); // filename (1).ext exists $this->assertEquals('dir/filename (2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename (1).ext', $viewMock)); $viewMock->expects($this->at(0)) ->method('file_exists') - ->will($this->returnValue(true)); + ->will($this->returnValue(true)); // filename (2).ext exists + $this->assertEquals('dir/filename (3).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename (2).ext', $viewMock)); + + $viewMock->expects($this->at(0)) + ->method('file_exists') + ->will($this->returnValue(true)); // filename (2).ext exists $viewMock->expects($this->at(1)) ->method('file_exists') - ->will($this->returnValue(true)); - $this->assertEquals('dir/filename (3).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename (2).ext', $viewMock)); + ->will($this->returnValue(true)); // filename (3).ext exists + $this->assertEquals('dir/filename (4).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename (2).ext', $viewMock)); $viewMock->expects($this->at(0)) ->method('file_exists') - ->will($this->returnValue(true)); + ->will($this->returnValue(true)); // filename(1).ext exists $this->assertEquals('dir/filename(2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1).ext', $viewMock)); $viewMock->expects($this->at(0)) ->method('file_exists') - ->will($this->returnValue(true)); + ->will($this->returnValue(true)); // filename(1) (1).ext exists $this->assertEquals('dir/filename(1) (2).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1) (1).ext', $viewMock)); + + $viewMock->expects($this->at(0)) + ->method('file_exists') + ->will($this->returnValue(true)); // filename(1) (1).ext exists + $viewMock->expects($this->at(1)) + ->method('file_exists') + ->will($this->returnValue(true)); // filename(1) (2).ext exists + $this->assertEquals('dir/filename(1) (3).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1) (1).ext', $viewMock)); + + $viewMock->expects($this->at(0)) + ->method('file_exists') + ->will($this->returnValue(true)); // filename(1) (2) (3).ext exists + $this->assertEquals('dir/filename(1) (2) (4).ext', OC_Helper::buildNotExistingFileNameForView('dir', 'filename(1) (2) (3).ext', $viewMock)); } } -- cgit v1.2.3 From 8c9e6db1b10c8dac559d3eaec0952e9b733dc7fe Mon Sep 17 00:00:00 2001 From: Christian Berendt Date: Mon, 8 Jul 2013 15:03:55 +0200 Subject: increasing allowed time difference --- tests/lib/files/storage/storage.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) (limited to 'tests') diff --git a/tests/lib/files/storage/storage.php b/tests/lib/files/storage/storage.php index 155a99d8bab..fbf502fbc1c 100644 --- a/tests/lib/files/storage/storage.php +++ b/tests/lib/files/storage/storage.php @@ -168,10 +168,10 @@ abstract class Storage extends \PHPUnit_Framework_TestCase { $this->assertTrue($this->instance->isReadable('/lorem.txt')); $ctimeEnd = time(); $mTime = $this->instance->filemtime('/lorem.txt'); - $this->assertTrue($this->instance->hasUpdated('/lorem.txt', $ctimeStart - 3)); - $this->assertTrue($this->instance->hasUpdated('/', $ctimeStart - 3)); + $this->assertTrue($this->instance->hasUpdated('/lorem.txt', $ctimeStart - 5)); + $this->assertTrue($this->instance->hasUpdated('/', $ctimeStart - 5)); - $this->assertTrue(($ctimeStart - 3) <= $mTime); + $this->assertTrue(($ctimeStart - 5) <= $mTime); $this->assertTrue($mTime <= ($ctimeEnd + 1)); $this->assertEquals(filesize($textFile), $this->instance->filesize('/lorem.txt')); @@ -185,10 +185,10 @@ abstract class Storage extends \PHPUnit_Framework_TestCase { $mtimeEnd = time(); if ($supportsTouch !== false) { $mTime = $this->instance->filemtime('/lorem.txt'); - $this->assertTrue(($mtimeStart - 3) <= $mTime); - $this->assertTrue($mTime <= ($mtimeEnd + 3)); + $this->assertTrue(($mtimeStart - 5) <= $mTime); + $this->assertTrue($mTime <= ($mtimeEnd + 5)); - $this->assertTrue($this->instance->hasUpdated('/lorem.txt', $mtimeStart - 3)); + $this->assertTrue($this->instance->hasUpdated('/lorem.txt', $mtimeStart - 5)); if ($this->instance->touch('/lorem.txt', 100) !== false) { $mTime = $this->instance->filemtime('/lorem.txt'); @@ -203,11 +203,11 @@ abstract class Storage extends \PHPUnit_Framework_TestCase { clearstatcache(); $mtimeEnd = time(); $mTime = $this->instance->filemtime('/lorem.txt'); - $this->assertTrue(($mtimeStart - 3) <= $mTime); - $this->assertTrue($mTime <= ($mtimeEnd + 3)); + $this->assertTrue(($mtimeStart - 5) <= $mTime); + $this->assertTrue($mTime <= ($mtimeEnd + 5)); $this->instance->unlink('/lorem.txt'); - $this->assertTrue($this->instance->hasUpdated('/', $mtimeStart - 3)); + $this->assertTrue($this->instance->hasUpdated('/', $mtimeStart - 5)); } public function testSearch() { -- cgit v1.2.3 From 925d09cb0e3aed2ab89a11d636111ba699cc403f Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Mon, 8 Jul 2013 16:45:19 +0200 Subject: add forwarding emitter for agregating multiple emitters --- lib/hooks/forwardingemitter.php | 42 ++++++++++++++++++++++++ tests/lib/hooks/forwardingemitter.php | 62 +++++++++++++++++++++++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 lib/hooks/forwardingemitter.php create mode 100644 tests/lib/hooks/forwardingemitter.php (limited to 'tests') diff --git a/lib/hooks/forwardingemitter.php b/lib/hooks/forwardingemitter.php new file mode 100644 index 00000000000..518641ac7cf --- /dev/null +++ b/lib/hooks/forwardingemitter.php @@ -0,0 +1,42 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Hooks; + +/** + * Class ForwardingEmitter + * + * allows forwarding all listen calls to other emitters + * + * @package OC\Hooks + */ +abstract class ForwardingEmitter extends BasicEmitter { + /** + * @var \OC\Hooks\Emitter[] array + */ + private $forwardEmitters = array(); + + /** + * @param string $scope + * @param string $method + * @param callable $callback + */ + public function listen($scope, $method, $callback) { + parent::listen($scope, $method, $callback); + foreach ($this->forwardEmitters as $emitter) { + $emitter->listen($scope, $method, $callback); + } + } + + /** + * @param \OC\Hooks\Emitter $emitter + */ + protected function forward($emitter) { + $this->forwardEmitters[] = $emitter; + } +} diff --git a/tests/lib/hooks/forwardingemitter.php b/tests/lib/hooks/forwardingemitter.php new file mode 100644 index 00000000000..0686ebece9f --- /dev/null +++ b/tests/lib/hooks/forwardingemitter.php @@ -0,0 +1,62 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\Hooks; +use OC\Hooks\PublicEmitter; + +class DummyForwardingEmitter extends \OC\Hooks\ForwardingEmitter { + public function emitEvent($scope, $method, $arguments = array()) { + $this->emit($scope, $method, $arguments); + } + + /** + * @param \OC\Hooks\Emitter $emitter + */ + public function forward($emitter) { + parent::forward($emitter); + } +} + +/** + * Class ForwardingEmitter + * + * allows forwarding all listen calls to other emitters + * + * @package OC\Hooks + */ +class ForwardingEmitter extends BasicEmitter { + public function testSingleForward() { + $baseEmitter = new PublicEmitter(); + $forwardingEmitter = new DummyForwardingEmitter(); + $forwardingEmitter->forward($baseEmitter); + $hookCalled = false; + $forwardingEmitter->listen('Test', 'test', function () use (&$hookCalled) { + $hookCalled = true; + }); + $baseEmitter->emit('Test', 'test'); + $this->assertTrue($hookCalled); + } + + public function testMultipleForwards() { + $baseEmitter1 = new PublicEmitter(); + $baseEmitter2 = new PublicEmitter(); + $forwardingEmitter = new DummyForwardingEmitter(); + $forwardingEmitter->forward($baseEmitter1); + $forwardingEmitter->forward($baseEmitter2); + $hookCalled = 0; + $forwardingEmitter->listen('Test', 'test1', function () use (&$hookCalled) { + $hookCalled++; + }); + $forwardingEmitter->listen('Test', 'test2', function () use (&$hookCalled) { + $hookCalled++; + }); + $baseEmitter1->emit('Test', 'test1'); + $baseEmitter1->emit('Test', 'test2'); + $this->assertEquals(2, $hookCalled); + } +} -- cgit v1.2.3 From 4a4e139c8391986ce54585f3292f3c2e40dd624d Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Mon, 8 Jul 2013 16:54:26 +0200 Subject: forward previously registerd hooks --- lib/hooks/basicemitter.php | 2 +- lib/hooks/forwardingemitter.php | 8 ++++++++ tests/lib/hooks/forwardingemitter.php | 12 ++++++++++++ 3 files changed, 21 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/lib/hooks/basicemitter.php b/lib/hooks/basicemitter.php index e615a58cfe8..9ffe1af2314 100644 --- a/lib/hooks/basicemitter.php +++ b/lib/hooks/basicemitter.php @@ -13,7 +13,7 @@ abstract class BasicEmitter implements Emitter { /** * @var (callable[])[] $listeners */ - private $listeners = array(); + protected $listeners = array(); /** * @param string $scope diff --git a/lib/hooks/forwardingemitter.php b/lib/hooks/forwardingemitter.php index 518641ac7cf..1aacc4012e0 100644 --- a/lib/hooks/forwardingemitter.php +++ b/lib/hooks/forwardingemitter.php @@ -38,5 +38,13 @@ abstract class ForwardingEmitter extends BasicEmitter { */ protected function forward($emitter) { $this->forwardEmitters[] = $emitter; + + //forward all previously connected hooks + foreach ($this->listeners as $key => $listeners) { + list($scope, $method) = explode('::', $key, 2); + foreach ($listeners as $listener) { + $emitter->listen($scope, $method, $listener); + } + } } } diff --git a/tests/lib/hooks/forwardingemitter.php b/tests/lib/hooks/forwardingemitter.php index 0686ebece9f..decf6bb354c 100644 --- a/tests/lib/hooks/forwardingemitter.php +++ b/tests/lib/hooks/forwardingemitter.php @@ -59,4 +59,16 @@ class ForwardingEmitter extends BasicEmitter { $baseEmitter1->emit('Test', 'test2'); $this->assertEquals(2, $hookCalled); } + + public function testForwardExistingHooks() { + $baseEmitter = new PublicEmitter(); + $forwardingEmitter = new DummyForwardingEmitter(); + $hookCalled = false; + $forwardingEmitter->listen('Test', 'test', function () use (&$hookCalled) { + $hookCalled = true; + }); + $forwardingEmitter->forward($baseEmitter); + $baseEmitter->emit('Test', 'test'); + $this->assertTrue($hookCalled); + } } -- cgit v1.2.3 From a735105a224e83f502e1d49366509e2afa2989b4 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Wed, 10 Jul 2013 00:06:22 +0200 Subject: update group management --- lib/group/group.php | 238 ++++++++++++++++++++++++++++++++ lib/group/manager.php | 163 ++++++++++++++++++++++ lib/user.php | 4 +- lib/user/manager.php | 3 + lib/user/user.php | 2 +- tests/lib/group/group.php | 320 ++++++++++++++++++++++++++++++++++++++++++++ tests/lib/group/manager.php | 297 ++++++++++++++++++++++++++++++++++++++++ 7 files changed, 1024 insertions(+), 3 deletions(-) create mode 100644 lib/group/group.php create mode 100644 lib/group/manager.php create mode 100644 tests/lib/group/group.php create mode 100644 tests/lib/group/manager.php (limited to 'tests') diff --git a/lib/group/group.php b/lib/group/group.php new file mode 100644 index 00000000000..6d0cb8f316b --- /dev/null +++ b/lib/group/group.php @@ -0,0 +1,238 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Group; + +class Group { + /** + * @var string $id + */ + private $id; + + /** + * @var \OC\User\User[] $users + */ + private $users; + + /** + * @var \OC_Group_Backend[] | \OC_Group_Database[] $backend + */ + private $backends; + + /** + * @var \OC\Hooks\PublicEmitter $emitter; + */ + private $emitter; + + /** + * @var \OC\User\Manager $userManager + */ + private $userManager; + + /** + * @param string $id + * @param \OC_Group_Backend[] $backends + * @param \OC\User\Manager $userManager + * @param \OC\Hooks\PublicEmitter $emitter + */ + public function __construct($id, $backends, $userManager, $emitter = null) { + $this->id = $id; + $this->backends = $backends; + $this->userManager = $userManager; + $this->emitter = $emitter; + } + + public function getGID() { + return $this->id; + } + + /** + * get all users in the group + * + * @return \OC\User\User[] + */ + public function getUsers() { + if ($this->users) { + return $this->users; + } + + $users = array(); + $userIds = array(); + foreach ($this->backends as $backend) { + $diff = array_diff( + $backend->usersInGroup($this->id), + $userIds + ); + if ($diff) { + $userIds = array_merge($userIds, $diff); + } + } + + foreach ($userIds as $userId) { + $users[] = $this->userManager->get($userId); + } + $this->users = $users; + return $users; + } + + /** + * check if a user is in the group + * + * @param \OC\User\User $user + * @return bool + */ + public function inGroup($user) { + foreach ($this->backends as $backend) { + if ($backend->inGroup($user->getUID(), $this->id)) { + return true; + } + } + return false; + } + + /** + * add a user to the group + * + * @param \OC\User\User $user + */ + public function addUser($user) { + if ($this->inGroup($user)) { + return; + } + + if ($this->emitter) { + $this->emitter->emit('\OC\Group', 'preAddUser', array($this, $user)); + } + foreach ($this->backends as $backend) { + if ($backend->implementsActions(OC_GROUP_BACKEND_ADD_TO_GROUP)) { + $backend->addToGroup($user->getUID(), $this->id); + if ($this->users) { + $this->users[] = $user; + } + if ($this->emitter) { + $this->emitter->emit('\OC\Group', 'addUser', array($this, $user)); + } + return; + } + } + } + + /** + * remove a user from the group + * + * @param \OC\User\User $user + */ + public function removeUser($user) { + $result = false; + if ($this->emitter) { + $this->emitter->emit('\OC\Group', 'preRemoveUser', array($this, $user)); + } + foreach ($this->backends as $backend) { + if ($backend->implementsActions(OC_GROUP_BACKEND_REMOVE_FROM_GOUP) and $backend->inGroup($user->getUID(), $this->id)) { + $backend->removeFromGroup($user->getUID(), $this->id); + $result = true; + } + } + if ($result) { + if ($this->emitter) { + $this->emitter->emit('\OC\Group', 'removeUser', array($this, $user)); + } + if ($this->users) { + foreach ($this->users as $index => $groupUser) { + if ($groupUser->getUID() === $user->getUID()) { + unset($this->users[$index]); + return; + } + } + } + } + } + + /** + * search for users in the group by userid + * + * @param string $search + * @param int $limit + * @param int $offset + * @return \OC\User\User[] + */ + public function searchUsers($search = '', $limit = null, $offset = null) { + $users = array(); + foreach ($this->backends as $backend) { + $userIds = $backend->usersInGroup($this->id, $search, $limit, $offset); + if (!is_null($limit)) { + $limit -= count($userIds); + } + if (!is_null($offset)) { + $offset -= count($userIds); + } + foreach ($userIds as $userId) { + $users[$userId] = $this->userManager->get($userId); + } + if (!is_null($limit) and $limit <= 0) { + return array_values($users); + } + } + return array_values($users); + } + + /** + * search for users in the group by displayname + * + * @param string $search + * @param int $limit + * @param int $offset + * @return \OC\User\User[] + */ + public function searchDisplayName($search = '', $limit = null, $offset = null) { + $users = array(); + foreach ($this->backends as $backend) { + if ($backend->implementsActions(OC_GROUP_BACKEND_GET_DISPLAYNAME)) { + $userIds = array_keys($backend->displayNamesInGroup($this->id, $search, $limit, $offset)); + } else { + $userIds = $backend->usersInGroup($this->id, $search, $limit, $offset); + } + if (!is_null($limit)) { + $limit -= count($userIds); + } + if (!is_null($offset)) { + $offset -= count($userIds); + } + foreach ($userIds as $userId) { + $users[$userId] = $this->userManager->get($userId); + } + if (!is_null($limit) and $limit <= 0) { + return array_values($users); + } + } + return array_values($users); + } + + /** + * delete the group + * + * @return bool + */ + public function delete() { + $result = false; + if ($this->emitter) { + $this->emitter->emit('\OC\Group', 'preDelete', array($this)); + } + foreach ($this->backends as $backend) { + if ($backend->implementsActions(OC_GROUP_BACKEND_DELETE_GROUP) and $backend->groupExists($this->id)) { + $result = true; + $backend->deleteGroup($this->id); + } + } + if ($result and $this->emitter) { + $this->emitter->emit('\OC\Group', 'delete', array($this)); + } + return $result; + } +} diff --git a/lib/group/manager.php b/lib/group/manager.php new file mode 100644 index 00000000000..7f96065c6d2 --- /dev/null +++ b/lib/group/manager.php @@ -0,0 +1,163 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Group; + +use OC\Hooks\PublicEmitter; + +/** + * Class Manager + * + * Hooks available in scope \OC\Group: + * - addUser(\OC\Group\Group $group, \OC\User\User $user) + * - preAddUser(\OC\Group\Group $group, \OC\User\User $user) + * - removeUser(\OC\Group\Group $group, \OC\User\User $user) + * - preRemoveUser(\OC\Group\Group $group, \OC\User\User $user) + * - delete(\OC\Group\Group $group) + * - preDelete(\OC\Group\Group $group) + * - create(\OC\Group\Group $group) + * - preCreate(string $groupId) + * + * @package OC\Group + */ +class Manager extends PublicEmitter { + /** + * @var \OC_Group_Backend[] | \OC_Group_Database[] $backends + */ + private $backends = array(); + + /** + * @var \OC\User\Manager $userManager + */ + private $userManager; + + /** + * @var \OC\Group\Group[] + */ + private $cachedGroups; + + /** + * @param \OC\User\Manager $userManager + */ + public function __construct($userManager) { + $this->userManager = $userManager; + $manager = $this; + $this->listen('\OC\Group', 'delete', function ($group) use (&$manager) { + /** + * @var \OC\Group\Group $group + */ + unset($manager->cachedGroups[$group->getGID()]); + }); + } + + /** + * @param \OC_Group_Backend $backend + */ + public function addBackend($backend) { + $this->backends[] = $backend; + } + + public function clearBackends() { + $this->backends = array(); + $this->cachedGroups = array(); + } + + /** + * @param string $gid + * @return \OC\Group\Group + */ + public function get($gid) { + if (isset($this->cachedGroups[$gid])) { + return $this->cachedGroups[$gid]; + } + foreach ($this->backends as $backend) { + if ($backend->groupExists($gid)) { + return $this->createGroupObject($gid); + } + } + return null; + } + + protected function createGroupObject($gid) { + $this->cachedGroups[$gid] = new Group($gid, $this->backends, $this->userManager, $this); + return $this->cachedGroups[$gid]; + } + + /** + * @param string $gid + * @return bool + */ + public function exists($gid) { + return !is_null($this->get($gid)); + } + + /** + * @param string $gid + * @return \OC\Group\Group + */ + public function create($gid) { + if (!$gid) { + return false; + } else if ($this->exists($gid)) { + return $this->get($gid); + } else { + $this->emit('\OC\Group', 'preCreate', array($gid)); + foreach ($this->backends as $backend) { + if ($backend->implementsActions(OC_GROUP_BACKEND_CREATE_GROUP)) { + $backend->createGroup($gid); + $group = $this->createGroupObject($gid); + $this->emit('\OC\Group', 'create', array($group)); + return $group; + } + } + return null; + } + } + + /** + * @param string $search + * @param int $limit + * @param int $offset + * @return \OC\Group\Group[] + */ + public function search($search = '', $limit = null, $offset = null) { + $groups = array(); + foreach ($this->backends as $backend) { + $groupIds = $backend->getGroups($search, $limit, $offset); + if (!is_null($limit)) { + $limit -= count($groupIds); + } + if (!is_null($offset)) { + $offset -= count($groupIds); + } + foreach ($groupIds as $groupId) { + $groups[$groupId] = $this->createGroupObject($groupId); + } + if (!is_null($limit) and $limit <= 0) { + return array_values($groups); + } + } + return array_values($groups); + } + + /** + * @param \OC\User\User $user + * @return \OC\Group\Group[] + */ + public function getUserGroups($user) { + $groups = array(); + foreach ($this->backends as $backend) { + $groupIds = $backend->getUserGroups($user->getUID()); + foreach ($groupIds as $groupId) { + $groups[$groupId] = $this->createGroupObject($groupId); + } + } + return array_values($groups); + } +} diff --git a/lib/user.php b/lib/user.php index 830f13bb8df..908f2cbabfb 100644 --- a/lib/user.php +++ b/lib/user.php @@ -39,7 +39,7 @@ class OC_User { public static $userSession = null; - private static function getUserSession() { + public static function getUserSession() { if (!self::$userSession) { $manager = new \OC\User\Manager(); self::$userSession = new \OC\User\Session($manager, \OC::$session); @@ -83,7 +83,7 @@ class OC_User { /** * @return \OC\User\Manager */ - private static function getManager() { + public static function getManager() { return self::getUserSession()->getManager(); } diff --git a/lib/user/manager.php b/lib/user/manager.php index d17cdf1a200..8dc9bfe2729 100644 --- a/lib/user/manager.php +++ b/lib/user/manager.php @@ -30,6 +30,9 @@ class Manager extends PublicEmitter { */ private $backends = array(); + /** + * @var \OC\User\User[] $cachedUsers + */ private $cachedUsers = array(); public function __construct() { diff --git a/lib/user/user.php b/lib/user/user.php index 55d7848a979..8115c43198c 100644 --- a/lib/user/user.php +++ b/lib/user/user.php @@ -44,7 +44,7 @@ class User { */ public function __construct($uid, $backend, $emitter = null) { $this->uid = $uid; - if ($backend->implementsActions(OC_USER_BACKEND_GET_DISPLAYNAME)) { + if ($backend and $backend->implementsActions(OC_USER_BACKEND_GET_DISPLAYNAME)) { $this->displayName = $backend->getDisplayName($uid); } else { $this->displayName = $uid; diff --git a/tests/lib/group/group.php b/tests/lib/group/group.php new file mode 100644 index 00000000000..116aefda81c --- /dev/null +++ b/tests/lib/group/group.php @@ -0,0 +1,320 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\Group; + +use OC\User\User; + +class Group extends \PHPUnit_Framework_TestCase { + /** + * @return \PHPUnit_Framework_MockObject_MockObject | \OC\User\Manager + */ + protected function getUserManager() { + $userManager = $this->getMock('\OC\User\Manager'); + $user1 = new User('user1', null); + $user2 = new User('user2', null); + $user3 = new User('user3', null); + $userManager->expects($this->any()) + ->method('get') + ->will($this->returnValueMap(array( + array('user1', $user1), + array('user2', $user2), + array('user3', $user3) + ))); + return $userManager; + } + + public function testGetUsersSingleBackend() { + $backend = $this->getMock('OC_Group_Database'); + $userManager = $this->getUserManager(); + $group = new \OC\Group\Group('group1', array($backend), $userManager); + + $backend->expects($this->once()) + ->method('usersInGroup') + ->with('group1') + ->will($this->returnValue(array('user1', 'user2'))); + + $users = $group->getUsers(); + + $this->assertEquals(2, count($users)); + $user1 = $users[0]; + $user2 = $users[1]; + $this->assertEquals('user1', $user1->getUID()); + $this->assertEquals('user2', $user2->getUID()); + } + + public function testGetUsersMultipleBackends() { + $backend1 = $this->getMock('OC_Group_Database'); + $backend2 = $this->getMock('OC_Group_Database'); + $userManager = $this->getUserManager(); + $group = new \OC\Group\Group('group1', array($backend1, $backend2), $userManager); + + $backend1->expects($this->once()) + ->method('usersInGroup') + ->with('group1') + ->will($this->returnValue(array('user1', 'user2'))); + + $backend2->expects($this->once()) + ->method('usersInGroup') + ->with('group1') + ->will($this->returnValue(array('user2', 'user3'))); + + $users = $group->getUsers(); + + $this->assertEquals(3, count($users)); + $user1 = $users[0]; + $user2 = $users[1]; + $user3 = $users[2]; + $this->assertEquals('user1', $user1->getUID()); + $this->assertEquals('user2', $user2->getUID()); + $this->assertEquals('user3', $user3->getUID()); + } + + public function testInGroupSingleBackend() { + $backend = $this->getMock('OC_Group_Database'); + $userManager = $this->getUserManager(); + $group = new \OC\Group\Group('group1', array($backend), $userManager); + + $backend->expects($this->once()) + ->method('inGroup') + ->with('user1', 'group1') + ->will($this->returnValue(true)); + + $this->assertTrue($group->inGroup(new User('user1', null))); + } + + public function testInGroupMultipleBackends() { + $backend1 = $this->getMock('OC_Group_Database'); + $backend2 = $this->getMock('OC_Group_Database'); + $userManager = $this->getUserManager(); + $group = new \OC\Group\Group('group1', array($backend1, $backend2), $userManager); + + $backend1->expects($this->once()) + ->method('inGroup') + ->with('user1', 'group1') + ->will($this->returnValue(false)); + + $backend2->expects($this->once()) + ->method('inGroup') + ->with('user1', 'group1') + ->will($this->returnValue(true)); + + $this->assertTrue($group->inGroup(new User('user1', null))); + } + + public function testAddUser() { + $backend = $this->getMock('OC_Group_Database'); + $userManager = $this->getUserManager(); + $group = new \OC\Group\Group('group1', array($backend), $userManager); + + $backend->expects($this->once()) + ->method('inGroup') + ->with('user1', 'group1') + ->will($this->returnValue(false)); + $backend->expects($this->any()) + ->method('implementsActions') + ->will($this->returnValue(true)); + + $backend->expects($this->once()) + ->method('addToGroup') + ->with('user1', 'group1'); + + $group->addUser(new User('user1', null)); + } + + public function testAddUserAlreadyInGroup() { + $backend = $this->getMock('OC_Group_Database'); + $userManager = $this->getUserManager(); + $group = new \OC\Group\Group('group1', array($backend), $userManager); + + $backend->expects($this->once()) + ->method('inGroup') + ->with('user1', 'group1') + ->will($this->returnValue(true)); + $backend->expects($this->any()) + ->method('implementsActions') + ->will($this->returnValue(true)); + + $backend->expects($this->never()) + ->method('addToGroup'); + + $group->addUser(new User('user1', null)); + } + + public function testRemoveUser() { + $backend = $this->getMock('OC_Group_Database'); + $userManager = $this->getUserManager(); + $group = new \OC\Group\Group('group1', array($backend), $userManager); + + $backend->expects($this->once()) + ->method('inGroup') + ->with('user1', 'group1') + ->will($this->returnValue(true)); + $backend->expects($this->any()) + ->method('implementsActions') + ->will($this->returnValue(true)); + + $backend->expects($this->once()) + ->method('removeFromGroup') + ->with('user1', 'group1'); + + $group->removeUser(new User('user1', null)); + } + + public function testRemoveUserNotInGroup() { + $backend = $this->getMock('OC_Group_Database'); + $userManager = $this->getUserManager(); + $group = new \OC\Group\Group('group1', array($backend), $userManager); + + $backend->expects($this->once()) + ->method('inGroup') + ->with('user1', 'group1') + ->will($this->returnValue(false)); + $backend->expects($this->any()) + ->method('implementsActions') + ->will($this->returnValue(true)); + + $backend->expects($this->never()) + ->method('removeFromGroup'); + + $group->removeUser(new User('user1', null)); + } + + public function testRemoveUserMultipleBackends() { + $backend1 = $this->getMock('OC_Group_Database'); + $backend2 = $this->getMock('OC_Group_Database'); + $userManager = $this->getUserManager(); + $group = new \OC\Group\Group('group1', array($backend1, $backend2), $userManager); + + $backend1->expects($this->once()) + ->method('inGroup') + ->with('user1', 'group1') + ->will($this->returnValue(true)); + $backend1->expects($this->any()) + ->method('implementsActions') + ->will($this->returnValue(true)); + + $backend1->expects($this->once()) + ->method('removeFromGroup') + ->with('user1', 'group1'); + + $backend2->expects($this->once()) + ->method('inGroup') + ->with('user1', 'group1') + ->will($this->returnValue(true)); + $backend2->expects($this->any()) + ->method('implementsActions') + ->will($this->returnValue(true)); + + $backend2->expects($this->once()) + ->method('removeFromGroup') + ->with('user1', 'group1'); + + $group->removeUser(new User('user1', null)); + } + + public function testSearchUsers() { + $backend = $this->getMock('OC_Group_Database'); + $userManager = $this->getUserManager(); + $group = new \OC\Group\Group('group1', array($backend), $userManager); + + $backend->expects($this->once()) + ->method('usersInGroup') + ->with('group1', '2') + ->will($this->returnValue(array('user2'))); + + $users = $group->searchUsers('2'); + + $this->assertEquals(1, count($users)); + $user2 = $users[0]; + $this->assertEquals('user2', $user2->getUID()); + } + + public function testSearchUsersMultipleBackends() { + $backend1 = $this->getMock('OC_Group_Database'); + $backend2 = $this->getMock('OC_Group_Database'); + $userManager = $this->getUserManager(); + $group = new \OC\Group\Group('group1', array($backend1, $backend2), $userManager); + + $backend1->expects($this->once()) + ->method('usersInGroup') + ->with('group1', '2') + ->will($this->returnValue(array('user2'))); + $backend2->expects($this->once()) + ->method('usersInGroup') + ->with('group1', '2') + ->will($this->returnValue(array('user2'))); + + $users = $group->searchUsers('2'); + + $this->assertEquals(1, count($users)); + $user2 = $users[0]; + $this->assertEquals('user2', $user2->getUID()); + } + + public function testSearchUsersLimitAndOffset() { + $backend = $this->getMock('OC_Group_Database'); + $userManager = $this->getUserManager(); + $group = new \OC\Group\Group('group1', array($backend), $userManager); + + $backend->expects($this->once()) + ->method('usersInGroup') + ->with('group1', 'user', 1, 1) + ->will($this->returnValue(array('user2'))); + + $users = $group->searchUsers('user', 1, 1); + + $this->assertEquals(1, count($users)); + $user2 = $users[0]; + $this->assertEquals('user2', $user2->getUID()); + } + + public function testSearchUsersMultipleBackendsLimitAndOffset() { + $backend1 = $this->getMock('OC_Group_Database'); + $backend2 = $this->getMock('OC_Group_Database'); + $userManager = $this->getUserManager(); + $group = new \OC\Group\Group('group1', array($backend1, $backend2), $userManager); + + $backend1->expects($this->once()) + ->method('usersInGroup') + ->with('group1', 'user', 2, 1) + ->will($this->returnValue(array('user2'))); + $backend2->expects($this->once()) + ->method('usersInGroup') + ->with('group1', 'user', 1, 0) + ->will($this->returnValue(array('user1'))); + + $users = $group->searchUsers('user', 2, 1); + + $this->assertEquals(2, count($users)); + $user2 = $users[0]; + $user1 = $users[1]; + $this->assertEquals('user2', $user2->getUID()); + $this->assertEquals('user1', $user1->getUID()); + } + + public function testDelete() { + $backend = $this->getMock('OC_Group_Database'); + $userManager = $this->getUserManager(); + $group = new \OC\Group\Group('group1', array($backend), $userManager); + + $backend->expects($this->once()) + ->method('deleteGroup') + ->with('group1'); + $backend->expects($this->once()) + ->method('groupExists') + ->with('group1') + ->will($this->returnValue(true)); + $backend->expects($this->any()) + ->method('implementsActions') + ->will($this->returnValue(true)); + + $group->delete(); + } +} diff --git a/tests/lib/group/manager.php b/tests/lib/group/manager.php new file mode 100644 index 00000000000..18f78c21a12 --- /dev/null +++ b/tests/lib/group/manager.php @@ -0,0 +1,297 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\Group; + +use OC\User\User; + +class Manager extends \PHPUnit_Framework_TestCase { + public function testGet() { + /** + * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend + */ + $backend = $this->getMock('\OC_Group_Database'); + $backend->expects($this->once()) + ->method('groupExists') + ->with('group1') + ->will($this->returnValue(true)); + + /** + * @var \OC\User\Manager $userManager + */ + $userManager = $this->getMock('\OC\User\Manager'); + $manager = new \OC\Group\Manager($userManager); + $manager->addBackend($backend); + + $group = $manager->get('group1'); + $this->assertNotNull($group); + $this->assertEquals('group1', $group->getGID()); + } + + public function testGetNoBackend() { + /** + * @var \OC\User\Manager $userManager + */ + $userManager = $this->getMock('\OC\User\Manager'); + $manager = new \OC\Group\Manager($userManager); + + $this->assertNull($manager->get('group1')); + } + + public function testGetNotExists() { + /** + * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend + */ + $backend = $this->getMock('\OC_Group_Database'); + $backend->expects($this->once()) + ->method('groupExists') + ->with('group1') + ->will($this->returnValue(false)); + + /** + * @var \OC\User\Manager $userManager + */ + $userManager = $this->getMock('\OC\User\Manager'); + $manager = new \OC\Group\Manager($userManager); + $manager->addBackend($backend); + + $this->assertNull($manager->get('group1')); + } + + public function testGetMultipleBackends() { + /** + * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend1 + */ + $backend1 = $this->getMock('\OC_Group_Database'); + $backend1->expects($this->once()) + ->method('groupExists') + ->with('group1') + ->will($this->returnValue(false)); + + /** + * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend2 + */ + $backend2 = $this->getMock('\OC_Group_Database'); + $backend2->expects($this->once()) + ->method('groupExists') + ->with('group1') + ->will($this->returnValue(true)); + + /** + * @var \OC\User\Manager $userManager + */ + $userManager = $this->getMock('\OC\User\Manager'); + $manager = new \OC\Group\Manager($userManager); + $manager->addBackend($backend1); + $manager->addBackend($backend2); + + $group = $manager->get('group1'); + $this->assertNotNull($group); + $this->assertEquals('group1', $group->getGID()); + } + + public function testCreate() { + /** + * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend + */ + $backend = $this->getMock('\OC_Group_Database'); + $backend->expects($this->once()) + ->method('groupExists') + ->with('group1') + ->will($this->returnValue(false)); + $backend->expects($this->once()) + ->method('implementsActions') + ->will($this->returnValue(true)); + $backend->expects($this->once()) + ->method('createGroup'); + + /** + * @var \OC\User\Manager $userManager + */ + $userManager = $this->getMock('\OC\User\Manager'); + $manager = new \OC\Group\Manager($userManager); + $manager->addBackend($backend); + + $group = $manager->create('group1'); + $this->assertEquals('group1', $group->getGID()); + } + + public function testCreateExists() { + /** + * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend + */ + $backend = $this->getMock('\OC_Group_Database'); + $backend->expects($this->once()) + ->method('groupExists') + ->with('group1') + ->will($this->returnValue(true)); + $backend->expects($this->never()) + ->method('createGroup'); + + /** + * @var \OC\User\Manager $userManager + */ + $userManager = $this->getMock('\OC\User\Manager'); + $manager = new \OC\Group\Manager($userManager); + $manager->addBackend($backend); + + $group = $manager->create('group1'); + $this->assertEquals('group1', $group->getGID()); + } + + public function testSearch() { + /** + * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend + */ + $backend = $this->getMock('\OC_Group_Database'); + $backend->expects($this->once()) + ->method('getGroups') + ->with('1') + ->will($this->returnValue(array('group1'))); + + /** + * @var \OC\User\Manager $userManager + */ + $userManager = $this->getMock('\OC\User\Manager'); + $manager = new \OC\Group\Manager($userManager); + $manager->addBackend($backend); + + $groups = $manager->search('1'); + $this->assertEquals(1, count($groups)); + $group1 = $groups[0]; + $this->assertEquals('group1', $group1->getGID()); + } + + public function testSearchMultipleBackends() { + /** + * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend1 + */ + $backend1 = $this->getMock('\OC_Group_Database'); + $backend1->expects($this->once()) + ->method('getGroups') + ->with('1') + ->will($this->returnValue(array('group1'))); + + /** + * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend2 + */ + $backend2 = $this->getMock('\OC_Group_Database'); + $backend2->expects($this->once()) + ->method('getGroups') + ->with('1') + ->will($this->returnValue(array('group12', 'group1'))); + + /** + * @var \OC\User\Manager $userManager + */ + $userManager = $this->getMock('\OC\User\Manager'); + $manager = new \OC\Group\Manager($userManager); + $manager->addBackend($backend1); + $manager->addBackend($backend2); + + $groups = $manager->search('1'); + $this->assertEquals(2, count($groups)); + $group1 = $groups[0]; + $group12 = $groups[1]; + $this->assertEquals('group1', $group1->getGID()); + $this->assertEquals('group12', $group12->getGID()); + } + + public function testSearchMultipleBackendsLimitAndOffset() { + /** + * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend1 + */ + $backend1 = $this->getMock('\OC_Group_Database'); + $backend1->expects($this->once()) + ->method('getGroups') + ->with('1', 2, 1) + ->will($this->returnValue(array('group1'))); + + /** + * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend2 + */ + $backend2 = $this->getMock('\OC_Group_Database'); + $backend2->expects($this->once()) + ->method('getGroups') + ->with('1', 1, 0) + ->will($this->returnValue(array('group12'))); + + /** + * @var \OC\User\Manager $userManager + */ + $userManager = $this->getMock('\OC\User\Manager'); + $manager = new \OC\Group\Manager($userManager); + $manager->addBackend($backend1); + $manager->addBackend($backend2); + + $groups = $manager->search('1', 2, 1); + $this->assertEquals(2, count($groups)); + $group1 = $groups[0]; + $group12 = $groups[1]; + $this->assertEquals('group1', $group1->getGID()); + $this->assertEquals('group12', $group12->getGID()); + } + + public function testGetUserGroups() { + /** + * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend + */ + $backend = $this->getMock('\OC_Group_Database'); + $backend->expects($this->once()) + ->method('getUserGroups') + ->with('user1') + ->will($this->returnValue(array('group1'))); + + /** + * @var \OC\User\Manager $userManager + */ + $userManager = $this->getMock('\OC\User\Manager'); + $manager = new \OC\Group\Manager($userManager); + $manager->addBackend($backend); + + $groups = $manager->getUserGroups(new User('user1', null)); + $this->assertEquals(1, count($groups)); + $group1 = $groups[0]; + $this->assertEquals('group1', $group1->getGID()); + } + + public function testGetUserGroupsMultipleBackends() { + /** + * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend1 + */ + $backend1 = $this->getMock('\OC_Group_Database'); + $backend1->expects($this->once()) + ->method('getUserGroups') + ->with('user1') + ->will($this->returnValue(array('group1'))); + /** + * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend2 + */ + $backend2 = $this->getMock('\OC_Group_Database'); + $backend2->expects($this->once()) + ->method('getUserGroups') + ->with('user1') + ->will($this->returnValue(array('group1', 'group2'))); + + /** + * @var \OC\User\Manager $userManager + */ + $userManager = $this->getMock('\OC\User\Manager'); + $manager = new \OC\Group\Manager($userManager); + $manager->addBackend($backend1); + $manager->addBackend($backend2); + + $groups = $manager->getUserGroups(new User('user1', null)); + $this->assertEquals(2, count($groups)); + $group1 = $groups[0]; + $group2 = $groups[1]; + $this->assertEquals('group1', $group1->getGID()); + $this->assertEquals('group2', $group2->getGID()); + } +} -- cgit v1.2.3 From 912b1515616c77007eb447ae1210225a1729a2c8 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Wed, 10 Jul 2013 00:07:46 +0200 Subject: use new group api as backend for the old api --- lib/group.php | 225 +++++++++++++++++++++++----------------------------- tests/lib/group.php | 108 ++++++++++++++----------- 2 files changed, 160 insertions(+), 173 deletions(-) (limited to 'tests') diff --git a/lib/group.php b/lib/group.php index d1a830730b7..4b57073a123 100644 --- a/lib/group.php +++ b/lib/group.php @@ -34,28 +34,43 @@ * post_removeFromGroup(uid, gid) */ class OC_Group { - // The backend used for group management /** - * @var OC_Group_Interface[] + * @var \OC\Group\Manager $manager */ - private static $_usedBackends = array(); + private static $manager; + + /** + * @var \OC\User\Manager + */ + private static $userManager; + + /** + * @return \OC\Group\Manager + */ + public static function getManager() { + if (self::$manager) { + return self::$manager; + } + self::$userManager = \OC_User::getManager(); + self::$manager = new \OC\Group\Manager(self::$userManager); + return self::$manager; + } /** * @brief set the group backend - * @param string $backend The backend to use for user managment + * @param \OC_Group_Backend $backend The backend to use for user managment * @return bool */ - public static function useBackend( $backend ) { - if($backend instanceof OC_Group_Interface) { - self::$_usedBackends[]=$backend; - } + public static function useBackend($backend) { + self::getManager()->addBackend($backend); + return true; } /** * remove all used backends */ public static function clearBackends() { - self::$_usedBackends=array(); + self::getManager()->clearBackends(); } /** @@ -66,32 +81,13 @@ class OC_Group { * Tries to create a new group. If the group name already exists, false will * be returned. Basic checking of Group name */ - public static function createGroup( $gid ) { - // No empty group names! - if( !$gid ) { - return false; - } - // No duplicate group names - if( in_array( $gid, self::getGroups())) { - return false; - } - - $run = true; - OC_Hook::emit( "OC_Group", "pre_createGroup", array( "run" => &$run, "gid" => $gid )); - - if($run) { - //create the group in the first backend that supports creating groups - foreach(self::$_usedBackends as $backend) { - if(!$backend->implementsActions(OC_GROUP_BACKEND_CREATE_GROUP)) - continue; + public static function createGroup($gid) { + OC_Hook::emit("OC_Group", "pre_createGroup", array("run" => true, "gid" => $gid)); - $backend->createGroup($gid); - OC_Hook::emit( "OC_User", "post_createGroup", array( "gid" => $gid )); - - return true; - } - return false; - }else{ + if (self::getManager()->create($gid)) { + OC_Hook::emit("OC_User", "post_createGroup", array("gid" => $gid)); + return true; + } else { return false; } } @@ -103,30 +99,22 @@ class OC_Group { * * Deletes a group and removes it from the group_user-table */ - public static function deleteGroup( $gid ) { + public static function deleteGroup($gid) { // Prevent users from deleting group admin - if( $gid == "admin" ) { + if ($gid == "admin") { return false; } - $run = true; - OC_Hook::emit( "OC_Group", "pre_deleteGroup", array( "run" => &$run, "gid" => $gid )); - - if($run) { - //delete the group from all backends - foreach(self::$_usedBackends as $backend) { - if(!$backend->implementsActions(OC_GROUP_BACKEND_DELETE_GROUP)) - continue; - - $backend->deleteGroup($gid); - OC_Hook::emit( "OC_User", "post_deleteGroup", array( "gid" => $gid )); + OC_Hook::emit("OC_Group", "pre_deleteGroup", array("run" => true, "gid" => $gid)); + $group = self::getManager()->get($gid); + if ($group) { + if ($group->delete()) { + OC_Hook::emit("OC_User", "post_deleteGroup", array("gid" => $gid)); return true; } - return false; - }else{ - return false; } + return false; } /** @@ -137,11 +125,11 @@ class OC_Group { * * Checks whether the user is member of a group or not. */ - public static function inGroup( $uid, $gid ) { - foreach(self::$_usedBackends as $backend) { - if($backend->inGroup($uid, $gid)) { - return true; - } + public static function inGroup($uid, $gid) { + $group = self::getManager()->get($gid); + $user = self::$userManager->get($uid); + if ($group and $user) { + return $group->inGroup($user); } return false; } @@ -154,33 +142,15 @@ class OC_Group { * * Adds a user to a group. */ - public static function addToGroup( $uid, $gid ) { - // Does the group exist? - if( !OC_Group::groupExists($gid)) { - return false; - } - - // Go go go - $run = true; - OC_Hook::emit( "OC_Group", "pre_addToGroup", array( "run" => &$run, "uid" => $uid, "gid" => $gid )); - - if($run) { - $success=false; - - //add the user to the all backends that have the group - foreach(self::$_usedBackends as $backend) { - if(!$backend->implementsActions(OC_GROUP_BACKEND_ADD_TO_GROUP)) - continue; - - if($backend->groupExists($gid)) { - $success|=$backend->addToGroup($uid, $gid); - } - } - if($success) { - OC_Hook::emit( "OC_User", "post_addToGroup", array( "uid" => $uid, "gid" => $gid )); - } - return $success; - }else{ + public static function addToGroup($uid, $gid) { + $group = self::getManager()->get($gid); + $user = self::$userManager->get($uid); + if ($group and $user) { + OC_Hook::emit("OC_Group", "pre_addToGroup", array("run" => true, "uid" => $uid, "gid" => $gid)); + $group->addUser($user); + OC_Hook::emit("OC_User", "post_addToGroup", array("uid" => $uid, "gid" => $gid)); + return true; + } else { return false; } } @@ -193,21 +163,15 @@ class OC_Group { * * removes the user from a group. */ - public static function removeFromGroup( $uid, $gid ) { - $run = true; - OC_Hook::emit( "OC_Group", "pre_removeFromGroup", array( "run" => &$run, "uid" => $uid, "gid" => $gid )); - - if($run) { - //remove the user from the all backends that have the group - foreach(self::$_usedBackends as $backend) { - if(!$backend->implementsActions(OC_GROUP_BACKEND_REMOVE_FROM_GOUP)) - continue; - - $backend->removeFromGroup($uid, $gid); - OC_Hook::emit( "OC_User", "post_removeFromGroup", array( "uid" => $uid, "gid" => $gid )); - } + public static function removeFromGroup($uid, $gid) { + $group = self::getManager()->get($gid); + $user = self::$userManager->get($uid); + if ($group and $user) { + OC_Hook::emit("OC_Group", "pre_removeFromGroup", array("run" => true, "uid" => $uid, "gid" => $gid)); + $group->removeUser($user); + OC_Hook::emit("OC_User", "post_removeFromGroup", array("uid" => $uid, "gid" => $gid)); return true; - }else{ + } else { return false; } } @@ -220,13 +184,18 @@ class OC_Group { * This function fetches all groups a user belongs to. It does not check * if the user exists at all. */ - public static function getUserGroups( $uid ) { - $groups=array(); - foreach(self::$_usedBackends as $backend) { - $groups=array_merge($backend->getUserGroups($uid), $groups); + public static function getUserGroups($uid) { + $user = self::$userManager->get($uid); + if ($user) { + $groups = self::getManager()->getUserGroups($user); + $groupIds = array(); + foreach ($groups as $group) { + $groupIds[] = $group->getGID(); + } + return $groupIds; + } else { + return array(); } - asort($groups); - return $groups; } /** @@ -235,27 +204,23 @@ class OC_Group { * * Returns a list with all groups */ - public static function getGroups($search = '', $limit = -1, $offset = 0) { - $groups = array(); - foreach (self::$_usedBackends as $backend) { - $groups = array_merge($backend->getGroups($search, $limit, $offset), $groups); + public static function getGroups($search = '', $limit = null, $offset = null) { + $groups = self::getManager()->search($search, $limit, $offset); + $groupIds = array(); + foreach ($groups as $group) { + $groupIds[] = $group->getGID(); } - asort($groups); - return $groups; + return $groupIds; } /** * check if a group exists + * * @param string $gid * @return bool */ public static function groupExists($gid) { - foreach(self::$_usedBackends as $backend) { - if ($backend->groupExists($gid)) { - return true; - } - } - return false; + return self::getManager()->exists($gid); } /** @@ -263,11 +228,17 @@ class OC_Group { * @returns array with user ids */ public static function usersInGroup($gid, $search = '', $limit = -1, $offset = 0) { - $users=array(); - foreach(self::$_usedBackends as $backend) { - $users = array_merge($backend->usersInGroup($gid, $search, $limit, $offset), $users); + $group = self::getManager()->get($gid); + if ($group) { + $users = $group->searchUsers($search . $limit, $offset); + $userIds = array(); + foreach ($users as $user) { + $userIds[] = $user->getUID(); + } + return $userIds; + } else { + return array(); } - return $users; } /** @@ -292,17 +263,17 @@ class OC_Group { * @returns array with display names (value) and user ids(key) */ public static function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) { - $displayNames=array(); - foreach(self::$_usedBackends as $backend) { - if($backend->implementsActions(OC_GROUP_BACKEND_GET_DISPLAYNAME)) { - $displayNames = array_merge($backend->displayNamesInGroup($gid, $search, $limit, $offset), $displayNames); - } else { - $users = $backend->usersInGroup($gid, $search, $limit, $offset); - $names = array_combine($users, $users); - $displayNames = array_merge($names, $displayNames); + $group = self::getManager()->get($gid); + if ($group) { + $users = $group->searchDisplayName($search . $limit, $offset); + $displayNames = array(); + foreach ($users as $user) { + $displayNames[] = $user->getDisplayName(); } + return $displayNames; + } else { + return array(); } - return $displayNames; } /** diff --git a/tests/lib/group.php b/tests/lib/group.php index 9128bd7ddce..d2c9ce46148 100644 --- a/tests/lib/group.php +++ b/tests/lib/group.php @@ -1,56 +1,61 @@ . -* -*/ + * ownCloud + * + * @author Robin Appelman + * @author Bernhard Posselt + * @copyright 2012 Robin Appelman icewind@owncloud.com + * @copyright 2012 Bernhard Posselt nukeawhale@gmail.com + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE + * License as published by the Free Software Foundation; either + * version 3 of the License, or any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU AFFERO GENERAL PUBLIC LICENSE for more details. + * + * You should have received a copy of the GNU Affero General Public + * License along with this library. If not, see . + * + */ class Test_Group extends PHPUnit_Framework_TestCase { function setUp() { OC_Group::clearBackends(); + OC_User::clearBackends(); } function testSingleBackend() { + $userBackend = new \OC_User_Dummy(); + \OC_User::getManager()->registerBackend($userBackend); OC_Group::useBackend(new OC_Group_Dummy()); - - $group1=uniqid(); - $group2=uniqid(); + + $group1 = uniqid(); + $group2 = uniqid(); OC_Group::createGroup($group1); OC_Group::createGroup($group2); - $user1=uniqid(); - $user2=uniqid(); + $user1 = uniqid(); + $user2 = uniqid(); + $userBackend->createUser($user1, ''); + $userBackend->createUser($user2, ''); $this->assertFalse(OC_Group::inGroup($user1, $group1)); $this->assertFalse(OC_Group::inGroup($user2, $group1)); $this->assertFalse(OC_Group::inGroup($user1, $group2)); $this->assertFalse(OC_Group::inGroup($user2, $group2)); - $this->assertTrue((bool)OC_Group::addToGroup($user1, $group1)); + $this->assertTrue(OC_Group::addToGroup($user1, $group1)); $this->assertTrue(OC_Group::inGroup($user1, $group1)); $this->assertFalse(OC_Group::inGroup($user2, $group1)); $this->assertFalse(OC_Group::inGroup($user1, $group2)); $this->assertFalse(OC_Group::inGroup($user2, $group2)); - $this->assertFalse((bool)OC_Group::addToGroup($user1, $group1)); + $this->assertTrue(OC_Group::addToGroup($user1, $group1)); $this->assertEquals(array($user1), OC_Group::usersInGroup($group1)); $this->assertEquals(array(), OC_Group::usersInGroup($group2)); @@ -65,37 +70,37 @@ class Test_Group extends PHPUnit_Framework_TestCase { } - public function testNoEmptyGIDs(){ + public function testNoEmptyGIDs() { OC_Group::useBackend(new OC_Group_Dummy()); $emptyGroup = null; - $this->assertEquals(false, OC_Group::createGroup($emptyGroup)); + $this->assertFalse(OC_Group::createGroup($emptyGroup)); } - public function testNoGroupsTwice(){ + public function testNoGroupsTwice() { OC_Group::useBackend(new OC_Group_Dummy()); $group = uniqid(); OC_Group::createGroup($group); $groupCopy = $group; - $this->assertEquals(false, OC_Group::createGroup($groupCopy)); + OC_Group::createGroup($groupCopy); $this->assertEquals(array($group), OC_Group::getGroups()); } - public function testDontDeleteAdminGroup(){ + public function testDontDeleteAdminGroup() { OC_Group::useBackend(new OC_Group_Dummy()); $adminGroup = 'admin'; OC_Group::createGroup($adminGroup); - $this->assertEquals(false, OC_Group::deleteGroup($adminGroup)); + $this->assertFalse(OC_Group::deleteGroup($adminGroup)); $this->assertEquals(array($adminGroup), OC_Group::getGroups()); } - public function testDontAddUserToNonexistentGroup(){ + public function testDontAddUserToNonexistentGroup() { OC_Group::useBackend(new OC_Group_Dummy()); $groupNonExistent = 'notExistent'; $user = uniqid(); @@ -105,8 +110,11 @@ class Test_Group extends PHPUnit_Framework_TestCase { } - public function testUsersInGroup(){ + public function testUsersInGroup() { OC_Group::useBackend(new OC_Group_Dummy()); + $userBackend = new \OC_User_Dummy(); + \OC_User::getManager()->registerBackend($userBackend); + $group1 = uniqid(); $group2 = uniqid(); $group3 = uniqid(); @@ -117,27 +125,32 @@ class Test_Group extends PHPUnit_Framework_TestCase { OC_Group::createGroup($group2); OC_Group::createGroup($group3); + $userBackend->createUser($user1, ''); + $userBackend->createUser($user2, ''); + $userBackend->createUser($user3, ''); + OC_Group::addToGroup($user1, $group1); OC_Group::addToGroup($user2, $group1); OC_Group::addToGroup($user3, $group1); OC_Group::addToGroup($user3, $group2); $this->assertEquals(array($user1, $user2, $user3), - OC_Group::usersInGroups(array($group1, $group2, $group3))); + OC_Group::usersInGroups(array($group1, $group2, $group3))); // FIXME: needs more parameter variation } - function testMultiBackend() { - $backend1=new OC_Group_Dummy(); - $backend2=new OC_Group_Dummy(); + $userBackend = new \OC_User_Dummy(); + \OC_User::getManager()->registerBackend($userBackend); + $backend1 = new OC_Group_Dummy(); + $backend2 = new OC_Group_Dummy(); OC_Group::useBackend($backend1); OC_Group::useBackend($backend2); - $group1=uniqid(); - $group2=uniqid(); + $group1 = uniqid(); + $group2 = uniqid(); OC_Group::createGroup($group1); //groups should be added to the first registered backend @@ -154,20 +167,23 @@ class Test_Group extends PHPUnit_Framework_TestCase { $this->assertTrue(OC_Group::groupExists($group1)); $this->assertTrue(OC_Group::groupExists($group2)); - $user1=uniqid(); - $user2=uniqid(); + $user1 = uniqid(); + $user2 = uniqid(); + + $userBackend->createUser($user1, ''); + $userBackend->createUser($user2, ''); $this->assertFalse(OC_Group::inGroup($user1, $group1)); $this->assertFalse(OC_Group::inGroup($user2, $group1)); - $this->assertTrue((bool)OC_Group::addToGroup($user1, $group1)); + $this->assertTrue(OC_Group::addToGroup($user1, $group1)); $this->assertTrue(OC_Group::inGroup($user1, $group1)); $this->assertFalse(OC_Group::inGroup($user2, $group1)); $this->assertFalse($backend2->inGroup($user1, $group1)); - $this->assertFalse((bool)OC_Group::addToGroup($user1, $group1)); + OC_Group::addToGroup($user1, $group1); $this->assertEquals(array($user1), OC_Group::usersInGroup($group1)); -- cgit v1.2.3 From 2d9be541eacf2dbde43a57ee14e2a0eba9597a47 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Wed, 10 Jul 2013 02:00:14 +0200 Subject: add test case for getting group after deleting it --- tests/lib/group/manager.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'tests') diff --git a/tests/lib/group/manager.php b/tests/lib/group/manager.php index 18f78c21a12..8a1db4592a4 100644 --- a/tests/lib/group/manager.php +++ b/tests/lib/group/manager.php @@ -64,6 +64,22 @@ class Manager extends \PHPUnit_Framework_TestCase { $this->assertNull($manager->get('group1')); } + public function testGetDeleted() { + $backend = new \OC_Group_Dummy(); + $backend->createGroup('group1'); + + /** + * @var \OC\User\Manager $userManager + */ + $userManager = $this->getMock('\OC\User\Manager'); + $manager = new \OC\Group\Manager($userManager); + $manager->addBackend($backend); + + $group = $manager->get('group1'); + $group->delete(); + $this->assertNull($manager->get('group1')); + } + public function testGetMultipleBackends() { /** * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend1 -- cgit v1.2.3 From 065bc96b02c6da881a08335f0b6b818350e9fcb8 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Wed, 10 Jul 2013 02:17:24 +0200 Subject: more consistent naming --- lib/group.php | 4 ++-- lib/group/manager.php | 8 ++++---- tests/lib/group/manager.php | 4 ++-- 3 files changed, 8 insertions(+), 8 deletions(-) (limited to 'tests') diff --git a/lib/group.php b/lib/group.php index 4b57073a123..8fbf5f86418 100644 --- a/lib/group.php +++ b/lib/group.php @@ -84,7 +84,7 @@ class OC_Group { public static function createGroup($gid) { OC_Hook::emit("OC_Group", "pre_createGroup", array("run" => true, "gid" => $gid)); - if (self::getManager()->create($gid)) { + if (self::getManager()->createGroup($gid)) { OC_Hook::emit("OC_User", "post_createGroup", array("gid" => $gid)); return true; } else { @@ -220,7 +220,7 @@ class OC_Group { * @return bool */ public static function groupExists($gid) { - return self::getManager()->exists($gid); + return self::getManager()->groupExists($gid); } /** diff --git a/lib/group/manager.php b/lib/group/manager.php index 0599015780f..7efcff0ade0 100644 --- a/lib/group/manager.php +++ b/lib/group/manager.php @@ -93,7 +93,7 @@ class Manager extends PublicEmitter { * @param string $gid * @return bool */ - public function exists($gid) { + public function groupExists($gid) { return !is_null($this->get($gid)); } @@ -101,10 +101,10 @@ class Manager extends PublicEmitter { * @param string $gid * @return \OC\Group\Group */ - public function create($gid) { + public function createGroup($gid) { if (!$gid) { return false; - } else if ($this->exists($gid)) { + } else if ($this->groupExists($gid)) { return $this->get($gid); } else { $this->emit('\OC\Group', 'preCreate', array($gid)); @@ -126,7 +126,7 @@ class Manager extends PublicEmitter { * @param int $offset * @return \OC\Group\Group[] */ - public function search($search = '', $limit = null, $offset = null) { + public function search($search, $limit = null, $offset = null) { $groups = array(); foreach ($this->backends as $backend) { $groupIds = $backend->getGroups($search, $limit, $offset); diff --git a/tests/lib/group/manager.php b/tests/lib/group/manager.php index 8a1db4592a4..2ecaefc1406 100644 --- a/tests/lib/group/manager.php +++ b/tests/lib/group/manager.php @@ -134,7 +134,7 @@ class Manager extends \PHPUnit_Framework_TestCase { $manager = new \OC\Group\Manager($userManager); $manager->addBackend($backend); - $group = $manager->create('group1'); + $group = $manager->createGroup('group1'); $this->assertEquals('group1', $group->getGID()); } @@ -157,7 +157,7 @@ class Manager extends \PHPUnit_Framework_TestCase { $manager = new \OC\Group\Manager($userManager); $manager->addBackend($backend); - $group = $manager->create('group1'); + $group = $manager->createGroup('group1'); $this->assertEquals('group1', $group->getGID()); } -- cgit v1.2.3 From 7ba4269c263a6e102e5efeefdce94909f9a59aab Mon Sep 17 00:00:00 2001 From: Thomas Mueller Date: Mon, 15 Jul 2013 10:28:14 +0200 Subject: <<config->setValue('foo', 'moo'); $this->assertAttributeEquals(array('foo' => 'moo'), 'cache', $this->config); $content = file_get_contents(self::CONFIG_FILE); - $this->assertEquals(<< 'moo', -); -EOL - , $content); + $expected = " '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); - $this->assertEquals(<< 'moo', - 'bar' => 'red', -); -EOL - , $content); + $expected = " 'moo',\n 'bar' => 'red',\n);\n"; + $this->assertEquals($expected, $content); } public function testDeleteKey() { @@ -70,13 +59,9 @@ EOL $this->config->deleteKey('foo'); $this->assertAttributeEquals(array(), 'cache', $this->config); $content = file_get_contents(self::CONFIG_FILE); - $this->assertEquals(<<assertEquals($expected, $content); } public function testSavingDebugMode() { @@ -85,14 +70,9 @@ EOL $this->assertAttributeEquals(array(), 'cache', $this->config); $this->assertAttributeEquals(true, 'debugMode', $this->config); $content = file_get_contents(self::CONFIG_FILE); - $this->assertEquals(<<assertEquals($expected, $content); } /** -- cgit v1.2.3 From 8ad148feaf975481815b3f2413fc1fa34b3e8be7 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 16 Jul 2013 15:46:27 +0200 Subject: memcache: some additional unit tests --- tests/lib/memcache/apc.php | 31 ++++++++++--------------------- tests/lib/memcache/cache.php | 36 ++++++++++++++++++++++++++++++++++++ tests/lib/memcache/memcached.php | 7 +++++-- tests/lib/memcache/xcache.php | 29 +++++++++-------------------- 4 files changed, 60 insertions(+), 43 deletions(-) create mode 100644 tests/lib/memcache/cache.php (limited to 'tests') diff --git a/tests/lib/memcache/apc.php b/tests/lib/memcache/apc.php index e3dccc09669..6b2a49470ba 100644 --- a/tests/lib/memcache/apc.php +++ b/tests/lib/memcache/apc.php @@ -1,31 +1,20 @@ . -* -*/ + * Copyright (c) 2013 Robin Appelman + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\Memcache; -class Test_Memcache_APC extends Test_Cache { +class APC extends Cache { public function setUp() { if(!\OC\Memcache\APC::isAvailable()) { $this->markTestSkipped('The apc extension is not available.'); return; } - $this->instance=new \OC\Memcache\APC(); + $this->instance=new \OC\Memcache\APC(uniqid()); } } diff --git a/tests/lib/memcache/cache.php b/tests/lib/memcache/cache.php new file mode 100644 index 00000000000..2c1dbc9d2f7 --- /dev/null +++ b/tests/lib/memcache/cache.php @@ -0,0 +1,36 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\Memcache; + +class Cache extends \Test_Cache { + public function testExistsAfterSet() { + $this->assertFalse($this->instance->hasKey('foo')); + $this->instance->set('foo', 'bar'); + $this->assertTrue($this->instance->hasKey('foo')); + } + + public function testGetAfterSet() { + $this->assertNull($this->instance->get('foo')); + $this->instance->set('foo', 'bar'); + $this->assertEquals('bar', $this->instance->get('foo')); + } + + public function testDoesNotExistAfterRemove() { + $this->instance->set('foo', 'bar'); + $this->instance->remove('foo'); + $this->assertFalse($this->instance->hasKey('foo')); + } + + public function tearDown() { + if ($this->instance) { + $this->instance->clear(); + } + } +} diff --git a/tests/lib/memcache/memcached.php b/tests/lib/memcache/memcached.php index a0be047ed1f..4b38ae8ef3c 100644 --- a/tests/lib/memcache/memcached.php +++ b/tests/lib/memcache/memcached.php @@ -1,4 +1,5 @@ * This file is licensed under the Affero General Public License version 3 or @@ -6,12 +7,14 @@ * See the COPYING-README file. */ -class Test_Memcache_Memcached extends Test_Cache { +namespace Test\Memcache; + +class Memcached extends Cache { public function setUp() { if (!\OC\Memcache\Memcached::isAvailable()) { $this->markTestSkipped('The memcached extension is not available.'); return; } - $this->instance = new \OC\Memcache\Memcached(); + $this->instance = new \OC\Memcache\Memcached(uniqid()); } } diff --git a/tests/lib/memcache/xcache.php b/tests/lib/memcache/xcache.php index 48773533c89..f59afda3966 100644 --- a/tests/lib/memcache/xcache.php +++ b/tests/lib/memcache/xcache.php @@ -1,31 +1,20 @@ . - * + * Copyright (c) 2013 Robin Appelman + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. */ -class Test_Memcache_XCache extends Test_Cache { +namespace Test\Memcache; + +class XCache extends Cache { public function setUp() { if (!\OC\Memcache\XCache::isAvailable()) { $this->markTestSkipped('The xcache extension is not available.'); return; } - $this->instance = new \OC\Memcache\XCache(); + $this->instance = new \OC\Memcache\XCache(uniqid()); } } -- cgit v1.2.3 From 504089940de88220a425db21e8e133582fe15c30 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 16 Jul 2013 16:06:00 +0200 Subject: mamcache: implement the ArrayAccess interface --- lib/memcache/cache.php | 20 +++++++++++++++++++- tests/lib/memcache/cache.php | 22 ++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/lib/memcache/cache.php b/lib/memcache/cache.php index 9db69ae4104..0ad1cc7ec03 100644 --- a/lib/memcache/cache.php +++ b/lib/memcache/cache.php @@ -8,7 +8,7 @@ namespace OC\Memcache; -abstract class Cache { +abstract class Cache implements \ArrayAccess { /** * @var string $prefix */ @@ -56,4 +56,22 @@ abstract class Cache { * @return mixed */ abstract public function clear($prefix = ''); + + //implement the ArrayAccess interface + + public function offsetExists($offset) { + return $this->hasKey($offset); + } + + public function offsetSet($offset, $value) { + $this->set($offset, $value); + } + + public function offsetGet($offset) { + return $this->get($offset); + } + + public function offsetUnset($offset) { + $this->remove($offset); + } } diff --git a/tests/lib/memcache/cache.php b/tests/lib/memcache/cache.php index 2c1dbc9d2f7..e2643b9fcd9 100644 --- a/tests/lib/memcache/cache.php +++ b/tests/lib/memcache/cache.php @@ -28,6 +28,28 @@ class Cache extends \Test_Cache { $this->assertFalse($this->instance->hasKey('foo')); } + public function testArrayAccessSet() { + $this->instance['foo'] = 'bar'; + $this->assertEquals('bar', $this->instance->get('foo')); + } + + public function testArrayAccessGet() { + $this->instance->set('foo', 'bar'); + $this->assertEquals('bar', $this->instance['foo']); + } + + public function testArrayAccessExists() { + $this->assertFalse(isset($this->instance['foo'])); + $this->instance->set('foo', 'bar'); + $this->assertTrue(isset($this->instance['foo'])); + } + + public function testArrayAccessUnset() { + $this->instance->set('foo', 'bar'); + unset($this->instance['foo']); + $this->assertFalse($this->instance->hasKey('foo')); + } + public function tearDown() { if ($this->instance) { $this->instance->clear(); -- cgit v1.2.3 From 97f0bc1c4aef3f46b7928e8c2a547c0a35007687 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 16 Jul 2013 23:04:07 +0200 Subject: Storage: remove tests for search --- tests/lib/files/storage/storage.php | 30 +----------------------------- 1 file changed, 1 insertion(+), 29 deletions(-) (limited to 'tests') diff --git a/tests/lib/files/storage/storage.php b/tests/lib/files/storage/storage.php index fb3e05e66b3..dc2de18be17 100644 --- a/tests/lib/files/storage/storage.php +++ b/tests/lib/files/storage/storage.php @@ -176,7 +176,7 @@ abstract class Storage extends \PHPUnit_Framework_TestCase { $this->assertEquals(filesize($textFile), $this->instance->filesize('/lorem.txt')); $stat = $this->instance->stat('/lorem.txt'); - //only size and mtime are requered in the result + //only size and mtime are required in the result $this->assertEquals($stat['size'], $this->instance->filesize('/lorem.txt')); $this->assertEquals($stat['mtime'], $mTime); @@ -210,34 +210,6 @@ abstract class Storage extends \PHPUnit_Framework_TestCase { $this->assertTrue($this->instance->hasUpdated('/', $mtimeStart - 1)); } - public function testSearch() { - $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt'; - $this->instance->file_put_contents('/lorem.txt', file_get_contents($textFile, 'r')); - $pngFile = \OC::$SERVERROOT . '/tests/data/logo-wide.png'; - $this->instance->file_put_contents('/logo-wide.png', file_get_contents($pngFile, 'r')); - $svgFile = \OC::$SERVERROOT . '/tests/data/logo-wide.svg'; - $this->instance->file_put_contents('/logo-wide.svg', file_get_contents($svgFile, 'r')); - $result = $this->instance->search('logo'); - $this->assertEquals(2, count($result)); - $this->assertContains('/logo-wide.svg', $result); - $this->assertContains('/logo-wide.png', $result); - } - - public function testSearchInSubFolder() { - $this->instance->mkdir('sub'); - $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt'; - $this->instance->file_put_contents('/sub/lorem.txt', file_get_contents($textFile, 'r')); - $pngFile = \OC::$SERVERROOT . '/tests/data/logo-wide.png'; - $this->instance->file_put_contents('/sub/logo-wide.png', file_get_contents($pngFile, 'r')); - $svgFile = \OC::$SERVERROOT . '/tests/data/logo-wide.svg'; - $this->instance->file_put_contents('/sub/logo-wide.svg', file_get_contents($svgFile, 'r')); - - $result = $this->instance->search('logo'); - $this->assertEquals(2, count($result)); - $this->assertContains('/sub/logo-wide.svg', $result); - $this->assertContains('/sub/logo-wide.png', $result); - } - public function testFOpen() { $textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt'; -- cgit v1.2.3 From cfac7fcd53974ddf20ddb75a0cb3b5a73047568a Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 16 Jul 2013 23:07:35 +0200 Subject: Storage: remove some unneeded strict tests of mtime behaviour --- tests/lib/files/storage/storage.php | 23 ++--------------------- 1 file changed, 2 insertions(+), 21 deletions(-) (limited to 'tests') diff --git a/tests/lib/files/storage/storage.php b/tests/lib/files/storage/storage.php index dc2de18be17..46818c3b4a1 100644 --- a/tests/lib/files/storage/storage.php +++ b/tests/lib/files/storage/storage.php @@ -180,31 +180,12 @@ abstract class Storage extends \PHPUnit_Framework_TestCase { $this->assertEquals($stat['size'], $this->instance->filesize('/lorem.txt')); $this->assertEquals($stat['mtime'], $mTime); - $mtimeStart = time(); - $supportsTouch = $this->instance->touch('/lorem.txt'); - $mtimeEnd = time(); - if ($supportsTouch !== false) { + if ($this->instance->touch('/lorem.txt', 100) !== false) { $mTime = $this->instance->filemtime('/lorem.txt'); - $this->assertTrue(($mtimeStart - 1) <= $mTime); - $this->assertTrue($mTime <= ($mtimeEnd + 1)); - - $this->assertTrue($this->instance->hasUpdated('/lorem.txt', $mtimeStart - 1)); - - if ($this->instance->touch('/lorem.txt', 100) !== false) { - $mTime = $this->instance->filemtime('/lorem.txt'); - $this->assertEquals($mTime, 100); - } + $this->assertEquals($mTime, 100); } $mtimeStart = time(); - $fh = $this->instance->fopen('/lorem.txt', 'a'); - fwrite($fh, ' '); - fclose($fh); - clearstatcache(); - $mtimeEnd = time(); - $mTime = $this->instance->filemtime('/lorem.txt'); - $this->assertTrue(($mtimeStart - 1) <= $mTime); - $this->assertTrue($mTime <= ($mtimeEnd + 1)); $this->instance->unlink('/lorem.txt'); $this->assertTrue($this->instance->hasUpdated('/', $mtimeStart - 1)); -- cgit v1.2.3 From b2bcc9774bb3c7857a99dc81116e0d949962657e Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 16 Jul 2013 23:11:22 +0200 Subject: memcache: make base testcase abstract --- tests/lib/memcache/cache.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/lib/memcache/cache.php b/tests/lib/memcache/cache.php index e2643b9fcd9..d07c492cef0 100644 --- a/tests/lib/memcache/cache.php +++ b/tests/lib/memcache/cache.php @@ -9,7 +9,7 @@ namespace Test\Memcache; -class Cache extends \Test_Cache { +abstract class Cache extends \Test_Cache { public function testExistsAfterSet() { $this->assertFalse($this->instance->hasKey('foo')); $this->instance->set('foo', 'bar'); -- cgit v1.2.3 From e3ea3ed3c563fce2b5fe1addfe199e3aaec7abdb Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Thu, 18 Jul 2013 12:15:34 +0200 Subject: group: only pass backends that hold that specific group to the group constructor --- lib/group/group.php | 2 +- lib/group/manager.php | 8 +++++++- tests/lib/group/group.php | 4 ---- tests/lib/group/manager.php | 10 +++++----- 4 files changed, 13 insertions(+), 11 deletions(-) (limited to 'tests') diff --git a/lib/group/group.php b/lib/group/group.php index d4036b4361b..7a639313247 100644 --- a/lib/group/group.php +++ b/lib/group/group.php @@ -225,7 +225,7 @@ class Group { $this->emitter->emit('\OC\Group', 'preDelete', array($this)); } foreach ($this->backends as $backend) { - if ($backend->implementsActions(OC_GROUP_BACKEND_DELETE_GROUP) and $backend->groupExists($this->gid)) { + if ($backend->implementsActions(OC_GROUP_BACKEND_DELETE_GROUP)) { $result = true; $backend->deleteGroup($this->gid); } diff --git a/lib/group/manager.php b/lib/group/manager.php index 7efcff0ade0..bf469d51d12 100644 --- a/lib/group/manager.php +++ b/lib/group/manager.php @@ -85,7 +85,13 @@ class Manager extends PublicEmitter { } protected function getGroupObject($gid) { - $this->cachedGroups[$gid] = new Group($gid, $this->backends, $this->userManager, $this); + $backends = array(); + foreach ($this->backends as $backend) { + if ($backend->groupExists($gid)) { + $backends[] = $backend; + } + } + $this->cachedGroups[$gid] = new Group($gid, $backends, $this->userManager, $this); return $this->cachedGroups[$gid]; } diff --git a/tests/lib/group/group.php b/tests/lib/group/group.php index 116aefda81c..75e975d9e65 100644 --- a/tests/lib/group/group.php +++ b/tests/lib/group/group.php @@ -307,10 +307,6 @@ class Group extends \PHPUnit_Framework_TestCase { $backend->expects($this->once()) ->method('deleteGroup') ->with('group1'); - $backend->expects($this->once()) - ->method('groupExists') - ->with('group1') - ->will($this->returnValue(true)); $backend->expects($this->any()) ->method('implementsActions') ->will($this->returnValue(true)); diff --git a/tests/lib/group/manager.php b/tests/lib/group/manager.php index 2ecaefc1406..9d3adf51a0c 100644 --- a/tests/lib/group/manager.php +++ b/tests/lib/group/manager.php @@ -17,7 +17,7 @@ class Manager extends \PHPUnit_Framework_TestCase { * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend */ $backend = $this->getMock('\OC_Group_Database'); - $backend->expects($this->once()) + $backend->expects($this->any()) ->method('groupExists') ->with('group1') ->will($this->returnValue(true)); @@ -85,7 +85,7 @@ class Manager extends \PHPUnit_Framework_TestCase { * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend1 */ $backend1 = $this->getMock('\OC_Group_Database'); - $backend1->expects($this->once()) + $backend1->expects($this->any()) ->method('groupExists') ->with('group1') ->will($this->returnValue(false)); @@ -94,7 +94,7 @@ class Manager extends \PHPUnit_Framework_TestCase { * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend2 */ $backend2 = $this->getMock('\OC_Group_Database'); - $backend2->expects($this->once()) + $backend2->expects($this->any()) ->method('groupExists') ->with('group1') ->will($this->returnValue(true)); @@ -117,7 +117,7 @@ class Manager extends \PHPUnit_Framework_TestCase { * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend */ $backend = $this->getMock('\OC_Group_Database'); - $backend->expects($this->once()) + $backend->expects($this->any()) ->method('groupExists') ->with('group1') ->will($this->returnValue(false)); @@ -143,7 +143,7 @@ class Manager extends \PHPUnit_Framework_TestCase { * @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend */ $backend = $this->getMock('\OC_Group_Database'); - $backend->expects($this->once()) + $backend->expects($this->any()) ->method('groupExists') ->with('group1') ->will($this->returnValue(true)); -- cgit v1.2.3 From 8f93490ac45fbd74bd0e9697a685fd43bb34239b Mon Sep 17 00:00:00 2001 From: Morris Jobke Date: Sun, 21 Jul 2013 11:13:29 +0200 Subject: fix failing master branch - Test_Config::testWriteData --- lib/config.php | 6 +++++- tests/lib/config.php | 11 +++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/lib/config.php b/lib/config.php index 00d9f5b4247..a38ce19c74f 100644 --- a/lib/config.php +++ b/lib/config.php @@ -144,7 +144,11 @@ class Config { continue; } unset($CONFIG); - include $file; + if((@include $file) === false) + { + throw new HintException("Can't read from config file '" . $file . "'. ". + 'This is usually caused by the wrong file permission.'); + } if (isset($CONFIG) && is_array($CONFIG)) { $this->cache = array_merge($this->cache, $CONFIG); } diff --git a/tests/lib/config.php b/tests/lib/config.php index c67a66c832e..1a1d062d688 100644 --- a/tests/lib/config.php +++ b/tests/lib/config.php @@ -80,6 +80,17 @@ class Test_Config extends PHPUnit_Framework_TestCase { */ 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'); } } -- cgit v1.2.3 From 5965f3ecead424bb969261a33279dd592e5b799e Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Sat, 16 Mar 2013 23:02:51 +0100 Subject: Split locating JS and CSS files to their own class --- lib/template/cssresourcelocator.php | 43 +++++++++++++ lib/template/jsresourcelocator.php | 43 +++++++++++++ lib/template/resourcelocator.php | 70 ++++++++++++++++++++ lib/templatelayout.php | 114 +++------------------------------ tests/lib/template/resourcelocator.php | 69 ++++++++++++++++++++ 5 files changed, 235 insertions(+), 104 deletions(-) create mode 100644 lib/template/cssresourcelocator.php create mode 100644 lib/template/jsresourcelocator.php create mode 100644 lib/template/resourcelocator.php create mode 100644 tests/lib/template/resourcelocator.php (limited to 'tests') diff --git a/lib/template/cssresourcelocator.php b/lib/template/cssresourcelocator.php new file mode 100644 index 00000000000..e27296d9f5f --- /dev/null +++ b/lib/template/cssresourcelocator.php @@ -0,0 +1,43 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Template; + +class CSSResourceLocator extends ResourceLocator { + public function doFind( $style ) { + if (strpos($style, '3rdparty') === 0 + && $this->appendIfExist($this->thirdpartyroot, $style.'.css') + || $this->appendIfExist($this->serverroot, $style.$this->form_factor.'.css') + || $this->appendIfExist($this->serverroot, $style.'.css') + || $this->appendIfExist($this->serverroot, 'core/'.$style.$this->form_factor.'.css') + || $this->appendIfExist($this->serverroot, 'core/'.$style.'.css') + ) { + return; + } + $app = substr($style, 0, strpos($style, '/')); + $style = substr($style, strpos($style, '/')+1); + $app_path = OC_App::getAppPath($app); + $app_url = $this->webroot . '/index.php/apps/' . $app; + if ($this->appendIfExist($app_path, $style.$this->form_factor.'.css', $app_url) + || $this->appendIfExist($app_path, $style.'.css', $ap_url) + ) { + return; + } + throw new \Exception('css file not found: style:'.$style); + } + + public function doFindTheme( $style ) { + $theme_dir = 'themes/'.$this->theme.'/'; + $this->appendIfExist($this->serverroot, $theme_dir.'apps/'.$style.$this->form_factor.'.css') + || $this->appendIfExist($this->serverroot, $theme_dir.'apps/'.$style.'.css') + || $this->appendIfExist($this->serverroot, $theme_dir.$style.$this->form_factor.'.css') + || $this->appendIfExist($this->serverroot, $theme_dir.$style.'.css') + || $this->appendIfExist($this->serverroot, $theme_dir.'core/'.$style.$this->form_factor.'.css') + || $this->appendIfExist($this->serverroot, $theme_dir.'core/'.$style.'.css'); + } +} diff --git a/lib/template/jsresourcelocator.php b/lib/template/jsresourcelocator.php new file mode 100644 index 00000000000..a1382edf2d5 --- /dev/null +++ b/lib/template/jsresourcelocator.php @@ -0,0 +1,43 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Template; + +class JSResourceLocator extends ResourceLocator { + public function doFind( $script ) { + $theme_dir = 'themes/'.$this->theme.'/'; + if (strpos($script, '3rdparty') === 0 + && $this->appendIfExist($this->thirdpartyroot, $script.'.js') + || $this->appendIfExist($this->serverroot, $theme_dir.'apps/'.$script.$this->form_factor.'.js') + || $this->appendIfExist($this->serverroot, $theme_dir.'apps/'.$script.'.js') + || $this->appendIfExist($this->serverroot, $theme_dir.$script.$this->form_factor.'.js') + || $this->appendIfExist($this->serverroot, $theme_dir.$script.'.js') + || $this->appendIfExist($this->serverroot, $script.$this->form_factor.'.js') + || $this->appendIfExist($this->serverroot, $script.'.js') + || $this->appendIfExist($this->serverroot, $theme_dir.'core/'.$script.$this->form_factor.'.js') + || $this->appendIfExist($this->serverroot, $theme_dir.'core/'.$script.'.js') + || $this->appendIfExist($this->serverroot, 'core/'.$script.$this->form_factor.'.js') + || $this->appendIfExist($this->serverroot, 'core/'.$script.'.js') + ) { + return; + } + $app = substr($script, 0, strpos($script, '/')); + $script = substr($script, strpos($script, '/')+1); + $app_path = OC_App::getAppPath($app); + $app_url = OC_App::getAppWebPath($app); + if ($this->appendIfExist($app_path, $script.$this->form_factor.'.js', $app_url) + || $this->appendIfExist($app_path, $script.'.js', $ap_url) + ) { + return; + } + throw new \Exception('js file not found: script:'.$script); + } + + public function doFindTheme( $script ) { + } +} diff --git a/lib/template/resourcelocator.php b/lib/template/resourcelocator.php new file mode 100644 index 00000000000..9f83673664d --- /dev/null +++ b/lib/template/resourcelocator.php @@ -0,0 +1,70 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Template; + +abstract class ResourceLocator { + protected $theme; + protected $form_factor; + + protected $mapping; + protected $serverroot; + protected $thirdpartyroot; + protected $webroot; + + protected $resources = array(); + + public function __construct( $theme, $form_factor, $core_map, $party_map ) { + $this->theme = $theme; + $this->form_factor = $form_factor; + $this->mapping = $core_map + $party_map; + $this->serverroot = key($core_map); + $this->thirdpartyroot = key($party_map); + $this->webroot = $this->mapping[$this->serverroot]; + } + + abstract public function doFind( $resource ); + abstract public function doFindTheme( $resource ); + + public function find( $resources ) { + try { + foreach($resources as $resource) { + $this->doFind($resource); + } + if (!empty($this->theme)) { + foreach($resources as $resource) { + $this->doFindTheme($resource); + } + } + } catch (\Exception $e) { + throw new \Exception($e->getMessage().' formfactor:'.$this->form_factor + .' serverroot:'.$this->serverroot); + } + } + + /* + * @brief append the $file resource if exist at $root + * @param $root path to check + * @param $file the filename + * @param $web base for path, default map $root to $webroot + */ + protected function appendIfExist($root, $file, $webroot = null) { + if (is_file($root.'/'.$file)) { + if (!$webroot) { + $webroot = $this->mapping[$root]; + } + $this->resources[] = array($root, $webroot, $file); + return true; + } + return false; + } + + public function getResources() { + return $this->resources; + } +} diff --git a/lib/templatelayout.php b/lib/templatelayout.php index 7115b8f0306..0024c9d4960 100644 --- a/lib/templatelayout.php +++ b/lib/templatelayout.php @@ -83,21 +83,6 @@ class OC_TemplateLayout extends OC_Template { } } - /* - * @brief append the $file-url if exist at $root - * @param $files array to append file info to - * @param $root path to check - * @param $web base for path - * @param $file the filename - */ - static public function appendIfExist(&$files, $root, $webroot, $file) { - if (is_file($root.'/'.$file)) { - $files[] = array($root, $webroot, $file); - return true; - } - return false; - } - static public function findStylesheetFiles($styles) { // Read the selected theme from the config file $theme = OC_Util::getTheme(); @@ -105,51 +90,11 @@ class OC_TemplateLayout extends OC_Template { // Read the detected formfactor and use the right file name. $fext = self::getFormFactorExtension(); - $files = array(); - foreach($styles as $style) { - // is it in 3rdparty? - if(strpos($style, '3rdparty') === 0 && - self::appendIfExist($files, OC::$THIRDPARTYROOT, OC::$THIRDPARTYWEBROOT, $style.'.css')) { - - // or in the owncloud root? - }elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "$style$fext.css" )) { - }elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "$style.css" )) { - - // or in core ? - }elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "core/$style$fext.css" )) { - }elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "core/$style.css" )) { - - }else{ - $app = substr($style, 0, strpos($style, '/')); - $style = substr($style, strpos($style, '/')+1); - $app_path = OC_App::getAppPath($app); - $app_url = OC::$WEBROOT . '/index.php/apps/' . $app; - if(self::appendIfExist($files, $app_path, $app_url, "$style$fext.css")) { - } - elseif(self::appendIfExist($files, $app_path, $app_url, "$style.css")) { - } - else { - echo('css file not found: style:'.$style.' formfactor:'.$fext - .' webroot:'.OC::$WEBROOT.' serverroot:'.OC::$SERVERROOT); - die(); - } - } - } - // Add the theme css files. you can override the default values here - if(!empty($theme)) { - foreach($styles as $style) { - if(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/apps/$style$fext.css" )) { - }elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/apps/$style.css" )) { - - }elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/$style$fext.css" )) { - }elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/$style.css" )) { - - }elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/core/$style$fext.css" )) { - }elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/core/$style.css" )) { - } - } - } - return $files; + $locator = new \OC\Template\CSSResourceLocator( $theme, $fext, + array( OC::$SERVERROOT => OC::$WEBROOT ), + array( OC::$THIRDPARTYROOT => OC::$THIRDPARTYWEBROOT )); + $locator->find($styles); + return $locator->getResources(); } static public function findJavascriptFiles($scripts) { @@ -159,49 +104,10 @@ class OC_TemplateLayout extends OC_Template { // Read the detected formfactor and use the right file name. $fext = self::getFormFactorExtension(); - $files = array(); - foreach($scripts as $script) { - // Is it in 3rd party? - if(strpos($script, '3rdparty') === 0 && - self::appendIfExist($files, OC::$THIRDPARTYROOT, OC::$THIRDPARTYWEBROOT, $script.'.js')) { - - // Is it in apps and overwritten by the theme? - }elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/apps/$script$fext.js" )) { - }elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/apps/$script.js" )) { - - // Is it in the owncloud root but overwritten by the theme? - }elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/$script$fext.js" )) { - }elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/$script.js" )) { - - // Is it in the owncloud root ? - }elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "$script$fext.js" )) { - }elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "$script.js" )) { - - // Is in core but overwritten by a theme? - }elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/core/$script$fext.js" )) { - }elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "themes/$theme/core/$script.js" )) { - - // Is it in core? - }elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "core/$script$fext.js" )) { - }elseif(self::appendIfExist($files, OC::$SERVERROOT, OC::$WEBROOT, "core/$script.js" )) { - - }else{ - // Is it part of an app? - $app = substr($script, 0, strpos($script, '/')); - $script = substr($script, strpos($script, '/')+1); - $app_path = OC_App::getAppPath($app); - $app_url = OC_App::getAppWebPath($app); - if(self::appendIfExist($files, $app_path, $app_url, "$script$fext.js")) { - } - elseif(self::appendIfExist($files, $app_path, $app_url, "$script.js")) { - } - else { - echo('js file not found: script:'.$script.' formfactor:'.$fext - .' webroot:'.OC::$WEBROOT.' serverroot:'.OC::$SERVERROOT); - die(); - } - } - } - return $files; + $locator = new \OC\Template\JSResourceLocator( $theme, $fext, + array( OC::$SERVERROOT => OC::$WEBROOT ), + array( OC::$THIRDPARTYROOT => OC::$THIRDPARTYWEBROOT )); + $locator->find($scripts); + return $locator->getResources(); } } diff --git a/tests/lib/template/resourcelocator.php b/tests/lib/template/resourcelocator.php new file mode 100644 index 00000000000..d80d222e2c9 --- /dev/null +++ b/tests/lib/template/resourcelocator.php @@ -0,0 +1,69 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +class Test_ResourceLocator extends PHPUnit_Framework_TestCase { + public function getResourceLocator( $theme, $form_factor, $core_map, $party_map, $appsroots ) { + return $this->getMockForAbstractClass('OC\Template\ResourceLocator', + array( $theme, $form_factor, $core_map, $party_map, $appsroots ), + '', true, true, true, array()); + } + + public function testConstructor() { + $locator = $this->getResourceLocator('theme', 'form_factor', + array('core'=>'map'), array('3rd'=>'party'), array('foo'=>'bar')); + $this->assertAttributeEquals('theme', 'theme', $locator); + $this->assertAttributeEquals('form_factor', 'form_factor', $locator); + $this->assertAttributeEquals('core', 'serverroot', $locator); + $this->assertAttributeEquals(array('core'=>'map','3rd'=>'party'), 'mapping', $locator); + $this->assertAttributeEquals('3rd', 'thirdpartyroot', $locator); + $this->assertAttributeEquals('map', 'webroot', $locator); + $this->assertAttributeEquals(array(), 'resources', $locator); + } + + public function testFind() { + $locator = $this->getResourceLocator('theme', 'form_factor', + array('core'=>'map'), array('3rd'=>'party'), array('foo'=>'bar')); + $locator->expects($this->once()) + ->method('doFind') + ->with('foo'); + $locator->expects($this->once()) + ->method('doFindTheme') + ->with('foo'); + $locator->find(array('foo')); + + $locator = $this->getResourceLocator('theme', 'form_factor', + array('core'=>'map'), array('3rd'=>'party'), array('foo'=>'bar')); + $locator->expects($this->once()) + ->method('doFind') + ->with('foo') + ->will($this->throwException(new Exception('test'))); + try { + $locator->find(array('foo')); + } catch (\Exception $e) { + $this->assertEquals('test formfactor:form_factor serverroot:core', $e->getMessage()); + } + } + + public function testAppendIfExist() { + $locator = $this->getResourceLocator('theme', 'form_factor', + array(__DIR__=>'map'), array('3rd'=>'party'), array('foo'=>'bar')); + $method = new ReflectionMethod($locator, 'appendIfExist'); + $method->setAccessible(true); + + $method->invoke($locator, __DIR__, basename(__FILE__), 'webroot'); + $resource1 = array(__DIR__, 'webroot', basename(__FILE__)); + $this->assertEquals(array($resource1), $locator->getResources()); + + $method->invoke($locator, __DIR__, basename(__FILE__)); + $resource2 = array(__DIR__, 'map', basename(__FILE__)); + $this->assertEquals(array($resource1, $resource2), $locator->getResources()); + + $method->invoke($locator, __DIR__, 'does-not-exist'); + $this->assertEquals(array($resource1, $resource2), $locator->getResources()); + } +} -- cgit v1.2.3 From 5caa7576d493ef6b639b59718701d67bf8db90cb Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Tue, 23 Jul 2013 17:36:08 +0200 Subject: Revert "fix failing master branch - Test_Config::testWriteData" This reverts commit 8f93490ac45fbd74bd0e9697a685fd43bb34239b. --- lib/config.php | 6 +----- tests/lib/config.php | 11 ----------- 2 files changed, 1 insertion(+), 16 deletions(-) (limited to 'tests') diff --git a/lib/config.php b/lib/config.php index a38ce19c74f..00d9f5b4247 100644 --- a/lib/config.php +++ b/lib/config.php @@ -144,11 +144,7 @@ class Config { continue; } unset($CONFIG); - if((@include $file) === false) - { - throw new HintException("Can't read from config file '" . $file . "'. ". - 'This is usually caused by the wrong file permission.'); - } + include $file; if (isset($CONFIG) && is_array($CONFIG)) { $this->cache = array_merge($this->cache, $CONFIG); } diff --git a/tests/lib/config.php b/tests/lib/config.php index 1a1d062d688..c67a66c832e 100644 --- a/tests/lib/config.php +++ b/tests/lib/config.php @@ -80,17 +80,6 @@ class Test_Config extends PHPUnit_Framework_TestCase { */ 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'); } } -- cgit v1.2.3 From 25003fb21380b71f7c098b8e27263031103e1655 Mon Sep 17 00:00:00 2001 From: Bart Visscher Date: Wed, 24 Jul 2013 21:50:15 +0200 Subject: Add ACPu memory cache --- lib/memcache/apcu.php | 28 ++++++++++++++++++++++++++++ lib/memcache/factory.php | 4 +++- tests/lib/memcache/apc.php | 4 ++++ tests/lib/memcache/apcu.php | 20 ++++++++++++++++++++ 4 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 lib/memcache/apcu.php create mode 100644 tests/lib/memcache/apcu.php (limited to 'tests') diff --git a/lib/memcache/apcu.php b/lib/memcache/apcu.php new file mode 100644 index 00000000000..ccc1aa6e562 --- /dev/null +++ b/lib/memcache/apcu.php @@ -0,0 +1,28 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Memcache; + +class APCu extends APC { + public function clear($prefix = '') { + $ns = $this->getNamespace() . $prefix; + $ns = preg_quote($ns, '/'); + $iter = new \APCIterator('/^'.$ns.'/'); + return apc_delete($iter); + } + + static public function isAvailable() { + if (!extension_loaded('apcu')) { + return false; + } elseif (!ini_get('apc.enable_cli') && \OC::$CLI) { + return false; + } else { + return true; + } + } +} diff --git a/lib/memcache/factory.php b/lib/memcache/factory.php index b1b49971031..4c1b1ab207f 100644 --- a/lib/memcache/factory.php +++ b/lib/memcache/factory.php @@ -18,6 +18,8 @@ class Factory { function create($prefix = '') { if (XCache::isAvailable()) { return new XCache($prefix); + } elseif (APCu::isAvailable()) { + return new APCu($prefix); } elseif (APC::isAvailable()) { return new APC($prefix); } elseif (Memcached::isAvailable()) { @@ -33,6 +35,6 @@ class Factory { * @return bool */ public function isAvailable() { - return XCache::isAvailable() || APC::isAvailable() || Memcached::isAvailable(); + return XCache::isAvailable() || APCu::isAvailable() || APC::isAvailable() || Memcached::isAvailable(); } } diff --git a/tests/lib/memcache/apc.php b/tests/lib/memcache/apc.php index 6b2a49470ba..e5d753a4fa5 100644 --- a/tests/lib/memcache/apc.php +++ b/tests/lib/memcache/apc.php @@ -15,6 +15,10 @@ class APC extends Cache { $this->markTestSkipped('The apc extension is not available.'); return; } + if(\OC\Memcache\APCu::isAvailable()) { + $this->markTestSkipped('The apc extension is emulated by ACPu.'); + return; + } $this->instance=new \OC\Memcache\APC(uniqid()); } } diff --git a/tests/lib/memcache/apcu.php b/tests/lib/memcache/apcu.php new file mode 100644 index 00000000000..7b99e7cd5e0 --- /dev/null +++ b/tests/lib/memcache/apcu.php @@ -0,0 +1,20 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\Memcache; + +class APCu extends Cache { + public function setUp() { + if(!\OC\Memcache\APCu::isAvailable()) { + $this->markTestSkipped('The APCu extension is not available.'); + return; + } + $this->instance=new \OC\Memcache\APCu(uniqid()); + } +} -- cgit v1.2.3 From 763afa5064b1c51751ba1200212d8a18a2cae38b Mon Sep 17 00:00:00 2001 From: Andreas Fischer Date: Thu, 25 Jul 2013 02:43:50 +0200 Subject: Add verbose to autotest phpunit, so we see skipped and incomplete tests. --- tests/phpunit-autotest.xml | 1 + 1 file changed, 1 insertion(+) (limited to 'tests') diff --git a/tests/phpunit-autotest.xml b/tests/phpunit-autotest.xml index 23b2d6c0060..a893e96ad97 100644 --- a/tests/phpunit-autotest.xml +++ b/tests/phpunit-autotest.xml @@ -1,6 +1,7 @@