xref: /webtrees/tests/app/Http/RequestHandlers/RedirectCompactPhpTest.php (revision 211018ab9ab6a5da13d161c40032525dfebcd13f)
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\IndividualFactory;
25use Fisharebest\Webtrees\Http\Exceptions\HttpGoneException;
26use Fisharebest\Webtrees\Individual;
27use Fisharebest\Webtrees\Module\CompactTreeChartModule;
28use Fisharebest\Webtrees\Registry;
29use Fisharebest\Webtrees\Services\ModuleService;
30use Fisharebest\Webtrees\Services\TreeService;
31use Fisharebest\Webtrees\TestCase;
32use Fisharebest\Webtrees\Tree;
33use Illuminate\Support\Collection;
34use PHPUnit\Framework\Attributes\CoversClass;
35
36#[CoversClass(RedirectCompactPhp::class)]
37class RedirectCompactPhpTest extends TestCase
38{
39    protected static bool $uses_database = true;
40
41    public function testRedirect(): void
42    {
43        $tree = $this->createMock(Tree::class);
44        $tree
45            ->method('name')
46            ->willReturn('tree1');
47
48        $tree_service = $this->createMock(TreeService::class);
49        $tree_service
50            ->expects(self::once())
51            ->method('all')
52            ->willReturn(new Collection(['tree1' => $tree]));
53
54        $individual = $this->createMock(Individual::class);
55
56        $individual_factory = $this->createMock(IndividualFactory::class);
57        $individual_factory
58            ->expects(self::once())
59            ->method('make')
60            ->with('X123', $tree)
61            ->willReturn($individual);
62
63        Registry::individualFactory($individual_factory);
64
65        $module = $this->createMock(CompactTreeChartModule::class);
66        $module
67            ->expects(self::once())
68            ->method('chartUrl')
69            ->willReturn('https://www.example.com');
70
71        $module_service = $this->createMock(ModuleService::class);
72        $module_service
73            ->expects(self::once())
74            ->method('findByInterface')
75            ->with(CompactTreeChartModule::class)
76            ->willReturn(new Collection([$module]));
77
78        $handler = new RedirectCompactPhp($module_service, $tree_service);
79
80        $request = self::createRequest(
81            RequestMethodInterface::METHOD_GET,
82            ['ged' => 'tree1', 'rootid' => 'X123']
83        );
84
85        $response = $handler->handle($request);
86
87        self::assertSame(StatusCodeInterface::STATUS_MOVED_PERMANENTLY, $response->getStatusCode());
88        self::assertSame('https://www.example.com', $response->getHeaderLine('Location'));
89    }
90
91    public function testModuleDisabled(): void
92    {
93        $module_service = $this->createMock(ModuleService::class);
94        $module_service
95            ->expects(self::once())->method('findByInterface')
96            ->with(CompactTreeChartModule::class)
97            ->willReturn(new Collection());
98
99        $tree = $this->createMock(Tree::class);
100
101        $tree_service = $this->createMock(TreeService::class);
102        $tree_service
103            ->expects(self::once())
104            ->method('all')
105            ->willReturn(new Collection([$tree]));
106
107        $handler = new RedirectCompactPhp($module_service, $tree_service);
108
109        $request = self::createRequest(
110            RequestMethodInterface::METHOD_GET,
111            ['ged' => 'tree1', 'rootid' => 'X123']
112        );
113
114        $this->expectException(HttpGoneException::class);
115
116        $handler->handle($request);
117    }
118
119    public function testNoSuchTree(): void
120    {
121        $module = $this->createMock(CompactTreeChartModule::class);
122
123        $module_service = $this->createMock(ModuleService::class);
124        $module_service
125            ->expects(self::once())
126            ->method('findByInterface')
127            ->with(CompactTreeChartModule::class)
128            ->willReturn(new Collection([$module]));
129
130        $tree_service = $this->createMock(TreeService::class);
131        $tree_service
132            ->expects(self::once())
133            ->method('all')
134            ->willReturn(new Collection([]));
135
136        $handler = new RedirectCompactPhp($module_service, $tree_service);
137
138        $request = self::createRequest(
139            RequestMethodInterface::METHOD_GET,
140            ['ged' => 'tree1', 'rootid' => 'X123']
141        );
142
143        $this->expectException(HttpGoneException::class);
144
145        $handler->handle($request);
146    }
147}
148