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\Contracts\UserInterface; 24use Fisharebest\Webtrees\Exceptions\HttpAccessDeniedException; 25use Fisharebest\Webtrees\Exceptions\HttpNotFoundException; 26use Fisharebest\Webtrees\Services\UserService; 27use Fisharebest\Webtrees\TestCase; 28use Fisharebest\Webtrees\User; 29 30/** 31 * @covers \Fisharebest\Webtrees\Http\RequestHandlers\DeleteUser 32 */ 33class DeleteUserTest extends TestCase 34{ 35 protected static bool $uses_database = true; 36 37 /** 38 * @return void 39 */ 40 public function testDeleteUser(): void 41 { 42 $user = $this->createMock(User::class); 43 $user->method('id')->willReturn(1); 44 45 $user_service = $this->createMock(UserService::class); 46 $user_service->expects(self::once())->method('find')->willReturn($user); 47 48 $request = self::createRequest() 49 ->withAttribute('user_id', $user->id()); 50 $handler = new DeleteUser($user_service); 51 $response = $handler->handle($request); 52 53 self::assertSame(StatusCodeInterface::STATUS_NO_CONTENT, $response->getStatusCode()); 54 } 55 56 /** 57 * @return void 58 */ 59 public function testDeleteNonExistingUser(): void 60 { 61 $this->expectException(HttpNotFoundException::class); 62 $this->expectExceptionMessage('User ID 98765 not found'); 63 64 $user_service = $this->createMock(UserService::class); 65 $user_service->expects(self::once())->method('find')->willReturn(null); 66 67 $request = self::createRequest() 68 ->withAttribute('user_id', 98765); 69 $handler = new DeleteUser($user_service); 70 $handler->handle($request); 71 } 72 73 /** 74 * @return void 75 */ 76 public function testCannotDeleteAdministrator(): void 77 { 78 $this->expectException(HttpAccessDeniedException::class); 79 $this->expectExceptionMessage('Cannot delete an administrator'); 80 81 $user = $this->createMock(User::class); 82 $user->method('id')->willReturn(1); 83 $user->expects(self::once())->method('getPreference')->with(UserInterface::PREF_IS_ADMINISTRATOR)->willReturn('1'); 84 85 $user_service = $this->createMock(UserService::class); 86 $user_service->expects(self::once())->method('find')->willReturn($user); 87 88 $request = self::createRequest() 89 ->withAttribute('user_id', $user->id()); 90 $handler = new DeleteUser($user_service); 91 $handler->handle($request); 92 } 93} 94