summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVincent Petry <pvince81@owncloud.com>2016-11-30 20:21:44 +0100
committerArthur Schiwon <blizzz@arthur-schiwon.de>2016-12-22 18:34:45 +0100
commit453f3beffaf629efa5c715f7ca88b5c0a1034af8 (patch)
tree68dce83e8613a7dd8321d53567a8f745c46d8266
parent7130fa7b4cddd35e9275f7453593049c1e989562 (diff)
downloadnextcloud-server-453f3beffaf629efa5c715f7ca88b5c0a1034af8.tar.gz
nextcloud-server-453f3beffaf629efa5c715f7ca88b5c0a1034af8.zip
Adding group display name support
-rw-r--r--lib/private/Group/Backend.php15
-rw-r--r--lib/private/Group/Group.php11
-rw-r--r--lib/private/Group/Manager.php16
-rw-r--r--lib/public/GroupInterface.php12
-rw-r--r--lib/public/IGroup.php9
-rw-r--r--tests/lib/Group/ManagerTest.php437
6 files changed, 270 insertions, 230 deletions
diff --git a/lib/private/Group/Backend.php b/lib/private/Group/Backend.php
index 14c36d93422..1e8d62f5e42 100644
--- a/lib/private/Group/Backend.php
+++ b/lib/private/Group/Backend.php
@@ -31,23 +31,14 @@ abstract class Backend implements \OCP\GroupInterface {
*/
const NOT_IMPLEMENTED = -501;
- /**
- * actions that user backends can define
- */
- const CREATE_GROUP = 0x00000001;
- const DELETE_GROUP = 0x00000010;
- const ADD_TO_GROUP = 0x00000100;
- const REMOVE_FROM_GOUP = 0x00001000;
- //OBSOLETE const GET_DISPLAYNAME = 0x00010000;
- const COUNT_USERS = 0x00100000;
-
- protected $possibleActions = array(
+ protected $possibleActions = [
self::CREATE_GROUP => 'createGroup',
self::DELETE_GROUP => 'deleteGroup',
self::ADD_TO_GROUP => 'addToGroup',
self::REMOVE_FROM_GOUP => 'removeFromGroup',
self::COUNT_USERS => 'countUsersInGroup',
- );
+ self::GROUP_DETAILS => 'getGroupDetails',
+ ];
/**
* Get all supported actions
diff --git a/lib/private/Group/Group.php b/lib/private/Group/Group.php
index 9379d604c48..bb85445f541 100644
--- a/lib/private/Group/Group.php
+++ b/lib/private/Group/Group.php
@@ -66,18 +66,27 @@ class Group implements IGroup {
* @param \OC\Group\Backend[] $backends
* @param \OC\User\Manager $userManager
* @param \OC\Hooks\PublicEmitter $emitter
+ * @param string $displayName
*/
- public function __construct($gid, $backends, $userManager, $emitter = null) {
+ public function __construct($gid, $backends, $userManager, $emitter = null, $displayName = null) {
$this->gid = $gid;
$this->backends = $backends;
$this->userManager = $userManager;
$this->emitter = $emitter;
+ $this->displayName = $displayName;
}
public function getGID() {
return $this->gid;
}
+ public function getDisplayName() {
+ if (is_null($this->displayName)) {
+ return $this->gid;
+ }
+ return $this->displayName;
+ }
+
/**
* get all users in the group
*
diff --git a/lib/private/Group/Manager.php b/lib/private/Group/Manager.php
index 31911138985..944598a8296 100644
--- a/lib/private/Group/Manager.php
+++ b/lib/private/Group/Manager.php
@@ -155,19 +155,29 @@ class Manager extends PublicEmitter implements IGroupManager {
/**
* @param string $gid
+ * @param string $displayName
* @return \OCP\IGroup
*/
- protected function getGroupObject($gid) {
+ protected function getGroupObject($gid, $displayName = null) {
$backends = array();
foreach ($this->backends as $backend) {
- if ($backend->groupExists($gid)) {
+ if ($backend->implementsActions(\OC\Group\Backend::GROUP_DETAILS)) {
+ $groupData = $backend->getGroupDetails($gid);
+ if (is_array($groupData)) {
+ // take the display name from the first backend that has a non-null one
+ if (is_null($displayName) && isset($groupData['displayName'])) {
+ $displayName = $groupData['displayName'];
+ }
+ $backends[] = $backend;
+ }
+ } else if ($backend->groupExists($gid)) {
$backends[] = $backend;
}
}
if (count($backends) === 0) {
return null;
}
- $this->cachedGroups[$gid] = new Group($gid, $backends, $this->userManager, $this);
+ $this->cachedGroups[$gid] = new Group($gid, $backends, $this->userManager, $this, $displayName);
return $this->cachedGroups[$gid];
}
diff --git a/lib/public/GroupInterface.php b/lib/public/GroupInterface.php
index 6f456f51fd7..97837e50b16 100644
--- a/lib/public/GroupInterface.php
+++ b/lib/public/GroupInterface.php
@@ -41,6 +41,18 @@ namespace OCP;
interface GroupInterface {
/**
+ * actions that user backends can define
+ */
+ const CREATE_GROUP = 0x00000001;
+ const DELETE_GROUP = 0x00000010;
+ const ADD_TO_GROUP = 0x00000100;
+ const REMOVE_FROM_GOUP = 0x00001000; // oops
+ const REMOVE_FROM_GROUP = 0x00001000;
+ //OBSOLETE const GET_DISPLAYNAME = 0x00010000;
+ const COUNT_USERS = 0x00100000;
+ const GROUP_DETAILS = 0x01000000;
+
+ /**
* Check if backend implements actions
* @param int $actions bitwise-or'ed actions
* @return boolean
diff --git a/lib/public/IGroup.php b/lib/public/IGroup.php
index d5fcbcc5cd9..0cc62e9a8ed 100644
--- a/lib/public/IGroup.php
+++ b/lib/public/IGroup.php
@@ -4,6 +4,7 @@
*
* @author Morris Jobke <hey@morrisjobke.de>
* @author Robin Appelman <robin@icewind.nl>
+ * @author Vincent Petry <PVince81@owncloud.com>
*
* @license AGPL-3.0
*
@@ -37,6 +38,14 @@ interface IGroup {
public function getGID();
/**
+ * Returns the group display name
+ *
+ * @return string
+ * @since 9.2
+ */
+ public function getDisplayName();
+
+ /**
* get all users in the group
*
* @return \OCP\IUser[]
diff --git a/tests/lib/Group/ManagerTest.php b/tests/lib/Group/ManagerTest.php
index ece99eb569d..93fe67ad9bb 100644
--- a/tests/lib/Group/ManagerTest.php
+++ b/tests/lib/Group/ManagerTest.php
@@ -1,52 +1,81 @@
<?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.
+ * @author Robin Appelman <icewind@owncloud.com>
+ * @author Vincent Petry <pvince81@owncloud.com>
+ *
+ * @copyright Copyright (c) 2016, ownCloud GmbH.
+ * @license AGPL-3.0
+ *
+ * This code is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License, version 3,
+ * as published by the Free Software Foundation.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License, version 3,
+ * along with this program. If not, see <http://www.gnu.org/licenses/>
+ *
*/
-
namespace Test\Group;
-use OC\User\Manager;
-use OC\User\User;
+use OCP\IUser;
+use OCP\GroupInterface;
class ManagerTest extends \Test\TestCase {
- /** @var Manager */
- private $userManager;
-
- public function setUp() {
- parent::setUp();
-
- $this->userManager = $this->getMockBuilder('\OC\User\Manager')
- ->disableOriginalConstructor()
- ->getMock();
+ private function getTestUser($userId) {
+ $mockUser = $this->createMock(IUser::class);
+ $mockUser->expects($this->any())
+ ->method('getUID')
+ ->will($this->returnValue($userId));
+ $mockUser->expects($this->any())
+ ->method('getDisplayName')
+ ->will($this->returnValue($userId));
+ return $mockUser;
}
- /**
- * @param string $uid
- * @param \OC\User\Backend $backend
- * @return User
- */
- private function newUser($uid, $backend) {
- $config = $this->getMockBuilder('\OCP\IConfig')
- ->disableOriginalConstructor()
- ->getMock();
- $urlgenerator = $this->getMockBuilder('\OCP\IURLGenerator')
- ->disableOriginalConstructor()
+ private function getTestBackend($implementedActions = null) {
+ if (is_null($implementedActions)) {
+ $implementedActions =
+ GroupInterface::ADD_TO_GROUP |
+ GroupInterface::REMOVE_FROM_GOUP |
+ GroupInterface::COUNT_USERS |
+ GroupInterface::CREATE_GROUP |
+ GroupInterface::DELETE_GROUP;
+ }
+ // need to declare it this way due to optional methods
+ // thanks to the implementsActions logic
+ $backend = $this->getMockBuilder(\OCP\GroupInterface::class)
+ ->disableOriginalConstructor()
+ ->setMethods([
+ 'getGroupDetails',
+ 'implementsActions',
+ 'getUserGroups',
+ 'inGroup',
+ 'getGroups',
+ 'groupExists',
+ 'usersInGroup',
+ 'createGroup',
+ 'addToGroup',
+ 'removeFromGroup',
+ ])
->getMock();
-
- return new User($uid, $backend, null, $config, $urlgenerator);
+ $backend->expects($this->any())
+ ->method('implementsActions')
+ ->will($this->returnCallback(function($actions) use ($implementedActions) {
+ return (bool)($actions & $implementedActions);
+ }));
+ return $backend;
}
-
+
public function testGet() {
/**
* @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend
*/
- $backend = $this->getMockBuilder('\OC\Group\Database')
- ->disableOriginalConstructor()
- ->getMock();
+ $backend = $this->getTestBackend();
$backend->expects($this->any())
->method('groupExists')
->with('group1')
@@ -70,9 +99,7 @@ class ManagerTest extends \Test\TestCase {
/**
* @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend
*/
- $backend = $this->getMockBuilder('\OC\Group\Database')
- ->disableOriginalConstructor()
- ->getMock();
+ $backend = $this->getTestBackend();
$backend->expects($this->once())
->method('groupExists')
->with('group1')
@@ -100,9 +127,7 @@ class ManagerTest extends \Test\TestCase {
/**
* @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend1
*/
- $backend1 = $this->getMockBuilder('\OC\Group\Database')
- ->disableOriginalConstructor()
- ->getMock();
+ $backend1 = $this->getTestBackend();
$backend1->expects($this->any())
->method('groupExists')
->with('group1')
@@ -111,9 +136,7 @@ class ManagerTest extends \Test\TestCase {
/**
* @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend2
*/
- $backend2 = $this->getMockBuilder('\OC\Group\Database')
- ->disableOriginalConstructor()
- ->getMock();
+ $backend2 = $this->getTestBackend();
$backend2->expects($this->any())
->method('groupExists')
->with('group1')
@@ -133,9 +156,7 @@ class ManagerTest extends \Test\TestCase {
* @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend
*/
$backendGroupCreated = false;
- $backend = $this->getMockBuilder('\OC\Group\Database')
- ->disableOriginalConstructor()
- ->getMock();
+ $backend = $this->getTestBackend();
$backend->expects($this->any())
->method('groupExists')
->with('group1')
@@ -143,9 +164,6 @@ class ManagerTest extends \Test\TestCase {
return $backendGroupCreated;
}));
$backend->expects($this->once())
- ->method('implementsActions')
- ->will($this->returnValue(true));
- $backend->expects($this->once())
->method('createGroup')
->will($this->returnCallback(function () use (&$backendGroupCreated) {
$backendGroupCreated = true;
@@ -162,9 +180,7 @@ class ManagerTest extends \Test\TestCase {
/**
* @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend
*/
- $backend = $this->getMockBuilder('\OC\Group\Database')
- ->disableOriginalConstructor()
- ->getMock();
+ $backend = $this->getTestBackend();
$backend->expects($this->any())
->method('groupExists')
->with('group1')
@@ -183,9 +199,7 @@ class ManagerTest extends \Test\TestCase {
/**
* @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend
*/
- $backend = $this->getMockBuilder('\OC\Group\Database')
- ->disableOriginalConstructor()
- ->getMock();
+ $backend = $this->getTestBackend();
$backend->expects($this->once())
->method('getGroups')
->with('1')
@@ -208,9 +222,7 @@ class ManagerTest extends \Test\TestCase {
/**
* @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend1
*/
- $backend1 = $this->getMockBuilder('\OC\Group\Database')
- ->disableOriginalConstructor()
- ->getMock();
+ $backend1 = $this->getTestBackend();
$backend1->expects($this->once())
->method('getGroups')
->with('1')
@@ -222,9 +234,7 @@ class ManagerTest extends \Test\TestCase {
/**
* @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend2
*/
- $backend2 = $this->getMockBuilder('\OC\Group\Database')
- ->disableOriginalConstructor()
- ->getMock();
+ $backend2 = $this->getTestBackend();
$backend2->expects($this->once())
->method('getGroups')
->with('1')
@@ -249,9 +259,7 @@ class ManagerTest extends \Test\TestCase {
/**
* @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend1
*/
- $backend1 = $this->getMockBuilder('\OC\Group\Database')
- ->disableOriginalConstructor()
- ->getMock();
+ $backend1 = $this->getTestBackend();
$backend1->expects($this->once())
->method('getGroups')
->with('1', 2, 1)
@@ -263,9 +271,7 @@ class ManagerTest extends \Test\TestCase {
/**
* @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend2
*/
- $backend2 = $this->getMockBuilder('\OC\Group\Database')
- ->disableOriginalConstructor()
- ->getMock();
+ $backend2 = $this->getTestBackend();
$backend2->expects($this->once())
->method('getGroups')
->with('1', 2, 1)
@@ -290,9 +296,7 @@ class ManagerTest extends \Test\TestCase {
/**
* @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend
*/
- $backend = $this->getMockBuilder('\OC\Group\Database')
- ->disableOriginalConstructor()
- ->getMock();
+ $backend = $this->getTestBackend();
$backend->expects($this->once())
->method('getUserGroups')
->with('user1')
@@ -308,7 +312,7 @@ class ManagerTest extends \Test\TestCase {
$manager = new \OC\Group\Manager($this->userManager);
$manager->addBackend($backend);
- $groups = $manager->getUserGroups($this->newUser('user1', $userBackend));
+ $groups = $manager->getUserGroups($this->getTestUser('user1'));
$this->assertEquals(1, count($groups));
$group1 = reset($groups);
$this->assertEquals('group1', $group1->getGID());
@@ -344,9 +348,7 @@ class ManagerTest extends \Test\TestCase {
/**
* @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend
*/
- $backend = $this->getMockBuilder('\OC\Group\Database')
- ->disableOriginalConstructor()
- ->getMock();
+ $backend = $this->getTestBackend();
$backend->expects($this->once())
->method('getUserGroups')
->with('user1')
@@ -365,9 +367,7 @@ class ManagerTest extends \Test\TestCase {
/**
* @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend
*/
- $backend = $this->getMockBuilder('\OC\Group\Database')
- ->disableOriginalConstructor()
- ->getMock();
+ $backend = $this->getTestBackend();
$backend->expects($this->once())
->method('getUserGroups')
->with('user1')
@@ -386,9 +386,7 @@ class ManagerTest extends \Test\TestCase {
/**
* @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend
*/
- $backend = $this->getMockBuilder('\OC\Group\Database')
- ->disableOriginalConstructor()
- ->getMock();
+ $backend = $this->getTestBackend();
$backend->expects($this->once())
->method('getUserGroups')
->with('user1')
@@ -407,9 +405,7 @@ class ManagerTest extends \Test\TestCase {
/**
* @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend1
*/
- $backend1 = $this->getMockBuilder('\OC\Group\Database')
- ->disableOriginalConstructor()
- ->getMock();
+ $backend1 = $this->getTestBackend();
$backend1->expects($this->once())
->method('getUserGroups')
->with('user1')
@@ -421,9 +417,7 @@ class ManagerTest extends \Test\TestCase {
/**
* @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend2
*/
- $backend2 = $this->getMockBuilder('\OC\Group\Database')
- ->disableOriginalConstructor()
- ->getMock();
+ $backend2 = $this->getTestBackend();
$backend2->expects($this->once())
->method('getUserGroups')
->with('user1')
@@ -439,7 +433,7 @@ class ManagerTest extends \Test\TestCase {
$manager->addBackend($backend1);
$manager->addBackend($backend2);
- $groups = $manager->getUserGroups($this->newUser('user1', $userBackend));
+ $groups = $manager->getUserGroups($this->getTestUser('user1'));
$this->assertEquals(2, count($groups));
$group1 = reset($groups);
$group2 = next($groups);
@@ -447,30 +441,28 @@ class ManagerTest extends \Test\TestCase {
$this->assertEquals('group2', $group2->getGID());
}
- public function testDisplayNamesInGroupWithOneUserBackend() {
+ public function testDisplayNamesInGroupWithOneUserBackend() {
/**
- * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend1
+ * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend
*/
- $backend = $this->getMockBuilder('\OC\Group\Database')
- ->disableOriginalConstructor()
- ->getMock();
+ $backend = $this->getTestBackend();
$backend->expects($this->exactly(1))
->method('groupExists')
->with('testgroup')
->will($this->returnValue(true));
- $backend->expects($this->any())
- ->method('InGroup')
+ $backend->expects($this->any())
+ ->method('inGroup')
->will($this->returnCallback(function($uid, $gid) {
- switch($uid) {
- case 'user1' : return false;
- case 'user2' : return true;
- case 'user3' : return false;
- case 'user33': return true;
- default:
- return null;
- }
- }));
+ switch($uid) {
+ case 'user1' : return false;
+ case 'user2' : return true;
+ case 'user3' : return false;
+ case 'user33': return true;
+ default:
+ return null;
+ }
+ }));
$userBackend = $this->getMockBuilder('\OC\User\Backend')
->disableOriginalConstructor()
@@ -480,21 +472,21 @@ class ManagerTest extends \Test\TestCase {
->method('searchDisplayName')
->with('user3')
->will($this->returnCallback(function($search, $limit, $offset) use ($userBackend) {
- switch($offset) {
- case 0 : return array('user3' => $this->newUser('user3', $userBackend),
- 'user33' => $this->newUser('user33', $userBackend));
- case 2 : return array();
- }
- }));
+ switch($offset) {
+ case 0 : return ['user3' => $this->getTestUser('user3'),
+ 'user33' => $this->getTestUser('user33')];
+ case 2 : return [];
+ }
+ }));
$this->userManager->expects($this->any())
->method('get')
->will($this->returnCallback(function($uid) use ($userBackend) {
switch($uid) {
- case 'user1' : return $this->newUser('user1', $userBackend);
- case 'user2' : return $this->newUser('user2', $userBackend);
- case 'user3' : return $this->newUser('user3', $userBackend);
- case 'user33': return $this->newUser('user33', $userBackend);
+ case 'user1' : return $this->getTestUser('user1');
+ case 'user2' : return $this->getTestUser('user2');
+ case 'user3' : return $this->getTestUser('user3');
+ case 'user33': return $this->getTestUser('user33');
default:
return null;
}
@@ -511,31 +503,29 @@ class ManagerTest extends \Test\TestCase {
$this->assertTrue(isset($users['user33']));
}
- public function testDisplayNamesInGroupWithOneUserBackendWithLimitSpecified() {
+ public function testDisplayNamesInGroupWithOneUserBackendWithLimitSpecified() {
/**
- * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend1
+ * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend
*/
- $backend = $this->getMockBuilder('\OC\Group\Database')
- ->disableOriginalConstructor()
- ->getMock();
+ $backend = $this->getTestBackend();
$backend->expects($this->exactly(1))
->method('groupExists')
->with('testgroup')
->will($this->returnValue(true));
- $backend->expects($this->any())
- ->method('InGroup')
+ $backend->expects($this->any())
+ ->method('inGroup')
->will($this->returnCallback(function($uid, $gid) {
- switch($uid) {
- case 'user1' : return false;
- case 'user2' : return true;
- case 'user3' : return false;
- case 'user33': return true;
- case 'user333': return true;
- default:
- return null;
- }
- }));
+ switch($uid) {
+ case 'user1' : return false;
+ case 'user2' : return true;
+ case 'user3' : return false;
+ case 'user33': return true;
+ case 'user333': return true;
+ default:
+ return null;
+ }
+ }));
$userBackend = $this->getMockBuilder('\OC\User\Backend')
->disableOriginalConstructor()
@@ -545,22 +535,22 @@ class ManagerTest extends \Test\TestCase {
->method('searchDisplayName')
->with('user3')
->will($this->returnCallback(function($search, $limit, $offset) use ($userBackend) {
- switch($offset) {
- case 0 : return array('user3' => $this->newUser('user3', $userBackend),
- 'user33' => $this->newUser('user33', $userBackend));
- case 2 : return array('user333' => $this->newUser('user333', $userBackend));
- }
- }));
+ switch($offset) {
+ case 0 : return ['user3' => $this->getTestUser('user3'),
+ 'user33' => $this->getTestUser('user33')];
+ case 2 : return ['user333' => $this->getTestUser('user333')];
+ }
+ }));
$this->userManager->expects($this->any())
->method('get')
->will($this->returnCallback(function($uid) use ($userBackend) {
switch($uid) {
- case 'user1' : return $this->newUser('user1', $userBackend);
- case 'user2' : return $this->newUser('user2', $userBackend);
- case 'user3' : return $this->newUser('user3', $userBackend);
- case 'user33': return $this->newUser('user33', $userBackend);
- case 'user333': return $this->newUser('user333', $userBackend);
+ case 'user1' : return $this->getTestUser('user1');
+ case 'user2' : return $this->getTestUser('user2');
+ case 'user3' : return $this->getTestUser('user3');
+ case 'user33': return $this->getTestUser('user33');
+ case 'user333': return $this->getTestUser('user333');
default:
return null;
}
@@ -580,29 +570,27 @@ class ManagerTest extends \Test\TestCase {
public function testDisplayNamesInGroupWithOneUserBackendWithLimitAndOffsetSpecified() {
/**
- * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend1
+ * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend
*/
- $backend = $this->getMockBuilder('\OC\Group\Database')
- ->disableOriginalConstructor()
- ->getMock();
+ $backend = $this->getTestBackend();
$backend->expects($this->exactly(1))
->method('groupExists')
->with('testgroup')
->will($this->returnValue(true));
- $backend->expects($this->any())
+ $backend->expects($this->any())
->method('inGroup')
->will($this->returnCallback(function($uid) {
- switch($uid) {
- case 'user1' : return false;
- case 'user2' : return true;
- case 'user3' : return false;
- case 'user33': return true;
- case 'user333': return true;
- default:
- return null;
- }
- }));
+ switch($uid) {
+ case 'user1' : return false;
+ case 'user2' : return true;
+ case 'user3' : return false;
+ case 'user33': return true;
+ case 'user333': return true;
+ default:
+ return null;
+ }
+ }));
$userBackend = $this->getMockBuilder('\OC\User\Backend')
->disableOriginalConstructor()
@@ -612,25 +600,25 @@ class ManagerTest extends \Test\TestCase {
->method('searchDisplayName')
->with('user3')
->will($this->returnCallback(function($search, $limit, $offset) use ($userBackend) {
- switch($offset) {
- case 0 :
- return array(
- 'user3' => $this->newUser('user3', $userBackend),
- 'user33' => $this->newUser('user33', $userBackend),
- 'user333' => $this->newUser('user333', $userBackend)
- );
- }
- }));
+ switch($offset) {
+ case 0 :
+ return [
+ 'user3' => $this->getTestUser('user3'),
+ 'user33' => $this->getTestUser('user33'),
+ 'user333' => $this->getTestUser('user333')
+ ];
+ }
+ }));
$this->userManager->expects($this->any())
->method('get')
->will($this->returnCallback(function($uid) use ($userBackend) {
switch($uid) {
- case 'user1' : return $this->newUser('user1', $userBackend);
- case 'user2' : return $this->newUser('user2', $userBackend);
- case 'user3' : return $this->newUser('user3', $userBackend);
- case 'user33': return $this->newUser('user33', $userBackend);
- case 'user333': return $this->newUser('user333', $userBackend);
+ case 'user1' : return $this->getTestUser('user1');
+ case 'user2' : return $this->getTestUser('user2');
+ case 'user3' : return $this->getTestUser('user3');
+ case 'user33': return $this->getTestUser('user33');
+ case 'user333': return $this->getTestUser('user333');
default:
return null;
}
@@ -650,17 +638,15 @@ class ManagerTest extends \Test\TestCase {
public function testDisplayNamesInGroupWithOneUserBackendAndSearchEmpty() {
/**
- * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend1
+ * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend
*/
- $backend = $this->getMockBuilder('\OC\Group\Database')
- ->disableOriginalConstructor()
- ->getMock();
+ $backend = $this->getTestBackend();
$backend->expects($this->exactly(1))
->method('groupExists')
->with('testgroup')
->will($this->returnValue(true));
- $backend->expects($this->once())
+ $backend->expects($this->once())
->method('usersInGroup')
->with('testgroup', '', -1, 0)
->will($this->returnValue(array('user2', 'user33')));
@@ -673,10 +659,10 @@ class ManagerTest extends \Test\TestCase {
->method('get')
->will($this->returnCallback(function($uid) use ($userBackend) {
switch($uid) {
- case 'user1' : return $this->newUser('user1', $userBackend);
- case 'user2' : return $this->newUser('user2', $userBackend);
- case 'user3' : return $this->newUser('user3', $userBackend);
- case 'user33': return $this->newUser('user33', $userBackend);
+ case 'user1' : return $this->getTestUser('user1');
+ case 'user2' : return $this->getTestUser('user2');
+ case 'user3' : return $this->getTestUser('user3');
+ case 'user33': return $this->getTestUser('user33');
default:
return null;
}
@@ -695,11 +681,9 @@ class ManagerTest extends \Test\TestCase {
public function testDisplayNamesInGroupWithOneUserBackendAndSearchEmptyAndLimitSpecified() {
/**
- * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend1
+ * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend
*/
- $backend = $this->getMockBuilder('\OC\Group\Database')
- ->disableOriginalConstructor()
- ->getMock();
+ $backend = $this->getTestBackend();
$backend->expects($this->exactly(1))
->method('groupExists')
->with('testgroup')
@@ -718,10 +702,10 @@ class ManagerTest extends \Test\TestCase {
->method('get')
->will($this->returnCallback(function($uid) use ($userBackend) {
switch($uid) {
- case 'user1' : return $this->newUser('user1', $userBackend);
- case 'user2' : return $this->newUser('user2', $userBackend);
- case 'user3' : return $this->newUser('user3', $userBackend);
- case 'user33': return $this->newUser('user33', $userBackend);
+ case 'user1' : return $this->getTestUser('user1');
+ case 'user2' : return $this->getTestUser('user2');
+ case 'user3' : return $this->getTestUser('user3');
+ case 'user33': return $this->getTestUser('user33');
default:
return null;
}
@@ -738,13 +722,11 @@ class ManagerTest extends \Test\TestCase {
$this->assertFalse(isset($users['user33']));
}
- public function testDisplayNamesInGroupWithOneUserBackendAndSearchEmptyAndLimitAndOffsetSpecified() {
+ public function testDisplayNamesInGroupWithOneUserBackendAndSearchEmptyAndLimitAndOffsetSpecified() {
/**
- * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend1
+ * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend
*/
- $backend = $this->getMockBuilder('\OC\Group\Database')
- ->disableOriginalConstructor()
- ->getMock();
+ $backend = $this->getTestBackend();
$backend->expects($this->exactly(1))
->method('groupExists')
->with('testgroup')
@@ -763,10 +745,10 @@ class ManagerTest extends \Test\TestCase {
->method('get')
->will($this->returnCallback(function($uid) use ($userBackend) {
switch($uid) {
- case 'user1' : return $this->newUser('user1', $userBackend);
- case 'user2' : return $this->newUser('user2', $userBackend);
- case 'user3' : return $this->newUser('user3', $userBackend);
- case 'user33': return $this->newUser('user33', $userBackend);
+ case 'user1' : return $this->getTestUser('user1');
+ case 'user2' : return $this->getTestUser('user2');
+ case 'user3' : return $this->getTestUser('user3');
+ case 'user33': return $this->getTestUser('user33');
default:
return null;
}
@@ -787,10 +769,8 @@ class ManagerTest extends \Test\TestCase {
/**
* @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend
*/
- $backend = $this->getMockBuilder('\OC\Group\Database')
- ->disableOriginalConstructor()
- ->getMock();
- $expectedGroups = array();
+ $backend = $this->getTestBackend();
+ $expectedGroups = [];
$backend->expects($this->any())
->method('getUserGroups')
->with('user1')
@@ -801,15 +781,12 @@ class ManagerTest extends \Test\TestCase {
->method('groupExists')
->with('group1')
->will($this->returnValue(true));
- $backend->expects($this->once())
- ->method('implementsActions')
- ->will($this->returnValue(true));
$manager = new \OC\Group\Manager($this->userManager);
$manager->addBackend($backend);
// prime cache
- $user1 = $this->newUser('user1', null);
+ $user1 = $this->getTestUser('user1');
$groups = $manager->getUserGroups($user1);
$this->assertEquals(array(), $groups);
@@ -829,10 +806,8 @@ class ManagerTest extends \Test\TestCase {
/**
* @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend
*/
- $backend = $this->getMockBuilder('\OC\Group\Database')
- ->disableOriginalConstructor()
- ->getMock();
- $expectedGroups = array('group1');
+ $backend = $this->getTestBackend();
+ $expectedGroups = ['group1'];
$backend->expects($this->any())
->method('getUserGroups')
->with('user1')
@@ -844,9 +819,6 @@ class ManagerTest extends \Test\TestCase {
->with('group1')
->will($this->returnValue(true));
$backend->expects($this->once())
- ->method('implementsActions')
- ->will($this->returnValue(true));
- $backend->expects($this->once())
->method('inGroup')
->will($this->returnValue(true));
$backend->expects($this->once())
@@ -857,7 +829,7 @@ class ManagerTest extends \Test\TestCase {
$manager->addBackend($backend);
// prime cache
- $user1 = $this->newUser('user1', null);
+ $user1 = $this->getTestUser('user1');
$groups = $manager->getUserGroups($user1);
$this->assertEquals(1, count($groups));
$group1 = reset($groups);
@@ -877,9 +849,7 @@ class ManagerTest extends \Test\TestCase {
/**
* @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend
*/
- $backend = $this->getMockBuilder('\OC\Group\Database')
- ->disableOriginalConstructor()
- ->getMock();
+ $backend = $this->getTestBackend();
$backend->expects($this->any())
->method('getUserGroups')
->with('user1')
@@ -892,4 +862,43 @@ class ManagerTest extends \Test\TestCase {
$this->assertEquals([], $groups);
}
+ public function testGroupDisplayName() {
+ /**
+ * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend
+ */
+ $backend = $this->getTestBackend(
+ GroupInterface::ADD_TO_GROUP |
+ GroupInterface::REMOVE_FROM_GOUP |
+ GroupInterface::COUNT_USERS |
+ GroupInterface::CREATE_GROUP |
+ GroupInterface::DELETE_GROUP |
+ GroupInterface::GROUP_DETAILS
+ );
+ $backend->expects($this->any())
+ ->method('getGroupDetails')
+ ->will($this->returnValueMap([
+ ['group1', ['gid' => 'group1', 'displayName' => 'Group One']],
+ ['group2', ['gid' => 'group2']],
+ ]));
+
+ /**
+ * @var \OC\User\Manager $userManager
+ */
+ $userManager = $this->createMock('\OC\User\Manager');
+ $manager = new \OC\Group\Manager($userManager);
+ $manager->addBackend($backend);
+
+ // group with display name
+ $group = $manager->get('group1');
+ $this->assertNotNull($group);
+ $this->assertEquals('group1', $group->getGID());
+ $this->assertEquals('Group One', $group->getDisplayName());
+
+ // group without display name
+ $group = $manager->get('group2');
+ $this->assertNotNull($group);
+ $this->assertEquals('group2', $group->getGID());
+ $this->assertEquals('group2', $group->getDisplayName());
+ }
+
}