xref: /webtrees/tests/app/Http/RequestHandlers/RedirectLifeSpanPhpTest.php (revision 202c018b592d5a516e4a465dc6dc515f3be37399)
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\Http\Exceptions\HttpGoneException;
25use Fisharebest\Webtrees\Module\LifespansChartModule;
26use Fisharebest\Webtrees\Services\ModuleService;
27use Fisharebest\Webtrees\Services\TreeService;
28use Fisharebest\Webtrees\TestCase;
29use Fisharebest\Webtrees\Tree;
30use Illuminate\Support\Collection;
31use PHPUnit\Framework\Attributes\CoversClass;
32
33#[CoversClass(RedirectLifeSpanPhp::class)]
34class RedirectLifeSpanPhpTest extends TestCase
35{
36    protected static bool $uses_database = true;
37
38    public function testRedirect(): void
39    {
40        $tree = $this->createStub(Tree::class);
41        $tree
42            ->method('name')
43            ->willReturn('tree1');
44
45        $tree_service = $this->createStub(TreeService::class);
46        $tree_service
47            ->expects(self::once())
48            ->method('all')
49            ->willReturn(new Collection(['tree1' => $tree]));
50
51        $module = $this->createStub(LifespansChartModule::class);
52        $module
53            ->expects(self::once())
54            ->method('chartUrl')
55            ->willReturn('https://www.example.com');
56
57        $module_service = $this->createStub(ModuleService::class);
58        $module_service
59            ->expects(self::once())
60            ->method('findByInterface')
61            ->with(LifespansChartModule::class)
62            ->willReturn(new Collection([$module]));
63
64        $handler = new RedirectLifeSpanPhp($module_service, $tree_service);
65
66        $request = self::createRequest(
67            RequestMethodInterface::METHOD_GET,
68            ['ged' => 'tree1', 'rootid' => 'X123']
69        );
70
71        $response = $handler->handle($request);
72
73        self::assertSame(StatusCodeInterface::STATUS_MOVED_PERMANENTLY, $response->getStatusCode());
74        self::assertSame('https://www.example.com', $response->getHeaderLine('Location'));
75    }
76
77    public function testModuleDisabled(): void
78    {
79        $module_service = $this->createStub(ModuleService::class);
80        $module_service
81            ->expects(self::once())->method('findByInterface')
82            ->with(LifespansChartModule::class)
83            ->willReturn(new Collection());
84
85        $tree = $this->createStub(Tree::class);
86
87        $tree_service = $this->createStub(TreeService::class);
88        $tree_service
89            ->expects(self::once())
90            ->method('all')
91            ->willReturn(new Collection([$tree]));
92
93        $handler = new RedirectLifeSpanPhp($module_service, $tree_service);
94
95        $request = self::createRequest(
96            RequestMethodInterface::METHOD_GET,
97            ['ged' => 'tree1', 'rootid' => 'X123']
98        );
99
100        $this->expectException(HttpGoneException::class);
101
102        $handler->handle($request);
103    }
104
105    public function testNoSuchTree(): void
106    {
107        $module = $this->createStub(LifespansChartModule::class);
108
109        $module_service  = $this->createStub(ModuleService::class);
110        $module_service
111            ->expects(self::once())
112            ->method('findByInterface')
113            ->with(LifespansChartModule::class)
114            ->willReturn(new Collection([$module]));
115
116        $tree_service = $this->createStub(TreeService::class);
117        $tree_service
118            ->expects(self::once())
119            ->method('all')
120            ->willReturn(new Collection([]));
121
122        $handler = new RedirectLifeSpanPhp($module_service, $tree_service);
123
124        $request = self::createRequest(
125            RequestMethodInterface::METHOD_GET,
126            ['ged' => 'tree1', 'rootid' => 'X123']
127        );
128
129        $this->expectException(HttpGoneException::class);
130
131        $handler->handle($request);
132    }
133}
134