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