1*040e7dbaSGreg Roach<?php 2*040e7dbaSGreg Roach 3*040e7dbaSGreg Roach/** 4*040e7dbaSGreg Roach * webtrees: online genealogy 5*040e7dbaSGreg Roach * Copyright (C) 2019 webtrees development team 6*040e7dbaSGreg Roach * This program is free software: you can redistribute it and/or modify 7*040e7dbaSGreg Roach * it under the terms of the GNU General Public License as published by 8*040e7dbaSGreg Roach * the Free Software Foundation, either version 3 of the License, or 9*040e7dbaSGreg Roach * (at your option) any later version. 10*040e7dbaSGreg Roach * This program is distributed in the hope that it will be useful, 11*040e7dbaSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12*040e7dbaSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13*040e7dbaSGreg Roach * GNU General Public License for more details. 14*040e7dbaSGreg Roach * You should have received a copy of the GNU General Public License 15*040e7dbaSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 16*040e7dbaSGreg Roach */ 17*040e7dbaSGreg Roach 18*040e7dbaSGreg Roachdeclare(strict_types=1); 19*040e7dbaSGreg Roach 20*040e7dbaSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers; 21*040e7dbaSGreg Roach 22*040e7dbaSGreg Roachuse Fig\Http\Message\StatusCodeInterface; 23*040e7dbaSGreg Roachuse Fisharebest\Webtrees\Auth; 24*040e7dbaSGreg Roachuse Fisharebest\Webtrees\Services\UserService; 25*040e7dbaSGreg Roachuse Fisharebest\Webtrees\Session; 26*040e7dbaSGreg Roachuse Fisharebest\Webtrees\TestCase; 27*040e7dbaSGreg Roachuse Fisharebest\Webtrees\User; 28*040e7dbaSGreg Roachuse Symfony\Component\HttpKernel\Exception\NotFoundHttpException; 29*040e7dbaSGreg Roach 30*040e7dbaSGreg Roach/** 31*040e7dbaSGreg Roach * @covers \Fisharebest\Webtrees\Http\RequestHandlers\Masquerade 32*040e7dbaSGreg Roach */ 33*040e7dbaSGreg Roachclass MasqueradeTest extends TestCase 34*040e7dbaSGreg Roach{ 35*040e7dbaSGreg Roach /** 36*040e7dbaSGreg Roach * @return void 37*040e7dbaSGreg Roach */ 38*040e7dbaSGreg Roach public function testMasqueradeAsUser(): void 39*040e7dbaSGreg Roach { 40*040e7dbaSGreg Roach $user1 = $this->createMock(User::class); 41*040e7dbaSGreg Roach $user1->method('id')->willReturn(1); 42*040e7dbaSGreg Roach 43*040e7dbaSGreg Roach $user2 = $this->createMock(User::class); 44*040e7dbaSGreg Roach $user2->method('id')->willReturn(2); 45*040e7dbaSGreg Roach 46*040e7dbaSGreg Roach $user_service = $this->createMock(UserService::class); 47*040e7dbaSGreg Roach $user_service->expects($this->once())->method('find')->willReturn($user2); 48*040e7dbaSGreg Roach 49*040e7dbaSGreg Roach $request = self::createRequest() 50*040e7dbaSGreg Roach ->withAttribute('user', $user1) 51*040e7dbaSGreg Roach ->withAttribute('user_id', $user2->id()); 52*040e7dbaSGreg Roach 53*040e7dbaSGreg Roach $handler = new Masquerade($user_service); 54*040e7dbaSGreg Roach $response = $handler->handle($request); 55*040e7dbaSGreg Roach 56*040e7dbaSGreg Roach self::assertSame(StatusCodeInterface::STATUS_NO_CONTENT, $response->getStatusCode()); 57*040e7dbaSGreg Roach self::assertSame($user2->id(), Auth::id()); 58*040e7dbaSGreg Roach self::assertSame('1', Session::get('masquerade')); 59*040e7dbaSGreg Roach } 60*040e7dbaSGreg Roach 61*040e7dbaSGreg Roach /** 62*040e7dbaSGreg Roach * @return void 63*040e7dbaSGreg Roach */ 64*040e7dbaSGreg Roach public function testCannotMasqueradeAsSelf(): void 65*040e7dbaSGreg Roach { 66*040e7dbaSGreg Roach $user = $this->createMock(User::class); 67*040e7dbaSGreg Roach $user->method('id')->willReturn(1); 68*040e7dbaSGreg Roach 69*040e7dbaSGreg Roach $user_service = $this->createMock(UserService::class); 70*040e7dbaSGreg Roach $user_service->expects($this->once())->method('find')->willReturn($user); 71*040e7dbaSGreg Roach 72*040e7dbaSGreg Roach $request = self::createRequest() 73*040e7dbaSGreg Roach ->withAttribute('user', $user) 74*040e7dbaSGreg Roach ->withAttribute('user_id', $user->id()); 75*040e7dbaSGreg Roach 76*040e7dbaSGreg Roach $handler = new Masquerade($user_service); 77*040e7dbaSGreg Roach $response = $handler->handle($request); 78*040e7dbaSGreg Roach 79*040e7dbaSGreg Roach self::assertSame(StatusCodeInterface::STATUS_NO_CONTENT, $response->getStatusCode()); 80*040e7dbaSGreg Roach self::assertNull(Session::get('masquerade')); 81*040e7dbaSGreg Roach } 82*040e7dbaSGreg Roach 83*040e7dbaSGreg Roach /** 84*040e7dbaSGreg Roach * @return void 85*040e7dbaSGreg Roach */ 86*040e7dbaSGreg Roach public function testMasqueradeAsNonExistingUser(): void 87*040e7dbaSGreg Roach { 88*040e7dbaSGreg Roach $this->expectException(NotFoundHttpException::class); 89*040e7dbaSGreg Roach $this->expectExceptionMessage('User ID 2 not found'); 90*040e7dbaSGreg Roach 91*040e7dbaSGreg Roach $user = $this->createMock(User::class); 92*040e7dbaSGreg Roach $user->method('id')->willReturn(1); 93*040e7dbaSGreg Roach 94*040e7dbaSGreg Roach $user_service = $this->createMock(UserService::class); 95*040e7dbaSGreg Roach $user_service->expects($this->once())->method('find')->willReturn(null); 96*040e7dbaSGreg Roach 97*040e7dbaSGreg Roach $request = self::createRequest() 98*040e7dbaSGreg Roach ->withAttribute('user', $user) 99*040e7dbaSGreg Roach ->withAttribute('user_id', 2); 100*040e7dbaSGreg Roach 101*040e7dbaSGreg Roach $handler = new Masquerade($user_service); 102*040e7dbaSGreg Roach $handler->handle($request); 103*040e7dbaSGreg Roach } 104*040e7dbaSGreg Roach} 105