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