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\InteractiveTreeModule; 28use Fisharebest\Webtrees\Module\PedigreeMapModule; 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(RedirectModulePhp::class)] 38class RedirectModulePhpTest extends TestCase 39{ 40 protected static bool $uses_database = true; 41 42 public function testRedirectPedigreeMap(): 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(self::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(self::once()) 60 ->method('make') 61 ->with('X123', $tree) 62 ->willReturn($individual); 63 64 Registry::individualFactory($individual_factory); 65 66 $module = $this->createMock(PedigreeMapModule::class); 67 $module 68 ->expects(self::once()) 69 ->method('chartUrl') 70 ->willReturn('https://www.example.com'); 71 72 $module_service = $this->createMock(ModuleService::class); 73 $module_service 74 ->expects(self::once()) 75 ->method('findByInterface') 76 ->with(PedigreeMapModule::class) 77 ->willReturn(new Collection([$module])); 78 79 $handler = new RedirectModulePhp($module_service, $tree_service); 80 81 $request = self::createRequest( 82 RequestMethodInterface::METHOD_GET, 83 ['mod' => 'googlemap', 'mod_action' => 'pedigree_map', 'ged' => 'tree1', 'rootid' => 'X123'] 84 ); 85 86 $response = $handler->handle($request); 87 88 self::assertSame(StatusCodeInterface::STATUS_MOVED_PERMANENTLY, $response->getStatusCode()); 89 self::assertSame('https://www.example.com', $response->getHeaderLine('Location')); 90 } 91 92 public function testRedirectInteractiveTree(): void 93 { 94 $tree = $this->createMock(Tree::class); 95 $tree 96 ->method('name') 97 ->willReturn('tree1'); 98 99 $tree_service = $this->createMock(TreeService::class); 100 $tree_service 101 ->expects(self::once()) 102 ->method('all') 103 ->willReturn(new Collection(['tree1' => $tree])); 104 105 $individual = $this->createMock(Individual::class); 106 107 $individual_factory = $this->createMock(IndividualFactory::class); 108 $individual_factory 109 ->expects(self::once()) 110 ->method('make') 111 ->with('X123', $tree) 112 ->willReturn($individual); 113 114 Registry::individualFactory($individual_factory); 115 116 $module = $this->createMock(InteractiveTreeModule::class); 117 $module 118 ->expects(self::once()) 119 ->method('chartUrl') 120 ->willReturn('https://www.example.com'); 121 122 $module_service = $this->createMock(ModuleService::class); 123 $module_service 124 ->expects(self::once()) 125 ->method('findByInterface') 126 ->with(InteractiveTreeModule::class) 127 ->willReturn(new Collection([$module])); 128 129 $handler = new RedirectModulePhp($module_service, $tree_service); 130 131 $request = self::createRequest( 132 RequestMethodInterface::METHOD_GET, 133 ['mod' => 'tree', 'mod_action' => 'treeview', 'ged' => 'tree1', 'rootid' => 'X123'] 134 ); 135 136 $response = $handler->handle($request); 137 138 self::assertSame(StatusCodeInterface::STATUS_MOVED_PERMANENTLY, $response->getStatusCode()); 139 self::assertSame('https://www.example.com', $response->getHeaderLine('Location')); 140 } 141 142 143 public function testNoSuchTree(): void 144 { 145 $module_service = $this->createMock(ModuleService::class); 146 $tree_service = $this->createMock(TreeService::class); 147 $tree_service 148 ->expects(self::once()) 149 ->method('all') 150 ->willReturn(new Collection([])); 151 152 $handler = new RedirectModulePhp($module_service, $tree_service); 153 154 $request = self::createRequest( 155 RequestMethodInterface::METHOD_GET, 156 ['ged' => 'tree1', 'rootid' => 'X123'] 157 ); 158 159 $this->expectException(HttpGoneException::class); 160 161 $handler->handle($request); 162 } 163 164 public function testNoSuchIndividual(): void 165 { 166 $tree = $this->createMock(Tree::class); 167 $tree 168 ->method('name') 169 ->willReturn('tree1'); 170 171 $individual_factory = $this->createMock(IndividualFactory::class); 172 $individual_factory 173 ->expects(self::once()) 174 ->method('make') 175 ->with('X123', $tree) 176 ->willReturn(null); 177 178 Registry::individualFactory($individual_factory); 179 $module_service = $this->createMock(ModuleService::class); 180 $tree_service = $this->createMock(TreeService::class); 181 $tree_service 182 ->expects(self::once()) 183 ->method('all') 184 ->willReturn(new Collection(['tree1' => $tree])); 185 186 $handler = new RedirectModulePhp($module_service, $tree_service); 187 188 $request = self::createRequest( 189 RequestMethodInterface::METHOD_GET, 190 ['ged' => 'tree1', 'rootid' => 'X123'] 191 ); 192 193 $this->expectException(HttpGoneException::class); 194 195 $handler->handle($request); 196 } 197 198 public function testPedigreeMapModuleDisabled(): void 199 { 200 $tree = $this->createMock(Tree::class); 201 $tree 202 ->method('name') 203 ->willReturn('tree1'); 204 205 $tree_service = $this->createMock(TreeService::class); 206 $tree_service 207 ->expects(self::once()) 208 ->method('all') 209 ->willReturn(new Collection(['tree1' => $tree])); 210 211 $individual = $this->createMock(Individual::class); 212 213 $individual_factory = $this->createMock(IndividualFactory::class); 214 $individual_factory 215 ->expects(self::once()) 216 ->method('make') 217 ->with('X123', $tree) 218 ->willReturn($individual); 219 220 Registry::individualFactory($individual_factory); 221 222 $module_service = $this->createMock(ModuleService::class); 223 $module_service 224 ->expects(self::once()) 225 ->method('findByInterface') 226 ->with(PedigreeMapModule::class) 227 ->willReturn(new Collection([])); 228 229 $handler = new RedirectModulePhp($module_service, $tree_service); 230 231 $request = self::createRequest( 232 RequestMethodInterface::METHOD_GET, 233 ['mod' => 'googlemap', 'mod_action' => 'pedigree_map', 'ged' => 'tree1', 'rootid' => 'X123'] 234 ); 235 236 $this->expectException(HttpGoneException::class); 237 238 $handler->handle($request); 239 } 240 241 public function testInteractiveTreeModuleDisabled(): void 242 { 243 $tree = $this->createMock(Tree::class); 244 $tree 245 ->method('name') 246 ->willReturn('tree1'); 247 248 $tree_service = $this->createMock(TreeService::class); 249 $tree_service 250 ->expects(self::once()) 251 ->method('all') 252 ->willReturn(new Collection(['tree1' => $tree])); 253 254 $individual = $this->createMock(Individual::class); 255 256 $individual_factory = $this->createMock(IndividualFactory::class); 257 $individual_factory 258 ->expects(self::once()) 259 ->method('make') 260 ->with('X123', $tree) 261 ->willReturn($individual); 262 263 Registry::individualFactory($individual_factory); 264 265 $module_service = $this->createMock(ModuleService::class); 266 $module_service 267 ->expects(self::once()) 268 ->method('findByInterface') 269 ->with(InteractiveTreeModule::class) 270 ->willReturn(new Collection([])); 271 272 $handler = new RedirectModulePhp($module_service, $tree_service); 273 274 $request = self::createRequest( 275 RequestMethodInterface::METHOD_GET, 276 ['mod' => 'tree', 'mod_action' => 'treeview', 'ged' => 'tree1', 'rootid' => 'X123'] 277 ); 278 279 $this->expectException(HttpGoneException::class); 280 281 $handler->handle($request); 282 } 283} 284