xref: /webtrees/tests/app/Http/RequestHandlers/RedirectBranchesPhpTest.php (revision b9a3cd35b03788204f73481cf07bb14e1dabd221)
13340ecd2SGreg Roach<?php
23340ecd2SGreg Roach
33340ecd2SGreg Roach/**
43340ecd2SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
63340ecd2SGreg Roach * This program is free software: you can redistribute it and/or modify
73340ecd2SGreg Roach * it under the terms of the GNU General Public License as published by
83340ecd2SGreg Roach * the Free Software Foundation, either version 3 of the License, or
93340ecd2SGreg Roach * (at your option) any later version.
103340ecd2SGreg Roach * This program is distributed in the hope that it will be useful,
113340ecd2SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
123340ecd2SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
133340ecd2SGreg Roach * GNU General Public License for more details.
143340ecd2SGreg Roach * You should have received a copy of the GNU General Public License
153340ecd2SGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
163340ecd2SGreg Roach */
173340ecd2SGreg Roach
183340ecd2SGreg Roachdeclare(strict_types=1);
193340ecd2SGreg Roach
203340ecd2SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
213340ecd2SGreg Roach
223340ecd2SGreg Roachuse Fig\Http\Message\RequestMethodInterface;
233340ecd2SGreg Roachuse Fig\Http\Message\StatusCodeInterface;
2415c4f62cSGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpGoneException;
253340ecd2SGreg Roachuse Fisharebest\Webtrees\Module\BranchesListModule;
2600ef1d3aSGreg Roachuse Fisharebest\Webtrees\Module\ModuleListInterface;
273340ecd2SGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
283340ecd2SGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
293340ecd2SGreg Roachuse Fisharebest\Webtrees\TestCase;
303340ecd2SGreg Roachuse Fisharebest\Webtrees\Tree;
313340ecd2SGreg Roachuse Illuminate\Support\Collection;
32202c018bSGreg Roachuse PHPUnit\Framework\Attributes\CoversClass;
333340ecd2SGreg Roach
34202c018bSGreg Roach#[CoversClass(RedirectBranchesPhp::class)]
353340ecd2SGreg Roachclass RedirectBranchesPhpTest extends TestCase
363340ecd2SGreg Roach{
37a26ec5edSGreg Roach    protected static bool $uses_database = true;
38a26ec5edSGreg Roach
393340ecd2SGreg Roach    public function testRedirect(): void
403340ecd2SGreg Roach    {
4162ff2f18SGreg Roach        $tree = $this->createMock(Tree::class);
423340ecd2SGreg Roach        $tree
433340ecd2SGreg Roach            ->method('name')
443340ecd2SGreg Roach            ->willReturn('tree1');
453340ecd2SGreg Roach
4662ff2f18SGreg Roach        $tree_service = $this->createMock(TreeService::class);
473340ecd2SGreg Roach        $tree_service
4800ef1d3aSGreg Roach            ->expects($this->once())
493340ecd2SGreg Roach            ->method('all')
503340ecd2SGreg Roach            ->willReturn(new Collection(['tree1' => $tree]));
513340ecd2SGreg Roach
5262ff2f18SGreg Roach        $module = $this->createMock(BranchesListModule::class);
53*b9a3cd35SGreg Roach        $module
54*b9a3cd35SGreg Roach            ->expects($this->once())
55*b9a3cd35SGreg Roach            ->method('listUrl')
56*b9a3cd35SGreg Roach            ->willReturn('https://www.example.com');
573340ecd2SGreg Roach
5862ff2f18SGreg Roach        $module_service = $this->createMock(ModuleService::class);
593340ecd2SGreg Roach        $module_service
6000ef1d3aSGreg Roach            ->expects($this->once())
6100ef1d3aSGreg Roach            ->method('findByComponent')
6200ef1d3aSGreg Roach            ->with(ModuleListInterface::class)
633340ecd2SGreg Roach            ->willReturn(new Collection([$module]));
643340ecd2SGreg Roach
653340ecd2SGreg Roach        $handler = new RedirectBranchesPhp($module_service, $tree_service);
663340ecd2SGreg Roach
673340ecd2SGreg Roach        $request = self::createRequest(
683340ecd2SGreg Roach            RequestMethodInterface::METHOD_GET,
693340ecd2SGreg Roach            ['ged' => 'tree1', 'surname' => 'XYZ'],
703340ecd2SGreg Roach            [],
713340ecd2SGreg Roach            [],
723340ecd2SGreg Roach            ['base_url' => 'https://www.example.com']
733340ecd2SGreg Roach        );
743340ecd2SGreg Roach
753340ecd2SGreg Roach        $response = $handler->handle($request);
763340ecd2SGreg Roach
773340ecd2SGreg Roach        self::assertSame(StatusCodeInterface::STATUS_MOVED_PERMANENTLY, $response->getStatusCode());
783340ecd2SGreg Roach        self::assertSame(
79*b9a3cd35SGreg Roach            'https://www.example.com',
803340ecd2SGreg Roach            $response->getHeaderLine('Location')
813340ecd2SGreg Roach        );
823340ecd2SGreg Roach    }
833340ecd2SGreg Roach
843340ecd2SGreg Roach    public function testModuleDisabled(): void
853340ecd2SGreg Roach    {
8662ff2f18SGreg Roach        $tree = $this->createMock(Tree::class);
873340ecd2SGreg Roach
8862ff2f18SGreg Roach        $tree_service = $this->createMock(TreeService::class);
893340ecd2SGreg Roach        $tree_service
9000ef1d3aSGreg Roach            ->expects($this->once())
913340ecd2SGreg Roach            ->method('all')
9200ef1d3aSGreg Roach            ->willReturn(new Collection(['tree1' => $tree]));
9300ef1d3aSGreg Roach
9400ef1d3aSGreg Roach        $module_service = $this->createMock(ModuleService::class);
9500ef1d3aSGreg Roach        $module_service
9600ef1d3aSGreg Roach            ->method('findByComponent')
9700ef1d3aSGreg Roach            ->with(ModuleListInterface::class)
9800ef1d3aSGreg Roach            ->willReturn(new Collection());
993340ecd2SGreg Roach
1003340ecd2SGreg Roach        $handler = new RedirectBranchesPhp($module_service, $tree_service);
1013340ecd2SGreg Roach
1023340ecd2SGreg Roach        $request = self::createRequest(
1033340ecd2SGreg Roach            RequestMethodInterface::METHOD_GET,
1043340ecd2SGreg Roach            ['ged' => 'tree1', 'surname' => 'XYZ']
1053340ecd2SGreg Roach        );
1063340ecd2SGreg Roach
10715c4f62cSGreg Roach        $this->expectException(HttpGoneException::class);
1083340ecd2SGreg Roach
1093340ecd2SGreg Roach        $handler->handle($request);
1103340ecd2SGreg Roach    }
1113340ecd2SGreg Roach
1123340ecd2SGreg Roach    public function testNoSuchTree(): void
1133340ecd2SGreg Roach    {
11462ff2f18SGreg Roach        $tree_service = $this->createMock(TreeService::class);
1153340ecd2SGreg Roach        $tree_service
11600ef1d3aSGreg Roach            ->expects($this->once())
1173340ecd2SGreg Roach            ->method('all')
1183340ecd2SGreg Roach            ->willReturn(new Collection([]));
1193340ecd2SGreg Roach
12000ef1d3aSGreg Roach        $module_service = $this->createMock(ModuleService::class);
12100ef1d3aSGreg Roach
1223340ecd2SGreg Roach        $handler = new RedirectBranchesPhp($module_service, $tree_service);
1233340ecd2SGreg Roach
1243340ecd2SGreg Roach        $request = self::createRequest(
1253340ecd2SGreg Roach            RequestMethodInterface::METHOD_GET,
1263340ecd2SGreg Roach            ['ged' => 'tree1', 'surname' => 'XYZ']
1273340ecd2SGreg Roach        );
1283340ecd2SGreg Roach
12915c4f62cSGreg Roach        $this->expectException(HttpGoneException::class);
1303340ecd2SGreg Roach
1313340ecd2SGreg Roach        $handler->handle($request);
1323340ecd2SGreg Roach    }
1333340ecd2SGreg Roach}
134