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