xref: /webtrees/tests/app/Http/RequestHandlers/RedirectPedigreePhpTest.php (revision 00ef1d3af59aba99c1ae5c92c7e655525c97797b)
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\GuestUser;
26use Fisharebest\Webtrees\Http\Exceptions\HttpGoneException;
27use Fisharebest\Webtrees\Individual;
28use Fisharebest\Webtrees\Module\ModuleChartInterface;
29use Fisharebest\Webtrees\Module\PedigreeChartModule;
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(RedirectPedigreePhp::class)]
39class RedirectPedigreePhpTest extends TestCase
40{
41    protected static bool $uses_database = true;
42
43    public function testRedirect(): 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(PedigreeChartModule::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('findByComponent')
77            ->with(ModuleChartInterface::class)
78            ->willReturn(new Collection([$module]));
79
80        $handler = new RedirectPedigreePhp($module_service, $tree_service);
81
82        $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1', 'rootid' => 'X123']);
83
84        $response = $handler->handle($request);
85
86        self::assertSame(StatusCodeInterface::STATUS_MOVED_PERMANENTLY, $response->getStatusCode());
87        self::assertSame('https://www.example.com', $response->getHeaderLine('Location'));
88    }
89
90    public function testModuleDisabled(): void
91    {
92        $tree = $this->createMock(Tree::class);
93        $tree
94            ->method('name')
95            ->willReturn('tree1');
96
97        $tree_service = $this->createMock(TreeService::class);
98        $tree_service
99            ->expects($this->once())
100            ->method('all')
101            ->willReturn(new Collection(['tree1' => $tree]));
102
103        $module_service = $this->createMock(ModuleService::class);
104        $module_service
105            ->method('findByComponent')
106            ->with(ModuleChartInterface::class, $tree, new GuestUser())
107            ->willReturn(new Collection());
108
109        $handler = new RedirectPedigreePhp($module_service, $tree_service);
110
111        $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1', 'rootid' => 'X123']);
112
113        $this->expectException(HttpGoneException::class);
114
115        $handler->handle($request);
116    }
117
118    public function testNoSuchTree(): void
119    {
120        $module_service  = $this->createMock(ModuleService::class);
121
122        $tree_service = $this->createMock(TreeService::class);
123        $tree_service
124            ->expects($this->once())
125            ->method('all')
126            ->willReturn(new Collection([]));
127
128        $handler = new RedirectPedigreePhp($module_service, $tree_service);
129
130        $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1', 'rootid' => 'X123']);
131
132        $this->expectException(HttpGoneException::class);
133
134        $handler->handle($request);
135    }
136}
137