xref: /webtrees/tests/app/Http/RequestHandlers/RedirectSourcePhpTest.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\SourceFactory;
25use Fisharebest\Webtrees\Http\Exceptions\HttpBadRequestException;
26use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
27use Fisharebest\Webtrees\Registry;
28use Fisharebest\Webtrees\Services\TreeService;
29use Fisharebest\Webtrees\Source;
30use Fisharebest\Webtrees\TestCase;
31use Fisharebest\Webtrees\Tree;
32use Illuminate\Support\Collection;
33
34/**
35 * @covers \Fisharebest\Webtrees\Http\RequestHandlers\RedirectSourcePhp
36 */
37class RedirectSourcePhpTest 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        $source = $this->createStub(Source::class);
56        $source
57            ->method('url')
58            ->willReturn('https://www.example.com');
59
60        $source_factory = $this->createStub(SourceFactory::class);
61        $source_factory
62            ->expects(self::once())
63            ->method('make')
64            ->with('X123', $tree)
65            ->willReturn($source);
66
67        Registry::sourceFactory($source_factory);
68
69        $handler = new RedirectSourcePhp($tree_service);
70
71        $request = self::createRequest(
72            RequestMethodInterface::METHOD_GET,
73            ['ged' => 'tree1', 'sid' => '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 RedirectSourcePhp($tree_service);
96
97        $request = self::createRequest(
98            RequestMethodInterface::METHOD_GET,
99            ['ged' => 'tree1', 'sid' => '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 RedirectSourcePhp($tree_service);
115
116        $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['sid' => '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 RedirectSourcePhp($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