1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2019 webtrees development team 6 * This program is free software: you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation, either version 3 of the License, or 9 * (at your option) any later version. 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * You should have received a copy of the GNU General Public License 15 * along with this program. If not, see <http://www.gnu.org/licenses/>. 16 */ 17declare(strict_types=1); 18 19namespace Fisharebest\Webtrees\Http\Middleware; 20 21use Fisharebest\Webtrees\GuestUser; 22use Fisharebest\Webtrees\TestCase; 23use Fisharebest\Webtrees\User; 24use Psr\Http\Server\RequestHandlerInterface; 25use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; 26 27use function response; 28 29/** 30 * Test the AuthAdministrator middleware. 31 * 32 * @covers \Fisharebest\Webtrees\Http\Middleware\AuthAdministrator 33 */ 34class AuthAdministratorTest extends TestCase 35{ 36 /** 37 * @return void 38 */ 39 public function testAllowed(): void 40 { 41 $handler = $this->createMock(RequestHandlerInterface::class); 42 $handler->method('handle')->willReturn(response('lorem ipsum')); 43 44 $user = $this->createMock(User::class); 45 $user->method('getPreference')->with('canadmin')->willReturn('1'); 46 47 $request = self::createRequest()->withAttribute('user', $user); 48 $middleware = new AuthAdministrator(); 49 $response = $middleware->process($request, $handler); 50 51 $this->assertSame(self::STATUS_OK, $response->getStatusCode()); 52 $this->assertSame('lorem ipsum', (string) $response->getBody()); 53 } 54 55 /** 56 * @expectedException \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException 57 * @return void 58 */ 59 public function testNotAllowed(): void 60 { 61 $handler = $this->createMock(RequestHandlerInterface::class); 62 $handler->method('handle')->willReturn(response('lorem ipsum')); 63 64 $user = $this->createMock(User::class); 65 $user->method('getPreference')->with('canadmin')->willReturn(''); 66 67 $request = self::createRequest()->withAttribute('user', $user); 68 $middleware = new AuthAdministrator(); 69 $middleware->process($request, $handler); 70 } 71 72 /** 73 * @return void 74 */ 75 public function testNotLoggedIn(): void 76 { 77 $handler = $this->createMock(RequestHandlerInterface::class); 78 $handler->method('handle')->willReturn(response('lorem ipsum')); 79 80 $request = self::createRequest()->withAttribute('user', new GuestUser()); 81 $middleware = new AuthAdministrator(); 82 $response = $middleware->process($request, $handler); 83 84 $this->assertSame(self::STATUS_FOUND, $response->getStatusCode()); 85 } 86} 87