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