Skip to content

Commit 244bc7e

Browse files
committed
add tests for removing group members
1 parent 787c166 commit 244bc7e

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

test/functional/PiRemoveUserTest.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
use PHPUnit\Framework\Attributes\DataProvider;
5+
use UnityWebPortal\lib\UnityUser;
6+
7+
class PiRemoveUserTest extends TestCase {
8+
private function removeUser(string $uid)
9+
{
10+
post(
11+
__DIR__ . "/../../webroot/panel/pi.php",
12+
["form_name" => "remUser", "uid" => $uid]
13+
);
14+
}
15+
16+
public function testRemoveUser()
17+
{
18+
global $USER, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK;
19+
switchUser(...getUserIsPIHasAtLeastOneMember());
20+
$pi = $USER;
21+
$piUid = $USER->getUID();
22+
$piGroup = $USER->getPIGroup();
23+
$this->assertTrue($piGroup->exists());
24+
$memberUIDs = $piGroup->getGroupMemberUIDs();
25+
// the 0th member of the PI group is the PI
26+
$this->assertGreaterThan(1, count($memberUIDs));
27+
$memberToDelete = null;
28+
foreach ($memberUIDs as $uid) {
29+
if ($uid != $piUid) {
30+
$memberToDelete = new UnityUser($uid, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK);
31+
break;
32+
}
33+
}
34+
$this->assertNotEquals($pi->getUID(), $memberToDelete->getUID());
35+
$this->assertTrue($piGroup->userExists($memberToDelete));
36+
try {
37+
$this->removeUser($memberToDelete->getUID());
38+
$this->assertFalse($piGroup->userExists($memberToDelete));
39+
} finally {
40+
if (!$piGroup->userExists($memberToDelete)) {
41+
$piGroup->newUserRequest($memberToDelete);
42+
$piGroup->approveUser($memberToDelete);
43+
}
44+
}
45+
}
46+
47+
public function testRemovePIFromTheirOwnGroup()
48+
{
49+
global $USER, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK;
50+
switchUser(...getUserIsPIHasAtLeastOneMember());
51+
$pi = $USER;
52+
$piGroup = $USER->getPIGroup();
53+
$this->assertTrue($piGroup->exists());
54+
$this->assertTrue($piGroup->userExists($pi));
55+
$this->expectException(Exception::class);
56+
try {
57+
$this->removeUser($pi->getUID());
58+
$this->assertTrue($piGroup->userExists($pi));
59+
} finally {
60+
if (!$piGroup->userExists($pi)) {
61+
$piGroup->newUserRequest($pi);
62+
$piGroup->approveUser($pi);
63+
}
64+
}
65+
}
66+
}

test/phpunit-bootstrap.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,10 @@ function post(string $phpfile, array $post_data): void
6767
include $phpfile;
6868
ob_get_clean(); // discard output
6969
} catch (Throwable $e) {
70-
error_log(ob_get_clean()); // don't discard output
70+
$output = ob_get_clean();
71+
if (!empty($output)) {
72+
error_log($output);
73+
}
7174
throw $e;
7275
} finally {
7376
unset($_POST);
@@ -119,3 +122,8 @@ function getUserIsPIHasNoMembersNoMemberRequests()
119122
{
120123
return ["[email protected]", "foo", "bar", "[email protected]"];
121124
}
125+
126+
function getUserIsPIHasAtLeastOneMember()
127+
{
128+
return ["[email protected]", "foo", "bar", "[email protected]"];
129+
}

0 commit comments

Comments
 (0)