xref: /webtrees/tests/app/Http/RequestHandlers/DeleteUserTest.php (revision 824da64c7ee0caf91cac88892494fc0024068397)
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 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fig\Http\Message\StatusCodeInterface;
23use Fisharebest\Webtrees\Exceptions\HttpAccessDeniedException;
24use Fisharebest\Webtrees\Exceptions\HttpNotFoundException;
25use Fisharebest\Webtrees\Services\UserService;
26use Fisharebest\Webtrees\TestCase;
27use Fisharebest\Webtrees\User;
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()
48            ->withAttribute('user_id', $user->id());
49        $handler  = new DeleteUser($user_service);
50        $response = $handler->handle($request);
51
52        self::assertSame(StatusCodeInterface::STATUS_NO_CONTENT, $response->getStatusCode());
53    }
54
55    /**
56     * @return void
57     */
58    public function testDeleteNonExistingUser(): void
59    {
60        $this->expectException(HttpNotFoundException::class);
61        $this->expectExceptionMessage('User ID 98765 not found');
62
63        $user_service = $this->createMock(UserService::class);
64        $user_service->expects($this->once())->method('find')->willReturn(null);
65
66        $request  = self::createRequest()
67            ->withAttribute('user_id', 98765);
68        $handler  = new DeleteUser($user_service);
69        $handler->handle($request);
70    }
71
72    /**
73     * @return void
74     */
75    public function testCannotDeleteAdministrator(): void
76    {
77        $this->expectException(HttpAccessDeniedException::class);
78        $this->expectExceptionMessage('Cannot delete an administrator');
79
80        $user = $this->createMock(User::class);
81        $user->method('id')->willReturn(1);
82        $user->expects($this->once())->method('getPreference')->with(User::PREF_IS_ADMINISTRATOR)->willReturn('1');
83
84        $user_service = $this->createMock(UserService::class);
85        $user_service->expects($this->once())->method('find')->willReturn($user);
86
87        $request  = self::createRequest()
88            ->withAttribute('user_id', $user->id());
89        $handler = new DeleteUser($user_service);
90        $handler->handle($request);
91    }
92}
93