xref: /webtrees/tests/app/Http/RequestHandlers/DeleteUserTest.php (revision 3976b4703df669696105ed6b024b96d433c8fbdb)
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 */
17declare(strict_types=1);
18
19namespace Fisharebest\Webtrees\Http\RequestHandlers;
20
21use Fisharebest\Webtrees\Services\UserService;
22use Fisharebest\Webtrees\TestCase;
23
24/**
25 * @covers \Fisharebest\Webtrees\Http\RequestHandlers\DeleteUser
26 */
27class DeleteUserTest extends TestCase
28{
29    protected static $uses_database = true;
30
31    /**
32     * @return void
33     */
34    public function testDeleteUser(): void
35    {
36        $user_service = new UserService();
37        $user         = $user_service->create('user1', 'real1', 'email1', 'pass1');
38        $request      = self::createRequest('POST', ['route' => 'delete-user'], ['user_id' => $user->id()]);
39        $response     = app(DeleteUser::class)->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        $request = self::createRequest('POST', ['route' => 'delete-user'], ['user_id' => 98765]);
56        app(DeleteUser::class)->handle($request);
57    }
58
59    /**
60     * @expectedException \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
61     * @expectedExceptionMessage Cannot delete an administrator
62     * @return void
63     */
64    public function testCannotDeleteAdministrator(): void
65    {
66        $user = app(UserService::class)->create('user1', 'real1', 'email1', 'pass1');
67        $user->setPreference('canadmin', '1');
68        $request = self::createRequest('POST', ['route' => 'delete-user'], ['user_id' => $user->id()]);
69        app(DeleteUser::class)->handle($request);
70    }
71}
72