1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2022 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\Http\Exceptions\HttpNotFoundException; 25use Fisharebest\Webtrees\Module\LifespansChartModule; 26use Fisharebest\Webtrees\Services\ModuleService; 27use Fisharebest\Webtrees\Services\TreeService; 28use Fisharebest\Webtrees\TestCase; 29use Fisharebest\Webtrees\Tree; 30use Illuminate\Support\Collection; 31 32/** 33 * @covers \Fisharebest\Webtrees\Http\RequestHandlers\RedirectLifeSpanPhp 34 */ 35class RedirectLifeSpanPhpTest extends TestCase 36{ 37 /** 38 * @return void 39 */ 40 public function testRedirect(): void 41 { 42 $tree = $this->createStub(Tree::class); 43 $tree 44 ->method('name') 45 ->willReturn('tree1'); 46 47 $tree_service = $this->createStub(TreeService::class); 48 $tree_service 49 ->expects(self::once()) 50 ->method('all') 51 ->willReturn(new Collection(['tree1' => $tree])); 52 53 $module = $this->createStub(LifespansChartModule::class); 54 $module 55 ->expects(self::once()) 56 ->method('chartUrl') 57 ->willReturn('https://www.example.com'); 58 59 $module_service = $this->createStub(ModuleService::class); 60 $module_service 61 ->expects(self::once()) 62 ->method('findByInterface') 63 ->with(LifespansChartModule::class) 64 ->willReturn(new Collection([$module])); 65 66 $handler = new RedirectLifeSpanPhp($module_service, $tree_service); 67 68 $request = self::createRequest( 69 RequestMethodInterface::METHOD_GET, 70 ['ged' => 'tree1', 'rootid' => 'X123'] 71 ); 72 73 $response = $handler->handle($request); 74 75 self::assertSame(StatusCodeInterface::STATUS_MOVED_PERMANENTLY, $response->getStatusCode()); 76 self::assertSame('https://www.example.com', $response->getHeaderLine('Location')); 77 } 78 79 /** 80 * @return void 81 */ 82 public function testModuleDisabled(): void 83 { 84 $module_service = $this->createStub(ModuleService::class); 85 $module_service 86 ->expects(self::once())->method('findByInterface') 87 ->with(LifespansChartModule::class) 88 ->willReturn(new Collection()); 89 90 $tree = $this->createStub(Tree::class); 91 92 $tree_service = $this->createStub(TreeService::class); 93 $tree_service 94 ->expects(self::once()) 95 ->method('all') 96 ->willReturn(new Collection([$tree])); 97 98 $handler = new RedirectLifeSpanPhp($module_service, $tree_service); 99 100 $request = self::createRequest( 101 RequestMethodInterface::METHOD_GET, 102 ['ged' => 'tree1', 'rootid' => 'X123'] 103 ); 104 105 $this->expectException(HttpNotFoundException::class); 106 107 $handler->handle($request); 108 } 109 110 /** 111 * @return void 112 */ 113 public function testNoSuchTree(): void 114 { 115 $module = $this->createStub(LifespansChartModule::class); 116 117 $module_service = $this->createStub(ModuleService::class); 118 $module_service 119 ->expects(self::once()) 120 ->method('findByInterface') 121 ->with(LifespansChartModule::class) 122 ->willReturn(new Collection([$module])); 123 124 $tree_service = $this->createStub(TreeService::class); 125 $tree_service 126 ->expects(self::once()) 127 ->method('all') 128 ->willReturn(new Collection([])); 129 130 $handler = new RedirectLifeSpanPhp($module_service, $tree_service); 131 132 $request = self::createRequest( 133 RequestMethodInterface::METHOD_GET, 134 ['ged' => 'tree1', 'rootid' => 'X123'] 135 ); 136 137 $this->expectException(HttpNotFoundException::class); 138 139 $handler->handle($request); 140 } 141} 142