xref: /webtrees/tests/app/Http/Middleware/AuthMemberTest.php (revision 91c514e5e3b4273fec62c3ff012bcd0a10cfaca1)
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\Middleware;
21
22use Fig\Http\Message\StatusCodeInterface;
23use Fisharebest\Webtrees\GuestUser;
24use Fisharebest\Webtrees\TestCase;
25use Fisharebest\Webtrees\Tree;
26use Fisharebest\Webtrees\User;
27use Psr\Http\Server\RequestHandlerInterface;
28use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
29
30use function response;
31
32/**
33 * Test the AuthMember middleware.
34 *
35 * @covers \Fisharebest\Webtrees\Http\Middleware\AuthMember
36 */
37class AuthMemberTest extends TestCase
38{
39    /**
40     * @return void
41     */
42    public function testAllowed(): void
43    {
44        $handler = $this->createMock(RequestHandlerInterface::class);
45        $handler->method('handle')->willReturn(response('lorem ipsum'));
46
47        $user = $this->createMock(User::class);
48        $user->method('getPreference')->with('canadmin')->willReturn('0');
49
50        $tree = $this->createMock(Tree::class);
51        $tree->method('getUserPreference')->with($user, 'canedit')->willReturn('access');
52
53        $request    = self::createRequest()->withAttribute('tree', $tree)->withAttribute('user', $user);
54        $middleware = new AuthMember();
55        $response   = $middleware->process($request, $handler);
56
57        $this->assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
58        $this->assertSame('lorem ipsum', (string) $response->getBody());
59    }
60
61    /**
62     * @return void
63     */
64    public function testNotAllowed(): void
65    {
66        $handler = $this->createMock(RequestHandlerInterface::class);
67        $handler->method('handle')->willReturn(response('lorem ipsum'));
68
69        $user = $this->createMock(User::class);
70        $user->method('getPreference')->with('canadmin')->willReturn('0');
71
72        $tree = $this->createMock(Tree::class);
73        $tree->method('getUserPreference')->with($user, 'canedit')->willReturn('');
74
75        $request    = self::createRequest()->withAttribute('tree', $tree)->withAttribute('user', $user);
76        $middleware = new AuthMember();
77        $response   = $middleware->process($request, $handler);
78
79        $this->assertSame(StatusCodeInterface::STATUS_FOUND, $response->getStatusCode());
80    }
81
82    /**
83     * @return void
84     */
85    public function testNotLoggedIn(): void
86    {
87        $handler = $this->createMock(RequestHandlerInterface::class);
88        $handler->method('handle')->willReturn(response('lorem ipsum'));
89
90        $tree = $this->createMock(Tree::class);
91
92        $request    = self::createRequest()->withAttribute('tree', $tree)->withAttribute('user', new GuestUser());
93        $middleware = new AuthMember();
94        $response   = $middleware->process($request, $handler);
95
96        $this->assertSame(StatusCodeInterface::STATUS_FOUND, $response->getStatusCode());
97    }
98}
99