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