xref: /webtrees/tests/app/Http/RequestHandlers/RedirectModulePhpTest.php (revision e873f434551745f888937263ff89e80db3b0f785)
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\InteractiveTreeModule;
28use Fisharebest\Webtrees\Module\ModuleListInterface;
29use Fisharebest\Webtrees\Module\PedigreeMapModule;
30use Fisharebest\Webtrees\Registry;
31use Fisharebest\Webtrees\Services\ModuleService;
32use Fisharebest\Webtrees\Services\TreeService;
33use Fisharebest\Webtrees\TestCase;
34use Fisharebest\Webtrees\Tree;
35use Illuminate\Support\Collection;
36use PHPUnit\Framework\Attributes\CoversClass;
37
38#[CoversClass(RedirectModulePhp::class)]
39class RedirectModulePhpTest extends TestCase
40{
41    protected static bool $uses_database = true;
42
43    public function testRedirectPedigreeMap(): void
44    {
45        $tree = $this->createMock(Tree::class);
46        $tree
47            ->method('name')
48            ->willReturn('tree1');
49
50        $tree_service = $this->createMock(TreeService::class);
51        $tree_service
52            ->expects($this->once())
53            ->method('all')
54            ->willReturn(new Collection(['tree1' => $tree]));
55
56        $individual = $this->createMock(Individual::class);
57
58        $individual_factory = $this->createMock(IndividualFactory::class);
59        $individual_factory
60            ->expects($this->once())
61            ->method('make')
62            ->with('X123', $tree)
63            ->willReturn($individual);
64
65        Registry::individualFactory($individual_factory);
66
67        $module = $this->createMock(PedigreeMapModule::class);
68        $module
69            ->expects($this->once())
70            ->method('chartUrl')
71            ->willReturn('https://www.example.com');
72
73        $module_service = $this->createMock(ModuleService::class);
74        $module_service
75            ->expects($this->once())
76            ->method('findByinterface')
77            ->with(PedigreeMapModule::class)
78            ->willReturn(new Collection([$module]));
79
80        $handler = new RedirectModulePhp($module_service, $tree_service);
81
82        $request = self::createRequest(
83            RequestMethodInterface::METHOD_GET,
84            ['mod' => 'googlemap', 'mod_action' => 'pedigree_map', 'ged' => 'tree1', 'rootid' => 'X123']
85        );
86
87        $response = $handler->handle($request);
88
89        self::assertSame(StatusCodeInterface::STATUS_MOVED_PERMANENTLY, $response->getStatusCode());
90        self::assertSame('https://www.example.com', $response->getHeaderLine('Location'));
91    }
92
93    public function testRedirectInteractiveTree(): void
94    {
95        $tree = $this->createMock(Tree::class);
96        $tree
97            ->method('name')
98            ->willReturn('tree1');
99
100        $tree_service = $this->createMock(TreeService::class);
101        $tree_service
102            ->expects($this->once())
103            ->method('all')
104            ->willReturn(new Collection(['tree1' => $tree]));
105
106        $individual = $this->createMock(Individual::class);
107
108        $individual_factory = $this->createMock(IndividualFactory::class);
109        $individual_factory
110            ->expects($this->once())
111            ->method('make')
112            ->with('X123', $tree)
113            ->willReturn($individual);
114
115        Registry::individualFactory($individual_factory);
116
117        $module = $this->createMock(InteractiveTreeModule::class);
118        $module
119            ->expects($this->once())
120            ->method('chartUrl')
121            ->willReturn('https://www.example.com');
122
123        $module_service = $this->createMock(ModuleService::class);
124        $module_service
125            ->expects($this->once())
126            ->method('findByInterface')
127            ->with(InteractiveTreeModule::class)
128            ->willReturn(new Collection([$module]));
129
130        $handler = new RedirectModulePhp($module_service, $tree_service);
131
132        $request = self::createRequest(
133            RequestMethodInterface::METHOD_GET,
134            ['mod' => 'tree', 'mod_action' => 'treeview', 'ged' => 'tree1', 'rootid' => 'X123']
135        );
136
137        $response = $handler->handle($request);
138
139        self::assertSame(StatusCodeInterface::STATUS_MOVED_PERMANENTLY, $response->getStatusCode());
140        self::assertSame('https://www.example.com', $response->getHeaderLine('Location'));
141    }
142
143    public function testNoSuchTree(): void
144    {
145        $module_service  = $this->createMock(ModuleService::class);
146        $tree_service = $this->createMock(TreeService::class);
147        $tree_service
148            ->expects($this->once())
149            ->method('all')
150            ->willReturn(new Collection([]));
151
152        $handler = new RedirectModulePhp($module_service, $tree_service);
153
154        $request = self::createRequest(
155            RequestMethodInterface::METHOD_GET,
156            ['ged' => 'tree1', 'rootid' => 'X123']
157        );
158
159        $this->expectException(HttpGoneException::class);
160
161        $handler->handle($request);
162    }
163
164    public function testNoSuchIndividual(): void
165    {
166        $tree = $this->createMock(Tree::class);
167        $tree
168            ->method('name')
169            ->willReturn('tree1');
170
171        $individual_factory = $this->createMock(IndividualFactory::class);
172        $individual_factory
173            ->expects($this->once())
174            ->method('make')
175            ->with('X123', $tree)
176            ->willReturn(null);
177
178        Registry::individualFactory($individual_factory);
179        $module_service  = $this->createMock(ModuleService::class);
180        $tree_service = $this->createMock(TreeService::class);
181        $tree_service
182            ->expects($this->once())
183            ->method('all')
184            ->willReturn(new Collection(['tree1' => $tree]));
185
186        $handler = new RedirectModulePhp($module_service, $tree_service);
187
188        $request = self::createRequest(
189            RequestMethodInterface::METHOD_GET,
190            ['ged' => 'tree1', 'rootid' => 'X123']
191        );
192
193        $this->expectException(HttpGoneException::class);
194
195        $handler->handle($request);
196    }
197
198    public function testPedigreeMapModuleDisabled(): void
199    {
200        $tree = $this->createMock(Tree::class);
201        $tree
202            ->method('name')
203            ->willReturn('tree1');
204
205        $tree_service = $this->createMock(TreeService::class);
206        $tree_service
207            ->expects($this->once())
208            ->method('all')
209            ->willReturn(new Collection(['tree1' => $tree]));
210
211        $individual = $this->createMock(Individual::class);
212
213        $individual_factory = $this->createMock(IndividualFactory::class);
214        $individual_factory
215            ->expects($this->once())
216            ->method('make')
217            ->with('X123', $tree)
218            ->willReturn($individual);
219
220        Registry::individualFactory($individual_factory);
221
222        $module_service = $this->createMock(ModuleService::class);
223        $module_service
224            ->expects($this->once())
225            ->method('findByInterface')
226            ->with(PedigreeMapModule::class)
227            ->willReturn(new Collection([]));
228
229        $handler = new RedirectModulePhp($module_service, $tree_service);
230
231        $request = self::createRequest(
232            RequestMethodInterface::METHOD_GET,
233            ['mod' => 'googlemap', 'mod_action' => 'pedigree_map', 'ged' => 'tree1', 'rootid' => 'X123']
234        );
235
236        $this->expectException(HttpGoneException::class);
237
238        $handler->handle($request);
239    }
240
241    public function testInteractiveTreeModuleDisabled(): void
242    {
243        $tree = $this->createMock(Tree::class);
244        $tree
245            ->method('name')
246            ->willReturn('tree1');
247
248        $tree_service = $this->createMock(TreeService::class);
249        $tree_service
250            ->expects($this->once())
251            ->method('all')
252            ->willReturn(new Collection(['tree1' => $tree]));
253
254        $individual = $this->createMock(Individual::class);
255
256        $individual_factory = $this->createMock(IndividualFactory::class);
257        $individual_factory
258            ->expects($this->once())
259            ->method('make')
260            ->with('X123', $tree)
261            ->willReturn($individual);
262
263        Registry::individualFactory($individual_factory);
264
265        $module_service = $this->createMock(ModuleService::class);
266        $module_service
267            ->expects($this->once())
268            ->method('findByInterface')
269            ->with(InteractiveTreeModule::class)
270            ->willReturn(new Collection([]));
271
272        $handler = new RedirectModulePhp($module_service, $tree_service);
273
274        $request = self::createRequest(
275            RequestMethodInterface::METHOD_GET,
276            ['mod' => 'tree', 'mod_action' => 'treeview', 'ged' => 'tree1', 'rootid' => 'X123']
277        );
278
279        $this->expectException(HttpGoneException::class);
280
281        $handler->handle($request);
282    }
283}
284