xref: /webtrees/tests/app/Http/RequestHandlers/DeleteUserTest.php (revision 80dcfb364a7797c77baca388219f53d615c9bab0)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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 <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fig\Http\Message\StatusCodeInterface;
23use Fisharebest\Webtrees\Contracts\UserInterface;
24use Fisharebest\Webtrees\Http\Exceptions\HttpAccessDeniedException;
25use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
26use Fisharebest\Webtrees\Services\UserService;
27use Fisharebest\Webtrees\TestCase;
28use Fisharebest\Webtrees\User;
29
30/**
31 * @covers \Fisharebest\Webtrees\Http\RequestHandlers\DeleteUser
32 */
33class DeleteUserTest extends TestCase
34{
35    protected static bool $uses_database = true;
36
37    public function testDeleteUser(): void
38    {
39        $user = $this->createMock(User::class);
40        $user->method('id')->willReturn(1);
41
42        $user_service = $this->createMock(UserService::class);
43        $user_service->expects(self::once())->method('find')->willReturn($user);
44
45        $request  = self::createRequest()
46            ->withAttribute('user_id', $user->id());
47        $handler  = new DeleteUser($user_service);
48        $response = $handler->handle($request);
49
50        self::assertSame(StatusCodeInterface::STATUS_NO_CONTENT, $response->getStatusCode());
51    }
52
53    public function testDeleteNonExistingUser(): void
54    {
55        $this->expectException(HttpNotFoundException::class);
56        $this->expectExceptionMessage('User ID 98765 not found');
57
58        $user_service = $this->createMock(UserService::class);
59        $user_service->expects(self::once())->method('find')->willReturn(null);
60
61        $request  = self::createRequest()
62            ->withAttribute('user_id', 98765);
63        $handler  = new DeleteUser($user_service);
64        $handler->handle($request);
65    }
66
67    public function testCannotDeleteAdministrator(): void
68    {
69        $this->expectException(HttpAccessDeniedException::class);
70        $this->expectExceptionMessage('Cannot delete an administrator');
71
72        $user = $this->createMock(User::class);
73        $user->method('id')->willReturn(1);
74        $user->expects(self::once())->method('getPreference')->with(UserInterface::PREF_IS_ADMINISTRATOR)->willReturn('1');
75
76        $user_service = $this->createMock(UserService::class);
77        $user_service->expects(self::once())->method('find')->willReturn($user);
78
79        $request  = self::createRequest()
80            ->withAttribute('user_id', $user->id());
81        $handler = new DeleteUser($user_service);
82        $handler->handle($request);
83    }
84}
85