Skip to content

Commit 4c8160f

Browse files
committed
add PRG into http_post function
1 parent f578f37 commit 4c8160f

File tree

3 files changed

+38
-56
lines changed

3 files changed

+38
-56
lines changed

test/functional/CancelRequestTest.php

Lines changed: 16 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,18 @@ public function testCancelPIRequest()
1616
global $USER, $SQL;
1717
switchUser(...getNonExistentUser());
1818
// First create a request
19-
try {
20-
http_post(
21-
__DIR__ . "/../../webroot/panel/new_account.php",
22-
["new_user_sel" => "pi", "eula" => "agree", "confirm_pi" => "agree"]
23-
);
24-
} catch (PhpUnitNoDieException $e) {
25-
// Ignore the exception from http_post
26-
}
19+
http_post(
20+
__DIR__ . "/../../webroot/panel/new_account.php",
21+
["new_user_sel" => "pi", "eula" => "agree", "confirm_pi" => "agree"]
22+
);
2723

2824
$this->assertNumberGroupRequests(1);
2925

3026
// Now try to cancel it
31-
try {
32-
http_post(
33-
__DIR__ . "/../../webroot/panel/new_account.php",
34-
["cancel" => "true"] # value of cancel is arbitrary
35-
);
36-
} catch (PhpUnitNoDieException $e) {
37-
// Ignore the exception from http_post
38-
}
27+
http_post(
28+
__DIR__ . "/../../webroot/panel/new_account.php",
29+
["cancel" => "true"] # value of cancel is arbitrary
30+
);
3931

4032
$this->assertNumberGroupRequests(0);
4133
}
@@ -45,26 +37,18 @@ public function testCancelGroupJoinRequest()
4537
global $USER, $SQL;
4638
switchUser(...getNonExistentUser());
4739

48-
try {
49-
http_post(
50-
__DIR__ . "/../../webroot/panel/new_account.php",
51-
["new_user_sel" => "not_pi", "eula" => "agree", "pi" => getExistingPI()]
52-
);
53-
} catch (PhpUnitNoDieException $e) {
54-
// Ignore the exception from http_post
55-
}
40+
http_post(
41+
__DIR__ . "/../../webroot/panel/new_account.php",
42+
["new_user_sel" => "not_pi", "eula" => "agree", "pi" => getExistingPI()]
43+
);
5644

5745
$this->assertNumberGroupRequests(1);
5846

5947
// Now try to cancel it
60-
try {
61-
http_post(
62-
__DIR__ . "/../../webroot/panel/new_account.php",
63-
["cancel" => "true"] # value of cancel is arbitrary
64-
);
65-
} catch (PhpUnitNoDieException $e) {
66-
// Ignore the exception from http_post
67-
}
48+
http_post(
49+
__DIR__ . "/../../webroot/panel/new_account.php",
50+
["cancel" => "true"] # value of cancel is arbitrary
51+
);
6852

6953
$this->assertNumberGroupRequests(0);
7054
}

test/functional/ViewAsUserTest.php

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,13 @@ public function _testViewAsUser(array $beforeUser, array $afterUser)
1515
// $this->assertTrue($USER->isAdmin());
1616
$beforeUid = $USER->getUID();
1717
// $this->assertNotEquals($afterUid, $beforeUid);
18-
try {
19-
http_post(
20-
__DIR__ . "/../../webroot/admin/user-mgmt.php",
21-
[
22-
"form_name" => "viewAsUser",
23-
"uid" => $afterUid,
24-
],
25-
);
26-
} catch (PhpUnitNoDieException) {}
18+
http_post(
19+
__DIR__ . "/../../webroot/admin/user-mgmt.php",
20+
[
21+
"form_name" => "viewAsUser",
22+
"uid" => $afterUid,
23+
],
24+
);
2725
$this->assertArrayHasKey("viewUser", $_SESSION);
2826
// redirect means that php process dies and user's browser will initiate a new one
2927
// this makes `require_once autoload.php` run again and init.php changes $USER
@@ -32,12 +30,10 @@ public function _testViewAsUser(array $beforeUser, array $afterUser)
3230
// now we should be new user
3331
$this->assertEquals($afterUid, $USER->getUID());
3432
// $this->assertTrue($_SESSION["user_exists"]);
35-
try {
36-
http_post(
37-
__DIR__ . "/../../resources/templates/header.php",
38-
["form_name" => "clearView"],
39-
);
40-
} catch (PhpUnitNoDieException) {}
33+
http_post(
34+
__DIR__ . "/../../resources/templates/header.php",
35+
["form_name" => "clearView"],
36+
);
4137
$this->assertArrayNotHasKey("viewUser", $_SESSION);
4238
// redirect means that php process dies and user's browser will initiate a new one
4339
// this makes `require_once autoload.php` run again and init.php changes $USER
@@ -69,15 +65,13 @@ public function testNonAdminViewAsAdmin()
6965
$adminUid = $USER->getUID();
7066
$this->assertTrue($USER->isAdmin());
7167
switchUser(...getNormalUser());
72-
try {
73-
http_post(
74-
__DIR__ . "/../../webroot/admin/user-mgmt.php",
75-
[
76-
"form_name" => "viewAsUser",
77-
"uid" => $adminUid,
78-
],
79-
);
80-
} catch (PhpUnitNoDieException) {}
68+
http_post(
69+
__DIR__ . "/../../webroot/admin/user-mgmt.php",
70+
[
71+
"form_name" => "viewAsUser",
72+
"uid" => $adminUid,
73+
],
74+
);
8175
$this->assertArrayNotHasKey("viewUser", $_SESSION);
8276
}
8377
}

test/phpunit-bootstrap.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,17 @@ function http_post(string $phpfile, array $post_data): void
8181
$_SERVER["PHP_SELF"] = preg_replace("/.*webroot\//", "/", $phpfile);
8282
$_POST = $post_data;
8383
ob_start();
84+
$post_did_redirect = false;
8485
try {
8586
include $phpfile;
87+
} catch (UnityWebPortal\lib\exceptions\PhpUnitNoDieException $e) {
88+
$post_did_redirect = true;
8689
} finally {
8790
ob_get_clean(); // discard output
8891
unset($_POST);
8992
$_SERVER = $_PREVIOUS_SERVER;
9093
}
94+
assert($post_did_redirect, "post did not redirect!");
9195
}
9296

9397
function http_get(string $phpfile, array $get_data = array()): void

0 commit comments

Comments
 (0)