. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Http\RequestHandlers; 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 = $this->createMock(User::class); $user->method('id')->willReturn(1); $user_service = $this->createMock(UserService::class); $user_service->expects($this->once())->method('find')->willReturn($user); $request = self::createRequest(self::METHOD_POST, [], ['user_id' => $user->id()]); $handler = new DeleteUser($user_service); $response = $handler->handle($request); self::assertSame(self::STATUS_NO_CONTENT, $response->getStatusCode()); } /** * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException * @expectedExceptionMessage User ID 98765 not found * @return void */ public function testDeleteNonExistingUser(): void { $user_service = $this->createMock(UserService::class); $user_service->expects($this->once())->method('find')->willReturn(null); $request = self::createRequest(self::METHOD_POST, [], ['user_id' => 98765]); $handler = new DeleteUser($user_service); $response = $handler->handle($request); } /** * @expectedException \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException * @expectedExceptionMessage Cannot delete an administrator * @return void */ public function testCannotDeleteAdministrator(): void { $user = $this->createMock(User::class); $user->method('id')->willReturn(1); $user->expects($this->once())->method('getPreference')->with('canadmin')->willReturn('1'); $user_service = $this->createMock(UserService::class); $user_service->expects($this->once())->method('find')->willReturn($user); $request = self::createRequest(self::METHOD_POST, [], ['user_id' => $user->id()]); $handler = new DeleteUser($user_service); $handler->handle($request); } }