xref: /webtrees/tests/app/Http/RequestHandlers/MasqueradeTest.php (revision a26ec5ede3c2ecdb45b783755ede967935aaecba)
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;
29040e7dbaSGreg Roach
30040e7dbaSGreg Roach/**
31040e7dbaSGreg Roach * @covers \Fisharebest\Webtrees\Http\RequestHandlers\Masquerade
32040e7dbaSGreg Roach */
33040e7dbaSGreg Roachclass MasqueradeTest extends TestCase
34040e7dbaSGreg Roach{
35*a26ec5edSGreg Roach    protected static bool $uses_database = true;
36*a26ec5edSGreg Roach
37040e7dbaSGreg Roach    public function testMasqueradeAsUser(): void
38040e7dbaSGreg Roach    {
39cd94ca66SGreg Roach        $user1 = $this->createMock(User::class);
40040e7dbaSGreg Roach        $user1->method('id')->willReturn(1);
41040e7dbaSGreg Roach
42cd94ca66SGreg Roach        $user2 = $this->createMock(User::class);
43040e7dbaSGreg Roach        $user2->method('id')->willReturn(2);
44040e7dbaSGreg Roach
45cd94ca66SGreg Roach        $user_service = $this->createMock(UserService::class);
465e933c21SGreg Roach        $user_service->expects(self::once())->method('find')->willReturn($user2);
47040e7dbaSGreg Roach
48040e7dbaSGreg Roach        $request = self::createRequest()
49040e7dbaSGreg Roach            ->withAttribute('user', $user1)
50040e7dbaSGreg Roach            ->withAttribute('user_id', $user2->id());
51040e7dbaSGreg Roach
52040e7dbaSGreg Roach        $handler  = new Masquerade($user_service);
53040e7dbaSGreg Roach        $response = $handler->handle($request);
54040e7dbaSGreg Roach
55040e7dbaSGreg Roach        self::assertSame(StatusCodeInterface::STATUS_NO_CONTENT, $response->getStatusCode());
56040e7dbaSGreg Roach        self::assertSame($user2->id(), Auth::id());
57040e7dbaSGreg Roach        self::assertSame('1', Session::get('masquerade'));
58040e7dbaSGreg Roach    }
59040e7dbaSGreg Roach
60040e7dbaSGreg Roach    public function testCannotMasqueradeAsSelf(): void
61040e7dbaSGreg Roach    {
62cd94ca66SGreg Roach        $user = $this->createMock(User::class);
63040e7dbaSGreg Roach        $user->method('id')->willReturn(1);
64040e7dbaSGreg Roach
65cd94ca66SGreg Roach        $user_service = $this->createMock(UserService::class);
665e933c21SGreg Roach        $user_service->expects(self::once())->method('find')->willReturn($user);
67040e7dbaSGreg Roach
68040e7dbaSGreg Roach        $request = self::createRequest()
69040e7dbaSGreg Roach            ->withAttribute('user', $user)
70040e7dbaSGreg Roach            ->withAttribute('user_id', $user->id());
71040e7dbaSGreg Roach
72040e7dbaSGreg Roach        $handler  = new Masquerade($user_service);
73040e7dbaSGreg Roach        $response = $handler->handle($request);
74040e7dbaSGreg Roach
75040e7dbaSGreg Roach        self::assertSame(StatusCodeInterface::STATUS_NO_CONTENT, $response->getStatusCode());
76040e7dbaSGreg Roach        self::assertNull(Session::get('masquerade'));
77040e7dbaSGreg Roach    }
78040e7dbaSGreg Roach
79040e7dbaSGreg Roach    public function testMasqueradeAsNonExistingUser(): void
80040e7dbaSGreg Roach    {
81d501c45dSGreg Roach        $this->expectException(HttpNotFoundException::class);
82040e7dbaSGreg Roach        $this->expectExceptionMessage('User ID 2 not found');
83040e7dbaSGreg Roach
84cd94ca66SGreg Roach        $user = $this->createMock(User::class);
85040e7dbaSGreg Roach        $user->method('id')->willReturn(1);
86040e7dbaSGreg Roach
87cd94ca66SGreg Roach        $user_service = $this->createMock(UserService::class);
885e933c21SGreg Roach        $user_service->expects(self::once())->method('find')->willReturn(null);
89040e7dbaSGreg Roach
90040e7dbaSGreg Roach        $request = self::createRequest()
91040e7dbaSGreg Roach            ->withAttribute('user', $user)
92040e7dbaSGreg Roach            ->withAttribute('user_id', 2);
93040e7dbaSGreg Roach
94040e7dbaSGreg Roach        $handler = new Masquerade($user_service);
95040e7dbaSGreg Roach        $handler->handle($request);
96040e7dbaSGreg Roach    }
97040e7dbaSGreg Roach}
98