xref: /webtrees/tests/app/Http/RequestHandlers/RedirectBranchesPhpTest.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\Http\Exceptions\HttpNotFoundException;
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;
31
32/**
33 * @covers \Fisharebest\Webtrees\Http\RequestHandlers\RedirectBranchesPhp
34 */
35class RedirectBranchesPhpTest extends TestCase
36{
37    /**
38     * @return void
39     */
40    public function testRedirect(): void
41    {
42        $tree = $this->createStub(Tree::class);
43        $tree
44            ->method('name')
45            ->willReturn('tree1');
46
47        $tree_service = $this->createStub(TreeService::class);
48        $tree_service
49            ->expects(self::once())
50            ->method('all')
51            ->willReturn(new Collection(['tree1' => $tree]));
52
53        $module = $this->createStub(BranchesListModule::class);
54
55        $module_service = $this->createStub(ModuleService::class);
56        $module_service
57            ->expects(self::once())
58            ->method('findByInterface')
59            ->with(BranchesListModule::class)
60            ->willReturn(new Collection([$module]));
61
62        $handler = new RedirectBranchesPhp($module_service, $tree_service);
63
64        $request = self::createRequest(
65            RequestMethodInterface::METHOD_GET,
66            ['ged' => 'tree1', 'surname' => 'XYZ'],
67            [],
68            [],
69            ['base_url' => 'https://www.example.com']
70        );
71
72        $response = $handler->handle($request);
73
74        self::assertSame(StatusCodeInterface::STATUS_MOVED_PERMANENTLY, $response->getStatusCode());
75        self::assertSame(
76            'https://www.example.com/index.php?route=%2F%2FPage%2Ftree1&surname=XYZ',
77            $response->getHeaderLine('Location')
78        );
79    }
80
81    /**
82     * @return void
83     */
84    public function testModuleDisabled(): void
85    {
86        $module_service = $this->createStub(ModuleService::class);
87        $module_service
88            ->expects(self::once())->method('findByInterface')
89            ->with(BranchesListModule::class)
90            ->willReturn(new Collection());
91
92        $tree = $this->createStub(Tree::class);
93
94        $tree_service = $this->createStub(TreeService::class);
95        $tree_service
96            ->expects(self::once())
97            ->method('all')
98            ->willReturn(new Collection([$tree]));
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(HttpNotFoundException::class);
108
109        $handler->handle($request);
110    }
111
112    /**
113     * @return void
114     */
115    public function testNoSuchTree(): void
116    {
117        $module = $this->createStub(BranchesListModule::class);
118
119        $module_service = $this->createStub(ModuleService::class);
120        $module_service
121            ->expects(self::once())
122            ->method('findByInterface')
123            ->with(BranchesListModule::class)
124            ->willReturn(new Collection([$module]));
125
126        $tree_service = $this->createStub(TreeService::class);
127        $tree_service
128            ->expects(self::once())
129            ->method('all')
130            ->willReturn(new Collection([]));
131
132        $handler = new RedirectBranchesPhp($module_service, $tree_service);
133
134        $request = self::createRequest(
135            RequestMethodInterface::METHOD_GET,
136            ['ged' => 'tree1', 'surname' => 'XYZ']
137        );
138
139        $this->expectException(HttpNotFoundException::class);
140
141        $handler->handle($request);
142    }
143}
144