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