13340ecd2SGreg Roach<?php 23340ecd2SGreg Roach 33340ecd2SGreg Roach/** 43340ecd2SGreg Roach * webtrees: online genealogy 5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 63340ecd2SGreg Roach * This program is free software: you can redistribute it and/or modify 73340ecd2SGreg Roach * it under the terms of the GNU General Public License as published by 83340ecd2SGreg Roach * the Free Software Foundation, either version 3 of the License, or 93340ecd2SGreg Roach * (at your option) any later version. 103340ecd2SGreg Roach * This program is distributed in the hope that it will be useful, 113340ecd2SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 123340ecd2SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 133340ecd2SGreg Roach * GNU General Public License for more details. 143340ecd2SGreg Roach * You should have received a copy of the GNU General Public License 153340ecd2SGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>. 163340ecd2SGreg Roach */ 173340ecd2SGreg Roach 183340ecd2SGreg Roachdeclare(strict_types=1); 193340ecd2SGreg Roach 203340ecd2SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers; 213340ecd2SGreg Roach 223340ecd2SGreg Roachuse Fig\Http\Message\RequestMethodInterface; 233340ecd2SGreg Roachuse Fig\Http\Message\StatusCodeInterface; 243340ecd2SGreg Roachuse Fisharebest\Webtrees\Factories\IndividualFactory; 2515c4f62cSGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpGoneException; 263340ecd2SGreg Roachuse Fisharebest\Webtrees\Individual; 273340ecd2SGreg Roachuse Fisharebest\Webtrees\Module\HourglassChartModule; 28*00ef1d3aSGreg Roachuse Fisharebest\Webtrees\Module\ModuleChartInterface; 293340ecd2SGreg Roachuse Fisharebest\Webtrees\Registry; 303340ecd2SGreg Roachuse Fisharebest\Webtrees\Services\ModuleService; 313340ecd2SGreg Roachuse Fisharebest\Webtrees\Services\TreeService; 323340ecd2SGreg Roachuse Fisharebest\Webtrees\TestCase; 333340ecd2SGreg Roachuse Fisharebest\Webtrees\Tree; 343340ecd2SGreg Roachuse Illuminate\Support\Collection; 35202c018bSGreg Roachuse PHPUnit\Framework\Attributes\CoversClass; 363340ecd2SGreg Roach 37202c018bSGreg Roach#[CoversClass(RedirectHourGlassPhp::class)] 383340ecd2SGreg Roachclass RedirectHourGlassPhpTest extends TestCase 393340ecd2SGreg Roach{ 40a26ec5edSGreg Roach protected static bool $uses_database = true; 41a26ec5edSGreg Roach 423340ecd2SGreg Roach public function testRedirect(): void 433340ecd2SGreg Roach { 4462ff2f18SGreg Roach $tree = $this->createMock(Tree::class); 453340ecd2SGreg Roach $tree 463340ecd2SGreg Roach ->method('name') 473340ecd2SGreg Roach ->willReturn('tree1'); 483340ecd2SGreg Roach 4962ff2f18SGreg Roach $tree_service = $this->createMock(TreeService::class); 503340ecd2SGreg Roach $tree_service 51*00ef1d3aSGreg Roach ->expects($this->once()) 523340ecd2SGreg Roach ->method('all') 533340ecd2SGreg Roach ->willReturn(new Collection(['tree1' => $tree])); 543340ecd2SGreg Roach 5562ff2f18SGreg Roach $individual = $this->createMock(Individual::class); 563340ecd2SGreg Roach 5762ff2f18SGreg Roach $individual_factory = $this->createMock(IndividualFactory::class); 583340ecd2SGreg Roach $individual_factory 59*00ef1d3aSGreg Roach ->expects($this->once()) 603340ecd2SGreg Roach ->method('make') 613340ecd2SGreg Roach ->with('X123', $tree) 623340ecd2SGreg Roach ->willReturn($individual); 633340ecd2SGreg Roach 643340ecd2SGreg Roach Registry::individualFactory($individual_factory); 653340ecd2SGreg Roach 6662ff2f18SGreg Roach $module = $this->createMock(HourglassChartModule::class); 673340ecd2SGreg Roach $module 68*00ef1d3aSGreg Roach ->expects($this->once()) 693340ecd2SGreg Roach ->method('chartUrl') 703340ecd2SGreg Roach ->willReturn('https://www.example.com'); 713340ecd2SGreg Roach 7262ff2f18SGreg Roach $module_service = $this->createMock(ModuleService::class); 733340ecd2SGreg Roach $module_service 74*00ef1d3aSGreg Roach ->expects($this->once()) 75*00ef1d3aSGreg Roach ->method('findByComponent') 76*00ef1d3aSGreg Roach ->with(ModuleChartInterface::class) 773340ecd2SGreg Roach ->willReturn(new Collection([$module])); 783340ecd2SGreg Roach 793340ecd2SGreg Roach $handler = new RedirectHourGlassPhp($module_service, $tree_service); 803340ecd2SGreg Roach 81*00ef1d3aSGreg Roach $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1', 'rootid' => 'X123']); 823340ecd2SGreg Roach 833340ecd2SGreg Roach $response = $handler->handle($request); 843340ecd2SGreg Roach 853340ecd2SGreg Roach self::assertSame(StatusCodeInterface::STATUS_MOVED_PERMANENTLY, $response->getStatusCode()); 863340ecd2SGreg Roach self::assertSame('https://www.example.com', $response->getHeaderLine('Location')); 873340ecd2SGreg Roach } 883340ecd2SGreg Roach 893340ecd2SGreg Roach public function testModuleDisabled(): void 903340ecd2SGreg Roach { 9162ff2f18SGreg Roach $module_service = $this->createMock(ModuleService::class); 923340ecd2SGreg Roach $module_service 93*00ef1d3aSGreg Roach ->expects($this->once())->method('findByComponent') 94*00ef1d3aSGreg Roach ->with(ModuleChartInterface::class) 953340ecd2SGreg Roach ->willReturn(new Collection()); 963340ecd2SGreg Roach 9762ff2f18SGreg Roach $tree = $this->createMock(Tree::class); 983340ecd2SGreg Roach 9962ff2f18SGreg Roach $tree_service = $this->createMock(TreeService::class); 1003340ecd2SGreg Roach $tree_service 101*00ef1d3aSGreg Roach ->expects($this->once()) 1023340ecd2SGreg Roach ->method('all') 103*00ef1d3aSGreg Roach ->willReturn(new Collection(['tree1' => $tree])); 1043340ecd2SGreg Roach 1053340ecd2SGreg Roach $handler = new RedirectHourGlassPhp($module_service, $tree_service); 1063340ecd2SGreg Roach 107*00ef1d3aSGreg Roach $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1', 'rootid' => 'X123']); 1083340ecd2SGreg Roach 10915c4f62cSGreg Roach $this->expectException(HttpGoneException::class); 1103340ecd2SGreg Roach 1113340ecd2SGreg Roach $handler->handle($request); 1123340ecd2SGreg Roach } 1133340ecd2SGreg Roach 1143340ecd2SGreg Roach public function testNoSuchTree(): void 1153340ecd2SGreg Roach { 11662ff2f18SGreg Roach $module_service = $this->createMock(ModuleService::class); 1173340ecd2SGreg Roach 11862ff2f18SGreg Roach $tree_service = $this->createMock(TreeService::class); 1193340ecd2SGreg Roach $tree_service 120*00ef1d3aSGreg Roach ->expects($this->once()) 1213340ecd2SGreg Roach ->method('all') 1223340ecd2SGreg Roach ->willReturn(new Collection([])); 1233340ecd2SGreg Roach 1243340ecd2SGreg Roach $handler = new RedirectHourGlassPhp($module_service, $tree_service); 1253340ecd2SGreg Roach 126*00ef1d3aSGreg Roach $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1', 'rootid' => 'X123']); 1273340ecd2SGreg Roach 12815c4f62cSGreg Roach $this->expectException(HttpGoneException::class); 1293340ecd2SGreg Roach 1303340ecd2SGreg Roach $handler->handle($request); 1313340ecd2SGreg Roach } 1323340ecd2SGreg Roach} 133