. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Http\Middleware; use Fisharebest\Webtrees\GuestUser; use Fisharebest\Webtrees\TestCase; use Fisharebest\Webtrees\Tree; use Fisharebest\Webtrees\User; use Psr\Http\Server\RequestHandlerInterface; use function response; /** * Test the AuthManager middleware. * * @covers \Fisharebest\Webtrees\Http\Middleware\AuthManager */ class AuthManagerTest 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('0'); $tree = $this->createMock(Tree::class); $tree->method('getUserPreference')->with($user, 'canedit')->willReturn('admin'); $request = self::createRequest()->withAttribute('tree', $tree)->withAttribute('user', $user); $middleware = new AuthManager(); $response = $middleware->process($request, $handler); $this->assertSame(self::STATUS_OK, $response->getStatusCode()); $this->assertSame('lorem ipsum', (string) $response->getBody()); } /** * @expectedException \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException * @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('0'); $tree = $this->createMock(Tree::class); $tree->method('getUserPreference')->with($user, 'canedit')->willReturn('accept'); $request = self::createRequest()->withAttribute('tree', $tree)->withAttribute('user', $user); $middleware = new AuthManager(); $middleware->process($request, $handler); } /** * @return void */ public function testNotLoggedIn(): void { $handler = $this->createMock(RequestHandlerInterface::class); $handler->method('handle')->willReturn(response('lorem ipsum')); $tree = $this->createMock(Tree::class); $request = self::createRequest()->withAttribute('tree', $tree)->withAttribute('user', new GuestUser()); $middleware = new AuthManager(); $response = $middleware->process($request, $handler); $this->assertSame(self::STATUS_FOUND, $response->getStatusCode()); } }