xref: /webtrees/tests/app/Http/RequestHandlers/RedirectIndividualPhpTest.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;
243340ecd2SGreg Roachuse Fisharebest\Webtrees\Factories\IndividualFactory;
253340ecd2SGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpBadRequestException;
2615c4f62cSGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpGoneException;
27f0c88a96SGreg Roachuse Fisharebest\Webtrees\Individual;
283340ecd2SGreg Roachuse Fisharebest\Webtrees\Registry;
293340ecd2SGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
303340ecd2SGreg Roachuse Fisharebest\Webtrees\TestCase;
313340ecd2SGreg Roachuse Fisharebest\Webtrees\Tree;
323340ecd2SGreg Roachuse Illuminate\Support\Collection;
33202c018bSGreg Roachuse PHPUnit\Framework\Attributes\CoversClass;
343340ecd2SGreg Roach
35202c018bSGreg Roach#[CoversClass(RedirectIndividualPhp::class)]
363340ecd2SGreg Roachclass RedirectIndividualPhpTest extends TestCase
373340ecd2SGreg Roach{
38a26ec5edSGreg Roach    protected static bool $uses_database = true;
39a26ec5edSGreg Roach
403340ecd2SGreg Roach    public function testRedirect(): void
413340ecd2SGreg Roach    {
4262ff2f18SGreg Roach        $tree = $this->createMock(Tree::class);
433340ecd2SGreg Roach        $tree
443340ecd2SGreg Roach            ->method('name')
453340ecd2SGreg Roach            ->willReturn('tree1');
463340ecd2SGreg Roach
4762ff2f18SGreg Roach        $tree_service = $this->createMock(TreeService::class);
483340ecd2SGreg Roach        $tree_service
49*00ef1d3aSGreg Roach            ->expects($this->once())
503340ecd2SGreg Roach            ->method('all')
513340ecd2SGreg Roach            ->willReturn(new Collection(['tree1' => $tree]));
523340ecd2SGreg Roach
5362ff2f18SGreg Roach        $individual = $this->createMock(Individual::class);
543340ecd2SGreg Roach        $individual
553340ecd2SGreg Roach            ->method('url')
563340ecd2SGreg Roach            ->willReturn('https://www.example.com');
573340ecd2SGreg Roach
5862ff2f18SGreg Roach        $individual_factory = $this->createMock(IndividualFactory::class);
593340ecd2SGreg Roach        $individual_factory
60*00ef1d3aSGreg Roach            ->expects($this->once())
613340ecd2SGreg Roach            ->method('make')
623340ecd2SGreg Roach            ->with('X123', $tree)
633340ecd2SGreg Roach            ->willReturn($individual);
643340ecd2SGreg Roach
653340ecd2SGreg Roach        Registry::individualFactory($individual_factory);
663340ecd2SGreg Roach
673340ecd2SGreg Roach        $handler = new RedirectIndividualPhp($tree_service);
683340ecd2SGreg Roach
69*00ef1d3aSGreg Roach        $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1', 'pid' => 'X123']);
703340ecd2SGreg Roach
713340ecd2SGreg Roach        $response = $handler->handle($request);
723340ecd2SGreg Roach
733340ecd2SGreg Roach        self::assertSame(StatusCodeInterface::STATUS_MOVED_PERMANENTLY, $response->getStatusCode());
743340ecd2SGreg Roach        self::assertSame('https://www.example.com', $response->getHeaderLine('Location'));
753340ecd2SGreg Roach    }
763340ecd2SGreg Roach
773340ecd2SGreg Roach    public function testNoSuchRecord(): void
783340ecd2SGreg Roach    {
7962ff2f18SGreg Roach        $tree = $this->createMock(Tree::class);
803340ecd2SGreg Roach
8162ff2f18SGreg Roach        $tree_service = $this->createMock(TreeService::class);
823340ecd2SGreg Roach        $tree_service
83*00ef1d3aSGreg Roach            ->expects($this->once())
843340ecd2SGreg Roach            ->method('all')
85*00ef1d3aSGreg Roach            ->willReturn(new Collection(['tree1' => $tree]));
863340ecd2SGreg Roach
873340ecd2SGreg Roach        $handler = new RedirectIndividualPhp($tree_service);
883340ecd2SGreg Roach
89*00ef1d3aSGreg Roach        $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1', 'pid' => 'X123']);
903340ecd2SGreg Roach
9115c4f62cSGreg Roach        $this->expectException(HttpGoneException::class);
923340ecd2SGreg Roach
933340ecd2SGreg Roach        $handler->handle($request);
943340ecd2SGreg Roach    }
953340ecd2SGreg Roach
963340ecd2SGreg Roach    public function testNoSuchTree(): void
973340ecd2SGreg Roach    {
9862ff2f18SGreg Roach        $tree_service = $this->createMock(TreeService::class);
993340ecd2SGreg Roach        $tree_service
100*00ef1d3aSGreg Roach            ->expects($this->once())
1013340ecd2SGreg Roach            ->method('all')
1023340ecd2SGreg Roach            ->willReturn(new Collection([]));
1033340ecd2SGreg Roach
1043340ecd2SGreg Roach        $handler = new RedirectIndividualPhp($tree_service);
1053340ecd2SGreg Roach
106*00ef1d3aSGreg Roach        $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1', 'pid' => 'X123']);
1073340ecd2SGreg Roach
10815c4f62cSGreg Roach        $this->expectException(HttpGoneException::class);
1093340ecd2SGreg Roach
1103340ecd2SGreg Roach        $handler->handle($request);
1113340ecd2SGreg Roach    }
1123340ecd2SGreg Roach
1133340ecd2SGreg Roach    public function testMissingXrefParameter(): void
1143340ecd2SGreg Roach    {
11562ff2f18SGreg Roach        $tree_service = $this->createMock(TreeService::class);
1163340ecd2SGreg Roach
1173340ecd2SGreg Roach        $handler = new RedirectFamilyPhp($tree_service);
1183340ecd2SGreg Roach
1193340ecd2SGreg Roach        $request = self::createRequest(RequestMethodInterface::METHOD_GET, ['ged' => 'tree1']);
1203340ecd2SGreg Roach
1213340ecd2SGreg Roach        $this->expectException(HttpBadRequestException::class);
1223340ecd2SGreg Roach
1233340ecd2SGreg Roach        $handler->handle($request);
1243340ecd2SGreg Roach    }
1253340ecd2SGreg Roach}
126