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