xref: /webtrees/tests/app/Http/RequestHandlers/MasqueradeTest.php (revision 2ebcf907ed34213f816592af04e6c160335d6311)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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    /**
36     * @return void
37     */
38    public function testMasqueradeAsUser(): void
39    {
40        $user1 = $this->createMock(User::class);
41        $user1->method('id')->willReturn(1);
42
43        $user2 = $this->createMock(User::class);
44        $user2->method('id')->willReturn(2);
45
46        $user_service = $this->createMock(UserService::class);
47        $user_service->expects(self::once())->method('find')->willReturn($user2);
48
49        $request = self::createRequest()
50            ->withAttribute('user', $user1)
51            ->withAttribute('user_id', $user2->id());
52
53        $handler  = new Masquerade($user_service);
54        $response = $handler->handle($request);
55
56        self::assertSame(StatusCodeInterface::STATUS_NO_CONTENT, $response->getStatusCode());
57        self::assertSame($user2->id(), Auth::id());
58        self::assertSame('1', Session::get('masquerade'));
59    }
60
61    /**
62     * @return void
63     */
64    public function testCannotMasqueradeAsSelf(): void
65    {
66        $user = $this->createMock(User::class);
67        $user->method('id')->willReturn(1);
68
69        $user_service = $this->createMock(UserService::class);
70        $user_service->expects(self::once())->method('find')->willReturn($user);
71
72        $request = self::createRequest()
73            ->withAttribute('user', $user)
74            ->withAttribute('user_id', $user->id());
75
76        $handler  = new Masquerade($user_service);
77        $response = $handler->handle($request);
78
79        self::assertSame(StatusCodeInterface::STATUS_NO_CONTENT, $response->getStatusCode());
80        self::assertNull(Session::get('masquerade'));
81    }
82
83    /**
84     * @return void
85     */
86    public function testMasqueradeAsNonExistingUser(): void
87    {
88        $this->expectException(HttpNotFoundException::class);
89        $this->expectExceptionMessage('User ID 2 not found');
90
91        $user = $this->createMock(User::class);
92        $user->method('id')->willReturn(1);
93
94        $user_service = $this->createMock(UserService::class);
95        $user_service->expects(self::once())->method('find')->willReturn(null);
96
97        $request = self::createRequest()
98            ->withAttribute('user', $user)
99            ->withAttribute('user_id', 2);
100
101        $handler = new Masquerade($user_service);
102        $handler->handle($request);
103    }
104}
105