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