|
| 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 | +} |
0 commit comments