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\FamilyBookChartModule; 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(RedirectFamilyBookPhp::class)] 37class RedirectFamilyBookPhpTest 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 $Compact_chart = $this->createMock(FamilyBookChartModule::class); 66 $Compact_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(FamilyBookChartModule::class) 76 ->willReturn(new Collection([$Compact_chart])); 77 78 $handler = new RedirectFamilyBookPhp($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(FamilyBookChartModule::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 $handler = new RedirectFamilyBookPhp($module_service, $tree_service); 103 104 $request = self::createRequest( 105 RequestMethodInterface::METHOD_GET, 106 ['ged' => 'tree1', 'rootid' => 'X123'] 107 ); 108 109 $this->expectException(HttpGoneException::class); 110 111 $handler->handle($request); 112 } 113 114 public function testNoSuchTree(): void 115 { 116 $module = $this->createMock(FamilyBookChartModule::class); 117 118 $module_service = $this->createMock(ModuleService::class); 119 $module_service 120 ->expects(self::once()) 121 ->method('findByInterface') 122 ->with(FamilyBookChartModule::class) 123 ->willReturn(new Collection([$module])); 124 125 $tree_service = $this->createMock(TreeService::class); 126 $tree_service 127 ->expects(self::once()) 128 ->method('all') 129 ->willReturn(new Collection([])); 130 131 $handler = new RedirectFamilyBookPhp($module_service, $tree_service); 132 133 $request = self::createRequest( 134 RequestMethodInterface::METHOD_GET, 135 ['ged' => 'tree1', 'rootid' => 'X123'] 136 ); 137 138 $this->expectException(HttpGoneException::class); 139 140 $handler->handle($request); 141 } 142} 143