Skip to content

Commit fec39dd

Browse files
committed
new tests
1 parent d935553 commit fec39dd

File tree

1 file changed

+162
-0
lines changed

1 file changed

+162
-0
lines changed

test/functional/NewUserTest.php

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
<?php
2+
3+
use PHPUnit\Framework\TestCase;
4+
use UnityWebPortal\lib\exceptions\PhpUnitNoDieException;
5+
6+
class NewUserTest extends TestCase
7+
{
8+
private function assertNumberGroupRequests(int $x)
9+
{
10+
global $USER, $SQL;
11+
$this->assertEquals($x, count($SQL->getRequestsByUser($USER->getUID())));
12+
}
13+
14+
private function requestGroupCreation()
15+
{
16+
$redirectedOrDied = false;
17+
try {
18+
http_post(
19+
__DIR__ . "/../../webroot/panel/new_account.php",
20+
["new_user_sel" => "pi", "eula" => "agree", "confirm_pi" => "agree"]
21+
);
22+
} catch (\UnityWebPortal\lib\exceptions\PhpUnitNoDieException) {
23+
$redirectedOrDied = true;
24+
}
25+
$this->assertTrue($redirectedOrDied);
26+
}
27+
28+
private function requestGroupMembership(string $gid)
29+
{
30+
$redirectedOrDied = false;
31+
try {
32+
http_post(
33+
__DIR__ . "/../../webroot/panel/new_account.php",
34+
["new_user_sel" => "pi", "eula" => "agree", "confirm_pi" => "agree"]
35+
);
36+
} catch (\UnityWebPortal\lib\exceptions\PhpUnitNoDieException) {
37+
$redirectedOrDied = true;
38+
}
39+
$this->assertTrue($redirectedOrDied);
40+
}
41+
42+
private function cancelAllRequests()
43+
{
44+
$redirectedOrDied = false;
45+
try {
46+
http_post(
47+
__DIR__ . "/../../webroot/panel/new_account.php",
48+
["cancel" => "true"] // value of cancel is arbitrary
49+
);
50+
} catch (\UnityWebPortal\lib\exceptions\PhpUnitNoDieException) {
51+
$redirectedOrDied = true;
52+
}
53+
$this->assertTrue($redirectedOrDied);
54+
}
55+
56+
public function testCreateUserByJoinGoup()
57+
{
58+
global $USER, $SQL;
59+
switchUser(...getUserIsPIHasNoMembersNoMemberRequests());
60+
$pi_group = $USER->getPIGroup();
61+
switchUser(...getNonExistentUser());
62+
$this->assertTrue(!$USER->exists());
63+
$this->assertTrue($pi_group->exists());
64+
$this->assertTrue(!$pi_group->userExists($USER));
65+
$this->assertNumberGroupRequests(0);
66+
try {
67+
$this->requestGroupMembership($pi_group->getPIUID());
68+
$this->assertNumberGroupRequests(1);
69+
70+
$second_request_failed = false;
71+
try {
72+
$this->requestGroupMembership($pi_group->getPIUID());
73+
} catch(Exception) {
74+
$second_request_failed = true;
75+
}
76+
$this->assertTrue($second_request_failed);
77+
$this->assertNumberGroupRequests(1);
78+
79+
$this->cancelAllRequests();
80+
$this->assertNumberGroupRequests(0);
81+
82+
$this->requestGroupMembership($pi_group->getPIUID());
83+
$this->assertNumberGroupRequests(1);
84+
85+
$pi_group->approveUser($USER->getUID());
86+
$this->assertNumberGroupRequests(0);
87+
$this->assertTrue($pi_group->userExists($USER));
88+
$this->assertTrue($USER->exists());
89+
90+
$third_request_failed = false;
91+
try {
92+
$this->requestGroupMembership($pi_group->getPIUID());
93+
} catch(Exception) {
94+
$third_request_failed = true;
95+
}
96+
$this->assertTrue($third_request_failed);
97+
$this->assertNumberGroupRequests(0);
98+
} finally {
99+
$SQL->deleteRequestsByUser($USER->getUID());
100+
if ($pi_group->userExists($USER)) {
101+
$pi_group->removeUser($USER);
102+
}
103+
if ($USER->exists()) {
104+
$USER->getLDAPUser->delete();
105+
assert(!$USER->exists());
106+
}
107+
}
108+
}
109+
110+
public function testCreateUserByCreateGroup()
111+
{
112+
global $USER, $SQL;
113+
switchuser(...getNonExistentUser());
114+
$pi_group = $USER->getPIGroup();
115+
$this->assertTrue(!$USER->exists());
116+
$this->assertTrue(!$pi_group->exists());
117+
try {
118+
$this->requestGroupCreation();
119+
$this->assertNumberGroupRequests(1);
120+
$this->assertNumberGroupRequests(0);
121+
122+
$second_request_failed = false;
123+
try {
124+
$this->requestGroupCreation();
125+
} catch(Exception) {
126+
$second_request_failed = true;
127+
}
128+
$this->assertTrue($second_request_failed);
129+
$this->assertNumberGroupRequests(1);
130+
131+
$this->cancelAllRequests();
132+
$this->assertNumberGroupRequests(0);
133+
134+
$this->requestGroupCreation();
135+
$this->assertNumberGroupRequests(1);
136+
137+
$pi_group->approveGroup();
138+
$this->assertNumberGroupRequests(0);
139+
$this->assertTrue($pi_group->exists());
140+
$this->assertTrue($USER->exists());
141+
142+
$third_request_failed = false;
143+
try {
144+
$this->requestGroupCreation();
145+
} catch(Exception) {
146+
$third_request_failed = true;
147+
}
148+
$this->assertTrue($third_request_failed);
149+
$this->assertNumberGroupRequests(0);
150+
} finally {
151+
$SQL->deleteRequestsByUser($USER->getUID());
152+
if ($pi_group->exists()) {
153+
$pi_group->getLDAPPIGroup()->delete();
154+
assert(!$pi_group->exists());
155+
}
156+
if ($USER->exists()) {
157+
$USER->getLDAPUser->delete();
158+
assert(!$USER->exists());
159+
}
160+
}
161+
}
162+
}

0 commit comments

Comments
 (0)