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