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; 25*00ef1d3aSGreg Roachuse Fisharebest\Webtrees\GuestUser; 2615c4f62cSGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpGoneException; 273340ecd2SGreg Roachuse Fisharebest\Webtrees\Individual; 28*00ef1d3aSGreg Roachuse Fisharebest\Webtrees\Module\ModuleChartInterface; 29*00ef1d3aSGreg Roachuse Fisharebest\Webtrees\Module\ModuleListInterface; 303340ecd2SGreg Roachuse Fisharebest\Webtrees\Module\TimelineChartModule; 313340ecd2SGreg Roachuse Fisharebest\Webtrees\Registry; 323340ecd2SGreg Roachuse Fisharebest\Webtrees\Services\ModuleService; 333340ecd2SGreg Roachuse Fisharebest\Webtrees\Services\TreeService; 343340ecd2SGreg Roachuse Fisharebest\Webtrees\TestCase; 353340ecd2SGreg Roachuse Fisharebest\Webtrees\Tree; 363340ecd2SGreg Roachuse Illuminate\Support\Collection; 37202c018bSGreg Roachuse PHPUnit\Framework\Attributes\CoversClass; 383340ecd2SGreg Roach 39202c018bSGreg Roach#[CoversClass(RedirectTimeLinePhp::class)] 403340ecd2SGreg Roachclass RedirectTimeLinePhpTest extends TestCase 413340ecd2SGreg Roach{ 42a26ec5edSGreg Roach protected static bool $uses_database = true; 43a26ec5edSGreg Roach 443340ecd2SGreg Roach public function testRedirect(): void 453340ecd2SGreg Roach { 4662ff2f18SGreg Roach $tree = $this->createMock(Tree::class); 473340ecd2SGreg Roach $tree 483340ecd2SGreg Roach ->method('name') 493340ecd2SGreg Roach ->willReturn('tree1'); 503340ecd2SGreg Roach 5162ff2f18SGreg Roach $tree_service = $this->createMock(TreeService::class); 523340ecd2SGreg Roach $tree_service 53*00ef1d3aSGreg Roach ->expects($this->once()) 543340ecd2SGreg Roach ->method('all') 553340ecd2SGreg Roach ->willReturn(new Collection(['tree1' => $tree])); 563340ecd2SGreg Roach 5762ff2f18SGreg Roach $individual = $this->createMock(Individual::class); 583340ecd2SGreg Roach 5962ff2f18SGreg Roach $individual_factory = $this->createMock(IndividualFactory::class); 603340ecd2SGreg Roach $individual_factory 61*00ef1d3aSGreg Roach ->expects($this->once()) 623340ecd2SGreg Roach ->method('make') 633340ecd2SGreg Roach ->with('X123', $tree) 643340ecd2SGreg Roach ->willReturn($individual); 653340ecd2SGreg Roach 663340ecd2SGreg Roach Registry::individualFactory($individual_factory); 673340ecd2SGreg Roach 6862ff2f18SGreg Roach $module = $this->createMock(TimelineChartModule::class); 693340ecd2SGreg Roach $module 70*00ef1d3aSGreg Roach ->expects($this->once()) 713340ecd2SGreg Roach ->method('chartUrl') 723340ecd2SGreg Roach ->willReturn('https://www.example.com'); 733340ecd2SGreg Roach 7462ff2f18SGreg Roach $module_service = $this->createMock(ModuleService::class); 753340ecd2SGreg Roach $module_service 76*00ef1d3aSGreg Roach ->expects($this->once()) 77*00ef1d3aSGreg Roach ->method('findByComponent') 78*00ef1d3aSGreg Roach ->with(ModuleChartInterface::class) 793340ecd2SGreg Roach ->willReturn(new Collection([$module])); 803340ecd2SGreg Roach 813340ecd2SGreg Roach $handler = new RedirectTimeLinePhp($module_service, $tree_service); 823340ecd2SGreg Roach 83*00ef1d3aSGreg Roach $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1', 'pids' => ['X123']]); 843340ecd2SGreg Roach 853340ecd2SGreg Roach $response = $handler->handle($request); 863340ecd2SGreg Roach 873340ecd2SGreg Roach self::assertSame(StatusCodeInterface::STATUS_MOVED_PERMANENTLY, $response->getStatusCode()); 883340ecd2SGreg Roach self::assertSame('https://www.example.com', $response->getHeaderLine('Location')); 893340ecd2SGreg Roach } 903340ecd2SGreg Roach 913340ecd2SGreg Roach public function testModuleDisabled(): void 923340ecd2SGreg Roach { 9362ff2f18SGreg Roach $tree = $this->createMock(Tree::class); 94*00ef1d3aSGreg Roach $tree 95*00ef1d3aSGreg Roach ->method('name') 96*00ef1d3aSGreg Roach ->willReturn('tree1'); 973340ecd2SGreg Roach 9862ff2f18SGreg Roach $tree_service = $this->createMock(TreeService::class); 993340ecd2SGreg Roach $tree_service 100*00ef1d3aSGreg Roach ->expects($this->once()) 1013340ecd2SGreg Roach ->method('all') 102*00ef1d3aSGreg Roach ->willReturn(new Collection(['tree1' => $tree])); 103*00ef1d3aSGreg Roach 104*00ef1d3aSGreg Roach $module_service = $this->createMock(ModuleService::class); 105*00ef1d3aSGreg Roach $module_service 106*00ef1d3aSGreg Roach ->method('findByComponent') 107*00ef1d3aSGreg Roach ->with(ModuleChartInterface::class, $tree, new GuestUser()) 108*00ef1d3aSGreg Roach ->willReturn(new Collection()); 1093340ecd2SGreg Roach 1103340ecd2SGreg Roach $handler = new RedirectTimeLinePhp($module_service, $tree_service); 1113340ecd2SGreg Roach 112*00ef1d3aSGreg Roach $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1', 'pids' => ['X123']]); 1133340ecd2SGreg Roach 11415c4f62cSGreg Roach $this->expectException(HttpGoneException::class); 1153340ecd2SGreg Roach 1163340ecd2SGreg Roach $handler->handle($request); 1173340ecd2SGreg Roach } 1183340ecd2SGreg Roach 1193340ecd2SGreg Roach public function testNoSuchTree(): void 1203340ecd2SGreg Roach { 12162ff2f18SGreg Roach $module_service = $this->createMock(ModuleService::class); 1223340ecd2SGreg Roach 12362ff2f18SGreg Roach $tree_service = $this->createMock(TreeService::class); 1243340ecd2SGreg Roach $tree_service 125*00ef1d3aSGreg Roach ->expects($this->once()) 1263340ecd2SGreg Roach ->method('all') 1273340ecd2SGreg Roach ->willReturn(new Collection([])); 1283340ecd2SGreg Roach 1293340ecd2SGreg Roach $handler = new RedirectTimeLinePhp($module_service, $tree_service); 1303340ecd2SGreg Roach 1313340ecd2SGreg Roach $request = self::createRequest( 1323340ecd2SGreg Roach RequestMethodInterface::METHOD_GET, 1333340ecd2SGreg Roach ['ged' => 'tree1', 'pids' => ['X123']] 1343340ecd2SGreg Roach ); 1353340ecd2SGreg Roach 13615c4f62cSGreg Roach $this->expectException(HttpGoneException::class); 1373340ecd2SGreg Roach 1383340ecd2SGreg Roach $handler->handle($request); 1393340ecd2SGreg Roach } 1403340ecd2SGreg Roach} 141