1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2023 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 <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Http\RequestHandlers; 21 22use Fig\Http\Message\StatusCodeInterface; 23use Fisharebest\Webtrees\Auth; 24use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException; 25use Fisharebest\Webtrees\Services\UserService; 26use Fisharebest\Webtrees\Session; 27use Fisharebest\Webtrees\TestCase; 28use Fisharebest\Webtrees\User; 29use PHPUnit\Framework\Attributes\CoversClass; 30 31#[CoversClass(Masquerade::class)] 32class MasqueradeTest extends TestCase 33{ 34 protected static bool $uses_database = true; 35 36 public function testMasqueradeAsUser(): void 37 { 38 $user1 = $this->createMock(User::class); 39 $user1->method('id')->willReturn(1); 40 41 $user2 = $this->createMock(User::class); 42 $user2->method('id')->willReturn(2); 43 44 $user_service = $this->createMock(UserService::class); 45 $user_service->expects(self::once())->method('find')->willReturn($user2); 46 47 $request = self::createRequest() 48 ->withAttribute('user', $user1) 49 ->withAttribute('user_id', $user2->id()); 50 51 $handler = new Masquerade($user_service); 52 $response = $handler->handle($request); 53 54 self::assertSame(StatusCodeInterface::STATUS_NO_CONTENT, $response->getStatusCode()); 55 self::assertSame($user2->id(), Auth::id()); 56 self::assertSame('1', Session::get('masquerade')); 57 } 58 59 public function testCannotMasqueradeAsSelf(): void 60 { 61 $user = $this->createMock(User::class); 62 $user->method('id')->willReturn(1); 63 64 $user_service = $this->createMock(UserService::class); 65 $user_service->expects(self::once())->method('find')->willReturn($user); 66 67 $request = self::createRequest() 68 ->withAttribute('user', $user) 69 ->withAttribute('user_id', $user->id()); 70 71 $handler = new Masquerade($user_service); 72 $response = $handler->handle($request); 73 74 self::assertSame(StatusCodeInterface::STATUS_NO_CONTENT, $response->getStatusCode()); 75 self::assertNull(Session::get('masquerade')); 76 } 77 78 public function testMasqueradeAsNonExistingUser(): void 79 { 80 $this->expectException(HttpNotFoundException::class); 81 $this->expectExceptionMessage('User ID 2 not found'); 82 83 $user = $this->createMock(User::class); 84 $user->method('id')->willReturn(1); 85 86 $user_service = $this->createMock(UserService::class); 87 $user_service->expects(self::once())->method('find')->willReturn(null); 88 89 $request = self::createRequest() 90 ->withAttribute('user', $user) 91 ->withAttribute('user_id', 2); 92 93 $handler = new Masquerade($user_service); 94 $handler->handle($request); 95 } 96} 97