xref: /webtrees/tests/app/Http/RequestHandlers/DeleteUserTest.php (revision 6b1381a306f9b3ddbe167a71b6a607d5ab7a609d)
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
18namespace Fisharebest\Webtrees\Http\RequestHandlers;
19
20use Fisharebest\Webtrees\Services\UserService;
21use Fisharebest\Webtrees\TestCase;
22
23/**
24 * @covers \Fisharebest\Webtrees\Http\RequestHandlers\DeleteUser
25 */
26class DeleteUserTest extends TestCase
27{
28    protected static $uses_database = true;
29
30    /**
31     * @return void
32     */
33    public function testDeleteUser(): void
34    {
35        $user_service = new UserService();
36        $user         = $user_service->create('user1', 'real1', 'email1', 'pass1');
37        $handler      = new DeleteUser($user_service);
38        $request      = self::createRequest('POST', ['route' => 'delete-user'], ['user_id' => $user->id()]);
39        $response     = $handler->handle($request);
40
41        // UserService caches user records
42        app('cache.array')->forget(UserService::class . $user->id());
43
44        self::assertSame(self::STATUS_NO_CONTENT, $response->getStatusCode());
45        self::assertNull($user_service->find($user->id()));
46    }
47
48    /**
49     * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
50     * @expectedExceptionMessage User ID 98765 not found
51     * @return void
52     */
53    public function testDeleteNonExistingUser(): void
54    {
55        $user_service = new UserService();
56        $user_service->create('user1', 'real1', 'email1', 'pass1');
57        $handler = new DeleteUser($user_service);
58        $request = self::createRequest('POST', ['route' => 'delete-user'], ['user_id' => 98765]);
59        $handler->handle($request);
60    }
61
62    /**
63     * @expectedException \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
64     * @expectedExceptionMessage Cannot delete an administrator
65     * @return void
66     */
67    public function testCannotDeleteAdministrator(): void
68    {
69        $user_service = new UserService();
70        $user         = $user_service->create('user1', 'real1', 'email1', 'pass1');
71        $user->setPreference('canadmin', '1');
72        $handler = new DeleteUser($user_service);
73        $request = self::createRequest('POST', ['route' => 'delete-user'], ['user_id' => $user->id()]);
74        $handler->handle($request);
75    }
76}
77