xref: /webtrees/tests/app/Http/RequestHandlers/RedirectFamilyBookPhpTest.php (revision b11cdcd45131b1585d66693fab363cfeb18c51a4)
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    /**
41     * @return void
42     */
43    public function testRedirect(): void
44    {
45        $tree = $this->createStub(Tree::class);
46        $tree
47            ->method('name')
48            ->willReturn('tree1');
49
50        $tree_service = $this->createStub(TreeService::class);
51        $tree_service
52            ->expects(self::once())
53            ->method('all')
54            ->willReturn(new Collection(['tree1' => $tree]));
55
56        $individual = $this->createStub(Individual::class);
57
58        $individual_factory = $this->createStub(IndividualFactory::class);
59        $individual_factory
60            ->expects(self::once())
61            ->method('make')
62            ->with('X123', $tree)
63            ->willReturn($individual);
64
65        Registry::individualFactory($individual_factory);
66
67        $Compact_chart = $this->createStub(FamilyBookChartModule::class);
68        $Compact_chart
69            ->expects(self::once())
70            ->method('chartUrl')
71            ->willReturn('https://www.example.com');
72
73        $module_service = $this->createStub(ModuleService::class);
74        $module_service
75            ->expects(self::once())
76            ->method('findByInterface')
77            ->with(FamilyBookChartModule::class)
78            ->willReturn(new Collection([$Compact_chart]));
79
80        $handler = new RedirectFamilyBookPhp($module_service, $tree_service);
81
82        $request  = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1', 'rootid' => 'X123']);
83        $response = $handler->handle($request);
84
85        self::assertSame(StatusCodeInterface::STATUS_MOVED_PERMANENTLY, $response->getStatusCode());
86        self::assertSame('https://www.example.com', $response->getHeaderLine('Location'));
87    }
88
89    /**
90     * @return void
91     */
92    public function testModuleDisabled(): void
93    {
94        $module_service = $this->createStub(ModuleService::class);
95        $module_service
96            ->expects(self::once())->method('findByInterface')
97            ->with(FamilyBookChartModule::class)
98            ->willReturn(new Collection());
99
100        $tree = $this->createStub(Tree::class);
101
102        $tree_service = $this->createStub(TreeService::class);
103        $tree_service
104            ->expects(self::once())
105            ->method('all')
106            ->willReturn(new Collection([$tree]));
107        $handler = new RedirectFamilyBookPhp($module_service, $tree_service);
108
109        $request = self::createRequest(
110            RequestMethodInterface::METHOD_GET,
111            ['ged' => 'tree1', 'rootid' => 'X123']
112        );
113
114        $this->expectException(HttpNotFoundException::class);
115
116        $handler->handle($request);
117    }
118
119    /**
120     * @return void
121     */
122    public function testNoSuchTree(): void
123    {
124        $module = $this->createStub(FamilyBookChartModule::class);
125
126        $module_service = $this->createStub(ModuleService::class);
127        $module_service
128            ->expects(self::once())
129            ->method('findByInterface')
130            ->with(FamilyBookChartModule::class)
131            ->willReturn(new Collection([$module]));
132
133        $tree_service = $this->createStub(TreeService::class);
134        $tree_service
135            ->expects(self::once())
136            ->method('all')
137            ->willReturn(new Collection([]));
138
139        $handler = new RedirectFamilyBookPhp($module_service, $tree_service);
140
141        $request = self::createRequest(
142            RequestMethodInterface::METHOD_GET,
143            ['ged' => 'tree1', 'rootid' => 'X123']
144        );
145
146        $this->expectException(HttpNotFoundException::class);
147
148        $handler->handle($request);
149    }
150}
151