Skip to content

Commit 3e74125

Browse files
committed
add new test
1 parent b2489b6 commit 3e74125

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-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: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,3 +128,13 @@ function getUserWithOneKey()
128128
{
129129
return ["[email protected]", "foo", "bar", "[email protected]"];
130130
}
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)