xref: /webtrees/tests/app/Http/RequestHandlers/DeleteUserTest.php (revision 0c0910bf0f275a14f35d2ccdf698f91f79e269d4)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17declare(strict_types=1);
18
19namespace Fisharebest\Webtrees\Http\RequestHandlers;
20
21use Fig\Http\Message\RequestMethodInterface;
22use Fig\Http\Message\StatusCodeInterface;
23use Fisharebest\Webtrees\Services\UserService;
24use Fisharebest\Webtrees\TestCase;
25use Fisharebest\Webtrees\User;
26use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
27use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
28
29/**
30 * @covers \Fisharebest\Webtrees\Http\RequestHandlers\DeleteUser
31 */
32class DeleteUserTest extends TestCase
33{
34    protected static $uses_database = true;
35
36    /**
37     * @return void
38     */
39    public function testDeleteUser(): void
40    {
41        $user = $this->createMock(User::class);
42        $user->method('id')->willReturn(1);
43
44        $user_service = $this->createMock(UserService::class);
45        $user_service->expects($this->once())->method('find')->willReturn($user);
46
47        $request  = self::createRequest(RequestMethodInterface::METHOD_POST, [], ['user_id' => $user->id()]);
48        $handler  = new DeleteUser($user_service);
49        $response = $handler->handle($request);
50
51        self::assertSame(StatusCodeInterface::STATUS_NO_CONTENT, $response->getStatusCode());
52    }
53
54    /**
55     * @return void
56     */
57    public function testDeleteNonExistingUser(): void
58    {
59        $this->expectException(NotFoundHttpException::class);
60        $this->expectExceptionMessage('User ID 98765 not found');
61
62        $user_service = $this->createMock(UserService::class);
63        $user_service->expects($this->once())->method('find')->willReturn(null);
64
65        $request  = self::createRequest(RequestMethodInterface::METHOD_POST, [], ['user_id' => 98765]);
66        $handler  = new DeleteUser($user_service);
67        $response = $handler->handle($request);
68    }
69
70    /**
71     * @return void
72     */
73    public function testCannotDeleteAdministrator(): void
74    {
75        $this->expectException(AccessDeniedHttpException::class);
76        $this->expectExceptionMessage('Cannot delete an administrator');
77
78        $user = $this->createMock(User::class);
79        $user->method('id')->willReturn(1);
80        $user->expects($this->once())->method('getPreference')->with('canadmin')->willReturn('1');
81
82        $user_service = $this->createMock(UserService::class);
83        $user_service->expects($this->once())->method('find')->willReturn($user);
84
85        $request = self::createRequest(RequestMethodInterface::METHOD_POST, [], ['user_id' => $user->id()]);
86        $handler = new DeleteUser($user_service);
87        $handler->handle($request);
88    }
89}
90