. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Http\Middleware; use Fig\Http\Message\StatusCodeInterface; use Fisharebest\Webtrees\Contracts\UserInterface; use Fisharebest\Webtrees\GuestUser; use Fisharebest\Webtrees\Http\Exceptions\HttpAccessDeniedException; use Fisharebest\Webtrees\TestCase; use Fisharebest\Webtrees\User; use PHPUnit\Framework\Attributes\CoversClass; use Psr\Http\Server\RequestHandlerInterface; use function response; #[CoversClass(AuthAdministrator::class)] class AuthAdministratorTest extends TestCase { public function testAllowed(): void { $handler = $this->createMock(RequestHandlerInterface::class); $handler->method('handle')->willReturn(response('lorem ipsum')); $user = $this->createMock(User::class); $user->method('getPreference')->with(UserInterface::PREF_IS_ADMINISTRATOR)->willReturn('1'); $request = self::createRequest()->withAttribute('user', $user); $middleware = new AuthAdministrator(); $response = $middleware->process($request, $handler); self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode()); self::assertSame('lorem ipsum', (string) $response->getBody()); } public function testNotAllowed(): void { $this->expectException(HttpAccessDeniedException::class); $this->expectExceptionMessage('You do not have permission to view this page.'); $handler = $this->createMock(RequestHandlerInterface::class); $handler->method('handle')->willReturn(response('lorem ipsum')); $user = $this->createMock(User::class); $user->method('getPreference')->with(UserInterface::PREF_IS_ADMINISTRATOR)->willReturn(''); $request = self::createRequest()->withAttribute('user', $user); $middleware = new AuthAdministrator(); $middleware->process($request, $handler); } public function testNotLoggedIn(): void { $handler = $this->createMock(RequestHandlerInterface::class); $handler->method('handle')->willReturn(response('lorem ipsum')); $request = self::createRequest()->withAttribute('user', new GuestUser()); $middleware = new AuthAdministrator(); $response = $middleware->process($request, $handler); self::assertSame(StatusCodeInterface::STATUS_FOUND, $response->getStatusCode()); } }