xref: /webtrees/tests/app/Http/RequestHandlers/RedirectModulePhpTest.php (revision 00ef1d3af59aba99c1ae5c92c7e655525c97797b)
13340ecd2SGreg Roach<?php
23340ecd2SGreg Roach
33340ecd2SGreg Roach/**
43340ecd2SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
63340ecd2SGreg Roach * This program is free software: you can redistribute it and/or modify
73340ecd2SGreg Roach * it under the terms of the GNU General Public License as published by
83340ecd2SGreg Roach * the Free Software Foundation, either version 3 of the License, or
93340ecd2SGreg Roach * (at your option) any later version.
103340ecd2SGreg Roach * This program is distributed in the hope that it will be useful,
113340ecd2SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
123340ecd2SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
133340ecd2SGreg Roach * GNU General Public License for more details.
143340ecd2SGreg Roach * You should have received a copy of the GNU General Public License
153340ecd2SGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
163340ecd2SGreg Roach */
173340ecd2SGreg Roach
183340ecd2SGreg Roachdeclare(strict_types=1);
193340ecd2SGreg Roach
203340ecd2SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
213340ecd2SGreg Roach
223340ecd2SGreg Roachuse Fig\Http\Message\RequestMethodInterface;
233340ecd2SGreg Roachuse Fig\Http\Message\StatusCodeInterface;
243340ecd2SGreg Roachuse Fisharebest\Webtrees\Factories\IndividualFactory;
2515c4f62cSGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpGoneException;
263340ecd2SGreg Roachuse Fisharebest\Webtrees\Individual;
273340ecd2SGreg Roachuse Fisharebest\Webtrees\Module\InteractiveTreeModule;
28*00ef1d3aSGreg Roachuse Fisharebest\Webtrees\Module\ModuleListInterface;
293340ecd2SGreg Roachuse Fisharebest\Webtrees\Module\PedigreeMapModule;
303340ecd2SGreg Roachuse Fisharebest\Webtrees\Registry;
313340ecd2SGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
323340ecd2SGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
333340ecd2SGreg Roachuse Fisharebest\Webtrees\TestCase;
343340ecd2SGreg Roachuse Fisharebest\Webtrees\Tree;
353340ecd2SGreg Roachuse Illuminate\Support\Collection;
36202c018bSGreg Roachuse PHPUnit\Framework\Attributes\CoversClass;
373340ecd2SGreg Roach
38202c018bSGreg Roach#[CoversClass(RedirectModulePhp::class)]
393340ecd2SGreg Roachclass RedirectModulePhpTest extends TestCase
403340ecd2SGreg Roach{
41a26ec5edSGreg Roach    protected static bool $uses_database = true;
42a26ec5edSGreg Roach
433340ecd2SGreg Roach    public function testRedirectPedigreeMap(): void
443340ecd2SGreg Roach    {
4562ff2f18SGreg Roach        $tree = $this->createMock(Tree::class);
463340ecd2SGreg Roach        $tree
473340ecd2SGreg Roach            ->method('name')
483340ecd2SGreg Roach            ->willReturn('tree1');
493340ecd2SGreg Roach
5062ff2f18SGreg Roach        $tree_service = $this->createMock(TreeService::class);
513340ecd2SGreg Roach        $tree_service
52*00ef1d3aSGreg Roach            ->expects($this->once())
533340ecd2SGreg Roach            ->method('all')
543340ecd2SGreg Roach            ->willReturn(new Collection(['tree1' => $tree]));
553340ecd2SGreg Roach
5662ff2f18SGreg Roach        $individual = $this->createMock(Individual::class);
573340ecd2SGreg Roach
5862ff2f18SGreg Roach        $individual_factory = $this->createMock(IndividualFactory::class);
593340ecd2SGreg Roach        $individual_factory
60*00ef1d3aSGreg Roach            ->expects($this->once())
613340ecd2SGreg Roach            ->method('make')
623340ecd2SGreg Roach            ->with('X123', $tree)
633340ecd2SGreg Roach            ->willReturn($individual);
643340ecd2SGreg Roach
653340ecd2SGreg Roach        Registry::individualFactory($individual_factory);
663340ecd2SGreg Roach
6762ff2f18SGreg Roach        $module = $this->createMock(PedigreeMapModule::class);
683340ecd2SGreg Roach        $module
69*00ef1d3aSGreg Roach            ->expects($this->once())
703340ecd2SGreg Roach            ->method('chartUrl')
713340ecd2SGreg Roach            ->willReturn('https://www.example.com');
723340ecd2SGreg Roach
7362ff2f18SGreg Roach        $module_service = $this->createMock(ModuleService::class);
743340ecd2SGreg Roach        $module_service
75*00ef1d3aSGreg Roach            ->expects($this->once())
76*00ef1d3aSGreg Roach            ->method('findByinterface')
773340ecd2SGreg Roach            ->with(PedigreeMapModule::class)
783340ecd2SGreg Roach            ->willReturn(new Collection([$module]));
793340ecd2SGreg Roach
803340ecd2SGreg Roach        $handler = new RedirectModulePhp($module_service, $tree_service);
813340ecd2SGreg Roach
823340ecd2SGreg Roach        $request = self::createRequest(
833340ecd2SGreg Roach            RequestMethodInterface::METHOD_GET,
843340ecd2SGreg Roach            ['mod' => 'googlemap', 'mod_action' => 'pedigree_map', 'ged' => 'tree1', 'rootid' => 'X123']
853340ecd2SGreg Roach        );
863340ecd2SGreg Roach
873340ecd2SGreg Roach        $response = $handler->handle($request);
883340ecd2SGreg Roach
893340ecd2SGreg Roach        self::assertSame(StatusCodeInterface::STATUS_MOVED_PERMANENTLY, $response->getStatusCode());
903340ecd2SGreg Roach        self::assertSame('https://www.example.com', $response->getHeaderLine('Location'));
913340ecd2SGreg Roach    }
923340ecd2SGreg Roach
933340ecd2SGreg Roach    public function testRedirectInteractiveTree(): void
943340ecd2SGreg Roach    {
9562ff2f18SGreg Roach        $tree = $this->createMock(Tree::class);
963340ecd2SGreg Roach        $tree
973340ecd2SGreg Roach            ->method('name')
983340ecd2SGreg Roach            ->willReturn('tree1');
993340ecd2SGreg Roach
10062ff2f18SGreg Roach        $tree_service = $this->createMock(TreeService::class);
1013340ecd2SGreg Roach        $tree_service
102*00ef1d3aSGreg Roach            ->expects($this->once())
1033340ecd2SGreg Roach            ->method('all')
1043340ecd2SGreg Roach            ->willReturn(new Collection(['tree1' => $tree]));
1053340ecd2SGreg Roach
10662ff2f18SGreg Roach        $individual = $this->createMock(Individual::class);
1073340ecd2SGreg Roach
10862ff2f18SGreg Roach        $individual_factory = $this->createMock(IndividualFactory::class);
1093340ecd2SGreg Roach        $individual_factory
110*00ef1d3aSGreg Roach            ->expects($this->once())
1113340ecd2SGreg Roach            ->method('make')
1123340ecd2SGreg Roach            ->with('X123', $tree)
1133340ecd2SGreg Roach            ->willReturn($individual);
1143340ecd2SGreg Roach
1153340ecd2SGreg Roach        Registry::individualFactory($individual_factory);
1163340ecd2SGreg Roach
11762ff2f18SGreg Roach        $module = $this->createMock(InteractiveTreeModule::class);
1183340ecd2SGreg Roach        $module
119*00ef1d3aSGreg Roach            ->expects($this->once())
1203340ecd2SGreg Roach            ->method('chartUrl')
1213340ecd2SGreg Roach            ->willReturn('https://www.example.com');
1223340ecd2SGreg Roach
12362ff2f18SGreg Roach        $module_service = $this->createMock(ModuleService::class);
1243340ecd2SGreg Roach        $module_service
125*00ef1d3aSGreg Roach            ->expects($this->once())
1263340ecd2SGreg Roach            ->method('findByInterface')
1273340ecd2SGreg Roach            ->with(InteractiveTreeModule::class)
1283340ecd2SGreg Roach            ->willReturn(new Collection([$module]));
1293340ecd2SGreg Roach
1303340ecd2SGreg Roach        $handler = new RedirectModulePhp($module_service, $tree_service);
1313340ecd2SGreg Roach
1323340ecd2SGreg Roach        $request = self::createRequest(
1333340ecd2SGreg Roach            RequestMethodInterface::METHOD_GET,
1343340ecd2SGreg Roach            ['mod' => 'tree', 'mod_action' => 'treeview', 'ged' => 'tree1', 'rootid' => 'X123']
1353340ecd2SGreg Roach        );
1363340ecd2SGreg Roach
1373340ecd2SGreg Roach        $response = $handler->handle($request);
1383340ecd2SGreg Roach
1393340ecd2SGreg Roach        self::assertSame(StatusCodeInterface::STATUS_MOVED_PERMANENTLY, $response->getStatusCode());
1403340ecd2SGreg Roach        self::assertSame('https://www.example.com', $response->getHeaderLine('Location'));
1413340ecd2SGreg Roach    }
1423340ecd2SGreg Roach
1433340ecd2SGreg Roach    public function testNoSuchTree(): void
1443340ecd2SGreg Roach    {
14562ff2f18SGreg Roach        $module_service  = $this->createMock(ModuleService::class);
14662ff2f18SGreg Roach        $tree_service = $this->createMock(TreeService::class);
1473340ecd2SGreg Roach        $tree_service
148*00ef1d3aSGreg Roach            ->expects($this->once())
1493340ecd2SGreg Roach            ->method('all')
1503340ecd2SGreg Roach            ->willReturn(new Collection([]));
1513340ecd2SGreg Roach
1523340ecd2SGreg Roach        $handler = new RedirectModulePhp($module_service, $tree_service);
1533340ecd2SGreg Roach
1543340ecd2SGreg Roach        $request = self::createRequest(
1553340ecd2SGreg Roach            RequestMethodInterface::METHOD_GET,
1563340ecd2SGreg Roach            ['ged' => 'tree1', 'rootid' => 'X123']
1573340ecd2SGreg Roach        );
1583340ecd2SGreg Roach
15915c4f62cSGreg Roach        $this->expectException(HttpGoneException::class);
1603340ecd2SGreg Roach
1613340ecd2SGreg Roach        $handler->handle($request);
1623340ecd2SGreg Roach    }
1633340ecd2SGreg Roach
1643340ecd2SGreg Roach    public function testNoSuchIndividual(): void
1653340ecd2SGreg Roach    {
16662ff2f18SGreg Roach        $tree = $this->createMock(Tree::class);
1673340ecd2SGreg Roach        $tree
1683340ecd2SGreg Roach            ->method('name')
1693340ecd2SGreg Roach            ->willReturn('tree1');
1703340ecd2SGreg Roach
17162ff2f18SGreg Roach        $individual_factory = $this->createMock(IndividualFactory::class);
1723340ecd2SGreg Roach        $individual_factory
173*00ef1d3aSGreg Roach            ->expects($this->once())
1743340ecd2SGreg Roach            ->method('make')
1753340ecd2SGreg Roach            ->with('X123', $tree)
1763340ecd2SGreg Roach            ->willReturn(null);
1773340ecd2SGreg Roach
1783340ecd2SGreg Roach        Registry::individualFactory($individual_factory);
17962ff2f18SGreg Roach        $module_service  = $this->createMock(ModuleService::class);
18062ff2f18SGreg Roach        $tree_service = $this->createMock(TreeService::class);
1813340ecd2SGreg Roach        $tree_service
182*00ef1d3aSGreg Roach            ->expects($this->once())
1833340ecd2SGreg Roach            ->method('all')
1843340ecd2SGreg Roach            ->willReturn(new Collection(['tree1' => $tree]));
1853340ecd2SGreg Roach
1863340ecd2SGreg Roach        $handler = new RedirectModulePhp($module_service, $tree_service);
1873340ecd2SGreg Roach
1883340ecd2SGreg Roach        $request = self::createRequest(
1893340ecd2SGreg Roach            RequestMethodInterface::METHOD_GET,
1903340ecd2SGreg Roach            ['ged' => 'tree1', 'rootid' => 'X123']
1913340ecd2SGreg Roach        );
1923340ecd2SGreg Roach
19315c4f62cSGreg Roach        $this->expectException(HttpGoneException::class);
1943340ecd2SGreg Roach
1953340ecd2SGreg Roach        $handler->handle($request);
1963340ecd2SGreg Roach    }
1973340ecd2SGreg Roach
1983340ecd2SGreg Roach    public function testPedigreeMapModuleDisabled(): void
1993340ecd2SGreg Roach    {
20062ff2f18SGreg Roach        $tree = $this->createMock(Tree::class);
2013340ecd2SGreg Roach        $tree
2023340ecd2SGreg Roach            ->method('name')
2033340ecd2SGreg Roach            ->willReturn('tree1');
2043340ecd2SGreg Roach
20562ff2f18SGreg Roach        $tree_service = $this->createMock(TreeService::class);
2063340ecd2SGreg Roach        $tree_service
207*00ef1d3aSGreg Roach            ->expects($this->once())
2083340ecd2SGreg Roach            ->method('all')
2093340ecd2SGreg Roach            ->willReturn(new Collection(['tree1' => $tree]));
2103340ecd2SGreg Roach
21162ff2f18SGreg Roach        $individual = $this->createMock(Individual::class);
2123340ecd2SGreg Roach
21362ff2f18SGreg Roach        $individual_factory = $this->createMock(IndividualFactory::class);
2143340ecd2SGreg Roach        $individual_factory
215*00ef1d3aSGreg Roach            ->expects($this->once())
2163340ecd2SGreg Roach            ->method('make')
2173340ecd2SGreg Roach            ->with('X123', $tree)
2183340ecd2SGreg Roach            ->willReturn($individual);
2193340ecd2SGreg Roach
2203340ecd2SGreg Roach        Registry::individualFactory($individual_factory);
2213340ecd2SGreg Roach
22262ff2f18SGreg Roach        $module_service = $this->createMock(ModuleService::class);
2233340ecd2SGreg Roach        $module_service
224*00ef1d3aSGreg Roach            ->expects($this->once())
2253340ecd2SGreg Roach            ->method('findByInterface')
2263340ecd2SGreg Roach            ->with(PedigreeMapModule::class)
2273340ecd2SGreg Roach            ->willReturn(new Collection([]));
2283340ecd2SGreg Roach
2293340ecd2SGreg Roach        $handler = new RedirectModulePhp($module_service, $tree_service);
2303340ecd2SGreg Roach
2313340ecd2SGreg Roach        $request = self::createRequest(
2323340ecd2SGreg Roach            RequestMethodInterface::METHOD_GET,
2333340ecd2SGreg Roach            ['mod' => 'googlemap', 'mod_action' => 'pedigree_map', 'ged' => 'tree1', 'rootid' => 'X123']
2343340ecd2SGreg Roach        );
2353340ecd2SGreg Roach
23615c4f62cSGreg Roach        $this->expectException(HttpGoneException::class);
2373340ecd2SGreg Roach
2383340ecd2SGreg Roach        $handler->handle($request);
2393340ecd2SGreg Roach    }
2403340ecd2SGreg Roach
2413340ecd2SGreg Roach    public function testInteractiveTreeModuleDisabled(): void
2423340ecd2SGreg Roach    {
24362ff2f18SGreg Roach        $tree = $this->createMock(Tree::class);
2443340ecd2SGreg Roach        $tree
2453340ecd2SGreg Roach            ->method('name')
2463340ecd2SGreg Roach            ->willReturn('tree1');
2473340ecd2SGreg Roach
24862ff2f18SGreg Roach        $tree_service = $this->createMock(TreeService::class);
2493340ecd2SGreg Roach        $tree_service
250*00ef1d3aSGreg Roach            ->expects($this->once())
2513340ecd2SGreg Roach            ->method('all')
2523340ecd2SGreg Roach            ->willReturn(new Collection(['tree1' => $tree]));
2533340ecd2SGreg Roach
25462ff2f18SGreg Roach        $individual = $this->createMock(Individual::class);
2553340ecd2SGreg Roach
25662ff2f18SGreg Roach        $individual_factory = $this->createMock(IndividualFactory::class);
2573340ecd2SGreg Roach        $individual_factory
258*00ef1d3aSGreg Roach            ->expects($this->once())
2593340ecd2SGreg Roach            ->method('make')
2603340ecd2SGreg Roach            ->with('X123', $tree)
2613340ecd2SGreg Roach            ->willReturn($individual);
2623340ecd2SGreg Roach
2633340ecd2SGreg Roach        Registry::individualFactory($individual_factory);
2643340ecd2SGreg Roach
26562ff2f18SGreg Roach        $module_service = $this->createMock(ModuleService::class);
2663340ecd2SGreg Roach        $module_service
267*00ef1d3aSGreg Roach            ->expects($this->once())
2683340ecd2SGreg Roach            ->method('findByInterface')
2693340ecd2SGreg Roach            ->with(InteractiveTreeModule::class)
2703340ecd2SGreg Roach            ->willReturn(new Collection([]));
2713340ecd2SGreg Roach
2723340ecd2SGreg Roach        $handler = new RedirectModulePhp($module_service, $tree_service);
2733340ecd2SGreg Roach
2743340ecd2SGreg Roach        $request = self::createRequest(
2753340ecd2SGreg Roach            RequestMethodInterface::METHOD_GET,
2763340ecd2SGreg Roach            ['mod' => 'tree', 'mod_action' => 'treeview', 'ged' => 'tree1', 'rootid' => 'X123']
2773340ecd2SGreg Roach        );
2783340ecd2SGreg Roach
27915c4f62cSGreg Roach        $this->expectException(HttpGoneException::class);
2803340ecd2SGreg Roach
2813340ecd2SGreg Roach        $handler->handle($request);
2823340ecd2SGreg Roach    }
2833340ecd2SGreg Roach}
284