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\Http\Exceptions\HttpGoneException; 25use Fisharebest\Webtrees\Module\BranchesListModule; 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\RedirectBranchesPhp 34 */ 35class RedirectBranchesPhpTest extends TestCase 36{ 37 protected static bool $uses_database = true; 38 39 public function testRedirect(): void 40 { 41 $tree = $this->createStub(Tree::class); 42 $tree 43 ->method('name') 44 ->willReturn('tree1'); 45 46 $tree_service = $this->createStub(TreeService::class); 47 $tree_service 48 ->expects(self::once()) 49 ->method('all') 50 ->willReturn(new Collection(['tree1' => $tree])); 51 52 $module = $this->createStub(BranchesListModule::class); 53 54 $module_service = $this->createStub(ModuleService::class); 55 $module_service 56 ->expects(self::once()) 57 ->method('findByInterface') 58 ->with(BranchesListModule::class) 59 ->willReturn(new Collection([$module])); 60 61 $handler = new RedirectBranchesPhp($module_service, $tree_service); 62 63 $request = self::createRequest( 64 RequestMethodInterface::METHOD_GET, 65 ['ged' => 'tree1', 'surname' => 'XYZ'], 66 [], 67 [], 68 ['base_url' => 'https://www.example.com'] 69 ); 70 71 $response = $handler->handle($request); 72 73 self::assertSame(StatusCodeInterface::STATUS_MOVED_PERMANENTLY, $response->getStatusCode()); 74 self::assertSame( 75 'https://www.example.com/index.php?route=%2F%2FPage%2Ftree1&soundex_dm=&soundex_std=&surname=XYZ', 76 $response->getHeaderLine('Location') 77 ); 78 } 79 80 public function testModuleDisabled(): void 81 { 82 $module_service = $this->createStub(ModuleService::class); 83 $module_service 84 ->expects(self::once())->method('findByInterface') 85 ->with(BranchesListModule::class) 86 ->willReturn(new Collection()); 87 88 $tree = $this->createStub(Tree::class); 89 90 $tree_service = $this->createStub(TreeService::class); 91 $tree_service 92 ->expects(self::once()) 93 ->method('all') 94 ->willReturn(new Collection([$tree])); 95 96 $handler = new RedirectBranchesPhp($module_service, $tree_service); 97 98 $request = self::createRequest( 99 RequestMethodInterface::METHOD_GET, 100 ['ged' => 'tree1', 'surname' => 'XYZ'] 101 ); 102 103 $this->expectException(HttpGoneException::class); 104 105 $handler->handle($request); 106 } 107 108 public function testNoSuchTree(): void 109 { 110 $module = $this->createStub(BranchesListModule::class); 111 112 $module_service = $this->createStub(ModuleService::class); 113 $module_service 114 ->expects(self::once()) 115 ->method('findByInterface') 116 ->with(BranchesListModule::class) 117 ->willReturn(new Collection([$module])); 118 119 $tree_service = $this->createStub(TreeService::class); 120 $tree_service 121 ->expects(self::once()) 122 ->method('all') 123 ->willReturn(new Collection([])); 124 125 $handler = new RedirectBranchesPhp($module_service, $tree_service); 126 127 $request = self::createRequest( 128 RequestMethodInterface::METHOD_GET, 129 ['ged' => 'tree1', 'surname' => 'XYZ'] 130 ); 131 132 $this->expectException(HttpGoneException::class); 133 134 $handler->handle($request); 135 } 136} 137