1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2023 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 <https://www.gnu.org/licenses/>. 16 */ 17 18declare(strict_types=1); 19 20namespace Fisharebest\Webtrees\Http\RequestHandlers; 21 22use Fig\Http\Message\StatusCodeInterface; 23use Fisharebest\Webtrees\GuestUser; 24use Fisharebest\Webtrees\Http\Exceptions\HttpAccessDeniedException; 25use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException; 26use Fisharebest\Webtrees\Module\AbstractModule; 27use Fisharebest\Webtrees\Module\ModuleInterface; 28use Fisharebest\Webtrees\Services\ModuleService; 29use Fisharebest\Webtrees\TestCase; 30use PHPUnit\Framework\Attributes\CoversClass; 31use Psr\Http\Message\ResponseInterface; 32 33use function response; 34 35#[CoversClass(ModuleAction::class)] 36class ModuleActionTest extends TestCase 37{ 38 public function testModuleAction(): void 39 { 40 $module_service = $this->createMock(ModuleService::class); 41 $module_service 42 ->expects(self::once()) 43 ->method('findByName') 44 ->with('test') 45 ->willReturn($this->fooModule()); 46 47 $user = new GuestUser(); 48 $request = self::createRequest() 49 ->withAttribute('module', 'test') 50 ->withAttribute('action', 'Test') 51 ->withAttribute('user', $user); 52 $handler = new ModuleAction($module_service); 53 $response = $handler->handle($request); 54 55 self::assertSame(StatusCodeInterface::STATUS_OK, $response->getStatusCode()); 56 self::assertSame('It works!', (string) $response->getBody()); 57 } 58 59 public function testNonExistingAction(): void 60 { 61 $this->expectException(HttpNotFoundException::class); 62 $this->expectExceptionMessage('Method getTestingAction() not found in test'); 63 64 $module_service = $this->createMock(ModuleService::class); 65 $module_service 66 ->expects(self::once()) 67 ->method('findByName') 68 ->with('test') 69 ->willReturn($this->fooModule()); 70 71 $user = new GuestUser(); 72 $request = self::createRequest() 73 ->withAttribute('module', 'test') 74 ->withAttribute('action', 'Testing') 75 ->withAttribute('user', $user); 76 $handler = new ModuleAction($module_service); 77 $handler->handle($request); 78 } 79 80 public function testNonExistingModule(): void 81 { 82 $this->expectException(HttpNotFoundException::class); 83 $this->expectExceptionMessage('Module test does not exist'); 84 85 $module_service = $this->createMock(ModuleService::class); 86 $module_service 87 ->expects(self::once()) 88 ->method('findByName') 89 ->with('test') 90 ->willReturn(null); 91 92 $user = new GuestUser(); 93 $request = self::createRequest() 94 ->withAttribute('module', 'test') 95 ->withAttribute('action', 'Test') 96 ->withAttribute('user', $user); 97 $handler = new ModuleAction($module_service); 98 $handler->handle($request); 99 } 100 101 public function testAdminAction(): void 102 { 103 $this->expectException(HttpAccessDeniedException::class); 104 $this->expectExceptionMessage('Admin only action'); 105 106 $module_service = $this->createMock(ModuleService::class); 107 $module_service 108 ->expects(self::once()) 109 ->method('findByName') 110 ->with('test') 111 ->willReturn($this->fooModule()); 112 113 $user = new GuestUser(); 114 $request = self::createRequest() 115 ->withAttribute('module', 'test') 116 ->withAttribute('action', 'Admin') 117 ->withAttribute('user', $user); 118 $handler = new ModuleAction($module_service); 119 $handler->handle($request); 120 } 121 122 private function fooModule(): ModuleInterface 123 { 124 return new class () extends AbstractModule { 125 public function getTestAction(): ResponseInterface 126 { 127 return response('It works!'); 128 } 129 }; 130 } 131} 132