xref: /webtrees/tests/app/Http/RequestHandlers/RedirectStatisticsPhpTest.php (revision d4ec5ec0fe611c85b1d77527db3e9becde7e473d)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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\StatisticsChartModule;
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\RedirectStatisticsPhp
37 */
38class RedirectStatisticsPhpTest 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        $module = $this->createStub(StatisticsChartModule::class);
57        $module
58            ->expects(self::once())
59            ->method('chartUrl')
60            ->willReturn('https://www.example.com');
61
62        $module_service = $this->createStub(ModuleService::class);
63        $module_service
64            ->expects(self::once())
65            ->method('findByInterface')
66            ->with(StatisticsChartModule::class)
67            ->willReturn(new Collection([$module]));
68
69        $handler = new RedirectStatisticsPhp($module_service, $tree_service);
70
71        $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1']);
72
73        $response = $handler->handle($request);
74
75        self::assertSame(StatusCodeInterface::STATUS_MOVED_PERMANENTLY, $response->getStatusCode());
76        self::assertSame('https://www.example.com', $response->getHeaderLine('Location'));
77    }
78
79    /**
80     * @return void
81     */
82    public function testModuleDisabled(): void
83    {
84        $module_service = $this->createStub(ModuleService::class);
85        $module_service
86            ->expects(self::once())->method('findByInterface')
87            ->with(StatisticsChartModule::class)
88            ->willReturn(new Collection());
89
90        $tree = $this->createStub(Tree::class);
91
92        $tree_service = $this->createStub(TreeService::class);
93        $tree_service
94            ->expects(self::once())
95            ->method('all')
96            ->willReturn(new Collection([$tree]));
97
98        $handler = new RedirectStatisticsPhp($module_service, $tree_service);
99
100        $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1']);
101
102        $this->expectException(HttpNotFoundException::class);
103
104        $handler->handle($request);
105    }
106
107    /**
108     * @return void
109     */
110    public function testNoSuchTree(): void
111    {
112        $module = $this->createStub(StatisticsChartModule::class);
113
114        $module_service  = $this->createStub(ModuleService::class);
115        $module_service
116            ->expects(self::once())
117            ->method('findByInterface')
118            ->with(StatisticsChartModule::class)
119            ->willReturn(new Collection([$module]));
120
121        $tree_service = $this->createStub(TreeService::class);
122        $tree_service
123            ->expects(self::once())
124            ->method('all')
125            ->willReturn(new Collection([]));
126
127        $handler = new RedirectStatisticsPhp($module_service, $tree_service);
128
129        $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1']);
130
131        $this->expectException(HttpNotFoundException::class);
132
133        $handler->handle($request);
134    }
135}
136