xref: /webtrees/tests/app/Http/RequestHandlers/RedirectIndividualPhpTest.php (revision 9660ebfefe26982c97c5f787230b1c6c5e61c1e1)
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\IndividualFactory;
25use Fisharebest\Webtrees\Http\Exceptions\HttpBadRequestException;
26use Fisharebest\Webtrees\Http\Exceptions\HttpGoneException;
27use Fisharebest\Webtrees\Individual;
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(RedirectIndividualPhp::class)]
36class RedirectIndividualPhpTest 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(self::once())
50            ->method('all')
51            ->willReturn(new Collection(['tree1' => $tree]));
52
53        $individual = $this->createMock(Individual::class);
54        $individual
55            ->method('url')
56            ->willReturn('https://www.example.com');
57
58        $individual_factory = $this->createMock(IndividualFactory::class);
59        $individual_factory
60            ->expects(self::once())
61            ->method('make')
62            ->with('X123', $tree)
63            ->willReturn($individual);
64
65        Registry::individualFactory($individual_factory);
66
67        $handler = new RedirectIndividualPhp($tree_service);
68
69        $request = self::createRequest(
70            RequestMethodInterface::METHOD_GET,
71            ['ged' => 'tree1', 'pid' => '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->createMock(Tree::class);
83
84        $tree_service = $this->createMock(TreeService::class);
85        $tree_service
86            ->expects(self::once())
87            ->method('all')
88            ->willReturn(new Collection([$tree]));
89
90        $handler = new RedirectIndividualPhp($tree_service);
91
92        $request = self::createRequest(
93            RequestMethodInterface::METHOD_GET,
94            ['ged' => 'tree1', 'pid' => '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->createMock(TreeService::class);
105        $tree_service
106            ->expects(self::once())
107            ->method('all')
108            ->willReturn(new Collection([]));
109
110        $handler = new RedirectIndividualPhp($tree_service);
111
112        $request = self::createRequest(
113            RequestMethodInterface::METHOD_GET,
114            ['ged' => 'tree1', 'pid' => 'X123']
115        );
116
117        $this->expectException(HttpGoneException::class);
118
119        $handler->handle($request);
120    }
121
122    public function testMissingXrefParameter(): void
123    {
124        $tree_service = $this->createMock(TreeService::class);
125
126        $handler = new RedirectFamilyPhp($tree_service);
127
128        $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1']);
129
130        $this->expectException(HttpBadRequestException::class);
131
132        $handler->handle($request);
133    }
134}
135