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