Skip to content

Commit 3eaf305

Browse files
authored
add more page loading tests (#490)
1 parent 66d27b3 commit 3eaf305

File tree

11 files changed

+44
-38
lines changed

11 files changed

+44
-38
lines changed

deployment/overrides/phpunit/config/config.ini

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@ custom_user_mappings_dir = "test/custom_user_mappings"
44
[site]
55
allow_die = false
66
enable_verbose_error_log = false
7-
enable_redirect_message = false
87
enable_exception_handler = false
98
enable_error_handler = false

phpstan.neon

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ parameters:
3636
- '#Call to method PHPUnit\\Framework\\Assert::assertTrue\(\) with true will always evaluate to true.#'
3737
paths:
3838
- test/functional/InvalidEPPNTest.php
39-
- test/functional/PageLoadTest.php
4039
# I cannot seem to make this error go away no matter how many functions I add @phpstan-impure to
4140
- messages:
4241
- '#Negated boolean expression is always false\.#'

resources/lib/UnityHTTPD.php

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,15 @@ enum UnityHTTPDMessageLevel: string
1818

1919
class UnityHTTPD
2020
{
21-
public static function die(mixed $x = null, bool $show_user = false): never
21+
public static function die(?string $x = null): never
2222
{
23-
if (CONFIG["site"]["allow_die"] == false) {
24-
if (is_null($x)) {
25-
throw new NoDieException();
26-
} else {
27-
throw new NoDieException($x);
28-
}
23+
if ($x !== null) {
24+
echo $x;
25+
}
26+
if (CONFIG["site"]["allow_die"]) {
27+
die();
2928
} else {
30-
if (!is_null($x) and $show_user) {
31-
die($x);
32-
} else {
33-
die();
34-
}
29+
throw new NoDieException();
3530
}
3631
}
3732

@@ -165,14 +160,15 @@ private static function throwableToArray(\Throwable $t): array
165160

166161
public static function badRequest(
167162
string $log_message,
163+
string $user_message = "",
168164
?\Throwable $error = null,
169165
?array $data = null,
170166
): never {
171167
self::gracefulDie(
172168
"bad request",
173169
$log_message,
174170
"Invalid requested action or submitted data.",
175-
"",
171+
$user_message,
176172
error: $error,
177173
http_response_code: 400,
178174
data: $data,
@@ -181,14 +177,15 @@ public static function badRequest(
181177

182178
public static function forbidden(
183179
string $log_message,
180+
string $user_message = "",
184181
?\Throwable $error = null,
185182
?array $data = null,
186183
): never {
187184
self::gracefulDie(
188185
"forbidden",
189186
$log_message,
190187
"Permission denied.",
191-
"",
188+
$user_message,
192189
error: $error,
193190
http_response_code: 403,
194191
data: $data,
@@ -197,14 +194,15 @@ public static function forbidden(
197194

198195
public static function internalServerError(
199196
string $log_message,
197+
string $user_message = "",
200198
?\Throwable $error = null,
201199
?array $data = null,
202200
): never {
203201
self::gracefulDie(
204202
"internal server error",
205203
$log_message,
206204
"An internal server error has occurred.",
207-
"",
205+
$user_message,
208206
error: $error,
209207
http_response_code: 500,
210208
data: $data,

test/functional/PageLoadTest.php

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,34 @@ class PageLoadTest extends UnityWebPortalTestCase
77
public static function provider()
88
{
99
return [
10-
["Admin", __DIR__ . "/../../webroot/admin/pi-mgmt.php"],
11-
["Admin", __DIR__ . "/../../webroot/admin/user-mgmt.php"],
12-
["Admin", __DIR__ . "/../../webroot/admin/content.php"],
13-
["Admin", __DIR__ . "/../../webroot/admin/notices.php"],
14-
["NonExistent", __DIR__ . "/../../webroot/panel/new_account.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"],
10+
// normal page load
11+
["Admin", "admin/pi-mgmt.php", "/PI Management/"],
12+
["Admin", "admin/user-mgmt.php", "/User Management/"],
13+
["Admin", "admin/content.php", "/Page Content Management/"],
14+
["Admin", "admin/notices.php", "/Cluster Notice Management/"],
15+
["NonExistent", "panel/new_account.php", "/Register New Account/"],
16+
["Blank", "panel/account.php", "/Account Settings/"],
17+
["Blank", "panel/groups.php", "/My Principal Investigators/"],
18+
["Blank", "panel/support.php", "/Support/"],
19+
["EmptyPIGroupOwner", "panel/pi.php", "/My Users/"],
20+
// normal user should not be able to access admin pages
21+
["Blank", "admin/pi-mgmt.php", "/You are not an admin/"],
22+
["Blank", "admin/user-mgmt.php", "/You are not an admin/"],
23+
["Blank", "admin/content.php", "/You are not an admin/"],
24+
["Blank", "admin/notices.php", "/You are not an admin/"],
25+
// new_account.php should redirect to account.php if account already exists
26+
["Blank", "panel/new_account.php", "/panel\/account\.php/"],
27+
// all pages should redirect to new_account.php if account does not exist
28+
["NonExistent", "panel/account.php", "/panel\/new_account\.php/"],
1929
];
2030
}
2131

2232
#[DataProvider("provider")]
23-
public function testLoadPage($nickname, $path)
33+
public function testLoadPage($nickname, $path, $regex)
2434
{
2535
global $USER, $SSO, $LDAP, $SQL, $MAILER, $WEBHOOK;
2636
$this->switchUser($nickname);
27-
http_get($path);
28-
$this->assertTrue(true); // assert there were no errors
37+
$output = http_get(__DIR__ . "/../../webroot/" . $path);
38+
$this->assertMatchesRegularExpression($regex, $output);
2939
}
3040
}

test/phpunit-bootstrap.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ function http_post(
109109
}
110110
}
111111

112-
function http_get(string $phpfile, array $get_data = []): void
112+
function http_get(string $phpfile, array $get_data = []): string
113113
{
114114
global $LDAP,
115115
$SQL,
@@ -131,9 +131,9 @@ function http_get(string $phpfile, array $get_data = []): void
131131
try {
132132
include $phpfile;
133133
} finally {
134-
ob_get_clean(); // discard output
135134
unset($_GET);
136-
$_PREVIOUS_SERVER = $_SERVER;
135+
$_SERVER = $_PREVIOUS_SERVER;
136+
return ob_get_clean();
137137
}
138138
}
139139

webroot/admin/ajax/get_group_members.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use UnityWebPortal\lib\UserFlag;
88

99
if (!$USER->getFlag(UserFlag::ADMIN)) {
10-
UnityHTTPD::forbidden("not an admin");
10+
UnityHTTPD::forbidden("not an admin", "You are not an admin.");
1111
}
1212

1313
$gid = UnityHTTPD::getQueryParameter("gid");

webroot/admin/ajax/get_page_contents.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use UnityWebPortal\lib\UserFlag;
77

88
if (!$USER->getFlag(UserFlag::ADMIN)) {
9-
UnityHTTPD::forbidden("not an admin");
9+
UnityHTTPD::forbidden("not an admin", "You are not an admin.");
1010
}
1111

1212
$pageid = UnityHTTPD::getQueryParameter("pageid");

webroot/admin/content.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use UnityWebPortal\lib\UserFlag;
77

88
if (!$USER->getFlag(UserFlag::ADMIN)) {
9-
UnityHTTPD::forbidden("not an admin");
9+
UnityHTTPD::forbidden("not an admin", "You are not an admin.");
1010
}
1111

1212
if ($_SERVER["REQUEST_METHOD"] == "POST") {

webroot/admin/notices.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use UnityWebPortal\lib\UserFlag;
77

88
if (!$USER->getFlag(UserFlag::ADMIN)) {
9-
UnityHTTPD::forbidden("not an admin");
9+
UnityHTTPD::forbidden("not an admin", "You are not an admin.");
1010
}
1111

1212
if ($_SERVER["REQUEST_METHOD"] == "POST") {

webroot/admin/pi-mgmt.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
use UnityWebPortal\lib\UserFlag;
1010

1111
if (!$USER->getFlag(UserFlag::ADMIN)) {
12-
UnityHTTPD::forbidden("not an admin");
12+
UnityHTTPD::forbidden("not an admin", "You are not an admin.");
1313
}
1414

1515
$getUserFromPost = function () {

0 commit comments

Comments
 (0)