123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292 |
- <?php
- namespace Test;
- use OCP\EventDispatcher\IEventDispatcher;
- use OCP\Group\Events\SubAdminAddedEvent;
- use OCP\Group\Events\SubAdminRemovedEvent;
- class SubAdminTest extends \Test\TestCase {
-
- private $userManager;
-
- private $groupManager;
-
- private $dbConn;
-
- private $eventDispatcher;
-
- private $users;
-
- private $groups;
- protected function setUp(): void {
- parent::setUp();
- $this->users = [];
- $this->groups = [];
- $this->userManager = \OC::$server->getUserManager();
- $this->groupManager = \OC::$server->getGroupManager();
- $this->dbConn = \OC::$server->getDatabaseConnection();
- $this->eventDispatcher = \OC::$server->get(IEventDispatcher::class);
-
- for ($i = 0; $i < 3; $i++) {
- $this->users[] = $this->userManager->createUser('user' . $i, 'user');
- $this->groups[] = $this->groupManager->createGroup('group' . $i);
- }
-
- if (!$this->groupManager->groupExists('admin')) {
- $this->groupManager->createGroup('admin');
- }
-
-
- $qb = $this->dbConn->getQueryBuilder();
- $qb->insert('group_admin')
- ->values([
- 'gid' => $qb->createNamedParameter($this->groups[0]->getGID()),
- 'uid' => $qb->createNamedParameter('orphanedUser')
- ])
- ->execute();
- $qb->insert('group_admin')
- ->values([
- 'gid' => $qb->createNamedParameter('orphanedGroup'),
- 'uid' => $qb->createNamedParameter('orphanedUser')
- ])
- ->execute();
- $qb->insert('group_admin')
- ->values([
- 'gid' => $qb->createNamedParameter('orphanedGroup'),
- 'uid' => $qb->createNamedParameter($this->users[0]->getUID())
- ])
- ->execute();
- }
- protected function tearDown(): void {
- foreach ($this->users as $user) {
- $user->delete();
- }
- foreach ($this->groups as $group) {
- $group->delete();
- }
- $qb = $this->dbConn->getQueryBuilder();
- $qb->delete('group_admin')
- ->where($qb->expr()->eq('uid', $qb->createNamedParameter('orphanedUser')))
- ->orWhere($qb->expr()->eq('gid', $qb->createNamedParameter('orphanedGroup')))
- ->execute();
- }
- public function testCreateSubAdmin(): void {
- $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
- $subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
-
- $qb = $this->dbConn->getQueryBuilder();
- $result = $qb->select(['gid', 'uid'])
- ->from('group_admin')
- ->where($qb->expr()->eq('gid', $qb->createNamedParameter($this->groups[0]->getGID())))
- ->andWHere($qb->expr()->eq('uid', $qb->createNamedParameter($this->users[0]->getUID())))
- ->execute()
- ->fetch();
- $this->assertEquals(
- [
- 'gid' => $this->groups[0]->getGID(),
- 'uid' => $this->users[0]->getUID()
- ], $result);
-
- $result = $qb->delete('*PREFIX*group_admin')
- ->where($qb->expr()->eq('gid', $qb->createNamedParameter($this->groups[0]->getGID())))
- ->andWHere($qb->expr()->eq('uid', $qb->createNamedParameter($this->users[0]->getUID())))
- ->execute();
- }
- public function testDeleteSubAdmin(): void {
- $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
- $subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
- $subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]);
-
- $qb = $this->dbConn->getQueryBuilder();
- $result = $qb->select(['gid', 'uid'])
- ->from('group_admin')
- ->where($qb->expr()->eq('gid', $qb->createNamedParameter($this->groups[0]->getGID())))
- ->andWHere($qb->expr()->eq('uid', $qb->createNamedParameter($this->users[0]->getUID())))
- ->execute()
- ->fetch();
- $this->assertEmpty($result);
- }
- public function testGetSubAdminsGroups(): void {
- $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
- $subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
- $subAdmin->createSubAdmin($this->users[0], $this->groups[1]);
- $result = $subAdmin->getSubAdminsGroups($this->users[0]);
- $this->assertContains($this->groups[0], $result);
- $this->assertContains($this->groups[1], $result);
- $this->assertNotContains($this->groups[2], $result);
- $this->assertNotContains(null, $result);
- $subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]);
- $subAdmin->deleteSubAdmin($this->users[0], $this->groups[1]);
- }
- public function testGetGroupsSubAdmins(): void {
- $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
- $subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
- $subAdmin->createSubAdmin($this->users[1], $this->groups[0]);
- $result = $subAdmin->getGroupsSubAdmins($this->groups[0]);
- $this->assertContains($this->users[0], $result);
- $this->assertContains($this->users[1], $result);
- $this->assertNotContains($this->users[2], $result);
- $this->assertNotContains(null, $result);
- $subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]);
- $subAdmin->deleteSubAdmin($this->users[1], $this->groups[0]);
- }
- public function testGetAllSubAdmin(): void {
- $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
- $subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
- $subAdmin->createSubAdmin($this->users[1], $this->groups[1]);
- $subAdmin->createSubAdmin($this->users[2], $this->groups[1]);
- $result = $subAdmin->getAllSubAdmins();
- $this->assertContains(['user' => $this->users[0], 'group' => $this->groups[0]], $result);
- $this->assertContains(['user' => $this->users[1], 'group' => $this->groups[1]], $result);
- $this->assertContains(['user' => $this->users[2], 'group' => $this->groups[1]], $result);
- $this->assertNotContains(['user' => null, 'group' => null], $result);
- }
- public function testIsSubAdminofGroup(): void {
- $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
- $subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
- $this->assertTrue($subAdmin->isSubAdminOfGroup($this->users[0], $this->groups[0]));
- $this->assertFalse($subAdmin->isSubAdminOfGroup($this->users[0], $this->groups[1]));
- $this->assertFalse($subAdmin->isSubAdminOfGroup($this->users[1], $this->groups[0]));
- $subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]);
- }
- public function testIsSubAdmin(): void {
- $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
- $subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
- $this->assertTrue($subAdmin->isSubAdmin($this->users[0]));
- $this->assertFalse($subAdmin->isSubAdmin($this->users[1]));
- $subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]);
- }
- public function testIsSubAdminAsAdmin(): void {
- $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
- $this->groupManager->get('admin')->addUser($this->users[0]);
- $this->assertTrue($subAdmin->isSubAdmin($this->users[0]));
- }
- public function testIsUserAccessible(): void {
- $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
- $this->groups[0]->addUser($this->users[1]);
- $this->groups[1]->addUser($this->users[1]);
- $this->groups[1]->addUser($this->users[2]);
- $subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
- $subAdmin->createSubAdmin($this->users[2], $this->groups[2]);
- $this->assertTrue($subAdmin->isUserAccessible($this->users[0], $this->users[1]));
- $this->assertFalse($subAdmin->isUserAccessible($this->users[0], $this->users[2]));
- $this->assertFalse($subAdmin->isUserAccessible($this->users[2], $this->users[0]));
- $subAdmin->deleteSubAdmin($this->users[0], $this->groups[0]);
- $subAdmin->deleteSubAdmin($this->users[2], $this->groups[2]);
- }
- public function testIsUserAccessibleAsUser(): void {
- $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
- $this->assertFalse($subAdmin->isUserAccessible($this->users[0], $this->users[1]));
- }
- public function testIsUserAccessibleAdmin(): void {
- $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
- $subAdmin->createSubAdmin($this->users[0], $this->groups[0]);
- $this->groupManager->get('admin')->addUser($this->users[1]);
- $this->assertFalse($subAdmin->isUserAccessible($this->users[0], $this->users[1]));
- }
- public function testPostDeleteUser(): void {
- $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
- $user = array_shift($this->users);
- foreach ($this->groups as $group) {
- $subAdmin->createSubAdmin($user, $group);
- }
- $user->delete();
- $this->assertEmpty($subAdmin->getAllSubAdmins());
- }
- public function testPostDeleteGroup(): void {
- $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
- $group = array_shift($this->groups);
- foreach ($this->users as $user) {
- $subAdmin->createSubAdmin($user, $group);
- }
- $group->delete();
- $this->assertEmpty($subAdmin->getAllSubAdmins());
- }
- public function testHooks(): void {
- $subAdmin = new \OC\SubAdmin($this->userManager, $this->groupManager, $this->dbConn, $this->eventDispatcher);
- $test = $this;
- $u = $this->users[0];
- $g = $this->groups[0];
- $count = 0;
- $this->eventDispatcher->addListener(SubAdminAddedEvent::class, function (SubAdminAddedEvent $event) use ($test, $u, $g, &$count) {
- $test->assertEquals($u->getUID(), $event->getUser()->getUID());
- $test->assertEquals($g->getGID(), $event->getGroup()->getGID());
- $count++;
- });
- $this->eventDispatcher->addListener(SubAdminRemovedEvent::class, function ($event) use ($test, $u, $g, &$count) {
- $test->assertEquals($u->getUID(), $event->getUser()->getUID());
- $test->assertEquals($g->getGID(), $event->getGroup()->getGID());
- $count++;
- });
- $subAdmin->createSubAdmin($u, $g);
- $this->assertEquals(1, $count);
- $subAdmin->deleteSubAdmin($u, $g);
- $this->assertEquals(2, $count);
- }
- }
|