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