xref: /webtrees/tests/app/Http/Middleware/AuthEditorTest.php (revision 202c018b592d5a516e4a465dc6dc515f3be37399)
103f10823SGreg Roach<?php
203f10823SGreg Roach
303f10823SGreg Roach/**
403f10823SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 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
1589f7189bSGreg Roach * along with this program. If not, see <https://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;
231fe542e9SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
2403f10823SGreg Roachuse Fisharebest\Webtrees\GuestUser;
2581b729d3SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpAccessDeniedException;
2603f10823SGreg Roachuse Fisharebest\Webtrees\TestCase;
2703f10823SGreg Roachuse Fisharebest\Webtrees\Tree;
2803f10823SGreg Roachuse Fisharebest\Webtrees\User;
29*202c018bSGreg Roachuse PHPUnit\Framework\Attributes\CoversClass;
3003f10823SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
3103f10823SGreg Roach
3203f10823SGreg Roachuse function response;
3303f10823SGreg Roach
34*202c018bSGreg Roach
35*202c018bSGreg Roach#[CoversClass(AuthEditor::class)]
3603f10823SGreg Roachclass AuthEditorTest extends TestCase
3703f10823SGreg Roach{
3803f10823SGreg Roach    public function testAllowed(): void
3903f10823SGreg Roach    {
40cd94ca66SGreg Roach        $handler = $this->createMock(RequestHandlerInterface::class);
4103f10823SGreg Roach        $handler->method('handle')->willReturn(response('lorem ipsum'));
4203f10823SGreg Roach
43cd94ca66SGreg Roach        $user = $this->createMock(User::class);
441fe542e9SGreg Roach        $user->method('getPreference')->with(UserInterface::PREF_IS_ADMINISTRATOR)->willReturn('');
4503f10823SGreg Roach
46cd94ca66SGreg Roach        $tree = $this->createMock(Tree::class);
471fe542e9SGreg Roach        $tree->method('getUserPreference')->with($user, UserInterface::PREF_TREE_ROLE)->willReturn(UserInterface::ROLE_EDITOR);
4803f10823SGreg Roach
4903f10823SGreg Roach        $request    = self::createRequest()->withAttribute('tree', $tree)->withAttribute('user', $user);
5003f10823SGreg Roach        $middleware = new AuthEditor();
5103f10823SGreg Roach        $response   = $middleware->process($request, $handler);
5203f10823SGreg Roach
535e933c21SGreg Roach        self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
545e933c21SGreg Roach        self::assertSame('lorem ipsum', (string) $response->getBody());
5503f10823SGreg Roach    }
5603f10823SGreg Roach
5703f10823SGreg Roach    public function testNotAllowed(): void
5803f10823SGreg Roach    {
59d501c45dSGreg Roach        $this->expectException(HttpAccessDeniedException::class);
605fb051e9SGreg Roach        $this->expectExceptionMessage('You do not have permission to view this page.');
615fb051e9SGreg Roach
62cd94ca66SGreg Roach        $handler = $this->createMock(RequestHandlerInterface::class);
6303f10823SGreg Roach        $handler->method('handle')->willReturn(response('lorem ipsum'));
6403f10823SGreg Roach
65cd94ca66SGreg Roach        $user = $this->createMock(User::class);
661fe542e9SGreg Roach        $user->method('getPreference')->with(UserInterface::PREF_IS_ADMINISTRATOR)->willReturn('');
6703f10823SGreg Roach
68cd94ca66SGreg Roach        $tree = $this->createMock(Tree::class);
691fe542e9SGreg Roach        $tree->method('getUserPreference')->with($user, UserInterface::PREF_TREE_ROLE)->willReturn(UserInterface::ROLE_MEMBER);
7003f10823SGreg Roach
7103f10823SGreg Roach        $request    = self::createRequest()->withAttribute('tree', $tree)->withAttribute('user', $user);
7203f10823SGreg Roach        $middleware = new AuthEditor();
730c0910bfSGreg Roach
745fb051e9SGreg Roach        $middleware->process($request, $handler);
7503f10823SGreg Roach    }
7603f10823SGreg Roach
7703f10823SGreg Roach    public function testNotLoggedIn(): void
7803f10823SGreg Roach    {
79cd94ca66SGreg Roach        $handler = $this->createMock(RequestHandlerInterface::class);
8003f10823SGreg Roach        $handler->method('handle')->willReturn(response('lorem ipsum'));
8103f10823SGreg Roach
82cd94ca66SGreg Roach        $tree = $this->createMock(Tree::class);
8303f10823SGreg Roach
8403f10823SGreg Roach        $request    = self::createRequest()->withAttribute('tree', $tree)->withAttribute('user', new GuestUser());
8503f10823SGreg Roach        $middleware = new AuthEditor();
8603f10823SGreg Roach        $response   = $middleware->process($request, $handler);
8703f10823SGreg Roach
885e933c21SGreg Roach        self::assertSame(StatusCodeInterface::STATUS_FOUND, $response->getStatusCode());
8903f10823SGreg Roach    }
9003f10823SGreg Roach}
91