1a0801ffbSGreg Roach<?php 23976b470SGreg Roach 3a0801ffbSGreg Roach/** 4a0801ffbSGreg Roach * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 6a0801ffbSGreg Roach * This program is free software: you can redistribute it and/or modify 7a0801ffbSGreg Roach * it under the terms of the GNU General Public License as published by 8a0801ffbSGreg Roach * the Free Software Foundation, either version 3 of the License, or 9a0801ffbSGreg Roach * (at your option) any later version. 10a0801ffbSGreg Roach * This program is distributed in the hope that it will be useful, 11a0801ffbSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12a0801ffbSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13a0801ffbSGreg Roach * GNU General Public License for more details. 14a0801ffbSGreg 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/>. 16a0801ffbSGreg Roach */ 17fcfa147eSGreg Roach 18a0801ffbSGreg Roachdeclare(strict_types=1); 19a0801ffbSGreg Roach 2074d6dc0eSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers; 2174d6dc0eSGreg Roach 2271378461SGreg Roachuse Fig\Http\Message\StatusCodeInterface; 231fe542e9SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface; 2481b729d3SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpAccessDeniedException; 2581b729d3SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException; 26a0801ffbSGreg Roachuse Fisharebest\Webtrees\Services\UserService; 27a0801ffbSGreg Roachuse Fisharebest\Webtrees\TestCase; 28d403609dSGreg Roachuse Fisharebest\Webtrees\User; 29202c018bSGreg Roachuse PHPUnit\Framework\Attributes\CoversClass; 30a0801ffbSGreg Roach 31202c018bSGreg Roach#[CoversClass(DeleteUser::class)] 32a0801ffbSGreg Roachclass DeleteUserTest extends TestCase 33a0801ffbSGreg Roach{ 34cd94ca66SGreg Roach protected static bool $uses_database = true; 35a0801ffbSGreg Roach 36a0801ffbSGreg Roach public function testDeleteUser(): void 37a0801ffbSGreg Roach { 38cd94ca66SGreg Roach $user = $this->createMock(User::class); 39d403609dSGreg Roach $user->method('id')->willReturn(1); 40a0801ffbSGreg Roach 41cd94ca66SGreg Roach $user_service = $this->createMock(UserService::class); 42*00ef1d3aSGreg Roach $user_service->expects($this->once())->method('find')->willReturn($user); 43d403609dSGreg Roach 44a2ae7ae7SGreg Roach $request = self::createRequest() 45a2ae7ae7SGreg Roach ->withAttribute('user_id', $user->id()); 46d403609dSGreg Roach $handler = new DeleteUser($user_service); 47d403609dSGreg Roach $response = $handler->handle($request); 48a0801ffbSGreg Roach 4971378461SGreg Roach self::assertSame(StatusCodeInterface::STATUS_NO_CONTENT, $response->getStatusCode()); 50a0801ffbSGreg Roach } 51a0801ffbSGreg Roach 52a0801ffbSGreg Roach public function testDeleteNonExistingUser(): void 53a0801ffbSGreg Roach { 54d501c45dSGreg Roach $this->expectException(HttpNotFoundException::class); 553cfcc809SGreg Roach $this->expectExceptionMessage('User ID 98765 not found'); 563cfcc809SGreg Roach 57cd94ca66SGreg Roach $user_service = $this->createMock(UserService::class); 58*00ef1d3aSGreg Roach $user_service->expects($this->once())->method('find')->willReturn(null); 59d403609dSGreg Roach 60a2ae7ae7SGreg Roach $request = self::createRequest() 61a2ae7ae7SGreg Roach ->withAttribute('user_id', 98765); 62d403609dSGreg Roach $handler = new DeleteUser($user_service); 63a2ae7ae7SGreg Roach $handler->handle($request); 64a0801ffbSGreg Roach } 65a0801ffbSGreg Roach 66a0801ffbSGreg Roach public function testCannotDeleteAdministrator(): void 67a0801ffbSGreg Roach { 68d501c45dSGreg Roach $this->expectException(HttpAccessDeniedException::class); 693cfcc809SGreg Roach $this->expectExceptionMessage('Cannot delete an administrator'); 703cfcc809SGreg Roach 71cd94ca66SGreg Roach $user = $this->createMock(User::class); 72d403609dSGreg Roach $user->method('id')->willReturn(1); 73*00ef1d3aSGreg Roach $user->expects($this->once())->method('getPreference')->with(UserInterface::PREF_IS_ADMINISTRATOR)->willReturn('1'); 74d403609dSGreg Roach 75cd94ca66SGreg Roach $user_service = $this->createMock(UserService::class); 76*00ef1d3aSGreg Roach $user_service->expects($this->once())->method('find')->willReturn($user); 77d403609dSGreg Roach 78a2ae7ae7SGreg Roach $request = self::createRequest() 79a2ae7ae7SGreg Roach ->withAttribute('user_id', $user->id()); 80d403609dSGreg Roach $handler = new DeleteUser($user_service); 81d403609dSGreg Roach $handler->handle($request); 82a0801ffbSGreg Roach } 83a0801ffbSGreg Roach} 84