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\MediaFactory; 25use Fisharebest\Webtrees\Http\Exceptions\HttpBadRequestException; 26use Fisharebest\Webtrees\Http\Exceptions\HttpGoneException; 27use Fisharebest\Webtrees\Media; 28use Fisharebest\Webtrees\Registry; 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(RedirectMediaViewerPhp::class)] 36class RedirectMediaViewerPhpTest extends TestCase 37{ 38 protected static bool $uses_database = true; 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 $media = $this->createStub(Media::class); 54 $media 55 ->method('url') 56 ->willReturn('https://www.example.com'); 57 58 $media_factory = $this->createStub(MediaFactory::class); 59 $media_factory 60 ->expects(self::once()) 61 ->method('make') 62 ->with('X123', $tree) 63 ->willReturn($media); 64 65 Registry::mediaFactory($media_factory); 66 67 $handler = new RedirectMediaViewerPhp($tree_service); 68 69 $request = self::createRequest( 70 RequestMethodInterface::METHOD_GET, 71 ['ged' => 'tree1', 'mid' => 'X123'] 72 ); 73 74 $response = $handler->handle($request); 75 76 self::assertSame(StatusCodeInterface::STATUS_MOVED_PERMANENTLY, $response->getStatusCode()); 77 self::assertSame('https://www.example.com', $response->getHeaderLine('Location')); 78 } 79 80 public function testNoSuchRecord(): void 81 { 82 $tree = $this->createStub(Tree::class); 83 84 $tree_service = $this->createStub(TreeService::class); 85 $tree_service 86 ->expects(self::once()) 87 ->method('all') 88 ->willReturn(new Collection([$tree])); 89 90 $handler = new RedirectMediaViewerPhp($tree_service); 91 92 $request = self::createRequest( 93 RequestMethodInterface::METHOD_GET, 94 ['ged' => 'tree1', 'mid' => 'X123'] 95 ); 96 97 $this->expectException(HttpGoneException::class); 98 99 $handler->handle($request); 100 } 101 102 public function testNoSuchTree(): void 103 { 104 $tree_service = $this->createStub(TreeService::class); 105 $tree_service 106 ->expects(self::once()) 107 ->method('all') 108 ->willReturn(new Collection([])); 109 110 $handler = new RedirectMediaViewerPhp($tree_service); 111 112 $request = self::createRequest( 113 RequestMethodInterface::METHOD_GET, 114 ['ged' => 'tree1', 'mid' => 'X123'] 115 ); 116 117 $this->expectException(HttpGoneException::class); 118 119 $handler->handle($request); 120 } 121 122 public function testMissingTreeParameter(): void 123 { 124 $tree_service = $this->createStub(TreeService::class); 125 126 $handler = new RedirectFamilyPhp($tree_service); 127 128 $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['mid' => 'X123']); 129 130 $this->expectException(HttpBadRequestException::class); 131 132 $handler->handle($request); 133 } 134 135 public function testMissingXrefParameter(): void 136 { 137 $tree_service = $this->createStub(TreeService::class); 138 139 $handler = new RedirectFamilyPhp($tree_service); 140 141 $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1']); 142 143 $this->expectException(HttpBadRequestException::class); 144 145 $handler->handle($request); 146 } 147} 148