xref: /webtrees/tests/app/Http/RequestHandlers/DeleteUserTest.php (revision 56f9a9c1fdf345d30ac6cd9caefe7542153b9246)
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;
23use Fisharebest\Webtrees\User;
24
25/**
26 * @covers \Fisharebest\Webtrees\Http\RequestHandlers\DeleteUser
27 */
28class DeleteUserTest extends TestCase
29{
30    protected static $uses_database = true;
31
32    /**
33     * @return void
34     */
35    public function testDeleteUser(): void
36    {
37        $user = $this->createMock(User::class);
38        $user->method('id')->willReturn(1);
39
40        $user_service = $this->createMock(UserService::class);
41        $user_service->expects($this->once())->method('find')->willReturn($user);
42
43        $request  = self::createRequest(self::METHOD_POST, [], ['user_id' => $user->id()]);
44        $handler  = new DeleteUser($user_service);
45        $response = $handler->handle($request);
46
47        self::assertSame(self::STATUS_NO_CONTENT, $response->getStatusCode());
48    }
49
50    /**
51     * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
52     * @expectedExceptionMessage User ID 98765 not found
53     * @return void
54     */
55    public function testDeleteNonExistingUser(): void
56    {
57        $user_service = $this->createMock(UserService::class);
58        $user_service->expects($this->once())->method('find')->willReturn(null);
59
60        $request  = self::createRequest(self::METHOD_POST, [], ['user_id' => 98765]);
61        $handler  = new DeleteUser($user_service);
62        $response = $handler->handle($request);
63    }
64
65    /**
66     * @expectedException \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException
67     * @expectedExceptionMessage Cannot delete an administrator
68     * @return void
69     */
70    public function testCannotDeleteAdministrator(): void
71    {
72        $user = $this->createMock(User::class);
73        $user->method('id')->willReturn(1);
74        $user->expects($this->once())->method('getPreference')->with('canadmin')->willReturn('1');
75
76        $user_service = $this->createMock(UserService::class);
77        $user_service->expects($this->once())->method('find')->willReturn($user);
78
79        $request = self::createRequest(self::METHOD_POST, [], ['user_id' => $user->id()]);
80        $handler = new DeleteUser($user_service);
81        $handler->handle($request);
82    }
83}
84