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