Skip to content

Commit a73bae0

Browse files
authored
refactor tests 3 (#482)
1 parent 5d9f4fc commit a73bae0

14 files changed

+217
-171
lines changed

CONTRIBUTING.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,9 @@ Note: `phpunit` can misbehave when using `expectException` and `try`/`finally`,
134134

135135
### creating the conditions for a test
136136

137-
Selecting users for tests happens with the `get...User...` family of functions from `phpunit-bootstrap.php`.
138-
Since this family of functions is growing large and their names long and complicated, it is better to start with a simpler state and create the desired conditions manually.
139-
For example, rather than using `getUserWithOneKey`, use `getUserHasNoSSHKeys` and add one key for them.
137+
There are many user conditions available which are selected using the `$nickname` argument to the `switchUser` function.
138+
Examples: `"Blank"`, `"NormalPI"`, `"EmptyPIGroupOwner"`.
139+
See `phpunit-bootstrap.php` for more information.
140140

141141
The LDAP entries available in the dev environment are defined in `tools/docker-dev/identity/bootstrap.ldif`.
142142
These entries may be subject to change.

test/functional/AccountDeletionRequestTest.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ public function testRequestAccountDeletionUserHasNoGroups()
66
{
77
global $USER, $SQL;
88
$this->switchUser("Blank");
9-
$this->assertEmpty($USER->getPIGroupGIDs());
109
$this->assertNumberAccountDeletionRequests(0);
1110
try {
1211
http_post(__DIR__ . "/../../webroot/panel/account.php", [
@@ -27,8 +26,7 @@ public function testRequestAccountDeletionUserHasGroup()
2726
{
2827
// FIXME this should be an error
2928
global $USER, $SQL;
30-
$this->switchUser("HasNotRequestedAccountDeletionHasGroup");
31-
$this->assertNotEmpty($USER->getPIGroupGIDs());
29+
$this->switchUser("NormalPI");
3230
$this->assertNumberAccountDeletionRequests(0);
3331
try {
3432
http_post(__DIR__ . "/../../webroot/panel/account.php", [
@@ -45,14 +43,10 @@ public function testRequestAccountDeletionUserHasGroup()
4543
public function testRequestAccountDeletionUserHasRequest()
4644
{
4745
global $USER, $SQL;
48-
$this->switchUser("IsPIHasNoMembersNoMemberRequests");
49-
$pi = $USER;
46+
$this->switchUser("EmptyPIGroupOwner");
5047
$pi_group = $USER->getPIGroup();
51-
$this->assertEqualsCanonicalizing([$pi->uid], $pi_group->getMemberUIDs());
5248
$this->switchUser("Blank");
53-
$this->assertEmpty($USER->getPIGroupGIDs());
5449
$this->assertNumberAccountDeletionRequests(0);
55-
$this->assertNumberRequests(0);
5650
try {
5751
$pi_group->newUserRequest($USER);
5852
$this->assertNumberRequests(1);
@@ -64,17 +58,17 @@ public function testRequestAccountDeletionUserHasRequest()
6458
} finally {
6559
$SQL->deleteAccountDeletionRequest($USER->uid);
6660
$this->assertNumberAccountDeletionRequests(0);
67-
ensureUserNotInPIGroup($pi_group);
61+
if ($pi_group->requestExists($USER)) {
62+
$pi_group->cancelGroupJoinRequest($USER);
63+
}
6864
}
6965
}
7066

7167
public function testRequestAccountDeletionCancel()
7268
{
7369
global $USER;
7470
$this->switchUser("Blank");
75-
$this->assertEmpty($USER->getPIGroupGIDs());
7671
$this->assertNumberAccountDeletionRequests(0);
77-
$this->assertNumberRequests(0);
7872
try {
7973
http_post(__DIR__ . "/../../webroot/panel/account.php", [
8074
"form_type" => "account_deletion_request",

test/functional/LoginShellSetTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ public function setUp(): void
1010
{
1111
global $USER;
1212
parent::setUp();
13-
$this->switchUser("Normal");
13+
$this->switchUser("Blank");
1414
self::$_initialLoginShell = $USER->getLoginShell();
1515
}
1616

test/functional/PIBecomeApproveTest.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,8 @@ private function approveGroup($uid)
3232
public function testApprovePI()
3333
{
3434
global $USER, $SSO, $LDAP, $SQL, $MAILER, $WEBHOOK;
35-
$this->switchUser("Unqualified");
35+
$this->switchUser("Blank");
3636
$pi_group = $USER->getPIGroup();
37-
$this->assertTrue($USER->exists());
38-
$this->assertFalse($pi_group->exists());
3937
try {
4038
$this->requestGroupCreation();
4139
$this->assertRequestedPIGroup(true);
@@ -58,7 +56,7 @@ public function testApprovePI()
5856
$approve_uid = $SSO["user"];
5957
$this->switchUser("Admin");
6058
$this->approveGroup($approve_uid);
61-
$this->switchUser("Unqualified");
59+
$this->switchUser("Blank", validate: false);
6260

6361
$this->assertRequestedPIGroup(false);
6462
$this->assertTrue($pi_group->exists());
@@ -73,7 +71,7 @@ public function testApprovePI()
7371
// $this->assertTrue($third_request_failed);
7472
$this->assertRequestedPIGroup(false);
7573
} finally {
76-
$this->switchUser("Unqualified");
74+
$this->switchUser("Blank", validate: false);
7775
ensurePIGroupDoesNotExist();
7876
}
7977
}

test/functional/PIMemberRequestTest.php

Lines changed: 88 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use UnityWebPortal\lib\UnityHTTPDMessageLevel;
44
use UnityWebPortal\lib\UnitySQL;
5+
use UnityWebPortal\lib\UnityGroup;
56
use UnityWebPortal\lib\UnityHTTPD;
67

78
class PIMemberRequestTest extends UnityWebPortalTestCase
@@ -23,39 +24,107 @@ private function cancelRequest(string $gid)
2324
]);
2425
}
2526

26-
public function testRequestMembership()
27+
public function testRequestMembershipAndCancel()
2728
{
2829
global $USER, $SQL;
29-
$this->switchUser("IsPIHasNoMembersNoMemberRequests");
30-
$pi = $USER;
30+
$this->switchUser("EmptyPIGroupOwner");
31+
$pi_group = $USER->getPIGroup();
32+
$this->switchUser("Blank");
33+
try {
34+
$this->requestMembership($pi_group->gid);
35+
$this->assertTrue($pi_group->requestExists($USER));
36+
$this->cancelRequest($pi_group->gid);
37+
$this->assertFalse($pi_group->requestExists($USER));
38+
} finally {
39+
if ($SQL->requestExists($USER->uid, $pi_group->gid)) {
40+
$SQL->removeRequest($USER->uid, $pi_group->gid);
41+
}
42+
}
43+
}
44+
45+
public function testRequestMembershipBogus()
46+
{
47+
global $USER, $SQL;
48+
$this->switchUser("EmptyPIGroupOwner");
3149
$pi_group = $USER->getPIGroup();
32-
$gid = $pi_group->gid;
33-
$this->assertTrue($USER->isPI());
34-
$this->assertTrue($pi_group->exists());
35-
$this->assertEqualsCanonicalizing([$pi], $pi_group->getGroupMembers());
36-
$this->assertEqualsCanonicalizing([], $SQL->getRequests($gid));
3750
$this->switchUser("Blank");
38-
$uid = $USER->uid;
39-
$this->assertFalse($USER->isPI());
40-
$this->assertFalse($SQL->requestExists($uid, UnitySQL::REQUEST_BECOME_PI));
41-
$this->assertFalse($pi_group->memberUIDExists($USER->uid));
4251
try {
43-
$this->requestMembership($gid);
44-
$this->assertTrue($SQL->requestExists($uid, $gid));
45-
$this->cancelRequest($gid);
46-
$this->assertFalse($SQL->requestExists($uid, $gid));
4752
UnityHTTPD::clearMessages();
4853
$this->requestMembership("asdlkjasldkj");
4954
$this->assertMessageExists(
5055
UnityHTTPDMessageLevel::ERROR,
5156
"/^This PI Doesn't Exist$/",
5257
"/.*/",
5358
);
59+
$this->assertFalse($pi_group->requestExists($USER));
60+
} finally {
61+
if ($SQL->requestExists($USER->uid, $pi_group->gid)) {
62+
$SQL->removeRequest($USER->uid, $pi_group->gid);
63+
}
64+
}
65+
}
66+
67+
public function testRequestMembershipByOwnerMail()
68+
{
69+
global $USER, $SQL;
70+
$this->switchUser("EmptyPIGroupOwner");
71+
$pi_group = $USER->getPIGroup();
72+
$this->switchUser("Blank");
73+
try {
5474
$this->requestMembership($pi_group->getOwner()->getMail());
55-
$this->assertTrue($SQL->requestExists($uid, $gid));
75+
$this->assertTrue($pi_group->requestExists($USER));
76+
} finally {
77+
if ($SQL->requestExists($USER->uid, $pi_group->gid)) {
78+
$SQL->removeRequest($USER->uid, $pi_group->gid);
79+
}
80+
}
81+
}
82+
83+
public function testRequestMembershipDuplicate()
84+
{
85+
global $USER, $SQL;
86+
$this->switchUser("EmptyPIGroupOwner");
87+
$pi_group = $USER->getPIGroup();
88+
$this->switchUser("Blank");
89+
$this->assertNumberRequests(0);
90+
try {
91+
$this->requestMembership($pi_group->gid);
92+
$this->assertNumberRequests(1);
93+
// $second_request_failed = false;
94+
// try {
95+
$this->requestMembership($pi_group->gid);
96+
// } catch(Exception) {
97+
// $second_request_failed = true;
98+
// }
99+
// $this->assertTrue($second_request_failed);
100+
$this->assertNumberRequests(1);
101+
} finally {
102+
if ($SQL->requestExists($USER->uid, $pi_group->gid)) {
103+
$SQL->removeRequest($USER->uid, $pi_group->gid);
104+
}
105+
}
106+
}
107+
108+
public function testRequestBecomePiAlreadyInGroup()
109+
{
110+
global $USER, $SQL;
111+
$this->switchUser("Normal");
112+
$pi_group_gids = $USER->getPIGroupGIDs();
113+
$this->assertGreaterThanOrEqual(1, count($pi_group_gids));
114+
$gid = $pi_group_gids[0];
115+
$this->assertNumberRequests(0);
116+
try {
117+
// $request_failed = false;
118+
// try {
119+
$this->requestMembership($gid);
120+
// } catch(Exception) {
121+
// $request_failed = true;
122+
// }
123+
// $this->assertTrue($request_failed);
124+
$this->assertNumberRequests(0);
56125
} finally {
57-
if ($SQL->requestExists($uid, $gid)) {
58-
$SQL->removeRequest($uid, $gid);
126+
if ($SQL->requestExists($USER->uid, $gid)) {
127+
$SQL->removeRequest($USER->uid, $gid);
59128
}
60129
}
61130
}

test/functional/PageLoadTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ public static function provider()
1212
["Admin", __DIR__ . "/../../webroot/admin/content.php"],
1313
["Admin", __DIR__ . "/../../webroot/admin/notices.php"],
1414
["NonExistent", __DIR__ . "/../../webroot/panel/new_account.php"],
15-
["Normal", __DIR__ . "/../../webroot/panel/account.php"],
16-
["Normal", __DIR__ . "/../../webroot/panel/groups.php"],
17-
["Normal", __DIR__ . "/../../webroot/panel/support.php"],
18-
["IsPIHasNoMembersNoMemberRequests", __DIR__ . "/../../webroot/panel/pi.php"],
15+
["Blank", __DIR__ . "/../../webroot/panel/account.php"],
16+
["Blank", __DIR__ . "/../../webroot/panel/groups.php"],
17+
["Blank", __DIR__ . "/../../webroot/panel/support.php"],
18+
["EmptyPIGroupOwner", __DIR__ . "/../../webroot/panel/pi.php"],
1919
];
2020
}
2121

test/functional/PiBecomeRequestTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ public function testRequestBecomePi()
88
{
99
global $USER, $SQL;
1010
$this->switchUser("Blank");
11-
$this->assertFalse($USER->isPI());
1211
$this->assertNumberPiBecomeRequests(0);
1312
try {
1413
http_post(__DIR__ . "/../../webroot/panel/account.php", [
@@ -43,11 +42,10 @@ public function testRequestBecomePi()
4342
public function testRequestBecomePiUserRequestedAccountDeletion()
4443
{
4544
global $USER, $SQL;
46-
$this->switchUser("NotPiNotRequestedBecomePiRequestedAccountDeletion");
47-
$this->assertFalse($USER->isPI());
45+
$this->switchUser("Blank");
4846
$this->assertNumberPiBecomeRequests(0);
49-
$this->assertTrue($SQL->accDeletionRequestExists($USER->uid));
5047
try {
48+
$USER->requestAccountDeletion();
5149
http_post(__DIR__ . "/../../webroot/panel/account.php", [
5250
"form_type" => "pi_request",
5351
"tos" => "agree",
@@ -58,6 +56,9 @@ public function testRequestBecomePiUserRequestedAccountDeletion()
5856
if ($SQL->requestExists($USER, UnitySQL::REQUEST_BECOME_PI)) {
5957
$SQL->removeRequest($USER->uid, UnitySQL::REQUEST_BECOME_PI);
6058
}
59+
if ($SQL->accDeletionRequestExists($USER->uid)) {
60+
$SQL->deleteAccountDeletionRequest($USER->uid);
61+
}
6162
}
6263
}
6364
}

0 commit comments

Comments
 (0)