Skip to content

Commit 9a04206

Browse files
committed
add test for worker remove-users-from-group.php
1 parent 24db426 commit 9a04206

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
use UnityWebPortal\lib\UnityUser;
5+
use UnityWebPortal\lib\UnityLDAP;
6+
7+
class WorkerRemoveUsersFromGroupTest extends TestCase
8+
{
9+
private function writeLinesToTmpFile(array $lines)
10+
{
11+
$file = tmpfile();
12+
if (!$file) {
13+
throw new RuntimeException("failed to make tmpfile");
14+
}
15+
$path = stream_get_meta_data($file)["uri"];
16+
$contents = implode("\n", $lines);
17+
$fwrite = fwrite($file, $contents);
18+
if ($fwrite === false) {
19+
throw new RuntimeException("failed to write to tmpfile '$path'");
20+
}
21+
return $file;
22+
}
23+
24+
public function testRemoveUsersFromGroup()
25+
{
26+
global $USER, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK;
27+
switchUser(...getUserIsPIHasNoMembersNoMemberRequests());
28+
$pi = $USER;
29+
$pi_group = $USER->getPIGroup();
30+
$this->assertTrue($pi->isPI());
31+
$this->assertEqualsCanonicalizing([$pi->uid], $pi_group->getGroupMemberUIDs(true));
32+
$this->assertEqualsCanonicalizing([$pi->uid], $pi_group->getGroupMemberUIDs(false));
33+
$this->assertEqualsCanonicalizing([], $pi_group->getRequests());
34+
$uids = getSomeUIDsOfQualifiedUsersNotRequestedAccountDeletion();
35+
$uids_to_remove = array_slice($uids, 0, 3);
36+
$expected_new_uids = array_diff(array_merge([$pi->uid], $uids), $uids_to_remove);
37+
$remove_uids_file = $this->writeLinesToTmpFile($uids_to_remove);
38+
$remove_uids_file_path = stream_get_meta_data($remove_uids_file)["uri"];
39+
try {
40+
foreach ($uids as $uid) {
41+
$user = new UnityUser($uid, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK);
42+
$pi_group->newUserRequest($user, false);
43+
$pi_group->approveUser($user, false);
44+
}
45+
[$_, $output] = executeWorker(
46+
"remove-users-from-group.php",
47+
"$pi_group->gid $remove_uids_file_path",
48+
);
49+
print implode("\n", $output);
50+
// our $LDAP is not aware of changes made by worker subprocess, so throw it out
51+
unset($GLOBALS["ldapconn"]);
52+
switchUser(...getUserIsPIHasNoMembersNoMemberRequests());
53+
$pi = $USER;
54+
$pi_group = $USER->getPIGroup();
55+
$this->assertEqualsCanonicalizing(
56+
$expected_new_uids,
57+
$pi_group->getGroupMemberUIDs(false),
58+
);
59+
$this->assertEqualsCanonicalizing(
60+
$expected_new_uids,
61+
$pi_group->getGroupMemberUIDs(true),
62+
);
63+
} finally {
64+
foreach ($uids as $uid) {
65+
$user = new UnityUser($uid, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK);
66+
$pi_group->removeUser($user);
67+
}
68+
}
69+
}
70+
}

test/phpunit-bootstrap.php

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,30 @@ function http_get(string $phpfile, array $get_data = []): void
158158
}
159159
}
160160

161+
/**
162+
* runs a worker script
163+
* @throws RuntimeException
164+
* @return [return code, output lines]
165+
*/
166+
function executeWorker(string $basename, string $args, bool $doThrowIfNonzero = true): array
167+
{
168+
$command = sprintf("%s %s/../workers/%s %s 2>&1", PHP_BINARY, __DIR__, $basename, $args);
169+
$output = [];
170+
$rc = null;
171+
exec($command, $output, $rc);
172+
if ($doThrowIfNonzero && $rc !== 0) {
173+
throw new RuntimeException(
174+
sprintf(
175+
"command failed! command='%s' rc=%d output=%s",
176+
$command,
177+
$rc,
178+
jsonEncode($output),
179+
),
180+
);
181+
}
182+
return [$rc, $output];
183+
}
184+
161185
// delete requests made by that user
162186
// delete user entry
163187
// delete user group entry
@@ -216,6 +240,7 @@ function ensureUserNotInPIGroup(UnityGroup $pi_group)
216240
$pi_group->removeUser($USER);
217241
ensure(!$pi_group->memberExists($USER));
218242
}
243+
// FIXME removeUser already removes the uid from this redis, this shouldn't be necessary
219244
$REDIS->removeCacheArray(
220245
$pi_group->gid,
221246
"members",
@@ -338,3 +363,17 @@ function getAdminUser()
338363
{
339364
return ["[email protected]", "foo", "bar", "[email protected]"];
340365
}
366+
367+
function getSomeUIDsOfQualifiedUsersNotRequestedAccountDeletion()
368+
{
369+
return [
370+
"user1_org1_test",
371+
"user3_org1_test",
372+
"user6_org1_test",
373+
"user7_org1_test",
374+
"user8_org1_test",
375+
"user9_org3_test",
376+
"user10_org1_test",
377+
"user11_org1_test",
378+
];
379+
}

0 commit comments

Comments
 (0)