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