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\Tree; 24use Fisharebest\Webtrees\User; 25use Psr\Http\Server\RequestHandlerInterface; 26 27use function response; 28 29/** 30 * Test the AuthModerator middleware. 31 * 32 * @covers \Fisharebest\Webtrees\Http\Middleware\AuthModerator 33 */ 34class AuthModeratorTest 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('0'); 46 47 $tree = $this->createMock(Tree::class); 48 $tree->method('getUserPreference')->with($user, 'canedit')->willReturn('accept'); 49 50 $request = self::createRequest()->withAttribute('tree', $tree)->withAttribute('user', $user); 51 $middleware = new AuthModerator(); 52 $response = $middleware->process($request, $handler); 53 54 $this->assertSame(self::STATUS_OK, $response->getStatusCode()); 55 $this->assertSame('lorem ipsum', (string) $response->getBody()); 56 } 57 58 /** 59 * @expectedException \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException 60 * @return void 61 */ 62 public function testNotAllowed(): void 63 { 64 $handler = $this->createMock(RequestHandlerInterface::class); 65 $handler->method('handle')->willReturn(response('lorem ipsum')); 66 67 $user = $this->createMock(User::class); 68 $user->method('getPreference')->with('canadmin')->willReturn('0'); 69 70 $tree = $this->createMock(Tree::class); 71 $tree->method('getUserPreference')->with($user, 'canedit')->willReturn('edit'); 72 73 $request = self::createRequest()->withAttribute('tree', $tree)->withAttribute('user', $user); 74 $middleware = new AuthModerator(); 75 $middleware->process($request, $handler); 76 } 77 78 /** 79 * @return void 80 */ 81 public function testNotLoggedIn(): void 82 { 83 $handler = $this->createMock(RequestHandlerInterface::class); 84 $handler->method('handle')->willReturn(response('lorem ipsum')); 85 86 $tree = $this->createMock(Tree::class); 87 88 $request = self::createRequest()->withAttribute('tree', $tree)->withAttribute('user', new GuestUser()); 89 $middleware = new AuthModerator(); 90 $response = $middleware->process($request, $handler); 91 92 $this->assertSame(self::STATUS_FOUND, $response->getStatusCode()); 93 } 94} 95