|
1 | 1 | <?php |
2 | 2 |
|
3 | 3 | use UnityWebPortal\lib\UnitySite; |
4 | | - |
| 4 | +use UnityWebPortal\lib\exceptions\PhpUnitNoDieException; |
5 | 5 | use PHPUnit\Framework\TestCase; |
6 | 6 | use PHPUnit\Framework\Attributes\DataProvider; |
7 | | -use PHPUnit\Framework\MockObject\MockBuilder; |
8 | 7 |
|
9 | 8 | class ViewAsUserTest extends TestCase |
10 | 9 | { |
11 | 10 | public function testViewAsUser() |
12 | 11 | { |
13 | | - global $USER, $CONFIG, $SITE; |
| 12 | + global $USER; |
14 | 13 | switchUser(...getAdminUser()); |
15 | | - $this->assertEquals("user1_org1_test", $USER->getUID()); |
16 | 14 | $this->assertTrue($USER->isAdmin()); |
17 | | - $adminUid = $USER->getUID(); |
18 | | - $oldSite = $SITE; |
| 15 | + $originalUid = $USER->getUID(); |
19 | 16 | try { |
20 | | - $SITE = $this->createMock(UnitySite::class); |
21 | | - $SITE->method("redirect"); |
22 | 17 | post( |
23 | 18 | __DIR__ . "/../../webroot/admin/user-mgmt.php", |
24 | 19 | [ |
25 | 20 | "form_name" => "viewAsUser", |
26 | 21 | "uid" => "foobar", |
27 | 22 | ], |
28 | 23 | ); |
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 { |
38 | 33 | post( |
39 | 34 | __DIR__ . "/../../resources/templates/header.php", |
40 | 35 | ["form_name" => "clearView"], |
41 | 36 | ); |
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()); |
51 | 44 | } |
52 | 45 | } |
0 commit comments