xref: /webtrees/tests/app/Http/RequestHandlers/RedirectFamilyPhpTest.php (revision b11cdcd45131b1585d66693fab363cfeb18c51a4)
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\HttpNotFoundException;
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\RedirectFamilyPhp
36 */
37class RedirectFamilyPhpTest extends TestCase
38{
39    /**
40     * @return void
41     */
42    public function testRedirect(): void
43    {
44        $tree = $this->createStub(Tree::class);
45        $tree
46            ->method('name')
47            ->willReturn('tree1');
48
49        $tree_service = $this->createStub(TreeService::class);
50        $tree_service
51            ->expects(self::once())
52            ->method('all')
53            ->willReturn(new Collection(['tree1' => $tree]));
54
55        $family = $this->createStub(Family::class);
56        $family
57            ->method('url')
58            ->willReturn('https://www.example.com');
59
60        $family_factory = $this->createStub(FamilyFactory::class);
61        $family_factory
62            ->expects(self::once())
63            ->method('make')
64            ->with('X123', $tree)
65            ->willReturn($family);
66
67        Registry::familyFactory($family_factory);
68
69        $handler = new RedirectFamilyPhp($tree_service);
70
71        $request = self::createRequest(
72            RequestMethodInterface::METHOD_GET,
73            ['ged' => 'tree1', 'famid' => 'X123']
74        );
75
76        $response = $handler->handle($request);
77
78        self::assertSame(StatusCodeInterface::STATUS_MOVED_PERMANENTLY, $response->getStatusCode());
79        self::assertSame('https://www.example.com', $response->getHeaderLine('Location'));
80    }
81
82    /**
83     * @return void
84     */
85    public function testNoSuchRecord(): void
86    {
87        $tree = $this->createStub(Tree::class);
88
89        $tree_service = $this->createStub(TreeService::class);
90        $tree_service
91            ->expects(self::once())
92            ->method('all')
93            ->willReturn(new Collection([$tree]));
94
95        $handler = new RedirectFamilyPhp($tree_service);
96
97        $request = self::createRequest(
98            RequestMethodInterface::METHOD_GET,
99            ['ged' => 'tree1', 'famid' => 'X123']
100        );
101
102        $this->expectException(HttpNotFoundException::class);
103
104        $handler->handle($request);
105    }
106
107    /**
108     * @return void
109     */
110    public function testMissingTreeParameter(): void
111    {
112        $tree_service = $this->createStub(TreeService::class);
113
114        $handler = new RedirectFamilyPhp($tree_service);
115
116        $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['famid' => 'X123']);
117
118        $this->expectException(HttpBadRequestException::class);
119
120        $handler->handle($request);
121    }
122
123    /**
124     * @return void
125     */
126    public function testMissingXrefParameter(): void
127    {
128        $tree_service = $this->createStub(TreeService::class);
129
130        $handler = new RedirectFamilyPhp($tree_service);
131
132        $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1']);
133
134        $this->expectException(HttpBadRequestException::class);
135
136        $handler->handle($request);
137    }
138}
139