Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 11 additions & 81 deletions test/functional/NewUserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,76 +85,6 @@ private function approveGroup($uid)
]);
}

// delete requests made by that user
// delete user entry
// delete user group entry
// remove user from org group
// remove user from "all users" group
// does not remove user from PI groups
private function ensureUserDoesNotExist()
{
global $USER, $SQL, $LDAP, $REDIS;
$SQL->deleteRequestsByUser($USER->uid);
if ($USER->exists()) {
$org = $USER->getOrgGroup();
if ($org->exists() and $org->inOrg($USER)) {
$org->removeUser($USER);
ensure(!$org->inOrg($USER));
}
$LDAP->getUserEntry($USER->uid)->delete();
ensure(!$USER->exists());
}
if ($USER->getGroupEntry()->exists()) {
$USER->getGroupEntry()->delete();
ensure(!$USER->getGroupEntry()->exists());
}
$all_users_group = $LDAP->getQualifiedUserGroup();
$all_member_uids = $all_users_group->getAttribute("memberuid");
if (in_array($USER->uid, $all_member_uids)) {
$all_users_group->setAttribute(
"memberuid",
// array_diff will break the contiguity of the array indexes
// ldap_mod_replace requires contiguity, array_values restores contiguity
array_values(array_diff($all_member_uids, [$USER->uid])),
);
$all_users_group->write();
ensure(!in_array($USER->uid, $all_users_group->getAttribute("memberuid")));
}
$REDIS->removeCacheArray("sorted_qualified_users", "", $USER->uid);
}

private function ensureOrgGroupDoesNotExist()
{
global $USER, $SSO, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK;
$org_group = $LDAP->getOrgGroupEntry($SSO["org"]);
if ($org_group->exists()) {
$org_group->delete();
ensure(!$org_group->exists());
}
$REDIS->removeCacheArray("sorted_orgs", "", $SSO["org"]);
}

private function ensureUserNotInPIGroup(UnityGroup $pi_group)
{
global $USER, $REDIS;
if ($pi_group->memberExists($USER)) {
$pi_group->removeUser($USER);
ensure(!$pi_group->memberExists($USER));
}
$REDIS->removeCacheArray($pi_group->gid, "members", $USER->uid);
}

private function ensurePIGroupDoesNotExist()
{
global $USER, $LDAP, $REDIS;
$gid = $USER->getPIGroup()->gid;
if ($USER->getPIGroup()->exists()) {
$LDAP->getPIGroupEntry($gid)->delete();
ensure(!$USER->getPIGroup()->exists());
}
$REDIS->removeCacheArray("sorted_groups", "", $gid);
}

#[DataProvider("provider")]
public function testCreateUserByJoinGoupByPI($user_to_create_args, $expected_uid_gid)
{
Expand Down Expand Up @@ -222,9 +152,9 @@ public function testCreateUserByJoinGoupByPI($user_to_create_args, $expected_uid
$this->assertTrue(!$pi_group->requestExists($USER));
} finally {
switchUser(...$user_to_create_args);
$this->ensureOrgGroupDoesNotExist();
$this->ensureUserNotInPIGroup($pi_group);
$this->ensureUserDoesNotExist();
ensureOrgGroupDoesNotExist();
ensureUserNotInPIGroup($pi_group);
ensureUserDoesNotExist();
}
}

Expand Down Expand Up @@ -258,8 +188,8 @@ public function testCreateMultipleUsersByJoinGoupByPI()
} finally {
foreach ($users_to_create_args as $user_to_create_args) {
switchUser(...$user_to_create_args);
$this->ensureUserNotInPIGroup($pi_group);
$this->ensureUserDoesNotExist();
ensureUserNotInPIGroup($pi_group);
ensureUserDoesNotExist();
}
}
}
Expand Down Expand Up @@ -330,9 +260,9 @@ public function testCreateUserByJoinGoupByAdmin($user_to_create_args, $expected_
$this->assertTrue(!$pi_group->requestExists($USER));
} finally {
switchUser(...$user_to_create_args);
$this->ensureOrgGroupDoesNotExist();
$this->ensureUserNotInPIGroup($pi_group);
$this->ensureUserDoesNotExist();
ensureOrgGroupDoesNotExist();
ensureUserNotInPIGroup($pi_group);
ensureUserDoesNotExist();
}
}

