Skip to content

Commit e466ba2

Browse files
committed
viewAsUser test
1 parent 6951cf5 commit e466ba2

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

test/functional/ViewAsUserTest.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
use UnityWebPortal\lib\UnitySite;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use PHPUnit\Framework\Attributes\DataProvider;
7+
use PHPUnit\Framework\MockObject\MockBuilder;
8+
9+
class ViewAsUserTest extends TestCase
10+
{
11+
public function testViewAsUser()
12+
{
13+
global $USER, $CONFIG, $SITE;
14+
switchUser(...getAdminUser());
15+
$this->assertEquals("user1_org1_test", $USER->getUID());
16+
$this->assertTrue($USER->isAdmin());
17+
$adminUid = $USER->getUID();
18+
$oldSite = $SITE;
19+
try {
20+
$SITE = $this->createMock(UnitySite::class);
21+
$SITE->method("redirect");
22+
post(
23+
__DIR__ . "/../../webroot/admin/user-mgmt.php",
24+
[
25+
"form_name" => "viewAsUser",
26+
"uid" => "foobar",
27+
],
28+
);
29+
$this->assertArrayHasKey("viewUser", $_SESSION);
30+
// redirect means that php process dies and user's browser will initiate a new one
31+
// this makes `require_once autoload.php` run again and init.php changes $USER
32+
session_write_close();
33+
get(__DIR__ . "/../../resources/init.php");
34+
$SITE = $this->createMock(UnitySite::class);
35+
$SITE->method("redirect");
36+
// now we should be new user
37+
$this->assertEquals("foobar", $USER->getUID());
38+
post(
39+
__DIR__ . "/../../resources/templates/header.php",
40+
["form_name" => "clearView"],
41+
);
42+
// redirect means that php process dies and user's browser will initiate a new one
43+
// this makes `require_once autoload.php` run again and init.php changes $USER
44+
session_write_close();
45+
get(__DIR__ . "/../../resources/init.php");
46+
// now we should be back to original user
47+
$this->assertEquals($adminUid, $USER->getUID());
48+
} finally {
49+
$SITE = $oldSite;
50+
}
51+
}
52+
}

test/phpunit-bootstrap.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,22 @@ function post(string $phpfile, array $post_data): void
7878
}
7979
}
8080

81+
function get(string $phpfile): void
82+
{
83+
global $CONFIG, $REDIS, $LDAP, $SQL, $MAILER, $WEBHOOK, $GITHUB, $SITE, $SSO, $OPERATOR, $USER, $SEND_PIMESG_TO_ADMINS, $LOC_HEADER, $LOC_FOOTER;
84+
$_SERVER["REQUEST_METHOD"] = "GET";
85+
ob_start();
86+
try {
87+
include $phpfile;
88+
ob_get_clean(); // discard output
89+
} catch (Throwable $e) {
90+
error_log(ob_get_clean()); // don't discard output
91+
throw $e;
92+
} finally {
93+
unset($_SERVER["REQUEST_METHOD"]);
94+
}
95+
}
96+
8197
function getNormalUser()
8298
{
8399
return ["[email protected]", "foo", "bar", "[email protected]"];
@@ -112,3 +128,13 @@ function getUserWithOneKey()
112128
{
113129
return ["[email protected]", "foo", "bar", "[email protected]"];
114130
}
131+
132+
function getNonExistentUser()
133+
{
134+
return ["[email protected]", "foo", "bar", "[email protected]"];
135+
}
136+
137+
function getAdminUser()
138+
{
139+
return ["[email protected]", "foo", "bar", "[email protected]"];
140+
}

0 commit comments

Comments
 (0)