Skip to content

Commit 7b4b4a1

Browse files
committed
update ViewAsUserTest
1 parent 42091f8 commit 7b4b4a1

File tree

1 file changed

+19
-26
lines changed

1 file changed

+19
-26
lines changed

test/functional/ViewAsUserTest.php

Lines changed: 19 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,45 @@
11
<?php
22

33
use UnityWebPortal\lib\UnitySite;
4-
4+
use UnityWebPortal\lib\exceptions\PhpUnitNoDieException;
55
use PHPUnit\Framework\TestCase;
66
use PHPUnit\Framework\Attributes\DataProvider;
7-
use PHPUnit\Framework\MockObject\MockBuilder;
87

98
class ViewAsUserTest extends TestCase
109
{
1110
public function testViewAsUser()
1211
{
13-
global $USER, $CONFIG, $SITE;
12+
global $USER;
1413
switchUser(...getAdminUser());
15-
$this->assertEquals("user1_org1_test", $USER->getUID());
1614
$this->assertTrue($USER->isAdmin());
17-
$adminUid = $USER->getUID();
18-
$oldSite = $SITE;
15+
$originalUid = $USER->getUID();
1916
try {
20-
$SITE = $this->createMock(UnitySite::class);
21-
$SITE->method("redirect");
2217
post(
2318
__DIR__ . "/../../webroot/admin/user-mgmt.php",
2419
[
2520
"form_name" => "viewAsUser",
2621
"uid" => "foobar",
2722
],
2823
);
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());
24+
} catch (PhpUnitNoDieException) {}
25+
$this->assertArrayHasKey("viewUser", $_SESSION);
26+
// redirect means that php process dies and user's browser will initiate a new one
27+
// this makes `require_once autoload.php` run again and init.php changes $USER
28+
session_write_close();
29+
get(__DIR__ . "/../../resources/init.php");
30+
// now we should be new user
31+
$this->assertEquals("foobar", $USER->getUID());
32+
try {
3833
post(
3934
__DIR__ . "/../../resources/templates/header.php",
4035
["form_name" => "clearView"],
4136
);
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-
}
37+
} catch (PhpUnitNoDieException) {}
38+
// redirect means that php process dies and user's browser will initiate a new one
39+
// this makes `require_once autoload.php` run again and init.php changes $USER
40+
session_write_close();
41+
get(__DIR__ . "/../../resources/init.php");
42+
// now we should be back to original user
43+
$this->assertEquals($originalUid, $USER->getUID());
5144
}
5245
}

0 commit comments

Comments
 (0)