xref: /webtrees/tests/app/Http/Middleware/AuthMemberTest.php (revision d11be7027e34e3121be11cc025421873364403f9)
101202f80SGreg Roach<?php
201202f80SGreg Roach
301202f80SGreg Roach/**
401202f80SGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
601202f80SGreg Roach * This program is free software: you can redistribute it and/or modify
701202f80SGreg Roach * it under the terms of the GNU General Public License as published by
801202f80SGreg Roach * the Free Software Foundation, either version 3 of the License, or
901202f80SGreg Roach * (at your option) any later version.
1001202f80SGreg Roach * This program is distributed in the hope that it will be useful,
1101202f80SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1201202f80SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1301202f80SGreg Roach * GNU General Public License for more details.
1401202f80SGreg 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/>.
1601202f80SGreg Roach */
17fcfa147eSGreg Roach
1801202f80SGreg Roachdeclare(strict_types=1);
1901202f80SGreg Roach
2001202f80SGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware;
2101202f80SGreg Roach
2271378461SGreg Roachuse Fig\Http\Message\StatusCodeInterface;
231fe542e9SGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
2401202f80SGreg Roachuse Fisharebest\Webtrees\GuestUser;
2581b729d3SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpAccessDeniedException;
2601202f80SGreg Roachuse Fisharebest\Webtrees\TestCase;
2701202f80SGreg Roachuse Fisharebest\Webtrees\Tree;
2801202f80SGreg Roachuse Fisharebest\Webtrees\User;
2901202f80SGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
3001202f80SGreg Roach
3101202f80SGreg Roachuse function response;
3201202f80SGreg Roach
3301202f80SGreg Roach/**
3401202f80SGreg Roach * Test the AuthMember middleware.
3501202f80SGreg Roach *
3601202f80SGreg Roach * @covers \Fisharebest\Webtrees\Http\Middleware\AuthMember
3701202f80SGreg Roach */
3801202f80SGreg Roachclass AuthMemberTest extends TestCase
3901202f80SGreg Roach{
4001202f80SGreg Roach    /**
4101202f80SGreg Roach     * @return void
4201202f80SGreg Roach     */
4301202f80SGreg Roach    public function testAllowed(): void
4401202f80SGreg Roach    {
45cd94ca66SGreg Roach        $handler = $this->createMock(RequestHandlerInterface::class);
4601202f80SGreg Roach        $handler->method('handle')->willReturn(response('lorem ipsum'));
4701202f80SGreg Roach
48cd94ca66SGreg Roach        $user = $this->createMock(User::class);
491fe542e9SGreg Roach        $user->method('getPreference')->with(UserInterface::PREF_IS_ADMINISTRATOR)->willReturn('');
5001202f80SGreg Roach
51cd94ca66SGreg Roach        $tree = $this->createMock(Tree::class);
521fe542e9SGreg Roach        $tree->method('getUserPreference')->with($user, UserInterface::PREF_TREE_ROLE)->willReturn(UserInterface::ROLE_MEMBER);
5301202f80SGreg Roach
5401202f80SGreg Roach        $request    = self::createRequest()->withAttribute('tree', $tree)->withAttribute('user', $user);
5501202f80SGreg Roach        $middleware = new AuthMember();
5601202f80SGreg Roach        $response   = $middleware->process($request, $handler);
5701202f80SGreg Roach
585e933c21SGreg Roach        self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode());
595e933c21SGreg Roach        self::assertSame('lorem ipsum', (string) $response->getBody());
6001202f80SGreg Roach    }
6101202f80SGreg Roach
6201202f80SGreg Roach    /**
6301202f80SGreg Roach     * @return void
6401202f80SGreg Roach     */
6501202f80SGreg Roach    public function testNotAllowed(): void
6601202f80SGreg Roach    {
67d501c45dSGreg Roach        $this->expectException(HttpAccessDeniedException::class);
685fb051e9SGreg Roach        $this->expectExceptionMessage('You do not have permission to view this page.');
695fb051e9SGreg Roach
70cd94ca66SGreg Roach        $handler = $this->createMock(RequestHandlerInterface::class);
7101202f80SGreg Roach        $handler->method('handle')->willReturn(response('lorem ipsum'));
7201202f80SGreg Roach
73cd94ca66SGreg Roach        $user = $this->createMock(User::class);
741fe542e9SGreg Roach        $user->method('getPreference')->with(UserInterface::PREF_IS_ADMINISTRATOR)->willReturn('');
7501202f80SGreg Roach
76cd94ca66SGreg Roach        $tree = $this->createMock(Tree::class);
771fe542e9SGreg Roach        $tree->method('getUserPreference')->with($user, UserInterface::PREF_TREE_ROLE)->willReturn('');
7801202f80SGreg Roach
7901202f80SGreg Roach        $request    = self::createRequest()->withAttribute('tree', $tree)->withAttribute('user', $user);
8001202f80SGreg Roach        $middleware = new AuthMember();
810c0910bfSGreg Roach
825fb051e9SGreg Roach        $middleware->process($request, $handler);
8301202f80SGreg Roach    }
8401202f80SGreg Roach
8501202f80SGreg Roach    /**
8601202f80SGreg Roach     * @return void
8701202f80SGreg Roach     */
8801202f80SGreg Roach    public function testNotLoggedIn(): void
8901202f80SGreg Roach    {
90cd94ca66SGreg Roach        $handler = $this->createMock(RequestHandlerInterface::class);
9101202f80SGreg Roach        $handler->method('handle')->willReturn(response('lorem ipsum'));
9201202f80SGreg Roach
93cd94ca66SGreg Roach        $tree = $this->createMock(Tree::class);
9401202f80SGreg Roach
9501202f80SGreg Roach        $request    = self::createRequest()->withAttribute('tree', $tree)->withAttribute('user', new GuestUser());
9601202f80SGreg Roach        $middleware = new AuthMember();
9701202f80SGreg Roach        $response   = $middleware->process($request, $handler);
9801202f80SGreg Roach
995e933c21SGreg Roach        self::assertSame(StatusCodeInterface::STATUS_FOUND, $response->getStatusCode());
10001202f80SGreg Roach    }
10101202f80SGreg Roach}
102