From 76d13120eaf0bb6ed5661baa898b13cc6d35b111 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 28 May 2013 00:50:00 +0200 Subject: [PATCH 01/10] Abstract the session away in a class --- lib/session/internal.php | 39 ++++++++++++++++++++++ lib/session/memory.php | 63 +++++++++++++++++++++++++++++++++++ lib/session/session.php | 48 ++++++++++++++++++++++++++ tests/lib/session/memory.php | 16 +++++++++ tests/lib/session/session.php | 55 ++++++++++++++++++++++++++++++ 5 files changed, 221 insertions(+) create mode 100644 lib/session/internal.php create mode 100644 lib/session/memory.php create mode 100644 lib/session/session.php create mode 100644 tests/lib/session/memory.php create mode 100644 tests/lib/session/session.php diff --git a/lib/session/internal.php b/lib/session/internal.php new file mode 100644 index 00000000000..713a154ecc1 --- /dev/null +++ b/lib/session/internal.php @@ -0,0 +1,39 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Session; + +/** + * Class Internal + * + * wrap php's internal session handling into the Session interface + * + * @package OC\Session + */ +class Internal extends Memory { + public function __construct($name) { + session_write_close(); + session_name($name); + if (@session_start()) { + throw new \Exception('Failed to start session'); + } + $this->data = $_SESSION; + } + + public function __destruct() { + $_SESSION = $this->data; + session_write_close(); + } + + public function clear() { + session_unset(); + @session_regenerate_id(true); + @session_start(); + $this->data = $_SESSION = array(); + } +} diff --git a/lib/session/memory.php b/lib/session/memory.php new file mode 100644 index 00000000000..4202ddfd2fc --- /dev/null +++ b/lib/session/memory.php @@ -0,0 +1,63 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Session; + +/** + * Class Internal + * + * store session data in an in-memory array, not persistance + * + * @package OC\Session + */ +class Memory implements Session { + protected $data; + + public function __construct($name) { + //no need to use $name since all data is already scoped to this instance + $this->data = array(); + } + + /** + * @param string $key + * @param mixed $value + */ + public function set($key, $value) { + $this->data[$key] = $value; + } + + /** + * @param string $key + * @return mixed + */ + public function get($key) { + if (!$this->exists($key)) { + return null; + } + return $this->data[$key]; + } + + /** + * @param string $key + * @return bool + */ + public function exists($key) { + return isset($this->data[$key]); + } + + /** + * @param string $key + */ + public function remove($key) { + unset($this->data[$key]); + } + + public function clear() { + $this->data = array(); + } +} diff --git a/lib/session/session.php b/lib/session/session.php new file mode 100644 index 00000000000..3dce3b7f5b3 --- /dev/null +++ b/lib/session/session.php @@ -0,0 +1,48 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace OC\Session; + +interface Session { + /** + * $name serves as a namespace for the session keys + * + * @param string $name + */ + public function __construct($name); + + /** + * @param string $key + * @param mixed $value + */ + public function set($key, $value); + + /** + * @param string $key + * @return mixed should return null if $key does not exist + */ + public function get($key); + + /** + * @param string $key + * @return bool + */ + public function exists($key); + + /** + * should not throw any errors if $key does not exist + * + * @param string $key + */ + public function remove($key); + + /** + * removes all entries within the cache namespace + */ + public function clear(); +} diff --git a/tests/lib/session/memory.php b/tests/lib/session/memory.php new file mode 100644 index 00000000000..2dc236b73bf --- /dev/null +++ b/tests/lib/session/memory.php @@ -0,0 +1,16 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\Session; + +class Memory extends Session { + public function setUp() { + $this->instance = new \OC\Session\Memory(uniqid()); + } +} diff --git a/tests/lib/session/session.php b/tests/lib/session/session.php new file mode 100644 index 00000000000..be28251608a --- /dev/null +++ b/tests/lib/session/session.php @@ -0,0 +1,55 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\Session; + +abstract class Session extends \PHPUnit_Framework_TestCase { + /** + * @var \OC\Session\Session + */ + protected $instance; + + public function tearDown() { + $this->instance->clear(); + } + + public function testNotExistsEmpty() { + $this->assertFalse($this->instance->exists('foo')); + } + + public function testExistsAfterSet() { + $this->instance->set('foo', 1); + $this->assertTrue($this->instance->exists('foo')); + } + + public function testNotExistsAfterRemove() { + $this->instance->set('foo', 1); + $this->instance->remove('foo'); + $this->assertFalse($this->instance->exists('foo')); + } + + public function testGetNonExisting() { + $this->assertNull($this->instance->get('foo')); + } + + public function testGetAfterSet() { + $this->instance->set('foo', 'bar'); + $this->assertEquals('bar', $this->instance->get(('foo'))); + } + + public function testRemoveNonExisting() { + $this->instance->remove('foo'); + } + + public function testNotExistsAfterClear() { + $this->instance->set('foo', 1); + $this->instance->clear(); + $this->assertFalse($this->instance->exists('foo')); + } +} From 44f9af5a7fb6f0f9846bfb36ff99f9bf8aee5985 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 28 May 2013 01:04:09 +0200 Subject: [PATCH 02/10] Use the new session wrapper --- apps/files_encryption/lib/proxy.php | 2 +- apps/files_encryption/lib/session.php | 20 +++++-------- apps/files_encryption/tests/util.php | 6 ++-- apps/files_sharing/public.php | 6 ++-- lib/base.php | 42 +++++++++++++++------------ lib/template.php | 8 ++--- lib/user.php | 16 +++++----- lib/util.php | 14 ++++----- 8 files changed, 57 insertions(+), 57 deletions(-) diff --git a/apps/files_encryption/lib/proxy.php b/apps/files_encryption/lib/proxy.php index d9520810bf4..0f7eb84dc1b 100644 --- a/apps/files_encryption/lib/proxy.php +++ b/apps/files_encryption/lib/proxy.php @@ -220,7 +220,7 @@ class Proxy extends \OC_FileProxy } elseif ( Crypt::mode() == 'server' - && isset( $_SESSION['legacyenckey'] ) + &&\OC::$session->exists('legacyenckey') && Crypt::isEncryptedMeta( $path ) ) { $plainData = Crypt::legacyBlockDecrypt( $data, $session->getLegacyKey() ); diff --git a/apps/files_encryption/lib/session.php b/apps/files_encryption/lib/session.php index 2ddad0a15da..d3353c73818 100644 --- a/apps/files_encryption/lib/session.php +++ b/apps/files_encryption/lib/session.php @@ -106,7 +106,7 @@ class Session */ public function setPrivateKey( $privateKey ) { - $_SESSION['privateKey'] = $privateKey; + \OC::$session->set('privateKey', $privateKey) return true; @@ -119,12 +119,9 @@ class Session */ public function getPrivateKey() { - if ( - isset( $_SESSION['privateKey'] ) - && !empty( $_SESSION['privateKey'] ) - ) { + if ( !is_null( \OC::$session->get('privateKey') ) ) { - return $_SESSION['privateKey']; + return \OC::$session->get('privateKey'); } else { @@ -141,7 +138,7 @@ class Session */ public function setLegacyKey( $legacyKey ) { - $_SESSION['legacyKey'] = $legacyKey; + \OC::$session->set('legacyKey', $legacyKey); return true; } @@ -153,12 +150,9 @@ class Session */ public function getLegacyKey() { - if ( - isset( $_SESSION['legacyKey'] ) - && !empty( $_SESSION['legacyKey'] ) - ) { + if ( !is_null( \OC::$session->get('legacyKey') ) ) { - return $_SESSION['legacyKey']; + return \OC::$session->get('legacyKey'); } else { @@ -168,4 +162,4 @@ class Session } -} \ No newline at end of file +} diff --git a/apps/files_encryption/tests/util.php b/apps/files_encryption/tests/util.php index 2069cae27e5..0dc452a41c8 100755 --- a/apps/files_encryption/tests/util.php +++ b/apps/files_encryption/tests/util.php @@ -183,7 +183,7 @@ class Test_Encryption_Util extends \PHPUnit_Framework_TestCase { $this->assertTrue(OCA\Encryption\Hooks::login($params)); - $this->assertEquals($this->legacyKey, $_SESSION['legacyKey']); + $this->assertEquals($this->legacyKey, \OC::$session->get('legacyKey')); } function testRecoveryEnabledForUser() { @@ -273,7 +273,7 @@ class Test_Encryption_Util extends \PHPUnit_Framework_TestCase { $this->assertTrue(OCA\Encryption\Hooks::login($params)); - $this->assertEquals($this->legacyKey, $_SESSION['legacyKey']); + $this->assertEquals($this->legacyKey, \OC::$session->get('legacyKey')); $files = $util->findEncFiles('/' . \Test_Encryption_Util::TEST_ENCRYPTION_UTIL_LEGACY_USER . '/files/'); @@ -314,4 +314,4 @@ class Test_Encryption_Util extends \PHPUnit_Framework_TestCase { $params['password'] = $password; OCA\Encryption\Hooks::login($params); } -} \ No newline at end of file +} diff --git a/apps/files_sharing/public.php b/apps/files_sharing/public.php index 59598e35fa2..98d2a84fb66 100644 --- a/apps/files_sharing/public.php +++ b/apps/files_sharing/public.php @@ -84,7 +84,7 @@ if (isset($path)) { exit(); } else { // Save item id in session for future requests - $_SESSION['public_link_authenticated'] = $linkItem['id']; + \OC::$session->set('public_link_authenticated', $linkItem['id']); } } else { OCP\Util::writeLog('share', 'Unknown share type '.$linkItem['share_type'] @@ -97,8 +97,8 @@ if (isset($path)) { } else { // Check if item id is set in session - if (!isset($_SESSION['public_link_authenticated']) - || $_SESSION['public_link_authenticated'] !== $linkItem['id'] + if ( ! \OC::$session->exists('public_link_authenticated') + || \OC::$session->get('public_link_authenticated') !== $linkItem['id'] ) { // Prompt for password $tmpl = new OCP\Template('files_sharing', 'authenticate', 'guest'); diff --git a/lib/base.php b/lib/base.php index 724bd250a5c..c6ea32362e4 100644 --- a/lib/base.php +++ b/lib/base.php @@ -74,6 +74,11 @@ class OC { */ protected static $router = null; + /** + * @var \OC\Session\Session + */ + public static $session = null; + /** * @var \OC\Autoloader $loader */ @@ -283,14 +288,14 @@ class OC { $cookie_path = OC::$WEBROOT ?: '/'; ini_set('session.cookie_path', $cookie_path); - // set the session name to the instance id - which is unique - session_name(OC_Util::getInstanceId()); - - // if session cant be started break with http 500 error - if (session_start() === false){ - OC_Log::write('core', 'Session could not be initialized', + try{ + // set the session name to the instance id - which is unique + self::$session=new \OC\Session\Internal(OC_Util::getInstanceId()); + // if session cant be started break with http 500 error + }catch (Exception $e){ + OC_Log::write('core', 'Session could not be initialized', OC_Log::ERROR); - + header('HTTP/1.1 500 Internal Server Error'); OC_Util::addStyle("styles"); $error = 'Session could not be initialized. Please contact your '; @@ -304,15 +309,15 @@ class OC { } // regenerate session id periodically to avoid session fixation - if (!isset($_SESSION['SID_CREATED'])) { - $_SESSION['SID_CREATED'] = time(); - } else if (time() - $_SESSION['SID_CREATED'] > 60*60*12) { + if (!self::$session->exists('SID_CREATED')) { + self::$session->set('SID_CREATED', time()); + } else if (time() - self::$session->get('SID_CREATED') > 60*60*12) { session_regenerate_id(true); - $_SESSION['SID_CREATED'] = time(); + self::$session->set('SID_CREATED', time()); } // session timeout - if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 60*60*24)) { + if (self::$session->exists('LAST_ACTIVITY') && (time() - self::$session->get('LAST_ACTIVITY') > 60*60*24)) { if (isset($_COOKIE[session_name()])) { setcookie(session_name(), '', time() - 42000, $cookie_path); } @@ -320,7 +325,8 @@ class OC { session_destroy(); session_start(); } - $_SESSION['LAST_ACTIVITY'] = time(); + + self::$session->set('LAST_ACTIVITY', time()); } public static function getRouter() { @@ -446,14 +452,14 @@ class OC { // User and Groups if (!OC_Config::getValue("installed", false)) { - $_SESSION['user_id'] = ''; + self::$session->set('user_id',''); } OC_User::useBackend(new OC_User_Database()); OC_Group::useBackend(new OC_Group_Database()); - if (isset($_SERVER['PHP_AUTH_USER']) && isset($_SESSION['user_id']) - && $_SERVER['PHP_AUTH_USER'] != $_SESSION['user_id']) { + if (isset($_SERVER['PHP_AUTH_USER']) && self::$session->exists('user_id') + && $_SERVER['PHP_AUTH_USER'] != self::$session->get('user_id')) { OC_User::logout(); } @@ -598,7 +604,7 @@ class OC { // Handle redirect URL for logged in users if (isset($_REQUEST['redirect_url']) && OC_User::isLoggedIn()) { $location = OC_Helper::makeURLAbsolute(urldecode($_REQUEST['redirect_url'])); - + // Deny the redirect if the URL contains a @ // This prevents unvalidated redirects like ?redirect_url=:user@domain.com if (strpos($location, '@') === false) { @@ -748,7 +754,7 @@ class OC { if (OC_User::login($_POST["user"], $_POST["password"])) { // setting up the time zone if (isset($_POST['timezone-offset'])) { - $_SESSION['timezone'] = $_POST['timezone-offset']; + self::$session->set('timezone', $_POST['timezone-offset']); } self::cleanupLoginTokens($_POST['user']); diff --git a/lib/template.php b/lib/template.php index 2f535335648..9467dedb62a 100644 --- a/lib/template.php +++ b/lib/template.php @@ -246,14 +246,14 @@ class OC_Template{ // if the formfactor is not yet autodetected do the // autodetection now. For possible formfactors check the // detectFormfactor documentation - if(!isset($_SESSION['formfactor'])) { - $_SESSION['formfactor'] = self::detectFormfactor(); + if (!\OC::$session->exists('formfactor')) { + \OC::$session->set('formfactor', self::detectFormfactor()); } // allow manual override via GET parameter if(isset($_GET['formfactor'])) { - $_SESSION['formfactor']=$_GET['formfactor']; + \OC::$session->set('formfactor', $_GET['formfactor']); } - $formfactor=$_SESSION['formfactor']; + $formfactor = \OC::$session->get('formfactor'); if($formfactor=='default') { $fext=''; }elseif($formfactor=='mobile') { diff --git a/lib/user.php b/lib/user.php index 26fe73f8bfe..1dde87a1339 100644 --- a/lib/user.php +++ b/lib/user.php @@ -264,7 +264,7 @@ class OC_User { * @brief Sets user id for session and triggers emit */ public static function setUserId($uid) { - $_SESSION['user_id'] = $uid; + \OC::$session->set('user_id', $uid); } /** @@ -285,7 +285,7 @@ class OC_User { $result = true; } if (OC_User::getUser() === $uid) { - $_SESSION['display_name'] = $displayName; + \OC::$session->set('display_name', $displayName); } return $result; } @@ -328,10 +328,10 @@ class OC_User { * Checks if the user is logged in */ public static function isLoggedIn() { - if( isset($_SESSION['user_id']) AND $_SESSION['user_id']) { + if( \OC::$session->get('user_id')) { OC_App::loadApps(array('authentication')); self::setupBackends(); - if (self::userExists($_SESSION['user_id']) ) { + if (self::userExists(\OC::$session->get('user_id')) ) { return true; } } @@ -356,8 +356,8 @@ class OC_User { * @return string uid or false */ public static function getUser() { - if( isset($_SESSION['user_id']) AND $_SESSION['user_id'] ) { - return $_SESSION['user_id']; + if( \OC::$session->get('user_id') ) { + return \OC::$session->get('user_id'); } else{ return false; @@ -371,8 +371,8 @@ class OC_User { public static function getDisplayName($user=null) { if ( $user ) { return self::determineDisplayName($user); - } else if( isset($_SESSION['display_name']) AND $_SESSION['display_name'] ) { - return $_SESSION['display_name']; + } else if( \OC::$session->get('display_name') ) { + return \OC::$session->get('display_name'); } else{ return false; diff --git a/lib/util.php b/lib/util.php index ce68568183b..581f35bc0ac 100755 --- a/lib/util.php +++ b/lib/util.php @@ -151,10 +151,10 @@ class OC_Util { * @param bool dateOnly option to omit time from the result */ public static function formatDate( $timestamp, $dateOnly=false) { - if(isset($_SESSION['timezone'])) {//adjust to clients timezone if we know it + if(\OC::$session->exists('timezone')) {//adjust to clients timezone if we know it $systemTimeZone = intval(date('O')); $systemTimeZone=(round($systemTimeZone/100, 0)*60)+($systemTimeZone%100); - $clientTimeZone=$_SESSION['timezone']*60; + $clientTimeZone=\OC::$session->get('timezone')*60; $offset=$clientTimeZone-$systemTimeZone; $timestamp=$timestamp+$offset*60; } @@ -458,13 +458,13 @@ class OC_Util { */ public static function callRegister() { // Check if a token exists - if(!isset($_SESSION['requesttoken'])) { + if(!\OC::$session->exists('requesttoken')) { // No valid token found, generate a new one. $requestToken = self::generate_random_bytes(20); - $_SESSION['requesttoken']=$requestToken; + \OC::$session->set('requesttoken', $requestToken); } else { // Valid token already exists, send it - $requestToken = $_SESSION['requesttoken']; + $requestToken = \OC::$session->get('requesttoken'); } return($requestToken); } @@ -476,7 +476,7 @@ class OC_Util { * @see OC_Util::callRegister() */ public static function isCallRegistered() { - if(!isset($_SESSION['requesttoken'])) { + if(!\OC::$session->exists('requesttoken')) { return false; } @@ -492,7 +492,7 @@ class OC_Util { } // Check if the token is valid - if($token !== $_SESSION['requesttoken']) { + if($token !== \OC::$session->get('requesttoken')) { // Not valid return false; } else { From e73c04501ebfe5d7e3f7895b349c941b7483ae6f Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 28 May 2013 01:10:18 +0200 Subject: [PATCH 03/10] Fix session not working error --- lib/base.php | 5 ++++- lib/session/internal.php | 1 - 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/lib/base.php b/lib/base.php index c6ea32362e4..42b57527063 100644 --- a/lib/base.php +++ b/lib/base.php @@ -290,9 +290,12 @@ class OC { try{ // set the session name to the instance id - which is unique - self::$session=new \OC\Session\Internal(OC_Util::getInstanceId()); + self::$session = new \OC\Session\Internal(OC_Util::getInstanceId()); // if session cant be started break with http 500 error }catch (Exception $e){ + //set the session object to a dummy session so code relying on the session existing still works + self::$session = new \OC\Session\Memory(''); + OC_Log::write('core', 'Session could not be initialized', OC_Log::ERROR); diff --git a/lib/session/internal.php b/lib/session/internal.php index 713a154ecc1..1f8fda47a9b 100644 --- a/lib/session/internal.php +++ b/lib/session/internal.php @@ -17,7 +17,6 @@ namespace OC\Session; */ class Internal extends Memory { public function __construct($name) { - session_write_close(); session_name($name); if (@session_start()) { throw new \Exception('Failed to start session'); From f547b66d6bf00040438ab3ba544bb55aab2f2bb5 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 28 May 2013 01:13:36 +0200 Subject: [PATCH 04/10] check fix if session start is successfull --- lib/session/internal.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/session/internal.php b/lib/session/internal.php index 1f8fda47a9b..60aecccc8aa 100644 --- a/lib/session/internal.php +++ b/lib/session/internal.php @@ -18,7 +18,8 @@ namespace OC\Session; class Internal extends Memory { public function __construct($name) { session_name($name); - if (@session_start()) { + session_start(); + if (!isset($_SESSION)) { throw new \Exception('Failed to start session'); } $this->data = $_SESSION; From 040045cc2ea1ecde2d199a45c5a33cd197bc583b Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 28 May 2013 01:15:38 +0200 Subject: [PATCH 05/10] provide a dummy session for CLI --- lib/base.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/lib/base.php b/lib/base.php index 42b57527063..f1145b651ae 100644 --- a/lib/base.php +++ b/lib/base.php @@ -445,6 +445,8 @@ class OC { self::checkSSL(); if ( !self::$CLI ) { self::initSession(); + } else { + self::$session = new \OC\Session\Memory(''); } $errors = OC_Util::checkServer(); From 9c99048429ff4bf0db742e58931c245d03efb060 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 28 May 2013 01:31:26 +0200 Subject: [PATCH 06/10] fix syntax error --- apps/files_encryption/lib/session.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apps/files_encryption/lib/session.php b/apps/files_encryption/lib/session.php index d3353c73818..30cb7b0e1b3 100644 --- a/apps/files_encryption/lib/session.php +++ b/apps/files_encryption/lib/session.php @@ -106,7 +106,7 @@ class Session */ public function setPrivateKey( $privateKey ) { - \OC::$session->set('privateKey', $privateKey) + \OC::$session->set('privateKey', $privateKey); return true; From 57f712f8a9e638157f1f7ce8158791834a365ee5 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 28 May 2013 16:52:40 +0200 Subject: [PATCH 07/10] implement ArrayInterface with Session --- lib/session/memory.php | 2 +- lib/session/session.php | 45 ++++- tests/lib/session/session.php | 9 + tests/lib/user/manager.php | 181 +++++++++++++++++++ tests/lib/user/user.php | 319 ++++++++++++++++++++++++++++++++++ 5 files changed, 548 insertions(+), 8 deletions(-) create mode 100644 tests/lib/user/manager.php create mode 100644 tests/lib/user/user.php diff --git a/lib/session/memory.php b/lib/session/memory.php index 4202ddfd2fc..c148ff4b9b9 100644 --- a/lib/session/memory.php +++ b/lib/session/memory.php @@ -15,7 +15,7 @@ namespace OC\Session; * * @package OC\Session */ -class Memory implements Session { +class Memory extends Session { protected $data; public function __construct($name) { diff --git a/lib/session/session.php b/lib/session/session.php index 3dce3b7f5b3..55515f57a87 100644 --- a/lib/session/session.php +++ b/lib/session/session.php @@ -8,41 +8,72 @@ namespace OC\Session; -interface Session { +abstract class Session implements \ArrayAccess { /** * $name serves as a namespace for the session keys * * @param string $name */ - public function __construct($name); + abstract public function __construct($name); /** * @param string $key * @param mixed $value */ - public function set($key, $value); + abstract public function set($key, $value); /** * @param string $key * @return mixed should return null if $key does not exist */ - public function get($key); + abstract public function get($key); /** * @param string $key * @return bool */ - public function exists($key); + abstract public function exists($key); /** * should not throw any errors if $key does not exist * * @param string $key */ - public function remove($key); + abstract public function remove($key); /** * removes all entries within the cache namespace */ - public function clear(); + abstract public function clear(); + + /** + * @param mixed $offset + * @return bool + */ + public function offsetExists($offset) { + return $this->exists($offset); + } + + /** + * @param mixed $offset + * @return mixed + */ + public function offsetGet($offset) { + return $this->get($offset); + } + + /** + * @param mixed $offset + * @param mixed $value + */ + public function offsetSet($offset, $value) { + $this->set($offset, $value); + } + + /** + * @param mixed $offset + */ + public function offsetUnset($offset) { + $this->remove($offset); + } } diff --git a/tests/lib/session/session.php b/tests/lib/session/session.php index be28251608a..72dee44e7cb 100644 --- a/tests/lib/session/session.php +++ b/tests/lib/session/session.php @@ -52,4 +52,13 @@ abstract class Session extends \PHPUnit_Framework_TestCase { $this->instance->clear(); $this->assertFalse($this->instance->exists('foo')); } + + public function testArrayInterface() { + $this->assertFalse(isset($this->instance['foo'])); + $this->instance['foo'] = 'bar'; + $this->assertTrue(isset($this->instance['foo'])); + $this->assertEquals('bar', $this->instance['foo']); + unset($this->instance['foo']); + $this->assertFalse(isset($this->instance['foo'])); + } } diff --git a/tests/lib/user/manager.php b/tests/lib/user/manager.php new file mode 100644 index 00000000000..401ff70297e --- /dev/null +++ b/tests/lib/user/manager.php @@ -0,0 +1,181 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\User; + +class Manager extends \PHPUnit_Framework_TestCase { + public function testUserExistsSingleBackendExists() { + /** + * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\OC_User_Dummy'); + $backend->expects($this->once()) + ->method('userExists') + ->with($this->equalTo('foo')) + ->will($this->returnValue(true)); + + $manager = new \OC\User\Manager(); + $manager->registerBackend($backend); + + $this->assertTrue($manager->userExists('foo')); + } + + public function testUserExistsSingleBackendNotExists() { + /** + * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\OC_User_Dummy'); + $backend->expects($this->once()) + ->method('userExists') + ->with($this->equalTo('foo')) + ->will($this->returnValue(false)); + + $manager = new \OC\User\Manager(); + $manager->registerBackend($backend); + + $this->assertFalse($manager->userExists('foo')); + } + + public function testUserExistsNoBackends() { + $manager = new \OC\User\Manager(); + + $this->assertFalse($manager->userExists('foo')); + } + + public function testUserExistsTwoBackendsSecondExists() { + /** + * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend1 + */ + $backend1 = $this->getMock('\OC_User_Dummy'); + $backend1->expects($this->once()) + ->method('userExists') + ->with($this->equalTo('foo')) + ->will($this->returnValue(false)); + + /** + * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend2 + */ + $backend2 = $this->getMock('\OC_User_Dummy'); + $backend2->expects($this->once()) + ->method('userExists') + ->with($this->equalTo('foo')) + ->will($this->returnValue(true)); + + $manager = new \OC\User\Manager(); + $manager->registerBackend($backend1); + $manager->registerBackend($backend2); + + $this->assertTrue($manager->userExists('foo')); + } + + public function testUserExistsTwoBackendsFirstExists() { + /** + * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend1 + */ + $backend1 = $this->getMock('\OC_User_Dummy'); + $backend1->expects($this->once()) + ->method('userExists') + ->with($this->equalTo('foo')) + ->will($this->returnValue(true)); + + /** + * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend2 + */ + $backend2 = $this->getMock('\OC_User_Dummy'); + $backend2->expects($this->never()) + ->method('userExists'); + + $manager = new \OC\User\Manager(); + $manager->registerBackend($backend1); + $manager->registerBackend($backend2); + + $this->assertTrue($manager->userExists('foo')); + } + + public function testGetOneBackendExists() { + /** + * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\OC_User_Dummy'); + $backend->expects($this->once()) + ->method('userExists') + ->with($this->equalTo('foo')) + ->will($this->returnValue(true)); + + $manager = new \OC\User\Manager(); + $manager->registerBackend($backend); + + $this->assertEquals('foo', $manager->get('foo')->getUID()); + } + + public function testGetOneBackendNotExists() { + /** + * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\OC_User_Dummy'); + $backend->expects($this->once()) + ->method('userExists') + ->with($this->equalTo('foo')) + ->will($this->returnValue(false)); + + $manager = new \OC\User\Manager(); + $manager->registerBackend($backend); + + $this->assertEquals(null, $manager->get('foo')); + } + + public function testSearchOneBackend() { + /** + * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\OC_User_Dummy'); + $backend->expects($this->once()) + ->method('getUsers') + ->with($this->equalTo('fo')) + ->will($this->returnValue(array('foo', 'afoo'))); + + $manager = new \OC\User\Manager(); + $manager->registerBackend($backend); + + $result = $manager->search('fo'); + $this->assertEquals(2, count($result)); + $this->assertEquals('afoo', $result[0]->getUID()); + $this->assertEquals('foo', $result[1]->getUID()); + } + + public function testSearchTwoBackendLimitOffset() { + /** + * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend1 + */ + $backend1 = $this->getMock('\OC_User_Dummy'); + $backend1->expects($this->once()) + ->method('getUsers') + ->with($this->equalTo('fo'), $this->equalTo(3), $this->equalTo(1)) + ->will($this->returnValue(array('foo1', 'foo2'))); + + /** + * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend2 + */ + $backend2 = $this->getMock('\OC_User_Dummy'); + $backend2->expects($this->once()) + ->method('getUsers') + ->with($this->equalTo('fo'), $this->equalTo(1), $this->equalTo(0)) + ->will($this->returnValue(array('foo3'))); + + $manager = new \OC\User\Manager(); + $manager->registerBackend($backend1); + $manager->registerBackend($backend2); + + $result = $manager->search('fo', 3, 1); + $this->assertEquals(3, count($result)); + $this->assertEquals('foo1', $result[0]->getUID()); + $this->assertEquals('foo2', $result[1]->getUID()); + $this->assertEquals('foo3', $result[2]->getUID()); + } +} diff --git a/tests/lib/user/user.php b/tests/lib/user/user.php new file mode 100644 index 00000000000..ad03bd57483 --- /dev/null +++ b/tests/lib/user/user.php @@ -0,0 +1,319 @@ + + * This file is licensed under the Affero General Public License version 3 or + * later. + * See the COPYING-README file. + */ + +namespace Test\User; + +use OC\Hooks\PublicEmitter; + +class User extends \PHPUnit_Framework_TestCase { + public function testDisplayName() { + /** + * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\OC_User_Backend'); + $backend->expects($this->once()) + ->method('getDisplayName') + ->with($this->equalTo('foo')) + ->will($this->returnValue('Foo')); + + $backend->expects($this->any()) + ->method('implementsActions') + ->with($this->equalTo(\OC_USER_BACKEND_GET_DISPLAYNAME)) + ->will($this->returnValue(true)); + + $user = new \OC\User\User('foo', $backend); + $this->assertEquals('Foo', $user->getDisplayName()); + } + + public function testDisplayNameNotSupported() { + /** + * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\OC_User_Backend'); + $backend->expects($this->never()) + ->method('getDisplayName'); + + $backend->expects($this->any()) + ->method('implementsActions') + ->with($this->equalTo(\OC_USER_BACKEND_GET_DISPLAYNAME)) + ->will($this->returnValue(false)); + + $user = new \OC\User\User('foo', $backend); + $this->assertEquals('foo', $user->getDisplayName()); + } + + public function testSetPassword() { + /** + * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\OC_User_Dummy'); + $backend->expects($this->once()) + ->method('setPassword') + ->with($this->equalTo('foo'), $this->equalTo('bar')); + + $backend->expects($this->any()) + ->method('implementsActions') + ->will($this->returnCallback(function ($actions) { + if ($actions === \OC_USER_BACKEND_SET_PASSWORD) { + return true; + } else { + return false; + } + })); + + $user = new \OC\User\User('foo', $backend); + $this->assertTrue($user->setPassword('bar')); + } + + public function testSetPasswordNotSupported() { + /** + * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\OC_User_Dummy'); + $backend->expects($this->never()) + ->method('setPassword'); + + $backend->expects($this->any()) + ->method('implementsActions') + ->will($this->returnValue(false)); + + $user = new \OC\User\User('foo', $backend); + $this->assertFalse($user->setPassword('bar')); + } + + public function testDelete() { + /** + * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\OC_User_Dummy'); + $backend->expects($this->once()) + ->method('deleteUser') + ->with($this->equalTo('foo')); + + $user = new \OC\User\User('foo', $backend); + $this->assertTrue($user->delete()); + } + + public function testCheckPassword() { + /** + * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\OC_User_Dummy'); + $backend->expects($this->once()) + ->method('checkPassword') + ->with($this->equalTo('foo'), $this->equalTo('bar')) + ->will($this->returnValue(true)); + + $backend->expects($this->any()) + ->method('implementsActions') + ->will($this->returnCallback(function ($actions) { + if ($actions === \OC_USER_BACKEND_CHECK_PASSWORD) { + return true; + } else { + return false; + } + })); + + $user = new \OC\User\User('foo', $backend); + $this->assertTrue($user->checkPassword('bar')); + } + + public function testCheckPasswordNotSupported() { + /** + * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\OC_User_Dummy'); + $backend->expects($this->never()) + ->method('checkPassword'); + + $backend->expects($this->any()) + ->method('implementsActions') + ->will($this->returnValue(false)); + + $user = new \OC\User\User('foo', $backend); + $this->assertFalse($user->checkPassword('bar')); + } + + public function testGetHome() { + /** + * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\OC_User_Dummy'); + $backend->expects($this->once()) + ->method('getHome') + ->with($this->equalTo('foo')) + ->will($this->returnValue('/home/foo')); + + $backend->expects($this->any()) + ->method('implementsActions') + ->will($this->returnCallback(function ($actions) { + if ($actions === \OC_USER_BACKEND_GET_HOME) { + return true; + } else { + return false; + } + })); + + $user = new \OC\User\User('foo', $backend); + $this->assertEquals('/home/foo', $user->getHome()); + } + + public function testGetHomeNotSupported() { + /** + * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\OC_User_Dummy'); + $backend->expects($this->never()) + ->method('getHome'); + + $backend->expects($this->any()) + ->method('implementsActions') + ->will($this->returnValue(false)); + + $user = new \OC\User\User('foo', $backend); + $this->assertEquals(\OC_Config::getValue("datadirectory", \OC::$SERVERROOT . "/data") . '/foo', $user->getHome()); + } + + public function testCanChangePassword() { + /** + * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\OC_User_Dummy'); + + $backend->expects($this->any()) + ->method('implementsActions') + ->will($this->returnCallback(function ($actions) { + if ($actions === \OC_USER_BACKEND_SET_PASSWORD) { + return true; + } else { + return false; + } + })); + + $user = new \OC\User\User('foo', $backend); + $this->assertTrue($user->canChangePassword()); + } + + public function testCanChangePasswordNotSupported() { + /** + * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\OC_User_Dummy'); + + $backend->expects($this->any()) + ->method('implementsActions') + ->will($this->returnValue(false)); + + $user = new \OC\User\User('foo', $backend); + $this->assertFalse($user->canChangePassword()); + } + + public function testCanChangeDisplayName() { + /** + * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\OC_User_Dummy'); + + $backend->expects($this->any()) + ->method('implementsActions') + ->will($this->returnCallback(function ($actions) { + if ($actions === \OC_USER_BACKEND_SET_DISPLAYNAME) { + return true; + } else { + return false; + } + })); + + $user = new \OC\User\User('foo', $backend); + $this->assertTrue($user->canChangeDisplayName()); + } + + public function testCanChangeDisplayNameNotSupported() { + /** + * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\OC_User_Dummy'); + + $backend->expects($this->any()) + ->method('implementsActions') + ->will($this->returnValue(false)); + + $user = new \OC\User\User('foo', $backend); + $this->assertFalse($user->canChangeDisplayName()); + } + + public function testSetPasswordHooks() { + $hooksCalled = 0; + $test = $this; + + /** + * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\OC_User_Dummy'); + $backend->expects($this->once()) + ->method('setPassword'); + + /** + * @param \OC\User\User $user + * @param string $password + */ + $hook = function ($user, $password) use ($test, &$hooksCalled) { + $hooksCalled++; + $test->assertEquals('foo', $user->getUID()); + $test->assertEquals('bar', $password); + }; + + $emitter = new PublicEmitter(); + $emitter->listen('\OC\User', 'preSetPassword', $hook); + $emitter->listen('\OC\User', 'postSetPassword', $hook); + + $backend->expects($this->any()) + ->method('implementsActions') + ->will($this->returnCallback(function ($actions) { + if ($actions === \OC_USER_BACKEND_SET_PASSWORD) { + return true; + } else { + return false; + } + })); + + $user = new \OC\User\User('foo', $backend, $emitter); + + $user->setPassword('bar'); + $this->assertEquals(2, $hooksCalled); + } + + public function testDeleteHooks() { + $hooksCalled = 0; + $test = $this; + + /** + * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend + */ + $backend = $this->getMock('\OC_User_Dummy'); + $backend->expects($this->once()) + ->method('deleteUser'); + + /** + * @param \OC\User\User $user + */ + $hook = function ($user) use ($test, &$hooksCalled) { + $hooksCalled++; + $test->assertEquals('foo', $user->getUID()); + }; + + $emitter = new PublicEmitter(); + $emitter->listen('\OC\User', 'preDelete', $hook); + $emitter->listen('\OC\User', 'postDelete', $hook); + + $user = new \OC\User\User('foo', $backend, $emitter); + $this->assertTrue($user->delete()); + $this->assertEquals(2, $hooksCalled); + } +} From 6ae7bde78849829e2f439dd06a8ff87ab6339450 Mon Sep 17 00:00:00 2001 From: Florin Peter Date: Tue, 28 May 2013 17:04:35 +0200 Subject: [PATCH 08/10] fixed encryption session namespace to avoid problems --- apps/files_encryption/hooks/hooks.php | 8 ++++---- apps/files_encryption/lib/proxy.php | 6 +++--- apps/files_encryption/lib/stream.php | 2 +- apps/files_encryption/lib/util.php | 2 +- apps/files_encryption/tests/crypt.php | 4 ++-- 5 files changed, 11 insertions(+), 11 deletions(-) diff --git a/apps/files_encryption/hooks/hooks.php b/apps/files_encryption/hooks/hooks.php index 9af1f2c6459..0ef796dbaef 100644 --- a/apps/files_encryption/hooks/hooks.php +++ b/apps/files_encryption/hooks/hooks.php @@ -57,7 +57,7 @@ class Hooks { $privateKey = Crypt::symmetricDecryptFileContent( $encryptedKey, $params['password'] ); - $session = new Session( $view ); + $session = new \OCA\Encryption\Session( $view ); $session->setPrivateKey( $privateKey, $params['uid'] ); @@ -151,7 +151,7 @@ class Hooks { $view = new \OC_FilesystemView('/'); - $session = new Session($view); + $session = new \OCA\Encryption\Session($view); // Get existing decrypted private key $privateKey = $session->getPrivateKey(); @@ -266,7 +266,7 @@ class Hooks { if ($params['itemType'] === 'file' || $params['itemType'] === 'folder') { $view = new \OC_FilesystemView('/'); - $session = new Session($view); + $session = new \OCA\Encryption\Session($view); $userId = \OCP\User::getUser(); $util = new Util($view, $userId); $path = $util->fileIdToPath($params['itemSource']); @@ -438,7 +438,7 @@ class Hooks { \OC_FileProxy::$enabled = false; $view = new \OC_FilesystemView('/'); - $session = new Session($view); + $session = new \OCA\Encryption\Session($view); $userId = \OCP\User::getUser(); $util = new Util( $view, $userId ); diff --git a/apps/files_encryption/lib/proxy.php b/apps/files_encryption/lib/proxy.php index 0f7eb84dc1b..e381ecab5c3 100644 --- a/apps/files_encryption/lib/proxy.php +++ b/apps/files_encryption/lib/proxy.php @@ -111,7 +111,7 @@ class Proxy extends \OC_FileProxy $userId = \OCP\USER::getUser(); $view = new \OC_FilesystemView( '/' ); $util = new Util( $view, $userId ); - $session = new Session( $view ); + $session = new \OCA\Encryption\Session( $view ); $privateKey = $session->getPrivateKey(); $filePath = $util->stripUserFilesPath( $path ); // Set the filesize for userland, before encrypting @@ -197,7 +197,7 @@ class Proxy extends \OC_FileProxy \OC_FileProxy::$enabled = false; // init session - $session = new Session( $view ); + $session = new \OCA\Encryption\Session( $view ); // If data is a catfile if ( @@ -439,7 +439,7 @@ class Proxy extends \OC_FileProxy \OC_FileProxy::$enabled = false; $view = new \OC_FilesystemView( '/' ); - $session = new Session( $view ); + $session = new \OCA\Encryption\Session( $view ); $userId = \OCP\User::getUser(); $util = new Util( $view, $userId ); diff --git a/apps/files_encryption/lib/stream.php b/apps/files_encryption/lib/stream.php index fa9df02f085..da8c2494139 100644 --- a/apps/files_encryption/lib/stream.php +++ b/apps/files_encryption/lib/stream.php @@ -228,7 +228,7 @@ class Stream // If a keyfile already exists if ( $this->encKeyfile ) { - $session = new Session( $this->rootView ); + $session = new \OCA\Encryption\Session( $this->rootView ); $privateKey = $session->getPrivateKey( $this->userId ); diff --git a/apps/files_encryption/lib/util.php b/apps/files_encryption/lib/util.php index 48485cf2e86..6ffe31c9bb4 100644 --- a/apps/files_encryption/lib/util.php +++ b/apps/files_encryption/lib/util.php @@ -1420,7 +1420,7 @@ class Util { if ($item['type'] == 'dir') { $this->addRecoveryKeys($filePath . '/'); } else { - $session = new Session(new \OC_FilesystemView('/')); + $session = new \OCA\Encryption\Session(new \OC_FilesystemView('/')); $sharingEnabled = \OCP\Share::isEnabled(); $file = substr($filePath, 0, -4); $usersSharing = $this->getSharingUsersArray($sharingEnabled, $file); diff --git a/apps/files_encryption/tests/crypt.php b/apps/files_encryption/tests/crypt.php index e9f155e2649..5b0486aad8c 100755 --- a/apps/files_encryption/tests/crypt.php +++ b/apps/files_encryption/tests/crypt.php @@ -270,7 +270,7 @@ class Test_Encryption_Crypt extends \PHPUnit_Framework_TestCase { $shareKey = Encryption\Keymanager::getShareKey($this->view, $this->userId, $filename); // get session - $session = new Encryption\Session($this->view); + $session = new \OCA\Encryption\Session($this->view); // get private key $privateKey = $session->getPrivateKey($this->userId); @@ -345,7 +345,7 @@ class Test_Encryption_Crypt extends \PHPUnit_Framework_TestCase { $shareKey = Encryption\Keymanager::getShareKey($this->view, $this->userId, $filename); // get session - $session = new Encryption\Session($this->view); + $session = new \OCA\Encryption\Session($this->view); // get private key $privateKey = $session->getPrivateKey($this->userId); From 4b4b447e2adfa383cd702b175af248900e1438fe Mon Sep 17 00:00:00 2001 From: Florin Peter Date: Tue, 28 May 2013 17:06:37 +0200 Subject: [PATCH 09/10] fixed missing convert to new session handler --- apps/files_encryption/lib/session.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/apps/files_encryption/lib/session.php b/apps/files_encryption/lib/session.php index 34913039b0c..d60c386fb1c 100644 --- a/apps/files_encryption/lib/session.php +++ b/apps/files_encryption/lib/session.php @@ -150,12 +150,11 @@ class Session */ public function getPublicSharePrivateKey() { - if (isset($_SESSION['publicSharePrivateKey']) && !empty($_SESSION['publicSharePrivateKey'])) { - return $_SESSION['publicSharePrivateKey']; + if (!is_null( \OC::$session->get('publicSharePrivateKey') )) { + return \OC::$session->get('publicSharePrivateKey'); } else { return false; } - } From fa6bfe8837c5f22e724df97c7049d60c1bb904ff Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 28 May 2013 17:21:44 +0200 Subject: [PATCH 10/10] didn't mean to commit this yet --- tests/lib/user/backend.php | 99 ----------- tests/lib/user/database.php | 44 ----- tests/lib/user/dummy.php | 27 --- tests/lib/user/manager.php | 181 -------------------- tests/lib/user/user.php | 319 ------------------------------------ 5 files changed, 670 deletions(-) delete mode 100644 tests/lib/user/backend.php delete mode 100644 tests/lib/user/database.php delete mode 100644 tests/lib/user/dummy.php delete mode 100644 tests/lib/user/manager.php delete mode 100644 tests/lib/user/user.php diff --git a/tests/lib/user/backend.php b/tests/lib/user/backend.php deleted file mode 100644 index 40674424c96..00000000000 --- a/tests/lib/user/backend.php +++ /dev/null @@ -1,99 +0,0 @@ -. -* -*/ - -/** - * Abstract class to provide the basis of backend-specific unit test classes. - * - * All subclasses MUST assign a backend property in setUp() which implements - * user operations (add, remove, etc.). Test methods in this class will then be - * run on each separate subclass and backend therein. - * - * For an example see /tests/lib/user/dummy.php - */ - -abstract class Test_User_Backend extends PHPUnit_Framework_TestCase { - /** - * @var OC_User_Backend $backend - */ - protected $backend; - - /** - * get a new unique user name - * test cases can override this in order to clean up created user - * @return array - */ - public function getUser() { - return uniqid('test_'); - } - - public function testAddRemove() { - //get the number of groups we start with, in case there are exising groups - $startCount=count($this->backend->getUsers()); - - $name1=$this->getUser(); - $name2=$this->getUser(); - $this->backend->createUser($name1, ''); - $count=count($this->backend->getUsers())-$startCount; - $this->assertEquals(1, $count); - $this->assertTrue((array_search($name1, $this->backend->getUsers())!==false)); - $this->assertFalse((array_search($name2, $this->backend->getUsers())!==false)); - $this->backend->createUser($name2, ''); - $count=count($this->backend->getUsers())-$startCount; - $this->assertEquals(2, $count); - $this->assertTrue((array_search($name1, $this->backend->getUsers())!==false)); - $this->assertTrue((array_search($name2, $this->backend->getUsers())!==false)); - - $this->backend->deleteUser($name2); - $count=count($this->backend->getUsers())-$startCount; - $this->assertEquals(1, $count); - $this->assertTrue((array_search($name1, $this->backend->getUsers())!==false)); - $this->assertFalse((array_search($name2, $this->backend->getUsers())!==false)); - } - - public function testLogin() { - $name1=$this->getUser(); - $name2=$this->getUser(); - - $this->assertFalse($this->backend->userExists($name1)); - $this->assertFalse($this->backend->userExists($name2)); - - $this->backend->createUser($name1, 'pass1'); - $this->backend->createUser($name2, 'pass2'); - - $this->assertTrue($this->backend->userExists($name1)); - $this->assertTrue($this->backend->userExists($name2)); - - $this->assertTrue($this->backend->checkPassword($name1, 'pass1')); - $this->assertTrue($this->backend->checkPassword($name2, 'pass2')); - - $this->assertFalse($this->backend->checkPassword($name1, 'pass2')); - $this->assertFalse($this->backend->checkPassword($name2, 'pass1')); - - $this->assertFalse($this->backend->checkPassword($name1, 'dummy')); - $this->assertFalse($this->backend->checkPassword($name2, 'foobar')); - - $this->backend->setPassword($name1, 'newpass1'); - $this->assertFalse($this->backend->checkPassword($name1, 'pass1')); - $this->assertTrue($this->backend->checkPassword($name1, 'newpass1')); - $this->assertFalse($this->backend->checkPassword($name2, 'newpass1')); - } -} diff --git a/tests/lib/user/database.php b/tests/lib/user/database.php deleted file mode 100644 index fe7d87c44de..00000000000 --- a/tests/lib/user/database.php +++ /dev/null @@ -1,44 +0,0 @@ -. -* -*/ - -class Test_User_Database extends Test_User_Backend { - /** - * get a new unique user name - * test cases can override this in order to clean up created user - * @return array - */ - public function getUser() { - $user=uniqid('test_'); - $this->users[]=$user; - return $user; - } - - public function setUp() { - $this->backend=new OC_User_Dummy(); - } - - public function tearDown() { - foreach($this->users as $user) { - $this->backend->deleteUser($user); - } - } -} diff --git a/tests/lib/user/dummy.php b/tests/lib/user/dummy.php deleted file mode 100644 index e417fd97603..00000000000 --- a/tests/lib/user/dummy.php +++ /dev/null @@ -1,27 +0,0 @@ -. -* -*/ - -class Test_User_Dummy extends Test_User_Backend { - public function setUp() { - $this->backend=new OC_User_Dummy(); - } -} diff --git a/tests/lib/user/manager.php b/tests/lib/user/manager.php deleted file mode 100644 index 401ff70297e..00000000000 --- a/tests/lib/user/manager.php +++ /dev/null @@ -1,181 +0,0 @@ - - * This file is licensed under the Affero General Public License version 3 or - * later. - * See the COPYING-README file. - */ - -namespace Test\User; - -class Manager extends \PHPUnit_Framework_TestCase { - public function testUserExistsSingleBackendExists() { - /** - * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend - */ - $backend = $this->getMock('\OC_User_Dummy'); - $backend->expects($this->once()) - ->method('userExists') - ->with($this->equalTo('foo')) - ->will($this->returnValue(true)); - - $manager = new \OC\User\Manager(); - $manager->registerBackend($backend); - - $this->assertTrue($manager->userExists('foo')); - } - - public function testUserExistsSingleBackendNotExists() { - /** - * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend - */ - $backend = $this->getMock('\OC_User_Dummy'); - $backend->expects($this->once()) - ->method('userExists') - ->with($this->equalTo('foo')) - ->will($this->returnValue(false)); - - $manager = new \OC\User\Manager(); - $manager->registerBackend($backend); - - $this->assertFalse($manager->userExists('foo')); - } - - public function testUserExistsNoBackends() { - $manager = new \OC\User\Manager(); - - $this->assertFalse($manager->userExists('foo')); - } - - public function testUserExistsTwoBackendsSecondExists() { - /** - * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend1 - */ - $backend1 = $this->getMock('\OC_User_Dummy'); - $backend1->expects($this->once()) - ->method('userExists') - ->with($this->equalTo('foo')) - ->will($this->returnValue(false)); - - /** - * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend2 - */ - $backend2 = $this->getMock('\OC_User_Dummy'); - $backend2->expects($this->once()) - ->method('userExists') - ->with($this->equalTo('foo')) - ->will($this->returnValue(true)); - - $manager = new \OC\User\Manager(); - $manager->registerBackend($backend1); - $manager->registerBackend($backend2); - - $this->assertTrue($manager->userExists('foo')); - } - - public function testUserExistsTwoBackendsFirstExists() { - /** - * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend1 - */ - $backend1 = $this->getMock('\OC_User_Dummy'); - $backend1->expects($this->once()) - ->method('userExists') - ->with($this->equalTo('foo')) - ->will($this->returnValue(true)); - - /** - * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend2 - */ - $backend2 = $this->getMock('\OC_User_Dummy'); - $backend2->expects($this->never()) - ->method('userExists'); - - $manager = new \OC\User\Manager(); - $manager->registerBackend($backend1); - $manager->registerBackend($backend2); - - $this->assertTrue($manager->userExists('foo')); - } - - public function testGetOneBackendExists() { - /** - * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend - */ - $backend = $this->getMock('\OC_User_Dummy'); - $backend->expects($this->once()) - ->method('userExists') - ->with($this->equalTo('foo')) - ->will($this->returnValue(true)); - - $manager = new \OC\User\Manager(); - $manager->registerBackend($backend); - - $this->assertEquals('foo', $manager->get('foo')->getUID()); - } - - public function testGetOneBackendNotExists() { - /** - * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend - */ - $backend = $this->getMock('\OC_User_Dummy'); - $backend->expects($this->once()) - ->method('userExists') - ->with($this->equalTo('foo')) - ->will($this->returnValue(false)); - - $manager = new \OC\User\Manager(); - $manager->registerBackend($backend); - - $this->assertEquals(null, $manager->get('foo')); - } - - public function testSearchOneBackend() { - /** - * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend - */ - $backend = $this->getMock('\OC_User_Dummy'); - $backend->expects($this->once()) - ->method('getUsers') - ->with($this->equalTo('fo')) - ->will($this->returnValue(array('foo', 'afoo'))); - - $manager = new \OC\User\Manager(); - $manager->registerBackend($backend); - - $result = $manager->search('fo'); - $this->assertEquals(2, count($result)); - $this->assertEquals('afoo', $result[0]->getUID()); - $this->assertEquals('foo', $result[1]->getUID()); - } - - public function testSearchTwoBackendLimitOffset() { - /** - * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend1 - */ - $backend1 = $this->getMock('\OC_User_Dummy'); - $backend1->expects($this->once()) - ->method('getUsers') - ->with($this->equalTo('fo'), $this->equalTo(3), $this->equalTo(1)) - ->will($this->returnValue(array('foo1', 'foo2'))); - - /** - * @var \OC_User_Dummy | \PHPUnit_Framework_MockObject_MockObject $backend2 - */ - $backend2 = $this->getMock('\OC_User_Dummy'); - $backend2->expects($this->once()) - ->method('getUsers') - ->with($this->equalTo('fo'), $this->equalTo(1), $this->equalTo(0)) - ->will($this->returnValue(array('foo3'))); - - $manager = new \OC\User\Manager(); - $manager->registerBackend($backend1); - $manager->registerBackend($backend2); - - $result = $manager->search('fo', 3, 1); - $this->assertEquals(3, count($result)); - $this->assertEquals('foo1', $result[0]->getUID()); - $this->assertEquals('foo2', $result[1]->getUID()); - $this->assertEquals('foo3', $result[2]->getUID()); - } -} diff --git a/tests/lib/user/user.php b/tests/lib/user/user.php deleted file mode 100644 index ad03bd57483..00000000000 --- a/tests/lib/user/user.php +++ /dev/null @@ -1,319 +0,0 @@ - - * This file is licensed under the Affero General Public License version 3 or - * later. - * See the COPYING-README file. - */ - -namespace Test\User; - -use OC\Hooks\PublicEmitter; - -class User extends \PHPUnit_Framework_TestCase { - public function testDisplayName() { - /** - * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend - */ - $backend = $this->getMock('\OC_User_Backend'); - $backend->expects($this->once()) - ->method('getDisplayName') - ->with($this->equalTo('foo')) - ->will($this->returnValue('Foo')); - - $backend->expects($this->any()) - ->method('implementsActions') - ->with($this->equalTo(\OC_USER_BACKEND_GET_DISPLAYNAME)) - ->will($this->returnValue(true)); - - $user = new \OC\User\User('foo', $backend); - $this->assertEquals('Foo', $user->getDisplayName()); - } - - public function testDisplayNameNotSupported() { - /** - * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend - */ - $backend = $this->getMock('\OC_User_Backend'); - $backend->expects($this->never()) - ->method('getDisplayName'); - - $backend->expects($this->any()) - ->method('implementsActions') - ->with($this->equalTo(\OC_USER_BACKEND_GET_DISPLAYNAME)) - ->will($this->returnValue(false)); - - $user = new \OC\User\User('foo', $backend); - $this->assertEquals('foo', $user->getDisplayName()); - } - - public function testSetPassword() { - /** - * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend - */ - $backend = $this->getMock('\OC_User_Dummy'); - $backend->expects($this->once()) - ->method('setPassword') - ->with($this->equalTo('foo'), $this->equalTo('bar')); - - $backend->expects($this->any()) - ->method('implementsActions') - ->will($this->returnCallback(function ($actions) { - if ($actions === \OC_USER_BACKEND_SET_PASSWORD) { - return true; - } else { - return false; - } - })); - - $user = new \OC\User\User('foo', $backend); - $this->assertTrue($user->setPassword('bar')); - } - - public function testSetPasswordNotSupported() { - /** - * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend - */ - $backend = $this->getMock('\OC_User_Dummy'); - $backend->expects($this->never()) - ->method('setPassword'); - - $backend->expects($this->any()) - ->method('implementsActions') - ->will($this->returnValue(false)); - - $user = new \OC\User\User('foo', $backend); - $this->assertFalse($user->setPassword('bar')); - } - - public function testDelete() { - /** - * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend - */ - $backend = $this->getMock('\OC_User_Dummy'); - $backend->expects($this->once()) - ->method('deleteUser') - ->with($this->equalTo('foo')); - - $user = new \OC\User\User('foo', $backend); - $this->assertTrue($user->delete()); - } - - public function testCheckPassword() { - /** - * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend - */ - $backend = $this->getMock('\OC_User_Dummy'); - $backend->expects($this->once()) - ->method('checkPassword') - ->with($this->equalTo('foo'), $this->equalTo('bar')) - ->will($this->returnValue(true)); - - $backend->expects($this->any()) - ->method('implementsActions') - ->will($this->returnCallback(function ($actions) { - if ($actions === \OC_USER_BACKEND_CHECK_PASSWORD) { - return true; - } else { - return false; - } - })); - - $user = new \OC\User\User('foo', $backend); - $this->assertTrue($user->checkPassword('bar')); - } - - public function testCheckPasswordNotSupported() { - /** - * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend - */ - $backend = $this->getMock('\OC_User_Dummy'); - $backend->expects($this->never()) - ->method('checkPassword'); - - $backend->expects($this->any()) - ->method('implementsActions') - ->will($this->returnValue(false)); - - $user = new \OC\User\User('foo', $backend); - $this->assertFalse($user->checkPassword('bar')); - } - - public function testGetHome() { - /** - * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend - */ - $backend = $this->getMock('\OC_User_Dummy'); - $backend->expects($this->once()) - ->method('getHome') - ->with($this->equalTo('foo')) - ->will($this->returnValue('/home/foo')); - - $backend->expects($this->any()) - ->method('implementsActions') - ->will($this->returnCallback(function ($actions) { - if ($actions === \OC_USER_BACKEND_GET_HOME) { - return true; - } else { - return false; - } - })); - - $user = new \OC\User\User('foo', $backend); - $this->assertEquals('/home/foo', $user->getHome()); - } - - public function testGetHomeNotSupported() { - /** - * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend - */ - $backend = $this->getMock('\OC_User_Dummy'); - $backend->expects($this->never()) - ->method('getHome'); - - $backend->expects($this->any()) - ->method('implementsActions') - ->will($this->returnValue(false)); - - $user = new \OC\User\User('foo', $backend); - $this->assertEquals(\OC_Config::getValue("datadirectory", \OC::$SERVERROOT . "/data") . '/foo', $user->getHome()); - } - - public function testCanChangePassword() { - /** - * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend - */ - $backend = $this->getMock('\OC_User_Dummy'); - - $backend->expects($this->any()) - ->method('implementsActions') - ->will($this->returnCallback(function ($actions) { - if ($actions === \OC_USER_BACKEND_SET_PASSWORD) { - return true; - } else { - return false; - } - })); - - $user = new \OC\User\User('foo', $backend); - $this->assertTrue($user->canChangePassword()); - } - - public function testCanChangePasswordNotSupported() { - /** - * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend - */ - $backend = $this->getMock('\OC_User_Dummy'); - - $backend->expects($this->any()) - ->method('implementsActions') - ->will($this->returnValue(false)); - - $user = new \OC\User\User('foo', $backend); - $this->assertFalse($user->canChangePassword()); - } - - public function testCanChangeDisplayName() { - /** - * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend - */ - $backend = $this->getMock('\OC_User_Dummy'); - - $backend->expects($this->any()) - ->method('implementsActions') - ->will($this->returnCallback(function ($actions) { - if ($actions === \OC_USER_BACKEND_SET_DISPLAYNAME) { - return true; - } else { - return false; - } - })); - - $user = new \OC\User\User('foo', $backend); - $this->assertTrue($user->canChangeDisplayName()); - } - - public function testCanChangeDisplayNameNotSupported() { - /** - * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend - */ - $backend = $this->getMock('\OC_User_Dummy'); - - $backend->expects($this->any()) - ->method('implementsActions') - ->will($this->returnValue(false)); - - $user = new \OC\User\User('foo', $backend); - $this->assertFalse($user->canChangeDisplayName()); - } - - public function testSetPasswordHooks() { - $hooksCalled = 0; - $test = $this; - - /** - * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend - */ - $backend = $this->getMock('\OC_User_Dummy'); - $backend->expects($this->once()) - ->method('setPassword'); - - /** - * @param \OC\User\User $user - * @param string $password - */ - $hook = function ($user, $password) use ($test, &$hooksCalled) { - $hooksCalled++; - $test->assertEquals('foo', $user->getUID()); - $test->assertEquals('bar', $password); - }; - - $emitter = new PublicEmitter(); - $emitter->listen('\OC\User', 'preSetPassword', $hook); - $emitter->listen('\OC\User', 'postSetPassword', $hook); - - $backend->expects($this->any()) - ->method('implementsActions') - ->will($this->returnCallback(function ($actions) { - if ($actions === \OC_USER_BACKEND_SET_PASSWORD) { - return true; - } else { - return false; - } - })); - - $user = new \OC\User\User('foo', $backend, $emitter); - - $user->setPassword('bar'); - $this->assertEquals(2, $hooksCalled); - } - - public function testDeleteHooks() { - $hooksCalled = 0; - $test = $this; - - /** - * @var \OC_User_Backend | \PHPUnit_Framework_MockObject_MockObject $backend - */ - $backend = $this->getMock('\OC_User_Dummy'); - $backend->expects($this->once()) - ->method('deleteUser'); - - /** - * @param \OC\User\User $user - */ - $hook = function ($user) use ($test, &$hooksCalled) { - $hooksCalled++; - $test->assertEquals('foo', $user->getUID()); - }; - - $emitter = new PublicEmitter(); - $emitter->listen('\OC\User', 'preDelete', $hook); - $emitter->listen('\OC\User', 'postDelete', $hook); - - $user = new \OC\User\User('foo', $backend, $emitter); - $this->assertTrue($user->delete()); - $this->assertEquals(2, $hooksCalled); - } -}