xref: /webtrees/tests/app/Http/RequestHandlers/RedirectSourcePhpTest.php (revision 06c3e14e4fed5ad862e3566855102053125b09c6)
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\HttpGoneException;
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;
33use PHPUnit\Framework\Attributes\CoversClass;
34
35#[CoversClass(RedirectSourcePhp::class)]
36class RedirectSourcePhpTest 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(self::once())
50            ->method('all')
51            ->willReturn(new Collection(['tree1' => $tree]));
52
53        $source = $this->createMock(Source::class);
54        $source
55            ->method('url')
56            ->willReturn('https://www.example.com');
57
58        $source_factory = $this->createMock(SourceFactory::class);
59        $source_factory
60            ->expects(self::once())
61            ->method('make')
62            ->with('X123', $tree)
63            ->willReturn($source);
64
65        Registry::sourceFactory($source_factory);
66
67        $handler = new RedirectSourcePhp($tree_service);
68
69        $request = self::createRequest(
70            RequestMethodInterface::METHOD_GET,
71            ['ged' => 'tree1', 'sid' => 'X123']
72        );
73
74        $response = $handler->handle($request);
75
76        self::assertSame(StatusCodeInterface::STATUS_MOVED_PERMANENTLY, $response->getStatusCode());
77        self::assertSame('https://www.example.com', $response->getHeaderLine('Location'));
78    }
79
80    public function testNoSuchRecord(): void
81    {
82        $tree = $this->createMock(Tree::class);
83
84        $tree_service = $this->createMock(TreeService::class);
85        $tree_service
86            ->expects(self::once())
87            ->method('all')
88            ->willReturn(new Collection([$tree]));
89
90        $handler = new RedirectSourcePhp($tree_service);
91
92        $request = self::createRequest(
93            RequestMethodInterface::METHOD_GET,
94            ['ged' => 'tree1', 'sid' => 'X123']
95        );
96
97        $this->expectException(HttpGoneException::class);
98
99        $handler->handle($request);
100    }
101
102    public function testMissingXrefParameter(): void
103    {
104        $tree_service = $this->createMock(TreeService::class);
105
106        $handler = new RedirectSourcePhp($tree_service);
107
108        $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1']);
109
110        $this->expectException(HttpBadRequestException::class);
111
112        $handler->handle($request);
113    }
114}
115