101202f80SGreg Roach<?php 201202f80SGreg Roach 301202f80SGreg Roach/** 401202f80SGreg Roach * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 601202f80SGreg Roach * This program is free software: you can redistribute it and/or modify 701202f80SGreg Roach * it under the terms of the GNU General Public License as published by 801202f80SGreg Roach * the Free Software Foundation, either version 3 of the License, or 901202f80SGreg Roach * (at your option) any later version. 1001202f80SGreg Roach * This program is distributed in the hope that it will be useful, 1101202f80SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 1201202f80SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1301202f80SGreg Roach * GNU General Public License for more details. 1401202f80SGreg Roach * You should have received a copy of the GNU General Public License 1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 1601202f80SGreg Roach */ 17fcfa147eSGreg Roach 1801202f80SGreg Roachdeclare(strict_types=1); 1901202f80SGreg Roach 2001202f80SGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware; 2101202f80SGreg Roach 2271378461SGreg Roachuse Fig\Http\Message\StatusCodeInterface; 231fe542e9SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 2401202f80SGreg Roachuse Fisharebest\Webtrees\GuestUser; 2581b729d3SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpAccessDeniedException; 2601202f80SGreg Roachuse Fisharebest\Webtrees\TestCase; 2701202f80SGreg Roachuse Fisharebest\Webtrees\Tree; 2801202f80SGreg Roachuse Fisharebest\Webtrees\User; 29*202c018bSGreg Roachuse PHPUnit\Framework\Attributes\CoversClass; 3001202f80SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 3101202f80SGreg Roach 3201202f80SGreg Roachuse function response; 3301202f80SGreg Roach 34*202c018bSGreg Roach#[CoversClass(AuthMember::class)] 3501202f80SGreg Roachclass AuthMemberTest extends TestCase 3601202f80SGreg Roach{ 3701202f80SGreg Roach public function testAllowed(): void 3801202f80SGreg Roach { 39cd94ca66SGreg Roach $handler = $this->createMock(RequestHandlerInterface::class); 4001202f80SGreg Roach $handler->method('handle')->willReturn(response('lorem ipsum')); 4101202f80SGreg Roach 42cd94ca66SGreg Roach $user = $this->createMock(User::class); 431fe542e9SGreg Roach $user->method('getPreference')->with(UserInterface::PREF_IS_ADMINISTRATOR)->willReturn(''); 4401202f80SGreg Roach 45cd94ca66SGreg Roach $tree = $this->createMock(Tree::class); 461fe542e9SGreg Roach $tree->method('getUserPreference')->with($user, UserInterface::PREF_TREE_ROLE)->willReturn(UserInterface::ROLE_MEMBER); 4701202f80SGreg Roach 4801202f80SGreg Roach $request = self::createRequest()->withAttribute('tree', $tree)->withAttribute('user', $user); 4901202f80SGreg Roach $middleware = new AuthMember(); 5001202f80SGreg Roach $response = $middleware->process($request, $handler); 5101202f80SGreg Roach 525e933c21SGreg Roach self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode()); 535e933c21SGreg Roach self::assertSame('lorem ipsum', (string) $response->getBody()); 5401202f80SGreg Roach } 5501202f80SGreg Roach 5601202f80SGreg Roach public function testNotAllowed(): void 5701202f80SGreg Roach { 58d501c45dSGreg Roach $this->expectException(HttpAccessDeniedException::class); 595fb051e9SGreg Roach $this->expectExceptionMessage('You do not have permission to view this page.'); 605fb051e9SGreg Roach 61cd94ca66SGreg Roach $handler = $this->createMock(RequestHandlerInterface::class); 6201202f80SGreg Roach $handler->method('handle')->willReturn(response('lorem ipsum')); 6301202f80SGreg Roach 64cd94ca66SGreg Roach $user = $this->createMock(User::class); 651fe542e9SGreg Roach $user->method('getPreference')->with(UserInterface::PREF_IS_ADMINISTRATOR)->willReturn(''); 6601202f80SGreg Roach 67cd94ca66SGreg Roach $tree = $this->createMock(Tree::class); 681fe542e9SGreg Roach $tree->method('getUserPreference')->with($user, UserInterface::PREF_TREE_ROLE)->willReturn(''); 6901202f80SGreg Roach 7001202f80SGreg Roach $request = self::createRequest()->withAttribute('tree', $tree)->withAttribute('user', $user); 7101202f80SGreg Roach $middleware = new AuthMember(); 720c0910bfSGreg Roach 735fb051e9SGreg Roach $middleware->process($request, $handler); 7401202f80SGreg Roach } 7501202f80SGreg Roach 7601202f80SGreg Roach public function testNotLoggedIn(): void 7701202f80SGreg Roach { 78cd94ca66SGreg Roach $handler = $this->createMock(RequestHandlerInterface::class); 7901202f80SGreg Roach $handler->method('handle')->willReturn(response('lorem ipsum')); 8001202f80SGreg Roach 81cd94ca66SGreg Roach $tree = $this->createMock(Tree::class); 8201202f80SGreg Roach 8301202f80SGreg Roach $request = self::createRequest()->withAttribute('tree', $tree)->withAttribute('user', new GuestUser()); 8401202f80SGreg Roach $middleware = new AuthMember(); 8501202f80SGreg Roach $response = $middleware->process($request, $handler); 8601202f80SGreg Roach 875e933c21SGreg Roach self::assertSame(StatusCodeInterface::STATUS_FOUND, $response->getStatusCode()); 8801202f80SGreg Roach } 8901202f80SGreg Roach} 90