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\RequestMethodInterface; 23use Fig\Http\Message\StatusCodeInterface; 24use Fisharebest\Webtrees\Http\Exceptions\HttpGoneException; 25use Fisharebest\Webtrees\Module\LifespansChartModule; 26use Fisharebest\Webtrees\Module\ModuleChartInterface; 27use Fisharebest\Webtrees\Services\ModuleService; 28use Fisharebest\Webtrees\Services\TreeService; 29use Fisharebest\Webtrees\TestCase; 30use Fisharebest\Webtrees\Tree; 31use Illuminate\Support\Collection; 32use PHPUnit\Framework\Attributes\CoversClass; 33 34#[CoversClass(RedirectLifeSpanPhp::class)] 35class RedirectLifeSpanPhpTest extends TestCase 36{ 37 protected static bool $uses_database = true; 38 39 public function testRedirect(): void 40 { 41 $tree = $this->createMock(Tree::class); 42 $tree 43 ->method('name') 44 ->willReturn('tree1'); 45 46 $tree_service = $this->createMock(TreeService::class); 47 $tree_service 48 ->expects($this->once()) 49 ->method('all') 50 ->willReturn(new Collection(['tree1' => $tree])); 51 52 $module = $this->createMock(LifespansChartModule::class); 53 $module 54 ->expects($this->once()) 55 ->method('chartUrl') 56 ->willReturn('https://www.example.com'); 57 58 $module_service = $this->createMock(ModuleService::class); 59 $module_service 60 ->expects($this->once()) 61 ->method('findByComponent') 62 ->with(ModuleChartInterface::class) 63 ->willReturn(new Collection([$module])); 64 65 $handler = new RedirectLifeSpanPhp($module_service, $tree_service); 66 67 $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1', 'rootid' => 'X123']); 68 69 $response = $handler->handle($request); 70 71 self::assertSame(StatusCodeInterface::STATUS_MOVED_PERMANENTLY, $response->getStatusCode()); 72 self::assertSame('https://www.example.com', $response->getHeaderLine('Location')); 73 } 74 75 public function testModuleDisabled(): void 76 { 77 $module_service = $this->createMock(ModuleService::class); 78 $module_service 79 ->expects($this->once())->method('findByComponent') 80 ->with(ModuleChartInterface::class) 81 ->willReturn(new Collection()); 82 83 $tree = $this->createMock(Tree::class); 84 85 $tree_service = $this->createMock(TreeService::class); 86 $tree_service 87 ->expects($this->once()) 88 ->method('all') 89 ->willReturn(new Collection(['tree1' => $tree])); 90 91 $handler = new RedirectLifeSpanPhp($module_service, $tree_service); 92 93 $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1', 'rootid' => 'X123']); 94 95 $this->expectException(HttpGoneException::class); 96 97 $handler->handle($request); 98 } 99 100 public function testNoSuchTree(): void 101 { 102 $module_service = $this->createMock(ModuleService::class); 103 104 $tree_service = $this->createMock(TreeService::class); 105 $tree_service 106 ->expects($this->once()) 107 ->method('all') 108 ->willReturn(new Collection([])); 109 110 $handler = new RedirectLifeSpanPhp($module_service, $tree_service); 111 112 $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1', 'rootid' => 'X123']); 113 114 $this->expectException(HttpGoneException::class); 115 116 $handler->handle($request); 117 } 118} 119