. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Http\RequestHandlers; use Fig\Http\Message\StatusCodeInterface; use Fisharebest\Webtrees\Exceptions\HttpAccessDeniedException; use Fisharebest\Webtrees\Exceptions\HttpNotFoundException; use Fisharebest\Webtrees\Services\UserService; use Fisharebest\Webtrees\TestCase; use Fisharebest\Webtrees\User; /** * @covers \Fisharebest\Webtrees\Http\RequestHandlers\DeleteUser */ class DeleteUserTest extends TestCase { protected static $uses_database = true; /** * @return void */ public function testDeleteUser(): void { $user = self::createMock(User::class); $user->method('id')->willReturn(1); $user_service = self::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()); } /** * @return void */ public function testDeleteNonExistingUser(): void { $this->expectException(HttpNotFoundException::class); $this->expectExceptionMessage('User ID 98765 not found'); $user_service = self::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); } /** * @return void */ public function testCannotDeleteAdministrator(): void { $this->expectException(HttpAccessDeniedException::class); $this->expectExceptionMessage('Cannot delete an administrator'); $user = self::createMock(User::class); $user->method('id')->willReturn(1); $user->expects(self::once())->method('getPreference')->with(User::PREF_IS_ADMINISTRATOR)->willReturn('1'); $user_service = self::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); } }