Expand Down Expand Up @@ -395,9 +325,9 @@ public function testCreateUserByCreateGroup($user_to_create_args, $expected_uid_
$this->assertRequestedPIGroup(false);
} finally {
switchUser(...$user_to_create_args);
$this->ensureOrgGroupDoesNotExist();
$this->ensurePIGroupDoesNotExist();
$this->ensureUserDoesNotExist();
ensureOrgGroupDoesNotExist();
ensurePIGroupDoesNotExist();
ensureUserDoesNotExist();
}
}
}
72 changes: 72 additions & 0 deletions test/phpunit-bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
require_once __DIR__ . "/../resources/lib/exceptions/EncodingUnknownException.php";
require_once __DIR__ . "/../resources/lib/exceptions/EncodingConversionException.php";

use UnityWebPortal\lib\UnityGroup;

$_SERVER["HTTP_HOST"] = "phpunit"; // used for config override
require_once __DIR__ . "/../resources/config.php";

Expand Down Expand Up @@ -161,6 +163,76 @@ function http_get(string $phpfile, array $get_data = []): void
}
}

// delete requests made by that user
// delete user entry
// delete user group entry
// remove user from org group
// remove user from "all users" group
// does not remove user from PI groups
function ensureUserDoesNotExist()
{
global $USER, $SQL, $LDAP, $REDIS;
$SQL->deleteRequestsByUser($USER->uid);
if ($USER->exists()) {
$org = $USER->getOrgGroup();
if ($org->exists() and $org->inOrg($USER)) {
$org->removeUser($USER);
ensure(!$org->inOrg($USER));
}
$LDAP->getUserEntry($USER->uid)->delete();
ensure(!$USER->exists());
}
if ($USER->getGroupEntry()->exists()) {
$USER->getGroupEntry()->delete();
ensure(!$USER->getGroupEntry()->exists());
}
$all_users_group = $LDAP->getQualifiedUserGroup();
$all_member_uids = $all_users_group->getAttribute("memberuid");
if (in_array($USER->uid, $all_member_uids)) {
$all_users_group->setAttribute(
"memberuid",
// array_diff will break the contiguity of the array indexes
// ldap_mod_replace requires contiguity, array_values restores contiguity
array_values(array_diff($all_member_uids, [$USER->uid])),
);
$all_users_group->write();
ensure(!in_array($USER->uid, $all_users_group->getAttribute("memberuid")));
}
$REDIS->removeCacheArray("sorted_qualified_users", "", $USER->uid);
}

function ensureOrgGroupDoesNotExist()
{
global $USER, $SSO, $LDAP, $SQL, $MAILER, $REDIS, $WEBHOOK;
$org_group = $LDAP->getOrgGroupEntry($SSO["org"]);
if ($org_group->exists()) {
$org_group->delete();
ensure(!$org_group->exists());
}
$REDIS->removeCacheArray("sorted_orgs", "", $SSO["org"]);
}

function ensureUserNotInPIGroup(UnityGroup $pi_group)
{
global $USER, $REDIS;
if ($pi_group->memberExists($USER)) {
$pi_group->removeUser($USER);
ensure(!$pi_group->memberExists($USER));
}
$REDIS->removeCacheArray($pi_group->gid, "members", $USER->uid);
}

function ensurePIGroupDoesNotExist()
{
global $USER, $LDAP, $REDIS;
$gid = $USER->getPIGroup()->gid;
if ($USER->getPIGroup()->exists()) {
$LDAP->getPIGroupEntry($gid)->delete();
ensure(!$USER->getPIGroup()->exists());
}
$REDIS->removeCacheArray("sorted_groups", "", $gid);
}

function getNormalUser()
{
return ["user2@org1.test", "foo", "bar", "user2@org1.test"];
Expand Down