xref: /webtrees/tests/app/Http/RequestHandlers/RedirectBranchesPhpTest.php (revision 9660ebfefe26982c97c5f787230b1c6c5e61c1e1)
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\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(RedirectBranchesPhp::class)]
34class RedirectBranchesPhpTest 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(BranchesListModule::class);
52
53        $module_service = $this->createMock(ModuleService::class);
54        $module_service
55            ->expects(self::once())
56            ->method('findByInterface')
57            ->with(BranchesListModule::class)
58            ->willReturn(new Collection([$module]));
59
60        $handler = new RedirectBranchesPhp($module_service, $tree_service);
61
62        $request = self::createRequest(
63            RequestMethodInterface::METHOD_GET,
64            ['ged' => 'tree1', 'surname' => 'XYZ'],
65            [],
66            [],
67            ['base_url' => 'https://www.example.com']
68        );
69
70        $response = $handler->handle($request);
71
72        self::assertSame(StatusCodeInterface::STATUS_MOVED_PERMANENTLY, $response->getStatusCode());
73        self::assertSame(
74            'https://www.example.com/index.php?route=%2F%2FPage%2Ftree1&soundex_dm=&soundex_std=&surname=XYZ',
75            $response->getHeaderLine('Location')
76        );
77    }
78
79    public function testModuleDisabled(): void
80    {
81        $module_service = $this->createMock(ModuleService::class);
82        $module_service
83            ->expects(self::once())->method('findByInterface')
84            ->with(BranchesListModule::class)
85            ->willReturn(new Collection());
86
87        $tree = $this->createMock(Tree::class);
88
89        $tree_service = $this->createMock(TreeService::class);
90        $tree_service
91            ->expects(self::once())
92            ->method('all')
93            ->willReturn(new Collection([$tree]));
94
95        $handler = new RedirectBranchesPhp($module_service, $tree_service);
96
97        $request = self::createRequest(
98            RequestMethodInterface::METHOD_GET,
99            ['ged' => 'tree1', 'surname' => 'XYZ']
100        );
101
102        $this->expectException(HttpGoneException::class);
103
104        $handler->handle($request);
105    }
106
107    public function testNoSuchTree(): void
108    {
109        $module = $this->createMock(BranchesListModule::class);
110
111        $module_service = $this->createMock(ModuleService::class);
112        $module_service
113            ->expects(self::once())
114            ->method('findByInterface')
115            ->with(BranchesListModule::class)
116            ->willReturn(new Collection([$module]));
117
118        $tree_service = $this->createMock(TreeService::class);
119        $tree_service
120            ->expects(self::once())
121            ->method('all')
122            ->willReturn(new Collection([]));
123
124        $handler = new RedirectBranchesPhp($module_service, $tree_service);
125
126        $request = self::createRequest(
127            RequestMethodInterface::METHOD_GET,
128            ['ged' => 'tree1', 'surname' => 'XYZ']
129        );
130
131        $this->expectException(HttpGoneException::class);
132
133        $handler->handle($request);
134    }
135}
136