. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Http\RequestHandlers; use Fig\Http\Message\StatusCodeInterface; use Fisharebest\Webtrees\Contracts\UserInterface; use Fisharebest\Webtrees\Http\Exceptions\HttpAccessDeniedException; use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException; use Fisharebest\Webtrees\Services\UserService; use Fisharebest\Webtrees\TestCase; use Fisharebest\Webtrees\User; use PHPUnit\Framework\Attributes\CoversClass; #[CoversClass(DeleteUser::class)] class DeleteUserTest extends TestCase { protected static bool $uses_database = true; public function testDeleteUser(): void { $user = $this->createMock(User::class); $user->method('id')->willReturn(1); $user_service = $this->createMock(UserService::class); $user_service->expects(self::once())->method('find')->willReturn($user); $request = self::createRequest() ->withAttribute('user_id', $user->id()); $handler = new DeleteUser($user_service); $response = $handler->handle($request); self::assertSame(StatusCodeInterface::STATUS_NO_CONTENT, $response->getStatusCode()); } public function testDeleteNonExistingUser(): void { $this->expectException(HttpNotFoundException::class); $this->expectExceptionMessage('User ID 98765 not found'); $user_service = $this->createMock(UserService::class); $user_service->expects(self::once())->method('find')->willReturn(null); $request = self::createRequest() ->withAttribute('user_id', 98765); $handler = new DeleteUser($user_service); $handler->handle($request); } public function testCannotDeleteAdministrator(): void { $this->expectException(HttpAccessDeniedException::class); $this->expectExceptionMessage('Cannot delete an administrator'); $user = $this->createMock(User::class); $user->method('id')->willReturn(1); $user->expects(self::once())->method('getPreference')->with(UserInterface::PREF_IS_ADMINISTRATOR)->willReturn('1'); $user_service = $this->createMock(UserService::class); $user_service->expects(self::once())->method('find')->willReturn($user); $request = self::createRequest() ->withAttribute('user_id', $user->id()); $handler = new DeleteUser($user_service); $handler->handle($request); } }