xref: /webtrees/tests/app/Http/RequestHandlers/MasqueradeTest.php (revision 67497a53f705a1c2998fed00c3bba0fd1c7a86ed)
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;
29
30/**
31 * @covers \Fisharebest\Webtrees\Http\RequestHandlers\Masquerade
32 */
33class MasqueradeTest extends TestCase
34{
35    protected static bool $uses_database = true;
36
37    public function testMasqueradeAsUser(): void
38    {
39        $user1 = $this->createMock(User::class);
40        $user1->method('id')->willReturn(1);
41
42        $user2 = $this->createMock(User::class);
43        $user2->method('id')->willReturn(2);
44
45        $user_service = $this->createMock(UserService::class);
46        $user_service->expects(self::once())->method('find')->willReturn($user2);
47
48        $request = self::createRequest()
49            ->withAttribute('user', $user1)
50            ->withAttribute('user_id', $user2->id());
51
52        $handler  = new Masquerade($user_service);
53        $response = $handler->handle($request);
54
55        self::assertSame(StatusCodeInterface::STATUS_NO_CONTENT, $response->getStatusCode());
56        self::assertSame($user2->id(), Auth::id());
57        self::assertSame('1', Session::get('masquerade'));
58    }
59
60    public function testCannotMasqueradeAsSelf(): void
61    {
62        $user = $this->createMock(User::class);
63        $user->method('id')->willReturn(1);
64
65        $user_service = $this->createMock(UserService::class);
66        $user_service->expects(self::once())->method('find')->willReturn($user);
67
68        $request = self::createRequest()
69            ->withAttribute('user', $user)
70            ->withAttribute('user_id', $user->id());
71
72        $handler  = new Masquerade($user_service);
73        $response = $handler->handle($request);
74
75        self::assertSame(StatusCodeInterface::STATUS_NO_CONTENT, $response->getStatusCode());
76        self::assertNull(Session::get('masquerade'));
77    }
78
79    public function testMasqueradeAsNonExistingUser(): void
80    {
81        $this->expectException(HttpNotFoundException::class);
82        $this->expectExceptionMessage('User ID 2 not found');
83
84        $user = $this->createMock(User::class);
85        $user->method('id')->willReturn(1);
86
87        $user_service = $this->createMock(UserService::class);
88        $user_service->expects(self::once())->method('find')->willReturn(null);
89
90        $request = self::createRequest()
91            ->withAttribute('user', $user)
92            ->withAttribute('user_id', 2);
93
94        $handler = new Masquerade($user_service);
95        $handler->handle($request);
96    }
97}
98