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