. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Http\Middleware; use Fig\Http\Message\StatusCodeInterface; use Fisharebest\Webtrees\GuestUser; use Fisharebest\Webtrees\TestCase; use Fisharebest\Webtrees\User; use Psr\Http\Server\RequestHandlerInterface; use function response; /** * Test the AuthAdministrator middleware. * * @covers \Fisharebest\Webtrees\Http\Middleware\AuthAdministrator */ class AuthAdministratorTest extends TestCase { /** * @return void */ 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('canadmin')->willReturn('1'); $request = self::createRequest()->withAttribute('user', $user); $middleware = new AuthAdministrator(); $response = $middleware->process($request, $handler); $this->assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode()); $this->assertSame('lorem ipsum', (string) $response->getBody()); } /** * @return void */ public function testNotAllowed(): void { $handler = $this->createMock(RequestHandlerInterface::class); $handler->method('handle')->willReturn(response('lorem ipsum')); $user = $this->createMock(User::class); $user->method('getPreference')->with('canadmin')->willReturn(''); $request = self::createRequest()->withAttribute('user', $user); $middleware = new AuthAdministrator(); $response = $middleware->process($request, $handler); $this->assertSame(StatusCodeInterface::STATUS_FOUND, $response->getStatusCode()); } /** * @return void */ 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); $this->assertSame(StatusCodeInterface::STATUS_FOUND, $response->getStatusCode()); } }