xref: /webtrees/tests/app/Http/RequestHandlers/RedirectFamilyPhpTest.php (revision 14bda9c81d9234b172dcb99ab8a9fd23a17fe59e)
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\FamilyFactory;
25use Fisharebest\Webtrees\Family;
26use Fisharebest\Webtrees\Http\Exceptions\HttpBadRequestException;
27use Fisharebest\Webtrees\Http\Exceptions\HttpGoneException;
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(RedirectFamilyPhp::class)]
36class RedirectFamilyPhpTest 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        $family = $this->createMock(Family::class);
54        $family
55            ->method('url')
56            ->willReturn('https://www.example.com');
57
58        $family_factory = $this->createMock(FamilyFactory::class);
59        $family_factory
60            ->expects($this->once())
61            ->method('make')
62            ->with('X123', $tree)
63            ->willReturn($family);
64
65        Registry::familyFactory($family_factory);
66
67        $handler = new RedirectFamilyPhp($tree_service);
68
69        $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1', 'famid' => 'X123']);
70
71        $response = $handler->handle($request);
72
73        self::assertSame(StatusCodeInterface::STATUS_MOVED_PERMANENTLY, $response->getStatusCode());
74        self::assertSame('https://www.example.com', $response->getHeaderLine('Location'));
75    }
76
77    public function testNoSuchRecord(): void
78    {
79        $tree = $this->createMock(Tree::class);
80
81        $tree_service = $this->createMock(TreeService::class);
82        $tree_service
83            ->expects($this->once())
84            ->method('all')
85            ->willReturn(new Collection(['tree1' => $tree]));
86
87        $handler = new RedirectFamilyPhp($tree_service);
88
89        $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1', 'famid' => 'X123']);
90
91        $this->expectException(HttpGoneException::class);
92
93        $handler->handle($request);
94    }
95
96    public function testMissingXrefParameter(): void
97    {
98        $tree_service = $this->createMock(TreeService::class);
99
100        $handler = new RedirectFamilyPhp($tree_service);
101
102        $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1']);
103
104        $this->expectException(HttpBadRequestException::class);
105
106        $handler->handle($request);
107    }
108}
109