1a0801ffbSGreg Roach<?php 23976b470SGreg Roach 3a0801ffbSGreg Roach/** 4a0801ffbSGreg Roach * webtrees: online genealogy 589f7189bSGreg Roach * Copyright (C) 2021 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; 24*81b729d3SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpAccessDeniedException; 25*81b729d3SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException; 26a0801ffbSGreg Roachuse Fisharebest\Webtrees\Services\UserService; 27a0801ffbSGreg Roachuse Fisharebest\Webtrees\TestCase; 28d403609dSGreg Roachuse Fisharebest\Webtrees\User; 29a0801ffbSGreg Roach 30a0801ffbSGreg Roach/** 31a0801ffbSGreg Roach * @covers \Fisharebest\Webtrees\Http\RequestHandlers\DeleteUser 32a0801ffbSGreg Roach */ 33a0801ffbSGreg Roachclass DeleteUserTest extends TestCase 34a0801ffbSGreg Roach{ 35cd94ca66SGreg Roach protected static bool $uses_database = true; 36a0801ffbSGreg Roach 37a0801ffbSGreg Roach /** 38a0801ffbSGreg Roach * @return void 39a0801ffbSGreg Roach */ 40a0801ffbSGreg Roach public function testDeleteUser(): void 41a0801ffbSGreg Roach { 42cd94ca66SGreg Roach $user = $this->createMock(User::class); 43d403609dSGreg Roach $user->method('id')->willReturn(1); 44a0801ffbSGreg Roach 45cd94ca66SGreg Roach $user_service = $this->createMock(UserService::class); 465e933c21SGreg Roach $user_service->expects(self::once())->method('find')->willReturn($user); 47d403609dSGreg Roach 48a2ae7ae7SGreg Roach $request = self::createRequest() 49a2ae7ae7SGreg Roach ->withAttribute('user_id', $user->id()); 50d403609dSGreg Roach $handler = new DeleteUser($user_service); 51d403609dSGreg Roach $response = $handler->handle($request); 52a0801ffbSGreg Roach 5371378461SGreg Roach self::assertSame(StatusCodeInterface::STATUS_NO_CONTENT, $response->getStatusCode()); 54a0801ffbSGreg Roach } 55a0801ffbSGreg Roach 56a0801ffbSGreg Roach /** 57a0801ffbSGreg Roach * @return void 58a0801ffbSGreg Roach */ 59a0801ffbSGreg Roach public function testDeleteNonExistingUser(): void 60a0801ffbSGreg Roach { 61d501c45dSGreg Roach $this->expectException(HttpNotFoundException::class); 623cfcc809SGreg Roach $this->expectExceptionMessage('User ID 98765 not found'); 633cfcc809SGreg Roach 64cd94ca66SGreg Roach $user_service = $this->createMock(UserService::class); 655e933c21SGreg Roach $user_service->expects(self::once())->method('find')->willReturn(null); 66d403609dSGreg Roach 67a2ae7ae7SGreg Roach $request = self::createRequest() 68a2ae7ae7SGreg Roach ->withAttribute('user_id', 98765); 69d403609dSGreg Roach $handler = new DeleteUser($user_service); 70a2ae7ae7SGreg Roach $handler->handle($request); 71a0801ffbSGreg Roach } 72a0801ffbSGreg Roach 73a0801ffbSGreg Roach /** 74a0801ffbSGreg Roach * @return void 75a0801ffbSGreg Roach */ 76a0801ffbSGreg Roach public function testCannotDeleteAdministrator(): void 77a0801ffbSGreg Roach { 78d501c45dSGreg Roach $this->expectException(HttpAccessDeniedException::class); 793cfcc809SGreg Roach $this->expectExceptionMessage('Cannot delete an administrator'); 803cfcc809SGreg Roach 81cd94ca66SGreg Roach $user = $this->createMock(User::class); 82d403609dSGreg Roach $user->method('id')->willReturn(1); 831fe542e9SGreg Roach $user->expects(self::once())->method('getPreference')->with(UserInterface::PREF_IS_ADMINISTRATOR)->willReturn('1'); 84d403609dSGreg Roach 85cd94ca66SGreg Roach $user_service = $this->createMock(UserService::class); 865e933c21SGreg Roach $user_service->expects(self::once())->method('find')->willReturn($user); 87d403609dSGreg Roach 88a2ae7ae7SGreg Roach $request = self::createRequest() 89a2ae7ae7SGreg Roach ->withAttribute('user_id', $user->id()); 90d403609dSGreg Roach $handler = new DeleteUser($user_service); 91d403609dSGreg Roach $handler->handle($request); 92a0801ffbSGreg Roach } 93a0801ffbSGreg Roach} 94