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 */ 17declare(strict_types=1); 18 19namespace Fisharebest\Webtrees\Http\RequestHandlers; 20 21use Fisharebest\Webtrees\GuestUser; 22use Fisharebest\Webtrees\Services\ModuleService; 23use Fisharebest\Webtrees\TestCase; 24use Fisharebest\Webtrees\Tree; 25use Psr\Http\Message\ServerRequestInterface; 26 27/** 28 * @covers \Fisharebest\Webtrees\Http\RequestHandlers\ModuleAction 29 */ 30class ModuleActionTest extends TestCase 31{ 32 protected static $uses_database = true; 33 34 /** 35 * @return void 36 */ 37 public function testModuleAction(): void 38 { 39 $tree = Tree::create('tree', 'tree'); 40 app()->instance(Tree::class, $tree); 41 $user = new GuestUser(); 42 $module_service = new ModuleService(); 43 $handler = new ModuleAction($module_service, $user); 44 $request = self::createRequest('GET', ['route' => 'module', 'module' => 'faq', 'action' => 'Show', 'ged' => $tree->name()]) 45 ->withAttribute('tree', $tree); 46 47 app()->instance(ServerRequestInterface::class, $request); 48 49 $response = $handler->handle($request); 50 51 $this->assertSame(self::STATUS_OK, $response->getStatusCode()); 52 } 53 54 /** 55 * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException 56 * @expectedExceptionMessage Method getFishAction() not found in faq 57 * @return void 58 */ 59 public function testNonExistingAction(): void 60 { 61 $user = new GuestUser(); 62 $module_service = new ModuleService(); 63 $handler = new ModuleAction($module_service, $user); 64 $request = self::createRequest('GET', ['route' => 'module', 'module' => 'faq', 'action' => 'Fish']); 65 $handler->handle($request); 66 } 67 68 /** 69 * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException 70 * @expectedExceptionMessage Module fish does not exist 71 * @return void 72 */ 73 public function testNonExistingModule(): void 74 { 75 $user = new GuestUser(); 76 $module_service = new ModuleService(); 77 $handler = new ModuleAction($module_service, $user); 78 $request = self::createRequest('GET', ['route' => 'module', 'module' => 'fish', 'action' => 'Show']); 79 $response = $handler->handle($request); 80 81 $this->assertSame(self::STATUS_OK, $response->getStatusCode()); 82 } 83 84 /** 85 * @expectedException \Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException 86 * @expectedExceptionMessage Admin only action 87 * @return void 88 */ 89 public function testAdminAction(): void 90 { 91 $tree = Tree::create('tree', 'tree'); 92 app()->instance(Tree::class, $tree); 93 $user = new GuestUser(); 94 $module_service = new ModuleService(); 95 $handler = new ModuleAction($module_service, $user); 96 $request = self::createRequest('GET', ['route' => 'module', 'module' => 'faq', 'action' => 'Admin', 'ged' => $tree->name()]); 97 $handler->handle($request); 98 } 99} 100