103f10823SGreg Roach<?php 203f10823SGreg Roach 303f10823SGreg Roach/** 403f10823SGreg Roach * webtrees: online genealogy 503f10823SGreg Roach * Copyright (C) 2019 webtrees development team 603f10823SGreg Roach * This program is free software: you can redistribute it and/or modify 703f10823SGreg Roach * it under the terms of the GNU General Public License as published by 803f10823SGreg Roach * the Free Software Foundation, either version 3 of the License, or 903f10823SGreg Roach * (at your option) any later version. 1003f10823SGreg Roach * This program is distributed in the hope that it will be useful, 1103f10823SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 1203f10823SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1303f10823SGreg Roach * GNU General Public License for more details. 1403f10823SGreg Roach * You should have received a copy of the GNU General Public License 1503f10823SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 1603f10823SGreg Roach */ 1703f10823SGreg Roachdeclare(strict_types=1); 1803f10823SGreg Roach 1903f10823SGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware; 2003f10823SGreg Roach 21*71378461SGreg Roachuse Fig\Http\Message\StatusCodeInterface; 2203f10823SGreg Roachuse Fisharebest\Webtrees\GuestUser; 2303f10823SGreg Roachuse Fisharebest\Webtrees\TestCase; 2403f10823SGreg Roachuse Fisharebest\Webtrees\User; 2503f10823SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 2603f10823SGreg Roachuse Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; 2703f10823SGreg Roach 2803f10823SGreg Roachuse function response; 2903f10823SGreg Roach 3003f10823SGreg Roach/** 3103f10823SGreg Roach * Test the AuthAdministrator middleware. 3203f10823SGreg Roach * 3303f10823SGreg Roach * @covers \Fisharebest\Webtrees\Http\Middleware\AuthAdministrator 3403f10823SGreg Roach */ 3503f10823SGreg Roachclass AuthAdministratorTest extends TestCase 3603f10823SGreg Roach{ 3703f10823SGreg Roach /** 3803f10823SGreg Roach * @return void 3903f10823SGreg Roach */ 4003f10823SGreg Roach public function testAllowed(): void 4103f10823SGreg Roach { 4203f10823SGreg Roach $handler = $this->createMock(RequestHandlerInterface::class); 4303f10823SGreg Roach $handler->method('handle')->willReturn(response('lorem ipsum')); 4403f10823SGreg Roach 4503f10823SGreg Roach $user = $this->createMock(User::class); 4603f10823SGreg Roach $user->method('getPreference')->with('canadmin')->willReturn('1'); 4703f10823SGreg Roach 4803f10823SGreg Roach $request = self::createRequest()->withAttribute('user', $user); 4903f10823SGreg Roach $middleware = new AuthAdministrator(); 5003f10823SGreg Roach $response = $middleware->process($request, $handler); 5103f10823SGreg Roach 52*71378461SGreg Roach $this->assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode()); 5303f10823SGreg Roach $this->assertSame('lorem ipsum', (string) $response->getBody()); 5403f10823SGreg Roach } 5503f10823SGreg Roach 5603f10823SGreg Roach /** 5703f10823SGreg Roach * @expectedException \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException 5803f10823SGreg Roach * @return void 5903f10823SGreg Roach */ 6003f10823SGreg Roach public function testNotAllowed(): void 6103f10823SGreg Roach { 6203f10823SGreg Roach $handler = $this->createMock(RequestHandlerInterface::class); 6303f10823SGreg Roach $handler->method('handle')->willReturn(response('lorem ipsum')); 6403f10823SGreg Roach 6503f10823SGreg Roach $user = $this->createMock(User::class); 6603f10823SGreg Roach $user->method('getPreference')->with('canadmin')->willReturn(''); 6703f10823SGreg Roach 6803f10823SGreg Roach $request = self::createRequest()->withAttribute('user', $user); 6903f10823SGreg Roach $middleware = new AuthAdministrator(); 7003f10823SGreg Roach $middleware->process($request, $handler); 7103f10823SGreg Roach } 7203f10823SGreg Roach 7303f10823SGreg Roach /** 7403f10823SGreg Roach * @return void 7503f10823SGreg Roach */ 7603f10823SGreg Roach public function testNotLoggedIn(): void 7703f10823SGreg Roach { 7803f10823SGreg Roach $handler = $this->createMock(RequestHandlerInterface::class); 7903f10823SGreg Roach $handler->method('handle')->willReturn(response('lorem ipsum')); 8003f10823SGreg Roach 8103f10823SGreg Roach $request = self::createRequest()->withAttribute('user', new GuestUser()); 8203f10823SGreg Roach $middleware = new AuthAdministrator(); 8303f10823SGreg Roach $response = $middleware->process($request, $handler); 8403f10823SGreg Roach 85*71378461SGreg Roach $this->assertSame(StatusCodeInterface::STATUS_FOUND, $response->getStatusCode()); 8603f10823SGreg Roach } 8703f10823SGreg Roach} 88