xref: /webtrees/tests/app/Http/RequestHandlers/RedirectMediaViewerPhpTest.php (revision 1270d2767576ed4a83917769b0ee3613e3b010bf)
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\HttpGoneException;
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;
33use PHPUnit\Framework\Attributes\CoversClass;
34
35#[CoversClass(RedirectMediaViewerPhp::class)]
36class RedirectMediaViewerPhpTest 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        $media = $this->createMock(Media::class);
54        $media
55            ->method('url')
56            ->willReturn('https://www.example.com');
57
58        $media_factory = $this->createMock(MediaFactory::class);
59        $media_factory
60            ->expects($this->once())
61            ->method('make')
62            ->with('X123', $tree)
63            ->willReturn($media);
64
65        Registry::mediaFactory($media_factory);
66
67        $handler = new RedirectMediaViewerPhp($tree_service);
68
69        $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1', 'mid' => '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 RedirectMediaViewerPhp($tree_service);
88
89        $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1', 'mid' => 'X123']);
90
91        $this->expectException(HttpGoneException::class);
92
93        $handler->handle($request);
94    }
95
96    public function testNoSuchTree(): void
97    {
98        $tree_service = $this->createMock(TreeService::class);
99        $tree_service
100            ->expects($this->once())
101            ->method('all')
102            ->willReturn(new Collection([]));
103
104        $handler = new RedirectMediaViewerPhp($tree_service);
105
106        $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1', 'mid' => 'X123']);
107
108        $this->expectException(HttpGoneException::class);
109
110        $handler->handle($request);
111    }
112
113    public function testMissingTreeParameter(): void
114    {
115        $tree_service = $this->createMock(TreeService::class);
116
117        $handler = new RedirectFamilyPhp($tree_service);
118
119        $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['mid' => 'X123']);
120
121        $this->expectException(HttpBadRequestException::class);
122
123        $handler->handle($request);
124    }
125
126    public function testMissingXrefParameter(): void
127    {
128        $tree_service = $this->createMock(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