2013-07-10 02:06:22 +04:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2016-11-30 22:21:44 +03:00
|
|
|
* @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/>
|
|
|
|
*
|
2013-07-10 02:06:22 +04:00
|
|
|
*/
|
2019-11-22 22:52:10 +03:00
|
|
|
|
2013-07-10 02:06:22 +04:00
|
|
|
namespace Test\Group;
|
|
|
|
|
2017-03-17 12:34:25 +03:00
|
|
|
use OC\Group\Database;
|
2016-12-22 22:53:52 +03:00
|
|
|
use OC\User\Manager;
|
2019-11-22 22:52:10 +03:00
|
|
|
use OCP\GroupInterface;
|
2017-03-17 12:23:04 +03:00
|
|
|
use OCP\ILogger;
|
2016-11-30 22:21:44 +03:00
|
|
|
use OCP\IUser;
|
2019-02-22 15:07:26 +03:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
2017-03-17 12:34:25 +03:00
|
|
|
use Test\TestCase;
|
2013-07-10 02:06:22 +04:00
|
|
|
|
2017-03-17 12:34:25 +03:00
|
|
|
class ManagerTest extends TestCase {
|
2019-02-22 15:07:26 +03:00
|
|
|
/** @var Manager|MockObject */
|
2016-12-22 22:53:52 +03:00
|
|
|
protected $userManager;
|
2019-02-22 15:07:26 +03:00
|
|
|
/** @var EventDispatcherInterface|MockObject */
|
|
|
|
protected $dispatcher;
|
|
|
|
/** @var ILogger|MockObject */
|
2017-03-17 12:23:04 +03:00
|
|
|
protected $logger;
|
2016-12-22 22:53:52 +03:00
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2016-12-22 22:53:52 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->userManager = $this->createMock(Manager::class);
|
2019-02-22 15:07:26 +03:00
|
|
|
$this->dispatcher = $this->createMock(EventDispatcherInterface::class);
|
2017-03-17 12:23:04 +03:00
|
|
|
$this->logger = $this->createMock(ILogger::class);
|
2016-12-22 22:53:52 +03:00
|
|
|
}
|
|
|
|
|
2016-11-30 22:21:44 +03:00
|
|
|
private function getTestUser($userId) {
|
|
|
|
$mockUser = $this->createMock(IUser::class);
|
|
|
|
$mockUser->expects($this->any())
|
|
|
|
->method('getUID')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($userId);
|
2016-11-30 22:21:44 +03:00
|
|
|
$mockUser->expects($this->any())
|
|
|
|
->method('getDisplayName')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn($userId);
|
2016-11-30 22:21:44 +03:00
|
|
|
return $mockUser;
|
2016-07-11 17:53:01 +03:00
|
|
|
}
|
|
|
|
|
2017-03-17 12:34:25 +03:00
|
|
|
/**
|
|
|
|
* @param null|int $implementedActions
|
2020-08-11 22:32:18 +03:00
|
|
|
* @return \PHPUnit\Framework\MockObject\MockObject
|
2017-03-17 12:34:25 +03:00
|
|
|
*/
|
2016-11-30 22:21:44 +03:00
|
|
|
private function getTestBackend($implementedActions = null) {
|
2017-03-17 12:34:25 +03:00
|
|
|
if ($implementedActions === null) {
|
2016-11-30 22:21:44 +03:00
|
|
|
$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
|
2017-03-17 12:34:25 +03:00
|
|
|
$backend = $this->getMockBuilder(GroupInterface::class)
|
2016-11-30 22:21:44 +03:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->setMethods([
|
|
|
|
'getGroupDetails',
|
|
|
|
'implementsActions',
|
|
|
|
'getUserGroups',
|
|
|
|
'inGroup',
|
|
|
|
'getGroups',
|
|
|
|
'groupExists',
|
|
|
|
'usersInGroup',
|
|
|
|
'createGroup',
|
|
|
|
'addToGroup',
|
|
|
|
'removeFromGroup',
|
|
|
|
])
|
2016-07-11 17:53:01 +03:00
|
|
|
->getMock();
|
2016-11-30 22:21:44 +03:00
|
|
|
$backend->expects($this->any())
|
|
|
|
->method('implementsActions')
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function ($actions) use ($implementedActions) {
|
2016-11-30 22:21:44 +03:00
|
|
|
return (bool)($actions & $implementedActions);
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2016-11-30 22:21:44 +03:00
|
|
|
return $backend;
|
2016-07-11 17:53:01 +03:00
|
|
|
}
|
2016-11-30 22:21:44 +03:00
|
|
|
|
2013-07-10 02:06:22 +04:00
|
|
|
public function testGet() {
|
|
|
|
/**
|
2020-08-11 22:32:18 +03:00
|
|
|
* @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
|
2013-07-10 02:06:22 +04:00
|
|
|
*/
|
2016-11-30 22:21:44 +03:00
|
|
|
$backend = $this->getTestBackend();
|
2013-07-18 14:15:34 +04:00
|
|
|
$backend->expects($this->any())
|
2013-07-10 02:06:22 +04:00
|
|
|
->method('groupExists')
|
|
|
|
->with('group1')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true);
|
2013-07-10 02:06:22 +04:00
|
|
|
|
2019-02-22 15:07:26 +03:00
|
|
|
$manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
|
2013-07-10 02:06:22 +04:00
|
|
|
$manager->addBackend($backend);
|
|
|
|
|
|
|
|
$group = $manager->get('group1');
|
|
|
|
$this->assertNotNull($group);
|
|
|
|
$this->assertEquals('group1', $group->getGID());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetNoBackend() {
|
2019-02-22 15:07:26 +03:00
|
|
|
$manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
|
2013-07-10 02:06:22 +04:00
|
|
|
|
|
|
|
$this->assertNull($manager->get('group1'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetNotExists() {
|
|
|
|
/**
|
2020-08-11 22:32:18 +03:00
|
|
|
* @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
|
2013-07-10 02:06:22 +04:00
|
|
|
*/
|
2016-11-30 22:21:44 +03:00
|
|
|
$backend = $this->getTestBackend();
|
2013-07-10 02:06:22 +04:00
|
|
|
$backend->expects($this->once())
|
|
|
|
->method('groupExists')
|
|
|
|
->with('group1')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(false);
|
2013-07-10 02:06:22 +04:00
|
|
|
|
2019-02-22 15:07:26 +03:00
|
|
|
$manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
|
2013-07-10 02:06:22 +04:00
|
|
|
$manager->addBackend($backend);
|
|
|
|
|
|
|
|
$this->assertNull($manager->get('group1'));
|
|
|
|
}
|
|
|
|
|
2013-07-10 04:00:14 +04:00
|
|
|
public function testGetDeleted() {
|
2017-03-17 15:54:58 +03:00
|
|
|
$backend = new \Test\Util\Group\Dummy();
|
2013-07-10 04:00:14 +04:00
|
|
|
$backend->createGroup('group1');
|
|
|
|
|
2019-02-22 15:07:26 +03:00
|
|
|
$manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
|
2013-07-10 04:00:14 +04:00
|
|
|
$manager->addBackend($backend);
|
|
|
|
|
|
|
|
$group = $manager->get('group1');
|
|
|
|
$group->delete();
|
|
|
|
$this->assertNull($manager->get('group1'));
|
|
|
|
}
|
|
|
|
|
2013-07-10 02:06:22 +04:00
|
|
|
public function testGetMultipleBackends() {
|
|
|
|
/**
|
2020-08-11 22:32:18 +03:00
|
|
|
* @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend1
|
2013-07-10 02:06:22 +04:00
|
|
|
*/
|
2016-11-30 22:21:44 +03:00
|
|
|
$backend1 = $this->getTestBackend();
|
2013-07-18 14:15:34 +04:00
|
|
|
$backend1->expects($this->any())
|
2013-07-10 02:06:22 +04:00
|
|
|
->method('groupExists')
|
|
|
|
->with('group1')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(false);
|
2013-07-10 02:06:22 +04:00
|
|
|
|
|
|
|
/**
|
2020-08-11 22:32:18 +03:00
|
|
|
* @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend2
|
2013-07-10 02:06:22 +04:00
|
|
|
*/
|
2016-11-30 22:21:44 +03:00
|
|
|
$backend2 = $this->getTestBackend();
|
2013-07-18 14:15:34 +04:00
|
|
|
$backend2->expects($this->any())
|
2013-07-10 02:06:22 +04:00
|
|
|
->method('groupExists')
|
|
|
|
->with('group1')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true);
|
2013-07-10 02:06:22 +04:00
|
|
|
|
2019-02-22 15:07:26 +03:00
|
|
|
$manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
|
2013-07-10 02:06:22 +04:00
|
|
|
$manager->addBackend($backend1);
|
|
|
|
$manager->addBackend($backend2);
|
|
|
|
|
|
|
|
$group = $manager->get('group1');
|
|
|
|
$this->assertNotNull($group);
|
|
|
|
$this->assertEquals('group1', $group->getGID());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testCreate() {
|
2020-08-11 22:32:18 +03:00
|
|
|
/**@var \PHPUnit\Framework\MockObject\MockObject|\OC\Group\Backend $backend */
|
2014-02-14 18:07:08 +04:00
|
|
|
$backendGroupCreated = false;
|
2016-11-30 22:21:44 +03:00
|
|
|
$backend = $this->getTestBackend();
|
2013-07-18 14:15:34 +04:00
|
|
|
$backend->expects($this->any())
|
2013-07-10 02:06:22 +04:00
|
|
|
->method('groupExists')
|
|
|
|
->with('group1')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturnCallback(function () use (&$backendGroupCreated) {
|
2014-02-14 18:07:08 +04:00
|
|
|
return $backendGroupCreated;
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2013-07-10 02:06:22 +04:00
|
|
|
$backend->expects($this->once())
|
2014-02-14 18:07:08 +04:00
|
|
|
->method('createGroup')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturnCallback(function () use (&$backendGroupCreated) {
|
2014-02-14 18:07:08 +04:00
|
|
|
$backendGroupCreated = true;
|
2019-10-08 19:05:13 +03:00
|
|
|
return true;
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2013-07-10 02:06:22 +04:00
|
|
|
|
2019-02-22 15:07:26 +03:00
|
|
|
$manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
|
2013-07-10 02:06:22 +04:00
|
|
|
$manager->addBackend($backend);
|
|
|
|
|
2013-07-10 04:17:24 +04:00
|
|
|
$group = $manager->createGroup('group1');
|
2013-07-10 02:06:22 +04:00
|
|
|
$this->assertEquals('group1', $group->getGID());
|
|
|
|
}
|
|
|
|
|
2019-10-08 19:05:13 +03:00
|
|
|
public function testCreateFailure() {
|
2020-08-11 22:32:18 +03:00
|
|
|
/**@var \PHPUnit\Framework\MockObject\MockObject|\OC\Group\Backend $backend */
|
2019-10-08 19:05:13 +03:00
|
|
|
$backendGroupCreated = false;
|
|
|
|
$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('groupExists')
|
|
|
|
->with('group1')
|
|
|
|
->willReturn(false);
|
|
|
|
$backend->expects($this->once())
|
|
|
|
->method('createGroup')
|
|
|
|
->willReturn(false);
|
|
|
|
$backend->expects($this->once())
|
|
|
|
->method('getGroupDetails')
|
|
|
|
->willReturn([]);
|
|
|
|
|
|
|
|
$manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
|
|
|
|
$manager->addBackend($backend);
|
|
|
|
|
|
|
|
$group = $manager->createGroup('group1');
|
|
|
|
$this->assertEquals(null, $group);
|
|
|
|
}
|
|
|
|
|
2013-07-10 02:06:22 +04:00
|
|
|
public function testCreateExists() {
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject|\OC\Group\Backend $backend */
|
2016-11-30 22:21:44 +03:00
|
|
|
$backend = $this->getTestBackend();
|
2013-07-18 14:15:34 +04:00
|
|
|
$backend->expects($this->any())
|
2013-07-10 02:06:22 +04:00
|
|
|
->method('groupExists')
|
|
|
|
->with('group1')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true);
|
2013-07-10 02:06:22 +04:00
|
|
|
$backend->expects($this->never())
|
|
|
|
->method('createGroup');
|
|
|
|
|
2019-02-22 15:07:26 +03:00
|
|
|
$manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
|
2013-07-10 02:06:22 +04:00
|
|
|
$manager->addBackend($backend);
|
|
|
|
|
2013-07-10 04:17:24 +04:00
|
|
|
$group = $manager->createGroup('group1');
|
2013-07-10 02:06:22 +04:00
|
|
|
$this->assertEquals('group1', $group->getGID());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSearch() {
|
|
|
|
/**
|
2020-08-11 22:32:18 +03:00
|
|
|
* @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
|
2013-07-10 02:06:22 +04:00
|
|
|
*/
|
2016-11-30 22:21:44 +03:00
|
|
|
$backend = $this->getTestBackend();
|
2013-07-10 02:06:22 +04:00
|
|
|
$backend->expects($this->once())
|
|
|
|
->method('getGroups')
|
|
|
|
->with('1')
|
2020-03-26 11:30:18 +03:00
|
|
|
->willReturn(['group1']);
|
2014-02-14 18:07:08 +04:00
|
|
|
$backend->expects($this->once())
|
|
|
|
->method('groupExists')
|
|
|
|
->with('group1')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true);
|
2013-07-10 02:06:22 +04:00
|
|
|
|
2019-02-22 15:07:26 +03:00
|
|
|
$manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
|
2013-07-10 02:06:22 +04:00
|
|
|
$manager->addBackend($backend);
|
|
|
|
|
|
|
|
$groups = $manager->search('1');
|
2017-03-17 12:34:25 +03:00
|
|
|
$this->assertCount(1, $groups);
|
2014-06-24 18:27:38 +04:00
|
|
|
$group1 = reset($groups);
|
2013-07-10 02:06:22 +04:00
|
|
|
$this->assertEquals('group1', $group1->getGID());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSearchMultipleBackends() {
|
|
|
|
/**
|
2020-08-11 22:32:18 +03:00
|
|
|
* @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend1
|
2013-07-10 02:06:22 +04:00
|
|
|
*/
|
2016-11-30 22:21:44 +03:00
|
|
|
$backend1 = $this->getTestBackend();
|
2013-07-10 02:06:22 +04:00
|
|
|
$backend1->expects($this->once())
|
|
|
|
->method('getGroups')
|
|
|
|
->with('1')
|
2020-03-26 11:30:18 +03:00
|
|
|
->willReturn(['group1']);
|
2014-02-14 18:07:08 +04:00
|
|
|
$backend1->expects($this->any())
|
|
|
|
->method('groupExists')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true);
|
2013-07-10 02:06:22 +04:00
|
|
|
|
|
|
|
/**
|
2020-08-11 22:32:18 +03:00
|
|
|
* @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend2
|
2013-07-10 02:06:22 +04:00
|
|
|
*/
|
2016-11-30 22:21:44 +03:00
|
|
|
$backend2 = $this->getTestBackend();
|
2013-07-10 02:06:22 +04:00
|
|
|
$backend2->expects($this->once())
|
|
|
|
->method('getGroups')
|
|
|
|
->with('1')
|
2020-03-26 11:30:18 +03:00
|
|
|
->willReturn(['group12', 'group1']);
|
2014-02-14 18:07:08 +04:00
|
|
|
$backend2->expects($this->any())
|
|
|
|
->method('groupExists')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true);
|
2013-07-10 02:06:22 +04:00
|
|
|
|
2019-02-22 15:07:26 +03:00
|
|
|
$manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
|
2013-07-10 02:06:22 +04:00
|
|
|
$manager->addBackend($backend1);
|
|
|
|
$manager->addBackend($backend2);
|
|
|
|
|
|
|
|
$groups = $manager->search('1');
|
2017-03-17 12:34:25 +03:00
|
|
|
$this->assertCount(2, $groups);
|
2014-06-24 18:27:38 +04:00
|
|
|
$group1 = reset($groups);
|
|
|
|
$group12 = next($groups);
|
2013-07-10 02:06:22 +04:00
|
|
|
$this->assertEquals('group1', $group1->getGID());
|
|
|
|
$this->assertEquals('group12', $group12->getGID());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSearchMultipleBackendsLimitAndOffset() {
|
|
|
|
/**
|
2020-08-11 22:32:18 +03:00
|
|
|
* @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend1
|
2013-07-10 02:06:22 +04:00
|
|
|
*/
|
2016-11-30 22:21:44 +03:00
|
|
|
$backend1 = $this->getTestBackend();
|
2013-07-10 02:06:22 +04:00
|
|
|
$backend1->expects($this->once())
|
|
|
|
->method('getGroups')
|
|
|
|
->with('1', 2, 1)
|
2020-03-26 11:30:18 +03:00
|
|
|
->willReturn(['group1']);
|
2014-02-14 18:07:08 +04:00
|
|
|
$backend1->expects($this->any())
|
|
|
|
->method('groupExists')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true);
|
2013-07-10 02:06:22 +04:00
|
|
|
|
|
|
|
/**
|
2020-08-11 22:32:18 +03:00
|
|
|
* @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend2
|
2013-07-10 02:06:22 +04:00
|
|
|
*/
|
2016-11-30 22:21:44 +03:00
|
|
|
$backend2 = $this->getTestBackend();
|
2013-07-10 02:06:22 +04:00
|
|
|
$backend2->expects($this->once())
|
|
|
|
->method('getGroups')
|
2014-04-15 19:53:57 +04:00
|
|
|
->with('1', 2, 1)
|
2020-03-26 11:30:18 +03:00
|
|
|
->willReturn(['group12']);
|
2014-02-14 18:07:08 +04:00
|
|
|
$backend2->expects($this->any())
|
|
|
|
->method('groupExists')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true);
|
2013-07-10 02:06:22 +04:00
|
|
|
|
2019-02-22 15:07:26 +03:00
|
|
|
$manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
|
2013-07-10 02:06:22 +04:00
|
|
|
$manager->addBackend($backend1);
|
|
|
|
$manager->addBackend($backend2);
|
|
|
|
|
|
|
|
$groups = $manager->search('1', 2, 1);
|
2017-03-17 12:34:25 +03:00
|
|
|
$this->assertCount(2, $groups);
|
2014-06-24 18:27:38 +04:00
|
|
|
$group1 = reset($groups);
|
|
|
|
$group12 = next($groups);
|
2013-07-10 02:06:22 +04:00
|
|
|
$this->assertEquals('group1', $group1->getGID());
|
|
|
|
$this->assertEquals('group12', $group12->getGID());
|
|
|
|
}
|
|
|
|
|
2017-01-19 17:02:46 +03:00
|
|
|
public function testSearchResultExistsButGroupDoesNot() {
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject|\OC\Group\Backend $backend */
|
2017-03-17 12:34:25 +03:00
|
|
|
$backend = $this->createMock(Database::class);
|
2017-01-19 17:02:46 +03:00
|
|
|
$backend->expects($this->once())
|
|
|
|
->method('getGroups')
|
|
|
|
->with('1')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(['group1']);
|
2017-01-19 17:02:46 +03:00
|
|
|
$backend->expects($this->once())
|
|
|
|
->method('groupExists')
|
|
|
|
->with('group1')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(false);
|
2017-01-19 17:02:46 +03:00
|
|
|
|
2017-03-17 12:34:25 +03:00
|
|
|
/** @var \OC\User\Manager $userManager */
|
|
|
|
$userManager = $this->createMock(Manager::class);
|
2017-01-19 17:02:46 +03:00
|
|
|
|
2019-02-22 15:07:26 +03:00
|
|
|
$manager = new \OC\Group\Manager($userManager, $this->dispatcher, $this->logger);
|
2017-01-19 17:02:46 +03:00
|
|
|
$manager->addBackend($backend);
|
|
|
|
|
|
|
|
$groups = $manager->search('1');
|
|
|
|
$this->assertEmpty($groups);
|
|
|
|
}
|
|
|
|
|
2013-07-10 02:06:22 +04:00
|
|
|
public function testGetUserGroups() {
|
|
|
|
/**
|
2020-08-11 22:32:18 +03:00
|
|
|
* @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
|
2013-07-10 02:06:22 +04:00
|
|
|
*/
|
2016-11-30 22:21:44 +03:00
|
|
|
$backend = $this->getTestBackend();
|
2013-07-10 02:06:22 +04:00
|
|
|
$backend->expects($this->once())
|
|
|
|
->method('getUserGroups')
|
|
|
|
->with('user1')
|
2020-03-26 11:30:18 +03:00
|
|
|
->willReturn(['group1']);
|
2014-02-14 18:07:08 +04:00
|
|
|
$backend->expects($this->any())
|
|
|
|
->method('groupExists')
|
|
|
|
->with('group1')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true);
|
2013-07-10 02:06:22 +04:00
|
|
|
|
2019-02-22 15:07:26 +03:00
|
|
|
$manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
|
2013-07-10 02:06:22 +04:00
|
|
|
$manager->addBackend($backend);
|
|
|
|
|
2016-11-30 22:21:44 +03:00
|
|
|
$groups = $manager->getUserGroups($this->getTestUser('user1'));
|
2017-03-17 12:34:25 +03:00
|
|
|
$this->assertCount(1, $groups);
|
2014-06-24 18:27:38 +04:00
|
|
|
$group1 = reset($groups);
|
2013-07-10 02:06:22 +04:00
|
|
|
$this->assertEquals('group1', $group1->getGID());
|
|
|
|
}
|
|
|
|
|
2015-11-06 12:40:09 +03:00
|
|
|
public function testGetUserGroupIds() {
|
2020-05-04 18:25:47 +03:00
|
|
|
/**
|
2020-08-11 22:32:18 +03:00
|
|
|
* @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
|
2020-05-04 18:25:47 +03:00
|
|
|
*/
|
|
|
|
$backend = $this->getTestBackend();
|
|
|
|
$backend->method('getUserGroups')
|
|
|
|
->willReturn(['123', 'abc']);
|
|
|
|
|
|
|
|
$manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
|
|
|
|
$manager->addBackend($backend);
|
2015-11-06 12:40:09 +03:00
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \OC\User\User|\PHPUnit\Framework\MockObject\MockObject $user */
|
2017-03-17 12:34:25 +03:00
|
|
|
$user = $this->createMock(IUser::class);
|
2015-11-06 12:40:09 +03:00
|
|
|
|
|
|
|
$groups = $manager->getUserGroupIds($user);
|
2017-03-17 12:34:25 +03:00
|
|
|
$this->assertCount(2, $groups);
|
2015-11-06 12:40:09 +03:00
|
|
|
|
|
|
|
foreach ($groups as $group) {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->assertIsString($group);
|
2015-11-06 12:40:09 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-19 17:02:46 +03:00
|
|
|
public function testGetUserGroupsWithDeletedGroup() {
|
|
|
|
/**
|
2020-08-11 22:32:18 +03:00
|
|
|
* @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
|
2017-01-19 17:02:46 +03:00
|
|
|
*/
|
2017-03-17 12:34:25 +03:00
|
|
|
$backend = $this->createMock(Database::class);
|
2017-01-19 17:02:46 +03:00
|
|
|
$backend->expects($this->once())
|
|
|
|
->method('getUserGroups')
|
|
|
|
->with('user1')
|
2020-03-26 11:30:18 +03:00
|
|
|
->willReturn(['group1']);
|
2017-01-19 17:02:46 +03:00
|
|
|
$backend->expects($this->any())
|
|
|
|
->method('groupExists')
|
|
|
|
->with('group1')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(false);
|
2017-01-19 17:02:46 +03:00
|
|
|
|
2019-02-22 15:07:26 +03:00
|
|
|
$manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
|
2017-01-19 17:02:46 +03:00
|
|
|
$manager->addBackend($backend);
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var \OC\User\User|\PHPUnit\Framework\MockObject\MockObject $user */
|
2017-01-19 17:02:46 +03:00
|
|
|
$user = $this->createMock(IUser::class);
|
2017-03-17 12:34:25 +03:00
|
|
|
$user->expects($this->atLeastOnce())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn('user1');
|
2017-01-19 17:02:46 +03:00
|
|
|
|
|
|
|
$groups = $manager->getUserGroups($user);
|
|
|
|
$this->assertEmpty($groups);
|
|
|
|
}
|
|
|
|
|
2014-12-16 22:33:13 +03:00
|
|
|
public function testInGroup() {
|
|
|
|
/**
|
2020-08-11 22:32:18 +03:00
|
|
|
* @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
|
2014-12-16 22:33:13 +03:00
|
|
|
*/
|
2016-11-30 22:21:44 +03:00
|
|
|
$backend = $this->getTestBackend();
|
2014-12-16 22:33:13 +03:00
|
|
|
$backend->expects($this->once())
|
|
|
|
->method('getUserGroups')
|
|
|
|
->with('user1')
|
2020-03-26 11:30:18 +03:00
|
|
|
->willReturn(['group1', 'admin', 'group2']);
|
2014-12-16 22:33:13 +03:00
|
|
|
$backend->expects($this->any())
|
|
|
|
->method('groupExists')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true);
|
2014-12-16 22:33:13 +03:00
|
|
|
|
2019-02-22 15:07:26 +03:00
|
|
|
$manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
|
2014-12-16 22:33:13 +03:00
|
|
|
$manager->addBackend($backend);
|
|
|
|
|
|
|
|
$this->assertTrue($manager->isInGroup('user1', 'group1'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsAdmin() {
|
|
|
|
/**
|
2020-08-11 22:32:18 +03:00
|
|
|
* @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
|
2014-12-16 22:33:13 +03:00
|
|
|
*/
|
2016-11-30 22:21:44 +03:00
|
|
|
$backend = $this->getTestBackend();
|
2014-12-16 22:33:13 +03:00
|
|
|
$backend->expects($this->once())
|
|
|
|
->method('getUserGroups')
|
|
|
|
->with('user1')
|
2020-03-26 11:30:18 +03:00
|
|
|
->willReturn(['group1', 'admin', 'group2']);
|
2014-12-16 22:33:13 +03:00
|
|
|
$backend->expects($this->any())
|
|
|
|
->method('groupExists')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true);
|
2014-12-16 22:33:13 +03:00
|
|
|
|
2019-02-22 15:07:26 +03:00
|
|
|
$manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
|
2014-12-16 22:33:13 +03:00
|
|
|
$manager->addBackend($backend);
|
|
|
|
|
|
|
|
$this->assertTrue($manager->isAdmin('user1'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNotAdmin() {
|
|
|
|
/**
|
2020-08-11 22:32:18 +03:00
|
|
|
* @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
|
2014-12-16 22:33:13 +03:00
|
|
|
*/
|
2016-11-30 22:21:44 +03:00
|
|
|
$backend = $this->getTestBackend();
|
2014-12-16 22:33:13 +03:00
|
|
|
$backend->expects($this->once())
|
|
|
|
->method('getUserGroups')
|
|
|
|
->with('user1')
|
2020-03-26 11:30:18 +03:00
|
|
|
->willReturn(['group1', 'group2']);
|
2014-12-16 22:33:13 +03:00
|
|
|
$backend->expects($this->any())
|
|
|
|
->method('groupExists')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true);
|
2014-12-16 22:33:13 +03:00
|
|
|
|
2019-02-22 15:07:26 +03:00
|
|
|
$manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
|
2014-12-16 22:33:13 +03:00
|
|
|
$manager->addBackend($backend);
|
|
|
|
|
|
|
|
$this->assertFalse($manager->isAdmin('user1'));
|
|
|
|
}
|
|
|
|
|
2013-07-10 02:06:22 +04:00
|
|
|
public function testGetUserGroupsMultipleBackends() {
|
|
|
|
/**
|
2020-08-11 22:32:18 +03:00
|
|
|
* @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend1
|
2013-07-10 02:06:22 +04:00
|
|
|
*/
|
2016-11-30 22:21:44 +03:00
|
|
|
$backend1 = $this->getTestBackend();
|
2013-07-10 02:06:22 +04:00
|
|
|
$backend1->expects($this->once())
|
|
|
|
->method('getUserGroups')
|
|
|
|
->with('user1')
|
2020-03-26 11:30:18 +03:00
|
|
|
->willReturn(['group1']);
|
2014-02-14 18:07:08 +04:00
|
|
|
$backend1->expects($this->any())
|
|
|
|
->method('groupExists')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true);
|
2014-02-14 18:07:08 +04:00
|
|
|
|
2013-07-10 02:06:22 +04:00
|
|
|
/**
|
2020-08-11 22:32:18 +03:00
|
|
|
* @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend2
|
2013-07-10 02:06:22 +04:00
|
|
|
*/
|
2016-11-30 22:21:44 +03:00
|
|
|
$backend2 = $this->getTestBackend();
|
2013-07-10 02:06:22 +04:00
|
|
|
$backend2->expects($this->once())
|
|
|
|
->method('getUserGroups')
|
|
|
|
->with('user1')
|
2020-03-26 11:30:18 +03:00
|
|
|
->willReturn(['group1', 'group2']);
|
2014-02-14 18:07:08 +04:00
|
|
|
$backend1->expects($this->any())
|
|
|
|
->method('groupExists')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true);
|
2013-07-10 02:06:22 +04:00
|
|
|
|
2019-02-22 15:07:26 +03:00
|
|
|
$manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
|
2013-07-10 02:06:22 +04:00
|
|
|
$manager->addBackend($backend1);
|
|
|
|
$manager->addBackend($backend2);
|
|
|
|
|
2016-11-30 22:21:44 +03:00
|
|
|
$groups = $manager->getUserGroups($this->getTestUser('user1'));
|
2017-03-17 12:34:25 +03:00
|
|
|
$this->assertCount(2, $groups);
|
2014-06-24 18:27:38 +04:00
|
|
|
$group1 = reset($groups);
|
|
|
|
$group2 = next($groups);
|
2013-07-10 02:06:22 +04:00
|
|
|
$this->assertEquals('group1', $group1->getGID());
|
|
|
|
$this->assertEquals('group2', $group2->getGID());
|
|
|
|
}
|
2014-03-14 16:51:17 +04:00
|
|
|
|
2016-11-30 22:21:44 +03:00
|
|
|
public function testDisplayNamesInGroupWithOneUserBackend() {
|
2014-08-28 15:51:48 +04:00
|
|
|
/**
|
2020-08-11 22:32:18 +03:00
|
|
|
* @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
|
2014-08-28 15:51:48 +04:00
|
|
|
*/
|
2016-11-30 22:21:44 +03:00
|
|
|
$backend = $this->getTestBackend();
|
2014-08-28 15:51:48 +04:00
|
|
|
$backend->expects($this->exactly(1))
|
|
|
|
->method('groupExists')
|
|
|
|
->with('testgroup')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true);
|
2014-08-28 15:51:48 +04:00
|
|
|
|
2016-11-30 22:21:44 +03:00
|
|
|
$backend->expects($this->any())
|
|
|
|
->method('inGroup')
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function ($uid, $gid) {
|
2020-04-10 15:19:56 +03:00
|
|
|
switch ($uid) {
|
2020-04-09 17:17:53 +03:00
|
|
|
case 'user1': return false;
|
|
|
|
case 'user2': return true;
|
|
|
|
case 'user3': return false;
|
2016-11-30 22:21:44 +03:00
|
|
|
case 'user33': return true;
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2014-08-28 15:51:48 +04:00
|
|
|
|
2016-07-11 17:53:01 +03:00
|
|
|
$this->userManager->expects($this->any())
|
2015-03-24 23:42:40 +03:00
|
|
|
->method('searchDisplayName')
|
2014-08-28 15:51:48 +04:00
|
|
|
->with('user3')
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function ($search, $limit, $offset) {
|
2020-04-10 15:19:56 +03:00
|
|
|
switch ($offset) {
|
2020-04-09 17:17:53 +03:00
|
|
|
case 0: return ['user3' => $this->getTestUser('user3'),
|
2020-04-09 10:22:29 +03:00
|
|
|
'user33' => $this->getTestUser('user33')];
|
2020-04-09 17:17:53 +03:00
|
|
|
case 2: return [];
|
2016-11-30 22:21:44 +03:00
|
|
|
}
|
2016-12-22 22:53:52 +03:00
|
|
|
return null;
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2016-07-11 17:53:01 +03:00
|
|
|
$this->userManager->expects($this->any())
|
2014-08-28 15:51:48 +04:00
|
|
|
->method('get')
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function ($uid) {
|
2020-04-10 15:19:56 +03:00
|
|
|
switch ($uid) {
|
2020-04-09 17:17:53 +03:00
|
|
|
case 'user1': return $this->getTestUser('user1');
|
|
|
|
case 'user2': return $this->getTestUser('user2');
|
|
|
|
case 'user3': return $this->getTestUser('user3');
|
2016-11-30 22:21:44 +03:00
|
|
|
case 'user33': return $this->getTestUser('user33');
|
2014-08-28 15:51:48 +04:00
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2014-08-28 15:51:48 +04:00
|
|
|
|
2019-02-22 15:07:26 +03:00
|
|
|
$manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
|
2014-08-28 15:51:48 +04:00
|
|
|
$manager->addBackend($backend);
|
|
|
|
|
|
|
|
$users = $manager->displayNamesInGroup('testgroup', 'user3');
|
2017-03-17 12:34:25 +03:00
|
|
|
$this->assertCount(1, $users);
|
2014-08-28 15:51:48 +04:00
|
|
|
$this->assertFalse(isset($users['user1']));
|
|
|
|
$this->assertFalse(isset($users['user2']));
|
|
|
|
$this->assertFalse(isset($users['user3']));
|
|
|
|
$this->assertTrue(isset($users['user33']));
|
|
|
|
}
|
|
|
|
|
2016-11-30 22:21:44 +03:00
|
|
|
public function testDisplayNamesInGroupWithOneUserBackendWithLimitSpecified() {
|
2014-03-14 16:51:17 +04:00
|
|
|
/**
|
2020-08-11 22:32:18 +03:00
|
|
|
* @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
|
2014-03-14 16:51:17 +04:00
|
|
|
*/
|
2016-11-30 22:21:44 +03:00
|
|
|
$backend = $this->getTestBackend();
|
2014-04-28 21:19:37 +04:00
|
|
|
$backend->expects($this->exactly(1))
|
2014-03-14 16:51:17 +04:00
|
|
|
->method('groupExists')
|
|
|
|
->with('testgroup')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true);
|
2014-03-14 16:51:17 +04:00
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
$backend->expects($this->any())
|
2016-11-30 22:21:44 +03:00
|
|
|
->method('inGroup')
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function ($uid, $gid) {
|
2020-04-10 15:19:56 +03:00
|
|
|
switch ($uid) {
|
2020-04-09 17:17:53 +03:00
|
|
|
case 'user1': return false;
|
|
|
|
case 'user2': return true;
|
|
|
|
case 'user3': return false;
|
2016-11-30 22:21:44 +03:00
|
|
|
case 'user33': return true;
|
|
|
|
case 'user333': return true;
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2014-03-14 16:51:17 +04:00
|
|
|
|
2016-07-11 17:53:01 +03:00
|
|
|
$this->userManager->expects($this->any())
|
2015-03-24 23:42:40 +03:00
|
|
|
->method('searchDisplayName')
|
2014-03-14 16:51:17 +04:00
|
|
|
->with('user3')
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function ($search, $limit, $offset) {
|
2020-04-10 15:19:56 +03:00
|
|
|
switch ($offset) {
|
2020-04-09 17:17:53 +03:00
|
|
|
case 0: return ['user3' => $this->getTestUser('user3'),
|
2020-04-09 10:22:29 +03:00
|
|
|
'user33' => $this->getTestUser('user33')];
|
2020-04-09 17:17:53 +03:00
|
|
|
case 2: return ['user333' => $this->getTestUser('user333')];
|
2016-11-30 22:21:44 +03:00
|
|
|
}
|
2016-12-22 22:53:52 +03:00
|
|
|
return null;
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2016-07-11 17:53:01 +03:00
|
|
|
$this->userManager->expects($this->any())
|
2014-03-14 16:51:17 +04:00
|
|
|
->method('get')
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function ($uid) {
|
2020-04-10 15:19:56 +03:00
|
|
|
switch ($uid) {
|
2020-04-09 17:17:53 +03:00
|
|
|
case 'user1': return $this->getTestUser('user1');
|
|
|
|
case 'user2': return $this->getTestUser('user2');
|
|
|
|
case 'user3': return $this->getTestUser('user3');
|
2016-11-30 22:21:44 +03:00
|
|
|
case 'user33': return $this->getTestUser('user33');
|
|
|
|
case 'user333': return $this->getTestUser('user333');
|
2014-03-14 16:51:17 +04:00
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2014-03-14 16:51:17 +04:00
|
|
|
|
2019-02-22 15:07:26 +03:00
|
|
|
$manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
|
2014-03-14 16:51:17 +04:00
|
|
|
$manager->addBackend($backend);
|
|
|
|
|
2014-08-28 15:51:48 +04:00
|
|
|
$users = $manager->displayNamesInGroup('testgroup', 'user3', 1);
|
2017-03-17 12:34:25 +03:00
|
|
|
$this->assertCount(1, $users);
|
2014-08-28 15:51:48 +04:00
|
|
|
$this->assertFalse(isset($users['user1']));
|
|
|
|
$this->assertFalse(isset($users['user2']));
|
|
|
|
$this->assertFalse(isset($users['user3']));
|
|
|
|
$this->assertTrue(isset($users['user33']));
|
|
|
|
$this->assertFalse(isset($users['user333']));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDisplayNamesInGroupWithOneUserBackendWithLimitAndOffsetSpecified() {
|
|
|
|
/**
|
2020-08-11 22:32:18 +03:00
|
|
|
* @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
|
2014-08-28 15:51:48 +04:00
|
|
|
*/
|
2016-11-30 22:21:44 +03:00
|
|
|
$backend = $this->getTestBackend();
|
2014-08-28 15:51:48 +04:00
|
|
|
$backend->expects($this->exactly(1))
|
|
|
|
->method('groupExists')
|
|
|
|
->with('testgroup')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true);
|
2014-08-28 15:51:48 +04:00
|
|
|
|
2016-11-30 22:21:44 +03:00
|
|
|
$backend->expects($this->any())
|
2014-10-17 22:53:09 +04:00
|
|
|
->method('inGroup')
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function ($uid) {
|
2020-04-10 15:19:56 +03:00
|
|
|
switch ($uid) {
|
2020-04-09 17:17:53 +03:00
|
|
|
case 'user1': return false;
|
|
|
|
case 'user2': return true;
|
|
|
|
case 'user3': return false;
|
2016-11-30 22:21:44 +03:00
|
|
|
case 'user33': return true;
|
|
|
|
case 'user333': return true;
|
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2014-08-28 15:51:48 +04:00
|
|
|
|
2016-07-11 17:53:01 +03:00
|
|
|
$this->userManager->expects($this->any())
|
2015-03-24 23:42:40 +03:00
|
|
|
->method('searchDisplayName')
|
2014-08-28 15:51:48 +04:00
|
|
|
->with('user3')
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function ($search, $limit, $offset) {
|
2020-04-10 15:19:56 +03:00
|
|
|
switch ($offset) {
|
2020-04-09 17:17:53 +03:00
|
|
|
case 0:
|
2016-12-22 22:53:52 +03:00
|
|
|
return [
|
|
|
|
'user3' => $this->getTestUser('user3'),
|
|
|
|
'user33' => $this->getTestUser('user33'),
|
|
|
|
'user333' => $this->getTestUser('user333')
|
|
|
|
];
|
|
|
|
}
|
|
|
|
return null;
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2016-07-11 17:53:01 +03:00
|
|
|
$this->userManager->expects($this->any())
|
2014-08-28 15:51:48 +04:00
|
|
|
->method('get')
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function ($uid) {
|
2020-04-10 15:19:56 +03:00
|
|
|
switch ($uid) {
|
2020-04-09 17:17:53 +03:00
|
|
|
case 'user1': return $this->getTestUser('user1');
|
|
|
|
case 'user2': return $this->getTestUser('user2');
|
|
|
|
case 'user3': return $this->getTestUser('user3');
|
2016-11-30 22:21:44 +03:00
|
|
|
case 'user33': return $this->getTestUser('user33');
|
|
|
|
case 'user333': return $this->getTestUser('user333');
|
2014-08-28 15:51:48 +04:00
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2014-08-28 15:51:48 +04:00
|
|
|
|
2019-02-22 15:07:26 +03:00
|
|
|
$manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
|
2014-08-28 15:51:48 +04:00
|
|
|
$manager->addBackend($backend);
|
|
|
|
|
|
|
|
$users = $manager->displayNamesInGroup('testgroup', 'user3', 1, 1);
|
2017-03-17 12:34:25 +03:00
|
|
|
$this->assertCount(1, $users);
|
2014-08-28 15:51:48 +04:00
|
|
|
$this->assertFalse(isset($users['user1']));
|
|
|
|
$this->assertFalse(isset($users['user2']));
|
|
|
|
$this->assertFalse(isset($users['user3']));
|
|
|
|
$this->assertFalse(isset($users['user33']));
|
|
|
|
$this->assertTrue(isset($users['user333']));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDisplayNamesInGroupWithOneUserBackendAndSearchEmpty() {
|
|
|
|
/**
|
2020-08-11 22:32:18 +03:00
|
|
|
* @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
|
2014-08-28 15:51:48 +04:00
|
|
|
*/
|
2016-11-30 22:21:44 +03:00
|
|
|
$backend = $this->getTestBackend();
|
2014-08-28 15:51:48 +04:00
|
|
|
$backend->expects($this->exactly(1))
|
|
|
|
->method('groupExists')
|
|
|
|
->with('testgroup')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true);
|
2014-08-28 15:51:48 +04:00
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
$backend->expects($this->once())
|
2014-08-28 15:51:48 +04:00
|
|
|
->method('usersInGroup')
|
|
|
|
->with('testgroup', '', -1, 0)
|
2020-03-26 11:30:18 +03:00
|
|
|
->willReturn(['user2', 'user33']);
|
2014-08-28 15:51:48 +04:00
|
|
|
|
2016-07-11 17:53:01 +03:00
|
|
|
$this->userManager->expects($this->any())
|
2014-08-28 15:51:48 +04:00
|
|
|
->method('get')
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function ($uid) {
|
2020-04-10 15:19:56 +03:00
|
|
|
switch ($uid) {
|
2020-04-09 17:17:53 +03:00
|
|
|
case 'user1': return $this->getTestUser('user1');
|
|
|
|
case 'user2': return $this->getTestUser('user2');
|
|
|
|
case 'user3': return $this->getTestUser('user3');
|
2016-11-30 22:21:44 +03:00
|
|
|
case 'user33': return $this->getTestUser('user33');
|
2014-08-28 15:51:48 +04:00
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2014-08-28 15:51:48 +04:00
|
|
|
|
2019-02-22 15:07:26 +03:00
|
|
|
$manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
|
2014-08-28 15:51:48 +04:00
|
|
|
$manager->addBackend($backend);
|
|
|
|
|
|
|
|
$users = $manager->displayNamesInGroup('testgroup', '');
|
2017-03-17 12:34:25 +03:00
|
|
|
$this->assertCount(2, $users);
|
2014-08-28 15:51:48 +04:00
|
|
|
$this->assertFalse(isset($users['user1']));
|
|
|
|
$this->assertTrue(isset($users['user2']));
|
|
|
|
$this->assertFalse(isset($users['user3']));
|
|
|
|
$this->assertTrue(isset($users['user33']));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDisplayNamesInGroupWithOneUserBackendAndSearchEmptyAndLimitSpecified() {
|
|
|
|
/**
|
2020-08-11 22:32:18 +03:00
|
|
|
* @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
|
2014-08-28 15:51:48 +04:00
|
|
|
*/
|
2016-11-30 22:21:44 +03:00
|
|
|
$backend = $this->getTestBackend();
|
2014-08-28 15:51:48 +04:00
|
|
|
$backend->expects($this->exactly(1))
|
|
|
|
->method('groupExists')
|
|
|
|
->with('testgroup')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true);
|
2014-08-28 15:51:48 +04:00
|
|
|
|
|
|
|
$backend->expects($this->once())
|
|
|
|
->method('usersInGroup')
|
|
|
|
->with('testgroup', '', 1, 0)
|
2020-03-26 11:30:18 +03:00
|
|
|
->willReturn(['user2']);
|
2014-08-28 15:51:48 +04:00
|
|
|
|
2016-07-11 17:53:01 +03:00
|
|
|
$this->userManager->expects($this->any())
|
2014-08-28 15:51:48 +04:00
|
|
|
->method('get')
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function ($uid) {
|
2020-04-10 15:19:56 +03:00
|
|
|
switch ($uid) {
|
2020-04-09 17:17:53 +03:00
|
|
|
case 'user1': return $this->getTestUser('user1');
|
|
|
|
case 'user2': return $this->getTestUser('user2');
|
|
|
|
case 'user3': return $this->getTestUser('user3');
|
2016-11-30 22:21:44 +03:00
|
|
|
case 'user33': return $this->getTestUser('user33');
|
2014-08-28 15:51:48 +04:00
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2014-08-28 15:51:48 +04:00
|
|
|
|
2019-02-22 15:07:26 +03:00
|
|
|
$manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
|
2014-08-28 15:51:48 +04:00
|
|
|
$manager->addBackend($backend);
|
|
|
|
|
|
|
|
$users = $manager->displayNamesInGroup('testgroup', '', 1);
|
2017-03-17 12:34:25 +03:00
|
|
|
$this->assertCount(1, $users);
|
2014-08-28 15:51:48 +04:00
|
|
|
$this->assertFalse(isset($users['user1']));
|
|
|
|
$this->assertTrue(isset($users['user2']));
|
|
|
|
$this->assertFalse(isset($users['user3']));
|
|
|
|
$this->assertFalse(isset($users['user33']));
|
|
|
|
}
|
|
|
|
|
2016-11-30 22:21:44 +03:00
|
|
|
public function testDisplayNamesInGroupWithOneUserBackendAndSearchEmptyAndLimitAndOffsetSpecified() {
|
2014-08-28 15:51:48 +04:00
|
|
|
/**
|
2020-08-11 22:32:18 +03:00
|
|
|
* @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
|
2014-08-28 15:51:48 +04:00
|
|
|
*/
|
2016-11-30 22:21:44 +03:00
|
|
|
$backend = $this->getTestBackend();
|
2014-08-28 15:51:48 +04:00
|
|
|
$backend->expects($this->exactly(1))
|
|
|
|
->method('groupExists')
|
|
|
|
->with('testgroup')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true);
|
2014-08-28 15:51:48 +04:00
|
|
|
|
|
|
|
$backend->expects($this->once())
|
|
|
|
->method('usersInGroup')
|
|
|
|
->with('testgroup', '', 1, 1)
|
2020-03-26 11:30:18 +03:00
|
|
|
->willReturn(['user33']);
|
2014-08-28 15:51:48 +04:00
|
|
|
|
2016-07-11 17:53:01 +03:00
|
|
|
$this->userManager->expects($this->any())
|
2014-08-28 15:51:48 +04:00
|
|
|
->method('get')
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function ($uid) {
|
2020-04-10 15:19:56 +03:00
|
|
|
switch ($uid) {
|
2020-04-09 17:17:53 +03:00
|
|
|
case 'user1': return $this->getTestUser('user1');
|
|
|
|
case 'user2': return $this->getTestUser('user2');
|
|
|
|
case 'user3': return $this->getTestUser('user3');
|
2016-11-30 22:21:44 +03:00
|
|
|
case 'user33': return $this->getTestUser('user33');
|
2014-08-28 15:51:48 +04:00
|
|
|
default:
|
|
|
|
return null;
|
|
|
|
}
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2014-08-28 15:51:48 +04:00
|
|
|
|
2019-02-22 15:07:26 +03:00
|
|
|
$manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
|
2014-08-28 15:51:48 +04:00
|
|
|
$manager->addBackend($backend);
|
|
|
|
|
|
|
|
$users = $manager->displayNamesInGroup('testgroup', '', 1, 1);
|
2017-03-17 12:34:25 +03:00
|
|
|
$this->assertCount(1, $users);
|
2014-08-28 15:51:48 +04:00
|
|
|
$this->assertFalse(isset($users['user1']));
|
|
|
|
$this->assertFalse(isset($users['user2']));
|
|
|
|
$this->assertFalse(isset($users['user3']));
|
2014-03-14 16:51:17 +04:00
|
|
|
$this->assertTrue(isset($users['user33']));
|
|
|
|
}
|
2014-05-07 19:54:38 +04:00
|
|
|
|
2014-02-28 00:04:44 +04:00
|
|
|
public function testGetUserGroupsWithAddUser() {
|
|
|
|
/**
|
2020-08-11 22:32:18 +03:00
|
|
|
* @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
|
2014-02-28 00:04:44 +04:00
|
|
|
*/
|
2016-11-30 22:21:44 +03:00
|
|
|
$backend = $this->getTestBackend();
|
|
|
|
$expectedGroups = [];
|
2014-02-28 00:04:44 +04:00
|
|
|
$backend->expects($this->any())
|
|
|
|
->method('getUserGroups')
|
|
|
|
->with('user1')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturnCallback(function () use (&$expectedGroups) {
|
2014-02-28 00:04:44 +04:00
|
|
|
return $expectedGroups;
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2014-02-28 00:04:44 +04:00
|
|
|
$backend->expects($this->any())
|
|
|
|
->method('groupExists')
|
|
|
|
->with('group1')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true);
|
2014-02-28 00:04:44 +04:00
|
|
|
|
2019-02-22 15:07:26 +03:00
|
|
|
$manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
|
2014-02-28 00:04:44 +04:00
|
|
|
$manager->addBackend($backend);
|
|
|
|
|
|
|
|
// prime cache
|
2016-11-30 22:21:44 +03:00
|
|
|
$user1 = $this->getTestUser('user1');
|
2014-02-28 00:04:44 +04:00
|
|
|
$groups = $manager->getUserGroups($user1);
|
2020-03-26 11:30:18 +03:00
|
|
|
$this->assertEquals([], $groups);
|
2014-02-28 00:04:44 +04:00
|
|
|
|
|
|
|
// add user
|
|
|
|
$group = $manager->get('group1');
|
|
|
|
$group->addUser($user1);
|
2017-03-17 12:34:25 +03:00
|
|
|
$expectedGroups[] = 'group1';
|
2014-02-28 00:04:44 +04:00
|
|
|
|
|
|
|
// check result
|
|
|
|
$groups = $manager->getUserGroups($user1);
|
2017-03-17 12:34:25 +03:00
|
|
|
$this->assertCount(1, $groups);
|
2014-06-24 18:27:38 +04:00
|
|
|
$group1 = reset($groups);
|
2014-02-28 00:04:44 +04:00
|
|
|
$this->assertEquals('group1', $group1->getGID());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetUserGroupsWithRemoveUser() {
|
|
|
|
/**
|
2020-08-11 22:32:18 +03:00
|
|
|
* @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
|
2014-02-28 00:04:44 +04:00
|
|
|
*/
|
2016-11-30 22:21:44 +03:00
|
|
|
$backend = $this->getTestBackend();
|
|
|
|
$expectedGroups = ['group1'];
|
2014-02-28 00:04:44 +04:00
|
|
|
$backend->expects($this->any())
|
|
|
|
->method('getUserGroups')
|
|
|
|
->with('user1')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturnCallback(function () use (&$expectedGroups) {
|
2014-02-28 00:04:44 +04:00
|
|
|
return $expectedGroups;
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2014-02-28 00:04:44 +04:00
|
|
|
$backend->expects($this->any())
|
|
|
|
->method('groupExists')
|
|
|
|
->with('group1')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true);
|
2014-02-28 00:04:44 +04:00
|
|
|
$backend->expects($this->once())
|
|
|
|
->method('inGroup')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true);
|
2014-02-28 00:04:44 +04:00
|
|
|
$backend->expects($this->once())
|
|
|
|
->method('removeFromGroup')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(true);
|
2014-02-28 00:04:44 +04:00
|
|
|
|
2019-02-22 15:07:26 +03:00
|
|
|
$manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
|
2014-02-28 00:04:44 +04:00
|
|
|
$manager->addBackend($backend);
|
|
|
|
|
|
|
|
// prime cache
|
2016-11-30 22:21:44 +03:00
|
|
|
$user1 = $this->getTestUser('user1');
|
2014-02-28 00:04:44 +04:00
|
|
|
$groups = $manager->getUserGroups($user1);
|
2017-03-17 12:34:25 +03:00
|
|
|
$this->assertCount(1, $groups);
|
2014-06-24 18:27:38 +04:00
|
|
|
$group1 = reset($groups);
|
2014-02-28 00:04:44 +04:00
|
|
|
$this->assertEquals('group1', $group1->getGID());
|
|
|
|
|
|
|
|
// remove user
|
|
|
|
$group = $manager->get('group1');
|
|
|
|
$group->removeUser($user1);
|
2020-03-26 11:30:18 +03:00
|
|
|
$expectedGroups = [];
|
2014-02-28 00:04:44 +04:00
|
|
|
|
|
|
|
// check result
|
|
|
|
$groups = $manager->getUserGroups($user1);
|
2016-07-11 17:53:01 +03:00
|
|
|
$this->assertEquals($expectedGroups, $groups);
|
2014-02-28 00:04:44 +04:00
|
|
|
}
|
2015-04-14 12:00:20 +03:00
|
|
|
|
|
|
|
public function testGetUserIdGroups() {
|
|
|
|
/**
|
2020-08-11 22:32:18 +03:00
|
|
|
* @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
|
2015-04-14 12:00:20 +03:00
|
|
|
*/
|
2016-11-30 22:21:44 +03:00
|
|
|
$backend = $this->getTestBackend();
|
2015-04-14 12:00:20 +03:00
|
|
|
$backend->expects($this->any())
|
|
|
|
->method('getUserGroups')
|
|
|
|
->with('user1')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(null);
|
2015-04-14 12:00:20 +03:00
|
|
|
|
2019-02-22 15:07:26 +03:00
|
|
|
$manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
|
2015-04-14 12:00:20 +03:00
|
|
|
$manager->addBackend($backend);
|
|
|
|
|
|
|
|
$groups = $manager->getUserIdGroups('user1');
|
|
|
|
$this->assertEquals([], $groups);
|
|
|
|
}
|
|
|
|
|
2016-11-30 22:21:44 +03:00
|
|
|
public function testGroupDisplayName() {
|
|
|
|
/**
|
2020-08-11 22:32:18 +03:00
|
|
|
* @var \PHPUnit\Framework\MockObject\MockObject | \OC\Group\Backend $backend
|
2016-11-30 22:21:44 +03:00
|
|
|
*/
|
|
|
|
$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')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturnMap([
|
2016-11-30 22:21:44 +03:00
|
|
|
['group1', ['gid' => 'group1', 'displayName' => 'Group One']],
|
|
|
|
['group2', ['gid' => 'group2']],
|
2020-03-26 00:21:27 +03:00
|
|
|
]);
|
2016-11-30 22:21:44 +03:00
|
|
|
|
2019-02-22 15:07:26 +03:00
|
|
|
$manager = new \OC\Group\Manager($this->userManager, $this->dispatcher, $this->logger);
|
2016-11-30 22:21:44 +03:00
|
|
|
$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());
|
|
|
|
}
|
2013-07-10 02:06:22 +04:00
|
|
|
}
|