1*03f10823SGreg Roach<?php 2*03f10823SGreg Roach 3*03f10823SGreg Roach/** 4*03f10823SGreg Roach * webtrees: online genealogy 5*03f10823SGreg Roach * Copyright (C) 2019 webtrees development team 6*03f10823SGreg Roach * This program is free software: you can redistribute it and/or modify 7*03f10823SGreg Roach * it under the terms of the GNU General Public License as published by 8*03f10823SGreg Roach * the Free Software Foundation, either version 3 of the License, or 9*03f10823SGreg Roach * (at your option) any later version. 10*03f10823SGreg Roach * This program is distributed in the hope that it will be useful, 11*03f10823SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12*03f10823SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13*03f10823SGreg Roach * GNU General Public License for more details. 14*03f10823SGreg Roach * You should have received a copy of the GNU General Public License 15*03f10823SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 16*03f10823SGreg Roach */ 17*03f10823SGreg Roachdeclare(strict_types=1); 18*03f10823SGreg Roach 19*03f10823SGreg Roachnamespace Fisharebest\Webtrees\Http\Middleware; 20*03f10823SGreg Roach 21*03f10823SGreg Roachuse Fisharebest\Webtrees\GuestUser; 22*03f10823SGreg Roachuse Fisharebest\Webtrees\TestCase; 23*03f10823SGreg Roachuse Fisharebest\Webtrees\Tree; 24*03f10823SGreg Roachuse Fisharebest\Webtrees\User; 25*03f10823SGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 26*03f10823SGreg Roachuse Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException; 27*03f10823SGreg Roach 28*03f10823SGreg Roachuse function response; 29*03f10823SGreg Roach 30*03f10823SGreg Roach/** 31*03f10823SGreg Roach * Test the AuthEditor middleware. 32*03f10823SGreg Roach * 33*03f10823SGreg Roach * @covers \Fisharebest\Webtrees\Http\Middleware\AuthEditor 34*03f10823SGreg Roach */ 35*03f10823SGreg Roachclass AuthEditorTest extends TestCase 36*03f10823SGreg Roach{ 37*03f10823SGreg Roach /** 38*03f10823SGreg Roach * @return void 39*03f10823SGreg Roach */ 40*03f10823SGreg Roach public function testAllowed(): void 41*03f10823SGreg Roach { 42*03f10823SGreg Roach $handler = $this->createMock(RequestHandlerInterface::class); 43*03f10823SGreg Roach $handler->method('handle')->willReturn(response('lorem ipsum')); 44*03f10823SGreg Roach 45*03f10823SGreg Roach $user = $this->createMock(User::class); 46*03f10823SGreg Roach $user->method('getPreference')->with('canadmin')->willReturn('0'); 47*03f10823SGreg Roach 48*03f10823SGreg Roach $tree = $this->createMock(Tree::class); 49*03f10823SGreg Roach $tree->method('getUserPreference')->with($user, 'canedit')->willReturn('edit'); 50*03f10823SGreg Roach 51*03f10823SGreg Roach $request = self::createRequest()->withAttribute('tree', $tree)->withAttribute('user', $user); 52*03f10823SGreg Roach $middleware = new AuthEditor(); 53*03f10823SGreg Roach $response = $middleware->process($request, $handler); 54*03f10823SGreg Roach 55*03f10823SGreg Roach $this->assertSame(self::STATUS_OK, $response->getStatusCode()); 56*03f10823SGreg Roach $this->assertSame('lorem ipsum', (string) $response->getBody()); 57*03f10823SGreg Roach } 58*03f10823SGreg Roach 59*03f10823SGreg Roach /** 60*03f10823SGreg Roach * @expectedException \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException 61*03f10823SGreg Roach * @return void 62*03f10823SGreg Roach */ 63*03f10823SGreg Roach public function testNotAllowed(): void 64*03f10823SGreg Roach { 65*03f10823SGreg Roach $handler = $this->createMock(RequestHandlerInterface::class); 66*03f10823SGreg Roach $handler->method('handle')->willReturn(response('lorem ipsum')); 67*03f10823SGreg Roach 68*03f10823SGreg Roach $user = $this->createMock(User::class); 69*03f10823SGreg Roach $user->method('getPreference')->with('canadmin')->willReturn('0'); 70*03f10823SGreg Roach 71*03f10823SGreg Roach $tree = $this->createMock(Tree::class); 72*03f10823SGreg Roach $tree->method('getUserPreference')->with($user, 'canedit')->willReturn('access'); 73*03f10823SGreg Roach 74*03f10823SGreg Roach $request = self::createRequest()->withAttribute('tree', $tree)->withAttribute('user', $user); 75*03f10823SGreg Roach $middleware = new AuthEditor(); 76*03f10823SGreg Roach $middleware->process($request, $handler); 77*03f10823SGreg Roach } 78*03f10823SGreg Roach 79*03f10823SGreg Roach /** 80*03f10823SGreg Roach * @return void 81*03f10823SGreg Roach */ 82*03f10823SGreg Roach public function testNotLoggedIn(): void 83*03f10823SGreg Roach { 84*03f10823SGreg Roach $handler = $this->createMock(RequestHandlerInterface::class); 85*03f10823SGreg Roach $handler->method('handle')->willReturn(response('lorem ipsum')); 86*03f10823SGreg Roach 87*03f10823SGreg Roach $tree = $this->createMock(Tree::class); 88*03f10823SGreg Roach 89*03f10823SGreg Roach $request = self::createRequest()->withAttribute('tree', $tree)->withAttribute('user', new GuestUser()); 90*03f10823SGreg Roach $middleware = new AuthEditor(); 91*03f10823SGreg Roach $response = $middleware->process($request, $handler); 92*03f10823SGreg Roach 93*03f10823SGreg Roach $this->assertSame(self::STATUS_FOUND, $response->getStatusCode()); 94*03f10823SGreg Roach } 95*03f10823SGreg Roach} 96