diff options
author | Joas Schilling <nickvergessen@gmx.de> | 2016-05-20 15:38:20 +0200 |
---|---|---|
committer | Thomas Müller <DeepDiver1975@users.noreply.github.com> | 2016-05-20 15:38:20 +0200 |
commit | 94ad54ec9b96d41a614fbbad4a97b34c41a6901f (patch) | |
tree | f3eb7cdda2704aaf0cd59d58efe66bcbd34cb67d /tests/lib/Group | |
parent | 2ef751b1ec28f7b5c7113af60ec8c9fa0ae1cf87 (diff) | |
download | nextcloud-server-94ad54ec9b96d41a614fbbad4a97b34c41a6901f.tar.gz nextcloud-server-94ad54ec9b96d41a614fbbad4a97b34c41a6901f.zip |
Move tests/ to PSR-4 (#24731)
* Move a-b to PSR-4
* Move c-d to PSR-4
* Move e+g to PSR-4
* Move h-l to PSR-4
* Move m-r to PSR-4
* Move s-u to PSR-4
* Move files/ to PSR-4
* Move remaining tests to PSR-4
* Remove Test\ from old autoloader
Diffstat (limited to 'tests/lib/Group')
-rw-r--r-- | tests/lib/Group/Backend.php | 161 | ||||
-rw-r--r-- | tests/lib/Group/DatabaseTest.php | 68 | ||||
-rw-r--r-- | tests/lib/Group/Dummy.php | 35 | ||||
-rw-r--r-- | tests/lib/Group/GroupTest.php | 386 | ||||
-rw-r--r-- | tests/lib/Group/LegacyGroupTest.php | 202 | ||||
-rw-r--r-- | tests/lib/Group/ManagerTest.php | 897 | ||||
-rw-r--r-- | tests/lib/Group/MetaDataTest.php | 136 |
7 files changed, 1885 insertions, 0 deletions
diff --git a/tests/lib/Group/Backend.php b/tests/lib/Group/Backend.php new file mode 100644 index 00000000000..eb17e9bded7 --- /dev/null +++ b/tests/lib/Group/Backend.php @@ -0,0 +1,161 @@ +<?php +/** + * @author Arthur Schiwon <blizzz@owncloud.com> + * @author Felix Moeller <mail@felixmoeller.de> + * @author Joas Schilling <nickvergessen@owncloud.com> + * @author Robin Appelman <icewind@owncloud.com> + * @author Scrutinizer Auto-Fixer <auto-fixer@scrutinizer-ci.com> + * @author Thomas Müller <thomas.mueller@tmit.eu> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @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; + +/** + * Class Backend + * + * @group DB + */ +abstract class Backend extends \Test\TestCase { + /** + * @var \OC\Group\Backend $backend + */ + protected $backend; + + /** + * get a new unique group name + * test cases can override this in order to clean up created groups + * + * @return string + */ + public function getGroupName($name = null) { + if (is_null($name)) { + return $this->getUniqueID('test_'); + } else { + return $name; + } + } + + /** + * get a new unique user name + * test cases can override this in order to clean up created user + * + * @return string + */ + public function getUserName() { + return $this->getUniqueID('test_'); + } + + public function testAddRemove() { + //get the number of groups we start with, in case there are exising groups + $startCount = count($this->backend->getGroups()); + + $name1 = $this->getGroupName(); + $name2 = $this->getGroupName(); + $this->backend->createGroup($name1); + $count = count($this->backend->getGroups()) - $startCount; + $this->assertEquals(1, $count); + $this->assertTrue((array_search($name1, $this->backend->getGroups()) !== false)); + $this->assertFalse((array_search($name2, $this->backend->getGroups()) !== false)); + $this->backend->createGroup($name2); + $count = count($this->backend->getGroups()) - $startCount; + $this->assertEquals(2, $count); + $this->assertTrue((array_search($name1, $this->backend->getGroups()) !== false)); + $this->assertTrue((array_search($name2, $this->backend->getGroups()) !== false)); + + $this->backend->deleteGroup($name2); + $count = count($this->backend->getGroups()) - $startCount; + $this->assertEquals(1, $count); + $this->assertTrue((array_search($name1, $this->backend->getGroups()) !== false)); + $this->assertFalse((array_search($name2, $this->backend->getGroups()) !== false)); + } + + public function testUser() { + $group1 = $this->getGroupName(); + $group2 = $this->getGroupName(); + $this->backend->createGroup($group1); + $this->backend->createGroup($group2); + + $user1 = $this->getUserName(); + $user2 = $this->getUserName(); + + $this->assertFalse($this->backend->inGroup($user1, $group1)); + $this->assertFalse($this->backend->inGroup($user2, $group1)); + $this->assertFalse($this->backend->inGroup($user1, $group2)); + $this->assertFalse($this->backend->inGroup($user2, $group2)); + + $this->assertTrue($this->backend->addToGroup($user1, $group1)); + + $this->assertTrue($this->backend->inGroup($user1, $group1)); + $this->assertFalse($this->backend->inGroup($user2, $group1)); + $this->assertFalse($this->backend->inGroup($user1, $group2)); + $this->assertFalse($this->backend->inGroup($user2, $group2)); + + $this->assertFalse($this->backend->addToGroup($user1, $group1)); + + $this->assertEquals(array($user1), $this->backend->usersInGroup($group1)); + $this->assertEquals(array(), $this->backend->usersInGroup($group2)); + + $this->assertEquals(array($group1), $this->backend->getUserGroups($user1)); + $this->assertEquals(array(), $this->backend->getUserGroups($user2)); + + $this->backend->deleteGroup($group1); + $this->assertEquals(array(), $this->backend->getUserGroups($user1)); + $this->assertEquals(array(), $this->backend->usersInGroup($group1)); + $this->assertFalse($this->backend->inGroup($user1, $group1)); + } + + public function testSearchGroups() { + $name1 = $this->getGroupName('foobarbaz'); + $name2 = $this->getGroupName('bazbarfoo'); + $name3 = $this->getGroupName('notme'); + + $this->backend->createGroup($name1); + $this->backend->createGroup($name2); + $this->backend->createGroup($name3); + + $result = $this->backend->getGroups('bar'); + $this->assertSame(2, count($result)); + } + + public function testSearchUsers() { + $group = $this->getGroupName(); + $this->backend->createGroup($group); + + $name1 = 'foobarbaz'; + $name2 = 'bazbarfoo'; + $name3 = 'notme'; + + $this->backend->addToGroup($name1, $group); + $this->backend->addToGroup($name2, $group); + $this->backend->addToGroup($name3, $group); + + $result = $this->backend->usersInGroup($group, 'bar'); + $this->assertSame(2, count($result)); + + $result = $this->backend->countUsersInGroup($group, 'bar'); + $this->assertSame(2, $result); + } + + public function testAddDouble() { + $group = $this->getGroupName(); + + $this->backend->createGroup($group); + $this->backend->createGroup($group); + } +} diff --git a/tests/lib/Group/DatabaseTest.php b/tests/lib/Group/DatabaseTest.php new file mode 100644 index 00000000000..395a442c5e3 --- /dev/null +++ b/tests/lib/Group/DatabaseTest.php @@ -0,0 +1,68 @@ +<?php +/** + * @author Arthur Schiwon <blizzz@owncloud.com> + * @author Joas Schilling <nickvergessen@owncloud.com> + * @author Robin Appelman <icewind@owncloud.com> + * @author Scrutinizer Auto-Fixer <auto-fixer@scrutinizer-ci.com> + * @author Thomas Müller <thomas.mueller@tmit.eu> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @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; + +/** + * Class Database + * + * @group DB + */ +class DatabaseTest extends Backend { + private $groups = array(); + + /** + * get a new unique group name + * test cases can override this in order to clean up created groups + * + * @return string + */ + public function getGroupName($name = null) { + $name = parent::getGroupName($name); + $this->groups[] = $name; + return $name; + } + + protected function setUp() { + parent::setUp(); + $this->backend = new \OC\Group\Database(); + } + + protected function tearDown() { + foreach ($this->groups as $group) { + $this->backend->deleteGroup($group); + } + parent::tearDown(); + } + + public function testAddDoubleNoCache() { + $group = $this->getGroupName(); + + $this->backend->createGroup($group); + + $backend = new \OC\Group\Database(); + $this->assertFalse($backend->createGroup($group)); + } +} diff --git a/tests/lib/Group/Dummy.php b/tests/lib/Group/Dummy.php new file mode 100644 index 00000000000..5504cd63a0a --- /dev/null +++ b/tests/lib/Group/Dummy.php @@ -0,0 +1,35 @@ +<?php +/** +* ownCloud +* +* @author Robin Appelman +* @copyright 2012 Robin Appelman icewind@owncloud.com +* +* This library is free software; you can redistribute it and/or +* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE +* License as published by the Free Software Foundation; either +* version 3 of the License, or any later version. +* +* This library 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 along with this library. If not, see <http://www.gnu.org/licenses/>. +* +*/ + +namespace Test\Group; + +/** + * Class Dummy + * + * @group DB + */ +class Dummy extends Backend { + protected function setUp() { + parent::setUp(); + $this->backend=new \Test\Util\Group\Dummy(); + } +} diff --git a/tests/lib/Group/GroupTest.php b/tests/lib/Group/GroupTest.php new file mode 100644 index 00000000000..5ab8e41e4af --- /dev/null +++ b/tests/lib/Group/GroupTest.php @@ -0,0 +1,386 @@ +<?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 GroupTest extends \Test\TestCase { + /** + * @return \OC\User\Manager | \OC\User\Manager + */ + protected function getUserManager() { + $userManager = $this->getMock('\OC\User\Manager'); + $backend = $this->getMock('\OC_User_Backend'); + $user1 = new User('user1', $backend); + $user2 = new User('user2', $backend); + $user3 = new User('user3', $backend); + $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['user1']; + $user2 = $users['user2']; + $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['user1']; + $user2 = $users['user2']; + $user3 = $users['user3']; + $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(); + $userBackend = $this->getMock('\OC_User_Backend'); + $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', $userBackend))); + } + + public function testInGroupMultipleBackends() { + $backend1 = $this->getMock('OC\Group\Database'); + $backend2 = $this->getMock('OC\Group\Database'); + $userManager = $this->getUserManager(); + $userBackend = $this->getMock('\OC_User_Backend'); + $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', $userBackend))); + } + + public function testAddUser() { + $backend = $this->getMock('OC\Group\Database'); + $userManager = $this->getUserManager(); + $userBackend = $this->getMock('\OC_User_Backend'); + $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', $userBackend)); + } + + public function testAddUserAlreadyInGroup() { + $backend = $this->getMock('OC\Group\Database'); + $userManager = $this->getUserManager(); + $userBackend = $this->getMock('\OC_User_Backend'); + $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', $userBackend)); + } + + public function testRemoveUser() { + $backend = $this->getMock('OC\Group\Database'); + $userManager = $this->getUserManager(); + $userBackend = $this->getMock('\OC_User_Backend'); + $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', $userBackend)); + } + + public function testRemoveUserNotInGroup() { + $backend = $this->getMock('OC\Group\Database'); + $userManager = $this->getUserManager(); + $userBackend = $this->getMock('\OC_User_Backend'); + $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', $userBackend)); + } + + public function testRemoveUserMultipleBackends() { + $backend1 = $this->getMock('OC\Group\Database'); + $backend2 = $this->getMock('OC\Group\Database'); + $userManager = $this->getUserManager(); + $userBackend = $this->getMock('\OC_User_Backend'); + $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', $userBackend)); + } + + 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', 2, 1) + ->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 testCountUsers() { + $backend1 = $this->getMock('OC\Group\Database'); + $userManager = $this->getUserManager(); + $group = new \OC\Group\Group('group1', array($backend1), $userManager); + + $backend1->expects($this->once()) + ->method('countUsersInGroup') + ->with('group1', '2') + ->will($this->returnValue(3)); + + $backend1->expects($this->any()) + ->method('implementsActions') + ->will($this->returnValue(true)); + + $users = $group->count('2'); + + $this->assertSame(3, $users); + } + + public function testCountUsersMultipleBackends() { + $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('countUsersInGroup') + ->with('group1', '2') + ->will($this->returnValue(3)); + $backend1->expects($this->any()) + ->method('implementsActions') + ->will($this->returnValue(true)); + + $backend2->expects($this->once()) + ->method('countUsersInGroup') + ->with('group1', '2') + ->will($this->returnValue(4)); + $backend2->expects($this->any()) + ->method('implementsActions') + ->will($this->returnValue(true)); + + $users = $group->count('2'); + + $this->assertSame(7, $users); + } + + public function testCountUsersNoMethod() { + $backend1 = $this->getMock('OC\Group\Database'); + $userManager = $this->getUserManager(); + $group = new \OC\Group\Group('group1', array($backend1), $userManager); + + $backend1->expects($this->never()) + ->method('countUsersInGroup'); + $backend1->expects($this->any()) + ->method('implementsActions') + ->will($this->returnValue(false)); + + $users = $group->count('2'); + + $this->assertSame(false, $users); + } + + 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(); + } +} diff --git a/tests/lib/Group/LegacyGroupTest.php b/tests/lib/Group/LegacyGroupTest.php new file mode 100644 index 00000000000..12bcfdddf15 --- /dev/null +++ b/tests/lib/Group/LegacyGroupTest.php @@ -0,0 +1,202 @@ +<?php +/** + * ownCloud + * + * @author Robin Appelman + * @author Bernhard Posselt + * @copyright 2012 Robin Appelman <icewind@owncloud.com> + * @copyright 2012 Bernhard Posselt <dev@bernhard-posselt.com> + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE + * License as published by the Free Software Foundation; either + * version 3 of the License, or any later version. + * + * This library 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 along with this library. If not, see <http://www.gnu.org/licenses/>. + * + */ + +namespace Test\Group; + +use OC_Group; +use OC_User; + +class LegacyGroupTest extends \Test\TestCase { + protected function setUp() { + parent::setUp(); + OC_Group::clearBackends(); + OC_User::clearBackends(); + } + + public function testSingleBackend() { + $userBackend = new \Test\Util\User\Dummy(); + \OC::$server->getUserManager()->registerBackend($userBackend); + OC_Group::useBackend(new \Test\Util\Group\Dummy()); + + $group1 = $this->getUniqueID(); + $group2 = $this->getUniqueID(); + OC_Group::createGroup($group1); + OC_Group::createGroup($group2); + + $user1 = $this->getUniqueID(); + $user2 = $this->getUniqueID(); + $userBackend->createUser($user1, ''); + $userBackend->createUser($user2, ''); + + $this->assertFalse(OC_Group::inGroup($user1, $group1), 'Asserting that user1 is not in group1'); + $this->assertFalse(OC_Group::inGroup($user2, $group1), 'Asserting that user2 is not in group1'); + $this->assertFalse(OC_Group::inGroup($user1, $group2), 'Asserting that user1 is not in group2'); + $this->assertFalse(OC_Group::inGroup($user2, $group2), 'Asserting that user2 is not in group2'); + + $this->assertTrue(OC_Group::addToGroup($user1, $group1)); + + $this->assertTrue(OC_Group::inGroup($user1, $group1), 'Asserting that user1 is in group1'); + $this->assertFalse(OC_Group::inGroup($user2, $group1), 'Asserting that user2 is not in group1'); + $this->assertFalse(OC_Group::inGroup($user1, $group2), 'Asserting that user1 is not in group2'); + $this->assertFalse(OC_Group::inGroup($user2, $group2), 'Asserting that user2 is not in group2'); + + $this->assertTrue(OC_Group::addToGroup($user1, $group1)); + + $this->assertEquals(array($user1), OC_Group::usersInGroup($group1)); + $this->assertEquals(array(), OC_Group::usersInGroup($group2)); + + $this->assertEquals(array($group1), OC_Group::getUserGroups($user1)); + $this->assertEquals(array(), OC_Group::getUserGroups($user2)); + + OC_Group::deleteGroup($group1); + $this->assertEquals(array(), OC_Group::getUserGroups($user1)); + $this->assertEquals(array(), OC_Group::usersInGroup($group1)); + $this->assertFalse(OC_Group::inGroup($user1, $group1)); + } + + + public function testNoEmptyGIDs() { + OC_Group::useBackend(new \Test\Util\Group\Dummy()); + $emptyGroup = null; + + $this->assertFalse(OC_Group::createGroup($emptyGroup)); + } + + + public function testNoGroupsTwice() { + OC_Group::useBackend(new \Test\Util\Group\Dummy()); + $group = $this->getUniqueID(); + OC_Group::createGroup($group); + + $groupCopy = $group; + + OC_Group::createGroup($groupCopy); + $this->assertEquals(array($group), OC_Group::getGroups()); + } + + + public function testDontDeleteAdminGroup() { + OC_Group::useBackend(new \Test\Util\Group\Dummy()); + $adminGroup = 'admin'; + OC_Group::createGroup($adminGroup); + + $this->assertFalse(OC_Group::deleteGroup($adminGroup)); + $this->assertEquals(array($adminGroup), OC_Group::getGroups()); + } + + + public function testDontAddUserToNonexistentGroup() { + OC_Group::useBackend(new \Test\Util\Group\Dummy()); + $groupNonExistent = 'notExistent'; + $user = $this->getUniqueID(); + + $this->assertEquals(false, OC_Group::addToGroup($user, $groupNonExistent)); + $this->assertEquals(array(), OC_Group::getGroups()); + } + + public function testUsersInGroup() { + OC_Group::useBackend(new \Test\Util\Group\Dummy()); + $userBackend = new \Test\Util\User\Dummy(); + \OC::$server->getUserManager()->registerBackend($userBackend); + + $group1 = $this->getUniqueID(); + $group2 = $this->getUniqueID(); + $group3 = $this->getUniqueID(); + $user1 = $this->getUniqueID(); + $user2 = $this->getUniqueID(); + $user3 = $this->getUniqueID(); + OC_Group::createGroup($group1); + OC_Group::createGroup($group2); + OC_Group::createGroup($group3); + + $userBackend->createUser($user1, ''); + $userBackend->createUser($user2, ''); + $userBackend->createUser($user3, ''); + + OC_Group::addToGroup($user1, $group1); + OC_Group::addToGroup($user2, $group1); + OC_Group::addToGroup($user3, $group1); + OC_Group::addToGroup($user3, $group2); + + $this->assertEquals(array($user1, $user2, $user3), + OC_Group::usersInGroups(array($group1, $group2, $group3))); + + // FIXME: needs more parameter variation + } + + public function testMultiBackend() { + $userBackend = new \Test\Util\User\Dummy(); + \OC::$server->getUserManager()->registerBackend($userBackend); + $backend1 = new \Test\Util\Group\Dummy(); + $backend2 = new \Test\Util\Group\Dummy(); + OC_Group::useBackend($backend1); + OC_Group::useBackend($backend2); + + $group1 = $this->getUniqueID(); + $group2 = $this->getUniqueID(); + OC_Group::createGroup($group1); + + //groups should be added to the first registered backend + $this->assertEquals(array($group1), $backend1->getGroups()); + $this->assertEquals(array(), $backend2->getGroups()); + + $this->assertEquals(array($group1), OC_Group::getGroups()); + $this->assertTrue(OC_Group::groupExists($group1)); + $this->assertFalse(OC_Group::groupExists($group2)); + + $backend1->createGroup($group2); + + $this->assertEquals(array($group1, $group2), OC_Group::getGroups()); + $this->assertTrue(OC_Group::groupExists($group1)); + $this->assertTrue(OC_Group::groupExists($group2)); + + $user1 = $this->getUniqueID(); + $user2 = $this->getUniqueID(); + + $userBackend->createUser($user1, ''); + $userBackend->createUser($user2, ''); + + $this->assertFalse(OC_Group::inGroup($user1, $group1)); + $this->assertFalse(OC_Group::inGroup($user2, $group1)); + + + $this->assertTrue(OC_Group::addToGroup($user1, $group1)); + + $this->assertTrue(OC_Group::inGroup($user1, $group1)); + $this->assertFalse(OC_Group::inGroup($user2, $group1)); + $this->assertFalse($backend2->inGroup($user1, $group1)); + + OC_Group::addToGroup($user1, $group1); + + $this->assertEquals(array($user1), OC_Group::usersInGroup($group1)); + + $this->assertEquals(array($group1), OC_Group::getUserGroups($user1)); + $this->assertEquals(array(), OC_Group::getUserGroups($user2)); + + OC_Group::deleteGroup($group1); + $this->assertEquals(array(), OC_Group::getUserGroups($user1)); + $this->assertEquals(array(), OC_Group::usersInGroup($group1)); + $this->assertFalse(OC_Group::inGroup($user1, $group1)); + } +} diff --git a/tests/lib/Group/ManagerTest.php b/tests/lib/Group/ManagerTest.php new file mode 100644 index 00000000000..907f2b59452 --- /dev/null +++ b/tests/lib/Group/ManagerTest.php @@ -0,0 +1,897 @@ +<?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 ManagerTest extends \Test\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 \Test\Util\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 + */ + $backendGroupCreated = false; + $backend = $this->getMock('\OC\Group\Database'); + $backend->expects($this->any()) + ->method('groupExists') + ->with('group1') + ->will($this->returnCallback(function () use (&$backendGroupCreated) { + 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; + }));; + + /** + * @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'))); + $backend->expects($this->once()) + ->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); + + $groups = $manager->search('1'); + $this->assertEquals(1, count($groups)); + $group1 = reset($groups); + $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'))); + $backend1->expects($this->any()) + ->method('groupExists') + ->will($this->returnValue(true)); + + /** + * @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'))); + $backend2->expects($this->any()) + ->method('groupExists') + ->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); + + $groups = $manager->search('1'); + $this->assertEquals(2, count($groups)); + $group1 = reset($groups); + $group12 = next($groups); + $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'))); + $backend1->expects($this->any()) + ->method('groupExists') + ->will($this->returnValue(true)); + + /** + * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend2 + */ + $backend2 = $this->getMock('\OC\Group\Database'); + $backend2->expects($this->once()) + ->method('getGroups') + ->with('1', 2, 1) + ->will($this->returnValue(array('group12'))); + $backend2->expects($this->any()) + ->method('groupExists') + ->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); + + $groups = $manager->search('1', 2, 1); + $this->assertEquals(2, count($groups)); + $group1 = reset($groups); + $group12 = next($groups); + $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'))); + $backend->expects($this->any()) + ->method('groupExists') + ->with('group1') + ->will($this->returnValue(true)); + + /** + * @var \OC\User\Manager $userManager + */ + $userManager = $this->getMock('\OC\User\Manager'); + $userBackend = $this->getMock('\OC_User_Backend'); + $manager = new \OC\Group\Manager($userManager); + $manager->addBackend($backend); + + $groups = $manager->getUserGroups(new User('user1', $userBackend)); + $this->assertEquals(1, count($groups)); + $group1 = reset($groups); + $this->assertEquals('group1', $group1->getGID()); + } + + public function testGetUserGroupIds() { + /** @var \PHPUnit_Framework_MockObject_MockObject|\OC\Group\Manager $manager */ + $manager = $this->getMockBuilder('OC\Group\Manager') + ->disableOriginalConstructor() + ->setMethods(['getUserGroups']) + ->getMock(); + $manager->expects($this->once()) + ->method('getUserGroups') + ->willReturn([ + '123' => '123', + 'abc' => 'abc', + ]); + + /** @var \OC\User\User $user */ + $user = $this->getMockBuilder('OC\User\User') + ->disableOriginalConstructor() + ->getMock(); + + $groups = $manager->getUserGroupIds($user); + $this->assertEquals(2, count($groups)); + + foreach ($groups as $group) { + $this->assertInternalType('string', $group); + } + } + + public function testInGroup() { + /** + * @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', 'admin', 'group2'))); + $backend->expects($this->any()) + ->method('groupExists') + ->will($this->returnValue(true)); + + /** + * @var \OC\User\Manager $userManager + */ + $userManager = $this->getMock('\OC\User\Manager'); + $userBackend = $this->getMock('\OC_User_Backend'); + $manager = new \OC\Group\Manager($userManager); + $manager->addBackend($backend); + + $this->assertTrue($manager->isInGroup('user1', 'group1')); + } + + public function testIsAdmin() { + /** + * @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', 'admin', 'group2'))); + $backend->expects($this->any()) + ->method('groupExists') + ->will($this->returnValue(true)); + + /** + * @var \OC\User\Manager $userManager + */ + $userManager = $this->getMock('\OC\User\Manager'); + $userBackend = $this->getMock('\OC_User_Backend'); + $manager = new \OC\Group\Manager($userManager); + $manager->addBackend($backend); + + $this->assertTrue($manager->isAdmin('user1')); + } + + public function testNotAdmin() { + /** + * @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', 'group2'))); + $backend->expects($this->any()) + ->method('groupExists') + ->will($this->returnValue(true)); + + /** + * @var \OC\User\Manager $userManager + */ + $userManager = $this->getMock('\OC\User\Manager'); + $userBackend = $this->getMock('\OC_User_Backend'); + $manager = new \OC\Group\Manager($userManager); + $manager->addBackend($backend); + + $this->assertFalse($manager->isAdmin('user1')); + } + + 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'))); + $backend1->expects($this->any()) + ->method('groupExists') + ->will($this->returnValue(true)); + + /** + * @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'))); + $backend1->expects($this->any()) + ->method('groupExists') + ->will($this->returnValue(true)); + + /** + * @var \OC\User\Manager $userManager + */ + $userManager = $this->getMock('\OC\User\Manager'); + $userBackend = $this->getMock('\OC_User_Backend'); + $manager = new \OC\Group\Manager($userManager); + $manager->addBackend($backend1); + $manager->addBackend($backend2); + + $groups = $manager->getUserGroups(new User('user1', $userBackend)); + $this->assertEquals(2, count($groups)); + $group1 = reset($groups); + $group2 = next($groups); + $this->assertEquals('group1', $group1->getGID()); + $this->assertEquals('group2', $group2->getGID()); + } + + public function testDisplayNamesInGroupWithOneUserBackend() { + /** + * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend1 + */ + $backend = $this->getMock('\OC\Group\Database'); + $backend->expects($this->exactly(1)) + ->method('groupExists') + ->with('testgroup') + ->will($this->returnValue(true)); + + $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; + } + })); + + /** + * @var \OC\User\Manager $userManager + */ + $userManager = $this->getMock('\OC\User\Manager'); + $userBackend = $this->getMock('\OC_User_Backend'); + + $userManager->expects($this->any()) + ->method('searchDisplayName') + ->with('user3') + ->will($this->returnCallback(function($search, $limit, $offset) use ($userBackend) { + switch($offset) { + case 0 : return array('user3' => new User('user3', $userBackend), + 'user33' => new User('user33', $userBackend)); + case 2 : return array(); + } + })); + + $userManager->expects($this->any()) + ->method('get') + ->will($this->returnCallback(function($uid) use ($userBackend) { + switch($uid) { + case 'user1' : return new User('user1', $userBackend); + case 'user2' : return new User('user2', $userBackend); + case 'user3' : return new User('user3', $userBackend); + case 'user33': return new User('user33', $userBackend); + default: + return null; + } + })); + + $manager = new \OC\Group\Manager($userManager); + $manager->addBackend($backend); + + $users = $manager->displayNamesInGroup('testgroup', 'user3'); + $this->assertEquals(1, count($users)); + $this->assertFalse(isset($users['user1'])); + $this->assertFalse(isset($users['user2'])); + $this->assertFalse(isset($users['user3'])); + $this->assertTrue(isset($users['user33'])); + } + + public function testDisplayNamesInGroupWithOneUserBackendWithLimitSpecified() { + /** + * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend1 + */ + $backend = $this->getMock('\OC\Group\Database'); + $backend->expects($this->exactly(1)) + ->method('groupExists') + ->with('testgroup') + ->will($this->returnValue(true)); + + $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; + } + })); + + /** + * @var \OC\User\Manager $userManager + */ + $userManager = $this->getMock('\OC\User\Manager'); + $userBackend = $this->getMock('\OC_User_Backend'); + + $userManager->expects($this->any()) + ->method('searchDisplayName') + ->with('user3') + ->will($this->returnCallback(function($search, $limit, $offset) use ($userBackend) { + switch($offset) { + case 0 : return array('user3' => new User('user3', $userBackend), + 'user33' => new User('user33', $userBackend)); + case 2 : return array('user333' => new User('user333', $userBackend)); + } + })); + + $userManager->expects($this->any()) + ->method('get') + ->will($this->returnCallback(function($uid) use ($userBackend) { + switch($uid) { + case 'user1' : return new User('user1', $userBackend); + case 'user2' : return new User('user2', $userBackend); + case 'user3' : return new User('user3', $userBackend); + case 'user33': return new User('user33', $userBackend); + case 'user333': return new User('user333', $userBackend); + default: + return null; + } + })); + + $manager = new \OC\Group\Manager($userManager); + $manager->addBackend($backend); + + $users = $manager->displayNamesInGroup('testgroup', 'user3', 1); + $this->assertEquals(1, count($users)); + $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() { + /** + * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend1 + */ + $backend = $this->getMock('\OC\Group\Database'); + $backend->expects($this->exactly(1)) + ->method('groupExists') + ->with('testgroup') + ->will($this->returnValue(true)); + + $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; + } + })); + + /** + * @var \OC\User\Manager $userManager + */ + $userManager = $this->getMock('\OC\User\Manager'); + $userBackend = $this->getMock('\OC_User_Backend'); + + $userManager->expects($this->any()) + ->method('searchDisplayName') + ->with('user3') + ->will($this->returnCallback(function($search, $limit, $offset) use ($userBackend) { + switch($offset) { + case 0 : + return array( + 'user3' => new User('user3', $userBackend), + 'user33' => new User('user33', $userBackend), + 'user333' => new User('user333', $userBackend) + ); + } + })); + + $userManager->expects($this->any()) + ->method('get') + ->will($this->returnCallback(function($uid) use ($userBackend) { + switch($uid) { + case 'user1' : return new User('user1', $userBackend); + case 'user2' : return new User('user2', $userBackend); + case 'user3' : return new User('user3', $userBackend); + case 'user33': return new User('user33', $userBackend); + case 'user333': return new User('user333', $userBackend); + default: + return null; + } + })); + + $manager = new \OC\Group\Manager($userManager); + $manager->addBackend($backend); + + $users = $manager->displayNamesInGroup('testgroup', 'user3', 1, 1); + $this->assertEquals(1, count($users)); + $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() { + /** + * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend1 + */ + $backend = $this->getMock('\OC\Group\Database'); + $backend->expects($this->exactly(1)) + ->method('groupExists') + ->with('testgroup') + ->will($this->returnValue(true)); + + $backend->expects($this->once()) + ->method('usersInGroup') + ->with('testgroup', '', -1, 0) + ->will($this->returnValue(array('user2', 'user33'))); + + /** + * @var \OC\User\Manager $userManager + */ + $userManager = $this->getMock('\OC\User\Manager'); + $userBackend = $this->getMock('\OC_User_Backend'); + + $userManager->expects($this->any()) + ->method('get') + ->will($this->returnCallback(function($uid) use ($userBackend) { + switch($uid) { + case 'user1' : return new User('user1', $userBackend); + case 'user2' : return new User('user2', $userBackend); + case 'user3' : return new User('user3', $userBackend); + case 'user33': return new User('user33', $userBackend); + default: + return null; + } + })); + + $manager = new \OC\Group\Manager($userManager); + $manager->addBackend($backend); + + $users = $manager->displayNamesInGroup('testgroup', ''); + $this->assertEquals(2, count($users)); + $this->assertFalse(isset($users['user1'])); + $this->assertTrue(isset($users['user2'])); + $this->assertFalse(isset($users['user3'])); + $this->assertTrue(isset($users['user33'])); + } + + public function testDisplayNamesInGroupWithOneUserBackendAndSearchEmptyAndLimitSpecified() { + /** + * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend1 + */ + $backend = $this->getMock('\OC\Group\Database'); + $backend->expects($this->exactly(1)) + ->method('groupExists') + ->with('testgroup') + ->will($this->returnValue(true)); + + $backend->expects($this->once()) + ->method('usersInGroup') + ->with('testgroup', '', 1, 0) + ->will($this->returnValue(array('user2'))); + /** + * @var \OC\User\Manager $userManager + */ + $userManager = $this->getMock('\OC\User\Manager'); + $userBackend = $this->getMock('\OC_User_Backend'); + + $userManager->expects($this->any()) + ->method('get') + ->will($this->returnCallback(function($uid) use ($userBackend) { + switch($uid) { + case 'user1' : return new User('user1', $userBackend); + case 'user2' : return new User('user2', $userBackend); + case 'user3' : return new User('user3', $userBackend); + case 'user33': return new User('user33', $userBackend); + default: + return null; + } + })); + + $manager = new \OC\Group\Manager($userManager); + $manager->addBackend($backend); + + $users = $manager->displayNamesInGroup('testgroup', '', 1); + $this->assertEquals(1, count($users)); + $this->assertFalse(isset($users['user1'])); + $this->assertTrue(isset($users['user2'])); + $this->assertFalse(isset($users['user3'])); + $this->assertFalse(isset($users['user33'])); + } + + public function testDisplayNamesInGroupWithOneUserBackendAndSearchEmptyAndLimitAndOffsetSpecified() { + /** + * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend1 + */ + $backend = $this->getMock('\OC\Group\Database'); + $backend->expects($this->exactly(1)) + ->method('groupExists') + ->with('testgroup') + ->will($this->returnValue(true)); + + $backend->expects($this->once()) + ->method('usersInGroup') + ->with('testgroup', '', 1, 1) + ->will($this->returnValue(array('user33'))); + + /** + * @var \OC\User\Manager $userManager + */ + $userManager = $this->getMock('\OC\User\Manager'); + $userBackend = $this->getMock('\OC_User_Backend'); + + $userManager->expects($this->any()) + ->method('get') + ->will($this->returnCallback(function($uid) use ($userBackend) { + switch($uid) { + case 'user1' : return new User('user1', $userBackend); + case 'user2' : return new User('user2', $userBackend); + case 'user3' : return new User('user3', $userBackend); + case 'user33': return new User('user33', $userBackend); + default: + return null; + } + })); + + $manager = new \OC\Group\Manager($userManager); + $manager->addBackend($backend); + + $users = $manager->displayNamesInGroup('testgroup', '', 1, 1); + $this->assertEquals(1, count($users)); + $this->assertFalse(isset($users['user1'])); + $this->assertFalse(isset($users['user2'])); + $this->assertFalse(isset($users['user3'])); + $this->assertTrue(isset($users['user33'])); + } + + public function testGetUserGroupsWithAddUser() { + /** + * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend + */ + $backend = $this->getMock('\OC\Group\Database'); + $expectedGroups = array(); + $backend->expects($this->any()) + ->method('getUserGroups') + ->with('user1') + ->will($this->returnCallback(function () use (&$expectedGroups) { + return $expectedGroups; + })); + $backend->expects($this->any()) + ->method('groupExists') + ->with('group1') + ->will($this->returnValue(true)); + $backend->expects($this->once()) + ->method('implementsActions') + ->will($this->returnValue(true)); + + /** + * @var \OC\User\Manager $userManager + */ + $userManager = $this->getMock('\OC\User\Manager'); + $manager = new \OC\Group\Manager($userManager); + $manager->addBackend($backend); + + // prime cache + $user1 = new User('user1', null); + $groups = $manager->getUserGroups($user1); + $this->assertEquals(array(), $groups); + + // add user + $group = $manager->get('group1'); + $group->addUser($user1); + $expectedGroups = array('group1'); + + // check result + $groups = $manager->getUserGroups($user1); + $this->assertEquals(1, count($groups)); + $group1 = reset($groups); + $this->assertEquals('group1', $group1->getGID()); + } + + public function testGetUserGroupsWithRemoveUser() { + /** + * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend + */ + $backend = $this->getMock('\OC\Group\Database'); + $expectedGroups = array('group1'); + $backend->expects($this->any()) + ->method('getUserGroups') + ->with('user1') + ->will($this->returnCallback(function () use (&$expectedGroups) { + return $expectedGroups; + })); + $backend->expects($this->any()) + ->method('groupExists') + ->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()) + ->method('removeFromGroup') + ->will($this->returnValue(true)); + + /** + * @var \OC\User\Manager $userManager + */ + $userManager = $this->getMock('\OC\User\Manager'); + $manager = new \OC\Group\Manager($userManager); + $manager->addBackend($backend); + + // prime cache + $user1 = new User('user1', null); + $groups = $manager->getUserGroups($user1); + $this->assertEquals(1, count($groups)); + $group1 = reset($groups); + $this->assertEquals('group1', $group1->getGID()); + + // remove user + $group = $manager->get('group1'); + $group->removeUser($user1); + $expectedGroups = array(); + + // check result + $groups = $manager->getUserGroups($user1); + $this->assertEquals(array(), $groups); + } + + public function testGetUserIdGroups() { + /** + * @var \PHPUnit_Framework_MockObject_MockObject | \OC\Group\Backend $backend + */ + $backend = $this->getMock('\OC\Group\Database'); + $backend->expects($this->any()) + ->method('getUserGroups') + ->with('user1') + ->will($this->returnValue(null)); + + /** + * @var \OC\User\Manager $userManager + */ + $userManager = $this->getMock('\OC\User\Manager'); + $manager = new \OC\Group\Manager($userManager); + $manager->addBackend($backend); + + $groups = $manager->getUserIdGroups('user1'); + $this->assertEquals([], $groups); + } + +} diff --git a/tests/lib/Group/MetaDataTest.php b/tests/lib/Group/MetaDataTest.php new file mode 100644 index 00000000000..593bbcb48db --- /dev/null +++ b/tests/lib/Group/MetaDataTest.php @@ -0,0 +1,136 @@ +<?php +/** + * @author Arthur Schiwon <blizzz@owncloud.com> + * @author Lukas Reschke <lukas@owncloud.com> + * + * @copyright Copyright (c) 2015, ownCloud, Inc. + * @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; + +class MetaDataTest extends \Test\TestCase { + /** @var \OC\Group\Manager */ + private $groupManager; + /** @var \OCP\IUserSession */ + private $userSession; + /** @var \OC\Group\MetaData */ + private $groupMetadata; + /** @var bool */ + private $isAdmin = true; + + public function setUp() { + parent::setUp(); + $this->groupManager = $this->getMockBuilder('\OC\Group\Manager') + ->disableOriginalConstructor() + ->getMock(); + $this->userSession = $this->getMock('\OCP\IUserSession'); + $this->groupMetadata = new \OC\Group\MetaData( + 'foo', + $this->isAdmin, + $this->groupManager, + $this->userSession + ); + } + + private function getGroupMock($countCallCount = 0) { + $group = $this->getMockBuilder('\OC\Group\Group') + ->disableOriginalConstructor() + ->getMock(); + + $group->expects($this->exactly(9)) + ->method('getGID') + ->will($this->onConsecutiveCalls( + 'admin', 'admin', 'admin', + 'g2', 'g2', 'g2', + 'g3', 'g3', 'g3')); + + $group->expects($this->exactly($countCallCount)) + ->method('count') + ->with('') + ->will($this->onConsecutiveCalls(2, 3, 5)); + + return $group; + } + + + public function testGet() { + $group = $this->getGroupMock(); + $groups = array_fill(0, 3, $group); + + $this->groupManager->expects($this->once()) + ->method('search') + ->with('') + ->will($this->returnValue($groups)); + + list($adminGroups, $ordinaryGroups) = $this->groupMetadata->get(); + + $this->assertSame(1, count($adminGroups)); + $this->assertSame(2, count($ordinaryGroups)); + + $this->assertSame('g2', $ordinaryGroups[0]['name']); + // user count is not loaded + $this->assertSame(0, $ordinaryGroups[0]['usercount']); + } + + public function testGetWithSorting() { + $this->groupMetadata->setSorting(1); + $group = $this->getGroupMock(3); + $groups = array_fill(0, 3, $group); + + $this->groupManager->expects($this->once()) + ->method('search') + ->with('') + ->will($this->returnValue($groups)); + + list($adminGroups, $ordinaryGroups) = $this->groupMetadata->get(); + + $this->assertSame(1, count($adminGroups)); + $this->assertSame(2, count($ordinaryGroups)); + + $this->assertSame('g3', $ordinaryGroups[0]['name']); + $this->assertSame(5, $ordinaryGroups[0]['usercount']); + } + + public function testGetWithCache() { + $group = $this->getGroupMock(); + $groups = array_fill(0, 3, $group); + + $this->groupManager->expects($this->once()) + ->method('search') + ->with('') + ->will($this->returnValue($groups)); + + //two calls, if caching fails call counts for group and groupmanager + //are exceeded + $this->groupMetadata->get(); + $this->groupMetadata->get(); + } + + //get() does not need to be tested with search parameters, because they are + //solely and only passed to GroupManager and Group. + + public function testGetGroupsAsAdmin() { + $this->groupManager + ->expects($this->once()) + ->method('search') + ->with('Foo') + ->will($this->returnValue(['DummyValue'])); + + $expected = ['DummyValue']; + $this->assertSame($expected, $this->invokePrivate($this->groupMetadata, 'getGroups', ['Foo'])); + } +} |