xref: /webtrees/tests/app/Http/RequestHandlers/DeleteUserTest.php (revision ef3fb807042ec0187072c1155b05525722f7fa7c)
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;
29use PHPUnit\Framework\Attributes\CoversClass;
30
31#[CoversClass(DeleteUser::class)]
32class DeleteUserTest extends TestCase
33{
34    protected static bool $uses_database = true;
35
36    public function testDeleteUser(): void
37    {
38        $user = $this->createMock(User::class);
39        $user->method('id')->willReturn(1);
40
41        $user_service = $this->createMock(UserService::class);
42        $user_service->expects($this->once())->method('find')->willReturn($user);
43
44        $request  = self::createRequest()
45            ->withAttribute('user_id', $user->id());
46        $handler  = new DeleteUser($user_service);
47        $response = $handler->handle($request);
48
49        self::assertSame(StatusCodeInterface::STATUS_NO_CONTENT, $response->getStatusCode());
50    }
51
52    public function testDeleteNonExistingUser(): void
53    {
54        $this->expectException(HttpNotFoundException::class);
55        $this->expectExceptionMessage('User ID 98765 not found');
56
57        $user_service = $this->createMock(UserService::class);
58        $user_service->expects($this->once())->method('find')->willReturn(null);
59
60        $request  = self::createRequest()
61            ->withAttribute('user_id', 98765);
62        $handler  = new DeleteUser($user_service);
63        $handler->handle($request);
64    }
65
66    public function testCannotDeleteAdministrator(): void
67    {
68        $this->expectException(HttpAccessDeniedException::class);
69        $this->expectExceptionMessage('Cannot delete an administrator');
70
71        $user = $this->createMock(User::class);
72        $user->method('id')->willReturn(1);
73        $user->expects($this->once())->method('getPreference')->with(UserInterface::PREF_IS_ADMINISTRATOR)->willReturn('1');
74
75        $user_service = $this->createMock(UserService::class);
76        $user_service->expects($this->once())->method('find')->willReturn($user);
77
78        $request  = self::createRequest()
79            ->withAttribute('user_id', $user->id());
80        $handler = new DeleteUser($user_service);
81        $handler->handle($request);
82    }
83}
84