xref: /webtrees/tests/app/Http/RequestHandlers/RedirectStatisticsPhpTest.php (revision d3f966cc3d8b3007d6335281eae81905c58008ac)
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\StatisticsChartModule;
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\RedirectStatisticsPhp
34 */
35class RedirectStatisticsPhpTest 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(StatisticsChartModule::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(StatisticsChartModule::class)
63            ->willReturn(new Collection([$module]));
64
65        $handler = new RedirectStatisticsPhp($module_service, $tree_service);
66
67        $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1']);
68
69        $response = $handler->handle($request);
70
71        self::assertSame(StatusCodeInterface::STATUS_MOVED_PERMANENTLY, $response->getStatusCode());
72        self::assertSame('https://www.example.com', $response->getHeaderLine('Location'));
73    }
74
75    public function testModuleDisabled(): void
76    {
77        $module_service = $this->createStub(ModuleService::class);
78        $module_service
79            ->expects(self::once())->method('findByInterface')
80            ->with(StatisticsChartModule::class)
81            ->willReturn(new Collection());
82
83        $tree = $this->createStub(Tree::class);
84
85        $tree_service = $this->createStub(TreeService::class);
86        $tree_service
87            ->expects(self::once())
88            ->method('all')
89            ->willReturn(new Collection([$tree]));
90
91        $handler = new RedirectStatisticsPhp($module_service, $tree_service);
92
93        $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1']);
94
95        $this->expectException(HttpGoneException::class);
96
97        $handler->handle($request);
98    }
99
100    public function testNoSuchTree(): void
101    {
102        $module = $this->createStub(StatisticsChartModule::class);
103
104        $module_service  = $this->createStub(ModuleService::class);
105        $module_service
106            ->expects(self::once())
107            ->method('findByInterface')
108            ->with(StatisticsChartModule::class)
109            ->willReturn(new Collection([$module]));
110
111        $tree_service = $this->createStub(TreeService::class);
112        $tree_service
113            ->expects(self::once())
114            ->method('all')
115            ->willReturn(new Collection([]));
116
117        $handler = new RedirectStatisticsPhp($module_service, $tree_service);
118
119        $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1']);
120
121        $this->expectException(HttpGoneException::class);
122
123        $handler->handle($request);
124    }
125}
126