xref: /webtrees/tests/app/Http/Middleware/AuthEditorTest.php (revision 5e933c21f8006e675d1df8bcedc634ee61f4aec2)
103f10823SGreg Roach<?php
203f10823SGreg Roach
303f10823SGreg Roach/**
403f10823SGreg Roach * webtrees: online genealogy
5*5e933c21SGreg Roach * Copyright (C) 2020 webtrees development team
603f10823SGreg Roach * This program is free software: you can redistribute it and/or modify
703f10823SGreg Roach * it under the terms of the GNU General Public License as published by
803f10823SGreg Roach * the Free Software Foundation, either version 3 of the License, or
903f10823SGreg Roach * (at your option) any later version.
1003f10823SGreg Roach * This program is distributed in the hope that it will be useful,
1103f10823SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1203f10823SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1303f10823SGreg Roach * GNU General Public License for more details.
1403f10823SGreg Roach * You should have received a copy of the GNU General Public License
1503f10823SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
1603f10823SGreg Roach */
17fcfa147eSGreg Roach
1803f10823SGreg Roachdeclare(strict_types=1);
1903f10823SGreg Roach
2003f10823SGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware;
2103f10823SGreg Roach
2271378461SGreg Roachuse Fig\Http\Message\StatusCodeInterface;
23d501c45dSGreg Roachuse Fisharebest\Webtrees\Exceptions\HttpAccessDeniedException;
2403f10823SGreg Roachuse Fisharebest\Webtrees\GuestUser;
2503f10823SGreg Roachuse Fisharebest\Webtrees\TestCase;
2603f10823SGreg Roachuse Fisharebest\Webtrees\Tree;
2703f10823SGreg Roachuse Fisharebest\Webtrees\User;
2803f10823SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
2903f10823SGreg Roach
3003f10823SGreg Roachuse function response;
3103f10823SGreg Roach
3203f10823SGreg Roach/**
3303f10823SGreg Roach * Test the AuthEditor middleware.
3403f10823SGreg Roach *
3503f10823SGreg Roach * @covers \Fisharebest\Webtrees\Http\Middleware\AuthEditor
3603f10823SGreg Roach */
3703f10823SGreg Roachclass AuthEditorTest extends TestCase
3803f10823SGreg Roach{
3903f10823SGreg Roach    /**
4003f10823SGreg Roach     * @return void
4103f10823SGreg Roach     */
4203f10823SGreg Roach    public function testAllowed(): void
4303f10823SGreg Roach    {
44*5e933c21SGreg Roach        $handler = self::createMock(RequestHandlerInterface::class);
4503f10823SGreg Roach        $handler->method('handle')->willReturn(response('lorem ipsum'));
4603f10823SGreg Roach
47*5e933c21SGreg Roach        $user = self::createMock(User::class);
487c4add84SGreg Roach        $user->method('getPreference')->with(User::PREF_IS_ADMINISTRATOR)->willReturn('');
4903f10823SGreg Roach
50*5e933c21SGreg Roach        $tree = self::createMock(Tree::class);
517c4add84SGreg Roach        $tree->method('getUserPreference')->with($user, User::PREF_TREE_ROLE)->willReturn('edit');
5203f10823SGreg Roach
5303f10823SGreg Roach        $request    = self::createRequest()->withAttribute('tree', $tree)->withAttribute('user', $user);
5403f10823SGreg Roach        $middleware = new AuthEditor();
5503f10823SGreg Roach        $response   = $middleware->process($request, $handler);
5603f10823SGreg Roach
57*5e933c21SGreg Roach        self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
58*5e933c21SGreg Roach        self::assertSame('lorem ipsum', (string) $response->getBody());
5903f10823SGreg Roach    }
6003f10823SGreg Roach
6103f10823SGreg Roach    /**
6203f10823SGreg Roach     * @return void
6303f10823SGreg Roach     */
6403f10823SGreg Roach    public function testNotAllowed(): void
6503f10823SGreg Roach    {
66d501c45dSGreg Roach        $this->expectException(HttpAccessDeniedException::class);
675fb051e9SGreg Roach        $this->expectExceptionMessage('You do not have permission to view this page.');
685fb051e9SGreg Roach
69*5e933c21SGreg Roach        $handler = self::createMock(RequestHandlerInterface::class);
7003f10823SGreg Roach        $handler->method('handle')->willReturn(response('lorem ipsum'));
7103f10823SGreg Roach
72*5e933c21SGreg Roach        $user = self::createMock(User::class);
737c4add84SGreg Roach        $user->method('getPreference')->with(User::PREF_IS_ADMINISTRATOR)->willReturn('');
7403f10823SGreg Roach
75*5e933c21SGreg Roach        $tree = self::createMock(Tree::class);
767c4add84SGreg Roach        $tree->method('getUserPreference')->with($user, User::PREF_TREE_ROLE)->willReturn('access');
7703f10823SGreg Roach
7803f10823SGreg Roach        $request    = self::createRequest()->withAttribute('tree', $tree)->withAttribute('user', $user);
7903f10823SGreg Roach        $middleware = new AuthEditor();
800c0910bfSGreg Roach
815fb051e9SGreg Roach        $middleware->process($request, $handler);
8203f10823SGreg Roach    }
8303f10823SGreg Roach
8403f10823SGreg Roach    /**
8503f10823SGreg Roach     * @return void
8603f10823SGreg Roach     */
8703f10823SGreg Roach    public function testNotLoggedIn(): void
8803f10823SGreg Roach    {
89*5e933c21SGreg Roach        $handler = self::createMock(RequestHandlerInterface::class);
9003f10823SGreg Roach        $handler->method('handle')->willReturn(response('lorem ipsum'));
9103f10823SGreg Roach
92*5e933c21SGreg Roach        $tree = self::createMock(Tree::class);
9303f10823SGreg Roach
9403f10823SGreg Roach        $request    = self::createRequest()->withAttribute('tree', $tree)->withAttribute('user', new GuestUser());
9503f10823SGreg Roach        $middleware = new AuthEditor();
9603f10823SGreg Roach        $response   = $middleware->process($request, $handler);
9703f10823SGreg Roach
98*5e933c21SGreg Roach        self::assertSame(StatusCodeInterface::STATUS_FOUND, $response->getStatusCode());
9903f10823SGreg Roach    }
10003f10823SGreg Roach}
101