xref: /webtrees/tests/app/Http/RequestHandlers/MasqueradeTest.php (revision 81b729d3a9a6e0a0e8b96285d1ad7955d2d0c659)
1040e7dbaSGreg Roach<?php
2040e7dbaSGreg Roach
3040e7dbaSGreg Roach/**
4040e7dbaSGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 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;
24*81b729d3SGreg 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{
35040e7dbaSGreg Roach    /**
36040e7dbaSGreg Roach     * @return void
37040e7dbaSGreg Roach     */
38040e7dbaSGreg Roach    public function testMasqueradeAsUser(): void
39040e7dbaSGreg Roach    {
40cd94ca66SGreg Roach        $user1 = $this->createMock(User::class);
41040e7dbaSGreg Roach        $user1->method('id')->willReturn(1);
42040e7dbaSGreg Roach
43cd94ca66SGreg Roach        $user2 = $this->createMock(User::class);
44040e7dbaSGreg Roach        $user2->method('id')->willReturn(2);
45040e7dbaSGreg Roach
46cd94ca66SGreg Roach        $user_service = $this->createMock(UserService::class);
475e933c21SGreg Roach        $user_service->expects(self::once())->method('find')->willReturn($user2);
48040e7dbaSGreg Roach
49040e7dbaSGreg Roach        $request = self::createRequest()
50040e7dbaSGreg Roach            ->withAttribute('user', $user1)
51040e7dbaSGreg Roach            ->withAttribute('user_id', $user2->id());
52040e7dbaSGreg Roach
53040e7dbaSGreg Roach        $handler  = new Masquerade($user_service);
54040e7dbaSGreg Roach        $response = $handler->handle($request);
55040e7dbaSGreg Roach
56040e7dbaSGreg Roach        self::assertSame(StatusCodeInterface::STATUS_NO_CONTENT, $response->getStatusCode());
57040e7dbaSGreg Roach        self::assertSame($user2->id(), Auth::id());
58040e7dbaSGreg Roach        self::assertSame('1', Session::get('masquerade'));
59040e7dbaSGreg Roach    }
60040e7dbaSGreg Roach
61040e7dbaSGreg Roach    /**
62040e7dbaSGreg Roach     * @return void
63040e7dbaSGreg Roach     */
64040e7dbaSGreg Roach    public function testCannotMasqueradeAsSelf(): void
65040e7dbaSGreg Roach    {
66cd94ca66SGreg Roach        $user = $this->createMock(User::class);
67040e7dbaSGreg Roach        $user->method('id')->willReturn(1);
68040e7dbaSGreg Roach
69cd94ca66SGreg Roach        $user_service = $this->createMock(UserService::class);
705e933c21SGreg Roach        $user_service->expects(self::once())->method('find')->willReturn($user);
71040e7dbaSGreg Roach
72040e7dbaSGreg Roach        $request = self::createRequest()
73040e7dbaSGreg Roach            ->withAttribute('user', $user)
74040e7dbaSGreg Roach            ->withAttribute('user_id', $user->id());
75040e7dbaSGreg Roach
76040e7dbaSGreg Roach        $handler  = new Masquerade($user_service);
77040e7dbaSGreg Roach        $response = $handler->handle($request);
78040e7dbaSGreg Roach
79040e7dbaSGreg Roach        self::assertSame(StatusCodeInterface::STATUS_NO_CONTENT, $response->getStatusCode());
80040e7dbaSGreg Roach        self::assertNull(Session::get('masquerade'));
81040e7dbaSGreg Roach    }
82040e7dbaSGreg Roach
83040e7dbaSGreg Roach    /**
84040e7dbaSGreg Roach     * @return void
85040e7dbaSGreg Roach     */
86040e7dbaSGreg Roach    public function testMasqueradeAsNonExistingUser(): void
87040e7dbaSGreg Roach    {
88d501c45dSGreg Roach        $this->expectException(HttpNotFoundException::class);
89040e7dbaSGreg Roach        $this->expectExceptionMessage('User ID 2 not found');
90040e7dbaSGreg Roach
91cd94ca66SGreg Roach        $user = $this->createMock(User::class);
92040e7dbaSGreg Roach        $user->method('id')->willReturn(1);
93040e7dbaSGreg Roach
94cd94ca66SGreg Roach        $user_service = $this->createMock(UserService::class);
955e933c21SGreg Roach        $user_service->expects(self::once())->method('find')->willReturn(null);
96040e7dbaSGreg Roach
97040e7dbaSGreg Roach        $request = self::createRequest()
98040e7dbaSGreg Roach            ->withAttribute('user', $user)
99040e7dbaSGreg Roach            ->withAttribute('user_id', 2);
100040e7dbaSGreg Roach
101040e7dbaSGreg Roach        $handler = new Masquerade($user_service);
102040e7dbaSGreg Roach        $handler->handle($request);
103040e7dbaSGreg Roach    }
104040e7dbaSGreg Roach}
105