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