xref: /webtrees/tests/app/Http/RequestHandlers/RedirectMediaViewerPhpTest.php (revision 8634febf6487fcb3ee7b59e88f828071a22cba22)
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\MediaFactory;
25use Fisharebest\Webtrees\Http\Exceptions\HttpBadRequestException;
26use Fisharebest\Webtrees\Http\Exceptions\HttpNotFoundException;
27use Fisharebest\Webtrees\Media;
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\RedirectMediaViewerPhp
36 */
37class RedirectMediaViewerPhpTest 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        $media = $this->createStub(Media::class);
56        $media
57            ->method('url')
58            ->willReturn('https://www.example.com');
59
60        $media_factory = $this->createStub(MediaFactory::class);
61        $media_factory
62            ->expects(self::once())
63            ->method('make')
64            ->with('X123', $tree)
65            ->willReturn($media);
66
67        Registry::mediaFactory($media_factory);
68
69        $handler = new RedirectMediaViewerPhp($tree_service);
70
71        $request = self::createRequest(
72            RequestMethodInterface::METHOD_GET,
73            ['ged' => 'tree1', 'mid' => '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 RedirectMediaViewerPhp($tree_service);
96
97        $request = self::createRequest(
98            RequestMethodInterface::METHOD_GET,
99            ['ged' => 'tree1', 'mid' => 'X123']
100        );
101
102        $this->expectException(HttpNotFoundException::class);
103
104        $handler->handle($request);
105    }
106
107    /**
108     * @return void
109     */
110    public function testNoSuchTree(): void
111    {
112        $tree_service = $this->createStub(TreeService::class);
113        $tree_service
114            ->expects(self::once())
115            ->method('all')
116            ->willReturn(new Collection([]));
117
118        $handler = new RedirectMediaViewerPhp($tree_service);
119
120        $request = self::createRequest(
121            RequestMethodInterface::METHOD_GET,
122            ['ged' => 'tree1', 'mid' => 'X123']
123        );
124
125        $this->expectException(HttpNotFoundException::class);
126
127        $handler->handle($request);
128    }
129
130    /**
131     * @return void
132     */
133    public function testMissingTreeParameter(): void
134    {
135        $tree_service = $this->createStub(TreeService::class);
136
137        $handler = new RedirectFamilyPhp($tree_service);
138
139        $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['mid' => 'X123']);
140
141        $this->expectException(HttpBadRequestException::class);
142
143        $handler->handle($request);
144    }
145
146    /**
147     * @return void
148     */
149    public function testMissingXrefParameter(): void
150    {
151        $tree_service = $this->createStub(TreeService::class);
152
153        $handler = new RedirectFamilyPhp($tree_service);
154
155        $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1']);
156
157        $this->expectException(HttpBadRequestException::class);
158
159        $handler->handle($request);
160    }
161}
162