xref: /webtrees/tests/app/Http/RequestHandlers/RedirectLifeSpanPhpTest.php (revision 00ef1d3af59aba99c1ae5c92c7e655525c97797b)
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\LifespansChartModule;
26*00ef1d3aSGreg Roachuse Fisharebest\Webtrees\Module\ModuleChartInterface;
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(RedirectLifeSpanPhp::class)]
353340ecd2SGreg Roachclass RedirectLifeSpanPhpTest 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
48*00ef1d3aSGreg Roach            ->expects($this->once())
493340ecd2SGreg Roach            ->method('all')
503340ecd2SGreg Roach            ->willReturn(new Collection(['tree1' => $tree]));
513340ecd2SGreg Roach
5262ff2f18SGreg Roach        $module = $this->createMock(LifespansChartModule::class);
533340ecd2SGreg Roach        $module
54*00ef1d3aSGreg Roach            ->expects($this->once())
553340ecd2SGreg Roach            ->method('chartUrl')
563340ecd2SGreg Roach            ->willReturn('https://www.example.com');
573340ecd2SGreg Roach
5862ff2f18SGreg Roach        $module_service = $this->createMock(ModuleService::class);
593340ecd2SGreg Roach        $module_service
60*00ef1d3aSGreg Roach            ->expects($this->once())
61*00ef1d3aSGreg Roach            ->method('findByComponent')
62*00ef1d3aSGreg Roach            ->with(ModuleChartInterface::class)
633340ecd2SGreg Roach            ->willReturn(new Collection([$module]));
643340ecd2SGreg Roach
653340ecd2SGreg Roach        $handler = new RedirectLifeSpanPhp($module_service, $tree_service);
663340ecd2SGreg Roach
67*00ef1d3aSGreg Roach        $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1', 'rootid' => 'X123']);
683340ecd2SGreg Roach
693340ecd2SGreg Roach        $response = $handler->handle($request);
703340ecd2SGreg Roach
713340ecd2SGreg Roach        self::assertSame(StatusCodeInterface::STATUS_MOVED_PERMANENTLY, $response->getStatusCode());
723340ecd2SGreg Roach        self::assertSame('https://www.example.com', $response->getHeaderLine('Location'));
733340ecd2SGreg Roach    }
743340ecd2SGreg Roach
753340ecd2SGreg Roach    public function testModuleDisabled(): void
763340ecd2SGreg Roach    {
7762ff2f18SGreg Roach        $module_service = $this->createMock(ModuleService::class);
783340ecd2SGreg Roach        $module_service
79*00ef1d3aSGreg Roach            ->expects($this->once())->method('findByComponent')
80*00ef1d3aSGreg Roach            ->with(ModuleChartInterface::class)
813340ecd2SGreg Roach            ->willReturn(new Collection());
823340ecd2SGreg Roach
8362ff2f18SGreg Roach        $tree = $this->createMock(Tree::class);
843340ecd2SGreg Roach
8562ff2f18SGreg Roach        $tree_service = $this->createMock(TreeService::class);
863340ecd2SGreg Roach        $tree_service
87*00ef1d3aSGreg Roach            ->expects($this->once())
883340ecd2SGreg Roach            ->method('all')
89*00ef1d3aSGreg Roach            ->willReturn(new Collection(['tree1' => $tree]));
903340ecd2SGreg Roach
913340ecd2SGreg Roach        $handler = new RedirectLifeSpanPhp($module_service, $tree_service);
923340ecd2SGreg Roach
93*00ef1d3aSGreg Roach        $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1', 'rootid' => 'X123']);
943340ecd2SGreg Roach
9515c4f62cSGreg Roach        $this->expectException(HttpGoneException::class);
963340ecd2SGreg Roach
973340ecd2SGreg Roach        $handler->handle($request);
983340ecd2SGreg Roach    }
993340ecd2SGreg Roach
1003340ecd2SGreg Roach    public function testNoSuchTree(): void
1013340ecd2SGreg Roach    {
10262ff2f18SGreg Roach        $module_service = $this->createMock(ModuleService::class);
1033340ecd2SGreg Roach
10462ff2f18SGreg Roach        $tree_service = $this->createMock(TreeService::class);
1053340ecd2SGreg Roach        $tree_service
106*00ef1d3aSGreg Roach            ->expects($this->once())
1073340ecd2SGreg Roach            ->method('all')
1083340ecd2SGreg Roach            ->willReturn(new Collection([]));
1093340ecd2SGreg Roach
1103340ecd2SGreg Roach        $handler = new RedirectLifeSpanPhp($module_service, $tree_service);
1113340ecd2SGreg Roach
112*00ef1d3aSGreg Roach        $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1', 'rootid' => 'X123']);
1133340ecd2SGreg Roach
11415c4f62cSGreg Roach        $this->expectException(HttpGoneException::class);
1153340ecd2SGreg Roach
1163340ecd2SGreg Roach        $handler->handle($request);
1173340ecd2SGreg Roach    }
1183340ecd2SGreg Roach}
119