|
| 1 | +<?php |
| 2 | + |
| 3 | +use PHPUnit\Framework\TestCase; |
| 4 | +use UnityWebPortal\lib\exceptions\PhpUnitNoDieException; |
| 5 | +use UnityWebPortal\lib\UnityGroup; |
| 6 | + |
| 7 | +class NewUserTest extends TestCase |
| 8 | +{ |
| 9 | + private function assertNumberGroupRequests(int $x) |
| 10 | + { |
| 11 | + global $USER, $SQL; |
| 12 | + $this->assertEquals($x, count($SQL->getRequestsByUser($USER->getUID()))); |
| 13 | + } |
| 14 | + |
| 15 | + private function requestGroupCreation() |
| 16 | + { |
| 17 | + $redirectedOrDied = false; |
| 18 | + try { |
| 19 | + http_post( |
| 20 | + __DIR__ . "/../../webroot/panel/new_account.php", |
| 21 | + ["new_user_sel" => "pi", "eula" => "agree", "confirm_pi" => "agree"] |
| 22 | + ); |
| 23 | + } catch (\UnityWebPortal\lib\exceptions\PhpUnitNoDieException) { |
| 24 | + $redirectedOrDied = true; |
| 25 | + } |
| 26 | + $this->assertTrue($redirectedOrDied); |
| 27 | + } |
| 28 | + |
| 29 | + private function requestGroupMembership(string $gid) |
| 30 | + { |
| 31 | + $redirectedOrDied = false; |
| 32 | + try { |
| 33 | + http_post( |
| 34 | + __DIR__ . "/../../webroot/panel/new_account.php", |
| 35 | + ["new_user_sel" => "not_pi", "eula" => "agree", "pi" => $gid] |
| 36 | + ); |
| 37 | + } catch (\UnityWebPortal\lib\exceptions\PhpUnitNoDieException) { |
| 38 | + $redirectedOrDied = true; |
| 39 | + } |
| 40 | + $this->assertTrue($redirectedOrDied); |
| 41 | + } |
| 42 | + |
| 43 | + private function cancelAllRequests() |
| 44 | + { |
| 45 | + $redirectedOrDied = false; |
| 46 | + try { |
| 47 | + http_post( |
| 48 | + __DIR__ . "/../../webroot/panel/new_account.php", |
| 49 | + ["cancel" => "true"] // value of cancel is arbitrary |
| 50 | + ); |
| 51 | + } catch (\UnityWebPortal\lib\exceptions\PhpUnitNoDieException) { |
| 52 | + $redirectedOrDied = true; |
| 53 | + } |
| 54 | + $this->assertTrue($redirectedOrDied); |
| 55 | + } |
| 56 | + |
| 57 | + // delete requests made by that user |
| 58 | + // delete user entry |
| 59 | + // remove user from org group |
| 60 | + // remove user from "all users" group |
| 61 | + // does not remove user from PI groups |
| 62 | + private function ensureUserDoesNotExist() |
| 63 | + { |
| 64 | + global $USER, $SQL, $LDAP; |
| 65 | + $SQL->deleteRequestsByUser($USER->getUID()); |
| 66 | + if ($USER->exists()) { |
| 67 | + $USER->getLDAPUser()->delete(); |
| 68 | + assert(!$USER->exists()); |
| 69 | + } |
| 70 | + $org = $USER->getOrgGroup(); |
| 71 | + if ($org->inOrg($USER)) { |
| 72 | + $org->removeUser($USER); |
| 73 | + assert(!$org->inOrg($USER)); |
| 74 | + } |
| 75 | + $all_users_group = $LDAP->getUserGroup(); |
| 76 | + $all_member_uids = $all_users_group->getAttribute("memberuid"); |
| 77 | + $new_uids = array_diff($all_member_uids, [$USER->getUID()]); |
| 78 | + if (in_array($USER->getUID(), $all_member_uids)) { |
| 79 | + $all_users_group->setAttribute( |
| 80 | + "memberuid", |
| 81 | + array_diff($all_member_uids, [$USER->getUID()]) |
| 82 | + ); |
| 83 | + $all_users_group->write(); |
| 84 | + assert(!in_array($USER->getUID(), $all_users_group->getAttribute("memberuid"))); |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + private function ensureOrgGroupDoesNotExist() |
| 89 | + { |
| 90 | + global $USER; |
| 91 | + $org_group = $USER->getOrgGroup(); |
| 92 | + if ($org_group->exists()) { |
| 93 | + $org_group->getLDAPOrgGroup()->delete(); |
| 94 | + assert(!$org_group->exists()); |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + private function ensureUserNotInPIGroup(UnityGroup $pi_group) |
| 99 | + { |
| 100 | + global $USER; |
| 101 | + if ($pi_group->userExists($USER)) { |
| 102 | + $pi_group->removeUser($USER); |
| 103 | + assert(!$pi_group->userExists($USER)); |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + private function ensurePIGroupDoesNotExist() |
| 108 | + { |
| 109 | + global $USER; |
| 110 | + if ($USER->getPIGroup()->exists()) { |
| 111 | + $USER->getPIGroup()->getLDAPPIGroup()->delete(); |
| 112 | + assert(!$USER->getPIGroup()->exists()); |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + public function testCreateUserByJoinGoup() |
| 117 | + { |
| 118 | + global $USER, $SQL, $LDAP; |
| 119 | + switchUser(...getUserIsPIHasNoMembersNoMemberRequests()); |
| 120 | + $pi_group = $USER->getPIGroup(); |
| 121 | + switchUser(...getNonExistentUser()); |
| 122 | + $this->assertTrue(!$USER->exists()); |
| 123 | + $this->assertTrue(!$USER->getOrgGroup()->exists()); |
| 124 | + $this->assertTrue($pi_group->exists()); |
| 125 | + $this->assertTrue(!$pi_group->userExists($USER)); |
| 126 | + $this->assertNumberGroupRequests(0); |
| 127 | + try { |
| 128 | + $this->requestGroupMembership($pi_group->getPIUID()); |
| 129 | + $this->assertNumberGroupRequests(1); |
| 130 | + |
| 131 | + // $second_request_failed = false; |
| 132 | + // try { |
| 133 | + $this->requestGroupMembership($pi_group->getPIUID()); |
| 134 | + // } catch(Exception) { |
| 135 | + // $second_request_failed = true; |
| 136 | + // } |
| 137 | + // $this->assertTrue($second_request_failed); |
| 138 | + $this->assertNumberGroupRequests(1); |
| 139 | + |
| 140 | + $this->cancelAllRequests(); |
| 141 | + $this->assertNumberGroupRequests(0); |
| 142 | + |
| 143 | + $this->requestGroupMembership($pi_group->getPIUID()); |
| 144 | + $this->assertTrue($pi_group->requestExists($USER)); |
| 145 | + $this->assertNumberGroupRequests(1); |
| 146 | + |
| 147 | + $pi_group->approveUser($USER); |
| 148 | + $this->assertTrue(!$pi_group->requestExists($USER)); |
| 149 | + $this->assertNumberGroupRequests(0); |
| 150 | + $this->assertTrue($pi_group->userExists($USER)); |
| 151 | + $this->assertTrue($USER->exists()); |
| 152 | + $this->assertTrue($USER->getOrgGroup()->exists()); |
| 153 | + |
| 154 | + // $third_request_failed = false; |
| 155 | + // try { |
| 156 | + $this->requestGroupMembership($pi_group->getPIUID()); |
| 157 | + // } catch(Exception) { |
| 158 | + // $third_request_failed = true; |
| 159 | + // } |
| 160 | + // $this->assertTrue($third_request_failed); |
| 161 | + $this->assertNumberGroupRequests(0); |
| 162 | + $this->assertTrue(!$pi_group->requestExists($USER)); |
| 163 | + } finally { |
| 164 | + $this->ensureUserNotInPIGroup($pi_group); |
| 165 | + $this->ensureUserDoesNotExist(); |
| 166 | + $this->ensureOrgGroupDoesNotExist(); |
| 167 | + } |
| 168 | + } |
| 169 | + |
| 170 | + public function testCreateUserByCreateGroup() |
| 171 | + { |
| 172 | + global $USER, $SQL, $LDAP; |
| 173 | + switchuser(...getNonExistentUser()); |
| 174 | + $pi_group = $USER->getPIGroup(); |
| 175 | + $this->assertTrue(!$USER->exists()); |
| 176 | + $this->assertTrue(!$pi_group->exists()); |
| 177 | + $this->assertTrue(!$USER->getOrgGroup()->exists()); |
| 178 | + try { |
| 179 | + $this->requestGroupCreation(); |
| 180 | + $this->assertNumberGroupRequests(1); |
| 181 | + |
| 182 | + // $second_request_failed = false; |
| 183 | + // try { |
| 184 | + $this->requestGroupCreation(); |
| 185 | + // } catch(Exception) { |
| 186 | + // $second_request_failed = true; |
| 187 | + // } |
| 188 | + // $this->assertTrue($second_request_failed); |
| 189 | + $this->assertNumberGroupRequests(1); |
| 190 | + |
| 191 | + $this->cancelAllRequests(); |
| 192 | + $this->assertNumberGroupRequests(0); |
| 193 | + |
| 194 | + $this->requestGroupCreation(); |
| 195 | + $this->assertNumberGroupRequests(1); |
| 196 | + |
| 197 | + $pi_group->approveGroup(); |
| 198 | + $this->assertNumberGroupRequests(0); |
| 199 | + $this->assertTrue($pi_group->exists()); |
| 200 | + $this->assertTrue($USER->exists()); |
| 201 | + $this->assertTrue($USER->getOrgGroup()->exists()); |
| 202 | + |
| 203 | + // $third_request_failed = false; |
| 204 | + // try { |
| 205 | + $this->requestGroupCreation(); |
| 206 | + // } catch(Exception) { |
| 207 | + // $third_request_failed = true; |
| 208 | + // } |
| 209 | + // $this->assertTrue($third_request_failed); |
| 210 | + $this->assertNumberGroupRequests(0); |
| 211 | + } finally { |
| 212 | + $this->ensurePIGroupDoesNotExist(); |
| 213 | + $this->ensureUserDoesNotExist(); |
| 214 | + $this->ensureOrgGroupDoesNotExist(); |
| 215 | + } |
| 216 | + } |
| 217 | +} |
0 commit comments