xref: /webtrees/tests/app/Http/RequestHandlers/MasqueradeTest.php (revision 202c018b592d5a516e4a465dc6dc515f3be37399)
1040e7dbaSGreg Roach<?php
2040e7dbaSGreg Roach
3040e7dbaSGreg Roach/**
4040e7dbaSGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
6040e7dbaSGreg Roach * This program is free software: you can redistribute it and/or modify
7040e7dbaSGreg Roach * it under the terms of the GNU General Public License as published by
8040e7dbaSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9040e7dbaSGreg Roach * (at your option) any later version.
10040e7dbaSGreg Roach * This program is distributed in the hope that it will be useful,
11040e7dbaSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12040e7dbaSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13040e7dbaSGreg Roach * GNU General Public License for more details.
14040e7dbaSGreg 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/>.
16040e7dbaSGreg Roach */
17040e7dbaSGreg Roach
18040e7dbaSGreg Roachdeclare(strict_types=1);
19040e7dbaSGreg Roach
20040e7dbaSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21040e7dbaSGreg Roach
22040e7dbaSGreg Roachuse Fig\Http\Message\StatusCodeInterface;
23040e7dbaSGreg Roachuse Fisharebest\Webtrees\Auth;
2481b729d3SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
25040e7dbaSGreg Roachuse Fisharebest\Webtrees\Services\UserService;
26040e7dbaSGreg Roachuse Fisharebest\Webtrees\Session;
27040e7dbaSGreg Roachuse Fisharebest\Webtrees\TestCase;
28040e7dbaSGreg Roachuse Fisharebest\Webtrees\User;
29*202c018bSGreg Roachuse PHPUnit\Framework\Attributes\CoversClass;
30040e7dbaSGreg Roach
31*202c018bSGreg Roach#[CoversClass(Masquerade::class)]
32040e7dbaSGreg Roachclass MasqueradeTest extends TestCase
33040e7dbaSGreg Roach{
34a26ec5edSGreg Roach    protected static bool $uses_database = true;
35a26ec5edSGreg Roach
36040e7dbaSGreg Roach    public function testMasqueradeAsUser(): void
37040e7dbaSGreg Roach    {
38cd94ca66SGreg Roach        $user1 = $this->createMock(User::class);
39040e7dbaSGreg Roach        $user1->method('id')->willReturn(1);
40040e7dbaSGreg Roach
41cd94ca66SGreg Roach        $user2 = $this->createMock(User::class);
42040e7dbaSGreg Roach        $user2->method('id')->willReturn(2);
43040e7dbaSGreg Roach
44cd94ca66SGreg Roach        $user_service = $this->createMock(UserService::class);
455e933c21SGreg Roach        $user_service->expects(self::once())->method('find')->willReturn($user2);
46040e7dbaSGreg Roach
47040e7dbaSGreg Roach        $request = self::createRequest()
48040e7dbaSGreg Roach            ->withAttribute('user', $user1)
49040e7dbaSGreg Roach            ->withAttribute('user_id', $user2->id());
50040e7dbaSGreg Roach
51040e7dbaSGreg Roach        $handler  = new Masquerade($user_service);
52040e7dbaSGreg Roach        $response = $handler->handle($request);
53040e7dbaSGreg Roach
54040e7dbaSGreg Roach        self::assertSame(StatusCodeInterface::STATUS_NO_CONTENT, $response->getStatusCode());
55040e7dbaSGreg Roach        self::assertSame($user2->id(), Auth::id());
56040e7dbaSGreg Roach        self::assertSame('1', Session::get('masquerade'));
57040e7dbaSGreg Roach    }
58040e7dbaSGreg Roach
59040e7dbaSGreg Roach    public function testCannotMasqueradeAsSelf(): void
60040e7dbaSGreg Roach    {
61cd94ca66SGreg Roach        $user = $this->createMock(User::class);
62040e7dbaSGreg Roach        $user->method('id')->willReturn(1);
63040e7dbaSGreg Roach
64cd94ca66SGreg Roach        $user_service = $this->createMock(UserService::class);
655e933c21SGreg Roach        $user_service->expects(self::once())->method('find')->willReturn($user);
66040e7dbaSGreg Roach
67040e7dbaSGreg Roach        $request = self::createRequest()
68040e7dbaSGreg Roach            ->withAttribute('user', $user)
69040e7dbaSGreg Roach            ->withAttribute('user_id', $user->id());
70040e7dbaSGreg Roach
71040e7dbaSGreg Roach        $handler  = new Masquerade($user_service);
72040e7dbaSGreg Roach        $response = $handler->handle($request);
73040e7dbaSGreg Roach
74040e7dbaSGreg Roach        self::assertSame(StatusCodeInterface::STATUS_NO_CONTENT, $response->getStatusCode());
75040e7dbaSGreg Roach        self::assertNull(Session::get('masquerade'));
76040e7dbaSGreg Roach    }
77040e7dbaSGreg Roach
78040e7dbaSGreg Roach    public function testMasqueradeAsNonExistingUser(): void
79040e7dbaSGreg Roach    {
80d501c45dSGreg Roach        $this->expectException(HttpNotFoundException::class);
81040e7dbaSGreg Roach        $this->expectExceptionMessage('User ID 2 not found');
82040e7dbaSGreg Roach
83cd94ca66SGreg Roach        $user = $this->createMock(User::class);
84040e7dbaSGreg Roach        $user->method('id')->willReturn(1);
85040e7dbaSGreg Roach
86cd94ca66SGreg Roach        $user_service = $this->createMock(UserService::class);
875e933c21SGreg Roach        $user_service->expects(self::once())->method('find')->willReturn(null);
88040e7dbaSGreg Roach
89040e7dbaSGreg Roach        $request = self::createRequest()
90040e7dbaSGreg Roach            ->withAttribute('user', $user)
91040e7dbaSGreg Roach            ->withAttribute('user_id', 2);
92040e7dbaSGreg Roach
93040e7dbaSGreg Roach        $handler = new Masquerade($user_service);
94040e7dbaSGreg Roach        $handler->handle($request);
95040e7dbaSGreg Roach    }
96040e7dbaSGreg Roach}
97