|
| 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 | +} |
0 commit comments