Merge pull request #3995 from owncloud/groups
Refactoring of group management coderemotes/origin/stable6
commit
98858e6f00
@ -0,0 +1,238 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Copyright (c) 2013 Robin Appelman <icewind@owncloud.com> |
||||
* This file is licensed under the Affero General Public License version 3 or |
||||
* later. |
||||
* See the COPYING-README file. |
||||
*/ |
||||
|
||||
namespace OC\Group; |
||||
|
||||
class Group { |
||||
/** |
||||
* @var string $id |
||||
*/ |
||||
private $fid; |
||||
|
||||
/** |
||||
* @var \OC\User\User[] $users |
||||
*/ |
||||
private $users; |
||||
|
||||
/** |
||||
* @var \OC_Group_Backend[] | \OC_Group_Database[] $backend |
||||
*/ |
||||
private $backends; |
||||
|
||||
/** |
||||
* @var \OC\Hooks\PublicEmitter $emitter; |
||||
*/ |
||||
private $emitter; |
||||
|
||||
/** |
||||
* @var \OC\User\Manager $userManager |
||||
*/ |
||||
private $userManager; |
||||
|
||||
/** |
||||
* @param string $gid |
||||
* @param \OC_Group_Backend[] $backends |
||||
* @param \OC\User\Manager $userManager |
||||
* @param \OC\Hooks\PublicEmitter $emitter |
||||
*/ |
||||
public function __construct($gid, $backends, $userManager, $emitter = null) { |
||||
$this->gid = $gid; |
||||
$this->backends = $backends; |
||||
$this->userManager = $userManager; |
||||
$this->emitter = $emitter; |
||||
} |
||||
|
||||
public function getGID() { |
||||
return $this->gid; |
||||
} |
||||
|
||||
/** |
||||
* get all users in the group |
||||
* |
||||
* @return \OC\User\User[] |
||||
*/ |
||||
public function getUsers() { |
||||
if ($this->users) { |
||||
return $this->users; |
||||
} |
||||
|
||||
$users = array(); |
||||
$userIds = array(); |
||||
foreach ($this->backends as $backend) { |
||||
$diff = array_diff( |
||||
$backend->usersInGroup($this->gid), |
||||
$userIds |
||||
); |
||||
if ($diff) { |
||||
$userIds = array_merge($userIds, $diff); |
||||
} |
||||
} |
||||
|
||||
foreach ($userIds as $userId) { |
||||
$users[] = $this->userManager->get($userId); |
||||
} |
||||
$this->users = $users; |
||||
return $users; |
||||
} |
||||
|
||||
/** |
||||
* check if a user is in the group |
||||
* |
||||
* @param \OC\User\User $user |
||||
* @return bool |
||||
*/ |
||||
public function inGroup($user) { |
||||
foreach ($this->backends as $backend) { |
||||
if ($backend->inGroup($user->getUID(), $this->gid)) { |
||||
return true; |
||||
} |
||||
} |
||||
return false; |
||||
} |
||||
|
||||
/** |
||||
* add a user to the group |
||||
* |
||||
* @param \OC\User\User $user |
||||
*/ |
||||
public function addUser($user) { |
||||
if ($this->inGroup($user)) { |
||||
return; |
||||
} |
||||
|
||||
if ($this->emitter) { |
||||
$this->emitter->emit('\OC\Group', 'preAddUser', array($this, $user)); |
||||
} |
||||
foreach ($this->backends as $backend) { |
||||
if ($backend->implementsActions(OC_GROUP_BACKEND_ADD_TO_GROUP)) { |
||||
$backend->addToGroup($user->getUID(), $this->gid); |
||||
if ($this->users) { |
||||
$this->users[] = $user; |
||||
} |
||||
if ($this->emitter) { |
||||
$this->emitter->emit('\OC\Group', 'postAddUser', array($this, $user)); |
||||
} |
||||
return; |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* remove a user from the group |
||||
* |
||||
* @param \OC\User\User $user |
||||
*/ |
||||
public function removeUser($user) { |
||||
$result = false; |
||||
if ($this->emitter) { |
||||
$this->emitter->emit('\OC\Group', 'preRemoveUser', array($this, $user)); |
||||
} |
||||
foreach ($this->backends as $backend) { |
||||
if ($backend->implementsActions(OC_GROUP_BACKEND_REMOVE_FROM_GOUP) and $backend->inGroup($user->getUID(), $this->gid)) { |
||||
$backend->removeFromGroup($user->getUID(), $this->gid); |
||||
$result = true; |
||||
} |
||||
} |
||||
if ($result) { |
||||
if ($this->emitter) { |
||||
$this->emitter->emit('\OC\Group', 'postRemoveUser', array($this, $user)); |
||||
} |
||||
if ($this->users) { |
||||
foreach ($this->users as $index => $groupUser) { |
||||
if ($groupUser->getUID() === $user->getUID()) { |
||||
unset($this->users[$index]); |
||||
return; |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* search for users in the group by userid |
||||
* |
||||
* @param string $search |
||||
* @param int $limit |
||||
* @param int $offset |
||||
* @return \OC\User\User[] |
||||
*/ |
||||
public function searchUsers($search, $limit = null, $offset = null) { |
||||
$users = array(); |
||||
foreach ($this->backends as $backend) { |
||||
$userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset); |
||||
if (!is_null($limit)) { |
||||
$limit -= count($userIds); |
||||
} |
||||
if (!is_null($offset)) { |
||||
$offset -= count($userIds); |
||||
} |
||||
foreach ($userIds as $userId) { |
||||
$users[$userId] = $this->userManager->get($userId); |
||||
} |
||||
if (!is_null($limit) and $limit <= 0) { |
||||
return array_values($users); |
||||
} |
||||
} |
||||
return array_values($users); |
||||
} |
||||
|
||||
/** |
||||
* search for users in the group by displayname |
||||
* |
||||
* @param string $search |
||||
* @param int $limit |
||||
* @param int $offset |
||||
* @return \OC\User\User[] |
||||
*/ |
||||
public function searchDisplayName($search, $limit = null, $offset = null) { |
||||
$users = array(); |
||||
foreach ($this->backends as $backend) { |
||||
if ($backend->implementsActions(OC_GROUP_BACKEND_GET_DISPLAYNAME)) { |
||||
$userIds = array_keys($backend->displayNamesInGroup($this->gid, $search, $limit, $offset)); |
||||
} else { |
||||
$userIds = $backend->usersInGroup($this->gid, $search, $limit, $offset); |
||||
} |
||||
if (!is_null($limit)) { |
||||
$limit -= count($userIds); |
||||
} |
||||
if (!is_null($offset)) { |
||||
$offset -= count($userIds); |
||||
} |
||||
foreach ($userIds as $userId) { |
||||
$users[$userId] = $this->userManager->get($userId); |
||||
} |
||||
if (!is_null($limit) and $limit <= 0) { |
||||
return array_values($users); |
||||
} |
||||
} |
||||
return array_values($users); |
||||
} |
||||
|
||||
/** |
||||
* delete the group |
||||
* |
||||
* @return bool |
||||
*/ |
||||
public function delete() { |
||||
$result = false; |
||||
if ($this->emitter) { |
||||
$this->emitter->emit('\OC\Group', 'preDelete', array($this)); |
||||
} |
||||
foreach ($this->backends as $backend) { |
||||
if ($backend->implementsActions(OC_GROUP_BACKEND_DELETE_GROUP)) { |
||||
$result = true; |
||||
$backend->deleteGroup($this->gid); |
||||
} |
||||
} |
||||
if ($result and $this->emitter) { |
||||
$this->emitter->emit('\OC\Group', 'postDelete', array($this)); |
||||
} |
||||
return $result; |
||||
} |
||||
} |
@ -0,0 +1,169 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Copyright (c) 2013 Robin Appelman <icewind@owncloud.com> |
||||
* This file is licensed under the Affero General Public License version 3 or |
||||
* later. |
||||
* See the COPYING-README file. |
||||
*/ |
||||
|
||||
namespace OC\Group; |
||||
|
||||
use OC\Hooks\PublicEmitter; |
||||
|
||||
/** |
||||
* Class Manager |
||||
* |
||||
* Hooks available in scope \OC\Group: |
||||
* - preAddUser(\OC\Group\Group $group, \OC\User\User $user) |
||||
* - postAddUser(\OC\Group\Group $group, \OC\User\User $user) |
||||
* - preRemoveUser(\OC\Group\Group $group, \OC\User\User $user) |
||||
* - postRemoveUser(\OC\Group\Group $group, \OC\User\User $user) |
||||
* - preDelete(\OC\Group\Group $group) |
||||
* - postDelete(\OC\Group\Group $group) |
||||
* - preCreate(string $groupId) |
||||
* - postCreate(\OC\Group\Group $group) |
||||
* |
||||
* @package OC\Group |
||||
*/ |
||||
class Manager extends PublicEmitter { |
||||
/** |
||||
* @var \OC_Group_Backend[] | \OC_Group_Database[] $backends |
||||
*/ |
||||
private $backends = array(); |
||||
|
||||
/** |
||||
* @var \OC\User\Manager $userManager |
||||
*/ |
||||
private $userManager; |
||||
|
||||
/** |
||||
* @var \OC\Group\Group[] |
||||
*/ |
||||
private $cachedGroups; |
||||
|
||||
/** |
||||
* @param \OC\User\Manager $userManager |
||||
*/ |
||||
public function __construct($userManager) { |
||||
$this->userManager = $userManager; |
||||
$cache = & $this->cachedGroups; |
||||
$this->listen('\OC\Group', 'postDelete', function ($group) use (&$cache) { |
||||
/** |
||||
* @var \OC\Group\Group $group |
||||
*/ |
||||
unset($cache[$group->getGID()]); |
||||
}); |
||||
} |
||||
|
||||
/** |
||||
* @param \OC_Group_Backend $backend |
||||
*/ |
||||
public function addBackend($backend) { |
||||
$this->backends[] = $backend; |
||||
} |
||||
|
||||
public function clearBackends() { |
||||
$this->backends = array(); |
||||
$this->cachedGroups = array(); |
||||
} |
||||
|
||||
/** |
||||
* @param string $gid |
||||
* @return \OC\Group\Group |
||||
*/ |
||||
public function get($gid) { |
||||
if (isset($this->cachedGroups[$gid])) { |
||||
return $this->cachedGroups[$gid]; |
||||
} |
||||
foreach ($this->backends as $backend) { |
||||
if ($backend->groupExists($gid)) { |
||||
return $this->getGroupObject($gid); |
||||
} |
||||
} |
||||
return null; |
||||
} |
||||
|
||||
protected function getGroupObject($gid) { |
||||
$backends = array(); |
||||
foreach ($this->backends as $backend) { |
||||
if ($backend->groupExists($gid)) { |
||||
$backends[] = $backend; |
||||
} |
||||
} |
||||
$this->cachedGroups[$gid] = new Group($gid, $backends, $this->userManager, $this); |
||||
return $this->cachedGroups[$gid]; |
||||
} |
||||
|
||||
/** |
||||
* @param string $gid |
||||
* @return bool |
||||
*/ |
||||
public function groupExists($gid) { |
||||
return !is_null($this->get($gid)); |
||||
} |
||||
|
||||
/** |
||||
* @param string $gid |
||||
* @return \OC\Group\Group |
||||
*/ |
||||
public function createGroup($gid) { |
||||
if (!$gid) { |
||||
return false; |
||||
} else if ($this->groupExists($gid)) { |
||||
return $this->get($gid); |
||||
} else { |
||||
$this->emit('\OC\Group', 'preCreate', array($gid)); |
||||
foreach ($this->backends as $backend) { |
||||
if ($backend->implementsActions(OC_GROUP_BACKEND_CREATE_GROUP)) { |
||||
$backend->createGroup($gid); |
||||
$group = $this->getGroupObject($gid); |
||||
$this->emit('\OC\Group', 'postCreate', array($group)); |
||||
return $group; |
||||
} |
||||
} |
||||
return null; |
||||
} |
||||
} |
||||
|
||||
/** |
||||
* @param string $search |
||||
* @param int $limit |
||||
* @param int $offset |
||||
* @return \OC\Group\Group[] |
||||
*/ |
||||
public function search($search, $limit = null, $offset = null) { |
||||
$groups = array(); |
||||
foreach ($this->backends as $backend) { |
||||
$groupIds = $backend->getGroups($search, $limit, $offset); |
||||
if (!is_null($limit)) { |
||||
$limit -= count($groupIds); |
||||
} |
||||
if (!is_null($offset)) { |
||||
$offset -= count($groupIds); |
||||
} |
||||
foreach ($groupIds as $groupId) { |
||||
$groups[$groupId] = $this->getGroupObject($groupId); |
||||
} |
||||
if (!is_null($limit) and $limit <= 0) { |
||||
return array_values($groups); |
||||
} |
||||
} |
||||
return array_values($groups); |
||||
} |
||||
|
||||
/** |
||||
* @param \OC\User\User $user |
||||
* @return \OC\Group\Group[] |
||||
*/ |
||||
public function getUserGroups($user) { |
||||
$groups = array(); |
||||
foreach ($this->backends as $backend) { |
||||
$groupIds = $backend->getUserGroups($user->getUID()); |
||||
foreach ($groupIds as $groupId) { |
||||
$groups[$groupId] = $this->getGroupObject($groupId); |
||||
} |
||||
} |
||||
return array_values($groups); |
||||
} |
||||
} |
@ -0,0 +1,316 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Copyright (c) 2013 Robin Appelman <icewind@owncloud.com> |
||||
* This file is licensed under the Affero General Public License version 3 or |
||||
* later. |
||||
* See the COPYING-README file. |
||||
*/ |
||||
|
||||
namespace Test\Group; |
||||
|
||||
use OC\User\User; |
||||
|
||||
class Group extends \PHPUnit_Framework_TestCase { |
||||
/** |
||||
* @return \PHPUnit_Framework_MockObject_MockObject | \OC\User\Manager |
||||
*/ |
||||
protected function getUserManager() { |
||||
$userManager = $this->getMock('\OC\User\Manager'); |
||||
$user1 = new User('user1', null); |
||||
$user2 = new User('user2', null); |
||||
$user3 = new User('user3', null); |
||||
$userManager->expects($this->any()) |
||||
->method('get') |
||||
->will($this->returnValueMap(array( |
||||
array('user1', $user1), |
||||
array('user2', $user2), |
||||
array('user3', $user3) |
||||
))); |
||||
return $userManager; |
||||
} |
||||
|
||||
public function testGetUsersSingleBackend() { |
||||
$backend = $this->getMock('OC_Group_Database'); |
||||
$userManager = $this->getUserManager(); |
||||
$group = new \OC\Group\Group('group1', array($backend), $userManager); |
||||
|
||||
$backend->expects($this->once()) |
||||
->method('usersInGroup') |
||||
->with('group1') |
||||
->will($this->returnValue(array('user1', 'user2'))); |
||||
|
||||
$users = $group->getUsers(); |
||||
|
||||
$this->assertEquals(2, count($users)); |
||||
$user1 = $users[0]; |
||||
$user2 = $users[1]; |
||||
$this->assertEquals('user1', $user1->getUID()); |
||||
$this->assertEquals('user2', $user2->getUID()); |
||||
} |
||||
|
||||
public function testGetUsersMultipleBackends() { |
||||
$backend1 = $this->getMock('OC_Group_Database'); |
||||
$backend2 = $this->getMock('OC_Group_Database'); |
||||
$userManager = $this->getUserManager(); |
||||
$group = new \OC\Group\Group('group1', array($backend1, $backend2), $userManager); |
||||
|
||||
$backend1->expects($this->once()) |
||||
->method('usersInGroup') |
||||
->with('group1') |
||||
->will($this->returnValue(array('user1', 'user2'))); |
||||
|
||||
$backend2->expects($this->once()) |
||||
->method('usersInGroup') |
||||
->with('group1') |
||||
->will($this->returnValue(array('user2', 'user3'))); |
||||
|
||||
$users = $group->getUsers(); |
||||
|
||||
$this->assertEquals(3, count($users)); |
||||
$user1 = $users[0]; |
||||
$user2 = $users[1]; |
||||
$user3 = $users[2]; |
||||
$this->assertEquals('user1', $user1->getUID()); |
||||
$this->assertEquals('user2', $user2->getUID()); |
||||
$this->assertEquals('user3', $user3->getUID()); |
||||
} |
||||
|
||||
public function testInGroupSingleBackend() { |
||||
$backend = $this->getMock('OC_Group_Database'); |
||||
$userManager = $this->getUserManager(); |
||||
$group = new \OC\Group\Group('group1', array($backend), $userManager); |
||||
|
||||
$backend->expects($this->once()) |
||||
->method('inGroup') |
||||
->with('user1', 'group1') |
||||
->will($this->returnValue(true)); |
||||
|
||||
$this->assertTrue($group->inGroup(new User('user1', null))); |
||||
} |
||||
|
||||
public function testInGroupMultipleBackends() { |
||||
$backend1 = $this->getMock('OC_Group_Database'); |
||||
$backend2 = $this->getMock('OC_Group_Database'); |
||||
$userManager = $this->getUserManager(); |
||||
$group = new \OC\Group\Group('group1', array($backend1, $backend2), $userManager); |
||||
|
||||
$backend1->expects($this->once()) |
||||
->method('inGroup') |
||||
->with('user1', 'group1') |
||||
->will($this->returnValue(false)); |
||||
|
||||
$backend2->expects($this->once()) |
||||
->method('inGroup') |
||||
->with('user1', 'group1') |
||||
->will($this->returnValue(true)); |
||||
|
||||
$this->assertTrue($group->inGroup(new User('user1', null))); |
||||
} |
||||
|
||||
public function testAddUser() { |
||||
$backend = $this->getMock('OC_Group_Database'); |
||||
$userManager = $this->getUserManager(); |
||||
$group = new \OC\Group\Group('group1', array($backend), $userManager); |
||||
|
||||
$backend->expects($this->once()) |
||||
->method('inGroup') |
||||
->with('user1', 'group1') |
||||
->will($this->returnValue(false)); |
||||
$backend->expects($this->any()) |
||||
->method('implementsActions') |
||||
->will($this->returnValue(true)); |
||||
|
||||
$backend->expects($this->once()) |
||||
->method('addToGroup') |
||||
->with('user1', 'group1'); |
||||
|
||||
$group->addUser(new User('user1', null)); |
||||
} |
||||
|
||||
public function testAddUserAlreadyInGroup() { |
||||
$backend = $this->getMock('OC_Group_Database'); |
||||
$userManager = $this->getUserManager(); |
||||
$group = new \OC\Group\Group('group1', array($backend), $userManager); |
||||
|
||||
$backend->expects($this->once()) |
||||
->method('inGroup') |
||||
->with('user1', 'group1') |
||||
->will($this->returnValue(true)); |
||||
$backend->expects($this->any()) |
||||
->method('implementsActions') |
||||
->will($this->returnValue(true)); |
||||
|
||||
$backend->expects($this->never()) |
||||
->method('addToGroup'); |
||||
|
||||
$group->addUser(new User('user1', null)); |
||||
} |
||||
|
||||
public function testRemoveUser() { |
||||
$backend = $this->getMock('OC_Group_Database'); |
||||
$userManager = $this->getUserManager(); |
||||
$group = new \OC\Group\Group('group1', array($backend), $userManager); |
||||
|
||||
$backend->expects($this->once()) |
||||
->method('inGroup') |
||||
->with('user1', 'group1') |
||||
->will($this->returnValue(true)); |
||||
$backend->expects($this->any()) |
||||
->method('implementsActions') |
||||
->will($this->returnValue(true)); |
||||
|
||||
$backend->expects($this->once()) |
||||
->method('removeFromGroup') |
||||
->with('user1', 'group1'); |
||||
|
||||
$group->removeUser(new User('user1', null)); |
||||
} |
||||
|
||||
public function testRemoveUserNotInGroup() { |
||||
$backend = $this->getMock('OC_Group_Database'); |
||||
$userManager = $this->getUserManager(); |
||||
$group = new \OC\Group\Group('group1', array($backend), $userManager); |
||||
|
||||
$backend->expects($this->once()) |
||||
->method('inGroup') |
||||
->with('user1', 'group1') |
||||
->will($this->returnValue(false)); |
||||
$backend->expects($this->any()) |
||||
->method('implementsActions') |
||||
->will($this->returnValue(true)); |
||||
|
||||
$backend->expects($this->never()) |
||||
->method('removeFromGroup'); |
||||
|
||||
$group->removeUser(new User('user1', null)); |
||||
} |
||||
|
||||
public function testRemoveUserMultipleBackends() { |
||||
$backend1 = $this->getMock('OC_Group_Database'); |
||||
$backend2 = $this->getMock('OC_Group_Database'); |
||||
$userManager = $this->getUserManager(); |
||||
$group = new \OC\Group\Group('group1', array($backend1, $backend2), $userManager); |
||||
|
||||
$backend1->expects($this->once()) |
||||
->method('inGroup') |
||||
->with('user1', 'group1') |
||||
->will($this->returnValue(true)); |
||||
$backend1->expects($this->any()) |
||||
->method('implementsActions') |
||||
->will($this->returnValue(true)); |
||||
|
||||
$backend1->expects($this->once()) |
||||
->method('removeFromGroup') |
||||
->with('user1', 'group1'); |
||||
|
||||
$backend2->expects($this->once()) |
||||
->method('inGroup') |
||||
->with('user1', 'group1') |
||||
->will($this->returnValue(true)); |
||||
$backend2->expects($this->any()) |
||||
->method('implementsActions') |
||||
->will($this->returnValue(true)); |
||||
|
||||
$backend2->expects($this->once()) |
||||
->method('removeFromGroup') |
||||
->with('user1', 'group1'); |
||||
|
||||
$group->removeUser(new User('user1', null)); |
||||
} |
||||
|
||||
public function testSearchUsers() { |
||||
$backend = $this->getMock('OC_Group_Database'); |
||||
$userManager = $this->getUserManager(); |
||||
$group = new \OC\Group\Group('group1', array($backend), $userManager); |
||||
|
||||
$backend->expects($this->once()) |
||||
->method('usersInGroup') |
||||
->with('group1', '2') |
||||
->will($this->returnValue(array('user2'))); |
||||
|
||||
$users = $group->searchUsers('2'); |
||||
|
||||
$this->assertEquals(1, count($users)); |
||||
$user2 = $users[0]; |
||||
$this->assertEquals('user2', $user2->getUID()); |
||||
} |
||||
|
||||
public function testSearchUsersMultipleBackends() { |
||||
$backend1 = $this->getMock('OC_Group_Database'); |
||||
$backend2 = $this->getMock('OC_Group_Database'); |
||||
$userManager = $this->getUserManager(); |
||||
$group = new \OC\Group\Group('group1', array($backend1, $backend2), $userManager); |
||||
|
||||
$backend1->expects($this->once()) |
||||
->method('usersInGroup') |
||||
->with('group1', '2') |
||||
->will($this->returnValue(array('user2'))); |
||||
$backend2->expects($this->once()) |
||||
->method('usersInGroup') |
||||
->with('group1', '2') |
||||
->will($this->returnValue(array('user2'))); |
||||
|
||||
$users = $group->searchUsers('2'); |
||||
|
||||
$this->assertEquals(1, count($users)); |
||||
$user2 = $users[0]; |
||||
$this->assertEquals('user2', $user2->getUID()); |
||||
} |
||||
|
||||
public function testSearchUsersLimitAndOffset() { |
||||
$backend = $this->getMock('OC_Group_Database'); |
||||
$userManager = $this->getUserManager(); |
||||
$group = new \OC\Group\Group('group1', array($backend), $userManager); |
||||
|
||||
$backend->expects($this->once()) |
||||
->method('usersInGroup') |
||||
->with('group1', 'user', 1, 1) |
||||
->will($this->returnValue(array('user2'))); |
||||
|
||||
$users = $group->searchUsers('user', 1, 1); |
||||
|
||||
$this->assertEquals(1, count($users)); |
||||
$user2 = $users[0]; |
||||
$this->assertEquals('user2', $user2->getUID()); |
||||
} |
||||
|
||||
public function testSearchUsersMultipleBackendsLimitAndOffset() { |
||||
$backend1 = $this->getMock('OC_Group_Database'); |
||||
$backend2 = $this->getMock('OC_Group_Database'); |
||||
$userManager = $this->getUserManager(); |
||||
$group = new \OC\Group\Group('group1', array($backend1, $backend2), $userManager); |
||||
|
||||
$backend1->expects($this->once()) |
||||
->method('usersInGroup') |
||||
->with('group1', 'user', 2, 1) |
||||
->will($this->returnValue(array('user2'))); |
||||
$backend2->expects($this->once()) |
||||
->method('usersInGroup') |
||||
->with('group1', 'user', 1, 0) |
||||
->will($this->returnValue(array('user1'))); |
||||
|
||||
$users = $group->searchUsers('user', 2, 1); |
||||
|
||||
$this->assertEquals(2, count($users)); |
||||
$user2 = $users[0]; |
||||
$user1 = $users[1]; |
||||
$this->assertEquals('user2', $user2->getUID()); |
||||
$this->assertEquals('user1', $user1->getUID()); |
||||
} |
||||
|
||||
public function testDelete() { |
||||
$backend = $this->getMock('OC_Group_Database'); |
||||
$userManager = $this->getUserManager(); |
||||
$group = new \OC\Group\Group('group1', array($backend), $userManager); |
||||
|
||||
$backend->expects($this->once()) |
||||
->method('deleteGroup') |
||||
->with('group1'); |
||||
$backend->expects($this->any()) |
||||
->method('implementsActions') |
||||
->will($this->returnValue(true)); |
||||
|
||||
$group->delete(); |
||||
} |
||||
} |
@ -0,0 +1,313 @@ |
||||
<?php |
||||
|
||||
/** |
||||
* Copyright (c) 2013 Robin Appelman <icewind@owncloud.com> |
||||
* This file is licensed under the Affero General Public License version 3 or |
||||
* later. |
||||
* See the COPYING-README file. |
||||
*/ |
||||
|
||||
namespace Test\Group; |
||||
|
||||
use OC\User\User; |
||||
|
||||
class Manager extends \PHPUnit_Framework_TestCase { |
||||
public function testGet() { |
||||
/** |
||||
* @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend |
||||
*/ |
||||
$backend = $this->getMock('\OC_Group_Database'); |
||||
$backend->expects($this->any()) |
||||
->method('groupExists') |
||||
->with('group1') |
||||
->will($this->returnValue(true)); |
||||
|
||||
/** |
||||
* @var \OC\User\Manager $userManager |
||||
*/ |
||||
$userManager = $this->getMock('\OC\User\Manager'); |
||||
$manager = new \OC\Group\Manager($userManager); |
||||
$manager->addBackend($backend); |
||||
|
||||
$group = $manager->get('group1'); |
||||
$this->assertNotNull($group); |
||||
$this->assertEquals('group1', $group->getGID()); |
||||
} |
||||
|
||||
public function testGetNoBackend() { |
||||
/** |
||||
* @var \OC\User\Manager $userManager |
||||
*/ |
||||
$userManager = $this->getMock('\OC\User\Manager'); |
||||
$manager = new \OC\Group\Manager($userManager); |
||||
|
||||
$this->assertNull($manager->get('group1')); |
||||
} |
||||
|
||||
public function testGetNotExists() { |
||||
/** |
||||
* @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend |
||||
*/ |
||||
$backend = $this->getMock('\OC_Group_Database'); |
||||
$backend->expects($this->once()) |
||||
->method('groupExists') |
||||
->with('group1') |
||||
->will($this->returnValue(false)); |
||||
|
||||
/** |
||||
* @var \OC\User\Manager $userManager |
||||
*/ |
||||
$userManager = $this->getMock('\OC\User\Manager'); |
||||
$manager = new \OC\Group\Manager($userManager); |
||||
$manager->addBackend($backend); |
||||
|
||||
$this->assertNull($manager->get('group1')); |
||||
} |
||||
|
||||
public function testGetDeleted() { |
||||
$backend = new \OC_Group_Dummy(); |
||||
$backend->createGroup('group1'); |
||||
|
||||
/** |
||||
* @var \OC\User\Manager $userManager |
||||
*/ |
||||
$userManager = $this->getMock('\OC\User\Manager'); |
||||
$manager = new \OC\Group\Manager($userManager); |
||||
$manager->addBackend($backend); |
||||
|
||||
$group = $manager->get('group1'); |
||||
$group->delete(); |
||||
$this->assertNull($manager->get('group1')); |
||||
} |
||||
|
||||
public function testGetMultipleBackends() { |
||||
/** |
||||
* @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend1 |
||||
*/ |
||||
$backend1 = $this->getMock('\OC_Group_Database'); |
||||
$backend1->expects($this->any()) |
||||
->method('groupExists') |
||||
->with('group1') |
||||
->will($this->returnValue(false)); |
||||
|
||||
/** |
||||
* @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend2 |
||||
*/ |
||||
$backend2 = $this->getMock('\OC_Group_Database'); |
||||
$backend2->expects($this->any()) |
||||
->method('groupExists') |
||||
->with('group1') |
||||
->will($this->returnValue(true)); |
||||
|
||||
/** |
||||
* @var \OC\User\Manager $userManager |
||||
*/ |
||||
$userManager = $this->getMock('\OC\User\Manager'); |
||||
$manager = new \OC\Group\Manager($userManager); |
||||
$manager->addBackend($backend1); |
||||
$manager->addBackend($backend2); |
||||
|
||||
$group = $manager->get('group1'); |
||||
$this->assertNotNull($group); |
||||
$this->assertEquals('group1', $group->getGID()); |
||||
} |
||||
|
||||
public function testCreate() { |
||||
/** |
||||
* @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend |
||||
*/ |
||||
$backend = $this->getMock('\OC_Group_Database'); |
||||
$backend->expects($this->any()) |
||||
->method('groupExists') |
||||
->with('group1') |
||||
->will($this->returnValue(false)); |
||||
$backend->expects($this->once()) |
||||
->method('implementsActions') |
||||
->will($this->returnValue(true)); |
||||
$backend->expects($this->once()) |
||||
->method('createGroup'); |
||||
|
||||
/** |
||||
* @var \OC\User\Manager $userManager |
||||
*/ |
||||
$userManager = $this->getMock('\OC\User\Manager'); |
||||
$manager = new \OC\Group\Manager($userManager); |
||||
$manager->addBackend($backend); |
||||
|
||||
$group = $manager->createGroup('group1'); |
||||
$this->assertEquals('group1', $group->getGID()); |
||||
} |
||||
|
||||
public function testCreateExists() { |
||||
/** |
||||
* @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend |
||||
*/ |
||||
$backend = $this->getMock('\OC_Group_Database'); |
||||
$backend->expects($this->any()) |
||||
->method('groupExists') |
||||
->with('group1') |
||||
->will($this->returnValue(true)); |
||||
$backend->expects($this->never()) |
||||
->method('createGroup'); |
||||
|
||||
/** |
||||
* @var \OC\User\Manager $userManager |
||||
*/ |
||||
$userManager = $this->getMock('\OC\User\Manager'); |
||||
$manager = new \OC\Group\Manager($userManager); |
||||
$manager->addBackend($backend); |
||||
|
||||
$group = $manager->createGroup('group1'); |
||||
$this->assertEquals('group1', $group->getGID()); |
||||
} |
||||
|
||||
public function testSearch() { |
||||
/** |
||||
* @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend |
||||
*/ |
||||
$backend = $this->getMock('\OC_Group_Database'); |
||||
$backend->expects($this->once()) |
||||
->method('getGroups') |
||||
->with('1') |
||||
->will($this->returnValue(array('group1'))); |
||||
|
||||
/** |
||||
* @var \OC\User\Manager $userManager |
||||
*/ |
||||
$userManager = $this->getMock('\OC\User\Manager'); |
||||
$manager = new \OC\Group\Manager($userManager); |
||||
$manager->addBackend($backend); |
||||
|
||||
$groups = $manager->search('1'); |
||||
$this->assertEquals(1, count($groups)); |
||||
$group1 = $groups[0]; |
||||
$this->assertEquals('group1', $group1->getGID()); |
||||
} |
||||
|
||||
public function testSearchMultipleBackends() { |
||||
/** |
||||
* @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend1 |
||||
*/ |
||||
$backend1 = $this->getMock('\OC_Group_Database'); |
||||
$backend1->expects($this->once()) |
||||
->method('getGroups') |
||||
->with('1') |
||||
->will($this->returnValue(array('group1'))); |
||||
|
||||
/** |
||||
* @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend2 |
||||
*/ |
||||
$backend2 = $this->getMock('\OC_Group_Database'); |
||||
$backend2->expects($this->once()) |
||||
->method('getGroups') |
||||
->with('1') |
||||
->will($this->returnValue(array('group12', 'group1'))); |
||||
|
||||
/** |
||||
* @var \OC\User\Manager $userManager |
||||
*/ |
||||
$userManager = $this->getMock('\OC\User\Manager'); |
||||
$manager = new \OC\Group\Manager($userManager); |
||||
$manager->addBackend($backend1); |
||||
$manager->addBackend($backend2); |
||||
|
||||
$groups = $manager->search('1'); |
||||
$this->assertEquals(2, count($groups)); |
||||
$group1 = $groups[0]; |
||||
$group12 = $groups[1]; |
||||
$this->assertEquals('group1', $group1->getGID()); |
||||
$this->assertEquals('group12', $group12->getGID()); |
||||
} |
||||
|
||||
public function testSearchMultipleBackendsLimitAndOffset() { |
||||
/** |
||||
* @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend1 |
||||
*/ |
||||
$backend1 = $this->getMock('\OC_Group_Database'); |
||||
$backend1->expects($this->once()) |
||||
->method('getGroups') |
||||
->with('1', 2, 1) |
||||
->will($this->returnValue(array('group1'))); |
||||
|
||||
/** |
||||
* @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend2 |
||||
*/ |
||||
$backend2 = $this->getMock('\OC_Group_Database'); |
||||
$backend2->expects($this->once()) |
||||
->method('getGroups') |
||||
->with('1', 1, 0) |
||||
->will($this->returnValue(array('group12'))); |
||||
|
||||
/** |
||||
* @var \OC\User\Manager $userManager |
||||
*/ |
||||
$userManager = $this->getMock('\OC\User\Manager'); |
||||
$manager = new \OC\Group\Manager($userManager); |
||||
$manager->addBackend($backend1); |
||||
$manager->addBackend($backend2); |
||||
|
||||
$groups = $manager->search('1', 2, 1); |
||||
$this->assertEquals(2, count($groups)); |
||||
$group1 = $groups[0]; |
||||
$group12 = $groups[1]; |
||||
$this->assertEquals('group1', $group1->getGID()); |
||||
$this->assertEquals('group12', $group12->getGID()); |
||||
} |
||||
|
||||
public function testGetUserGroups() { |
||||
/** |
||||
* @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend |
||||
*/ |
||||
$backend = $this->getMock('\OC_Group_Database'); |
||||
$backend->expects($this->once()) |
||||
->method('getUserGroups') |
||||
->with('user1') |
||||
->will($this->returnValue(array('group1'))); |
||||
|
||||
/** |
||||
* @var \OC\User\Manager $userManager |
||||
*/ |
||||
$userManager = $this->getMock('\OC\User\Manager'); |
||||
$manager = new \OC\Group\Manager($userManager); |
||||
$manager->addBackend($backend); |
||||
|
||||
$groups = $manager->getUserGroups(new User('user1', null)); |
||||
$this->assertEquals(1, count($groups)); |
||||
$group1 = $groups[0]; |
||||
$this->assertEquals('group1', $group1->getGID()); |
||||
} |
||||
|
||||
public function testGetUserGroupsMultipleBackends() { |
||||
/** |
||||
* @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend1 |
||||
*/ |
||||
$backend1 = $this->getMock('\OC_Group_Database'); |
||||
$backend1->expects($this->once()) |
||||
->method('getUserGroups') |
||||
->with('user1') |
||||
->will($this->returnValue(array('group1'))); |
||||
/** |
||||
* @var \PHPUnit_Framework_MockObject_MockObject | \OC_Group_Backend $backend2 |
||||
*/ |
||||
$backend2 = $this->getMock('\OC_Group_Database'); |
||||
$backend2->expects($this->once()) |
||||
->method('getUserGroups') |
||||
->with('user1') |
||||
->will($this->returnValue(array('group1', 'group2'))); |
||||
|
||||
/** |
||||
* @var \OC\User\Manager $userManager |
||||
*/ |
||||
$userManager = $this->getMock('\OC\User\Manager'); |
||||
$manager = new \OC\Group\Manager($userManager); |
||||
$manager->addBackend($backend1); |
||||
$manager->addBackend($backend2); |
||||
|
||||
$groups = $manager->getUserGroups(new User('user1', null)); |
||||
$this->assertEquals(2, count($groups)); |
||||
$group1 = $groups[0]; |
||||
$group2 = $groups[1]; |
||||
$this->assertEquals('group1', $group1->getGID()); |
||||
$this->assertEquals('group2', $group2->getGID()); |
||||
} |
||||
} |
Loading…
Reference in new issue