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\NoteFactory; 25use Fisharebest\Webtrees\Http\Exceptions\HttpBadRequestException; 26use Fisharebest\Webtrees\Http\Exceptions\HttpGoneException; 27use Fisharebest\Webtrees\Note; 28use Fisharebest\Webtrees\Registry; 29use Fisharebest\Webtrees\Services\TreeService; 30use Fisharebest\Webtrees\TestCase; 31use Fisharebest\Webtrees\Tree; 32use Illuminate\Support\Collection; 33 34/** 35 * @covers \Fisharebest\Webtrees\Http\RequestHandlers\RedirectNotePhp 36 */ 37class RedirectNotePhpTest extends TestCase 38{ 39 protected static bool $uses_database = true; 40 41 public function testRedirect(): void 42 { 43 $tree = $this->createStub(Tree::class); 44 $tree 45 ->method('name') 46 ->willReturn('tree1'); 47 48 $tree_service = $this->createStub(TreeService::class); 49 $tree_service 50 ->expects(self::once()) 51 ->method('all') 52 ->willReturn(new Collection(['tree1' => $tree])); 53 54 $note = $this->createStub(Note::class); 55 $note 56 ->method('url') 57 ->willReturn('https://www.example.com'); 58 59 $note_factory = $this->createStub(NoteFactory::class); 60 $note_factory 61 ->expects(self::once()) 62 ->method('make') 63 ->with('X123', $tree) 64 ->willReturn($note); 65 66 Registry::noteFactory($note_factory); 67 68 $handler = new RedirectNotePhp($tree_service); 69 70 $request = self::createRequest( 71 RequestMethodInterface::METHOD_GET, 72 ['ged' => 'tree1', 'nid' => 'X123'] 73 ); 74 75 $response = $handler->handle($request); 76 77 self::assertSame(StatusCodeInterface::STATUS_MOVED_PERMANENTLY, $response->getStatusCode()); 78 self::assertSame('https://www.example.com', $response->getHeaderLine('Location')); 79 } 80 81 public function testNoSuchRecord(): void 82 { 83 $tree = $this->createStub(Tree::class); 84 85 $tree_service = $this->createStub(TreeService::class); 86 $tree_service 87 ->expects(self::once()) 88 ->method('all') 89 ->willReturn(new Collection([$tree])); 90 91 $handler = new RedirectNotePhp($tree_service); 92 93 $request = self::createRequest( 94 RequestMethodInterface::METHOD_GET, 95 ['ged' => 'tree1', 'nid' => 'X123'] 96 ); 97 98 $this->expectException(HttpGoneException::class); 99 100 $handler->handle($request); 101 } 102 103 public function testMissingXrefParameter(): void 104 { 105 $tree_service = $this->createStub(TreeService::class); 106 107 $handler = new RedirectNotePhp($tree_service); 108 109 $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1']); 110 111 $this->expectException(HttpBadRequestException::class); 112 113 $handler->handle($request); 114 } 115} 116