Add dispatcher events to User and Group objects

Signed-off-by: Joas Schilling <coding@schilljs.com>
This commit is contained in:
Joas Schilling 2019-02-22 13:07:26 +01:00 committed by Julius Härtl
parent 55ae7fa2a4
commit 01b4db62fb
No known key found for this signature in database
GPG Key ID: 4C614C6ED2CDE6DF
21 changed files with 332 additions and 256 deletions

View File

@ -27,6 +27,7 @@ namespace OCA\Files\Tests\Service;
use OC\Tags; use OC\Tags;
use OCA\Files\Service\TagService; use OCA\Files\Service\TagService;
use OCP\Activity\IManager; use OCP\Activity\IManager;
use OCP\IUser;
use OCP\IUserSession; use OCP\IUserSession;
use Symfony\Component\EventDispatcher\EventDispatcherInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
@ -75,8 +76,7 @@ class TagServiceTest extends \Test\TestCase {
\OC::$server->getUserManager()->createUser($this->user, 'test'); \OC::$server->getUserManager()->createUser($this->user, 'test');
\OC_User::setUserId($this->user); \OC_User::setUserId($this->user);
\OC_Util::setupFS($this->user); \OC_Util::setupFS($this->user);
/** @var \OCP\IUser */ $user = $this->createMock(IUser::class);
$user = new \OC\User\User($this->user, null);
/** /**
* @var \OCP\IUserSession * @var \OCP\IUserSession
*/ */

View File

@ -64,7 +64,7 @@ class UserGlobalStoragesServiceTest extends GlobalStoragesServiceTest {
$this->globalStoragesService = $this->service; $this->globalStoragesService = $this->service;
$this->user = new \OC\User\User(self::USER_ID, null); $this->user = $this->createMock(IUser::class);
/** @var \OCP\IUserSession|\PHPUnit_Framework_MockObject_MockObject $userSession */ /** @var \OCP\IUserSession|\PHPUnit_Framework_MockObject_MockObject $userSession */
$userSession = $this->createMock(IUserSession::class); $userSession = $this->createMock(IUserSession::class);
$userSession $userSession

View File

@ -1299,7 +1299,7 @@ class View {
if ($owner instanceof IUser) { if ($owner instanceof IUser) {
return $owner; return $owner;
} else { } else {
return new User($ownerId, null); return new User($ownerId, null, \OC::$server->getEventDispatcher());
} }
} }

View File

@ -31,55 +31,50 @@
namespace OC\Group; namespace OC\Group;
use OCP\Group\Backend\IHideFromCollaborationBackend; use OCP\Group\Backend\IHideFromCollaborationBackend;
use OC\Hooks\PublicEmitter;
use OCP\GroupInterface; use OCP\GroupInterface;
use OCP\IGroup; use OCP\IGroup;
use OCP\IUser; use OCP\IUser;
use OCP\Group\Backend\ICountDisabledInGroup; use OCP\Group\Backend\ICountDisabledInGroup;
use OCP\IUserManager;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
class Group implements IGroup { class Group implements IGroup {
/** @var null|string */ /** @var null|string */
protected $displayName; protected $displayName;
/** /** @var string */
* @var string $id
*/
private $gid; private $gid;
/** /** @var \OC\User\User[] */
* @var \OC\User\User[] $users
*/
private $users = array(); private $users = array();
/** /** @var bool */
* @var bool $usersLoaded
*/
private $usersLoaded; private $usersLoaded;
/** /** @var Backend[] */
* @var \OC\Group\Backend[]|\OC\Group\Database[] $backend
*/
private $backends; private $backends;
/** @var EventDispatcherInterface */
/** private $dispatcher;
* @var \OC\Hooks\PublicEmitter $emitter /** @var \OC\User\Manager|IUserManager */
*/ private $userManager;
/** @var PublicEmitter */
private $emitter; private $emitter;
/**
* @var \OC\User\Manager $userManager
*/
private $userManager;
/** /**
* @param string $gid * @param string $gid
* @param \OC\Group\Backend[] $backends * @param Backend[] $backends
* @param \OC\User\Manager $userManager * @param EventDispatcherInterface $dispatcher
* @param \OC\Hooks\PublicEmitter $emitter * @param IUserManager $userManager
* @param PublicEmitter $emitter
* @param string $displayName * @param string $displayName
*/ */
public function __construct($gid, $backends, $userManager, $emitter = null, $displayName = null) { public function __construct(string $gid, array $backends, EventDispatcherInterface $dispatcher, IUserManager $userManager, PublicEmitter $emitter = null, ?string $displayName = null) {
$this->gid = $gid; $this->gid = $gid;
$this->backends = $backends; $this->backends = $backends;
$this->dispatcher = $dispatcher;
$this->userManager = $userManager; $this->userManager = $userManager;
$this->emitter = $emitter; $this->emitter = $emitter;
$this->displayName = $displayName; $this->displayName = $displayName;
@ -151,6 +146,10 @@ class Group implements IGroup {
return; return;
} }
$this->dispatcher->dispatch(IGroup::class . '::preAddUser', new GenericEvent($this, [
'user' => $user,
]));
if ($this->emitter) { if ($this->emitter) {
$this->emitter->emit('\OC\Group', 'preAddUser', array($this, $user)); $this->emitter->emit('\OC\Group', 'preAddUser', array($this, $user));
} }
@ -160,6 +159,11 @@ class Group implements IGroup {
if ($this->users) { if ($this->users) {
$this->users[$user->getUID()] = $user; $this->users[$user->getUID()] = $user;
} }
$this->dispatcher->dispatch(IGroup::class . '::postAddUser', new GenericEvent($this, [
'user' => $user,
]));
if ($this->emitter) { if ($this->emitter) {
$this->emitter->emit('\OC\Group', 'postAddUser', array($this, $user)); $this->emitter->emit('\OC\Group', 'postAddUser', array($this, $user));
} }
@ -175,6 +179,9 @@ class Group implements IGroup {
*/ */
public function removeUser($user) { public function removeUser($user) {
$result = false; $result = false;
$this->dispatcher->dispatch(IGroup::class . '::preRemoveUser', new GenericEvent($this, [
'user' => $user,
]));
if ($this->emitter) { if ($this->emitter) {
$this->emitter->emit('\OC\Group', 'preRemoveUser', array($this, $user)); $this->emitter->emit('\OC\Group', 'preRemoveUser', array($this, $user));
} }
@ -185,6 +192,9 @@ class Group implements IGroup {
} }
} }
if ($result) { if ($result) {
$this->dispatcher->dispatch(IGroup::class . '::postRemoveUser', new GenericEvent($this, [
'user' => $user,
]));
if ($this->emitter) { if ($this->emitter) {
$this->emitter->emit('\OC\Group', 'postRemoveUser', array($this, $user)); $this->emitter->emit('\OC\Group', 'postRemoveUser', array($this, $user));
} }
@ -292,6 +302,7 @@ class Group implements IGroup {
} }
$result = false; $result = false;
$this->dispatcher->dispatch(IGroup::class . '::preDelete', new GenericEvent($this));
if ($this->emitter) { if ($this->emitter) {
$this->emitter->emit('\OC\Group', 'preDelete', array($this)); $this->emitter->emit('\OC\Group', 'preDelete', array($this));
} }
@ -301,8 +312,11 @@ class Group implements IGroup {
$backend->deleteGroup($this->gid); $backend->deleteGroup($this->gid);
} }
} }
if ($result and $this->emitter) { if ($result) {
$this->emitter->emit('\OC\Group', 'postDelete', array($this)); $this->dispatcher->dispatch(IGroup::class . '::postDelete', new GenericEvent($this));
if ($this->emitter) {
$this->emitter->emit('\OC\Group', 'postDelete', array($this));
}
} }
return $result; return $result;
} }

View File

@ -44,6 +44,7 @@ use OCP\IGroup;
use OCP\IGroupManager; use OCP\IGroupManager;
use OCP\ILogger; use OCP\ILogger;
use OCP\IUser; use OCP\IUser;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/** /**
* Class Manager * Class Manager
@ -61,39 +62,37 @@ use OCP\IUser;
* @package OC\Group * @package OC\Group
*/ */
class Manager extends PublicEmitter implements IGroupManager { class Manager extends PublicEmitter implements IGroupManager {
/** /** @var GroupInterface[] */
* @var GroupInterface[] $backends private $backends = [];
*/
private $backends = array();
/** /** @var \OC\User\Manager */
* @var \OC\User\Manager $userManager
*/
private $userManager; private $userManager;
/** @var EventDispatcherInterface */
private $dispatcher;
/** @var ILogger */
private $logger;
/** /** @var \OC\Group\Group[] */
* @var \OC\Group\Group[] private $cachedGroups = [];
*/
private $cachedGroups = array();
/** /** @var \OC\Group\Group[] */
* @var \OC\Group\Group[] private $cachedUserGroups = [];
*/
private $cachedUserGroups = array();
/** @var \OC\SubAdmin */ /** @var \OC\SubAdmin */
private $subAdmin = null; private $subAdmin = null;
/** @var ILogger */
private $logger;
/** /**
* @param \OC\User\Manager $userManager * @param \OC\User\Manager $userManager
* @param EventDispatcherInterface $dispatcher
* @param ILogger $logger * @param ILogger $logger
*/ */
public function __construct(\OC\User\Manager $userManager, ILogger $logger) { public function __construct(\OC\User\Manager $userManager,
EventDispatcherInterface $dispatcher,
ILogger $logger) {
$this->userManager = $userManager; $this->userManager = $userManager;
$this->dispatcher = $dispatcher;
$this->logger = $logger; $this->logger = $logger;
$cachedGroups = & $this->cachedGroups; $cachedGroups = & $this->cachedGroups;
$cachedUserGroups = & $this->cachedUserGroups; $cachedUserGroups = & $this->cachedUserGroups;
$this->listen('\OC\Group', 'postDelete', function ($group) use (&$cachedGroups, &$cachedUserGroups) { $this->listen('\OC\Group', 'postDelete', function ($group) use (&$cachedGroups, &$cachedUserGroups) {
@ -101,19 +100,19 @@ class Manager extends PublicEmitter implements IGroupManager {
* @var \OC\Group\Group $group * @var \OC\Group\Group $group
*/ */
unset($cachedGroups[$group->getGID()]); unset($cachedGroups[$group->getGID()]);
$cachedUserGroups = array(); $cachedUserGroups = [];
}); });
$this->listen('\OC\Group', 'postAddUser', function ($group) use (&$cachedUserGroups) { $this->listen('\OC\Group', 'postAddUser', function ($group) use (&$cachedUserGroups) {
/** /**
* @var \OC\Group\Group $group * @var \OC\Group\Group $group
*/ */
$cachedUserGroups = array(); $cachedUserGroups = [];
}); });
$this->listen('\OC\Group', 'postRemoveUser', function ($group) use (&$cachedUserGroups) { $this->listen('\OC\Group', 'postRemoveUser', function ($group) use (&$cachedUserGroups) {
/** /**
* @var \OC\Group\Group $group * @var \OC\Group\Group $group
*/ */
$cachedUserGroups = array(); $cachedUserGroups = [];
}); });
} }
@ -144,7 +143,7 @@ class Manager extends PublicEmitter implements IGroupManager {
} }
public function clearBackends() { public function clearBackends() {
$this->backends = array(); $this->backends = [];
$this->clearCaches(); $this->clearCaches();
} }
@ -158,8 +157,8 @@ class Manager extends PublicEmitter implements IGroupManager {
protected function clearCaches() { protected function clearCaches() {
$this->cachedGroups = array(); $this->cachedGroups = [];
$this->cachedUserGroups = array(); $this->cachedUserGroups = [];
} }
/** /**
@ -179,7 +178,7 @@ class Manager extends PublicEmitter implements IGroupManager {
* @return \OCP\IGroup * @return \OCP\IGroup
*/ */
protected function getGroupObject($gid, $displayName = null) { protected function getGroupObject($gid, $displayName = null) {
$backends = array(); $backends = [];
foreach ($this->backends as $backend) { foreach ($this->backends as $backend) {
if ($backend->implementsActions(\OC\Group\Backend::GROUP_DETAILS)) { if ($backend->implementsActions(\OC\Group\Backend::GROUP_DETAILS)) {
$groupData = $backend->getGroupDetails($gid); $groupData = $backend->getGroupDetails($gid);
@ -197,7 +196,7 @@ class Manager extends PublicEmitter implements IGroupManager {
if (count($backends) === 0) { if (count($backends) === 0) {
return null; return null;
} }
$this->cachedGroups[$gid] = new Group($gid, $backends, $this->userManager, $this, $displayName); $this->cachedGroups[$gid] = new Group($gid, $backends, $this->dispatcher, $this->userManager, $this, $displayName);
return $this->cachedGroups[$gid]; return $this->cachedGroups[$gid];
} }
@ -239,7 +238,7 @@ class Manager extends PublicEmitter implements IGroupManager {
* @return \OC\Group\Group[] * @return \OC\Group\Group[]
*/ */
public function search($search, $limit = null, $offset = null) { public function search($search, $limit = null, $offset = null) {
$groups = array(); $groups = [];
foreach ($this->backends as $backend) { foreach ($this->backends as $backend) {
$groupIds = $backend->getGroups($search, $limit, $offset); $groupIds = $backend->getGroups($search, $limit, $offset);
foreach ($groupIds as $groupId) { foreach ($groupIds as $groupId) {
@ -276,7 +275,7 @@ class Manager extends PublicEmitter implements IGroupManager {
if (isset($this->cachedUserGroups[$uid])) { if (isset($this->cachedUserGroups[$uid])) {
return $this->cachedUserGroups[$uid]; return $this->cachedUserGroups[$uid];
} }
$groups = array(); $groups = [];
foreach ($this->backends as $backend) { foreach ($this->backends as $backend) {
$groupIds = $backend->getUserGroups($uid); $groupIds = $backend->getUserGroups($uid);
if (is_array($groupIds)) { if (is_array($groupIds)) {
@ -351,11 +350,11 @@ class Manager extends PublicEmitter implements IGroupManager {
public function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) { public function displayNamesInGroup($gid, $search = '', $limit = -1, $offset = 0) {
$group = $this->get($gid); $group = $this->get($gid);
if(is_null($group)) { if(is_null($group)) {
return array(); return [];
} }
$search = trim($search); $search = trim($search);
$groupUsers = array(); $groupUsers = [];
if(!empty($search)) { if(!empty($search)) {
// only user backends have the capability to do a complex search for users // only user backends have the capability to do a complex search for users
@ -384,7 +383,7 @@ class Manager extends PublicEmitter implements IGroupManager {
$groupUsers = $group->searchUsers('', $limit, $offset); $groupUsers = $group->searchUsers('', $limit, $offset);
} }
$matchingUsers = array(); $matchingUsers = [];
foreach($groupUsers as $groupUser) { foreach($groupUsers as $groupUser) {
$matchingUsers[$groupUser->getUID()] = $groupUser->getDisplayName(); $matchingUsers[$groupUser->getUID()] = $groupUser->getDisplayName();
} }

View File

@ -316,14 +316,13 @@ class Server extends ServerContainer implements IServerContainer {
$this->registerAlias('LazyRootFolder', \OCP\Files\IRootFolder::class); $this->registerAlias('LazyRootFolder', \OCP\Files\IRootFolder::class);
$this->registerService(\OC\User\Manager::class, function (Server $c) { $this->registerService(\OC\User\Manager::class, function (Server $c) {
$config = $c->getConfig(); return new \OC\User\Manager($c->getConfig(), $c->getEventDispatcher());
return new \OC\User\Manager($config);
}); });
$this->registerAlias('UserManager', \OC\User\Manager::class); $this->registerAlias('UserManager', \OC\User\Manager::class);
$this->registerAlias(\OCP\IUserManager::class, \OC\User\Manager::class); $this->registerAlias(\OCP\IUserManager::class, \OC\User\Manager::class);
$this->registerService(\OCP\IGroupManager::class, function (Server $c) { $this->registerService(\OCP\IGroupManager::class, function (Server $c) {
$groupManager = new \OC\Group\Manager($this->getUserManager(), $this->getLogger()); $groupManager = new \OC\Group\Manager($this->getUserManager(), $c->getEventDispatcher(), $this->getLogger());
$groupManager->listen('\OC\Group', 'preCreate', function ($gid) { $groupManager->listen('\OC\Group', 'preCreate', function ($gid) {
\OC_Hook::emit('OC_Group', 'pre_createGroup', array('run' => true, 'gid' => $gid)); \OC_Hook::emit('OC_Group', 'pre_createGroup', array('run' => true, 'gid' => $gid));
}); });
@ -1095,6 +1094,8 @@ class Server extends ServerContainer implements IServerContainer {
$this->registerAlias(\OCP\Collaboration\AutoComplete\IManager::class, \OC\Collaboration\AutoComplete\Manager::class); $this->registerAlias(\OCP\Collaboration\AutoComplete\IManager::class, \OC\Collaboration\AutoComplete\Manager::class);
$this->registerAlias(\OCP\Collaboration\Resources\IManager::class, \OC\Collaboration\Resources\Manager::class);
$this->registerService('SettingsManager', function (Server $c) { $this->registerService('SettingsManager', function (Server $c) {
$manager = new \OC\Settings\Manager( $manager = new \OC\Settings\Manager(
$c->getLogger(), $c->getLogger(),

View File

@ -39,6 +39,7 @@ use OCP\IUserBackend;
use OCP\IUserManager; use OCP\IUserManager;
use OCP\IConfig; use OCP\IConfig;
use OCP\UserInterface; use OCP\UserInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/** /**
* Class Manager * Class Manager
@ -68,16 +69,14 @@ class Manager extends PublicEmitter implements IUserManager {
*/ */
private $cachedUsers = array(); private $cachedUsers = array();
/** /** @var IConfig */
* @var \OCP\IConfig $config
*/
private $config; private $config;
/** @var EventDispatcherInterface */
private $dispatcher;
/** public function __construct(IConfig $config, EventDispatcherInterface $dispatcher) {
* @param \OCP\IConfig $config
*/
public function __construct(IConfig $config) {
$this->config = $config; $this->config = $config;
$this->dispatcher = $dispatcher;
$cachedUsers = &$this->cachedUsers; $cachedUsers = &$this->cachedUsers;
$this->listen('\OC\User', 'postDelete', function ($user) use (&$cachedUsers) { $this->listen('\OC\User', 'postDelete', function ($user) use (&$cachedUsers) {
/** @var \OC\User\User $user */ /** @var \OC\User\User $user */
@ -156,7 +155,7 @@ class Manager extends PublicEmitter implements IUserManager {
return $this->cachedUsers[$uid]; return $this->cachedUsers[$uid];
} }
$user = new User($uid, $backend, $this, $this->config); $user = new User($uid, $backend, $this->dispatcher, $this, $this->config);
if ($cacheUser) { if ($cacheUser) {
$this->cachedUsers[$uid] = $user; $this->cachedUsers[$uid] = $user;
} }

View File

@ -42,30 +42,34 @@ use OCP\IUser;
use OCP\IConfig; use OCP\IConfig;
use OCP\UserInterface; use OCP\UserInterface;
use \OCP\IUserBackend; use \OCP\IUserBackend;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent;
class User implements IUser { class User implements IUser {
/** @var string $uid */ /** @var string */
private $uid; private $uid;
/** @var string $displayName */ /** @var string */
private $displayName; private $displayName;
/** @var UserInterface $backend */ /** @var UserInterface|null */
private $backend; private $backend;
/** @var EventDispatcherInterface */
private $dispatcher;
/** @var bool $enabled */ /** @var bool */
private $enabled; private $enabled;
/** @var Emitter|Manager $emitter */ /** @var Emitter|Manager */
private $emitter; private $emitter;
/** @var string $home */ /** @var string */
private $home; private $home;
/** @var int $lastLogin */ /** @var int */
private $lastLogin; private $lastLogin;
/** @var \OCP\IConfig $config */ /** @var \OCP\IConfig */
private $config; private $config;
/** @var IAvatarManager */ /** @var IAvatarManager */
@ -74,16 +78,10 @@ class User implements IUser {
/** @var IURLGenerator */ /** @var IURLGenerator */
private $urlGenerator; private $urlGenerator;
/** public function __construct(string $uid, ?UserInterface $backend, EventDispatcherInterface $dispatcher, $emitter = null, IConfig $config = null, $urlGenerator = null) {
* @param string $uid
* @param UserInterface $backend
* @param \OC\Hooks\Emitter $emitter
* @param IConfig|null $config
* @param IURLGenerator $urlGenerator
*/
public function __construct($uid, $backend, $emitter = null, IConfig $config = null, $urlGenerator = null) {
$this->uid = $uid; $this->uid = $uid;
$this->backend = $backend; $this->backend = $backend;
$this->dispatcher = $dispatcher;
$this->emitter = $emitter; $this->emitter = $emitter;
if(is_null($config)) { if(is_null($config)) {
$config = \OC::$server->getConfig(); $config = \OC::$server->getConfig();
@ -115,7 +113,7 @@ class User implements IUser {
public function getDisplayName() { public function getDisplayName() {
if (!isset($this->displayName)) { if (!isset($this->displayName)) {
$displayName = ''; $displayName = '';
if ($this->backend and $this->backend->implementsActions(Backend::GET_DISPLAYNAME)) { if ($this->backend && $this->backend->implementsActions(Backend::GET_DISPLAYNAME)) {
// get display name and strip whitespace from the beginning and end of it // get display name and strip whitespace from the beginning and end of it
$backendDisplayName = $this->backend->getDisplayName($this->uid); $backendDisplayName = $this->backend->getDisplayName($this->uid);
if (is_string($backendDisplayName)) { if (is_string($backendDisplayName)) {
@ -199,6 +197,7 @@ class User implements IUser {
* @return bool * @return bool
*/ */
public function delete() { public function delete() {
$this->dispatcher->dispatch(IUser::class . '::preDelete', new GenericEvent($this));
if ($this->emitter) { if ($this->emitter) {
$this->emitter->emit('\OC\User', 'preDelete', array($this)); $this->emitter->emit('\OC\User', 'preDelete', array($this));
} }
@ -243,6 +242,7 @@ class User implements IUser {
$accountManager = \OC::$server->query(AccountManager::class); $accountManager = \OC::$server->query(AccountManager::class);
$accountManager->deleteUser($this); $accountManager->deleteUser($this);
$this->dispatcher->dispatch(IUser::class . '::postDelete', new GenericEvent($this));
if ($this->emitter) { if ($this->emitter) {
$this->emitter->emit('\OC\User', 'postDelete', array($this)); $this->emitter->emit('\OC\User', 'postDelete', array($this));
} }
@ -258,11 +258,19 @@ class User implements IUser {
* @return bool * @return bool
*/ */
public function setPassword($password, $recoveryPassword = null) { public function setPassword($password, $recoveryPassword = null) {
$this->dispatcher->dispatch(IUser::class . '::preSetPassword', new GenericEvent($this, [
'password' => $password,
'recoveryPassword' => $recoveryPassword,
]));
if ($this->emitter) { if ($this->emitter) {
$this->emitter->emit('\OC\User', 'preSetPassword', array($this, $password, $recoveryPassword)); $this->emitter->emit('\OC\User', 'preSetPassword', array($this, $password, $recoveryPassword));
} }
if ($this->backend->implementsActions(Backend::SET_PASSWORD)) { if ($this->backend->implementsActions(Backend::SET_PASSWORD)) {
$result = $this->backend->setPassword($this->uid, $password); $result = $this->backend->setPassword($this->uid, $password);
$this->dispatcher->dispatch(IUser::class . '::postSetPassword', new GenericEvent($this, [
'password' => $password,
'recoveryPassword' => $recoveryPassword,
]));
if ($this->emitter) { if ($this->emitter) {
$this->emitter->emit('\OC\User', 'postSetPassword', array($this, $password, $recoveryPassword)); $this->emitter->emit('\OC\User', 'postSetPassword', array($this, $password, $recoveryPassword));
} }
@ -455,6 +463,11 @@ class User implements IUser {
} }
public function triggerChange($feature, $value = null, $oldValue = null) { public function triggerChange($feature, $value = null, $oldValue = null) {
$this->dispatcher->dispatch(IUser::class . '::changeUser', new GenericEvent($this, [
'feature' => $feature,
'value' => $value,
'oldValue' => $oldValue,
]));
if ($this->emitter) { if ($this->emitter) {
$this->emitter->emit('\OC\User', 'changeUser', array($this, $feature, $value, $oldValue)); $this->emitter->emit('\OC\User', 'changeUser', array($this, $feature, $value, $oldValue));
} }

View File

@ -17,7 +17,9 @@ use OCP\App\AppPathNotFoundException;
use OCP\App\IAppManager; use OCP\App\IAppManager;
use OCP\ICache; use OCP\ICache;
use OCP\ICacheFactory; use OCP\ICacheFactory;
use OCP\IGroup;
use OCP\IGroupManager; use OCP\IGroupManager;
use OCP\IUser;
use OCP\IUserSession; use OCP\IUserSession;
use OCP\IAppConfig; use OCP\IAppConfig;
use OCP\IConfig; use OCP\IConfig;
@ -144,10 +146,14 @@ class AppManagerTest extends TestCase {
} }
public function testEnableAppForGroups() { public function testEnableAppForGroups() {
$groups = array( $group1 = $this->createMock(IGroup::class);
new Group('group1', array(), null), $group1->method('getGID')
new Group('group2', array(), null) ->willReturn('group1');
); $group2 = $this->createMock(IGroup::class);
$group2->method('getGID')
->willReturn('group2');
$groups = [$group1, $group2];
$this->expectClearCache(); $this->expectClearCache();
/** @var AppManager|\PHPUnit_Framework_MockObject_MockObject $manager */ /** @var AppManager|\PHPUnit_Framework_MockObject_MockObject $manager */
@ -187,10 +193,14 @@ class AppManagerTest extends TestCase {
* @param array $appInfo * @param array $appInfo
*/ */
public function testEnableAppForGroupsAllowedTypes(array $appInfo) { public function testEnableAppForGroupsAllowedTypes(array $appInfo) {
$groups = array( $group1 = $this->createMock(IGroup::class);
new Group('group1', array(), null), $group1->method('getGID')
new Group('group2', array(), null) ->willReturn('group1');
); $group2 = $this->createMock(IGroup::class);
$group2->method('getGID')
->willReturn('group2');
$groups = [$group1, $group2];
$this->expectClearCache(); $this->expectClearCache();
/** @var AppManager|\PHPUnit_Framework_MockObject_MockObject $manager */ /** @var AppManager|\PHPUnit_Framework_MockObject_MockObject $manager */
@ -237,10 +247,14 @@ class AppManagerTest extends TestCase {
* @expectedExceptionMessage test can't be enabled for groups. * @expectedExceptionMessage test can't be enabled for groups.
*/ */
public function testEnableAppForGroupsForbiddenTypes($type) { public function testEnableAppForGroupsForbiddenTypes($type) {
$groups = array( $group1 = $this->createMock(IGroup::class);
new Group('group1', array(), null), $group1->method('getGID')
new Group('group2', array(), null) ->willReturn('group1');
); $group2 = $this->createMock(IGroup::class);
$group2->method('getGID')
->willReturn('group2');
$groups = [$group1, $group2];
/** @var AppManager|\PHPUnit_Framework_MockObject_MockObject $manager */ /** @var AppManager|\PHPUnit_Framework_MockObject_MockObject $manager */
$manager = $this->getMockBuilder(AppManager::class) $manager = $this->getMockBuilder(AppManager::class)
@ -284,10 +298,11 @@ class AppManagerTest extends TestCase {
} }
private function newUser($uid) { private function newUser($uid) {
$config = $this->createMock(IConfig::class); $user = $this->createMock(IUser::class);
$urlgenerator = $this->createMock(IURLGenerator::class); $user->method('getUID')
->willReturn($uid);
return new User($uid, null, null, $config, $urlgenerator); return $user;
} }
public function testIsEnabledForUserEnabled() { public function testIsEnabledForUserEnabled() {

View File

@ -332,7 +332,7 @@ class CacheTest extends \Test\TestCase {
$userId = $this->getUniqueId('user'); $userId = $this->getUniqueId('user');
\OC::$server->getUserManager()->createUser($userId, $userId); \OC::$server->getUserManager()->createUser($userId, $userId);
$this->loginAsUser($userId); $this->loginAsUser($userId);
$user = new \OC\User\User($userId, null); $user = new \OC\User\User($userId, null, \OC::$server->getEventDispatcher());
$file1 = 'folder'; $file1 = 'folder';
$file2 = 'folder/foobar'; $file2 = 'folder/foobar';
@ -402,7 +402,7 @@ class CacheTest extends \Test\TestCase {
$userId = static::getUniqueID('user'); $userId = static::getUniqueID('user');
\OC::$server->getUserManager()->createUser($userId, $userId); \OC::$server->getUserManager()->createUser($userId, $userId);
static::loginAsUser($userId); static::loginAsUser($userId);
$user = new \OC\User\User($userId, null); $user = new \OC\User\User($userId, null, \OC::$server->getEventDispatcher());
$file1 = 'folder'; $file1 = 'folder';
$file2 = 'folder/foobar'; $file2 = 'folder/foobar';

View File

@ -14,6 +14,7 @@ use OC\Files\Storage\Home;
use OC\Files\Storage\Temporary; use OC\Files\Storage\Temporary;
use OC\User\User; use OC\User\User;
use OCP\IConfig; use OCP\IConfig;
use OCP\IUser;
use Test\TestCase; use Test\TestCase;
use Test\Traits\UserTrait; use Test\Traits\UserTrait;
@ -29,9 +30,15 @@ class FileInfoTest extends TestCase {
} }
public function testIsMountedHomeStorage() { public function testIsMountedHomeStorage() {
$user = $this->createMock(IUser::class);
$user->method('getUID')
->willReturn('foo');
$user->method('getHome')
->willReturn('foo');
$fileInfo = new FileInfo( $fileInfo = new FileInfo(
'', '',
new Home(['user' => new User('foo', $this->userBackend, null, $this->config)]), new Home(['user' => $user]),
'', [], null); '', [], null);
$this->assertFalse($fileInfo->isMounted()); $this->assertFalse($fileInfo->isMounted());
} }

View File

@ -45,7 +45,7 @@ class IntegrationTest extends \Test\TestCase {
\OC_Hook::clear('OC_Filesystem'); \OC_Hook::clear('OC_Filesystem');
$user = new User($this->getUniqueID('user'), new \Test\Util\User\Dummy); $user = new User($this->getUniqueID('user'), new \Test\Util\User\Dummy, \OC::$server->getEventDispatcher());
$this->loginAsUser($user->getUID()); $this->loginAsUser($user->getUID());
$this->view = new View(); $this->view = new View();

View File

@ -18,6 +18,7 @@ use OCP\Files\Storage;
use OCP\IConfig; use OCP\IConfig;
use OCP\ILogger; use OCP\ILogger;
use OCP\IURLGenerator; use OCP\IURLGenerator;
use OCP\IUser;
use OCP\IUserManager; use OCP\IUserManager;
use OCP\Files\NotFoundException; use OCP\Files\NotFoundException;
@ -45,14 +46,7 @@ abstract class NodeTest extends \Test\TestCase {
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
$config = $this->getMockBuilder(IConfig::class) $this->user = $this->createMock(IUser::class);
->disableOriginalConstructor()
->getMock();
$urlGenerator = $this->getMockBuilder(IURLGenerator
::class)
->disableOriginalConstructor()
->getMock();
$this->user = new \OC\User\User('', new \Test\Util\User\Dummy, null, $config, $urlGenerator);
$this->manager = $this->getMockBuilder(Manager::class) $this->manager = $this->getMockBuilder(Manager::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();

View File

@ -39,14 +39,7 @@ class RootTest extends \Test\TestCase {
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
$config = $this->getMockBuilder(IConfig::class) $this->user = $this->createMock(IUser::class);
->disableOriginalConstructor()
->getMock();
$urlgenerator = $this->getMockBuilder(IURLGenerator::class)
->disableOriginalConstructor()
->getMock();
$this->user = new \OC\User\User('', new \Test\Util\User\Dummy, null, $config, $urlgenerator);
$this->manager = $this->getMockBuilder(Manager::class) $this->manager = $this->getMockBuilder(Manager::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();

View File

@ -12,8 +12,19 @@ namespace Test\Group;
use OC\User\User; use OC\User\User;
use OCP\IConfig; use OCP\IConfig;
use OCP\IURLGenerator; use OCP\IURLGenerator;
use OCP\IUser;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
class GroupTest extends \Test\TestCase { class GroupTest extends \Test\TestCase {
/** @var EventDispatcherInterface|MockObject */
protected $dispatcher;
public function setUp() {
parent::setUp();
$this->dispatcher = $this->createMock(EventDispatcherInterface::class);
}
/** /**
* @param string $uid * @param string $uid
@ -21,14 +32,13 @@ class GroupTest extends \Test\TestCase {
* @return User * @return User
*/ */
private function newUser($uid, \OC\User\Backend $backend) { private function newUser($uid, \OC\User\Backend $backend) {
$config = $this->getMockBuilder(IConfig::class) $user = $this->createMock(IUser::class);
->disableOriginalConstructor() $user->method('getUID')
->getMock(); ->willReturn($uid);
$urlgenerator = $this->getMockBuilder(IURLGenerator::class) $user->method('getBackend')
->disableOriginalConstructor() ->willReturn($backend);
->getMock();
return new User($uid, $backend, null, $config, $urlgenerator); return $user;
} }
/** /**
@ -59,7 +69,7 @@ class GroupTest extends \Test\TestCase {
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$userManager = $this->getUserManager(); $userManager = $this->getUserManager();
$group = new \OC\Group\Group('group1', array($backend), $userManager); $group = new \OC\Group\Group('group1', array($backend), $this->dispatcher, $userManager);
$backend->expects($this->once()) $backend->expects($this->once())
->method('usersInGroup') ->method('usersInGroup')
@ -83,7 +93,7 @@ class GroupTest extends \Test\TestCase {
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$userManager = $this->getUserManager(); $userManager = $this->getUserManager();
$group = new \OC\Group\Group('group1', array($backend1, $backend2), $userManager); $group = new \OC\Group\Group('group1', array($backend1, $backend2), $this->dispatcher, $userManager);
$backend1->expects($this->once()) $backend1->expects($this->once())
->method('usersInGroup') ->method('usersInGroup')
@ -114,7 +124,7 @@ class GroupTest extends \Test\TestCase {
$userBackend = $this->getMockBuilder('\OC\User\Backend') $userBackend = $this->getMockBuilder('\OC\User\Backend')
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$group = new \OC\Group\Group('group1', array($backend), $userManager); $group = new \OC\Group\Group('group1', array($backend), $this->dispatcher, $userManager);
$backend->expects($this->once()) $backend->expects($this->once())
->method('inGroup') ->method('inGroup')
@ -135,7 +145,7 @@ class GroupTest extends \Test\TestCase {
$userBackend = $this->getMockBuilder(\OC\User\Backend::class) $userBackend = $this->getMockBuilder(\OC\User\Backend::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$group = new \OC\Group\Group('group1', array($backend1, $backend2), $userManager); $group = new \OC\Group\Group('group1', array($backend1, $backend2), $this->dispatcher, $userManager);
$backend1->expects($this->once()) $backend1->expects($this->once())
->method('inGroup') ->method('inGroup')
@ -158,7 +168,7 @@ class GroupTest extends \Test\TestCase {
$userBackend = $this->getMockBuilder('\OC\User\Backend') $userBackend = $this->getMockBuilder('\OC\User\Backend')
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$group = new \OC\Group\Group('group1', array($backend), $userManager); $group = new \OC\Group\Group('group1', array($backend), $this->dispatcher, $userManager);
$backend->expects($this->once()) $backend->expects($this->once())
->method('inGroup') ->method('inGroup')
@ -183,7 +193,7 @@ class GroupTest extends \Test\TestCase {
$userBackend = $this->getMockBuilder('\OC\User\Backend') $userBackend = $this->getMockBuilder('\OC\User\Backend')
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$group = new \OC\Group\Group('group1', array($backend), $userManager); $group = new \OC\Group\Group('group1', array($backend), $this->dispatcher, $userManager);
$backend->expects($this->once()) $backend->expects($this->once())
->method('inGroup') ->method('inGroup')
@ -207,7 +217,7 @@ class GroupTest extends \Test\TestCase {
$userBackend = $this->getMockBuilder('\OC\User\Backend') $userBackend = $this->getMockBuilder('\OC\User\Backend')
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$group = new \OC\Group\Group('group1', array($backend), $userManager); $group = new \OC\Group\Group('group1', array($backend), $this->dispatcher, $userManager);
$backend->expects($this->once()) $backend->expects($this->once())
->method('inGroup') ->method('inGroup')
@ -232,7 +242,7 @@ class GroupTest extends \Test\TestCase {
$userBackend = $this->getMockBuilder(\OC\User\Backend::class) $userBackend = $this->getMockBuilder(\OC\User\Backend::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$group = new \OC\Group\Group('group1', array($backend), $userManager); $group = new \OC\Group\Group('group1', array($backend), $this->dispatcher, $userManager);
$backend->expects($this->once()) $backend->expects($this->once())
->method('inGroup') ->method('inGroup')
@ -259,7 +269,7 @@ class GroupTest extends \Test\TestCase {
$userBackend = $this->getMockBuilder('\OC\User\Backend') $userBackend = $this->getMockBuilder('\OC\User\Backend')
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$group = new \OC\Group\Group('group1', array($backend1, $backend2), $userManager); $group = new \OC\Group\Group('group1', array($backend1, $backend2), $this->dispatcher, $userManager);
$backend1->expects($this->once()) $backend1->expects($this->once())
->method('inGroup') ->method('inGroup')
@ -293,7 +303,7 @@ class GroupTest extends \Test\TestCase {
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$userManager = $this->getUserManager(); $userManager = $this->getUserManager();
$group = new \OC\Group\Group('group1', array($backend), $userManager); $group = new \OC\Group\Group('group1', array($backend), $this->dispatcher, $userManager);
$backend->expects($this->once()) $backend->expects($this->once())
->method('usersInGroup') ->method('usersInGroup')
@ -315,7 +325,7 @@ class GroupTest extends \Test\TestCase {
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$userManager = $this->getUserManager(); $userManager = $this->getUserManager();
$group = new \OC\Group\Group('group1', array($backend1, $backend2), $userManager); $group = new \OC\Group\Group('group1', array($backend1, $backend2), $this->dispatcher, $userManager);
$backend1->expects($this->once()) $backend1->expects($this->once())
->method('usersInGroup') ->method('usersInGroup')
@ -338,7 +348,7 @@ class GroupTest extends \Test\TestCase {
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$userManager = $this->getUserManager(); $userManager = $this->getUserManager();
$group = new \OC\Group\Group('group1', array($backend), $userManager); $group = new \OC\Group\Group('group1', array($backend), $this->dispatcher, $userManager);
$backend->expects($this->once()) $backend->expects($this->once())
->method('usersInGroup') ->method('usersInGroup')
@ -360,7 +370,7 @@ class GroupTest extends \Test\TestCase {
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$userManager = $this->getUserManager(); $userManager = $this->getUserManager();
$group = new \OC\Group\Group('group1', array($backend1, $backend2), $userManager); $group = new \OC\Group\Group('group1', array($backend1, $backend2), $this->dispatcher, $userManager);
$backend1->expects($this->once()) $backend1->expects($this->once())
->method('usersInGroup') ->method('usersInGroup')
@ -385,7 +395,7 @@ class GroupTest extends \Test\TestCase {
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$userManager = $this->getUserManager(); $userManager = $this->getUserManager();
$group = new \OC\Group\Group('group1', array($backend1), $userManager); $group = new \OC\Group\Group('group1', array($backend1), $this->dispatcher, $userManager);
$backend1->expects($this->once()) $backend1->expects($this->once())
->method('countUsersInGroup') ->method('countUsersInGroup')
@ -409,7 +419,7 @@ class GroupTest extends \Test\TestCase {
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$userManager = $this->getUserManager(); $userManager = $this->getUserManager();
$group = new \OC\Group\Group('group1', array($backend1, $backend2), $userManager); $group = new \OC\Group\Group('group1', array($backend1, $backend2), $this->dispatcher, $userManager);
$backend1->expects($this->once()) $backend1->expects($this->once())
->method('countUsersInGroup') ->method('countUsersInGroup')
@ -437,7 +447,7 @@ class GroupTest extends \Test\TestCase {
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$userManager = $this->getUserManager(); $userManager = $this->getUserManager();
$group = new \OC\Group\Group('group1', array($backend1), $userManager); $group = new \OC\Group\Group('group1', array($backend1), $this->dispatcher, $userManager);
$backend1->expects($this->never()) $backend1->expects($this->never())
->method('countUsersInGroup'); ->method('countUsersInGroup');
@ -455,7 +465,7 @@ class GroupTest extends \Test\TestCase {
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$userManager = $this->getUserManager(); $userManager = $this->getUserManager();
$group = new \OC\Group\Group('group1', array($backend), $userManager); $group = new \OC\Group\Group('group1', array($backend), $this->dispatcher, $userManager);
$backend->expects($this->once()) $backend->expects($this->once())
->method('deleteGroup') ->method('deleteGroup')

View File

@ -27,18 +27,23 @@ use OC\User\Manager;
use OCP\ILogger; use OCP\ILogger;
use OCP\IUser; use OCP\IUser;
use OCP\GroupInterface; use OCP\GroupInterface;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Test\TestCase; use Test\TestCase;
class ManagerTest extends TestCase { class ManagerTest extends TestCase {
/** @var Manager|\PHPUnit_Framework_MockObject_MockObject $userManager */ /** @var Manager|MockObject */
protected $userManager; protected $userManager;
/** @var ILogger|\PHPUnit_Framework_MockObject_MockObject $userManager */ /** @var EventDispatcherInterface|MockObject */
protected $dispatcher;
/** @var ILogger|MockObject */
protected $logger; protected $logger;
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
$this->userManager = $this->createMock(Manager::class); $this->userManager = $this->createMock(Manager::class);
$this->dispatcher = $this->createMock(EventDispatcherInterface::class);
$this->logger = $this->createMock(ILogger::class); $this->logger = $this->createMock(ILogger::class);
} }
@ -101,7 +106,7 @@ class ManagerTest extends TestCase {
->with('group1') ->with('group1')
->will($this->returnValue(true)); ->will($this->returnValue(true));
$manager = new \OC\Group\Manager($this->userManager, $this->logger); $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
$manager->addBackend($backend); $manager->addBackend($backend);
$group = $manager->get('group1'); $group = $manager->get('group1');
@ -110,7 +115,7 @@ class ManagerTest extends TestCase {
} }
public function testGetNoBackend() { public function testGetNoBackend() {
$manager = new \OC\Group\Manager($this->userManager, $this->logger); $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
$this->assertNull($manager->get('group1')); $this->assertNull($manager->get('group1'));
} }
@ -125,7 +130,7 @@ class ManagerTest extends TestCase {
->with('group1') ->with('group1')
->will($this->returnValue(false)); ->will($this->returnValue(false));
$manager = new \OC\Group\Manager($this->userManager, $this->logger); $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
$manager->addBackend($backend); $manager->addBackend($backend);
$this->assertNull($manager->get('group1')); $this->assertNull($manager->get('group1'));
@ -135,7 +140,7 @@ class ManagerTest extends TestCase {
$backend = new \Test\Util\Group\Dummy(); $backend = new \Test\Util\Group\Dummy();
$backend->createGroup('group1'); $backend->createGroup('group1');
$manager = new \OC\Group\Manager($this->userManager, $this->logger); $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
$manager->addBackend($backend); $manager->addBackend($backend);
$group = $manager->get('group1'); $group = $manager->get('group1');
@ -162,7 +167,7 @@ class ManagerTest extends TestCase {
->with('group1') ->with('group1')
->will($this->returnValue(true)); ->will($this->returnValue(true));
$manager = new \OC\Group\Manager($this->userManager, $this->logger); $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
$manager->addBackend($backend1); $manager->addBackend($backend1);
$manager->addBackend($backend2); $manager->addBackend($backend2);
@ -187,7 +192,7 @@ class ManagerTest extends TestCase {
$backendGroupCreated = true; $backendGroupCreated = true;
})); }));
$manager = new \OC\Group\Manager($this->userManager, $this->logger); $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
$manager->addBackend($backend); $manager->addBackend($backend);
$group = $manager->createGroup('group1'); $group = $manager->createGroup('group1');
@ -204,7 +209,7 @@ class ManagerTest extends TestCase {
$backend->expects($this->never()) $backend->expects($this->never())
->method('createGroup'); ->method('createGroup');
$manager = new \OC\Group\Manager($this->userManager, $this->logger); $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
$manager->addBackend($backend); $manager->addBackend($backend);
$group = $manager->createGroup('group1'); $group = $manager->createGroup('group1');
@ -225,7 +230,7 @@ class ManagerTest extends TestCase {
->with('group1') ->with('group1')
->will($this->returnValue(true)); ->will($this->returnValue(true));
$manager = new \OC\Group\Manager($this->userManager, $this->logger); $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
$manager->addBackend($backend); $manager->addBackend($backend);
$groups = $manager->search('1'); $groups = $manager->search('1');
@ -259,7 +264,7 @@ class ManagerTest extends TestCase {
->method('groupExists') ->method('groupExists')
->will($this->returnValue(true)); ->will($this->returnValue(true));
$manager = new \OC\Group\Manager($this->userManager, $this->logger); $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
$manager->addBackend($backend1); $manager->addBackend($backend1);
$manager->addBackend($backend2); $manager->addBackend($backend2);
@ -296,7 +301,7 @@ class ManagerTest extends TestCase {
->method('groupExists') ->method('groupExists')
->will($this->returnValue(true)); ->will($this->returnValue(true));
$manager = new \OC\Group\Manager($this->userManager, $this->logger); $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
$manager->addBackend($backend1); $manager->addBackend($backend1);
$manager->addBackend($backend2); $manager->addBackend($backend2);
@ -323,7 +328,7 @@ class ManagerTest extends TestCase {
/** @var \OC\User\Manager $userManager */ /** @var \OC\User\Manager $userManager */
$userManager = $this->createMock(Manager::class); $userManager = $this->createMock(Manager::class);
$manager = new \OC\Group\Manager($userManager, $this->logger); $manager = new \OC\Group\Manager($userManager, $this->dispatcher, $this->logger);
$manager->addBackend($backend); $manager->addBackend($backend);
$groups = $manager->search('1'); $groups = $manager->search('1');
@ -344,7 +349,7 @@ class ManagerTest extends TestCase {
->with('group1') ->with('group1')
->will($this->returnValue(true)); ->will($this->returnValue(true));
$manager = new \OC\Group\Manager($this->userManager, $this->logger); $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
$manager->addBackend($backend); $manager->addBackend($backend);
$groups = $manager->getUserGroups($this->getTestUser('user1')); $groups = $manager->getUserGroups($this->getTestUser('user1'));
@ -391,7 +396,7 @@ class ManagerTest extends TestCase {
->with('group1') ->with('group1')
->will($this->returnValue(false)); ->will($this->returnValue(false));
$manager = new \OC\Group\Manager($this->userManager, $this->logger); $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
$manager->addBackend($backend); $manager->addBackend($backend);
/** @var \OC\User\User|\PHPUnit_Framework_MockObject_MockObject $user */ /** @var \OC\User\User|\PHPUnit_Framework_MockObject_MockObject $user */
@ -417,7 +422,7 @@ class ManagerTest extends TestCase {
->method('groupExists') ->method('groupExists')
->will($this->returnValue(true)); ->will($this->returnValue(true));
$manager = new \OC\Group\Manager($this->userManager, $this->logger); $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
$manager->addBackend($backend); $manager->addBackend($backend);
$this->assertTrue($manager->isInGroup('user1', 'group1')); $this->assertTrue($manager->isInGroup('user1', 'group1'));
@ -436,7 +441,7 @@ class ManagerTest extends TestCase {
->method('groupExists') ->method('groupExists')
->will($this->returnValue(true)); ->will($this->returnValue(true));
$manager = new \OC\Group\Manager($this->userManager, $this->logger); $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
$manager->addBackend($backend); $manager->addBackend($backend);
$this->assertTrue($manager->isAdmin('user1')); $this->assertTrue($manager->isAdmin('user1'));
@ -455,7 +460,7 @@ class ManagerTest extends TestCase {
->method('groupExists') ->method('groupExists')
->will($this->returnValue(true)); ->will($this->returnValue(true));
$manager = new \OC\Group\Manager($this->userManager, $this->logger); $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
$manager->addBackend($backend); $manager->addBackend($backend);
$this->assertFalse($manager->isAdmin('user1')); $this->assertFalse($manager->isAdmin('user1'));
@ -486,7 +491,7 @@ class ManagerTest extends TestCase {
->method('groupExists') ->method('groupExists')
->will($this->returnValue(true)); ->will($this->returnValue(true));
$manager = new \OC\Group\Manager($this->userManager, $this->logger); $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
$manager->addBackend($backend1); $manager->addBackend($backend1);
$manager->addBackend($backend2); $manager->addBackend($backend2);
@ -545,7 +550,7 @@ class ManagerTest extends TestCase {
} }
})); }));
$manager = new \OC\Group\Manager($this->userManager, $this->logger); $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
$manager->addBackend($backend); $manager->addBackend($backend);
$users = $manager->displayNamesInGroup('testgroup', 'user3'); $users = $manager->displayNamesInGroup('testgroup', 'user3');
@ -605,7 +610,7 @@ class ManagerTest extends TestCase {
} }
})); }));
$manager = new \OC\Group\Manager($this->userManager, $this->logger); $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
$manager->addBackend($backend); $manager->addBackend($backend);
$users = $manager->displayNamesInGroup('testgroup', 'user3', 1); $users = $manager->displayNamesInGroup('testgroup', 'user3', 1);
@ -669,7 +674,7 @@ class ManagerTest extends TestCase {
} }
})); }));
$manager = new \OC\Group\Manager($this->userManager, $this->logger); $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
$manager->addBackend($backend); $manager->addBackend($backend);
$users = $manager->displayNamesInGroup('testgroup', 'user3', 1, 1); $users = $manager->displayNamesInGroup('testgroup', 'user3', 1, 1);
@ -709,7 +714,7 @@ class ManagerTest extends TestCase {
} }
})); }));
$manager = new \OC\Group\Manager($this->userManager, $this->logger); $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
$manager->addBackend($backend); $manager->addBackend($backend);
$users = $manager->displayNamesInGroup('testgroup', ''); $users = $manager->displayNamesInGroup('testgroup', '');
@ -748,7 +753,7 @@ class ManagerTest extends TestCase {
} }
})); }));
$manager = new \OC\Group\Manager($this->userManager, $this->logger); $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
$manager->addBackend($backend); $manager->addBackend($backend);
$users = $manager->displayNamesInGroup('testgroup', '', 1); $users = $manager->displayNamesInGroup('testgroup', '', 1);
@ -787,7 +792,7 @@ class ManagerTest extends TestCase {
} }
})); }));
$manager = new \OC\Group\Manager($this->userManager, $this->logger); $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
$manager->addBackend($backend); $manager->addBackend($backend);
$users = $manager->displayNamesInGroup('testgroup', '', 1, 1); $users = $manager->displayNamesInGroup('testgroup', '', 1, 1);
@ -815,7 +820,7 @@ class ManagerTest extends TestCase {
->with('group1') ->with('group1')
->will($this->returnValue(true)); ->will($this->returnValue(true));
$manager = new \OC\Group\Manager($this->userManager, $this->logger); $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
$manager->addBackend($backend); $manager->addBackend($backend);
// prime cache // prime cache
@ -858,7 +863,7 @@ class ManagerTest extends TestCase {
->method('removeFromGroup') ->method('removeFromGroup')
->will($this->returnValue(true)); ->will($this->returnValue(true));
$manager = new \OC\Group\Manager($this->userManager, $this->logger); $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
$manager->addBackend($backend); $manager->addBackend($backend);
// prime cache // prime cache
@ -888,7 +893,7 @@ class ManagerTest extends TestCase {
->with('user1') ->with('user1')
->will($this->returnValue(null)); ->will($this->returnValue(null));
$manager = new \OC\Group\Manager($this->userManager, $this->logger); $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
$manager->addBackend($backend); $manager->addBackend($backend);
$groups = $manager->getUserIdGroups('user1'); $groups = $manager->getUserIdGroups('user1');
@ -914,7 +919,7 @@ class ManagerTest extends TestCase {
['group2', ['gid' => 'group2']], ['group2', ['gid' => 'group2']],
])); ]));
$manager = new \OC\Group\Manager($this->userManager, $this->logger); $manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
$manager->addBackend($backend); $manager->addBackend($backend);
// group with display name // group with display name

View File

@ -21,6 +21,7 @@
*/ */
namespace Test; namespace Test;
use OCP\IUser;
use OCP\IUserSession; use OCP\IUserSession;
/** /**
@ -49,7 +50,9 @@ class TagsTest extends \Test\TestCase {
$userId = $this->getUniqueID('user_'); $userId = $this->getUniqueID('user_');
\OC::$server->getUserManager()->createUser($userId, 'pass'); \OC::$server->getUserManager()->createUser($userId, 'pass');
\OC_User::setUserId($userId); \OC_User::setUserId($userId);
$this->user = new \OC\User\User($userId, null); $this->user = $this->createMock(IUser::class);
$this->user->method('getUID')
->willReturn($userId);
$this->userSession = $this->createMock(IUserSession::class); $this->userSession = $this->createMock(IUserSession::class);
$this->userSession $this->userSession
->expects($this->any()) ->expects($this->any())
@ -292,14 +295,19 @@ class TagsTest extends \Test\TestCase {
$tagger = $this->tagMgr->load('test'); $tagger = $this->tagMgr->load('test');
$tagger->tagAs(1, $testTag); $tagger->tagAs(1, $testTag);
$otherUserId = $this->getUniqueID('user2_'); $otherUserId = $this->getUniqueID('user2_');
$otherUser = $this->createMock(IUser::class);
$otherUser->method('getUID')
->willReturn($otherUserId);
\OC::$server->getUserManager()->createUser($otherUserId, 'pass'); \OC::$server->getUserManager()->createUser($otherUserId, 'pass');
\OC_User::setUserId($otherUserId); \OC_User::setUserId($otherUserId);
$otherUserSession = $this->createMock(IUserSession::class); $otherUserSession = $this->createMock(IUserSession::class);
$otherUserSession $otherUserSession
->expects($this->any()) ->expects($this->any())
->method('getUser') ->method('getUser')
->will($this->returnValue(new \OC\User\User($otherUserId, null))); ->willReturn($otherUser);
$otherTagMgr = new \OC\TagManager($this->tagMapper, $otherUserSession); $otherTagMgr = new \OC\TagManager($this->tagMapper, $otherUserSession);
$otherTagger = $otherTagMgr->load('test'); $otherTagger = $otherTagMgr->load('test');

View File

@ -22,7 +22,7 @@
namespace Test\User; namespace Test\User;
use OC\HintException; use OC\HintException;
use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\EventDispatcher\GenericEvent; use Symfony\Component\EventDispatcher\GenericEvent;
use OC\User\User; use OC\User\User;
@ -34,7 +34,7 @@ use OC\User\User;
class DatabaseTest extends Backend { class DatabaseTest extends Backend {
/** @var array */ /** @var array */
private $users; private $users;
/** @var EventDispatcher | \PHPUnit_Framework_MockObject_MockObject */ /** @var EventDispatcherInterface|\PHPUnit_Framework_MockObject_MockObject */
private $eventDispatcher; private $eventDispatcher;
public function getUser() { public function getUser() {
@ -46,7 +46,7 @@ class DatabaseTest extends Backend {
protected function setUp() { protected function setUp() {
parent::setUp(); parent::setUp();
$this->eventDispatcher = $this->createMock(EventDispatcher::class); $this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$this->backend=new \OC\User\Database($this->eventDispatcher); $this->backend=new \OC\User\Database($this->eventDispatcher);
} }
@ -124,8 +124,8 @@ class DatabaseTest extends Backend {
$user2 = $this->getUser(); $user2 = $this->getUser();
$this->backend->createUser($user2, 'pass1'); $this->backend->createUser($user2, 'pass1');
$user1Obj = new User($user1, $this->backend); $user1Obj = new User($user1, $this->backend, $this->eventDispatcher);
$user2Obj = new User($user2, $this->backend); $user2Obj = new User($user2, $this->backend, $this->eventDispatcher);
$emailAddr1 = "$user1@nextcloud.com"; $emailAddr1 = "$user1@nextcloud.com";
$emailAddr2 = "$user2@nextcloud.com"; $emailAddr2 = "$user2@nextcloud.com";

View File

@ -13,6 +13,7 @@ use OC\User\Database;
use OC\User\Manager; use OC\User\Manager;
use OCP\IConfig; use OCP\IConfig;
use OCP\IUser; use OCP\IUser;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Test\TestCase; use Test\TestCase;
/** /**
@ -26,16 +27,19 @@ class ManagerTest extends TestCase {
/** @var IConfig */ /** @var IConfig */
private $config; private $config;
/** @var EventDispatcherInterface */
private $dispatcher;
public function setUp() { public function setUp() {
parent::setUp(); parent::setUp();
$this->config = $this->createMock(IConfig::class); $this->config = $this->createMock(IConfig::class);
$this->dispatcher = $this->createMock(EventDispatcherInterface::class);
} }
public function testGetBackends() { public function testGetBackends() {
$userDummyBackend = $this->createMock(\Test\Util\User\Dummy::class); $userDummyBackend = $this->createMock(\Test\Util\User\Dummy::class);
$manager = new \OC\User\Manager($this->config); $manager = new \OC\User\Manager($this->config, $this->dispatcher);
$manager->registerBackend($userDummyBackend); $manager->registerBackend($userDummyBackend);
$this->assertEquals([$userDummyBackend], $manager->getBackends()); $this->assertEquals([$userDummyBackend], $manager->getBackends());
$dummyDatabaseBackend = $this->createMock(Database::class); $dummyDatabaseBackend = $this->createMock(Database::class);
@ -54,7 +58,7 @@ class ManagerTest extends TestCase {
->with($this->equalTo('foo')) ->with($this->equalTo('foo'))
->will($this->returnValue(true)); ->will($this->returnValue(true));
$manager = new \OC\User\Manager($this->config); $manager = new \OC\User\Manager($this->config, $this->dispatcher);
$manager->registerBackend($backend); $manager->registerBackend($backend);
$this->assertTrue($manager->userExists('foo')); $this->assertTrue($manager->userExists('foo'));
@ -70,14 +74,14 @@ class ManagerTest extends TestCase {
->with($this->equalTo('foo')) ->with($this->equalTo('foo'))
->will($this->returnValue(false)); ->will($this->returnValue(false));
$manager = new \OC\User\Manager($this->config); $manager = new \OC\User\Manager($this->config, $this->dispatcher);
$manager->registerBackend($backend); $manager->registerBackend($backend);
$this->assertFalse($manager->userExists('foo')); $this->assertFalse($manager->userExists('foo'));
} }
public function testUserExistsNoBackends() { public function testUserExistsNoBackends() {
$manager = new \OC\User\Manager($this->config); $manager = new \OC\User\Manager($this->config, $this->dispatcher);
$this->assertFalse($manager->userExists('foo')); $this->assertFalse($manager->userExists('foo'));
} }
@ -101,7 +105,7 @@ class ManagerTest extends TestCase {
->with($this->equalTo('foo')) ->with($this->equalTo('foo'))
->will($this->returnValue(true)); ->will($this->returnValue(true));
$manager = new \OC\User\Manager($this->config); $manager = new \OC\User\Manager($this->config, $this->dispatcher);
$manager->registerBackend($backend1); $manager->registerBackend($backend1);
$manager->registerBackend($backend2); $manager->registerBackend($backend2);
@ -125,7 +129,7 @@ class ManagerTest extends TestCase {
$backend2->expects($this->never()) $backend2->expects($this->never())
->method('userExists'); ->method('userExists');
$manager = new \OC\User\Manager($this->config); $manager = new \OC\User\Manager($this->config, $this->dispatcher);
$manager->registerBackend($backend1); $manager->registerBackend($backend1);
$manager->registerBackend($backend2); $manager->registerBackend($backend2);
@ -152,7 +156,7 @@ class ManagerTest extends TestCase {
} }
})); }));
$manager = new \OC\User\Manager($this->config); $manager = new \OC\User\Manager($this->config, $this->dispatcher);
$manager->registerBackend($backend); $manager->registerBackend($backend);
$user = $manager->checkPassword('foo', 'bar'); $user = $manager->checkPassword('foo', 'bar');
@ -171,7 +175,7 @@ class ManagerTest extends TestCase {
->method('implementsActions') ->method('implementsActions')
->will($this->returnValue(false)); ->will($this->returnValue(false));
$manager = new \OC\User\Manager($this->config); $manager = new \OC\User\Manager($this->config, $this->dispatcher);
$manager->registerBackend($backend); $manager->registerBackend($backend);
$this->assertFalse($manager->checkPassword('foo', 'bar')); $this->assertFalse($manager->checkPassword('foo', 'bar'));
@ -189,7 +193,7 @@ class ManagerTest extends TestCase {
$backend->expects($this->never()) $backend->expects($this->never())
->method('loginName2UserName'); ->method('loginName2UserName');
$manager = new \OC\User\Manager($this->config); $manager = new \OC\User\Manager($this->config, $this->dispatcher);
$manager->registerBackend($backend); $manager->registerBackend($backend);
$this->assertEquals('foo', $manager->get('foo')->getUID()); $this->assertEquals('foo', $manager->get('foo')->getUID());
@ -205,7 +209,7 @@ class ManagerTest extends TestCase {
->with($this->equalTo('foo')) ->with($this->equalTo('foo'))
->will($this->returnValue(false)); ->will($this->returnValue(false));
$manager = new \OC\User\Manager($this->config); $manager = new \OC\User\Manager($this->config, $this->dispatcher);
$manager->registerBackend($backend); $manager->registerBackend($backend);
$this->assertEquals(null, $manager->get('foo')); $this->assertEquals(null, $manager->get('foo'));
@ -223,7 +227,7 @@ class ManagerTest extends TestCase {
$backend->expects($this->never()) $backend->expects($this->never())
->method('loginName2UserName'); ->method('loginName2UserName');
$manager = new \OC\User\Manager($this->config); $manager = new \OC\User\Manager($this->config, $this->dispatcher);
$manager->registerBackend($backend); $manager->registerBackend($backend);
$this->assertEquals('bLeNdEr', $manager->get('bLeNdEr')->getUID()); $this->assertEquals('bLeNdEr', $manager->get('bLeNdEr')->getUID());
@ -241,7 +245,7 @@ class ManagerTest extends TestCase {
$backend->expects($this->never()) $backend->expects($this->never())
->method('loginName2UserName'); ->method('loginName2UserName');
$manager = new \OC\User\Manager($this->config); $manager = new \OC\User\Manager($this->config, $this->dispatcher);
$manager->registerBackend($backend); $manager->registerBackend($backend);
$result = $manager->search('fo'); $result = $manager->search('fo');
@ -275,7 +279,7 @@ class ManagerTest extends TestCase {
$backend2->expects($this->never()) $backend2->expects($this->never())
->method('loginName2UserName'); ->method('loginName2UserName');
$manager = new \OC\User\Manager($this->config); $manager = new \OC\User\Manager($this->config, $this->dispatcher);
$manager->registerBackend($backend1); $manager->registerBackend($backend1);
$manager->registerBackend($backend2); $manager->registerBackend($backend2);
@ -329,7 +333,7 @@ class ManagerTest extends TestCase {
->willReturn(true); ->willReturn(true);
$manager = new \OC\User\Manager($this->config); $manager = new \OC\User\Manager($this->config, $this->dispatcher);
$manager->registerBackend($backend); $manager->registerBackend($backend);
$this->expectException(\InvalidArgumentException::class, $exception); $this->expectException(\InvalidArgumentException::class, $exception);
@ -356,7 +360,7 @@ class ManagerTest extends TestCase {
$backend->expects($this->never()) $backend->expects($this->never())
->method('loginName2UserName'); ->method('loginName2UserName');
$manager = new \OC\User\Manager($this->config); $manager = new \OC\User\Manager($this->config, $this->dispatcher);
$manager->registerBackend($backend); $manager->registerBackend($backend);
$user = $manager->createUser('foo', 'bar'); $user = $manager->createUser('foo', 'bar');
@ -383,7 +387,7 @@ class ManagerTest extends TestCase {
->with($this->equalTo('foo')) ->with($this->equalTo('foo'))
->will($this->returnValue(true)); ->will($this->returnValue(true));
$manager = new \OC\User\Manager($this->config); $manager = new \OC\User\Manager($this->config, $this->dispatcher);
$manager->registerBackend($backend); $manager->registerBackend($backend);
$manager->createUser('foo', 'bar'); $manager->createUser('foo', 'bar');
@ -404,14 +408,14 @@ class ManagerTest extends TestCase {
$backend->expects($this->never()) $backend->expects($this->never())
->method('userExists'); ->method('userExists');
$manager = new \OC\User\Manager($this->config); $manager = new \OC\User\Manager($this->config, $this->dispatcher);
$manager->registerBackend($backend); $manager->registerBackend($backend);
$this->assertFalse($manager->createUser('foo', 'bar')); $this->assertFalse($manager->createUser('foo', 'bar'));
} }
public function testCreateUserNoBackends() { public function testCreateUserNoBackends() {
$manager = new \OC\User\Manager($this->config); $manager = new \OC\User\Manager($this->config, $this->dispatcher);
$this->assertFalse($manager->createUser('foo', 'bar')); $this->assertFalse($manager->createUser('foo', 'bar'));
} }
@ -431,7 +435,7 @@ class ManagerTest extends TestCase {
->with('MyUid', 'MyPassword') ->with('MyUid', 'MyPassword')
->willReturn(false); ->willReturn(false);
$manager = new Manager($config); $manager = new Manager($config, $this->dispatcher);
$manager->createUserFromBackend('MyUid', 'MyPassword', $backend); $manager->createUserFromBackend('MyUid', 'MyPassword', $backend);
} }
@ -471,7 +475,7 @@ class ManagerTest extends TestCase {
->with($this->equalTo('foo')) ->with($this->equalTo('foo'))
->will($this->returnValue(true)); ->will($this->returnValue(true));
$manager = new \OC\User\Manager($this->config); $manager = new \OC\User\Manager($this->config, $this->dispatcher);
$manager->registerBackend($backend1); $manager->registerBackend($backend1);
$manager->registerBackend($backend2); $manager->registerBackend($backend2);
@ -479,7 +483,7 @@ class ManagerTest extends TestCase {
} }
public function testCountUsersNoBackend() { public function testCountUsersNoBackend() {
$manager = new \OC\User\Manager($this->config); $manager = new \OC\User\Manager($this->config, $this->dispatcher);
$result = $manager->countUsers(); $result = $manager->countUsers();
$this->assertTrue(is_array($result)); $this->assertTrue(is_array($result));
@ -504,7 +508,7 @@ class ManagerTest extends TestCase {
->method('getBackendName') ->method('getBackendName')
->will($this->returnValue('Mock_Test_Util_User_Dummy')); ->will($this->returnValue('Mock_Test_Util_User_Dummy'));
$manager = new \OC\User\Manager($this->config); $manager = new \OC\User\Manager($this->config, $this->dispatcher);
$manager->registerBackend($backend); $manager->registerBackend($backend);
$result = $manager->countUsers(); $result = $manager->countUsers();
@ -545,7 +549,7 @@ class ManagerTest extends TestCase {
->method('getBackendName') ->method('getBackendName')
->will($this->returnValue('Mock_Test_Util_User_Dummy')); ->will($this->returnValue('Mock_Test_Util_User_Dummy'));
$manager = new \OC\User\Manager($this->config); $manager = new \OC\User\Manager($this->config, $this->dispatcher);
$manager->registerBackend($backend1); $manager->registerBackend($backend1);
$manager->registerBackend($backend2); $manager->registerBackend($backend2);
@ -660,7 +664,7 @@ class ManagerTest extends TestCase {
->with('foo', 'login', 'lastLogin') ->with('foo', 'login', 'lastLogin')
->will($this->returnValue(0)); ->will($this->returnValue(0));
$manager = new \OC\User\Manager($config); $manager = new \OC\User\Manager($config, $this->dispatcher);
$backend = new \Test\Util\User\Dummy(); $backend = new \Test\Util\User\Dummy();
$manager->registerBackend($backend); $manager->registerBackend($backend);
@ -694,7 +698,7 @@ class ManagerTest extends TestCase {
->with($this->equalTo('uid2')) ->with($this->equalTo('uid2'))
->will($this->returnValue(true)); ->will($this->returnValue(true));
$manager = new \OC\User\Manager($config); $manager = new \OC\User\Manager($config, $this->dispatcher);
$manager->registerBackend($backend); $manager->registerBackend($backend);
$users = $manager->getByEmail('test@example.com'); $users = $manager->getByEmail('test@example.com');

View File

@ -28,6 +28,7 @@ use OCP\IUser;
use OCP\Lockdown\ILockdownManager; use OCP\Lockdown\ILockdownManager;
use OCP\Security\ICrypto; use OCP\Security\ICrypto;
use OCP\Security\ISecureRandom; use OCP\Security\ISecureRandom;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
/** /**
* @group DB * @group DB
@ -167,7 +168,7 @@ class SessionTest extends \Test\TestCase {
'getUser' 'getUser'
]) ])
->getMock(); ->getMock();
$user = new User('sepp', null); $user = new User('sepp', null, $this->createMock(EventDispatcherInterface::class));
$userSession->expects($this->once()) $userSession->expects($this->once())
->method('getUser') ->method('getUser')
->will($this->returnValue($isLoggedIn ? $user : null)); ->will($this->returnValue($isLoggedIn ? $user : null));
@ -184,7 +185,7 @@ class SessionTest extends \Test\TestCase {
$backend = $this->createMock(\Test\Util\User\Dummy::class); $backend = $this->createMock(\Test\Util\User\Dummy::class);
$user = $this->getMockBuilder(User::class)->setConstructorArgs(['foo', $backend])->getMock(); $user = $this->createMock(IUser::class);
$user->expects($this->once()) $user->expects($this->once())
->method('getUID') ->method('getUID')
->will($this->returnValue('foo')); ->will($this->returnValue('foo'));
@ -220,12 +221,12 @@ class SessionTest extends \Test\TestCase {
$mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']); $mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']);
$manager = $this->getMockBuilder(Manager::class) $manager = $this->getMockBuilder(Manager::class)
->setMethods($mockedManagerMethods) ->setMethods($mockedManagerMethods)
->setConstructorArgs([$this->config]) ->setConstructorArgs([$this->config, $this->createMock(EventDispatcherInterface::class)])
->getMock(); ->getMock();
$backend = $this->createMock(\Test\Util\User\Dummy::class); $backend = $this->createMock(\Test\Util\User\Dummy::class);
$user = $this->getMockBuilder(User::class)->setConstructorArgs(['foo', $backend])->getMock(); $user = $this->createMock(IUser::class);
$user->expects($this->any()) $user->expects($this->any())
->method('isEnabled') ->method('isEnabled')
->will($this->returnValue(true)); ->will($this->returnValue(true));
@ -271,12 +272,12 @@ class SessionTest extends \Test\TestCase {
$mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']); $mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']);
$manager = $this->getMockBuilder(Manager::class) $manager = $this->getMockBuilder(Manager::class)
->setMethods($mockedManagerMethods) ->setMethods($mockedManagerMethods)
->setConstructorArgs([$this->config]) ->setConstructorArgs([$this->config, $this->createMock(EventDispatcherInterface::class)])
->getMock(); ->getMock();
$backend = $this->createMock(\Test\Util\User\Dummy::class); $backend = $this->createMock(\Test\Util\User\Dummy::class);
$user = $this->getMockBuilder(User::class)->setConstructorArgs(['foo', $backend])->getMock(); $user = $this->createMock(IUser::class);
$user->expects($this->any()) $user->expects($this->any())
->method('isEnabled') ->method('isEnabled')
->will($this->returnValue(false)); ->will($this->returnValue(false));
@ -299,12 +300,12 @@ class SessionTest extends \Test\TestCase {
$mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']); $mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']);
$manager = $this->getMockBuilder(Manager::class) $manager = $this->getMockBuilder(Manager::class)
->setMethods($mockedManagerMethods) ->setMethods($mockedManagerMethods)
->setConstructorArgs([$this->config]) ->setConstructorArgs([$this->config, $this->createMock(EventDispatcherInterface::class)])
->getMock(); ->getMock();
$backend = $this->createMock(\Test\Util\User\Dummy::class); $backend = $this->createMock(\Test\Util\User\Dummy::class);
$userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger); $userSession = new \OC\User\Session($manager, $session, $this->timeFactory, $this->tokenProvider, $this->config, $this->random, $this->lockdownManager, $this->logger);
$user = $this->getMockBuilder(User::class)->setConstructorArgs(['foo', $backend])->getMock(); $user = $this->createMock(IUser::class);
$session->expects($this->never()) $session->expects($this->never())
->method('set'); ->method('set');
@ -535,7 +536,7 @@ class SessionTest extends \Test\TestCase {
$mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']); $mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']);
$manager = $this->getMockBuilder(Manager::class) $manager = $this->getMockBuilder(Manager::class)
->setMethods($mockedManagerMethods) ->setMethods($mockedManagerMethods)
->setConstructorArgs([$this->config]) ->setConstructorArgs([$this->config, $this->createMock(EventDispatcherInterface::class)])
->getMock(); ->getMock();
$userSession = $this->getMockBuilder(Session::class) $userSession = $this->getMockBuilder(Session::class)
//override, otherwise tests will fail because of setcookie() //override, otherwise tests will fail because of setcookie()
@ -621,7 +622,7 @@ class SessionTest extends \Test\TestCase {
$mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']); $mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']);
$manager = $this->getMockBuilder(Manager::class) $manager = $this->getMockBuilder(Manager::class)
->setMethods($mockedManagerMethods) ->setMethods($mockedManagerMethods)
->setConstructorArgs([$this->config]) ->setConstructorArgs([$this->config, $this->createMock(EventDispatcherInterface::class)])
->getMock(); ->getMock();
$userSession = $this->getMockBuilder(Session::class) $userSession = $this->getMockBuilder(Session::class)
//override, otherwise tests will fail because of setcookie() //override, otherwise tests will fail because of setcookie()
@ -681,7 +682,7 @@ class SessionTest extends \Test\TestCase {
$mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']); $mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']);
$manager = $this->getMockBuilder(Manager::class) $manager = $this->getMockBuilder(Manager::class)
->setMethods($mockedManagerMethods) ->setMethods($mockedManagerMethods)
->setConstructorArgs([$this->config]) ->setConstructorArgs([$this->config, $this->createMock(EventDispatcherInterface::class)])
->getMock(); ->getMock();
$userSession = $this->getMockBuilder(Session::class) $userSession = $this->getMockBuilder(Session::class)
//override, otherwise tests will fail because of setcookie() //override, otherwise tests will fail because of setcookie()
@ -729,7 +730,7 @@ class SessionTest extends \Test\TestCase {
$mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']); $mockedManagerMethods = array_diff($managerMethods, ['__construct', 'emit', 'listen']);
$manager = $this->getMockBuilder(Manager::class) $manager = $this->getMockBuilder(Manager::class)
->setMethods($mockedManagerMethods) ->setMethods($mockedManagerMethods)
->setConstructorArgs([$this->config]) ->setConstructorArgs([$this->config, $this->createMock(EventDispatcherInterface::class)])
->getMock(); ->getMock();
$userSession = $this->getMockBuilder(Session::class) $userSession = $this->getMockBuilder(Session::class)
//override, otherwise tests will fail because of setcookie() //override, otherwise tests will fail because of setcookie()
@ -765,8 +766,8 @@ class SessionTest extends \Test\TestCase {
public function testActiveUserAfterSetSession() { public function testActiveUserAfterSetSession() {
$users = array( $users = array(
'foo' => new User('foo', null), 'foo' => new User('foo', null, $this->createMock(EventDispatcherInterface::class)),
'bar' => new User('bar', null) 'bar' => new User('bar', null, $this->createMock(EventDispatcherInterface::class))
); );
$manager = $this->getMockBuilder('\OC\User\Manager') $manager = $this->getMockBuilder('\OC\User\Manager')

View File

@ -17,6 +17,8 @@ use OCP\IUser;
use OCP\Notification\IManager as INotificationManager; use OCP\Notification\IManager as INotificationManager;
use OCP\Notification\INotification; use OCP\Notification\INotification;
use OCP\UserInterface; use OCP\UserInterface;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Test\TestCase; use Test\TestCase;
/** /**
@ -27,6 +29,15 @@ use Test\TestCase;
* @package Test\User * @package Test\User
*/ */
class UserTest extends TestCase { class UserTest extends TestCase {
/** @var EventDispatcherInterface|MockObject */
protected $dispatcher;
public function setUp() {
parent::setUp();
$this->dispatcher = $this->createMock(EventDispatcherInterface::class);
}
public function testDisplayName() { public function testDisplayName() {
/** /**
* @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend * @var \OC\User\Backend | \PHPUnit_Framework_MockObject_MockObject $backend
@ -42,7 +53,7 @@ class UserTest extends TestCase {
->with($this->equalTo(\OC\User\Backend::GET_DISPLAYNAME)) ->with($this->equalTo(\OC\User\Backend::GET_DISPLAYNAME))
->will($this->returnValue(true)); ->will($this->returnValue(true));
$user = new User('foo', $backend); $user = new User('foo', $backend, $this->dispatcher);
$this->assertEquals('Foo', $user->getDisplayName()); $this->assertEquals('Foo', $user->getDisplayName());
} }
@ -64,7 +75,7 @@ class UserTest extends TestCase {
->with($this->equalTo(\OC\User\Backend::GET_DISPLAYNAME)) ->with($this->equalTo(\OC\User\Backend::GET_DISPLAYNAME))
->will($this->returnValue(true)); ->will($this->returnValue(true));
$user = new User('foo', $backend); $user = new User('foo', $backend, $this->dispatcher);
$this->assertEquals('foo', $user->getDisplayName()); $this->assertEquals('foo', $user->getDisplayName());
} }
@ -81,7 +92,7 @@ class UserTest extends TestCase {
->with($this->equalTo(\OC\User\Backend::GET_DISPLAYNAME)) ->with($this->equalTo(\OC\User\Backend::GET_DISPLAYNAME))
->will($this->returnValue(false)); ->will($this->returnValue(false));
$user = new User('foo', $backend); $user = new User('foo', $backend, $this->dispatcher);
$this->assertEquals('foo', $user->getDisplayName()); $this->assertEquals('foo', $user->getDisplayName());
} }
@ -104,7 +115,7 @@ class UserTest extends TestCase {
} }
})); }));
$user = new User('foo', $backend); $user = new User('foo', $backend, $this->dispatcher);
$this->assertTrue($user->setPassword('bar','')); $this->assertTrue($user->setPassword('bar',''));
} }
@ -120,7 +131,7 @@ class UserTest extends TestCase {
->method('implementsActions') ->method('implementsActions')
->will($this->returnValue(false)); ->will($this->returnValue(false));
$user = new User('foo', $backend); $user = new User('foo', $backend, $this->dispatcher);
$this->assertFalse($user->setPassword('bar','')); $this->assertFalse($user->setPassword('bar',''));
} }
@ -144,7 +155,7 @@ class UserTest extends TestCase {
} }
})); }));
$user = new User('foo', $backend); $user = new User('foo', $backend, $this->dispatcher);
$this->assertTrue($user->canChangeAvatar()); $this->assertTrue($user->canChangeAvatar());
} }
@ -168,7 +179,7 @@ class UserTest extends TestCase {
} }
})); }));
$user = new User('foo', $backend); $user = new User('foo', $backend, $this->dispatcher);
$this->assertFalse($user->canChangeAvatar()); $this->assertFalse($user->canChangeAvatar());
} }
@ -184,7 +195,7 @@ class UserTest extends TestCase {
->method('implementsActions') ->method('implementsActions')
->willReturn(false); ->willReturn(false);
$user = new User('foo', $backend); $user = new User('foo', $backend, $this->dispatcher);
$this->assertTrue($user->canChangeAvatar()); $this->assertTrue($user->canChangeAvatar());
} }
@ -197,7 +208,7 @@ class UserTest extends TestCase {
->method('deleteUser') ->method('deleteUser')
->with($this->equalTo('foo')); ->with($this->equalTo('foo'));
$user = new User('foo', $backend); $user = new User('foo', $backend, $this->dispatcher);
$this->assertTrue($user->delete()); $this->assertTrue($user->delete());
} }
@ -229,7 +240,7 @@ class UserTest extends TestCase {
->method('deleteUser') ->method('deleteUser')
->with($this->equalTo('foo')); ->with($this->equalTo('foo'));
$user = new User('foo', $backend); $user = new User('foo', $backend, $this->dispatcher);
$this->assertTrue($user->delete()); $this->assertTrue($user->delete());
} }
@ -253,14 +264,14 @@ class UserTest extends TestCase {
} }
})); }));
$user = new User('foo', $backend); $user = new User('foo', $backend, $this->dispatcher);
$this->assertEquals('/home/foo', $user->getHome()); $this->assertEquals('/home/foo', $user->getHome());
} }
public function testGetBackendClassName() { public function testGetBackendClassName() {
$user = new User('foo', new \Test\Util\User\Dummy()); $user = new User('foo', new \Test\Util\User\Dummy(), $this->dispatcher);
$this->assertEquals('Dummy', $user->getBackendClassName()); $this->assertEquals('Dummy', $user->getBackendClassName());
$user = new User('foo', new \OC\User\Database()); $user = new User('foo', new \OC\User\Database(), $this->dispatcher);
$this->assertEquals('Database', $user->getBackendClassName()); $this->assertEquals('Database', $user->getBackendClassName());
} }
@ -287,7 +298,7 @@ class UserTest extends TestCase {
->with($this->equalTo('datadirectory')) ->with($this->equalTo('datadirectory'))
->will($this->returnValue('arbitrary/path')); ->will($this->returnValue('arbitrary/path'));
$user = new User('foo', $backend, null, $allConfig); $user = new User('foo', $backend, $this->dispatcher, null, $allConfig);
$this->assertEquals('arbitrary/path/foo', $user->getHome()); $this->assertEquals('arbitrary/path/foo', $user->getHome());
} }
@ -307,7 +318,7 @@ class UserTest extends TestCase {
} }
})); }));
$user = new User('foo', $backend); $user = new User('foo', $backend, $this->dispatcher);
$this->assertTrue($user->canChangePassword()); $this->assertTrue($user->canChangePassword());
} }
@ -321,7 +332,7 @@ class UserTest extends TestCase {
->method('implementsActions') ->method('implementsActions')
->will($this->returnValue(false)); ->will($this->returnValue(false));
$user = new User('foo', $backend); $user = new User('foo', $backend, $this->dispatcher);
$this->assertFalse($user->canChangePassword()); $this->assertFalse($user->canChangePassword());
} }
@ -341,7 +352,7 @@ class UserTest extends TestCase {
} }
})); }));
$user = new User('foo', $backend); $user = new User('foo', $backend, $this->dispatcher);
$this->assertTrue($user->canChangeDisplayName()); $this->assertTrue($user->canChangeDisplayName());
} }
@ -355,7 +366,7 @@ class UserTest extends TestCase {
->method('implementsActions') ->method('implementsActions')
->will($this->returnValue(false)); ->will($this->returnValue(false));
$user = new User('foo', $backend); $user = new User('foo', $backend, $this->dispatcher);
$this->assertFalse($user->canChangeDisplayName()); $this->assertFalse($user->canChangeDisplayName());
} }
@ -380,7 +391,7 @@ class UserTest extends TestCase {
->with('foo','Foo') ->with('foo','Foo')
->willReturn(true); ->willReturn(true);
$user = new User('foo', $backend); $user = new User('foo', $backend, $this->dispatcher);
$this->assertTrue($user->setDisplayName('Foo')); $this->assertTrue($user->setDisplayName('Foo'));
$this->assertEquals('Foo',$user->getDisplayName()); $this->assertEquals('Foo',$user->getDisplayName());
} }
@ -404,7 +415,7 @@ class UserTest extends TestCase {
} }
})); }));
$user = new User('foo', $backend); $user = new User('foo', $backend, $this->dispatcher);
$this->assertFalse($user->setDisplayName(' ')); $this->assertFalse($user->setDisplayName(' '));
$this->assertEquals('foo',$user->getDisplayName()); $this->assertEquals('foo',$user->getDisplayName());
} }
@ -422,7 +433,7 @@ class UserTest extends TestCase {
$backend->expects($this->never()) $backend->expects($this->never())
->method('setDisplayName'); ->method('setDisplayName');
$user = new User('foo', $backend); $user = new User('foo', $backend, $this->dispatcher);
$this->assertFalse($user->setDisplayName('Foo')); $this->assertFalse($user->setDisplayName('Foo'));
$this->assertEquals('foo',$user->getDisplayName()); $this->assertEquals('foo',$user->getDisplayName());
} }
@ -462,7 +473,7 @@ class UserTest extends TestCase {
} }
})); }));
$user = new User('foo', $backend, $emitter); $user = new User('foo', $backend, $this->dispatcher, $emitter);
$user->setPassword('bar',''); $user->setPassword('bar','');
$this->assertEquals(2, $hooksCalled); $this->assertEquals(2, $hooksCalled);
@ -492,7 +503,7 @@ class UserTest extends TestCase {
->method('deleteUser') ->method('deleteUser')
->willReturn($result); ->willReturn($result);
$emitter = new PublicEmitter(); $emitter = new PublicEmitter();
$user = new User('foo', $backend, $emitter); $user = new User('foo', $backend, $this->dispatcher, $emitter);
/** /**
* @param User $user * @param User $user
@ -573,7 +584,7 @@ class UserTest extends TestCase {
->method('getAbsoluteURL') ->method('getAbsoluteURL')
->withAnyParameters() ->withAnyParameters()
->willReturn('http://localhost:8888/owncloud'); ->willReturn('http://localhost:8888/owncloud');
$user = new User('foo', $backend, null, null, $urlGenerator); $user = new User('foo', $backend, $this->dispatcher, null, null, $urlGenerator);
$this->assertEquals('foo@localhost:8888/owncloud', $user->getCloudId()); $this->assertEquals('foo@localhost:8888/owncloud', $user->getCloudId());
} }
@ -609,7 +620,7 @@ class UserTest extends TestCase {
'email' 'email'
); );
$user = new User('foo', $backend, $emitter, $config); $user = new User('foo', $backend, $this->dispatcher, $emitter, $config);
$user->setEMailAddress(''); $user->setEMailAddress('');
} }
@ -646,7 +657,7 @@ class UserTest extends TestCase {
'foo@bar.com' 'foo@bar.com'
); );
$user = new User('foo', $backend, $emitter, $config); $user = new User('foo', $backend, $this->dispatcher, $emitter, $config);
$user->setEMailAddress('foo@bar.com'); $user->setEMailAddress('foo@bar.com');
} }
@ -674,7 +685,7 @@ class UserTest extends TestCase {
'foo@bar.com' 'foo@bar.com'
); );
$user = new User('foo', $backend, $emitter, $config); $user = new User('foo', $backend, $this->dispatcher, $emitter, $config);
$user->setEMailAddress('foo@bar.com'); $user->setEMailAddress('foo@bar.com');
} }
@ -711,7 +722,7 @@ class UserTest extends TestCase {
'23 TB' '23 TB'
); );
$user = new User('foo', $backend, $emitter, $config); $user = new User('foo', $backend, $this->dispatcher, $emitter, $config);
$user->setQuota('23 TB'); $user->setQuota('23 TB');
} }
@ -739,7 +750,7 @@ class UserTest extends TestCase {
'23 TB' '23 TB'
); );
$user = new User('foo', $backend, $emitter, $config); $user = new User('foo', $backend, $this->dispatcher, $emitter, $config);
$user->setQuota('23 TB'); $user->setQuota('23 TB');
} }
@ -759,7 +770,7 @@ class UserTest extends TestCase {
} }
})); }));
$user = new User('foo', $backend, null, $config); $user = new User('foo', $backend, $this->dispatcher, null, $config);
$this->assertSame(42, $user->getLastLogin()); $this->assertSame(42, $user->getLastLogin());
} }
@ -779,7 +790,7 @@ class UserTest extends TestCase {
'true' 'true'
); );
$user = new User('foo', $backend, null, $config); $user = new User('foo', $backend, $this->dispatcher, null, $config);
$user->setEnabled(true); $user->setEnabled(true);
} }
@ -803,6 +814,7 @@ class UserTest extends TestCase {
->setConstructorArgs([ ->setConstructorArgs([
'foo', 'foo',
$backend, $backend,
$this->dispatcher,
null, null,
$config, $config,
]) ])
@ -836,6 +848,7 @@ class UserTest extends TestCase {
->setConstructorArgs([ ->setConstructorArgs([
'foo', 'foo',
$backend, $backend,
$this->dispatcher,
null, null,
$config, $config,
]) ])
@ -867,7 +880,7 @@ class UserTest extends TestCase {
} }
})); }));
$user = new User('foo', $backend, null, $config); $user = new User('foo', $backend, $this->dispatcher, null, $config);
$this->assertSame('foo@bar.com', $user->getEMailAddress()); $this->assertSame('foo@bar.com', $user->getEMailAddress());
} }
} }